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 "inspstring.h"
36 #include "helperfuncs.h"
37 #include "socketengine.h"
39 extern SocketEngine* SE;
41 extern int boundPortCount;
42 extern int openSockfd[MAXSOCKS];
45 InspSocket* socket_ref[65535];
47 InspSocket::InspSocket()
49 this->state = I_DISCONNECTED;
52 InspSocket::InspSocket(int newfd, char* ip)
55 this->state = I_CONNECTED;
57 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
58 socket_ref[this->fd] = this;
61 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
64 if ((this->fd = OpenTCPSocket()) == ERROR)
67 this->state = I_ERROR;
68 this->OnError(I_ERR_SOCKET);
69 log(DEBUG,"OpenTCPSocket() error");
74 if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
78 this->state = I_ERROR;
79 this->OnError(I_ERR_BIND);
80 log(DEBUG,"BindSocket() error %s",strerror(errno));
85 this->state = I_LISTENING;
86 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
87 socket_ref[this->fd] = this;
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);
135 socket_ref[this->fd] = this;
140 void InspSocket::Close()
145 shutdown(this->fd,2);
147 socket_ref[this->fd] = NULL;
152 std::string InspSocket::GetIP()
157 char* InspSocket::Read()
159 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
167 log(DEBUG,"EOF or error on socket");
172 // There are two possible outcomes to this function.
173 // It will either write all of the data, or an undefined amount.
174 // If an undefined amount is written the connection has failed
175 // and should be aborted.
176 int InspSocket::Write(std::string data)
178 char* d = (char*)data.c_str();
179 unsigned int written = 0;
181 int s = data.length();
182 while ((written < data.length()) && (n >= 0))
184 n = send(this->fd,d,s,0);
187 // If we didnt write everything, advance
188 // the pointers so that when we retry
189 // the next time around the loop, we try
190 // to write what we failed to write before.
199 bool InspSocket::Timeout(time_t current)
201 if ((this->state == I_CONNECTING) && (current > timeout_end))
203 // for non-listening sockets, the timeout can occur
204 // which causes termination of the connection after
205 // the given number of seconds without a successful
208 this->OnError(I_ERR_TIMEOUT);
210 this->state = I_ERROR;
216 bool InspSocket::Poll()
223 this->SetState(I_CONNECTED);
224 /* Our socket was in write-state, so delete it and re-add it
228 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
229 return this->OnConnected();
232 length = sizeof (client);
233 incoming = accept (this->fd, (sockaddr*)&client,&length);
234 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
238 return this->OnDataReady();
247 void InspSocket::SetState(InspSocketState s)
249 log(DEBUG,"Socket state change");
253 InspSocketState InspSocket::GetState()
258 int InspSocket::GetFd()
263 bool InspSocket::OnConnected() { return true; }
264 void InspSocket::OnError(InspSocketError e) { return; }
265 int InspSocket::OnDisconnect() { return 0; }
266 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
267 bool InspSocket::OnDataReady() { return true; }
268 void InspSocket::OnTimeout() { return; }
269 void InspSocket::OnClose() { return; }
271 InspSocket::~InspSocket()
277 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
278 int OpenTCPSocket (void)