]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
Fixed a memory leak when resizing the connection pool and realloc() failed.
[ngircd-alex.git] / src / ngircd / conn.c
index b2ad9a257195b8b580eda94de8a8f13b9e4a8a33..aad61494a0a62e70d2fa8679006af15735a1afee 100644 (file)
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.136 2004/05/11 00:53:14 alex Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.142 2005/01/17 11:57:39 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
-#include <stdarg.h>
+#ifdef PROTOTYPES
+#      include <stdarg.h>
+#else
+#      include <varargs.h>
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -628,6 +632,9 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
        {
                /* Conn_Close() has been called recursively for this link;
                 * probabe reason: Try_Write() failed  -- see below. */
+#ifdef DEBUG
+               Log( LOG_DEBUG, "Recursive request to close connection: %d", Idx );
+#endif
                return;
        }
 
@@ -723,6 +730,10 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
 
        /* Clean up connection structure (=free it) */
        Init_Conn_Struct( Idx );
+
+#ifdef DEBUG
+       Log( LOG_DEBUG, "Shutdown of connection %d completed.", Idx );
+#endif
 } /* Conn_Close */
 
 
@@ -964,9 +975,9 @@ New_Connection( INT Sock )
        cnt = Count_Connections( new_addr );
        if(( Conf_MaxConnectionsIP > 0 ) && ( cnt >= Conf_MaxConnectionsIP ))
        {
-               /* Access denied, too many connections from this IP! */
-               Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP!", inet_ntoa( new_addr.sin_addr ), cnt);
-               Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP!" );
+               /* Access denied, too many connections from this IP address! */
+               Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", inet_ntoa( new_addr.sin_addr ), cnt);
+               Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
                close( new_sock );
                return;
        }
@@ -1001,32 +1012,17 @@ New_Connection( INT Sock )
                        return;
                }
 
-               /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
-                * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
-                * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
                ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
                if( ! ptr )
                {
-                       /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
-                       ptr = (POINTER *)malloc( sizeof( CONNECTION ) * new_size );
-                       if( ! ptr )
-                       {
-                               /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
-                               Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
-                               Simple_Message( new_sock, "ERROR: Internal error" );
-                               close( new_sock );
-                               return;
-                       }
-
-                       /* Struktur umkopieren ... */
-                       memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
-
-#ifdef DEBUG
-                       Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size );
-#endif
+                       Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
+                       Simple_Message( new_sock, "ERROR: Internal error" );
+                       close( new_sock );
+                       return;
                }
+
 #ifdef DEBUG
-               else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
+               Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
 #endif
 
                /* Adjust pointer to new block */
@@ -1133,8 +1129,8 @@ Read_Request( CONN_ID Idx )
 #endif
        {
                /* Der Lesepuffer ist voll */
-               Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
-               Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
+               Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
+               Conn_Close( Idx, "Receive buffer overflow!", NULL, FALSE );
                return;
        }
 
@@ -1272,22 +1268,20 @@ Handle_Buffer( CONN_ID Idx )
                                /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
                                 * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
                                 * umkopieren, damit diese nun zunaechst entkomprimiert werden */
+                               if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
                                {
-                                       if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
-                                       {
-                                               /* Hupsa! Soviel Platz haben wir aber gar nicht! */
-                                               Log( LOG_ALERT, "Can't move read buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
-                                               return FALSE;
-                                       }
-                                       memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
-                                       My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
-                                       My_Connections[Idx].rdatalen = 0;
-#ifdef DEBUG
-                                       Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
-#endif
+                                       /* Hupsa! Soviel Platz haben wir aber gar nicht! */
+                                       Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
+                                       return FALSE;
                                }
+                               memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
+                               My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
+                               My_Connections[Idx].rdatalen = 0;
+#ifdef DEBUG
+                               Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
+#endif /* DEBUG */
                        }
-#endif
+#endif /* ZLIB */
                }
 
                if( action ) result = TRUE;
@@ -1634,7 +1628,7 @@ Read_Resolver_Result( INT r_fd )
        assert( s != NULL );
 
        /* Read result from pipe */
-       len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - HOST_LEN - 1 );
+       len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - s->bufpos - 1 );
        if( len < 0 )
        {
                /* Error! */