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 void InspSocket::SetQueues(int nfd)
119 // attempt to increase socket sendq and recvq as high as its possible
122 setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
123 setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
126 bool InspSocket::DoResolve()
128 log(DEBUG,"In DoResolve(), trying to resolve IP");
129 if (this->dns.HasResult())
131 log(DEBUG,"Socket has result");
132 std::string res_ip = dns.GetResultIP();
135 log(DEBUG,"Socket result set to %s",res_ip.c_str());
137 socket_ref[this->fd] = NULL;
141 log(DEBUG,"Socket DNS failure");
143 this->state = I_ERROR;
144 this->OnError(I_ERR_RESOLVE);
147 return this->DoConnect();
149 log(DEBUG,"No result for socket yet!");
153 bool InspSocket::DoConnect()
155 log(DEBUG,"In DoConnect()");
156 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
158 log(DEBUG,"Cant socket()");
159 this->state = I_ERROR;
160 this->OnError(I_ERR_SOCKET);
164 log(DEBUG,"Part 2 DoConnect() %s",this->IP.c_str());
165 inet_aton(this->IP.c_str(),&addy);
166 addr.sin_family = AF_INET;
167 addr.sin_addr = addy;
168 addr.sin_port = htons(this->port);
171 flags = fcntl(this->fd, F_GETFL, 0);
172 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
174 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
176 if (errno != EINPROGRESS)
178 log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
179 this->OnError(I_ERR_CONNECT);
180 this->state = I_ERROR;
185 this->state = I_CONNECTING;
186 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
187 socket_ref[this->fd] = this;
188 this->SetQueues(this->fd);
193 void InspSocket::Close()
198 shutdown(this->fd,2);
200 socket_ref[this->fd] = NULL;
205 std::string InspSocket::GetIP()
210 char* InspSocket::Read()
212 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
226 log(DEBUG,"EOF or error on socket");
232 // There are two possible outcomes to this function.
233 // It will either write all of the data, or an undefined amount.
234 // If an undefined amount is written the connection has failed
235 // and should be aborted.
236 int InspSocket::Write(std::string data)
238 this->Buffer.append(data);
239 return data.length();
242 void InspSocket::FlushWriteBuffer()
245 if (this->Buffer.length())
247 result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
250 if (result == (int)this->Buffer.length())
256 /* If we wrote some, advance the buffer forwards */
257 char* n = (char*)this->Buffer.c_str();
265 bool InspSocket::Timeout(time_t current)
267 if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
269 // for non-listening sockets, the timeout can occur
270 // which causes termination of the connection after
271 // the given number of seconds without a successful
274 this->OnError(I_ERR_TIMEOUT);
276 this->state = I_ERROR;
279 this->FlushWriteBuffer();
283 bool InspSocket::Poll()
291 log(DEBUG,"State = I_RESOLVING, calling DoResolve()");
292 return this->DoResolve();
295 log(DEBUG,"State = I_CONNECTED");
296 this->SetState(I_CONNECTED);
297 /* Our socket was in write-state, so delete it and re-add it
300 ServerInstance->SE->DelFd(this->fd);
301 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
302 return this->OnConnected();
305 length = sizeof (client);
306 incoming = accept (this->fd, (sockaddr*)&client,&length);
307 this->SetQueues(incoming);
308 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
312 n = this->OnDataReady();
313 /* Flush any pending, but not till after theyre done with the event
314 * so there are less write calls involved. */
315 this->FlushWriteBuffer();
324 void InspSocket::SetState(InspSocketState s)
326 log(DEBUG,"Socket state change");
330 InspSocketState InspSocket::GetState()
335 int InspSocket::GetFd()
340 bool InspSocket::OnConnected() { return true; }
341 void InspSocket::OnError(InspSocketError e) { return; }
342 int InspSocket::OnDisconnect() { return 0; }
343 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
344 bool InspSocket::OnDataReady() { return true; }
345 void InspSocket::OnTimeout() { return; }
346 void InspSocket::OnClose() { return; }
348 InspSocket::~InspSocket()