]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
topic no longer limited to 127 chars (now only limited by protocol)
[ngircd-alex.git] / src / ngircd / conn.c
index 1f112273397d882f1e4641bfae0aad676cc1a5e8..07c504cc506825275979f490821b1eda50854b02 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.162 2005/07/11 20:58:05 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.166 2005/07/28 16:13:09 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -115,7 +115,7 @@ cb_listen(int sock, short irrelevant)
 
 
 static void
-cb_connserver(int sock, short what)
+cb_connserver(int sock, UNUSED short what)
 {
        int res, err;
        socklen_t sock_len;
@@ -201,6 +201,7 @@ FreeRes_stat( CONNECTION *c )
 
        io_close( c->res_stat->pipe[0] );
 
+       array_free(&c->res_stat->buffer);
        free( c->res_stat );
        c->res_stat = NULL;
 }
@@ -333,6 +334,7 @@ Conn_ExitListeners( void )
 } /* Conn_ExitListeners */
 
 
+/* return new listening port file descriptor or -1 on failure */
 LOCAL int
 NewListener( const UINT16 Port )
 {
@@ -436,16 +438,11 @@ NewListener( const UINT16 Port )
 GLOBAL void
 Conn_Handler( void )
 {
-       /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
-        * werden dabei durchgefuehrt, bis der Server terminieren oder neu
-        * starten soll:
-        *
-        *  - neue Verbindungen annehmen,
-        *  - Server-Verbindungen aufbauen,
-        *  - geschlossene Verbindungen loeschen,
-        *  - volle Schreibpuffer versuchen zu schreiben,
-        *  - volle Lesepuffer versuchen zu verarbeiten,
-        *  - Antworten von Resolver Sub-Prozessen annehmen.
+       /* "Main Loop.": Loop until a signal (for shutdown or restart) arrives.
+        * Call io_dispatch() to check for read/writeable sockets every second
+        * Wait for status change on pending connections (e.g: when the hostname has been resolved)
+        * check for penalty/timeouts
+        * handle input buffers
         */
        int i;
        unsigned int wdatalen;
@@ -486,7 +483,7 @@ Conn_Handler( void )
                for( i = 0; i < Pool_Size; i++ ) {
                        if ( My_Connections[i].sock <= NONE )
                                continue;
-                       
+
                        wdatalen = array_bytes(&My_Connections[i].wbuf);
 
 #ifdef ZLIB
@@ -733,7 +730,7 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
 
        /* Try to write out the write buffer */
        (void)Handle_Write( Idx );
-       
+
        /* Shut down socket */
        if( ! io_close( My_Connections[Idx].sock ))
        {
@@ -961,8 +958,7 @@ New_Connection( int Sock )
                }
 
                ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
-               if( ! ptr )
-               {
+               if( ! ptr ) {
                        Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
                        Simple_Message( new_sock, "ERROR: Internal error" );
                        close( new_sock );
@@ -986,8 +982,7 @@ New_Connection( int Sock )
 
        /* Client-Struktur initialisieren */
        c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
-       if( ! c )
-       {
+       if( ! c ) {
                Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
                Simple_Message( new_sock, "ERROR :Internal error" );
                close( new_sock );
@@ -1000,7 +995,11 @@ New_Connection( int Sock )
        My_Connections[idx].addr = new_addr;
 
        /* Neuen Socket registrieren */
-       io_event_create( new_sock, IO_WANTREAD, cb_clientserver);
+       if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
+               Simple_Message( new_sock, "ERROR :Internal error" );
+               Conn_Close( idx, "io_event_create() failed", NULL, false );
+               return;
+       }
 
        Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
 
@@ -1050,26 +1049,13 @@ Read_Request( CONN_ID Idx )
        /* Daten von Socket einlesen und entsprechend behandeln.
         * Tritt ein Fehler auf, so wird der Socket geschlossen. */
 
-       unsigned int bsize;
        int len;
        char readbuf[1024];
-#ifdef ZLIB
-       CLIENT *c;
-#endif
 
        assert( Idx > NONE );
        assert( My_Connections[Idx].sock > NONE );
 
-       /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
-        * ansonsten koennen Daten ggf. nicht umkopiert werden. */
-       bsize = READBUFFER_LEN;
 #ifdef ZLIB
-       c = Client_GetFromConn( Idx );
-
-       if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) &&
-                       ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN ))
-               bsize = ZREADBUFFER_LEN;
-
        if (( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN ) ||
                ( array_bytes(&My_Connections[Idx].zip.rbuf) >= ZREADBUFFER_LEN ))
 #else
@@ -1086,11 +1072,12 @@ Read_Request( CONN_ID Idx )
        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),
-                                               inet_ntoa( My_Connections[Idx].addr.sin_addr ));
+                       My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port),
+                                       inet_ntoa( My_Connections[Idx].addr.sin_addr ));
                Conn_Close( Idx, "Socket closed!", "Client closed connection", false );
                return;
        }
