added cmd_restart cmd_reload and cmd_die; added Socket.NoDelay setting to configurati...
[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 int connect_socket(struct ClientSocket *client) {
81     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
82     int sock;
83     
84     struct addrinfo hints, *res;
85     struct sockaddr_in *ip4 = NULL;
86     struct sockaddr_in6 *ip6 = NULL;
87     memset (&hints, 0, sizeof (hints));
88     hints.ai_family = PF_UNSPEC;
89     hints.ai_socktype = SOCK_STREAM;
90     hints.ai_flags |= AI_CANONNAME;
91     if (getaddrinfo (client->host, NULL, &hints, &res)) {
92         return 0;
93     }
94     while (res) {
95         switch (res->ai_family) {
96         case AF_INET:
97             ip4 = (struct sockaddr_in *) res->ai_addr;
98             break;
99         case AF_INET6:
100             ip6 = (struct sockaddr_in6 *) res->ai_addr;
101             break;
102         }
103         res = res->ai_next;
104     }
105     
106     if(ip6) {
107         sock = socket(AF_INET6, SOCK_STREAM, 0);
108         if(sock == -1) {
109             perror("socket() failed");
110             return 0;
111         }
112         
113         ip6->sin6_family = AF_INET6;
114         ip6->sin6_port = htons(client->port);
115         
116         struct sockaddr_in6 *ip6vhost = NULL;
117         if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
118             while (res) {
119                 switch (res->ai_family) {
120                 case AF_INET6:
121                     ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
122                     break;
123                 }
124                 res = res->ai_next;
125             }
126         }
127         if(ip6vhost) {
128             ip6vhost->sin6_family = AF_INET6;
129             ip6vhost->sin6_port = htons(0);
130             bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
131         }
132         
133         if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
134             perror("connect() failed");
135             return 0;
136         }
137         
138     } else if(ip4) {
139         sock = socket(AF_INET, SOCK_STREAM, 0);
140         if(sock == -1) {
141             perror("socket() failed");
142             return 0;
143         }
144         
145         ip4->sin_family = AF_INET;
146         ip4->sin_port = htons(client->port);
147         
148         struct sockaddr_in *ip4vhost = NULL;
149         if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
150             while (res) {
151                 switch (res->ai_family) {
152                 case AF_INET:
153                     ip4vhost = (struct sockaddr_in *) res->ai_addr;
154                     break;
155                 }
156                 res = res->ai_next;
157             }
158         }
159         if(ip4vhost) {
160             ip4vhost->sin_family = AF_INET;
161             ip4vhost->sin_port = htons(0);
162             bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
163         }
164         
165         if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
166             perror("connect() failed");
167             return 0;
168         }
169         
170     } else
171         return 0;
172     
173     if(get_int_field("Sockets.NoDelay")) {
174         int flag = 1;
175         if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
176             perror("setsockopt() failed");
177             return 0;
178         }
179     }
180     
181     client->sock = sock;
182     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
183     client->connection_time = time(0);
184     
185     if(client->flags & SOCKET_FLAG_SSL) {
186         ssl_connect(client);
187     }
188     
189     //send the IRC Headers
190     char sendBuf[512];
191     int len;
192     
193     if(client->pass && strcmp(client->pass, "")) {
194         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
195         write_socket(client, sendBuf, len);
196     }
197     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
198     write_socket(client, sendBuf, len);
199     len = sprintf(sendBuf, "NICK %s\n", client->nick);
200     write_socket(client, sendBuf, len);
201     
202     return 1;
203 }
204
205 int close_socket(struct ClientSocket *client) {
206     if(client == NULL) return 0;
207     if((client->flags & SOCKET_FLAG_CONNECTED))
208         close(client->sock);
209     if(client->flags & SOCKET_FLAG_SSL)
210         ssl_disconnect(client);
211     struct ClientSocket *sock, *last_sock = NULL;
212     for (sock = sockets->data; sock; sock = sock->next) {
213         if(sock == client) {
214             if(last_sock)
215                 last_sock->next = sock->next;
216             else
217                 sockets->data = sock->next;
218             sockets->count--;
219         } else
220             last_sock = sock;
221     }
222     if(client->queue)
223         queue_destroy(client);
224     if(client->whoqueue_first)
225         clear_whoqueue(client);
226     if(client->handleinfo_first)
227         clear_handleinfoqueue(client);
228     free(client->host);
229     if(client->bind)
230         free(client->bind);
231     if(client->pass)
232         free(client->pass);
233     free(client);
234     return 1;
235 }
236
237 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
238     printf("[send %d] %s", len, msg);
239     if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) {
240         #ifdef WIN32
241         send(client->sock, msg, len, 0);
242         #else
243         write(client->sock, msg, len);
244         #endif
245     }
246     client->traffic_out += len;
247     return 1;
248 }
249
250 int write_socket(struct ClientSocket *client, char* msg, int len) {
251     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
252     if(client->flags & SOCKET_FLAG_USE_QUEUE)
253         return queue_add(client, msg, len);
254     else
255         return write_socket_force(client, msg, len);
256 }
257
258 void socket_loop(int timeout_seconds) {
259     if(sockets == NULL) return;
260     fd_set fds;
261     struct timeval timeout;
262     struct ClientSocket *sock;
263     int ret = 0, bytes, i;
264     
265     FD_ZERO(&fds);
266     for (sock = sockets->data; sock; sock = sock->next) {
267         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
268         FD_SET(sock->sock, &fds);
269         if(sock->sock > ret)
270             ret = sock->sock;
271     }
272     timeout.tv_sec = timeout_seconds;
273     timeout.tv_usec = 0;
274     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
275     if(ret == 0) return;
276     for (sock = sockets->data; sock; sock = sock->next) {
277         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
278             if(sock->bufferpos != 0) {
279                 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
280                     #ifdef WIN32
281                     bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
282                     #else
283                     bytes = read(sock->sock, buffer, sizeof(buffer));
284                     #endif
285                 }
286                 if(bytes > 0) {
287                     for(i = 0; i < bytes; i++) {
288                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
289                         sock->buffer[sock->bufferpos + i] = buffer[i];
290                     }
291                     sock->bufferpos += i;
292                 }
293             } else {
294                 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
295                     #ifdef WIN32
296                     bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
297                     #else
298                     bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
299                     #endif
300                 }
301                 if(bytes > 0)
302                     sock->bufferpos = bytes;
303             }
304             if(bytes <= 0) {
305                 //error
306                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
307                 bot_disconnect(sock);
308                 if(sock->queue)
309                     queue_destroy(sock);
310                 close(sock->sock);
311                 if(sock->flags & SOCKET_FLAG_SSL)
312                     ssl_disconnect(sock);
313             } else {
314                 sock->traffic_in += bytes;
315                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
316                 if(used == sock->bufferpos + 1) {
317                     //used all bytes so just reset the bufferpos
318                     sock->bufferpos = 0;
319                 } else {
320                     for(i = 0; i < sock->bufferpos - used; i++) {
321                         sock->buffer[i] = sock->buffer[i+used];
322                     }
323                     sock->bufferpos -= used;
324                 }
325             }
326         } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
327             if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
328                 connect_socket(sock);
329             }
330         }
331     }
332 }
333
334 void
335 putsock(struct ClientSocket *client, const char *text, ...)
336 {
337     va_list arg_list;
338     char sendBuf[MAXLEN];
339     int pos;
340     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
341     sendBuf[0] = '\0';
342     va_start(arg_list, text);
343     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
344     va_end(arg_list);
345     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
346     sendBuf[pos] = '\n';
347     sendBuf[pos+1] = '\0';
348     write_socket(client, sendBuf, pos+1);
349 }
350
351 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
352     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
353     if(sock == NULL) return NULL;
354     for (; sock; sock = sock->next) {
355         if(!flags || (sock->flags & flags) == flags)
356             return sock;
357     }
358     return NULL;
359 }
360
361 void free_sockets() {
362     if(!sockets) return;
363     struct ClientSocket *client, *next;
364     for (client = sockets->data; client; client = next) {
365         next = client->next;
366         if((client->flags & SOCKET_FLAG_CONNECTED))
367             close(client->sock);
368         if(client->flags & SOCKET_FLAG_SSL)
369             ssl_disconnect(client);
370         if(client->queue)
371             queue_destroy(client);
372         free(client->host);
373         if(client->bind)
374             free(client->bind);
375         if(client->pass)
376             free(client->pass);
377         free(client);
378     }
379     free(sockets);
380     sockets = NULL;
381 }