]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
if we can't bind a socket we now Log the port number, too.
[ngircd-alex.git] / src / ngircd / conn.c
index 13c8085524bf36aba2061202663fc1f32a6654ca..6932af0fc559acd1126978a61b91efbf6a12dfcd 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.194 2006/05/09 14:49:08 alex Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.203 2007/02/21 11:06:06 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -123,7 +123,6 @@ cb_connserver(int sock, UNUSED short what)
 {
        int res, err;
        socklen_t sock_len;
-       CLIENT *c;
        CONN_ID idx = Socket2Index( sock );
        if (idx <= NONE) {
                LogDebug("cb_connserver wants to write on unknown socket?!");
@@ -150,14 +149,7 @@ cb_connserver(int sock, UNUSED short what)
                            Conf_Server[Conf_GetServer(idx)].port,
                            idx, strerror(err));
 
-               /* Clean up the CLIENT structure (to avoid silly log
-                * messages) and call Conn_Close() to do the rest. */
-               c = Conn_GetClient(idx);
-               if (c)
-                       Client_DestroyNow(c);
-               Conn_Close(idx, "Can't connect!", NULL, false);
+               Conn_Close(idx, "Can't connect!", NULL, false);
                return;
        }
 
@@ -216,12 +208,13 @@ Conn_Init( void )
                if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
        }
        
-       if (!array_alloc(&My_ConnArray, sizeof(CONNECTION),  Pool_Size)) {
+       if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), (size_t)Pool_Size)) {
                Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
                exit( 1 );
        }
 
-       /* XXX: My_Connetions/Pool_Size are needed by other parts of the code; remove them */
+       /* FIXME: My_Connetions/Pool_Size is needed by other parts of the
+        * code; remove them! */
        My_Connections = (CONNECTION*) array_start(&My_ConnArray);
 
        LogDebug("Allocated connection pool for %d items (%ld bytes).",
@@ -266,10 +259,11 @@ Conn_Exit( void )
 } /* Conn_Exit */
 
 
