]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
- volle Lesebuffer und Timeouts behandelt Conn_Handle() nun besser.
[ngircd-alex.git] / src / ngircd / conn.c
index cbef7f3e6ccf438395b3d0e9a470e32bae815e54..b941aefc8fb82466fd631038e411292f7f09cb09 100644 (file)
@@ -9,7 +9,7 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: conn.c,v 1.89 2002/11/11 00:54:25 alex Exp $
+ * $Id: conn.c,v 1.95 2002/11/23 17:04:07 alex Exp $
  *
  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
  */
@@ -86,7 +86,7 @@ LOCAL VOID New_Connection PARAMS(( INT Sock ));
 LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
 LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
 LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
-LOCAL VOID Handle_Buffer PARAMS(( CONN_ID Idx ));
+LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
 LOCAL VOID Check_Connections PARAMS(( VOID ));
 LOCAL VOID Check_Servers PARAMS(( VOID ));
 LOCAL VOID Init_Conn_Struct PARAMS(( LONG Idx ));
@@ -187,6 +187,43 @@ Conn_Exit( VOID )
 } /* Conn_Exit */
 
 
+GLOBAL INT
+Conn_InitListeners( VOID )
+{
+       /* Ports, auf denen der Server Verbindungen entgegennehmen
+       * soll, initialisieren */
+
+       INT created, i;
+
+       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!", Conf_ListenPorts[i] );
+       }
+       return created;
+} /* Conn_InitListeners */
+
+
+GLOBAL VOID
+Conn_ExitListeners( VOID )
+{
+       /* Alle "Listen-Sockets" schliessen */
+
+       INT i;
+
+       Log( LOG_INFO, "Shutting down all listening sockets ..." );
+       for( i = 0; i < Conn_MaxFD + 1; i++ )
+       {
+               if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
+               {
+                       close( i );
+                       Log( LOG_DEBUG, "Listening socket %d closed.", i );
+               }
+       }
+} /* Conn_ExitListeners */
+
+
 GLOBAL BOOLEAN
 Conn_NewListener( CONST UINT Port )
 {
@@ -260,10 +297,13 @@ Conn_Handler( VOID )
        struct timeval tv;
        time_t start, t;
        LONG i, idx;
+       BOOLEAN timeout;
 
        start = time( NULL );
        while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
        {
+               timeout = TRUE;
+       
                Check_Servers( );
 
                Check_Connections( );
@@ -274,7 +314,7 @@ Conn_Handler( VOID )
                        if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
                        {
                                /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
-                               Handle_Buffer( i );
+                               if( Handle_Buffer( i )) timeout = FALSE;
                        }
                }
 
@@ -326,8 +366,9 @@ Conn_Handler( VOID )
                }
 
                /* Timeout initialisieren */
-               tv.tv_sec = 1;
                tv.tv_usec = 0;
+               if( timeout ) tv.tv_sec = TIME_RES;
+               else tv.tv_sec = 0;
                
                /* Auf Aktivitaet warten */
                i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
@@ -500,11 +541,11 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
 
        if( close( My_Connections[Idx].sock ) != 0 )
        {
-               Log( LOG_ERR, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+               Log( LOG_ERR, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
        }
        else
        {
-               Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed.", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
+               Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (%.1fK in/%.1fK out).", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), (DOUBLE)My_Connections[Idx].bytes_in / 1024,  (DOUBLE)My_Connections[Idx].bytes_out / 1024 );
        }
        
        /* Socket als "ungueltig" markieren */
@@ -679,14 +720,18 @@ Try_Write( CONN_ID Idx )
         * Socket zu schreiben. */
 
        fd_set write_socket;
+       struct timeval tv;
 
        assert( Idx > NONE );
        assert( My_Connections[Idx].sock > NONE );
        assert( My_Connections[Idx].wdatalen > 0 );
 
+       /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
+       tv.tv_sec = 0; tv.tv_usec = 0;
+
        FD_ZERO( &write_socket );
        FD_SET( My_Connections[Idx].sock, &write_socket );
-       if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, 0 ) == -1 )
+       if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
        {
                /* Fehler! */
                if( errno != EINTR )
@@ -779,7 +824,7 @@ Handle_Write( CONN_ID Idx )
                Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port );
 
                /* PASS und SERVER verschicken */
-               Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd, NGIRCd_ProtoID );
+               Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd_out, NGIRCd_ProtoID );
                return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
        }
 
