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(std::string host, int port, bool listening, unsigned long maxtime)
57 hostent* hoste = gethostbyname(host.c_str());
59 ip = (char*)host.c_str();
61 struct in_addr* ia = (in_addr*)hoste->h_addr;
65 timeout_end = time(NULL)+maxtime;
67 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
69 this->state = I_ERROR;
70 this->OnError(I_ERR_SOCKET);
75 addr.sin_family = AF_INET;
77 addr.sin_port = htons(this->port);
80 flags = fcntl(this->fd, F_GETFL, 0);
81 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
83 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
85 if (errno != EINPROGRESS)
88 this->OnError(I_ERR_CONNECT);
89 this->state = I_ERROR;
93 this->state = I_CONNECTING;
98 void InspSocket::Close()
103 shutdown(this->fd,2);
109 char* InspSocket::Read()
111 int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
122 // There are two possible outcomes to this function.
123 // It will either write all of the data, or an undefined amount.
124 // If an undefined amount is written the connection has failed
125 // and should be aborted.
126 int InspSocket::Write(std::string data)
128 char* d = (char*)data.c_str();
129 unsigned int written = 0;
131 int s = data.length();
132 while ((written < data.length()) && (n >= 0))
134 n = send(this->fd,d,s,0);
137 // If we didnt write everything, advance
138 // the pointers so that when we retry
139 // the next time around the loop, we try
140 // to write what we failed to write before.
149 bool InspSocket::Poll()
151 if (time(NULL) > timeout_end)
155 this->OnError(I_ERR_TIMEOUT);
157 this->state = I_ERROR;
161 state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
162 int ret = poll(&polls,1,1);
169 return this->OnConnected();
172 this->OnIncomingConnection();
175 return this->OnDataReady();
185 void InspSocket::SetState(InspSocketState s)
190 bool InspSocket::OnConnected() { return true; }
191 void InspSocket::OnError(InspSocketError e) { return; }
192 int InspSocket::OnDisconnect() { return 0; }
193 int InspSocket::OnIncomingConnection() { return 0; }
194 bool InspSocket::OnDataReady() { return true; }
195 void InspSocket::OnTimeout() { return; }
196 void InspSocket::OnClose() { return; }
198 InspSocket::~InspSocket()
204 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
205 int OpenTCPSocket (void)