2e2eb1c703fca717a79ab4c87e33f856d7539ef2
[NeonServV5.git] / src / ClientSocket.c
1 /* ClientSocket.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "ClientSocket.h"
19 #include "IRCParser.h"
20 #include "UserNode.h"
21 #include "IRCQueue.h"
22 #include "WHOHandler.h"
23 #include "HandleInfoHandler.h"
24 #include "ssl.h"
25 #include "ConfigParser.h"
26
27 struct socket_list {
28     struct ClientSocket *data;
29     unsigned count;
30 };
31
32 //the magic list :P
33 static struct socket_list *sockets = NULL;
34 static char buffer[BUF_SIZ];
35
36 static void init_sockets() {
37     sockets = malloc(sizeof(*sockets));
38     if (!sockets)
39     {
40         perror("malloc() failed");
41         return;
42     }
43     sockets->data = NULL;
44     sockets->count = 0;
45 }
46
47 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
48     if(sockets == NULL) init_sockets();
49     struct ClientSocket *client = malloc(sizeof(*client));
50     if (!client)
51     {
52         perror("malloc() failed");
53         return NULL;
54     }
55     client->host = strdup(host);
56     client->port = port;
57     client->bind = (bindto ? strdup(bindto) : NULL);
58     client->pass = (pass == NULL ? NULL : strdup(pass));
59     client->nick = strdup(nick);
60     client->ident = strdup(ident);
61     client->realname = strdup(realname);
62     client->user = NULL;
63     client->flags = 0;
64     client->bufferpos = 0;
65     client->traffic_in = 0;
66     client->traffic_out = 0;
67     client->connection_time = 0;
68         client->botid = 0;
69     client->clientid = 0;
70     client->queue = NULL;
71     client->whoqueue_first = NULL;
72     client->whoqueue_last = NULL;
73     client->handleinfo_first = NULL;
74     client->handleinfo_last = NULL;
75     client->next = sockets->data;
76     sockets->data = client;
77     return client;
78 }
79
80 #ifndef WIN32
81 int connect_socket(struct ClientSocket *client) {
82     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
83     int sock;
84     
85     struct addrinfo hints, *res;
86     struct sockaddr_in *ip4 = NULL;
87     struct sockaddr_in6 *ip6 = NULL;
88     memset (&hints, 0, sizeof (hints));
89     hints.ai_family = PF_UNSPEC;
90     hints.ai_socktype = SOCK_STREAM;
91     hints.ai_flags |= AI_CANONNAME;
92     if (getaddrinfo (client->host, NULL, &hints, &res)) {
93         return 0;
94     }
95     while (res) {
96         switch (res->ai_family) {
97         case AF_INET:
98             ip4 = (struct sockaddr_in *) res->ai_addr;
99             break;
100         case AF_INET6:
101             ip6 = (struct sockaddr_in6 *) res->ai_addr;
102             break;
103         }
104         res = res->ai_next;
105     }
106     
107     if(ip6) {
108         sock = socket(AF_INET6, SOCK_STREAM, 0);
109         if(sock == -1) {
110             perror("socket() failed");
111             return 0;
112         }
113         
114         ip6->sin6_family = AF_INET6;
115         ip6->sin6_port = htons(client->port);
116         
117         struct sockaddr_in6 *ip6vhost = NULL;
118         if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
119             while (res) {
120                 switch (res->ai_family) {
121                 case AF_INET6:
122                     ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
123                     break;
124                 }
125                 res = res->ai_next;
126             }
127         }
128         if(ip6vhost) {
129             ip6vhost->sin6_family = AF_INET6;
130             ip6vhost->sin6_port = htons(0);
131             bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
132         }
133         
134         if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
135             perror("connect() failed");
136             return 0;
137         }
138         
139     } else if(ip4) {
140         sock = socket(AF_INET, SOCK_STREAM, 0);
141         if(sock == -1) {
142             perror("socket() failed");
143             return 0;
144         }
145         
146         ip4->sin_family = AF_INET;
147         ip4->sin_port = htons(client->port);
148         
149         struct sockaddr_in *ip4vhost = NULL;
150         if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
151             while (res) {
152                 switch (res->ai_family) {
153                 case AF_INET:
154                     ip4vhost = (struct sockaddr_in *) res->ai_addr;
155                     break;
156                 }
157                 res = res->ai_next;
158             }
159         }
160         if(ip4vhost) {
161             ip4vhost->sin_family = AF_INET;
162             ip4vhost->sin_port = htons(0);
163             bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
164         }
165         
166         if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
167             perror("connect() failed");
168             return 0;
169         }
170         
171     } else
172         return 0;
173     
174     if(get_int_field("Sockets.NoDelay")) {
175         int flag = 1;
176         if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
177             perror("setsockopt() failed");
178             return 0;
179         }
180     }
181     
182     client->sock = sock;
183     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
184     client->connection_time = time(0);
185     
186     if(client->flags & SOCKET_FLAG_SSL) {
187         ssl_connect(client);
188         client->flags |= SOCKET_FLAG_HAVE_SSL;
189     }
190     
191     //send the IRC Headers
192     char sendBuf[512];
193     int len;
194     
195     if(client->pass && strcmp(client->pass, "")) {
196         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
197         write_socket(client, sendBuf, len);
198     }
199     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
200     write_socket(client, sendBuf, len);
201     len = sprintf(sendBuf, "NICK %s\n", client->nick);
202     write_socket(client, sendBuf, len);
203     
204     return 1;
205 }
206 #else
207 int connect_socket(struct ClientSocket *client) {
208     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
209     struct hostent *host;
210     struct sockaddr_in addr;
211     int sock;
212     addr.sin_addr.s_addr = inet_addr(client->host);
213     if (addr.sin_addr.s_addr == INADDR_NONE) {
214         host = gethostbyname(client->host);
215         if(!host) {
216             return SOCKET_ERROR;
217         }
218         memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
219     }
220     sock = socket(PF_INET, SOCK_STREAM, 0);
221     if (sock == -1)
222     {
223         perror("socket() failed");
224         return 0;
225     }
226
227     addr.sin_port = htons(client->port);
228     addr.sin_family = AF_INET;
229
230     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
231     {
232         perror("connect() failed");
233         return 0;
234     }
235
236     client->sock = sock;
237     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
238     client->connection_time = time(0);
239
240
241     if(client->flags & SOCKET_FLAG_SSL) {
242         ssl_connect(client);
243         client->flags |= SOCKET_FLAG_HAVE_SSL;
244     }
245
246
247     //send the IRC Headers
248     char sendBuf[512];
249     int len;
250
251     if(client->pass && strcmp(client->pass, "")) {
252         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
253         write_socket(client, sendBuf, len);
254     }
255     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
256     write_socket(client, sendBuf, len);
257     len = sprintf(sendBuf, "NICK %s\n", client->nick);
258     write_socket(client, sendBuf, len);
259
260     return 1;
261 }
262 #endif
263
264 int close_socket(struct ClientSocket *client) {
265     if(client == NULL) return 0;
266     if((client->flags & SOCKET_FLAG_CONNECTED))
267         close(client->sock);
268     if(client->flags & SOCKET_FLAG_HAVE_SSL)
269         ssl_disconnect(client);
270     struct ClientSocket *sock, *last_sock = NULL;
271     for (sock = sockets->data; sock; sock = sock->next) {
272         if(sock == client) {
273             if(last_sock)
274                 last_sock->next = sock->next;
275             else
276                 sockets->data = sock->next;
277             sockets->count--;
278         } else
279             last_sock = sock;
280     }
281     if(client->queue)
282         queue_destroy(client);
283     if(client->whoqueue_first)
284         clear_whoqueue(client);
285     if(client->handleinfo_first)
286         clear_handleinfoqueue(client);
287     free(client->host);
288     if(client->bind)
289         free(client->bind);
290     if(client->pass)
291         free(client->pass);
292     free(client);
293     return 1;
294 }
295
296 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
297     printf("[send %d] %s", len, msg);
298     if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) {
299         #ifdef WIN32
300         send(client->sock, msg, len, 0);
301         #else
302         write(client->sock, msg, len);
303         #endif
304     }
305     client->traffic_out += len;
306     return 1;
307 }
308
309 int write_socket(struct ClientSocket *client, char* msg, int len) {
310     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
311     if(client->flags & SOCKET_FLAG_USE_QUEUE)
312         return queue_add(client, msg, len);
313     else
314         return write_socket_force(client, msg, len);
315 }
316
317 void socket_loop(int timeout_seconds) {
318     if(sockets == NULL) return;
319     fd_set fds;
320     struct timeval timeout;
321     struct ClientSocket *sock;
322     int ret = 0, bytes, i;
323     
324     FD_ZERO(&fds);
325     for (sock = sockets->data; sock; sock = sock->next) {
326         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
327         FD_SET(sock->sock, &fds);
328         if(sock->sock > ret)
329             ret = sock->sock;
330     }
331     timeout.tv_sec = timeout_seconds;
332     timeout.tv_usec = 0;
333     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
334     if(ret == 0) return;
335     for (sock = sockets->data; sock; sock = sock->next) {
336         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
337             if(sock->bufferpos != 0) {
338                 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
339                     #ifdef WIN32
340                     bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
341                     #else
342                     bytes = read(sock->sock, buffer, sizeof(buffer));
343                     #endif
344                 }
345                 if(bytes > 0) {
346                     for(i = 0; i < bytes; i++) {
347                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
348                         sock->buffer[sock->bufferpos + i] = buffer[i];
349                     }
350                     sock->bufferpos += i;
351                 }
352             } else {
353                 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
354                     #ifdef WIN32
355                     bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
356                     #else
357                     bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
358                     #endif
359                 }
360                 if(bytes > 0)
361                     sock->bufferpos = bytes;
362             }
363             if(bytes <= 0) {
364                 //error
365                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
366                 bot_disconnect(sock);
367                 if(sock->queue)
368                     queue_destroy(sock);
369                 close(sock->sock);
370                 if(sock->flags & SOCKET_FLAG_HAVE_SSL)
371                     ssl_disconnect(sock);
372             } else {
373                 sock->traffic_in += bytes;
374                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
375                 if(used == sock->bufferpos + 1) {
376                     //used all bytes so just reset the bufferpos
377                     sock->bufferpos = 0;
378                 } else {
379                     for(i = 0; i < sock->bufferpos - used; i++) {
380                         sock->buffer[i] = sock->buffer[i+used];
381                     }
382                     sock->bufferpos -= used;
383                 }
384             }
385         } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
386             if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
387                 connect_socket(sock);
388             }
389         }
390     }
391 }
392
393 void
394 putsock(struct ClientSocket *client, const char *text, ...)
395 {
396     va_list arg_list;
397     char sendBuf[MAXLEN];
398     int pos;
399     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
400     sendBuf[0] = '\0';
401     va_start(arg_list, text);
402     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
403     va_end(arg_list);
404     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
405     sendBuf[pos] = '\n';
406     sendBuf[pos+1] = '\0';
407     write_socket(client, sendBuf, pos+1);
408 }
409
410 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
411     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
412     if(sock == NULL) return NULL;
413     for (; sock; sock = sock->next) {
414         if(!flags || (sock->flags & flags) == flags)
415             return sock;
416     }
417     return NULL;
418 }
419
420 void free_sockets() {
421     if(!sockets) return;
422     struct ClientSocket *client, *next;
423     for (client = sockets->data; client; client = next) {
424         next = client->next;
425         if((client->flags & SOCKET_FLAG_CONNECTED))
426             close(client->sock);
427         if(client->flags & SOCKET_FLAG_HAVE_SSL)
428             ssl_disconnect(client);
429         if(client->queue)
430             queue_destroy(client);
431         free(client->host);
432         if(client->bind)
433             free(client->bind);
434         if(client->pass)
435             free(client->pass);
436         free(client);
437     }
438     free(sockets);
439     sockets = NULL;
440 }