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