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;
51 InspSocket::InspSocket(int newfd, char* ip)
54 this->state = I_CONNECTED;
56 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
57 socket_ref[this->fd] = this;
60 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
63 if ((this->fd = OpenTCPSocket()) == ERROR)
66 this->state = I_ERROR;
67 this->OnError(I_ERR_SOCKET);
68 log(DEBUG,"OpenTCPSocket() error");
73 if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
77 this->state = I_ERROR;
78 this->OnError(I_ERR_BIND);
79 log(DEBUG,"BindSocket() error %s",strerror(errno));
84 this->state = I_LISTENING;
85 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
86 socket_ref[this->fd] = this;
87 log(DEBUG,"New socket now in I_LISTENING state");
97 if (!inet_aton(host.c_str(),&addy))
99 log(DEBUG,"Attempting to resolve %s",this->host.c_str());
100 /* Its not an ip, spawn the resolver */
101 this->dns.SetNS(std::string(Config->DNSServer));
102 this->dns.ForwardLookupWithFD(host,fd);
103 timeout_end = time(NULL)+maxtime;
105 this->state = I_RESOLVING;
106 socket_ref[this->fd] = this;
110 log(DEBUG,"No need to resolve %s",this->host.c_str());
117 bool InspSocket::DoResolve()
119 log(DEBUG,"In DoResolve(), trying to resolve IP");
120 if (this->dns.HasResult())
122 log(DEBUG,"Socket has result");
123 std::string res_ip = dns.GetResultIP();
126 log(DEBUG,"Socket result set to %s",res_ip.c_str());
128 socket_ref[this->fd] = NULL;
132 log(DEBUG,"Socket DNS failure");
134 this->state = I_ERROR;
135 this->OnError(I_ERR_RESOLVE);
138 return this->DoConnect();
140 log(DEBUG,"No result for socket yet!");
144 bool InspSocket::DoConnect()
146 log(DEBUG,"In DoConnect()");
147 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
149 log(DEBUG,"Cant socket()");
150 this->state = I_ERROR;
151 this->OnError(I_ERR_SOCKET);
155 log(DEBUG,"Part 2 DoConnect() %s",this->IP.c_str());
156 inet_aton(this->IP.c_str(),&addy);
157 addr.sin_family = AF_INET;
158 addr.sin_addr = addy;
159 addr.sin_port = htons(this->port);
162 flags = fcntl(this->fd, F_GETFL, 0);
163 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
165 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
167 if (errno != EINPROGRESS)
169 log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
170 this->OnError(I_ERR_CONNECT);
171 this->state = I_ERROR;
176 this->state = I_CONNECTING;
177 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
178 socket_ref[this->fd] = this;
183 void InspSocket::Close()
188 shutdown(this->fd,2);
190 socket_ref[this->fd] = NULL;
195 std::string InspSocket::GetIP()
200 char* InspSocket::Read()
202 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
216 log(DEBUG,"EOF or error on socket");
222 // There are two possible outcomes to this function.
223 // It will either write all of the data, or an undefined amount.
224 // If an undefined amount is written the connection has failed
225 // and should be aborted.
226 int InspSocket::Write(std::string data)
228 this->Buffer = this->Buffer + data;
229 this->FlushWriteBuffer();
230 return data.length();
233 void InspSocket::FlushWriteBuffer()
236 if (this->Buffer.length())
238 result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
241 /* If we wrote some, advance the buffer forwards */
242 char* n = (char*)this->Buffer.c_str();
249 bool InspSocket::Timeout(time_t current)
251 if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
253 // for non-listening sockets, the timeout can occur
254 // which causes termination of the connection after
255 // the given number of seconds without a successful
258 this->OnError(I_ERR_TIMEOUT);
260 this->state = I_ERROR;
263 if (this->Buffer.length())
264 this->FlushWriteBuffer();
268 bool InspSocket::Poll()
275 log(DEBUG,"State = I_RESOLVING, calling DoResolve()");
276 return this->DoResolve();
279 log(DEBUG,"State = I_CONNECTED");
280 this->SetState(I_CONNECTED);
281 /* Our socket was in write-state, so delete it and re-add it
284 ServerInstance->SE->DelFd(this->fd);
285 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
286 return this->OnConnected();
289 length = sizeof (client);
290 incoming = accept (this->fd, (sockaddr*)&client,&length);
291 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
295 return this->OnDataReady();
301 if (this->Buffer.length())
302 this->FlushWriteBuffer();
307 void InspSocket::SetState(InspSocketState s)
309 log(DEBUG,"Socket state change");
313 InspSocketState InspSocket::GetState()
318 int InspSocket::GetFd()
323 bool InspSocket::OnConnected() { return true; }
324 void InspSocket::OnError(InspSocketError e) { return; }
325 int InspSocket::OnDisconnect() { return 0; }
326 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
327 bool InspSocket::OnDataReady() { return true; }
328 void InspSocket::OnTimeout() { return; }
329 void InspSocket::OnClose() { return; }
331 InspSocket::~InspSocket()