]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
9bc2f09e38772b885c6b724dd2c57565edc44688
[user/henk/code/inspircd.git] / src / socket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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 "inspstring.h"
36 #include "helperfuncs.h"
37 #include "socketengine.h"
38
39
40 extern InspIRCd* ServerInstance;
41 extern time_t TIME;
42
43 InspSocket* socket_ref[MAX_DESCRIPTORS];
44
45 InspSocket::InspSocket()
46 {
47         this->state = I_DISCONNECTED;
48 }
49
50 InspSocket::InspSocket(int newfd, char* ip)
51 {
52         this->fd = newfd;
53         this->state = I_CONNECTED;
54         this->IP = ip;
55         ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
56         socket_ref[this->fd] = this;
57 }
58
59 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
60 {
61         if (listening) {
62                 if ((this->fd = OpenTCPSocket()) == ERROR)
63                 {
64                         this->fd = -1;
65                         this->state = I_ERROR;
66                         this->OnError(I_ERR_SOCKET);
67                         log(DEBUG,"OpenTCPSocket() error");
68                         return;
69                 }
70                 else
71                 {
72                         if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
73                         {
74                                 this->Close();
75                                 this->fd = -1;
76                                 this->state = I_ERROR;
77                                 this->OnError(I_ERR_BIND);
78                                 log(DEBUG,"BindSocket() error %s",strerror(errno));
79                                 return;
80                         }
81                         else
82                         {
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");
87                                 return;
88                         }
89                 }                       
90         }
91         else
92         {
93                 this->host = host;
94
95                 if (!inet_aton(host.c_str(),&addy))
96                 {
97                         /* Its not an ip, spawn the resolver */
98                         this->dns.ForwardLookupWithFD(host,fd);
99                         timeout_end = time(NULL)+maxtime;
100                         timeout = false;
101                         this->state = I_RESOLVING;
102                 }
103                 else
104                 {
105                         this->IP = host;
106                         this->DoConnect();
107                 }
108         }
109 }
110
111 bool InspSocket::DoResolve()
112 {
113         if (this->dns.HasResult())
114         {
115                 std::string res_ip = dns.GetResultIP();
116                 if (res_ip != "")
117                 {
118                         this->IP = res_ip;
119                 }
120                 else
121                 {
122                         this->Close();
123                         this->state = I_ERROR;
124                         this->OnError(I_ERR_RESOLVE);
125                         return false;
126                 }
127                 return this->DoConnect();
128         }
129         else return true;
130 }
131
132 bool InspSocket::DoConnect()
133 {
134         if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
135         {
136                 this->state = I_ERROR;
137                 this->OnError(I_ERR_SOCKET);
138                 return false;
139         }
140
141         this->port = port;
142         inet_aton(this->IP.c_str(),&addy);
143         addr.sin_family = AF_INET;
144         addr.sin_addr = addy;
145         addr.sin_port = htons(this->port);
146
147         int flags;
148         flags = fcntl(this->fd, F_GETFL, 0);
149         fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
150
151         if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
152         {
153                 if (errno != EINPROGRESS)
154                 {
155                         this->Close();
156                         this->OnError(I_ERR_CONNECT);
157                         this->state = I_ERROR;
158                         return false;
159                 }
160         }
161         this->state = I_CONNECTING;
162         ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
163         socket_ref[this->fd] = this;
164         return true;
165 }
166
167
168 void InspSocket::Close()
169 {
170         if (this->fd != -1)
171         {
172                 this->OnClose();
173                 shutdown(this->fd,2);
174                 close(this->fd);
175                 socket_ref[this->fd] = NULL;
176                 this->fd = -1;
177         }
178 }
179
180 std::string InspSocket::GetIP()
181 {
182         return this->IP;
183 }
184
185 char* InspSocket::Read()
186 {
187         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
188         if (n > 0)
189         {
190                 ibuf[n] = 0;
191                 return ibuf;
192         }
193         else
194         {
195                 if (n == EAGAIN)
196                 {
197                         return "";
198                 }
199                 else
200                 {
201                         log(DEBUG,"EOF or error on socket");
202                         return NULL;
203                 }
204         }
205 }
206
207 // There are two possible outcomes to this function.
208 // It will either write all of the data, or an undefined amount.
209 // If an undefined amount is written the connection has failed
210 // and should be aborted.
211 int InspSocket::Write(std::string data)
212 {
213         this->Buffer = this->Buffer + data;
214         this->FlushWriteBuffer();
215         return data.length();
216 }
217
218 void InspSocket::FlushWriteBuffer()
219 {
220         int result = 0;
221         if (this->Buffer.length())
222         {
223                 result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
224                 if (result > 0)
225                 {
226                         /* If we wrote some, advance the buffer forwards */
227                         char* n = (char*)this->Buffer.c_str();
228                         n += result;
229                         this->Buffer = n;
230                 }
231         }
232 }
233
234 bool InspSocket::Timeout(time_t current)
235 {
236         if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
237         {
238                 // for non-listening sockets, the timeout can occur
239                 // which causes termination of the connection after
240                 // the given number of seconds without a successful
241                 // connection.
242                 this->OnTimeout();
243                 this->OnError(I_ERR_TIMEOUT);
244                 timeout = true;
245                 this->state = I_ERROR;
246                 return true;
247         }
248         if (this->Buffer.length())
249                 this->FlushWriteBuffer();
250         return false;
251 }
252
253 bool InspSocket::Poll()
254 {
255         int incoming = -1;
256         
257         switch (this->state)
258         {
259                 case I_RESOLVING:
260                         return this->DoResolve();
261                 break;
262                 case I_CONNECTING:
263                         this->SetState(I_CONNECTED);
264                         /* Our socket was in write-state, so delete it and re-add it
265                          * in read-state.
266                          */
267                         ServerInstance->SE->DelFd(this->fd);
268                         ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
269                         return this->OnConnected();
270                 break;
271                 case I_LISTENING:
272                         length = sizeof (client);
273                         incoming = accept (this->fd, (sockaddr*)&client,&length);
274                         this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
275                         return true;
276                 break;
277                 case I_CONNECTED:
278                         return this->OnDataReady();
279                 break;
280                 default:
281                 break;
282         }
283
284         if (this->Buffer.length())
285                 this->FlushWriteBuffer();
286
287         return true;
288 }
289
290 void InspSocket::SetState(InspSocketState s)
291 {
292         log(DEBUG,"Socket state change");
293         this->state = s;
294 }
295
296 InspSocketState InspSocket::GetState()
297 {
298         return this->state;
299 }
300
301 int InspSocket::GetFd()
302 {
303         return this->fd;
304 }
305
306 bool InspSocket::OnConnected() { return true; }
307 void InspSocket::OnError(InspSocketError e) { return; }
308 int InspSocket::OnDisconnect() { return 0; }
309 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
310 bool InspSocket::OnDataReady() { return true; }
311 void InspSocket::OnTimeout() { return; }
312 void InspSocket::OnClose() { return; }
313
314 InspSocket::~InspSocket()
315 {
316         this->Close();
317 }
318