X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=9b44e2064cffcfd5ece05e43b4efd1304d104f4f;hb=6927fcd82e211ff0561ae50e0b9acebedbbe789a;hp=898b05fb2a9dc3a0ac27f0597f62e0cb85f39493;hpb=3e468d2c23318616f86dc7c180102ebb2c34bd6c;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index 898b05f..9b44e20 100644 --- a/src/ClientSocket.c +++ b/src/ClientSocket.c @@ -22,6 +22,8 @@ #include "WHOHandler.h" #include "HandleInfoHandler.h" #include "ssl.h" +#include "ConfigParser.h" +#include "version.h" struct socket_list { struct ClientSocket *data; @@ -43,7 +45,7 @@ static void init_sockets() { sockets->count = 0; } -struct ClientSocket* create_socket(char *host, int port, char *pass, char *nick, char *ident, char *realname) { +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) @@ -53,7 +55,7 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, char *nick, } 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->nick = strdup(nick); client->ident = strdup(ident); @@ -76,51 +78,122 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, char *nick, 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 | SOCKET_FLAG_RECONNECT; client->connection_time = time(0); - - + if(client->flags & SOCKET_FLAG_SSL) { ssl_connect(client); - } - - + client->flags |= SOCKET_FLAG_HAVE_SSL; + } else + 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); @@ -129,26 +202,22 @@ int connect_socket(struct ClientSocket *client) { write_socket(client, sendBuf, len); 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) @@ -170,9 +239,12 @@ int connect_socket(struct ClientSocket *client) { 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; + } else + client->flags &= ~SOCKET_FLAG_HAVE_SSL; //send the IRC Headers char sendBuf[512]; @@ -186,17 +258,21 @@ int connect_socket(struct ClientSocket *client) { write_socket(client, sendBuf, len); 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)) + if((client->flags & SOCKET_FLAG_CONNECTED)) { + char quitbuf[MAXLEN]; + int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel); + write_socket_force(client, quitbuf, quitlen); close(client->sock); - if(client->flags & SOCKET_FLAG_SSL) + bot_disconnect(client); + } + if(client->flags & SOCKET_FLAG_HAVE_SSL) ssl_disconnect(client); struct ClientSocket *sock, *last_sock = NULL; for (sock = sockets->data; sock; sock = sock->next) { @@ -216,14 +292,38 @@ int close_socket(struct ClientSocket *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 disconnect_socket(struct ClientSocket *client) { + if(client == NULL) return 0; + if((client->flags & SOCKET_FLAG_CONNECTED)) { + char quitbuf[MAXLEN]; + int quitlen = sprintf(quitbuf, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel); + write_socket_force(client, quitbuf, quitlen); + close(client->sock); + bot_disconnect(client); + } + if(client->flags & SOCKET_FLAG_HAVE_SSL) + ssl_disconnect(client); + if(client->queue) + queue_destroy(client); + if(client->whoqueue_first) + clear_whoqueue(client); + if(client->handleinfo_first) + clear_handleinfoqueue(client); + client->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY | SOCKET_FLAG_RECONNECT); + return 1; +} + int write_socket_force(struct ClientSocket *client, char* msg, int len) { printf("[send %d] %s", len, msg); - if(!(client->flags & SOCKET_FLAG_SSL) || ssl_write(client, msg, len) == -2) { + if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) { #ifdef WIN32 send(client->sock, msg, len, 0); #else @@ -263,7 +363,7 @@ 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) { - if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, buffer, sizeof(buffer))) == -2) { + 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 @@ -278,7 +378,7 @@ void socket_loop(int timeout_seconds) { sock->bufferpos += i; } } else { - if(!(sock->flags & SOCKET_FLAG_SSL) || (bytes = ssl_read(sock, sock->buffer, sizeof(sock->buffer))) == -2) { + 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 @@ -295,8 +395,12 @@ void socket_loop(int timeout_seconds) { if(sock->queue) queue_destroy(sock); close(sock->sock); - if(sock->flags & SOCKET_FLAG_SSL) + if(sock->flags & SOCKET_FLAG_HAVE_SSL) ssl_disconnect(sock); + if(sock->whoqueue_first) + clear_whoqueue(sock); + if(sock->handleinfo_first) + clear_handleinfoqueue(sock); } else { sock->traffic_in += bytes; int used = parse_lines(sock, sock->buffer, sock->bufferpos); @@ -352,12 +456,15 @@ void free_sockets() { next = client->next; if((client->flags & SOCKET_FLAG_CONNECTED)) close(client->sock); - if(client->flags & SOCKET_FLAG_SSL) + 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);