1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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 "inspircd_util.h"
36 #include "inspstring.h"
37 #include "helperfuncs.h"
38 #include "socketengine.h"
40 extern SocketEngine* SE;
42 extern FILE *log_file;
43 extern int boundPortCount;
44 extern int openSockfd[MAXSOCKS];
46 extern bool unlimitcore;
49 InspSocket* socket_ref[65535];
51 InspSocket::InspSocket()
53 this->state = I_DISCONNECTED;
56 InspSocket::InspSocket(int newfd, char* ip)
59 this->state = I_CONNECTED;
61 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
62 socket_ref[this->fd] = this;
65 InspSocket::InspSocket(std::string host, int port, 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,port,(char*)host.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 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
91 socket_ref[this->fd] = this;
92 log(DEBUG,"New socket now in I_LISTENING state");
99 hostent* hoste = gethostbyname(host.c_str());
101 ip = (char*)host.c_str();
103 struct in_addr* ia = (in_addr*)hoste->h_addr;
109 timeout_end = time(NULL)+maxtime;
111 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
113 this->state = I_ERROR;
114 this->OnError(I_ERR_SOCKET);
119 addr.sin_family = AF_INET;
120 addr.sin_addr = addy;
121 addr.sin_port = htons(this->port);
124 flags = fcntl(this->fd, F_GETFL, 0);
125 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
127 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
129 if (errno != EINPROGRESS)
132 this->OnError(I_ERR_CONNECT);
133 this->state = I_ERROR;
137 this->state = I_CONNECTING;
138 SE->AddFd(this->fd,false,X_ESTAB_MODULE);
139 socket_ref[this->fd] = this;
144 void InspSocket::Close()
149 shutdown(this->fd,2);
151 socket_ref[this->fd] = NULL;
156 std::string InspSocket::GetIP()
161 char* InspSocket::Read()
163 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
171 log(DEBUG,"EOF or error on socket");
176 // There are two possible outcomes to this function.
177 // It will either write all of the data, or an undefined amount.
178 // If an undefined amount is written the connection has failed
179 // and should be aborted.
180 int InspSocket::Write(std::string data)
182 char* d = (char*)data.c_str();
183 unsigned int written = 0;
185 int s = data.length();
186 while ((written < data.length()) && (n >= 0))
188 n = send(this->fd,d,s,0);
191 // If we didnt write everything, advance
192 // the pointers so that when we retry
193 // the next time around the loop, we try
194 // to write what we failed to write before.
203 bool InspSocket::Timeout(time_t current)
205 if ((this->state == I_CONNECTING) && (current > timeout_end))
207 // for non-listening sockets, the timeout can occur
208 // which causes termination of the connection after
209 // the given number of seconds without a successful
212 this->OnError(I_ERR_TIMEOUT);
214 this->state = I_ERROR;
220 bool InspSocket::Poll()
227 this->SetState(I_CONNECTED);
228 /* Our socket was in write-state, so delete it and re-add it
232 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
233 return this->OnConnected();
236 length = sizeof (client);
237 incoming = accept (this->fd, (sockaddr*)&client,&length);
238 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
242 return this->OnDataReady();
251 void InspSocket::SetState(InspSocketState s)
253 log(DEBUG,"Socket state change");
257 InspSocketState InspSocket::GetState()
262 int InspSocket::GetFd()
267 bool InspSocket::OnConnected() { return true; }
268 void InspSocket::OnError(InspSocketError e) { return; }
269 int InspSocket::OnDisconnect() { return 0; }
270 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
271 bool InspSocket::OnDataReady() { return true; }
272 void InspSocket::OnTimeout() { return; }
273 void InspSocket::OnClose() { return; }
275 InspSocket::~InspSocket()
281 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
282 int OpenTCPSocket (void)