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"
40 extern InspIRCd* ServerInstance;
43 InspSocket* socket_ref[65535];
45 InspSocket::InspSocket()
47 this->state = I_DISCONNECTED;
50 InspSocket::InspSocket(int newfd, char* ip)
53 this->state = I_CONNECTED;
55 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
56 socket_ref[this->fd] = this;
59 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
62 if ((this->fd = OpenTCPSocket()) == ERROR)
65 this->state = I_ERROR;
66 this->OnError(I_ERR_SOCKET);
67 log(DEBUG,"OpenTCPSocket() error");
72 if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
76 this->state = I_ERROR;
77 this->OnError(I_ERR_BIND);
78 log(DEBUG,"BindSocket() error %s",strerror(errno));
83 this->state = I_LISTENING;
84 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
85 socket_ref[this->fd] = this;
86 log(DEBUG,"New socket now in I_LISTENING state");
93 hostent* hoste = gethostbyname(host.c_str());
95 ip = (char*)host.c_str();
97 struct in_addr* ia = (in_addr*)hoste->h_addr;
103 timeout_end = time(NULL)+maxtime;
105 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
107 this->state = I_ERROR;
108 this->OnError(I_ERR_SOCKET);
113 addr.sin_family = AF_INET;
114 addr.sin_addr = addy;
115 addr.sin_port = htons(this->port);
118 flags = fcntl(this->fd, F_GETFL, 0);
119 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
121 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
123 if (errno != EINPROGRESS)
126 this->OnError(I_ERR_CONNECT);
127 this->state = I_ERROR;
131 this->state = I_CONNECTING;
132 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
133 socket_ref[this->fd] = this;
138 void InspSocket::Close()
143 shutdown(this->fd,2);
145 socket_ref[this->fd] = NULL;
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 this->Buffer = this->Buffer + data;
177 this->FlushWriteBuffer();
178 return data.length();
181 void InspSocket::FlushWriteBuffer()
184 if (this->Buffer.length())
186 result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
189 /* If we wrote some, advance the buffer forwards */
190 char* n = (char*)this->Buffer.c_str();
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;
211 if (this->Buffer.length())
212 this->FlushWriteBuffer();
216 bool InspSocket::Poll()
223 this->SetState(I_CONNECTED);
224 /* Our socket was in write-state, so delete it and re-add it
227 ServerInstance->SE->DelFd(this->fd);
228 ServerInstance->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)