@@ -789,6 +834,9 @@ Handle_Write( CONN_ID Idx )
        len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
        if( len < 0 )
        {
+               /* Operation haette Socket "nur" blockiert ... */
+               if( errno == EAGAIN ) return TRUE;
+
                /* Oops, ein Fehler! */
                Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
                Conn_Close( Idx, "Write error!", NULL, FALSE );
@@ -822,6 +870,7 @@ 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 )
@@ -830,6 +879,9 @@ New_Connection( INT Sock )
                return;
        }
 
+       /* Socket initialisieren */
+       Init_Socket( new_sock );
+
        /* Freie Connection-Struktur suchen */
        for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
        if( idx >= Pool_Size )
@@ -900,6 +952,8 @@ New_Connection( INT Sock )
        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 );
 
        /* Hostnamen ermitteln */
+       strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
+       Client_SetHostname( c, My_Connections[idx].host );
        s = Resolve_Addr( &new_addr );
        if( s )
        {
@@ -907,12 +961,6 @@ New_Connection( INT Sock )
                Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX );
                My_Connections[idx].res_stat = s;
        }
-       else
-       {
-               /* kann Namen nicht aufloesen, daher wird die IP-Adresse verwendet */
-               strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
-               Client_SetHostname( c, My_Connections[idx].host );
-       }
        
        /* Penalty-Zeit setzen */
        Conn_SetPenalty( idx, 4 );
@@ -972,6 +1020,9 @@ Read_Request( CONN_ID Idx )
 
        if( len < 0 )
        {
+               /* Operation haette Socket "nur" blockiert ... */
+               if( errno == EAGAIN ) return;
+
                /* Fehler beim Lesen */
                Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
                Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
@@ -993,16 +1044,19 @@ Read_Request( CONN_ID Idx )
 } /* Read_Request */
 
 
-LOCAL VOID
+LOCAL BOOLEAN
 Handle_Buffer( CONN_ID Idx )
 {
-       /* Daten im Lese-Puffer einer Verbindung verarbeiten. */
+       /* Daten im Lese-Puffer einer Verbindung verarbeiten.
+        * Wurde ein Request verarbeitet, so wird TRUE geliefert,
+        * ansonsten FALSE (auch bei Fehlern). */
 
 #ifndef STRICT_RFC
        CHAR *ptr1, *ptr2;
 #endif
        CHAR *ptr;
        INT len, delta;
+       BOOLEAN action;
 
        /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
         * RFC 2812. Haben wir eine? */
@@ -1023,6 +1077,7 @@ Handle_Buffer( CONN_ID Idx )
        }
 #endif
 
+       action = FALSE;
        if( ptr )
        {
                /* Ende der Anfrage wurde gefunden */
@@ -1031,23 +1086,26 @@ Handle_Buffer( CONN_ID Idx )
                if( len > ( COMMAND_LEN - 1 ))
                {
                        /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
-                       * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
-                       * empfangen wird, wird der Client disconnectiert. */
+                        * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
+                        * empfangen wird, wird der Client disconnectiert. */
                        Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
                        Conn_Close( Idx, NULL, "Request too long", TRUE );
-                       return;
+                       return FALSE;
                }
 
                if( len > delta )
                {
                        /* Es wurde ein Request gelesen */
-                       if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
+                       if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
+                       else action = TRUE;
                }
 
                /* Puffer anpassen */
                My_Connections[Idx].rdatalen -= len;
                memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
        }
+       
+       return action;
 } /* Handle_Buffer */
 
 
@@ -1162,19 +1220,16 @@ Check_Servers( VOID )
                My_Connections[idx].sock = SERVER_WAIT;
                My_Connections[idx].our_server = i;
 
-               /* Hostnamen in IP aufloesen */
+               /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den
+                * konfigurierten Text direkt als IP-Adresse zu verwenden ... */
+               strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
+               strcpy( My_Connections[idx].host, Conf_Server[i].host );
                s = Resolve_Name( Conf_Server[i].host );
                if( s )
                {
                        /* Sub-Prozess wurde asyncron gestartet */
                        My_Connections[idx].res_stat = s;
                }
-               else
-               {
-                       /* kann Namen nicht aufloesen: nun versuchen wir einfach,
-                        * den "Text" direkt als IP-Adresse zu verwenden ... */
-                       strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
-               }
        }
 } /* Check_Servers */
 
@@ -1352,6 +1407,8 @@ Read_Resolver_Result( INT r_fd )
                return;
        }
 
+       Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
+       
        /* Aufraeumen */
        close( My_Connections[i].res_stat->pipe[0] );
        close( My_Connections[i].res_stat->pipe[1] );