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::InspSocket()
51 this->state = I_DISCONNECTED;
54 InspSocket::InspSocket(int newfd, char* ip)
57 this->state = I_CONNECTED;
59 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
62 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 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
88 log(DEBUG,"New socket now in I_LISTENING state");
95 hostent* hoste = gethostbyname(host.c_str());
97 ip = (char*)host.c_str();
99 struct in_addr* ia = (in_addr*)hoste->h_addr;
105 timeout_end = time(NULL)+maxtime;
107 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
109 this->state = I_ERROR;
110 this->OnError(I_ERR_SOCKET);
115 addr.sin_family = AF_INET;
116 addr.sin_addr = addy;
117 addr.sin_port = htons(this->port);
120 flags = fcntl(this->fd, F_GETFL, 0);
121 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
123 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
125 if (errno != EINPROGRESS)
128 this->OnError(I_ERR_CONNECT);
129 this->state = I_ERROR;
133 this->state = I_CONNECTING;
134 SE->AddFd(this->fd,false,X_ESTAB_MODULE);
139 void InspSocket::Close()
144 shutdown(this->fd,2);
150 std::string InspSocket::GetIP()
155 char* InspSocket::Read()
157 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
165 log(DEBUG,"EOF or error on socket");
170 // There are two possible outcomes to this function.
171 // It will either write all of the data, or an undefined amount.
172 // If an undefined amount is written the connection has failed
173 // and should be aborted.
174 int InspSocket::Write(std::string data)
176 char* d = (char*)data.c_str();
177 unsigned int written = 0;
179 int s = data.length();
180 while ((written < data.length()) && (n >= 0))
182 n = send(this->fd,d,s,0);
185 // If we didnt write everything, advance
186 // the pointers so that when we retry
187 // the next time around the loop, we try
188 // to write what we failed to write before.
197 bool InspSocket::Timeout(time_t current)
199 if ((this->state == I_CONNECTING) && (current > timeout_end))
201 // for non-listening sockets, the timeout can occur
202 // which causes termination of the connection after
203 // the given number of seconds without a successful
206 this->OnError(I_ERR_TIMEOUT);
208 this->state = I_ERROR;
214 bool InspSocket::Poll()
221 this->SetState(I_CONNECTED);
222 /* Our socket was in write-state, so delete it and re-add it
226 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
227 return this->OnConnected();
230 length = sizeof (client);
231 incoming = accept (this->fd, (sockaddr*)&client,&length);
232 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
236 return this->OnDataReady();
245 void InspSocket::SetState(InspSocketState s)
247 log(DEBUG,"Socket state change");
251 InspSocketState InspSocket::GetState()
256 int InspSocket::GetFd()
261 bool InspSocket::OnConnected() { return true; }
262 void InspSocket::OnError(InspSocketError e) { return; }
263 int InspSocket::OnDisconnect() { return 0; }
264 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
265 bool InspSocket::OnDataReady() { return true; }
266 void InspSocket::OnTimeout() { return; }
267 void InspSocket::OnClose() { return; }
269 InspSocket::~InspSocket()
275 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
276 int OpenTCPSocket (void)