-static unsigned int
+static int
 ports_initlisteners(array *a, void (*func)(int,short))
 {
-       unsigned int created = 0, len;
+       int created = 0;
+       size_t len;
        int fd;
        UINT16 *port;
 
@@ -301,7 +295,7 @@ Conn_InitListeners( void )
 {
        /* Initialize ports on which the server should accept connections */
 
-       unsigned int created;
+       int created;
 
        if (!io_library_init(CONNECTION_POOL)) {
                Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
@@ -328,7 +322,7 @@ Conn_ExitListeners( void )
        Log( LOG_INFO, "Shutting down all listening sockets (%d total)...", arraylen );
        fd = array_start(&My_Listeners);
        while(arraylen--) {
-               assert(fd);
+               assert(fd != NULL);
                assert(*fd >= 0);
                io_close(*fd);
                LogDebug("Listening socket %d closed.", *fd );
@@ -354,7 +348,7 @@ NewListener( const UINT16 Port )
        /* Server-"Listen"-Socket initialisieren */
        memset( &addr, 0, sizeof( addr ));
        memset( &inaddr, 0, sizeof( inaddr ));
-       addr.sin_family = AF_INET;
+       addr.sin_family = (sa_family_t)AF_INET;
        addr.sin_port = htons( Port );
        if( Conf_ListenAddress[0] )
        {
@@ -381,8 +375,8 @@ NewListener( const UINT16 Port )
 
        if( ! Init_Socket( sock )) return -1;
 
-       if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 ) {
-               Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
+       if (bind(sock, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) != 0) {
+               Log( LOG_CRIT, "Can't bind socket (port %d) : %s!", Port, strerror( errno ));
                close( sock );
                return -1;
        }
@@ -424,8 +418,11 @@ NewListener( const UINT16 Port )
        }
 
        /* Add port number to description if non-standard */
-       if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port );
-       else strlcpy( name, info, sizeof( name ));
+       if (Port != 6667)
+               snprintf(name, sizeof name, "%s (port %u)", info,
+                        (unsigned int)Port);
+       else
+               strlcpy(name, info, sizeof name);
 
        /* Register service */
        Rendezvous_Register( name, MDNS_TYPE, Port );
@@ -482,7 +479,7 @@ Conn_Handler( void )
                        if ( My_Connections[i].sock <= NONE )
                                continue;
 
-                       wdatalen = array_bytes(&My_Connections[i].wbuf);
+                       wdatalen = (unsigned int)array_bytes(&My_Connections[i].wbuf);
 
 #ifdef ZLIB
                        if (( wdatalen > 0 ) || ( array_bytes(&My_Connections[i].zip.wbuf)> 0 ))
@@ -611,7 +608,7 @@ va_dcl
 
 
 GLOBAL bool
-Conn_Write( CONN_ID Idx, char *Data, unsigned int Len )
+Conn_Write( CONN_ID Idx, char *Data, size_t Len )
 {
        /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
         * der Client disconnectiert und false geliefert. */
@@ -696,11 +693,16 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        /* Mark link as "closing" */
        Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCLOSING );
                
-       if( LogMsg ) txt = LogMsg;
-       else txt = FwdMsg;
-       if( ! txt ) txt = "Reason unknown";
+       if (LogMsg)
+               txt = LogMsg;
+       else
+               txt = FwdMsg;
+       if (! txt)
+               txt = "Reason unknown";
 
-       Log( LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx, LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ));
+       Log(LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx,
+           LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host,
+           ntohs(My_Connections[Idx].addr.sin_port));
 
        /* Search client, if any */
        c = Conn_GetClient( Idx );
@@ -736,39 +738,57 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        c = Conn_GetClient( Idx );
 
        /* Shut down socket */
-       if( ! io_close( My_Connections[Idx].sock ))
-       {
+       if (! io_close(My_Connections[Idx].sock)) {
                /* Oops, we can't close the socket!? This is ... ugly! */
-               Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+               Log(LOG_CRIT,
+                   "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)",
+                   Idx, My_Connections[Idx].sock, My_Connections[Idx].host,
+                   ntohs(My_Connections[Idx].addr.sin_port), strerror(errno));
        }
 
        /* Mark socket as invalid: */
        My_Connections[Idx].sock = NONE;
 
        /* If there is still a client, unregister it now */
-       if( c ) Client_Destroy( c, LogMsg, FwdMsg, true );
+       if (c)
+               Client_Destroy(c, LogMsg, FwdMsg, true);
 
        /* Calculate statistics and log information */
        in_k = (double)My_Connections[Idx].bytes_in / 1024;
        out_k = (double)My_Connections[Idx].bytes_out / 1024;
 #ifdef ZLIB
-       if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
+       if (Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP)) {
                in_z_k = (double)My_Connections[Idx].zip.bytes_in / 1024;
                out_z_k = (double)My_Connections[Idx].zip.bytes_out / 1024;
+               /* Make sure that no division by zero can occur during
+                * the calculation of in_p and out_p: in_z_k and out_z_k
+                * are non-zero, that's guaranteed by the protocol until
+                * compression can be enabled. */
+               if (! in_z_k)
+                       in_z_k = in_k;
+               if (! out_z_k)
+                       out_z_k = out_k;
                in_p = (int)(( in_k * 100 ) / in_z_k );
                out_p = (int)(( out_k * 100 ) / out_z_k );
-               Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, in_z_k, in_p, out_k, out_z_k, out_p );
+               Log(LOG_INFO,
+                   "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).",
+                   Idx, My_Connections[Idx].host,
+                   ntohs(My_Connections[Idx].addr.sin_port),
+                   in_k, in_z_k, in_p, out_k, out_z_k, out_p);
        }
        else
 #endif
        {
-               Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k );
+               Log(LOG_INFO,
+                   "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).",
+                   Idx, My_Connections[Idx].host,
+                   ntohs(My_Connections[Idx].addr.sin_port),
+                   in_k, out_k);
        }
 
        /* cancel running resolver */
