]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
- Penalty-Delays gelten nun auch für Schreibvorgaenge.
[ngircd-alex.git] / src / ngircd / conn.c
index d6ceb629f4f4ed7e4c642cf94fe568b54ad6c889..76b0822baff04471d138c062074892a4d08b3741 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.65 2002/05/27 13:09:26 alex Exp $
+ * $Id: conn.c,v 1.72 2002/09/07 22:34:44 alex Exp $
  *
  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
  */
@@ -74,6 +74,7 @@ typedef struct _Connection
        time_t lastdata;                /* Letzte Aktivitaet */
        time_t lastping;                /* Letzter PING */
        time_t lastprivmsg;             /* Letzte PRIVMSG */
+       time_t delaytime;               /* Nicht beachten bis ("penalty") */
 } CONNECTION;
 
 
@@ -147,7 +148,11 @@ Conn_Exit( VOID )
                                close( i );
                                Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
                        }
-                       else if( idx < MAX_CONNECTIONS ) Conn_Close( idx, NULL, "Server going down", TRUE );
+                       else if( idx < MAX_CONNECTIONS )
+                       {
+                               if( NGIRCd_Restart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
+                               else Conn_Close( idx, NULL, "Server going down", TRUE );
+                       }
                        else
                        {
                                Log( LOG_WARNING, "Closing unknown connection %d ...", i );
@@ -213,11 +218,12 @@ Conn_NewListener( CONST UINT Port )
 
 
 GLOBAL VOID
-Conn_Handler( INT Timeout )
+Conn_Handler( VOID )
 {
-       /* Aktive Verbindungen ueberwachen. Mindestens alle "Timeout"
-        * Sekunden wird die Funktion verlassen. 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,
@@ -228,20 +234,16 @@ Conn_Handler( INT Timeout )
 
        fd_set read_sockets, write_sockets;
        struct timeval tv;
-       time_t start;
+       time_t start, t;
        INT i;
 
        start = time( NULL );
-       while(( time( NULL ) - start < Timeout ) && ( ! NGIRCd_Quit ))
+       while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
        {
                Check_Servers( );
 
                Check_Connections( );
 
-               /* Timeout initialisieren */
-               tv.tv_sec = 0;
-               tv.tv_usec = 50000;
-
                /* noch volle Lese-Buffer suchen */
                for( i = 0; i < MAX_CONNECTIONS; i++ )
                {
@@ -269,6 +271,7 @@ Conn_Handler( INT Timeout )
                }
 
                /* von welchen Sockets koennte gelesen werden? */
+               t = time( NULL );
                read_sockets = My_Sockets;
                for( i = 0; i < MAX_CONNECTIONS; i++ )
                {
@@ -282,6 +285,12 @@ Conn_Handler( INT Timeout )
                                /* Hier laeuft noch ein asyncrones connect() */
                                FD_CLR( My_Connections[i].sock, &read_sockets );
                        }
+                       if( My_Connections[i].delaytime > t )
+                       {
+                               /* 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++ )
                {
@@ -292,13 +301,24 @@ Conn_Handler( INT Timeout )
                        }
                }
 
+               /* 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 ));
-                               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
+                               Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
                                exit( 1 );
                        }
                        continue;
@@ -319,8 +339,16 @@ Conn_Handler( INT Timeout )
 } /* Conn_Handler */
 
 
+#ifdef PROTOTYPES
 GLOBAL BOOLEAN
 Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
+#else
+GLOBAL BOOLEAN
+Conn_WriteStr( Idx, Format, va_alist )
+CONN_ID Idx;
+CHAR *Format;
+va_dcl
+#endif
 {
        /* String in Socket schreiben. CR+LF wird von dieser Funktion
         * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
@@ -333,8 +361,12 @@ Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
        assert( Idx >= 0 );
        assert( My_Connections[Idx].sock > NONE );
        assert( Format != NULL );
-       
+
+#ifdef PROTOTYPES
        va_start( ap, Format );
+#else
+       va_start( ap );
+#endif
        if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
        {
                Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
@@ -479,6 +511,24 @@ Conn_LastPing( CONN_ID Idx )
 } /* Conn_LastPing */
 
 
+GLOBAL VOID
+Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
+{
+       /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
+        * waehrend dieser Zeit wird der entsprechende Socket vom Server
+        * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
+        * dieser Funktion nur erhoeht, nicht aber verringert werden. */
+       
+       time_t t;
+       
+       assert( Idx >= 0 );
+       assert( Seconds >= 0 );
+       
+       t = time( NULL ) + Seconds;
+       if( t > My_Connections[Idx].delaytime ) My_Connections[Idx].delaytime = t;
+} /* Conn_SetPenalty */
+
+
 LOCAL BOOLEAN
 Try_Write( CONN_ID Idx )
 {
@@ -586,7 +636,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 "PASSSERVERADD, Conf_Server[My_Connections[Idx].our_server].pwd );
+               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;
@@ -676,6 +726,9 @@ New_Connection( INT Sock )
                strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
                Client_SetHostname( c, My_Connections[idx].host );
        }
+       
+       /* Penalty-Zeit setzen */
+       Conn_SetPenalty( idx, 1 );
 } /* New_Connection */
 
 
@@ -781,7 +834,7 @@ Handle_Buffer( CONN_ID Idx )
                        /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
                        * (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!", Idx, My_Connections[Idx].rdatalen );
+                       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;
                }
@@ -1029,6 +1082,7 @@ Init_Conn_Struct( INT Idx )
        My_Connections[Idx].lastdata = time( NULL );
        My_Connections[Idx].lastping = 0;
        My_Connections[Idx].lastprivmsg = time( NULL );
+       My_Connections[Idx].delaytime = 0;
 } /* Init_Conn_Struct */