]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
- "schneller Server-Reconnect" wird nun nur dann ausgefuehrt, wenn die
[ngircd-alex.git] / src / ngircd / conn.c
index d4ecc1ade8fb87a53db4a4ee60f71d7f7aaa8d7a..d694c5e9e2b22a903cf2e9bbe6bc525248309ab8 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.68 2002/08/26 00:03:15 alex Exp $
+ * $Id: conn.c,v 1.80 2002/10/09 17:07:22 alex Exp $
  *
  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
  */
@@ -75,6 +75,7 @@ typedef struct _Connection
        time_t lastping;                /* Letzter PING */
        time_t lastprivmsg;             /* Letzte PRIVMSG */
        time_t delaytime;               /* Nicht beachten bis ("penalty") */
+       LONG bytes_in, bytes_out;       /* Counter fuer Statistik */
 } CONNECTION;
 
 
@@ -220,8 +221,10 @@ Conn_NewListener( CONST UINT Port )
 GLOBAL VOID
 Conn_Handler( VOID )
 {
-       /* Aktive Verbindungen ueberwachen. Folgende Aktionen
-        * werden durchgefuehrt:
+       /* "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,
@@ -242,10 +245,6 @@ Conn_Handler( VOID )
 
                Check_Connections( );
 
-               /* Timeout initialisieren */
-               tv.tv_sec = 1;
-               tv.tv_usec = 0;
-
                /* noch volle Lese-Buffer suchen */
                for( i = 0; i < MAX_CONNECTIONS; i++ )
                {
@@ -291,6 +290,7 @@ Conn_Handler( VOID )
                        {
                                /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
                                FD_CLR( My_Connections[i].sock, &read_sockets );
+                               FD_CLR( My_Connections[i].sock, &write_sockets );
                        }
                }
                for( i = 0; i < Conn_MaxFD + 1; i++ )
@@ -302,9 +302,20 @@ Conn_Handler( VOID )
                        }
                }
 
+               /* Timeout initialisieren */
+               tv.tv_sec = 1;
+               tv.tv_usec = 0;
+               
                /* Auf Aktivitaet warten */
-               if( select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv ) == -1 )
+               i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
+               if( i == 0 )
                {
+                       /* keine Veraenderung an den Sockets */
+                       continue;
+               }
+               if( i == -1 )
+               {
+                       /* Fehler (z.B. Interrupt) */
                        if( errno != EINTR )
                        {
                                Log( LOG_EMERG, "select(): %s!", strerror( errno ));
@@ -349,7 +360,6 @@ va_dcl
        va_list ap;
 
        assert( Idx >= 0 );
-       assert( My_Connections[Idx].sock > NONE );
        assert( Format != NULL );
 
 #ifdef PROTOTYPES
@@ -383,10 +393,20 @@ Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
         * der Client disconnectiert und FALSE geliefert. */
 
        assert( Idx >= 0 );
-       assert( My_Connections[Idx].sock > NONE );
        assert( Data != NULL );
        assert( Len > 0 );
 
+       /* Ist der entsprechende Socket ueberhaupt noch offen?
+        * In einem "Handler-Durchlauf" kann es passieren, dass
+        * dem nicht mehr so ist, wenn einer von mehreren
+        * Conn_Write()'s fehlgeschlagen ist. In diesem Fall
+        * wird hier einfach ein Fehler geliefert. */
+       if( My_Connections[Idx].sock <= NONE )
+       {
+               Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
+               return FALSE;
+       }
+
        /* pruefen, ob Daten im Schreibpuffer sind. Wenn ja, zunaechst
         * pruefen, ob diese gesendet werden koennen */
        if( My_Connections[Idx].wdatalen > 0 )
@@ -428,8 +448,17 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
        assert( Idx >= 0 );
        assert( My_Connections[Idx].sock > NONE );
 
+       c = Client_GetFromConn( Idx );
+
        if( InformClient )
        {
+               /* Statistik an Client melden, wenn User */
+               if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
+               {
+                       Conn_WriteStr( Idx, "NOTICE %s :%sConnection statistics: %.1f kb received, %.1f kb sent.", Client_ThisServer( ), NOTICE_TXTPREFIX, (DOUBLE)My_Connections[Idx].bytes_in / 1024,  (DOUBLE)My_Connections[Idx].bytes_out / 1024 );
+               }
+
+               /* ERROR an Client schicken (von RFC so vorgesehen!) */
                if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
                else Conn_WriteStr( Idx, "ERROR :Closing connection." );
                if( My_Connections[Idx].sock == NONE ) return;
@@ -444,7 +473,6 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
                Log( LOG_INFO, "Connection %d with %s:%d closed.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
        }
 
-       c = Client_GetFromConn( Idx );
        if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
 
        if( My_Connections[Idx].res_stat )
@@ -456,12 +484,13 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
                free( My_Connections[Idx].res_stat );
        }
 
-       /* Bei Server-Verbindungen lasttry-Zeitpunkt so setzen, dass
-        * der naechste Verbindungsversuch in RECONNECT_DELAY Sekunden
-        * gestartet wird. */
-       if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry <  time( NULL )))
+       /* Startzeit des naechsten Connect-Versuchs modifizieren? */
+       if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry <  time( NULL ) - Conf_ConnectRetry ))
        {
-               /* Okay, die Verbindung stand schon "genuegend lange" */
+               /* Okay, die Verbindung stand schon "genuegend lange":
+                * lasttry-Zeitpunkt so setzen, dass der naechste
+                * Verbindungsversuch in RECONNECT_DELAY Sekunden
+                * gestartet wird. */
                Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
        }
 
