fixed null pointer crash when passing NULL to putsock/write_socket
[NeonServV5.git] / src / ClientSocket.c
index 5e790dac4e3b2de07d61dc3f15057f6d6fe7ea5e..a46a4bf82d1bbc2e4d1b4ab8c64436c99e0a0f52 100644 (file)
@@ -20,6 +20,7 @@
 #include "UserNode.h"
 #include "IRCQueue.h"
 #include "WHOHandler.h"
+#include "HandleInfoHandler.h"
 
 struct socket_list {
     struct ClientSocket *data;
@@ -41,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)
@@ -53,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;
@@ -64,6 +68,8 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
     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;
@@ -112,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;
@@ -165,11 +171,11 @@ 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;
 }
 
@@ -194,6 +200,8 @@ int close_socket(struct ClientSocket *client) {
         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);
@@ -212,7 +220,7 @@ int write_socket_force(struct ClientSocket *client, char* msg, int len) {
 }
 
 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
@@ -294,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);