1 /* ClientSocket.c - NeonServ v5.4
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "ClientSocket.h"
19 #include "IRCParser.h"
22 #include "WHOHandler.h"
23 #include "HandleInfoHandler.h"
25 #include "ConfigParser.h"
29 struct ClientSocket *data;
34 static pthread_mutex_t synchronized;
35 static pthread_mutex_t synchronized_recv;
39 struct ParseOrder *next;
41 struct ParseOrder *parse_order = NULL;
45 static struct socket_list *sockets = NULL;
46 static char buffer[BUF_SIZ];
49 THREAD_MUTEX_INIT(synchronized);
50 THREAD_MUTEX_INIT(synchronized_recv);
52 sockets = malloc(sizeof(*sockets));
55 perror("malloc() failed");
62 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
63 struct ClientSocket *client = malloc(sizeof(*client));
66 perror("malloc() failed");
69 client->host = strdup(host);
71 client->bind = (bindto ? strdup(bindto) : NULL);
72 client->pass = (pass == NULL ? NULL : strdup(pass));
73 client->nick = strdup(nick);
74 client->ident = strdup(ident);
75 client->realname = strdup(realname);
77 client->network_name = NULL;
79 client->bufferpos = 0;
80 client->traffic_in = 0;
81 client->traffic_out = 0;
82 client->connection_time = 0;
86 client->whoqueue_first = NULL;
87 client->whoqueue_last = NULL;
88 client->handleinfo_first = NULL;
89 client->handleinfo_last = NULL;
90 SYNCHRONIZE(synchronized);
91 client->next = sockets->data;
92 sockets->data = client;
93 DESYNCHRONIZE(synchronized);
97 static int _connect_socket(struct ClientSocket *client);
99 int connect_socket(struct ClientSocket *client) {
100 SYNCHRONIZE(synchronized);
101 int ret = _connect_socket(client);
102 DESYNCHRONIZE(synchronized);
107 static int _connect_socket(struct ClientSocket *client) {
108 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
111 struct addrinfo hints, *res;
112 struct sockaddr_in *ip4 = NULL;
113 struct sockaddr_in6 *ip6 = NULL;
114 memset (&hints, 0, sizeof (hints));
115 hints.ai_family = PF_UNSPEC;
116 hints.ai_socktype = SOCK_STREAM;
117 hints.ai_flags |= AI_CANONNAME;
118 if (getaddrinfo (client->host, NULL, &hints, &res)) {
122 switch (res->ai_family) {
124 ip4 = (struct sockaddr_in *) res->ai_addr;
127 ip6 = (struct sockaddr_in6 *) res->ai_addr;
134 sock = socket(AF_INET6, SOCK_STREAM, 0);
136 perror("socket() failed");
140 ip6->sin6_family = AF_INET6;
141 ip6->sin6_port = htons(client->port);
143 struct sockaddr_in6 *ip6vhost = NULL;
144 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
146 switch (res->ai_family) {
148 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
155 ip6vhost->sin6_family = AF_INET6;
156 ip6vhost->sin6_port = htons(0);
157 bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
160 if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
161 perror("connect() failed");
166 sock = socket(AF_INET, SOCK_STREAM, 0);
168 perror("socket() failed");
172 ip4->sin_family = AF_INET;
173 ip4->sin_port = htons(client->port);
175 struct sockaddr_in *ip4vhost = NULL;
176 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
178 switch (res->ai_family) {
180 ip4vhost = (struct sockaddr_in *) res->ai_addr;
187 ip4vhost->sin_family = AF_INET;
188 ip4vhost->sin_port = htons(0);
189 bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
192 if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
193 perror("connect() failed");
200 if(get_int_field("Sockets.NoDelay")) {
202 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
203 perror("setsockopt() failed");
209 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
210 client->connection_time = time(0);
212 if(client->flags & SOCKET_FLAG_SSL) {
214 client->flags |= SOCKET_FLAG_HAVE_SSL;
216 client->flags &= ~SOCKET_FLAG_HAVE_SSL;
218 //send the IRC Headers
222 if(client->pass && strcmp(client->pass, "")) {
223 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
224 write_socket(client, sendBuf, len);
226 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
227 write_socket(client, sendBuf, len);
228 len = sprintf(sendBuf, "NICK %s\n", client->nick);
229 write_socket(client, sendBuf, len);
234 static int _connect_socket(struct ClientSocket *client) {
235 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
236 struct hostent *host;
237 struct sockaddr_in addr;
239 addr.sin_addr.s_addr = inet_addr(client->host);
240 if (addr.sin_addr.s_addr == INADDR_NONE) {
241 host = gethostbyname(client->host);
245 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
247 sock = socket(PF_INET, SOCK_STREAM, 0);
250 perror("socket() failed");
254 addr.sin_port = htons(client->port);
255 addr.sin_family = AF_INET;
257 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
259 perror("connect() failed");
264 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
265 client->connection_time = time(0);
268 if(client->flags & SOCKET_FLAG_SSL) {
270 client->flags |= SOCKET_FLAG_HAVE_SSL;
272 client->flags &= ~SOCKET_FLAG_HAVE_SSL;
274 //send the IRC Headers
278 if(client->pass && strcmp(client->pass, "")) {
279 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
280 write_socket(client, sendBuf, len);
282 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
283 write_socket(client, sendBuf, len);
284 len = sprintf(sendBuf, "NICK %s\n", client->nick);
285 write_socket(client, sendBuf, len);
291 int close_socket(struct ClientSocket *client) {
292 if(client == NULL) return 0;
293 if((client->flags & SOCKET_FLAG_CONNECTED)) {
294 char quitbuf[MAXLEN];
295 int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
296 write_socket_force(client, quitbuf, quitlen);
298 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT);
299 client->flags |= SOCKET_FLAG_QUITTED | SOCKET_FLAG_DEAD;
303 int disconnect_socket(struct ClientSocket *client) {
304 if(client == NULL) return 0;
305 if((client->flags & SOCKET_FLAG_CONNECTED)) {
306 char quitbuf[MAXLEN];
307 int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
308 write_socket_force(client, quitbuf, quitlen);
310 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT);
311 client->flags |= SOCKET_FLAG_QUITTED;
315 static void destroy_socket(struct ClientSocket *client, int free_socket) {
316 SYNCHRONIZE(synchronized);
317 if((client->flags & SOCKET_FLAG_CONNECTED)) {
319 bot_disconnect(client);
321 if(client->flags & SOCKET_FLAG_HAVE_SSL)
322 ssl_disconnect(client);
324 queue_destroy(client);
325 if(client->whoqueue_first)
326 clear_whoqueue(client);
327 if(client->handleinfo_first)
328 clear_handleinfoqueue(client);
329 client->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY | SOCKET_FLAG_HAVE_SSL);
331 struct ClientSocket *sock, *last_sock = NULL;
332 for (sock = sockets->data; sock; sock = sock->next) {
335 last_sock->next = sock->next;
337 sockets->data = sock->next;
348 if(client->network_name)
349 free(client->network_name);
351 } else if(client->flags & SOCKET_FLAG_FAST_JUMP) {
352 client->flags &= ~SOCKET_FLAG_FAST_JUMP;
353 connect_socket(client);
355 DESYNCHRONIZE(synchronized);
358 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
359 SYNCHRONIZE(synchronized);
361 putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
363 putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
365 if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) {
367 send(client->sock, msg, len, 0);
369 write(client->sock, msg, len);
372 client->traffic_out += len;
373 DESYNCHRONIZE(synchronized);
377 int write_socket(struct ClientSocket *client, char* msg, int len) {
378 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
379 if(client->flags & SOCKET_FLAG_USE_QUEUE)
380 return queue_add(client, msg, len);
382 return write_socket_force(client, msg, len);
386 static void clientsocket_start_of_recv(unsigned int tid) {
387 SYNCHRONIZE(whohandler_sync);
388 struct ParseOrder *entry, *last;
389 for(last = parse_order; last; last = last->next) {
390 if(last->next == NULL)
393 entry = malloc(sizeof(*entry));
400 DESYNCHRONIZE(whohandler_sync);
403 static void clientsocket_end_of_recv(unsigned int tid) {
404 SYNCHRONIZE(whohandler_sync);
405 struct ParseOrder *entry, *last = NULL;
406 for(entry = parse_order; entry; entry = entry->next) {
407 if(entry->tid == tid) {
409 last->next = entry->next;
411 parse_order = entry->next;
417 DESYNCHRONIZE(whohandler_sync);
420 int clientsocket_parseorder_top(unsigned int tid) {
421 if(parse_order && parse_order->tid == tid)
428 int socket_loop(int timeout_seconds) {
429 if(sockets == NULL) return 0;
430 int is_synchronized = 1;
431 SYNCHRONIZE(synchronized_recv);
433 struct timeval timeout;
434 struct ClientSocket *sock, *next;
435 int ret = 0, bytes, i;
438 for (sock = sockets->data; sock; sock = sock->next) {
439 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
440 FD_SET(sock->sock, &fds);
444 timeout.tv_sec = timeout_seconds;
446 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
448 DESYNCHRONIZE(synchronized_recv);
451 for (sock = sockets->data; sock; sock = next) {
453 if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_QUITTED)) == SOCKET_FLAG_CONNECTED && FD_ISSET(sock->sock, &fds)) {
454 if(sock->bufferpos != 0) {
455 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
457 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
459 bytes = read(sock->sock, buffer, sizeof(buffer));
463 for(i = 0; i < bytes; i++) {
464 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
465 sock->buffer[sock->bufferpos + i] = buffer[i];
467 sock->bufferpos += i;
470 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
472 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
474 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
478 sock->bufferpos = bytes;
482 sock->flags |= SOCKET_FLAG_QUITTED;
484 sock->traffic_in += bytes;
486 char linesbuf[BUF_SIZ*2];
487 strcpy(linesbuf, sock->buffer);
489 for(i = 0; i < sock->bufferpos; i++) {
490 if(sock->buffer[i] == '\n') {
494 if(used == sock->bufferpos + 1) {
495 //used all bytes so just reset the bufferpos
498 for(i = 0; i < sock->bufferpos - used; i++) {
499 sock->buffer[i] = sock->buffer[i+used];
501 sock->bufferpos -= used;
504 unsigned int tid = (unsigned int) pthread_self_tid();
505 clientsocket_start_of_recv(tid);
506 DESYNCHRONIZE(synchronized_recv);
507 parse_lines(sock, linesbuf, used);
508 clientsocket_end_of_recv(tid);
510 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
511 if(used == sock->bufferpos + 1) {
512 //used all bytes so just reset the bufferpos
515 for(i = 0; i < sock->bufferpos - used; i++) {
516 sock->buffer[i] = sock->buffer[i+used];
518 sock->bufferpos -= used;
521 DESYNCHRONIZE(synchronized_recv);
524 FD_ZERO(&fds); //zero out all other pending sockets here (we have other threads receiving from them)
527 } else if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT)) == SOCKET_FLAG_RECONNECT) {
528 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
529 connect_socket(sock);
532 if((sock->flags & SOCKET_FLAG_QUITTED)) {
533 sock->flags &= ~SOCKET_FLAG_QUITTED;
534 destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD));
537 if(is_synchronized) {
538 DESYNCHRONIZE(synchronized_recv);
544 putsock(struct ClientSocket *client, const char *text, ...)
547 char sendBuf[MAXLEN];
549 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
551 va_start(arg_list, text);
552 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
554 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
556 sendBuf[pos+1] = '\0';
557 write_socket(client, sendBuf, pos+1);
560 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
561 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
562 if(sock == NULL) return NULL;
563 for (; sock; sock = sock->next) {
564 if(!flags || (sock->flags & flags) == flags)
570 void free_sockets() {
572 struct ClientSocket *client, *next;
573 for (client = sockets->data; client; client = next) {
575 if((client->flags & SOCKET_FLAG_CONNECTED))
577 if(client->flags & SOCKET_FLAG_HAVE_SSL)
578 ssl_disconnect(client);
580 queue_destroy(client);
586 if(client->network_name)
587 free(client->network_name);