-       if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat)) {
+       if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat))
                Resolve_Shutdown(&My_Connections[Idx].res_stat);
-       }
 
        /* Servers: Modify time of next connect attempt? */
        Conf_UnsetServer( Idx );
@@ -785,6 +805,7 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
 
        array_free(&My_Connections[Idx].rbuf);
        array_free(&My_Connections[Idx].wbuf);
+
        /* Clean up connection structure (=free it) */
        Init_Conn_Struct( Idx );
 
@@ -824,13 +845,14 @@ Conn_SyncServerStruct( void )
 } /* SyncServerStruct */
 
 
+/**
+ * Send out data of write buffer; connect new sockets.
+ */
 static bool
 Handle_Write( CONN_ID Idx )
 {
-       /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
-
-       int len;
-       unsigned int wdatalen;
+       ssize_t len;
+       size_t wdatalen;
 
        assert( Idx > NONE );
        if ( My_Connections[Idx].sock < 0 ) {
@@ -839,9 +861,8 @@ Handle_Write( CONN_ID Idx )
        }
        assert( My_Connections[Idx].sock > NONE );
 
-       LogDebug("Handle_Write() called for connection %d ...", Idx);
-
        wdatalen = array_bytes(&My_Connections[Idx].wbuf );
+
 #ifdef ZLIB
        if (wdatalen == 0 && !array_bytes(&My_Connections[Idx].zip.wbuf)) {
                io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
@@ -859,8 +880,11 @@ Handle_Write( CONN_ID Idx )
        }
 #endif
 
-       /* Zip_Flush() may have changed the write buffer ... */
+       /* Zip_Flush() may have changed the write buffer ... */
        wdatalen = array_bytes(&My_Connections[Idx].wbuf);
+       LogDebug
+           ("Handle_Write() called for connection %d, %ld bytes pending ...",
+            Idx, wdatalen);
 
        len = write(My_Connections[Idx].sock,
                    array_start(&My_Connections[Idx].wbuf), wdatalen );
@@ -876,7 +900,7 @@ Handle_Write( CONN_ID Idx )
        }
 
        /* move any data not yet written to beginning */
-       array_moveleft(&My_Connections[Idx].wbuf, 1, len);
+       array_moveleft(&My_Connections[Idx].wbuf, 1, (size_t)len);
 
        return true;
 } /* Handle_Write */
@@ -898,11 +922,11 @@ New_Connection( int Sock )
 
        assert( Sock > NONE );
        /* Connection auf Listen-Socket annehmen */
-       new_sock_len = sizeof( new_addr );
-       new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
-       if( new_sock < 0 )
-       {
-               Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
+       new_sock_len = (int)sizeof new_addr;
+       new_sock = accept(Sock, (struct sockaddr *)&new_addr,
+                         (socklen_t *)&new_sock_len);
+       if (new_sock < 0) {
+               Log(LOG_CRIT, "Can't accept connection: %s!", strerror(errno));
                return -1;
        }
 
@@ -947,7 +971,8 @@ New_Connection( int Sock )
                        return -1;
                }
 
-               if (!array_alloc(&My_ConnArray, sizeof( CONNECTION ), new_sock)) {
+               if (!array_alloc(&My_ConnArray, sizeof(CONNECTION),
+                                (size_t)new_sock)) {
                        Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
                        Simple_Message( new_sock, "ERROR: Internal error" );
                        close( new_sock );
@@ -962,11 +987,19 @@ New_Connection( int Sock )
                        Init_Conn_Struct(Pool_Size++);
        }
 
+       /* register callback */
+       if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
+               Log(LOG_ALERT, "Can't accept connection: io_event_create failed!");
+               Simple_Message(new_sock, "ERROR :Internal error");
+               close(new_sock);
+               return -1;
+       }
+
        c = Client_NewLocal( new_sock, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
        if( ! c ) {
-               Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
-               Simple_Message( new_sock, "ERROR :Internal error" );
-               close( new_sock );
+               Log(LOG_ALERT, "Can't accept connection: can't create client structure!");
+               Simple_Message(new_sock, "ERROR :Internal error");
+               io_close(new_sock);
                return -1;
        }
 
@@ -975,13 +1008,6 @@ New_Connection( int Sock )
        My_Connections[new_sock].addr = new_addr;
        My_Connections[new_sock].client = c;
 
-       /* register callback */
-       if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
-               Simple_Message( new_sock, "ERROR :Internal error" );
-               Conn_Close( new_sock, "io_event_create() failed", NULL, false );
-               return -1;
-       }
-
        Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", new_sock,
                        inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
 
