]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
removed obsolete "int Conn_MaxFD"
[ngircd-alex.git] / src / ngircd / conn.c
index e460bb48e6a92aa16aa9dda036995bf8e0c9f16d..bff3db61149f19cdb376afcb41f5f09d054a44c5 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.183 2005/09/24 02:48:46 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.187 2006/02/02 21:00:21 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -138,25 +138,27 @@ cb_connserver(int sock, UNUSED short what)
        res = getsockopt( My_Connections[idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
        assert( sock_len == sizeof( err ));
 
-       /* Fehler aufgetreten? */
-       if(( res != 0 ) || ( err != 0 )) {
-               if ( res != 0 ) 
-                       Log( LOG_CRIT, "getsockopt (connection %d): %s!", idx, strerror( errno ));
-               else
-                       Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!",
-                               My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port,
-                                                                               idx, strerror( err ));
-
-                       /* Clean up socket, connection and client structures */
-                       c = Client_GetFromConn( idx );
-                       if( c ) Client_DestroyNow( c );
-                       io_close( My_Connections[idx].sock );
-                       Init_Conn_Struct( idx );
-
-                       /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
-                       Conf_Server[Conf_GetServer( idx )].lasttry = time( NULL );
-                       Conf_UnsetServer( idx );
-                       return;
+       /* Error while connecting? */
+       if ((res != 0) || (err != 0)) {
+               if (res != 0)
+                       Log(LOG_CRIT, "getsockopt (connection %d): %s!",
+                           idx, strerror(errno));
+               else
+                       Log(LOG_CRIT,
+                           "Can't connect socket to \"%s:%d\" (connection %d): %s!",
+                           My_Connections[idx].host,
+                           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 = Client_GetFromConn(idx);
+               if (c)
+                       Client_DestroyNow(c);
+               Conn_Close(idx, "Can't connect!", NULL, false);
+               return;
        }
 
        Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
@@ -226,9 +228,6 @@ Conn_Init( void )
 
        array_free( &My_Listeners );
 
-       /* Groesster File-Descriptor fuer select() */
-       Conn_MaxFD = 0;
-
        /* Connection-Struktur initialisieren */
        for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
 
@@ -460,7 +459,9 @@ Conn_Handler( void )
 #endif
 
                /* Should the configuration be reloaded? */
-               if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
+               if (NGIRCd_SignalRehash) {
+                       NGIRCd_Rehash( );
+               }
 
                /* Check configured servers and established links */
                Check_Servers( );
@@ -731,9 +732,15 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
                        Conn_WriteStr(Idx, "ERROR :Closing connection.");
        }
 
-       /* Try to write out the write buffer */
+       /* Try to write out the write buffer. Note: Handle_Write() eventually
+        * removes the CLIENT structure associated with this connection if an
+        * error occurs! So we have to re-check if there is still an valid
+        * CLIENT structure after calling Handle_Write() ...*/
        (void)Handle_Write( Idx );
 
+       /* Search client, if any (re-check!) */
+       c = Client_GetFromConn( Idx );
+
        /* Shut down socket */
        if( ! io_close( My_Connections[Idx].sock ))
        {
@@ -803,10 +810,10 @@ Conn_SyncServerStruct( void )
        CONN_ID i;
        int c;
 
-       for( i = 0; i < Pool_Size; i++ )
-       {
+       for( i = 0; i < Pool_Size; i++ ) {
                /* Established connection? */
-               if( My_Connections[i].sock <= NONE ) continue;
+               if (My_Connections[i].sock < 0)
+                       continue;
 
                /* Server connection? */
                client = Client_GetFromConn( i );
@@ -1549,15 +1556,15 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events )
 
        /* Read result from pipe */
        len = Resolve_Read(&My_Connections[i].res_stat, readbuf, sizeof readbuf -1);
-       if (len == 0) 
-               return;
+       if (len == 0)
+               goto out;
 
        readbuf[len] = '\0';
        identptr = strchr(readbuf, '\n');
        assert(identptr != NULL);
        if (!identptr) {
                Log( LOG_CRIT, "Resolver: Got malformed result!");
-               return;
+               goto out;
        }
 
        *identptr = '\0';
@@ -1602,6 +1609,16 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events )
 
        /* Reset penalty time */
        Conn_ResetPenalty( i );
+       return;
+out:
+       if (My_Connections[i].sock == SERVER_WAIT) {
+               n = Conf_GetServer( i );
+               assert(n > NONE );
+               if (n > NONE) {
+                       Conf_Server[n].conn_id = NONE;
+                       Init_Conn_Struct(i);
+               }
+       }
 } /* cb_Read_Resolver_Result */