]> arthur.barton.de Git - ngircd.git/blobdiff - src/ngircd/conn.c
fix embarassing buffer-off-by one
[ngircd.git] / src / ngircd / conn.c
index b63a89fbade8ecc17412cf54ffbe39e052f30131..1f112273397d882f1e4641bfae0aad676cc1a5e8 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.158 2005/07/08 16:18:39 alex Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.162 2005/07/11 20:58:05 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -94,6 +94,7 @@ LOCAL bool Init_Socket PARAMS(( int Sock ));
 LOCAL void New_Server PARAMS(( int Server, CONN_ID Idx ));
 LOCAL void Simple_Message PARAMS(( int Sock, char *Msg ));
 LOCAL int Count_Connections PARAMS(( struct sockaddr_in addr ));
+LOCAL int NewListener PARAMS(( const UINT16 Port ));
 
 static array My_Listeners;
 
@@ -180,7 +181,7 @@ cb_clientserver(int sock, short what)
 #endif
                io_close(sock);
                return;
-       }
+       }
 
        if (what & IO_WANTREAD)
                Read_Request( idx );
@@ -278,7 +279,8 @@ Conn_InitListeners( void )
 {
        /* Initialize ports on which the server should accept connections */
 
-       int created, i;
+       int created, fd;
+       unsigned int i;
 
        if (!io_library_init(CONNECTION_POOL)) {
                Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
@@ -286,10 +288,19 @@ Conn_InitListeners( void )
        }
 
        created = 0;
-       for( i = 0; i < Conf_ListenPorts_Count; i++ )
-       {
-               if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
-               else Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
+       for( i = 0; i < Conf_ListenPorts_Count; i++ ) {
+               fd = NewListener( Conf_ListenPorts[i] );
+               if (fd < 0) {
+                       Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
+                       continue;
+               }
+               if (!io_event_create( fd, IO_WANTREAD, cb_listen )) {
+                       Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!",
+                                               fd, (unsigned int) Conf_ListenPorts[i], strerror(errno));
+                       close(fd);
+                       continue;
+               }
+               created++;
        }
        return created;
 } /* Conn_InitListeners */
@@ -311,8 +322,8 @@ Conn_ExitListeners( void )
                fd = (int*) array_get(&My_Listeners, sizeof (int), arraylen);
                if (fd) {
                        close(*fd);
-                       Log( LOG_DEBUG, "Listening socket %d closed.", *fd );
 #ifdef DEBUG
+                       Log( LOG_DEBUG, "Listening socket %d closed.", *fd );
                } else {
                        Log( LOG_DEBUG, "array_get pos %d returned NULL", arraylen );
 #endif
@@ -322,8 +333,8 @@ Conn_ExitListeners( void )
 } /* Conn_ExitListeners */
 
 
-GLOBAL bool
-Conn_NewListener( const UINT16 Port )
+LOCAL int
+NewListener( const UINT16 Port )
 {
        /* Create new listening socket on specified port */
 
@@ -349,7 +360,7 @@ Conn_NewListener( const UINT16 Port )
 #endif
                {
                        Log( LOG_CRIT, "Can't listen on %s:%u: can't convert ip address %s!", Conf_ListenAddress, Port, Conf_ListenAddress );
-                       return false;
+                       return -1;
                }
        }
        else inaddr.s_addr = htonl( INADDR_ANY );
@@ -360,17 +371,17 @@ Conn_NewListener( const UINT16 Port )
        if( sock < 0 )
        {
                Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
-               return false;
+               return -1;
        }
 
-       if( ! Init_Socket( sock )) return false;
+       if( ! Init_Socket( sock )) return -1;
 
        /* an Port binden */
        if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
        {
                Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
                close( sock );
-               return false;
+               return -1;
        }
 
        /* in "listen mode" gehen :-) */
@@ -378,16 +389,15 @@ Conn_NewListener( const UINT16 Port )
        {
                Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
                close( sock );
-               return false;
+               return -1;
        }
 
        /* Neuen Listener in Strukturen einfuegen */
        if (!array_catb( &My_Listeners,(char*) &sock, sizeof(int) )) {
                Log( LOG_CRIT, "Can't add socket to My_Listeners array: %s!", strerror( errno ));
                close( sock );
-               return false;
+               return -1;
        }
-       io_event_create( sock, IO_WANTREAD, cb_listen ); 
 
        if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
        else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
