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)
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");
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)
249 if ((data != "") && (this->Buffer.length() + data.length() < this->Buffer.max_size()))
250 this->Buffer.append(data);
252 catch (std::length_error)
254 log(DEBUG,"std::length_error exception caught while appending to socket buffer!");
257 return data.length();
260 void InspSocket::FlushWriteBuffer()
262 if ((this->fd > -1) && (this->state == I_CONNECTED))
264 int result = 0, v = 0;
265 const char* n = Buffer.c_str();
269 result = write(this->fd,n,v);
278 /* If we wrote some, advance the buffer forwards */
287 bool InspSocket::Timeout(time_t current)
289 if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
291 log(DEBUG,"Timed out, current=%lu timeout_end=%lu");
292 // for non-listening sockets, the timeout can occur
293 // which causes termination of the connection after
294 // the given number of seconds without a successful
297 this->OnError(I_ERR_TIMEOUT);
299 this->state = I_ERROR;
302 this->FlushWriteBuffer();
306 bool InspSocket::Poll()
314 log(DEBUG,"State = I_RESOLVING, calling DoResolve()");
315 return this->DoResolve();
318 log(DEBUG,"State = I_CONNECTING");
319 this->SetState(I_CONNECTED);
320 /* Our socket was in write-state, so delete it and re-add it
323 ServerInstance->SE->DelFd(this->fd);
324 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
325 return this->OnConnected();
328 length = sizeof (client);
329 incoming = accept (this->fd, (sockaddr*)&client,&length);
330 this->SetQueues(incoming);
331 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
335 n = this->OnDataReady();
336 /* Flush any pending, but not till after theyre done with the event
337 * so there are less write calls involved. */
338 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()