+
        if( len < 0 ) {
                if( errno == EAGAIN ) return;
                Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx,
@@ -1135,7 +1122,6 @@ Handle_Buffer( CONN_ID Idx )
 #endif
        char *ptr;
        int len, delta;
-       unsigned int arraylen;
        bool action, result;
 #ifdef ZLIB
        bool old_z;
@@ -1151,16 +1137,12 @@ Handle_Buffer( CONN_ID Idx )
                        if( ! Unzip_Buffer( Idx )) return false;
 #endif
 
-               arraylen = array_bytes(&My_Connections[Idx].rbuf);
-               if (arraylen == 0)
+               if (0 == array_bytes(&My_Connections[Idx].rbuf))
                        break;
 
-               if (!array_cat0(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
+               if (!array_cat0_temporary(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
                        return false;
 
-               array_truncate(&My_Connections[Idx].rbuf, 1, arraylen); /* do not count trailing NULL */
-
-
                /* A Complete Request end with CR+LF, see RFC 2812. */
                ptr = strstr( array_start(&My_Connections[Idx].rbuf), "\r\n" );
 
@@ -1207,7 +1189,7 @@ Handle_Buffer( CONN_ID Idx )
                        My_Connections[Idx].msg_in++;
                        if( ! Parse_Request( Idx, (char*)array_start(&My_Connections[Idx].rbuf) )) return false;
                        else action = true;
-                               
+
                        array_moveleft(&My_Connections[Idx].rbuf, 1, len);
 #ifdef DEBUG
                        Log(LOG_DEBUG, "%d byte left in rbuf", array_bytes(&My_Connections[Idx].rbuf));
@@ -1373,13 +1355,10 @@ New_Server( int Server, CONN_ID Idx )
        assert( Idx > NONE );
 
        /* Did we get a valid IP address? */
-       if( ! Conf_Server[Server].ip[0] )
-       {
+       if( ! Conf_Server[Server].ip[0] ) {
                /* No. Free connection structure and abort: */
-               Init_Conn_Struct( Idx );
-               Conf_Server[Server].conn_id = NONE;
                Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
-               return;
+               goto out;
        }
 
        Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Conf_Server[Server].port, Idx );
@@ -1393,10 +1372,8 @@ New_Server( int Server, CONN_ID Idx )
 #endif
        {
                /* Can't convert IP address */
-               Init_Conn_Struct( Idx );
-               Conf_Server[Server].conn_id = NONE;
                Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): can't convert ip address %s!", Conf_Server[Server].host, Idx, Conf_Server[Server].ip );
-               return;
+               goto out;
        }
 
        memset( &new_addr, 0, sizeof( new_addr ));
@@ -1407,10 +1384,8 @@ New_Server( int Server, CONN_ID Idx )
        new_sock = socket( PF_INET, SOCK_STREAM, 0 );
        if ( new_sock < 0 ) {
                /* Can't create socket */
-               Init_Conn_Struct( Idx );
-               Conf_Server[Server].conn_id = NONE;
                Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
-               return;
+               goto out;
        }
 
        if( ! Init_Socket( new_sock )) return;
@@ -1420,21 +1395,18 @@ New_Server( int Server, CONN_ID Idx )
                /* Can't connect socket */
                Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
                close( new_sock );
-               Init_Conn_Struct( Idx );
-               Conf_Server[Server].conn_id = NONE;
-               return;
+               goto out;
        }
 
        /* Client-Struktur initialisieren */
        c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
        if( ! c ) {
                /* Can't create new client structure */
-               close( new_sock );
-               Init_Conn_Struct( Idx );
-               Conf_Server[Server].conn_id = NONE;
                Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
-               return;
+               close( new_sock );
+               goto out;
        }
+
        Client_SetIntroducer( c, c );
        Client_SetToken( c, TOKEN_OUTBOUND );
 
@@ -1444,12 +1416,20 @@ New_Server( int Server, CONN_ID Idx )
        strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
 
        /* Register new socket */
-       io_event_create( new_sock, IO_WANTWRITE, cb_connserver);
-       Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
+       if (!io_event_create( new_sock, IO_WANTWRITE, cb_connserver)) {
+               Log( LOG_ALERT, "io_event_create(): could not add fd %d", strerror(errno));
+               Conn_Close( Idx, "io_event_create() failed", NULL, false );
+               goto out;
+       }
 
 #ifdef DEBUG
        Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
 #endif
+       Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
+       return;
+out:
+       Init_Conn_Struct( Idx );
+       Conf_Server[Server].conn_id = NONE;
 } /* New_Server */
 
 
@@ -1512,9 +1492,12 @@ void Read_Resolver_Result( int r_fd )
         * IDENT user name.*/
 
        CLIENT *c;
-       int len, i, n;
+       int bytes_read, i, n;
+       unsigned int len;
        RES_STAT *s;
        char *ptr;
+       char *bufptr;
+       char readbuf[HOST_LEN];
 
        Log( LOG_DEBUG, "Resolver: started, fd %d\n", r_fd );
        /* Search associated connection ... */
@@ -1541,28 +1524,41 @@ void Read_Resolver_Result( int r_fd )
        assert( s != NULL );
 
        /* Read result from pipe */
-       len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - s->bufpos - 1 );
-       if( len < 0 )
+       bytes_read = read( r_fd, readbuf, sizeof readbuf -1 );
+       if( bytes_read < 0 )
        {
                /* Error! */
                Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
                FreeRes_stat( &My_Connections[i] );
                return;
        }
-       s->bufpos += len;
-       s->buffer[s->bufpos] = '\0';
+       len = (unsigned int) bytes_read;
+       readbuf[len] = '\0';
+       if (!array_catb(&s->buffer, readbuf, len)) { 
+               Log( LOG_CRIT, "Resolver: Can't append result %s to buffer: %s", readbuf, strerror( errno ));
+               FreeRes_stat(&My_Connections[i]);
+               return;
+       }
+
+       if (!array_cat0_temporary(&s->buffer)) {
+               Log( LOG_CRIT, "Resolver: Can't append result %s to buffer: %s", readbuf, strerror( errno ));
+               FreeRes_stat(&My_Connections[i]);
+               return;
+       }
 
        /* If the result string is incomplete, return to main loop and
         * wait until we can read in more bytes. */
 #ifdef IDENTAUTH
 try_resolve:
 #endif
-       ptr = strchr( s->buffer, '\n' );
+       bufptr = (char*) array_start(&s->buffer);
+       assert(bufptr != NULL);
+       ptr = strchr( bufptr, '\n' );
        if( ! ptr ) return;
        *ptr = '\0';
 
 #ifdef DEBUG
-       Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%d bytes), stage %d.", s->buffer, len, s->stage );
+       Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%u bytes read), stage %d.", bufptr, len, s->stage);
 #endif
 
        /* Okay, we got a complete result: this is a host name for outgoing
@@ -1579,15 +1575,14 @@ try_resolve:
                {
                        switch(s->stage) {
                                case 0: /* host name */
