]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Made compile on linux
[user/henk/code/inspircd.git] / src / socket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <string>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <sstream>
30 #include <iostream>
31 #include <fstream>
32 #include "socket.h"
33 #include "inspircd.h"
34 #include "inspircd_io.h"
35 #include "inspircd_util.h"
36 #include "inspstring.h"
37 #include "helperfuncs.h"
38
39 extern FILE *log_file;
40 extern int boundPortCount;
41 extern int openSockfd[MAXSOCKS];
42 extern time_t TIME;
43 extern bool unlimitcore;
44 extern int MaxConn;
45
46 InspSocket::InspSocket()
47 {
48         this->state = I_DISCONNECTED;
49 }
50
51 InspSocket::InspSocket(int newfd, char* ip)
52 {
53         this->fd = newfd;
54         this->state = I_CONNECTED;
55         this->IP = ip;
56 }
57
58 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
59 {
60         if (listening) {
61                 if ((this->fd = OpenTCPSocket()) == ERROR)
62                 {
63                         this->fd = -1;
64                         this->state = I_ERROR;
65                         this->OnError(I_ERR_SOCKET);
66                         log(DEBUG,"OpenTCPSocket() error");
67                         return;
68                 }
69                 else
70                 {
71                         if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
72                         {
73                                 this->Close();
74                                 this->fd = -1;
75                                 this->state = I_ERROR;
76                                 this->OnError(I_ERR_BIND);
77                                 log(DEBUG,"BindSocket() error %s",strerror(errno));
78                                 return;
79                         }
80                         else
81                         {
82                                 this->state = I_LISTENING;
83                                 log(DEBUG,"New socket now in I_LISTENING state");
84                                 return;
85                         }
86                 }                       
87         } else {
88                 char* ip;
89                 this->host = host;
90                 hostent* hoste = gethostbyname(host.c_str());
91                 if (!hoste) {
92                         ip = (char*)host.c_str();
93                 } else {
94                         struct in_addr* ia = (in_addr*)hoste->h_addr;
95                         ip = inet_ntoa(*ia);
96                 }
97
98                 this->IP = ip;
99
100                 timeout_end = time(NULL)+maxtime;
101                 timeout = false;
102                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
103                 {
104                         this->state = I_ERROR;
105                         this->OnError(I_ERR_SOCKET);
106                         return;
107                 }
108                 this->port = port;
109                 inet_aton(ip,&addy);
110                 addr.sin_family = AF_INET;
111                 addr.sin_addr = addy;
112                 addr.sin_port = htons(this->port);
113
114                 int flags;
115                 flags = fcntl(this->fd, F_GETFL, 0);
116                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
117
118                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
119                 {
120                         if (errno != EINPROGRESS)
121                         {
122                                 this->Close();
123                                 this->OnError(I_ERR_CONNECT);
124                                 this->state = I_ERROR;
125                                 return;
126                         }
127                 }
128                 this->state = I_CONNECTING;
129                 return;
130         }
131 }
132
133 void InspSocket::Close()
134 {
135         if (this->fd != -1)
136         {
137                 this->OnClose();
138                 shutdown(this->fd,2);
139                 close(this->fd);
140                 this->fd = -1;
141         }
142 }
143
144 std::string InspSocket::GetIP()
145 {
146         return this->IP;
147 }
148
149 char* InspSocket::Read()
150 {
151         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
152         if (n > 0)
153         {
154                 ibuf[n] = 0;
155                 return ibuf;
156         }
157         else
158         {
159                 log(DEBUG,"EOF or error on socket");
160                 return NULL;
161         }
162 }
163
164 // There are two possible outcomes to this function.
165 // It will either write all of the data, or an undefined amount.
166 // If an undefined amount is written the connection has failed
167 // and should be aborted.
168 int InspSocket::Write(std::string data)
169 {
170         char* d = (char*)data.c_str();
171         unsigned int written = 0;
172         int n = 0;
173         int s = data.length();
174         while ((written < data.length()) && (n >= 0))
175         {
176                 n = send(this->fd,d,s,0);
177                 if (n > 0)
178                 {
179                         // If we didnt write everything, advance
180                         // the pointers so that when we retry
181                         // the next time around the loop, we try
182                         // to write what we failed to write before.
183                         written += n;
184                         s -= n;
185                         d += n;
186                 }
187         }
188         return written;
189 }
190
191 bool InspSocket::Poll()
192 {
193         if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
194         {
195                 // for non-listening sockets, the timeout can occur
196                 // which causes termination of the connection after
197                 // the given number of seconds without a successful
198                 // connection.
199                 this->OnTimeout();
200                 this->OnError(I_ERR_TIMEOUT);
201                 timeout = true;
202                 this->state = I_ERROR;
203                 return false;
204         }
205         polls.fd = this->fd;
206         state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
207         int ret = poll(&polls,1,1);
208
209         if (ret > 0)
210         {
211                 int incoming = -1;
212                 
213                 switch (this->state)
214                 {
215                         case I_CONNECTING:
216                                 this->SetState(I_CONNECTED);
217                                 return this->OnConnected();
218                         break;
219                         case I_LISTENING:
220                                 length = sizeof (client);
221                                 incoming = accept (this->fd, (sockaddr*)&client,&length);
222                                 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
223                                 return true;
224                         break;
225                         case I_CONNECTED:
226                                 return this->OnDataReady();
227                         break;
228                         default:
229                         break;
230                 }
231         }
232         return true;
233 }
234
235 void InspSocket::SetState(InspSocketState s)
236 {
237         log(DEBUG,"Socket state change");
238         this->state = s;
239 }
240
241 InspSocketState InspSocket::GetState()
242 {
243         return this->state;
244 }
245
246 bool InspSocket::OnConnected() { return true; }
247 void InspSocket::OnError(InspSocketError e) { return; }
248 int InspSocket::OnDisconnect() { return 0; }
249 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
250 bool InspSocket::OnDataReady() { return true; }
251 void InspSocket::OnTimeout() { return; }
252 void InspSocket::OnClose() { return; }
253
254 InspSocket::~InspSocket()
255 {
256         this->Close();
257 }
258
259 /*
260 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
261 int OpenTCPSocket (void)
262 */