]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Left figure from output
[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 #include "socketengine.h"
39
40 extern SocketEngine* SE;
41
42 extern FILE *log_file;
43 extern int boundPortCount;
44 extern int openSockfd[MAXSOCKS];
45 extern time_t TIME;
46 extern bool unlimitcore;
47 extern int MaxConn;
48
49 InspSocket* socket_ref[65535];
50
51 InspSocket::InspSocket()
52 {
53         this->state = I_DISCONNECTED;
54 }
55
56 InspSocket::InspSocket(int newfd, char* ip)
57 {
58         this->fd = newfd;
59         this->state = I_CONNECTED;
60         this->IP = ip;
61         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
62         socket_ref[this->fd] = this;
63 }
64
65 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
66 {
67         if (listening) {
68                 if ((this->fd = OpenTCPSocket()) == ERROR)
69                 {
70                         this->fd = -1;
71                         this->state = I_ERROR;
72                         this->OnError(I_ERR_SOCKET);
73                         log(DEBUG,"OpenTCPSocket() error");
74                         return;
75                 }
76                 else
77                 {
78                         if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
79                         {
80                                 this->Close();
81                                 this->fd = -1;
82                                 this->state = I_ERROR;
83                                 this->OnError(I_ERR_BIND);
84                                 log(DEBUG,"BindSocket() error %s",strerror(errno));
85                                 return;
86                         }
87                         else
88                         {
89                                 this->state = I_LISTENING;
90                                 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
91                                 socket_ref[this->fd] = this;
92                                 log(DEBUG,"New socket now in I_LISTENING state");
93                                 return;
94                         }
95                 }                       
96         } else {
97                 char* ip;
98                 this->host = host;
99                 hostent* hoste = gethostbyname(host.c_str());
100                 if (!hoste) {
101                         ip = (char*)host.c_str();
102                 } else {
103                         struct in_addr* ia = (in_addr*)hoste->h_addr;
104                         ip = inet_ntoa(*ia);
105                 }
106
107                 this->IP = ip;
108
109                 timeout_end = time(NULL)+maxtime;
110                 timeout = false;
111                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
112                 {
113                         this->state = I_ERROR;
114                         this->OnError(I_ERR_SOCKET);
115                         return;
116                 }
117                 this->port = port;
118                 inet_aton(ip,&addy);
119                 addr.sin_family = AF_INET;
120                 addr.sin_addr = addy;
121                 addr.sin_port = htons(this->port);
122
123                 int flags;
124                 flags = fcntl(this->fd, F_GETFL, 0);
125                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
126
127                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
128                 {
129                         if (errno != EINPROGRESS)
130                         {
131                                 this->Close();
132                                 this->OnError(I_ERR_CONNECT);
133                                 this->state = I_ERROR;
134                                 return;
135                         }
136                 }
137                 this->state = I_CONNECTING;
138                 SE->AddFd(this->fd,false,X_ESTAB_MODULE);
139                 socket_ref[this->fd] = this;
140                 return;
141         }
142 }
143
144 void InspSocket::Close()
145 {
146         if (this->fd != -1)
147         {
148                 this->OnClose();
149                 shutdown(this->fd,2);
150                 close(this->fd);
151                 socket_ref[this->fd] = NULL;
152                 this->fd = -1;
153         }
154 }
155
156 std::string InspSocket::GetIP()
157 {
158         return this->IP;
159 }
160
161 char* InspSocket::Read()
162 {
163         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
164         if (n > 0)
165         {
166                 ibuf[n] = 0;
167                 return ibuf;
168         }
169         else
170         {
171                 log(DEBUG,"EOF or error on socket");
172                 return NULL;
173         }
174 }
175
176 // There are two possible outcomes to this function.
177 // It will either write all of the data, or an undefined amount.
178 // If an undefined amount is written the connection has failed
179 // and should be aborted.
180 int InspSocket::Write(std::string data)
181 {
182         char* d = (char*)data.c_str();
183         unsigned int written = 0;
184         int n = 0;
185         int s = data.length();
186         while ((written < data.length()) && (n >= 0))
187         {
188                 n = send(this->fd,d,s,0);
189                 if (n > 0)
190                 {
191                         // If we didnt write everything, advance
192                         // the pointers so that when we retry
193                         // the next time around the loop, we try
194                         // to write what we failed to write before.
195                         written += n;
196                         s -= n;
197                         d += n;
198                 }
199         }
200         return written;
201 }
202
203 bool InspSocket::Timeout(time_t current)
204 {
205         if ((this->state == I_CONNECTING) && (current > timeout_end))
206         {
207                 // for non-listening sockets, the timeout can occur
208                 // which causes termination of the connection after
209                 // the given number of seconds without a successful
210                 // connection.
211                 this->OnTimeout();
212                 this->OnError(I_ERR_TIMEOUT);
213                 timeout = true;
214                 this->state = I_ERROR;
215                 return true;
216         }
217         return false;
218 }
219
220 bool InspSocket::Poll()
221 {
222         int incoming = -1;
223         
224         switch (this->state)
225         {
226                 case I_CONNECTING:
227                         this->SetState(I_CONNECTED);
228                         /* Our socket was in write-state, so delete it and re-add it
229                          * in read-state.
230                          */
231                         SE->DelFd(this->fd);
232                         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
233                         return this->OnConnected();
234                 break;
235                 case I_LISTENING:
236                         length = sizeof (client);
237                         incoming = accept (this->fd, (sockaddr*)&client,&length);
238                         this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
239                         return true;
240                 break;
241                 case I_CONNECTED:
242                         return this->OnDataReady();
243                 break;
244                 default:
245                 break;
246         }
247
248         return true;
249 }
250
251 void InspSocket::SetState(InspSocketState s)
252 {
253         log(DEBUG,"Socket state change");
254         this->state = s;
255 }
256
257 InspSocketState InspSocket::GetState()
258 {
259         return this->state;
260 }
261
262 int InspSocket::GetFd()
263 {
264         return this->fd;
265 }
266
267 bool InspSocket::OnConnected() { return true; }
268 void InspSocket::OnError(InspSocketError e) { return; }
269 int InspSocket::OnDisconnect() { return 0; }
270 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
271 bool InspSocket::OnDataReady() { return true; }
272 void InspSocket::OnTimeout() { return; }
273 void InspSocket::OnClose() { return; }
274
275 InspSocket::~InspSocket()
276 {
277         this->Close();
278 }
279
280 /*
281 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
282 int OpenTCPSocket (void)
283 */