X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=2e2eb1c703fca717a79ab4c87e33f856d7539ef2;hb=391260f64a1dfb35bf0a55e7044fe1b8d83f2561;hp=470fa505432a16980d343ac6c5f35b6d0d771f73;hpb=2d9db1adb1946aba00b203f40eff7d5db8163f01;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index 470fa50..2e2eb1c 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -1,4 +1,4 @@ -/* ClientSocket.c - NeonServ v5.1 +/* ClientSocket.c - NeonServ v5.2 * Copyright (C) 2011 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -18,6 +18,11 @@ #include "ClientSocket.h" #include "IRCParser.h" #include "UserNode.h" +#include "IRCQueue.h" +#include "WHOHandler.h" +#include "HandleInfoHandler.h" +#include "ssl.h" +#include "ConfigParser.h" struct socket_list { struct ClientSocket *data; @@ -39,7 +44,7 @@ static void init_sockets() { sockets->count = 0; } -struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) { +struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) { if(sockets == NULL) init_sockets(); struct ClientSocket *client = malloc(sizeof(*client)); if (!client) @@ -49,9 +54,12 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User } client->host = strdup(host); client->port = port; - printf("Connect: %s:%d\n", client->host, client->port); + client->bind = (bindto ? strdup(bindto) : NULL); client->pass = (pass == NULL ? NULL : strdup(pass)); - client->user = user; + client->nick = strdup(nick); + client->ident = strdup(ident); + client->realname = strdup(realname); + client->user = NULL; client->flags = 0; client->bufferpos = 0; client->traffic_in = 0; @@ -59,78 +67,155 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User client->connection_time = 0; client->botid = 0; client->clientid = 0; + client->queue = NULL; + client->whoqueue_first = NULL; + client->whoqueue_last = NULL; + client->handleinfo_first = NULL; + client->handleinfo_last = NULL; client->next = sockets->data; sockets->data = client; return client; } -#ifdef WIN32 - +#ifndef WIN32 int connect_socket(struct ClientSocket *client) { if((client->flags & SOCKET_FLAG_CONNECTED)) return 1; - struct hostent *host; - struct sockaddr_in addr; int sock; - addr.sin_addr.s_addr = inet_addr(client->host); - if (addr.sin_addr.s_addr == INADDR_NONE) { - host = gethostbyname(client->host); - if(!host) { - return SOCKET_ERROR; - } - memcpy(&(addr.sin_addr), host->h_addr_list[0], 4); - } - sock = socket(PF_INET, SOCK_STREAM, 0); - if (sock == -1) - { - perror("socket() failed"); + + struct addrinfo hints, *res; + struct sockaddr_in *ip4 = NULL; + struct sockaddr_in6 *ip6 = NULL; + memset (&hints, 0, sizeof (hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags |= AI_CANONNAME; + if (getaddrinfo (client->host, NULL, &hints, &res)) { return 0; } - - addr.sin_port = htons(client->port); - addr.sin_family = AF_INET; - - if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) - { - perror("connect() failed"); + while (res) { + switch (res->ai_family) { + case AF_INET: + ip4 = (struct sockaddr_in *) res->ai_addr; + break; + case AF_INET6: + ip6 = (struct sockaddr_in6 *) res->ai_addr; + break; + } + res = res->ai_next; + } + + if(ip6) { + sock = socket(AF_INET6, SOCK_STREAM, 0); + if(sock == -1) { + perror("socket() failed"); + return 0; + } + + ip6->sin6_family = AF_INET6; + ip6->sin6_port = htons(client->port); + + struct sockaddr_in6 *ip6vhost = NULL; + if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) { + while (res) { + switch (res->ai_family) { + case AF_INET6: + ip6vhost = (struct sockaddr_in6 *) res->ai_addr; + break; + } + res = res->ai_next; + } + } + if(ip6vhost) { + ip6vhost->sin6_family = AF_INET6; + ip6vhost->sin6_port = htons(0); + bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost)); + } + + if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) { + perror("connect() failed"); + return 0; + } + + } else if(ip4) { + sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock == -1) { + perror("socket() failed"); + return 0; + } + + ip4->sin_family = AF_INET; + ip4->sin_port = htons(client->port); + + struct sockaddr_in *ip4vhost = NULL; + if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) { + while (res) { + switch (res->ai_family) { + case AF_INET: + ip4vhost = (struct sockaddr_in *) res->ai_addr; + break; + } + res = res->ai_next; + } + } + if(ip4vhost) { + ip4vhost->sin_family = AF_INET; + ip4vhost->sin_port = htons(0); + bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost)); + } + + if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) { + perror("connect() failed"); + return 0; + } + + } else return 0; + + if(get_int_field("Sockets.NoDelay")) { + int flag = 1; + if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) { + perror("setsockopt() failed"); + return 0; + } } - + client->sock = sock; - client->flags |= SOCKET_FLAG_CONNECTED; + client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT; client->connection_time = time(0); - + + if(client->flags & SOCKET_FLAG_SSL) { + ssl_connect(client); + client->flags |= SOCKET_FLAG_HAVE_SSL; + } + //send the IRC Headers char sendBuf[512]; int len; - + if(client->pass && strcmp(client->pass, "")) { len = sprintf(sendBuf, "PASS :%s\n", client->pass); write_socket(client, sendBuf, len); } - len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname); + len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname); write_socket(client, sendBuf, len); - len = sprintf(sendBuf, "NICK %s\n", client->user->nick); + len = sprintf(sendBuf, "NICK %s\n", client->nick); write_socket(client, sendBuf, len); - + return 1; } - #else - int connect_socket(struct ClientSocket *client) { if((client->flags & SOCKET_FLAG_CONNECTED)) return 1; struct hostent *host; struct sockaddr_in addr; int sock; - if (!inet_aton(client->host, &addr.sin_addr)) - { + addr.sin_addr.s_addr = inet_addr(client->host); + if (addr.sin_addr.s_addr == INADDR_NONE) { host = gethostbyname(client->host); - if (!host) - { - perror("gethostbyname() failed"); - return 0; + if(!host) { + return SOCKET_ERROR; } - addr.sin_addr = *(struct in_addr*)host->h_addr; + memcpy(&(addr.sin_addr), host->h_addr_list[0], 4); } sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == -1) @@ -149,31 +234,39 @@ int connect_socket(struct ClientSocket *client) { } client->sock = sock; - client->flags |= SOCKET_FLAG_CONNECTED; + client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT; client->connection_time = time(0); + + if(client->flags & SOCKET_FLAG_SSL) { + ssl_connect(client); + client->flags |= SOCKET_FLAG_HAVE_SSL; + } + + //send the IRC Headers char sendBuf[512]; int len; - if(client->pass) { + if(client->pass && strcmp(client->pass, "")) { len = sprintf(sendBuf, "PASS :%s\n", client->pass); write_socket(client, sendBuf, len); } - len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname); + len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname); write_socket(client, sendBuf, len); - len = sprintf(sendBuf, "NICK %s\n", client->user->nick); + len = sprintf(sendBuf, "NICK %s\n", client->nick); write_socket(client, sendBuf, len); return 1; } - #endif int close_socket(struct ClientSocket *client) { if(client == NULL) return 0; if((client->flags & SOCKET_FLAG_CONNECTED)) close(client->sock); + if(client->flags & SOCKET_FLAG_HAVE_SSL) + ssl_disconnect(client); struct ClientSocket *sock, *last_sock = NULL; for (sock = sockets->data; sock; sock = sock->next) { if(sock == client) { @@ -185,24 +278,42 @@ int close_socket(struct ClientSocket *client) { } else last_sock = sock; } + if(client->queue) + queue_destroy(client); + if(client->whoqueue_first) + clear_whoqueue(client); + if(client->handleinfo_first) + clear_handleinfoqueue(client); free(client->host); - free(client->pass); + if(client->bind) + free(client->bind); + if(client->pass) + free(client->pass); free(client); return 1; } -int write_socket(struct ClientSocket *client, char* msg, int len) { - if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0; +int write_socket_force(struct ClientSocket *client, char* msg, int len) { printf("[send %d] %s", len, msg); - #ifdef WIN32 - send(client->sock, msg, len, 0); - #else - write(client->sock, msg, len); - #endif + if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) { + #ifdef WIN32 + send(client->sock, msg, len, 0); + #else + write(client->sock, msg, len); + #endif + } client->traffic_out += len; return 1; } +int write_socket(struct ClientSocket *client, char* msg, int len) { + if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0; + if(client->flags & SOCKET_FLAG_USE_QUEUE) + return queue_add(client, msg, len); + else + return write_socket_force(client, msg, len); +} + void socket_loop(int timeout_seconds) { if(sockets == NULL) return; fd_set fds; @@ -224,11 +335,13 @@ void socket_loop(int timeout_seconds) { for (sock = sockets->data; sock; sock = sock->next) { if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) { if(sock->bufferpos != 0) { - #ifdef WIN32 - bytes = recv(sock->sock, buffer, sizeof(buffer), 0); - #else - bytes = read(sock->sock, buffer, sizeof(buffer)); - #endif + if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) { + #ifdef WIN32 + bytes = recv(sock->sock, buffer, sizeof(buffer), 0); + #else + bytes = read(sock->sock, buffer, sizeof(buffer)); + #endif + } if(bytes > 0) { for(i = 0; i < bytes; i++) { if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow @@ -237,11 +350,13 @@ void socket_loop(int timeout_seconds) { sock->bufferpos += i; } } else { - #ifdef WIN32 - bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0); - #else - bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer)); - #endif + if(!(sock->flags & SOCKET_FLAG_HAVE_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) { + #ifdef WIN32 + bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0); + #else + bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer)); + #endif + } if(bytes > 0) sock->bufferpos = bytes; } @@ -249,6 +364,11 @@ void socket_loop(int timeout_seconds) { //error sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY); bot_disconnect(sock); + if(sock->queue) + queue_destroy(sock); + close(sock->sock); + if(sock->flags & SOCKET_FLAG_HAVE_SSL) + ssl_disconnect(sock); } else { sock->traffic_in += bytes; int used = parse_lines(sock, sock->buffer, sock->bufferpos); @@ -262,6 +382,10 @@ void socket_loop(int timeout_seconds) { sock->bufferpos -= used; } } + } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) { + if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) { + connect_socket(sock); + } } } } @@ -272,7 +396,7 @@ putsock(struct ClientSocket *client, const char *text, ...) va_list arg_list; char sendBuf[MAXLEN]; int pos; - if (!(client->flags & SOCKET_FLAG_CONNECTED)) return; + if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return; sendBuf[0] = '\0'; va_start(arg_list, text); pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list); @@ -300,8 +424,15 @@ void free_sockets() { next = client->next; if((client->flags & SOCKET_FLAG_CONNECTED)) close(client->sock); + if(client->flags & SOCKET_FLAG_HAVE_SSL) + ssl_disconnect(client); + if(client->queue) + queue_destroy(client); free(client->host); - free(client->pass); + if(client->bind) + free(client->bind); + if(client->pass) + free(client->pass); free(client); } free(sockets);