*** VERSION 5.5.0 ***
[NeonServV5.git] / src / ClientSocket.c
index 6d2c34e5d20efe744237d1d6b3fab270a15c7607..cdf76f4fea8ef9113c4d309b553d8c473144f971 100644 (file)
@@ -1,4 +1,4 @@
-/* ClientSocket.c - NeonServ v5.3
+/* ClientSocket.c - NeonServ v5.5
  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -74,6 +74,7 @@ struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pas
     client->ident = strdup(ident);
     client->realname = strdup(realname);
     client->user = NULL;
+    client->network_name = NULL;
     client->flags = 0;
     client->bufferpos = 0;
     client->traffic_in = 0;
@@ -105,6 +106,7 @@ int connect_socket(struct ClientSocket *client) {
 #ifndef WIN32
 static int _connect_socket(struct ClientSocket *client) {
     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
+    client->connection_time = time(0);
     int sock;
     
     struct addrinfo hints, *res;
@@ -206,7 +208,6 @@ static int _connect_socket(struct ClientSocket *client) {
     
     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);
@@ -232,6 +233,7 @@ static int _connect_socket(struct ClientSocket *client) {
 #else
 static int _connect_socket(struct ClientSocket *client) {
     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
+    client->connection_time = time(0);
     struct hostent *host;
     struct sockaddr_in addr;
     int sock;
@@ -261,7 +263,6 @@ static int _connect_socket(struct ClientSocket *client) {
 
     client->sock = sock;
     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
-    client->connection_time = time(0);
 
 
     if(client->flags & SOCKET_FLAG_SSL) {
@@ -344,6 +345,8 @@ static void destroy_socket(struct ClientSocket *client, int free_socket) {
             free(client->bind);
         if(client->pass)
             free(client->pass);
+        if(client->network_name)
+            free(client->network_name);
         free(client);
     } else if(client->flags & SOCKET_FLAG_FAST_JUMP) {
         client->flags &= ~SOCKET_FLAG_FAST_JUMP;
@@ -354,17 +357,22 @@ static void destroy_socket(struct ClientSocket *client, int free_socket) {
 
 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
     SYNCHRONIZE(synchronized);
-    printf("[send %d] %s", len, msg);
+    #ifdef HAVE_THREADS
+    putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
+    #else
+    putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
+    #endif
+       int ret = 1;
     if(!(client->flags & SOCKET_FLAG_HAVE_SSL) || ssl_write(client, msg, len) == -2) {
         #ifdef WIN32
-        send(client->sock, msg, len, 0);
+        ret = send(client->sock, msg, len, 0);
         #else
-        write(client->sock, msg, len);
+        ret = write(client->sock, msg, len);
         #endif
     }
     client->traffic_out += len;
     DESYNCHRONIZE(synchronized);
-    return 1;
+    return ret;
 }
 
 int write_socket(struct ClientSocket *client, char* msg, int len) {
@@ -418,8 +426,8 @@ int clientsocket_parseorder_top(unsigned int tid) {
 }
 #endif
 
-void socket_loop(int timeout_seconds) {
-    if(sockets == NULL) return;
+int socket_loop(int timeout_seconds) {
+    if(sockets == NULL) return 0;
     int is_synchronized = 1;
     SYNCHRONIZE(synchronized_recv);
     fd_set fds;
@@ -437,10 +445,6 @@ void socket_loop(int timeout_seconds) {
     timeout.tv_sec = timeout_seconds;
     timeout.tv_usec = 0;
     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
-    if(ret == 0) {
-        DESYNCHRONIZE(synchronized_recv);
-        return;
-    }
     for (sock = sockets->data; sock; sock = next) {
         next = sock->next;
         if((sock->flags & (SOCKET_FLAG_CONNECTED | SOCKET_FLAG_QUITTED)) == SOCKET_FLAG_CONNECTED && FD_ISSET(sock->sock, &fds)) {
@@ -530,6 +534,7 @@ void socket_loop(int timeout_seconds) {
     if(is_synchronized) {
         DESYNCHRONIZE(synchronized_recv);
     }
+    return 1;
 }
 
 void
@@ -575,6 +580,8 @@ void free_sockets() {
             free(client->bind);
         if(client->pass)
             free(client->pass);
+        if(client->network_name)
+            free(client->network_name);
         free(client);
     }
     free(sockets);