fixed WIN32 compatibility (use old connect function without IPv6 and bind support)
[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     }
189     
190     //send the IRC Headers
191     char sendBuf[512];
192     int len;
193     
194     if(client->pass && strcmp(client->pass, "")) {
195         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
196         write_socket(client, sendBuf, len);
197     }
198     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
199     write_socket(client, sendBuf, len);
200     len = sprintf(sendBuf, "NICK %s\n", client->nick);
201     write_socket(client, sendBuf, len);
202     
203     return 1;
204 }
205 #else
206 int connect_socket(struct ClientSocket *client) {
207     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
208     struct hostent *host;
209     struct sockaddr_in addr;
210     int sock;
211     addr.sin_addr.s_addr = inet_addr(client->host);
212     if (addr.sin_addr.s_addr == INADDR_NONE) {
213         host = gethostbyname(client->host);
214         if(!host) {
215             return SOCKET_ERROR;
216         }
217         memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
218     }
219     sock = socket(PF_INET, SOCK_STREAM, 0);
220     if (sock == -1)
221     {
222         perror("socket() failed");
223         return 0;
224     }
225
226     addr.sin_port = htons(client->port);
227     addr.sin_family = AF_INET;
228
229     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
230     {
231         perror("connect() failed");
232         return 0;
233     }
234
235     client->sock = sock;
236     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
237     client->connection_time = time(0);
238
239
240     if(client->flags & SOCKET_FLAG_SSL) {
241         ssl_connect(client);
242     }
243
244
245     //send the IRC Headers
246     char sendBuf[512];
247     int len;
248
249     if(client->pass && strcmp(client->pass, "")) {
250         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
251         write_socket(client, sendBuf, len);
252     }
253     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
254     write_socket(client, sendBuf, len);
255     len = sprintf(sendBuf, "NICK %s\n", client->nick);
256     write_socket(client, sendBuf, len);
257
258     return 1;
259 }
260 #endif
261
262 int close_socket(struct ClientSocket *client) {
263     if(client == NULL) return 0;
264     if((client->flags & SOCKET_FLAG_CONNECTED))
265         close(client->sock);
266     if(client->flags & SOCKET_FLAG_SSL)
267         ssl_disconnect(client);
268     struct ClientSocket *sock, *last_sock = NULL;
269     for (sock = sockets->data; sock; sock = sock->next) {
270         if(sock == client) {
271             if(last_sock)
272                 last_sock->next = sock->next;
273             else
274                 sockets->data = sock->next;
275             sockets->count--;
276         } else
277             last_sock = sock;
278     }
279     if(client->queue)
280         queue_destroy(client);
281     if(client->whoqueue_first)
282         clear_whoqueue(client);
283     if(client->handleinfo_first)
284         clear_handleinfoqueue(client);
285     free(client->host);
286     if(client->bind)
287         free(client->bind);
288     if(client->pass)
289         free(client->pass);
290     free(client);
291     return 1;
292 }
293
294 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
295     printf("[send %d] %s", len, msg);
296     if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) {
297         #ifdef WIN32
298         send(client->sock, msg, len, 0);
299         #else
300         write(client->sock, msg, len);
301         #endif
302     }
303     client->traffic_out += len;
304     return 1;
305 }
306
307 int write_socket(struct ClientSocket *client, char* msg, int len) {
308     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
309     if(client->flags & SOCKET_FLAG_USE_QUEUE)
310         return queue_add(client, msg, len);
311     else
312         return write_socket_force(client, msg, len);
313 }
314
315 void socket_loop(int timeout_seconds) {
316     if(sockets == NULL) return;
317     fd_set fds;
318     struct timeval timeout;
319     struct ClientSocket *sock;
320     int ret = 0, bytes, i;
321     
322     FD_ZERO(&fds);
323     for (sock = sockets->data; sock; sock = sock->next) {
324         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
325         FD_SET(sock->sock, &fds);
326         if(sock->sock > ret)
327             ret = sock->sock;
328     }
329     timeout.tv_sec = timeout_seconds;
330     timeout.tv_usec = 0;
331     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
332     if(ret == 0) return;
333     for (sock = sockets->data; sock; sock = sock->next) {
334         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
335             if(sock->bufferpos != 0) {
336                 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
337                     #ifdef WIN32
338                     bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
339                     #else
340                     bytes = read(sock->sock, buffer, sizeof(buffer));
341                     #endif
342                 }
343                 if(bytes > 0) {
344                     for(i = 0; i < bytes; i++) {
345                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
346                         sock->buffer[sock->bufferpos + i] = buffer[i];
347                     }
348                     sock->bufferpos += i;
349                 }
350             } else {
351                 if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
352                     #ifdef WIN32
353                     bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
354                     #else
355                     bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
356                     #endif
357                 }
358                 if(bytes > 0)
359                     sock->bufferpos = bytes;
360             }
361             if(bytes <= 0) {
362                 //error
363                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
364                 bot_disconnect(sock);
365                 if(sock->queue)
366                     queue_destroy(sock);
367                 close(sock->sock);
368                 if(sock->flags & SOCKET_FLAG_SSL)
369                     ssl_disconnect(sock);
370             } else {
371                 sock->traffic_in += bytes;
372                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
373                 if(used == sock->bufferpos + 1) {
374                     //used all bytes so just reset the bufferpos
375                     sock->bufferpos = 0;
376                 } else {
377                     for(i = 0; i < sock->bufferpos - used; i++) {
378                         sock->buffer[i] = sock->buffer[i+used];
379                     }
380                     sock->bufferpos -= used;
381                 }
382             }
383         } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
384             if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
385                 connect_socket(sock);
386             }
387         }
388     }
389 }
390
391 void
392 putsock(struct ClientSocket *client, const char *text, ...)
393 {
394     va_list arg_list;
395     char sendBuf[MAXLEN];
396     int pos;
397     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
398     sendBuf[0] = '\0';
399     va_start(arg_list, text);
400     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
401     va_end(arg_list);
402     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
403     sendBuf[pos] = '\n';
404     sendBuf[pos+1] = '\0';
405     write_socket(client, sendBuf, pos+1);
406 }
407
408 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
409     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
410     if(sock == NULL) return NULL;
411     for (; sock; sock = sock->next) {
412         if(!flags || (sock->flags & flags) == flags)
413             return sock;
414     }
415     return NULL;
416 }
417
418 void free_sockets() {
419     if(!sockets) return;
420     struct ClientSocket *client, *next;
421     for (client = sockets->data; client; client = next) {
422         next = client->next;
423         if((client->flags & SOCKET_FLAG_CONNECTED))
424             close(client->sock);
425         if(client->flags & SOCKET_FLAG_SSL)
426             ssl_disconnect(client);
427         if(client->queue)
428             queue_destroy(client);
429         free(client->host);
430         if(client->bind)
431             free(client->bind);
432         if(client->pass)
433             free(client->pass);
434         free(client);
435     }
436     free(sockets);
437     sockets = NULL;
438 }