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