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);
366 if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) {
368 ret = send(client->sock, msg, len, 0);
370 ret = write(client->sock, msg, len);
373 client->traffic_out += len;
374 DESYNCHRONIZE(synchronized);
378 int write_socket(struct ClientSocket *client, char* msg, int len) {
379 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
380 if(client->flags & SOCKET_FLAG_USE_QUEUE)
381 return queue_add(client, msg, len);
383 return write_socket_force(client, msg, len);
387 static void clientsocket_start_of_recv(unsigned int tid) {
388 SYNCHRONIZE(whohandler_sync);
389 struct ParseOrder *entry, *last;
390 for(last = parse_order; last; last = last->next) {
391 if(last->next == NULL)
394 entry = malloc(sizeof(*entry));
401 DESYNCHRONIZE(whohandler_sync);
404 static void clientsocket_end_of_recv(unsigned int tid) {
405 SYNCHRONIZE(whohandler_sync);
406 struct ParseOrder *entry, *last = NULL;
407 for(entry = parse_order; entry; entry = entry->next) {
408 if(entry->tid == tid) {
410 last->next = entry->next;
412 parse_order = entry->next;
418 DESYNCHRONIZE(whohandler_sync);
421 int clientsocket_parseorder_top(unsigned int tid) {
422 if(parse_order && parse_order->tid == tid)
429 int socket_loop(int timeout_seconds) {
430 if(sockets == NULL) return 0;
431 int is_synchronized = 1;
432 SYNCHRONIZE(synchronized_recv);
434 struct timeval timeout;
435 struct ClientSocket *sock, *next;
436 int ret = 0, bytes, i;
439 for (sock = sockets->data; sock; sock = sock->next) {
440 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
441 FD_SET(sock->sock, &fds);
445 timeout.tv_sec = timeout_seconds;
447 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
449 DESYNCHRONIZE(synchronized_recv);
452 for (sock = sockets->data; sock; sock = next) {
454 if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_QUITTED)) == SOCKET_FLAG_CONNECTED && FD_ISSET(sock->sock, &fds)) {
455 if(sock->bufferpos != 0) {
456 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
458 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
460 bytes = read(sock->sock, buffer, sizeof(buffer));
464 for(i = 0; i < bytes; i++) {
465 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
466 sock->buffer[sock->bufferpos + i] = buffer[i];
468 sock->bufferpos += i;
471 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
473 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
475 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
479 sock->bufferpos = bytes;
483 sock->flags |= SOCKET_FLAG_QUITTED;
485 sock->traffic_in += bytes;
487 char linesbuf[BUF_SIZ*2];
488 strcpy(linesbuf, sock->buffer);
490 for(i = 0; i < sock->bufferpos; i++) {
491 if(sock->buffer[i] == '\n') {
495 if(used == sock->bufferpos + 1) {
496 //used all bytes so just reset the bufferpos
499 for(i = 0; i < sock->bufferpos - used; i++) {
500 sock->buffer[i] = sock->buffer[i+used];
502 sock->bufferpos -= used;
505 unsigned int tid = (unsigned int) pthread_self_tid();
506 clientsocket_start_of_recv(tid);
507 DESYNCHRONIZE(synchronized_recv);
508 parse_lines(sock, linesbuf, used);
509 clientsocket_end_of_recv(tid);
511 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
512 if(used == sock->bufferpos + 1) {
513 //used all bytes so just reset the bufferpos
516 for(i = 0; i < sock->bufferpos - used; i++) {
517 sock->buffer[i] = sock->buffer[i+used];
519 sock->bufferpos -= used;
522 DESYNCHRONIZE(synchronized_recv);
525 FD_ZERO(&fds); //zero out all other pending sockets here (we have other threads receiving from them)
528 } else if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT)) == SOCKET_FLAG_RECONNECT) {
529 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
530 connect_socket(sock);
533 if((sock->flags & SOCKET_FLAG_QUITTED)) {
534 sock->flags &= ~SOCKET_FLAG_QUITTED;
535 destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD));
538 if(is_synchronized) {
539 DESYNCHRONIZE(synchronized_recv);
545 putsock(struct ClientSocket *client, const char *text, ...)
548 char sendBuf[MAXLEN];
550 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
552 va_start(arg_list, text);
553 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
555 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
557 sendBuf[pos+1] = '\0';
558 write_socket(client, sendBuf, pos+1);
561 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
562 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
563 if(sock == NULL) return NULL;
564 for (; sock; sock = sock->next) {
565 if(!flags || (sock->flags & flags) == flags)
571 void free_sockets() {
573 struct ClientSocket *client, *next;
574 for (client = sockets->data; client; client = next) {
576 if((client->flags & SOCKET_FLAG_CONNECTED))
578 if(client->flags & SOCKET_FLAG_HAVE_SSL)
579 ssl_disconnect(client);
581 queue_destroy(client);
587 if(client->network_name)
588 free(client->network_name);