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);
151 log(DEBUG,"EOF or error on socket");
156 // There are two possible outcomes to this function.
157 // It will either write all of the data, or an undefined amount.
158 // If an undefined amount is written the connection has failed
159 // and should be aborted.
160 int InspSocket::Write(std::string data)
162 char* d = (char*)data.c_str();
163 unsigned int written = 0;
165 int s = data.length();
166 while ((written < data.length()) && (n >= 0))
168 n = send(this->fd,d,s,0);
171 // If we didnt write everything, advance
172 // the pointers so that when we retry
173 // the next time around the loop, we try
174 // to write what we failed to write before.
183 bool InspSocket::Poll()
185 if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
187 // for non-listening sockets, the timeout can occur
188 // which causes termination of the connection after
189 // the given number of seconds without a successful
192 this->OnError(I_ERR_TIMEOUT);
194 this->state = I_ERROR;
198 state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
199 int ret = poll(&polls,1,1);
208 this->SetState(I_CONNECTED);
209 return this->OnConnected();
212 length = sizeof (client);
213 incoming = accept (this->fd, (sockaddr*)&client,&length);
214 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
218 return this->OnDataReady();
227 void InspSocket::SetState(InspSocketState s)
229 log(DEBUG,"Socket state change");
233 InspSocketState InspSocket::GetState()
238 bool InspSocket::OnConnected() { return true; }
239 void InspSocket::OnError(InspSocketError e) { return; }
240 int InspSocket::OnDisconnect() { return 0; }
241 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
242 bool InspSocket::OnDataReady() { return true; }
243 void InspSocket::OnTimeout() { return; }
244 void InspSocket::OnClose() { return; }
246 InspSocket::~InspSocket()
252 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
253 int OpenTCPSocket (void)