X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2FClientSocket.c;h=6b32b17e33b2393f31ebcb9b2557bbffa28b4ae3;hb=003f6906aeb911cce26ee25b48a818d0d1c4aea5;hp=cda3ee76adeb93b82501db4b305fc05322ef7aa1;hpb=c254e7d9a1622b19aae9b4ebecb4082657288f4a;p=NeonServV5.git diff --git a/src/ClientSocket.c b/src/ClientSocket.c index cda3ee7..6b32b17 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 @@ -19,6 +19,9 @@ #include "IRCParser.h" #include "UserNode.h" #include "IRCQueue.h" +#include "WHOHandler.h" +#include "HandleInfoHandler.h" +#include "ssl.h" struct socket_list { struct ClientSocket *data; @@ -40,7 +43,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 *pass, char *nick, char *ident, char *realname) { if(sockets == NULL) init_sockets(); struct ClientSocket *client = malloc(sizeof(*client)); if (!client) @@ -52,7 +55,10 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User client->port = port; printf("Connect: %s:%d\n", client->host, client->port); 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; @@ -61,6 +67,10 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User 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; @@ -98,9 +108,15 @@ 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); + } + + //send the IRC Headers char sendBuf[512]; int len; @@ -109,9 +125,9 @@ int connect_socket(struct ClientSocket *client) { 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; @@ -151,22 +167,26 @@ 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); + } + //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; } @@ -176,6 +196,8 @@ 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_SSL) + ssl_disconnect(client); struct ClientSocket *sock, *last_sock = NULL; for (sock = sockets->data; sock; sock = sock->next) { if(sock == client) { @@ -189,6 +211,10 @@ int close_socket(struct ClientSocket *client) { } 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); free(client); @@ -197,17 +223,19 @@ int close_socket(struct ClientSocket *client) { 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_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->flags & SOCKET_FLAG_CONNECTED)) return 0; + if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0; if(client->flags & SOCKET_FLAG_USE_QUEUE) return queue_add(client, msg, len); else @@ -235,11 +263,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_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 @@ -248,11 +278,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_SSL) || (bytes = ssl_read(sock, buffer, sizeof(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; } @@ -262,6 +294,9 @@ void socket_loop(int timeout_seconds) { bot_disconnect(sock); if(sock->queue) queue_destroy(sock); + close(sock->sock); + if(sock->flags & SOCKET_FLAG_SSL) + ssl_disconnect(sock); } else { sock->traffic_in += bytes; int used = parse_lines(sock, sock->buffer, sock->bufferpos); @@ -275,6 +310,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); + } } } } @@ -285,7 +324,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); @@ -313,6 +352,8 @@ void free_sockets() { next = client->next; if((client->flags & SOCKET_FLAG_CONNECTED)) close(client->sock); + if(client->flags & SOCKET_FLAG_SSL) + ssl_disconnect(client); if(client->queue) queue_destroy(client); free(client->host);