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"
39 extern FILE *log_file;
40 extern int boundPortCount;
41 extern int openSockfd[MAXSOCKS];
43 extern bool unlimitcore;
46 InspSocket::InspSocket()
48 this->state = I_DISCONNECTED;
51 InspSocket::InspSocket(int newfd, char* ip)
54 this->state = I_CONNECTED;
58 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
61 if ((this->fd = OpenTCPSocket()) == ERROR)
64 this->state = I_ERROR;
65 this->OnError(I_ERR_SOCKET);
66 log(DEBUG,"OpenTCPSocket() error");
71 if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
75 this->state = I_ERROR;
76 this->OnError(I_ERR_BIND);
77 log(DEBUG,"BindSocket() error %s",strerror(errno));
82 this->state = I_LISTENING;
83 log(DEBUG,"New socket now in I_LISTENING state");
90 hostent* hoste = gethostbyname(host.c_str());
92 ip = (char*)host.c_str();
94 struct in_addr* ia = (in_addr*)hoste->h_addr;
100 timeout_end = time(NULL)+maxtime;
102 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
104 this->state = I_ERROR;
105 this->OnError(I_ERR_SOCKET);
110 addr.sin_family = AF_INET;
111 addr.sin_addr = addy;
112 addr.sin_port = htons(this->port);
115 flags = fcntl(this->fd, F_GETFL, 0);
116 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
118 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
120 if (errno != EINPROGRESS)
123 this->OnError(I_ERR_CONNECT);
124 this->state = I_ERROR;
128 this->state = I_CONNECTING;
133 void InspSocket::Close()
138 shutdown(this->fd,2);
144 std::string InspSocket::GetIP()
149 char* InspSocket::Read()
151 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
159 log(DEBUG,"EOF or error on socket");
164 // There are two possible outcomes to this function.
165 // It will either write all of the data, or an undefined amount.
166 // If an undefined amount is written the connection has failed
167 // and should be aborted.
168 int InspSocket::Write(std::string data)
170 char* d = (char*)data.c_str();
171 unsigned int written = 0;
173 int s = data.length();
174 while ((written < data.length()) && (n >= 0))
176 n = send(this->fd,d,s,0);
179 // If we didnt write everything, advance
180 // the pointers so that when we retry
181 // the next time around the loop, we try
182 // to write what we failed to write before.
191 bool InspSocket::Poll()
193 if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
195 // for non-listening sockets, the timeout can occur
196 // which causes termination of the connection after
197 // the given number of seconds without a successful
200 this->OnError(I_ERR_TIMEOUT);
202 this->state = I_ERROR;
206 state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
207 int ret = poll(&polls,1,1);
216 this->SetState(I_CONNECTED);
217 return this->OnConnected();
220 length = sizeof (client);
221 incoming = accept (this->fd, (sockaddr*)&client,&length);
222 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
226 return this->OnDataReady();
235 void InspSocket::SetState(InspSocketState s)
237 log(DEBUG,"Socket state change");
241 InspSocketState InspSocket::GetState()
246 bool InspSocket::OnConnected() { return true; }
247 void InspSocket::OnError(InspSocketError e) { return; }
248 int InspSocket::OnDisconnect() { return 0; }
249 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
250 bool InspSocket::OnDataReady() { return true; }
251 void InspSocket::OnTimeout() { return; }
252 void InspSocket::OnClose() { return; }
254 InspSocket::~InspSocket()
260 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
261 int OpenTCPSocket (void)