@@ -626,7 +655,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, PASSSERVERADD );
+               Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd, NGIRCd_ProtoID );
                Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
 
                return TRUE;
@@ -644,6 +673,9 @@ Handle_Write( CONN_ID Idx )
                return FALSE;
        }
 
+       /* Connection-Statistik aktualisieren */
+       My_Connections[Idx].bytes_out += len;
+
        /* Puffer anpassen */
        My_Connections[Idx].wdatalen -= len;
        memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
@@ -708,6 +740,7 @@ New_Connection( INT Sock )
        if( s )
        {
                /* Sub-Prozess wurde asyncron gestartet */
+               Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX );
                My_Connections[idx].res_stat = s;
        }
        else
@@ -775,6 +808,9 @@ Read_Request( CONN_ID Idx )
                return;
        }
 
+       /* Connection-Statistik aktualisieren */
+       My_Connections[Idx].bytes_in += len;
+
        /* Lesebuffer updaten */
        My_Connections[Idx].rdatalen += len;
        assert( My_Connections[Idx].rdatalen < READBUFFER_LEN );
@@ -1073,6 +1109,8 @@ Init_Conn_Struct( INT Idx )
        My_Connections[Idx].lastping = 0;
        My_Connections[Idx].lastprivmsg = time( NULL );
        My_Connections[Idx].delaytime = 0;
+       My_Connections[Idx].bytes_in = 0;
+       My_Connections[Idx].bytes_out = 0;
 } /* Init_Conn_Struct */
 
 
@@ -1114,7 +1152,7 @@ Read_Resolver_Result( INT r_fd )
        FD_CLR( r_fd, &Resolver_FDs );
 
        /* Anfrage vom Parent lesen */
-       len = read( r_fd, result, HOST_LEN);
+       len = read( r_fd, result, HOST_LEN - 1 );
        if( len < 0 )
        {
                /* Fehler beim Lesen aus der Pipe */
@@ -1151,6 +1189,8 @@ Read_Resolver_Result( INT r_fd )
                assert( c != NULL );
                strcpy( My_Connections[i].host, result );
                Client_SetHostname( c, result );
+
+               Conn_WriteStr( i, "NOTICE AUTH :%sGot your hostname.", NOTICE_TXTPREFIX );
        }
        else
        {