1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
35 #include "inspircd_io.h"
36 #include "inspstring.h"
37 #include "helperfuncs.h"
38 #include "socketengine.h"
41 extern InspIRCd* ServerInstance;
42 extern ServerConfig* Config;
45 InspSocket* socket_ref[MAX_DESCRIPTORS];
47 InspSocket::InspSocket()
49 this->state = I_DISCONNECTED;
53 InspSocket::InspSocket(int newfd, char* ip)
56 this->state = I_CONNECTED;
58 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
59 socket_ref[this->fd] = this;
62 InspSocket::InspSocket(std::string ahost, int aport, bool listening, unsigned long maxtime)
66 this->outbuffer.clear();
68 if ((this->fd = OpenTCPSocket()) == ERROR)
71 this->state = I_ERROR;
72 this->OnError(I_ERR_SOCKET);
73 log(DEBUG,"OpenTCPSocket() error");
78 if (BindSocket(this->fd,this->client,this->server,aport,(char*)ahost.c_str()) == ERROR)
82 this->state = I_ERROR;
83 this->OnError(I_ERR_BIND);
84 log(DEBUG,"BindSocket() error %s",strerror(errno));
89 this->state = I_LISTENING;
90 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
91 socket_ref[this->fd] = this;
92 log(DEBUG,"New socket now in I_LISTENING state");
102 if (!inet_aton(host.c_str(),&addy))
104 log(DEBUG,"Attempting to resolve %s",this->host.c_str());
105 /* Its not an ip, spawn the resolver */
106 this->dns.SetNS(std::string(Config->DNSServer));
107 this->dns.ForwardLookupWithFD(host,fd);
108 timeout_end = time(NULL) + maxtime;
110 this->state = I_RESOLVING;
111 socket_ref[this->fd] = this;
115 log(DEBUG,"No need to resolve %s",this->host.c_str());
117 timeout_end = time(NULL) + maxtime;
123 void InspSocket::SetQueues(int nfd)
125 // attempt to increase socket sendq and recvq as high as its possible
128 setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
129 setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
132 bool InspSocket::DoResolve()
134 log(DEBUG,"In DoResolve(), trying to resolve IP");
135 if (this->dns.HasResult())
137 log(DEBUG,"Socket has result");
138 std::string res_ip = dns.GetResultIP();
141 log(DEBUG,"Socket result set to %s",res_ip.c_str());
143 socket_ref[this->fd] = NULL;
147 log(DEBUG,"Socket DNS failure");
149 this->state = I_ERROR;
150 this->OnError(I_ERR_RESOLVE);
153 return this->DoConnect();
155 log(DEBUG,"No result for socket yet!");
159 bool InspSocket::DoConnect()
161 log(DEBUG,"In DoConnect()");
162 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
164 log(DEBUG,"Cant socket()");
165 this->state = I_ERROR;
166 this->OnError(I_ERR_SOCKET);
170 log(DEBUG,"Part 2 DoConnect() %s",this->IP.c_str());
171 inet_aton(this->IP.c_str(),&addy);
172 addr.sin_family = AF_INET;
173 addr.sin_addr = addy;
174 addr.sin_port = htons(this->port);
177 flags = fcntl(this->fd, F_GETFL, 0);
178 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
180 if (connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
182 if (errno != EINPROGRESS)
184 log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
185 this->OnError(I_ERR_CONNECT);
186 this->state = I_ERROR;
191 this->state = I_CONNECTING;
192 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
193 socket_ref[this->fd] = this;
194 this->SetQueues(this->fd);
195 log(DEBUG,"Returning true from InspSocket::DoConnect");
200 void InspSocket::Close()
205 shutdown(this->fd,2);
207 socket_ref[this->fd] = NULL;
212 std::string InspSocket::GetIP()
217 char* InspSocket::Read()
219 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
221 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
222 if ((n > 0) && (n <= (int)sizeof(this->ibuf)))
235 log(DEBUG,"EOF or error on socket: %s",strerror(errno));
241 // There are two possible outcomes to this function.
242 // It will either write all of the data, or an undefined amount.
243 // If an undefined amount is written the connection has failed
244 // and should be aborted.
245 int InspSocket::Write(std::string data)
247 /* Try and append the data to the back of the queue, and send it on its way
249 outbuffer.push_back(data);
250 return (!this->FlushWriteBuffer());
253 bool InspSocket::FlushWriteBuffer()
255 if ((this->fd > -1) && (this->state == I_CONNECTED))
257 if (outbuffer.size())
259 int result = write(this->fd,outbuffer[0].c_str(),outbuffer[0].length());
262 if ((unsigned int)result == outbuffer[0].length())
264 /* The whole block was written (usually a line)
265 * Pop the block off the front of the queue
267 outbuffer.pop_front();
271 outbuffer[0] = outbuffer[0].substr(result + 1,outbuffer[0].length());
274 else if ((result == -1) && (errno != EAGAIN))
276 log(DEBUG,"Write error on socket: %s",strerror(errno));
277 this->OnError(I_ERR_WRITE);
278 this->state = I_ERROR;
286 bool InspSocket::Timeout(time_t current)
288 if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
290 log(DEBUG,"Timed out, current=%lu timeout_end=%lu");
291 // for non-listening sockets, the timeout can occur
292 // which causes termination of the connection after
293 // the given number of seconds without a successful
296 this->OnError(I_ERR_TIMEOUT);
298 this->state = I_ERROR;
301 return this->FlushWriteBuffer();
304 bool InspSocket::Poll()
312 log(DEBUG,"State = I_RESOLVING, calling DoResolve()");
313 return this->DoResolve();
316 log(DEBUG,"State = I_CONNECTING");
317 this->SetState(I_CONNECTED);
318 /* Our socket was in write-state, so delete it and re-add it
321 ServerInstance->SE->DelFd(this->fd);
322 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
323 return this->OnConnected();
326 length = sizeof (client);
327 incoming = accept (this->fd, (sockaddr*)&client,&length);
328 this->SetQueues(incoming);
329 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
333 n = this->OnDataReady();
334 /* Flush any pending, but not till after theyre done with the event
335 * so there are less write calls involved.
336 * Both FlushWriteBuffer AND the return result of OnDataReady must
337 * return true for this to be ok.
339 return (n && !this->FlushWriteBuffer());
347 void InspSocket::SetState(InspSocketState s)
349 log(DEBUG,"Socket state change");
353 InspSocketState InspSocket::GetState()
358 int InspSocket::GetFd()
363 bool InspSocket::OnConnected() { return true; }
364 void InspSocket::OnError(InspSocketError e) { return; }
365 int InspSocket::OnDisconnect() { return 0; }
366 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
367 bool InspSocket::OnDataReady() { return true; }
368 void InspSocket::OnTimeout() { return; }
369 void InspSocket::OnClose() { return; }
371 InspSocket::~InspSocket()