-                               strlcpy( My_Connections[i].host, s->buffer, sizeof( My_Connections[i].host ));
-                               Client_SetHostname( c, s->buffer );
+                               strlcpy( My_Connections[i].host, bufptr, sizeof( My_Connections[i].host));
+
+                               Client_SetHostname( c, bufptr);
 #ifdef IDENTAUTH
                                /* clean up buffer for IDENT result */
-                               len = strlen( s->buffer ) + 1;
-                               assert((size_t) len <= sizeof( s->buffer ));
-                               memmove( s->buffer, s->buffer + len, sizeof( s->buffer ) - len );
-                               assert(len <= s->bufpos );
-                               s->bufpos -= len;
+                               len = strlen(bufptr) + 1;
+                               assert(len <= array_bytes(&s->buffer));
+                               array_moveleft(&s->buffer, 1, len);
 
                                /* Don't close pipe and clean up, but
                                 * instead wait for IDENT result */
@@ -1595,10 +1590,10 @@ try_resolve:
                                goto try_resolve;
 
                                case 1: /* IDENT user name */
-                               if( s->buffer[0] )
-                               {
-                                       Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, s->buffer );
-                                       Client_SetUser( c, s->buffer, true );
+                               if (array_bytes(&s->buffer)) {
+                                       bufptr = (char*) array_start(&s->buffer);
+                                       Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, bufptr);
+                                       Client_SetUser( c, bufptr, true );
                                }
                                else Log( LOG_INFO, "IDENT lookup for connection %ld: no result.", i );
 #endif
@@ -1619,8 +1614,9 @@ try_resolve:
                /* Search server ... */
                n = Conf_GetServer( i );
                assert( n > NONE );
-
-               strlcpy( Conf_Server[n].ip, s->buffer, sizeof( Conf_Server[n].ip ));
+               
+               bufptr = (char*) array_start(&s->buffer);
+               strlcpy( Conf_Server[n].ip, bufptr, sizeof( Conf_Server[n].ip ));
        }
 
        /* Clean up ... */