1 /* ClientSocket.c - NeonServ v5.3
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);
78 client->bufferpos = 0;
79 client->traffic_in = 0;
80 client->traffic_out = 0;
81 client->connection_time = 0;
85 client->whoqueue_first = NULL;
86 client->whoqueue_last = NULL;
87 client->handleinfo_first = NULL;
88 client->handleinfo_last = NULL;
89 SYNCHRONIZE(synchronized);
90 client->next = sockets->data;
91 sockets->data = client;
92 DESYNCHRONIZE(synchronized);
96 static int _connect_socket(struct ClientSocket *client);
98 int connect_socket(struct ClientSocket *client) {
99 SYNCHRONIZE(synchronized);
100 int ret = _connect_socket(client);
101 DESYNCHRONIZE(synchronized);
106 static int _connect_socket(struct ClientSocket *client) {
107 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
110 struct addrinfo hints, *res;
111 struct sockaddr_in *ip4 = NULL;
112 struct sockaddr_in6 *ip6 = NULL;
113 memset (&hints, 0, sizeof (hints));
114 hints.ai_family = PF_UNSPEC;
115 hints.ai_socktype = SOCK_STREAM;
116 hints.ai_flags |= AI_CANONNAME;
117 if (getaddrinfo (client->host, NULL, &hints, &res)) {
121 switch (res->ai_family) {
123 ip4 = (struct sockaddr_in *) res->ai_addr;
126 ip6 = (struct sockaddr_in6 *) res->ai_addr;
133 sock = socket(AF_INET6, SOCK_STREAM, 0);
135 perror("socket() failed");
139 ip6->sin6_family = AF_INET6;
140 ip6->sin6_port = htons(client->port);
142 struct sockaddr_in6 *ip6vhost = NULL;
143 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
145 switch (res->ai_family) {
147 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
154 ip6vhost->sin6_family = AF_INET6;
155 ip6vhost->sin6_port = htons(0);
156 bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
159 if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
160 perror("connect() failed");
165 sock = socket(AF_INET, SOCK_STREAM, 0);
167 perror("socket() failed");
171 ip4->sin_family = AF_INET;
172 ip4->sin_port = htons(client->port);
174 struct sockaddr_in *ip4vhost = NULL;
175 if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
177 switch (res->ai_family) {
179 ip4vhost = (struct sockaddr_in *) res->ai_addr;
186 ip4vhost->sin_family = AF_INET;
187 ip4vhost->sin_port = htons(0);
188 bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
191 if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
192 perror("connect() failed");
199 if(get_int_field("Sockets.NoDelay")) {
201 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
202 perror("setsockopt() failed");
208 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
209 client->connection_time = time(0);
211 if(client->flags & SOCKET_FLAG_SSL) {
213 client->flags |= SOCKET_FLAG_HAVE_SSL;
215 client->flags &= ~SOCKET_FLAG_HAVE_SSL;
217 //send the IRC Headers
221 if(client->pass && strcmp(client->pass, "")) {
222 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
223 write_socket(client, sendBuf, len);
225 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
226 write_socket(client, sendBuf, len);
227 len = sprintf(sendBuf, "NICK %s\n", client->nick);
228 write_socket(client, sendBuf, len);
233 static int _connect_socket(struct ClientSocket *client) {
234 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
235 struct hostent *host;
236 struct sockaddr_in addr;
238 addr.sin_addr.s_addr = inet_addr(client->host);
239 if (addr.sin_addr.s_addr == INADDR_NONE) {
240 host = gethostbyname(client->host);
244 memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
246 sock = socket(PF_INET, SOCK_STREAM, 0);
249 perror("socket() failed");
253 addr.sin_port = htons(client->port);
254 addr.sin_family = AF_INET;
256 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
258 perror("connect() failed");
263 client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
264 client->connection_time = time(0);
267 if(client->flags & SOCKET_FLAG_SSL) {
269 client->flags |= SOCKET_FLAG_HAVE_SSL;
271 client->flags &= ~SOCKET_FLAG_HAVE_SSL;
273 //send the IRC Headers
277 if(client->pass && strcmp(client->pass, "")) {
278 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
279 write_socket(client, sendBuf, len);
281 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
282 write_socket(client, sendBuf, len);
283 len = sprintf(sendBuf, "NICK %s\n", client->nick);
284 write_socket(client, sendBuf, len);
290 int close_socket(struct ClientSocket *client) {
291 if(client == NULL) return 0;
292 if((client->flags & SOCKET_FLAG_CONNECTED)) {
293 char quitbuf[MAXLEN];
294 int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
295 write_socket_force(client, quitbuf, quitlen);
297 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT);
298 client->flags |= SOCKET_FLAG_QUITTED | SOCKET_FLAG_DEAD;
302 int disconnect_socket(struct ClientSocket *client) {
303 if(client == NULL) return 0;
304 if((client->flags & SOCKET_FLAG_CONNECTED)) {
305 char quitbuf[MAXLEN];
306 int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
307 write_socket_force(client, quitbuf, quitlen);
309 client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT);
310 client->flags |= SOCKET_FLAG_QUITTED;
314 static void destroy_socket(struct ClientSocket *client, int free_socket) {
315 SYNCHRONIZE(synchronized);
316 if((client->flags & SOCKET_FLAG_CONNECTED)) {
318 bot_disconnect(client);
320 if(client->flags & SOCKET_FLAG_HAVE_SSL)
321 ssl_disconnect(client);
323 queue_destroy(client);
324 if(client->whoqueue_first)
325 clear_whoqueue(client);
326 if(client->handleinfo_first)
327 clear_handleinfoqueue(client);
328 client->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY | SOCKET_FLAG_HAVE_SSL);
330 struct ClientSocket *sock, *last_sock = NULL;
331 for (sock = sockets->data; sock; sock = sock->next) {
334 last_sock->next = sock->next;
336 sockets->data = sock->next;
348 } else if(client->flags & SOCKET_FLAG_FAST_JUMP) {
349 client->flags &= ~SOCKET_FLAG_FAST_JUMP;
350 connect_socket(client);
352 DESYNCHRONIZE(synchronized);
355 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
356 SYNCHRONIZE(synchronized);
357 printf("[send %d] %s", len, msg);
358 if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) {
360 send(client->sock, msg, len, 0);
362 write(client->sock, msg, len);
365 client->traffic_out += len;
366 DESYNCHRONIZE(synchronized);
370 int write_socket(struct ClientSocket *client, char* msg, int len) {
371 if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
372 if(client->flags & SOCKET_FLAG_USE_QUEUE)
373 return queue_add(client, msg, len);
375 return write_socket_force(client, msg, len);
379 static void clientsocket_start_of_recv(unsigned int tid) {
380 SYNCHRONIZE(whohandler_sync);
381 struct ParseOrder *entry, *last;
382 for(last = parse_order; last; last = last->next) {
383 if(last->next == NULL)
386 entry = malloc(sizeof(*entry));
393 DESYNCHRONIZE(whohandler_sync);
396 static void clientsocket_end_of_recv(unsigned int tid) {
397 SYNCHRONIZE(whohandler_sync);
398 struct ParseOrder *entry, *last = NULL;
399 for(entry = parse_order; entry; entry = entry->next) {
400 if(entry->tid == tid) {
402 last->next = entry->next;
404 parse_order = entry->next;
410 DESYNCHRONIZE(whohandler_sync);
413 int clientsocket_parseorder_top(unsigned int tid) {
414 if(parse_order && parse_order->tid == tid)
421 void socket_loop(int timeout_seconds) {
422 if(sockets == NULL) return;
423 int is_synchronized = 1;
424 SYNCHRONIZE(synchronized_recv);
426 struct timeval timeout;
427 struct ClientSocket *sock, *next;
428 int ret = 0, bytes, i;
431 for (sock = sockets->data; sock; sock = sock->next) {
432 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
433 FD_SET(sock->sock, &fds);
437 timeout.tv_sec = timeout_seconds;
439 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
441 DESYNCHRONIZE(synchronized_recv);
444 for (sock = sockets->data; sock; sock = next) {
446 if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_QUITTED)) == SOCKET_FLAG_CONNECTED && FD_ISSET(sock->sock, &fds)) {
447 if(sock->bufferpos != 0) {
448 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) {
450 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
452 bytes = read(sock->sock, buffer, sizeof(buffer));
456 for(i = 0; i < bytes; i++) {
457 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
458 sock->buffer[sock->bufferpos + i] = buffer[i];
460 sock->bufferpos += i;
463 if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) {
465 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
467 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
471 sock->bufferpos = bytes;
475 sock->flags |= SOCKET_FLAG_QUITTED;
477 sock->traffic_in += bytes;
479 char linesbuf[BUF_SIZ*2];
480 strcpy(linesbuf, sock->buffer);
482 for(i = 0; i < sock->bufferpos; i++) {
483 if(sock->buffer[i] == '\n') {
487 if(used == sock->bufferpos + 1) {
488 //used all bytes so just reset the bufferpos
491 for(i = 0; i < sock->bufferpos - used; i++) {
492 sock->buffer[i] = sock->buffer[i+used];
494 sock->bufferpos -= used;
497 unsigned int tid = (unsigned int) pthread_self_tid();
498 clientsocket_start_of_recv(tid);
499 DESYNCHRONIZE(synchronized_recv);
500 parse_lines(sock, linesbuf, used);
501 clientsocket_end_of_recv(tid);
503 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
504 if(used == sock->bufferpos + 1) {
505 //used all bytes so just reset the bufferpos
508 for(i = 0; i < sock->bufferpos - used; i++) {
509 sock->buffer[i] = sock->buffer[i+used];
511 sock->bufferpos -= used;
514 DESYNCHRONIZE(synchronized_recv);
517 FD_ZERO(&fds); //zero out all other pending sockets here (we have other threads receiving from them)
520 } else if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT)) == SOCKET_FLAG_RECONNECT) {
521 if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
522 connect_socket(sock);
525 if((sock->flags & SOCKET_FLAG_QUITTED)) {
526 sock->flags &= ~SOCKET_FLAG_QUITTED;
527 destroy_socket(sock, (sock->flags & SOCKET_FLAG_DEAD));
530 if(is_synchronized) {
531 DESYNCHRONIZE(synchronized_recv);
536 putsock(struct ClientSocket *client, const char *text, ...)
539 char sendBuf[MAXLEN];
541 if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
543 va_start(arg_list, text);
544 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
546 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
548 sendBuf[pos+1] = '\0';
549 write_socket(client, sendBuf, pos+1);
552 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
553 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
554 if(sock == NULL) return NULL;
555 for (; sock; sock = sock->next) {
556 if(!flags || (sock->flags & flags) == flags)
562 void free_sockets() {
564 struct ClientSocket *client, *next;
565 for (client = sockets->data; client; client = next) {
567 if((client->flags & SOCKET_FLAG_CONNECTED))
569 if(client->flags & SOCKET_FLAG_HAVE_SSL)
570 ssl_disconnect(client);
572 queue_destroy(client);