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