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 int boundPortCount;
43 extern int openSockfd[MAXSOCKS];
46 InspSocket* socket_ref[65535];
48 InspSocket::InspSocket()
50 this->state = I_DISCONNECTED;
53 InspSocket::InspSocket(int newfd, char* ip)
56 this->state = I_CONNECTED;
58 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
59 socket_ref[this->fd] = this;
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 socket_ref[this->fd] = this;
89 log(DEBUG,"New socket now in I_LISTENING state");
96 hostent* hoste = gethostbyname(host.c_str());
98 ip = (char*)host.c_str();
100 struct in_addr* ia = (in_addr*)hoste->h_addr;
106 timeout_end = time(NULL)+maxtime;
108 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
110 this->state = I_ERROR;
111 this->OnError(I_ERR_SOCKET);
116 addr.sin_family = AF_INET;
117 addr.sin_addr = addy;
118 addr.sin_port = htons(this->port);
121 flags = fcntl(this->fd, F_GETFL, 0);
122 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
124 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
126 if (errno != EINPROGRESS)
129 this->OnError(I_ERR_CONNECT);
130 this->state = I_ERROR;
134 this->state = I_CONNECTING;
135 SE->AddFd(this->fd,false,X_ESTAB_MODULE);
136 socket_ref[this->fd] = this;
141 void InspSocket::Close()
146 shutdown(this->fd,2);
148 socket_ref[this->fd] = NULL;
153 std::string InspSocket::GetIP()
158 char* InspSocket::Read()
160 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
168 log(DEBUG,"EOF or error on socket");
173 // There are two possible outcomes to this function.
174 // It will either write all of the data, or an undefined amount.
175 // If an undefined amount is written the connection has failed
176 // and should be aborted.
177 int InspSocket::Write(std::string data)
179 char* d = (char*)data.c_str();
180 unsigned int written = 0;
182 int s = data.length();
183 while ((written < data.length()) && (n >= 0))
185 n = send(this->fd,d,s,0);
188 // If we didnt write everything, advance
189 // the pointers so that when we retry
190 // the next time around the loop, we try
191 // to write what we failed to write before.
200 bool InspSocket::Timeout(time_t current)
202 if ((this->state == I_CONNECTING) && (current > timeout_end))
204 // for non-listening sockets, the timeout can occur
205 // which causes termination of the connection after
206 // the given number of seconds without a successful
209 this->OnError(I_ERR_TIMEOUT);
211 this->state = I_ERROR;
217 bool InspSocket::Poll()
224 this->SetState(I_CONNECTED);
225 /* Our socket was in write-state, so delete it and re-add it
229 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
230 return this->OnConnected();
233 length = sizeof (client);
234 incoming = accept (this->fd, (sockaddr*)&client,&length);
235 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
239 return this->OnDataReady();
248 void InspSocket::SetState(InspSocketState s)
250 log(DEBUG,"Socket state change");
254 InspSocketState InspSocket::GetState()
259 int InspSocket::GetFd()
264 bool InspSocket::OnConnected() { return true; }
265 void InspSocket::OnError(InspSocketError e) { return; }
266 int InspSocket::OnDisconnect() { return 0; }
267 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
268 bool InspSocket::OnDataReady() { return true; }
269 void InspSocket::OnTimeout() { return; }
270 void InspSocket::OnClose() { return; }
272 InspSocket::~InspSocket()
278 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
279 int OpenTCPSocket (void)