@@ -419,8 +429,8 @@ Conn_NewListener( const UINT16 Port )
        /* Register service */
        Rendezvous_Register( name, MDNS_TYPE, Port );
 #endif
-       return true;
-} /* Conn_NewListener */
+       return sock;
+} /* NewListener */
 
 
 GLOBAL void
@@ -488,7 +498,6 @@ Conn_Handler( void )
                                /* Socket der Verbindung in Set aufnehmen */
                                io_event_add( My_Connections[i].sock, IO_WANTWRITE );
                        }
-
                }
 
                /* von welchen Sockets koennte gelesen werden? */
@@ -1060,9 +1069,7 @@ Read_Request( CONN_ID Idx )
        if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) &&
                        ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN ))
                bsize = ZREADBUFFER_LEN;
-#endif
 
-#ifdef ZLIB
        if (( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN ) ||
                ( array_bytes(&My_Connections[Idx].zip.rbuf) >= ZREADBUFFER_LEN ))
 #else
@@ -1076,7 +1083,7 @@ Read_Request( CONN_ID Idx )
                return;
        }
 
-       len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf );
+       len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf -1 );
        if( len == 0 ) {
                Log( LOG_INFO, "%s:%d (%s) is closing the connection ...",
                         My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port),
@@ -1135,8 +1142,7 @@ Handle_Buffer( CONN_ID Idx )
 #endif
 
        result = false;
-       do
-       {
+       do {
                /* Check penalty */
                if( My_Connections[Idx].delaytime > time( NULL )) return result;
 #ifdef ZLIB
@@ -1238,26 +1244,21 @@ Handle_Buffer( CONN_ID Idx )
 LOCAL void
 Check_Connections( void )
 {
-       /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
-        * nicht der Fall, zunaechst PING-PONG spielen und, wenn
-        * auch das nicht "hilft", Client disconnectieren. */
-
+       /* check if connections are alive. if not, play PING-PONG first.
+        * if this doesn't help either, disconnect client. */
        CLIENT *c;
        CONN_ID i;
 
-       for( i = 0; i < Pool_Size; i++ )
-       {
+       for( i = 0; i < Pool_Size; i++ ) {
                if( My_Connections[i].sock == NONE ) continue;
 
                c = Client_GetFromConn( i );
                if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
                {
-                       /* verbundener User, Server oder Service */
-                       if( My_Connections[i].lastping > My_Connections[i].lastdata )
-                       {
-                               /* es wurde bereits ein PING gesendet */
-                               if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
-                               {
+                       /* connected User, Server or Service */
+                       if( My_Connections[i].lastping > My_Connections[i].lastdata ) {
+                               /* we already sent a ping */
+                               if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout ) {
                                        /* Timeout */
 #ifdef DEBUG
                                        Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
@@ -1265,9 +1266,8 @@ Check_Connections( void )
                                        Conn_Close( i, NULL, "Ping timeout", true );
                                }
                        }
-                       else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
-                       {
-                               /* es muss ein PING gesendet werden */
+                       else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout ) {
+                               /* we need to sent a PING */
 #ifdef DEBUG
                                Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
 #endif
@@ -1277,7 +1277,7 @@ Check_Connections( void )
                }
                else
                {
-                       /* noch nicht vollstaendig aufgebaute Verbindung */
+                       /* connection is not fully established yet */
                        if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
                        {
                                /* Timeout */
@@ -1405,8 +1405,7 @@ New_Server( int Server, CONN_ID Idx )
        new_addr.sin_port = htons( Conf_Server[Server].port );
 
        new_sock = socket( PF_INET, SOCK_STREAM, 0 );
-       if ( new_sock < 0 )
-       {
+       if ( new_sock < 0 ) {
                /* Can't create socket */
                Init_Conn_Struct( Idx );
                Conf_Server[Server].conn_id = NONE;
@@ -1417,8 +1416,7 @@ New_Server( int Server, CONN_ID Idx )
        if( ! Init_Socket( new_sock )) return;
 
        res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
-       if(( res != 0 ) && ( errno != EINPROGRESS ))
-       {
+       if(( res != 0 ) && ( errno != EINPROGRESS )) {
                /* Can't connect socket */
                Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
                close( new_sock );
@@ -1429,8 +1427,7 @@ New_Server( int Server, CONN_ID Idx )
 
        /* Client-Struktur initialisieren */
        c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
-       if( ! c )
-       {
+       if( ! c ) {
                /* Can't create new client structure */
                close( new_sock );
                Init_Conn_Struct( Idx );