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>
34 #include "inspircd_io.h"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
37 #include "socketengine.h"
40 extern InspIRCd* ServerInstance;
41 extern ServerConfig* Config;
44 InspSocket* socket_ref[MAX_DESCRIPTORS];
46 InspSocket::InspSocket()
48 this->state = I_DISCONNECTED;
52 InspSocket::InspSocket(int newfd, char* ip)
55 this->state = I_CONNECTED;
57 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
58 socket_ref[this->fd] = this;
61 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
65 if ((this->fd = OpenTCPSocket()) == ERROR)
68 this->state = I_ERROR;
69 this->OnError(I_ERR_SOCKET);
70 log(DEBUG,"OpenTCPSocket() error");
75 if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
79 this->state = I_ERROR;
80 this->OnError(I_ERR_BIND);
81 log(DEBUG,"BindSocket() error %s",strerror(errno));
86 this->state = I_LISTENING;
87 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
88 socket_ref[this->fd] = this;
89 log(DEBUG,"New socket now in I_LISTENING state");
99 if (!inet_aton(host.c_str(),&addy))
101 log(DEBUG,"Attempting to resolve %s",this->host.c_str());
102 /* Its not an ip, spawn the resolver */
103 this->dns.SetNS(std::string(Config->DNSServer));
104 this->dns.ForwardLookupWithFD(host,fd);
105 timeout_end = time(NULL)+maxtime;
107 this->state = I_RESOLVING;
108 socket_ref[this->fd] = this;
112 log(DEBUG,"No need to resolve %s",this->host.c_str());
119 void InspSocket::SetQueues(int nfd)
121 // attempt to increase socket sendq and recvq as high as its possible
124 setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
125 setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
128 bool InspSocket::DoResolve()
130 log(DEBUG,"In DoResolve(), trying to resolve IP");
131 if (this->dns.HasResult())
133 log(DEBUG,"Socket has result");
134 std::string res_ip = dns.GetResultIP();
137 log(DEBUG,"Socket result set to %s",res_ip.c_str());
139 socket_ref[this->fd] = NULL;
143 log(DEBUG,"Socket DNS failure");
145 this->state = I_ERROR;
146 this->OnError(I_ERR_RESOLVE);
149 return this->DoConnect();
151 log(DEBUG,"No result for socket yet!");
155 bool InspSocket::DoConnect()
157 log(DEBUG,"In DoConnect()");
158 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
160 log(DEBUG,"Cant socket()");
161 this->state = I_ERROR;
162 this->OnError(I_ERR_SOCKET);
166 log(DEBUG,"Part 2 DoConnect() %s",this->IP.c_str());
167 inet_aton(this->IP.c_str(),&addy);
168 addr.sin_family = AF_INET;
169 addr.sin_addr = addy;
170 addr.sin_port = htons(this->port);
173 flags = fcntl(this->fd, F_GETFL, 0);
174 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
176 if (connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
178 if (errno != EINPROGRESS)
180 log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
181 this->OnError(I_ERR_CONNECT);
182 this->state = I_ERROR;
187 this->state = I_CONNECTING;
188 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
189 socket_ref[this->fd] = this;
190 this->SetQueues(this->fd);
191 log(DEBUG,"Returning true from InspSocket::DoConnect");
196 void InspSocket::Close()
201 shutdown(this->fd,2);
203 socket_ref[this->fd] = NULL;
208 std::string InspSocket::GetIP()
213 char* InspSocket::Read()
215 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
217 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
218 if ((n > 0) && (n <= (int)sizeof(this->ibuf)))
231 log(DEBUG,"EOF or error on socket");
237 // There are two possible outcomes to this function.
238 // It will either write all of the data, or an undefined amount.
239 // If an undefined amount is written the connection has failed
240 // and should be aborted.
241 int InspSocket::Write(std::string data)
243 this->Buffer.append(data);
244 return data.length();
247 void InspSocket::FlushWriteBuffer()
250 if (this->Buffer.length())
252 result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
255 if (result == (int)this->Buffer.length())
261 /* If we wrote some, advance the buffer forwards */
262 char* n = (char*)this->Buffer.c_str();
270 bool InspSocket::Timeout(time_t current)
272 if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
274 // for non-listening sockets, the timeout can occur
275 // which causes termination of the connection after
276 // the given number of seconds without a successful
279 this->OnError(I_ERR_TIMEOUT);
281 this->state = I_ERROR;
284 this->FlushWriteBuffer();
288 bool InspSocket::Poll()
296 log(DEBUG,"State = I_RESOLVING, calling DoResolve()");
297 return this->DoResolve();
300 log(DEBUG,"State = I_CONNECTED");
301 this->SetState(I_CONNECTED);
302 /* Our socket was in write-state, so delete it and re-add it
305 ServerInstance->SE->DelFd(this->fd);
306 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
307 return this->OnConnected();
310 length = sizeof (client);
311 incoming = accept (this->fd, (sockaddr*)&client,&length);
312 this->SetQueues(incoming);
313 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
317 n = this->OnDataReady();
318 /* Flush any pending, but not till after theyre done with the event
319 * so there are less write calls involved. */
320 this->FlushWriteBuffer();
329 void InspSocket::SetState(InspSocketState s)
331 log(DEBUG,"Socket state change");
335 InspSocketState InspSocket::GetState()
340 int InspSocket::GetFd()
345 bool InspSocket::OnConnected() { return true; }
346 void InspSocket::OnError(InspSocketError e) { return; }
347 int InspSocket::OnDisconnect() { return 0; }
348 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
349 bool InspSocket::OnDataReady() { return true; }
350 void InspSocket::OnTimeout() { return; }
351 void InspSocket::OnClose() { return; }
353 InspSocket::~InspSocket()