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)
54 this->state = I_CONNECTED;
57 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
60 if ((this->fd = OpenTCPSocket()) == ERROR)
63 this->state = I_ERROR;
64 this->OnError(I_ERR_SOCKET);
65 log(DEBUG,"OpenTCPSocket() error");
70 if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
74 this->state = I_ERROR;
75 this->OnError(I_ERR_BIND);
76 log(DEBUG,"BindSocket() error %s",strerror(errno));
81 this->state = I_LISTENING;
82 log(DEBUG,"New socket now in I_LISTENING state");
89 hostent* hoste = gethostbyname(host.c_str());
91 ip = (char*)host.c_str();
93 struct in_addr* ia = (in_addr*)hoste->h_addr;
97 timeout_end = time(NULL)+maxtime;
99 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
101 this->state = I_ERROR;
102 this->OnError(I_ERR_SOCKET);
107 addr.sin_family = AF_INET;
108 addr.sin_addr = addy;
109 addr.sin_port = htons(this->port);
112 flags = fcntl(this->fd, F_GETFL, 0);
113 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
115 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
117 if (errno != EINPROGRESS)
120 this->OnError(I_ERR_CONNECT);
121 this->state = I_ERROR;
125 this->state = I_CONNECTING;
130 void InspSocket::Close()
135 shutdown(this->fd,2);
141 char* InspSocket::Read()
143 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
150 log(DEBUG,"EOF or error on socket");
155 // There are two possible outcomes to this function.
156 // It will either write all of the data, or an undefined amount.
157 // If an undefined amount is written the connection has failed
158 // and should be aborted.
159 int InspSocket::Write(std::string data)
161 char* d = (char*)data.c_str();
162 unsigned int written = 0;
164 int s = data.length();
165 while ((written < data.length()) && (n >= 0))
167 n = send(this->fd,d,s,0);
170 // If we didnt write everything, advance
171 // the pointers so that when we retry
172 // the next time around the loop, we try
173 // to write what we failed to write before.
182 bool InspSocket::Poll()
184 if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
186 // for non-listening sockets, the timeout can occur
187 // which causes termination of the connection after
188 // the given number of seconds without a successful
191 this->OnError(I_ERR_TIMEOUT);
193 this->state = I_ERROR;
197 state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
198 int ret = poll(&polls,1,1);
207 this->SetState(I_CONNECTED);
208 return this->OnConnected();
211 length = sizeof (client);
212 incoming = accept (this->fd, (sockaddr*)&client,&length);
213 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
217 return this->OnDataReady();
226 void InspSocket::SetState(InspSocketState s)
228 log(DEBUG,"Socket state change");
232 InspSocketState InspSocket::GetState()
237 bool InspSocket::OnConnected() { return true; }
238 void InspSocket::OnError(InspSocketError e) { return; }
239 int InspSocket::OnDisconnect() { return 0; }
240 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
241 bool InspSocket::OnDataReady() { return true; }
242 void InspSocket::OnTimeout() { return; }
243 void InspSocket::OnClose() { return; }
245 InspSocket::~InspSocket()
251 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
252 int OpenTCPSocket (void)