@@ -1023,7 +1049,7 @@ Read_Request( CONN_ID Idx )
        /* Daten von Socket einlesen und entsprechend behandeln.
         * Tritt ein Fehler auf, so wird der Socket geschlossen. */
 
-       int len;
+       ssize_t len;
        char readbuf[1024];
        CLIENT *c;
 
@@ -1061,10 +1087,14 @@ Read_Request( CONN_ID Idx )
                return;
        }
 #ifdef ZLIB
-       if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
-               if (!array_catb( &My_Connections[Idx].zip.rbuf, readbuf, len)) {
-                       Log( LOG_ERR, "Could not append recieved data to zip input buffer (connn %d): %d bytes!", Idx, len );
-                       Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
+       if (Conn_OPTION_ISSET(&My_Connections[Idx], CONN_ZIP)) {
+               if (!array_catb(&My_Connections[Idx].zip.rbuf, readbuf,
+                               (size_t) len)) {
+                       Log(LOG_ERR,
+                           "Could not append recieved data to zip input buffer (connn %d): %d bytes!",
+                           Idx, len);
+                       Conn_Close(Idx, "Receive buffer overflow!", NULL,
+                                  false);
                        return;
                }
        } else
@@ -1107,7 +1137,7 @@ Handle_Buffer( CONN_ID Idx )
        char *ptr1, *ptr2;
 #endif
        char *ptr;
-       int len, delta;
+       size_t len, delta;
        bool result;
        time_t starttime;
 #ifdef ZLIB
@@ -1156,7 +1186,7 @@ Handle_Buffer( CONN_ID Idx )
 
                len = ( ptr - (char*) array_start(&My_Connections[Idx].rbuf)) + delta;
 
-               if( len < 0 || len > ( COMMAND_LEN - 1 )) {
+               if( len > ( COMMAND_LEN - 1 )) {
                        /* Request must not exceed 512 chars (incl. CR+LF!), see
                         * RFC 2812. Disconnect Client if this happens. */
                        Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!",
@@ -1331,7 +1361,7 @@ New_Server( int Server )
        }
 
        memset( &new_addr, 0, sizeof( new_addr ));
-       new_addr.sin_family = AF_INET;
+       new_addr.sin_family = (sa_family_t)AF_INET;
        new_addr.sin_addr = inaddr;
        new_addr.sin_port = htons( Conf_Server[Server].port );
 
@@ -1343,15 +1373,18 @@ New_Server( int Server )
 
        if( ! Init_Socket( new_sock )) return;
 
-       res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
+       res = connect(new_sock, (struct sockaddr *)&new_addr,
+                       (socklen_t)sizeof(new_addr));
        if(( res != 0 ) && ( errno != EINPROGRESS )) {
                Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
                close( new_sock );
                return;
        }
        
-       if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), new_sock)) {
-               Log( LOG_ALERT, "Cannot allocate memory for server connection (socket %d)", new_sock);
+       if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), (size_t)new_sock)) {
+               Log(LOG_ALERT,
+                   "Cannot allocate memory for server connection (socket %d)",
+                   new_sock);
                close( new_sock );
                return;
        }
@@ -1598,7 +1631,7 @@ Conn_GetClient( CONN_ID Idx )
        CONNECTION *c;
        assert( Idx >= 0 );
 
-       c = array_get(&My_ConnArray, sizeof (CONNECTION), Idx);
+       c = array_get(&My_ConnArray, sizeof (CONNECTION), (size_t)Idx);
        
        assert(c != NULL);