fixed null pointer crash when passing NULL to putsock/write_socket
[NeonServV5.git] / src / ClientSocket.c
index 30c2efb4ec7e2d9c7cf69280fc47719720a36884..a46a4bf82d1bbc2e4d1b4ab8c64436c99e0a0f52 100644 (file)
@@ -1,7 +1,26 @@
+/* ClientSocket.c - NeonServ v5.2
+ * Copyright (C) 2011  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
 
 #include "ClientSocket.h"
 #include "IRCParser.h"
 #include "UserNode.h"
+#include "IRCQueue.h"
+#include "WHOHandler.h"
+#include "HandleInfoHandler.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -23,7 +42,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)
@@ -35,7 +54,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;
@@ -43,6 +65,11 @@ 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;
@@ -80,7 +107,7 @@ 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);
 
     //send the IRC Headers
@@ -91,9 +118,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;
@@ -133,22 +160,22 @@ 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);
 
     //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;
 }
 
@@ -169,14 +196,19 @@ 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);
     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);
@@ -187,6 +219,14 @@ int write_socket(struct ClientSocket *client, char* msg, int 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;
@@ -233,6 +273,8 @@ void socket_loop(int timeout_seconds) {
                 //error
                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
                 bot_disconnect(sock);
+                if(sock->queue)
+                    queue_destroy(sock);
             } else {
                 sock->traffic_in += bytes;
                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
@@ -246,6 +288,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);
+            }
         }
     }
 }
@@ -256,7 +302,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);
@@ -284,6 +330,8 @@ void free_sockets() {
         next = client->next;
         if((client->flags & SOCKET_FLAG_CONNECTED))
             close(client->sock);
+        if(client->queue)
+            queue_destroy(client);
         free(client->host);
         free(client->pass);
         free(client);