]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
- Bei ausgehenden Verbindungen wird der Ziel-Port ins Log geschrieben.
[ngircd-alex.git] / src / ngircd / conn.c
index 8e2455de0f59c722ef3f288c1b59752f15821ecc..df12bb29b00ca7e5dcf1d06e260a1c9ba4ee1753 100644 (file)
@@ -9,11 +9,40 @@
  * 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.29 2002/01/04 01:36:40 alex Exp $
+ * $Id: conn.c,v 1.38 2002/02/19 20:34:31 alex Exp $
  *
  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
  *
  * $Log: conn.c,v $
+ * Revision 1.38  2002/02/19 20:34:31  alex
+ * - Bei ausgehenden Verbindungen wird der Ziel-Port ins Log geschrieben.
+ *
+ * Revision 1.37  2002/02/19 20:05:37  alex
+ * - "Passive-Mode" implementiert: kein Auto-Conect zu anderen Servern.
+ *
+ * Revision 1.36  2002/02/11 01:00:50  alex
+ * - neue Funktion Conn_LastPing().
+ *
+ * Revision 1.35  2002/01/18 11:12:11  alex
+ * - der Sniffer wird nun nur noch aktiviert, wenn auf Kommandozeile angegeben.
+ *
+ * Revision 1.34  2002/01/07 15:29:52  alex
+ * - PASSSERVERADD definiert, wird beim PASS-Befehl an Server verwendet.
+ *
+ * Revision 1.33  2002/01/06 15:18:14  alex
+ * - Loglevel und Meldungen nochmals geaendert. Level passen nun besser.
+ *
+ * Revision 1.32  2002/01/05 23:25:25  alex
+ * - Vorbereitungen fuer Ident-Abfragen bei neuen Client-Strukturen.
+ *
+ * Revision 1.31  2002/01/05 19:15:03  alex
+ * - Fehlerpruefung bei select() in der "Hauptschleife" korrigiert.
+ *
+ * Revision 1.30  2002/01/05 15:56:23  alex
+ * - "arpa/inet.h" wird nur noch includiert, wenn vorhanden.
+ * - Ein Fehler bei select() fuerht nun zum Abbruch von ngIRCd.
+ * - NO_ADDRESS durch NO_DATA ersetzt: ist wohl portabler.
+ *
  * Revision 1.29  2002/01/04 01:36:40  alex
  * - Loglevel ein wenig angepasst.
  *
 #include <sys/types.h>
 #include <time.h>
 #include <netinet/in.h>
-#include <arpa/inet.h>
 #include <netdb.h>
 
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#else
+#define PF_INET AF_INET
+#endif
+
 #ifdef HAVE_STDINT_H
 #include <stdint.h>                    /* u.a. fuer Mac OS X */
 #endif
@@ -226,6 +260,7 @@ GLOBAL VOID Conn_Exit( VOID )
        INT i;
 
        /* Sockets schliessen */
+       Log( LOG_DEBUG, "Shutting down all connections ..." );
        for( i = 0; i < My_Max_Fd + 1; i++ )
        {
                if( FD_ISSET( i, &My_Sockets ))
@@ -234,11 +269,11 @@ GLOBAL VOID Conn_Exit( VOID )
                        {
                                if( My_Connections[idx].sock == i ) break;
                        }
-                       if( idx < MAX_CONNECTIONS ) Conn_Close( idx, "Server going down ..." );
+                       if( idx < MAX_CONNECTIONS ) Conn_Close( idx, NULL, "Server going down", TRUE );
                        else if( FD_ISSET( i, &My_Listeners ))
                        {
                                close( i );
-                               Log( LOG_INFO, "Listening socket %d closed.", i );
+                               Log( LOG_DEBUG, "Listening socket %d closed.", i );
                        }
                        else
                        {
@@ -269,27 +304,27 @@ GLOBAL BOOLEAN Conn_NewListener( CONST INT Port )
        sock = socket( PF_INET, SOCK_STREAM, 0);
        if( sock < 0 )
        {
-               Log( LOG_ALERT, "Can't create socket: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
                return FALSE;
        }
 
        /* Socket-Optionen setzen */
        if( fcntl( sock, F_SETFL, O_NONBLOCK ) != 0 )
        {
-               Log( LOG_ALERT, "Can't enable non-blocking mode: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
                close( sock );
                return FALSE;
        }
        if( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
        {
-               Log( LOG_CRIT, "Can't set socket options: %s!", strerror( errno ));
+               Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
                /* dieser Fehler kann ignoriert werden. */
        }
 
        /* an Port binden */
        if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
        {
-               Log( LOG_ALERT, "Can't bind socket: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
                close( sock );
                return FALSE;
        }
@@ -297,7 +332,7 @@ GLOBAL BOOLEAN Conn_NewListener( CONST INT Port )
        /* in "listen mode" gehen :-) */
        if( listen( sock, 10 ) != 0 )
        {
-               Log( LOG_ALERT, "Can't listen on soecket: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
                close( sock );
                return FALSE;
        }
@@ -386,8 +421,13 @@ GLOBAL VOID Conn_Handler( INT Timeout )
                /* Auf Aktivitaet warten */
                if( select( My_Max_Fd + 1, &read_sockets, &write_sockets, NULL, &tv ) == -1 )
                {
-                       if( errno != EINTR ) Log( LOG_ALERT, "select(): %s!", strerror( errno ));
-                       return;
+                       if( errno != EINTR )
+                       {
+                               Log( LOG_EMERG, "select(): %s!", strerror( errno ));
+                               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
+                               exit( 1 );
+                       }
+                       continue;
                }
 
                /* Koennen Daten geschrieben werden? */
@@ -418,13 +458,13 @@ GLOBAL BOOLEAN Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
        va_start( ap, Format );
        if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
        {
-               Log( LOG_ALERT, "String too long to send (connection %d)!", Idx );
-               Conn_Close( Idx, "Server error: String too long to send!" );
+               Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
+               Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
                return FALSE;
        }
 
 #ifdef SNIFFER
-       Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
+       if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
 #endif
 
        strcat( buffer, "\r\n" );
@@ -457,7 +497,7 @@ GLOBAL BOOLEAN Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
        {
                /* der Puffer ist dummerweise voll ... */
                Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
-               Conn_Close( Idx, NULL );
+               Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
                return FALSE;
        }
 
@@ -475,7 +515,7 @@ GLOBAL BOOLEAN Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
 } /* Conn_Write */
 
 
-GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg )
+GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
 {
        /* Verbindung schliessen. Evtl. noch von Resolver
         * Sub-Prozessen offene Pipes werden geschlossen. */
@@ -485,9 +525,10 @@ GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg )
        assert( Idx >= 0 );
        assert( My_Connections[Idx].sock > NONE );
 
-       if( Msg )
+       if( InformClient )
        {
-               Conn_WriteStr( Idx, "ERROR :%s", Msg );
+               if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
+               else Conn_WriteStr( Idx, "ERROR :Closing connection." );
                if( My_Connections[Idx].sock == NONE ) return;
        }
 
@@ -501,7 +542,7 @@ GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg )
        }
 
        c = Client_GetFromConn( Idx );
-       if( c ) Client_Destroy( c );
+       if( c ) Client_Destroy( c, LogMsg, FwdMsg );
 
        if( My_Connections[Idx].res_stat )
        {
@@ -538,6 +579,15 @@ GLOBAL INT32 Conn_GetIdle( CONN_ID Idx )
 } /* Conn_GetIdle */
 
 
+GLOBAL INT32 Conn_LastPing( CONN_ID Idx )
+{
+       /* Zeitpunkt des letzten PING liefern */
+
+       assert( Idx >= 0 );
+       return My_Connections[Idx].lastping;
+} /* Conn_LastPing */
+
+
 LOCAL BOOLEAN Try_Write( CONN_ID Idx )
 {
        /* Versuchen, Daten aus dem Schreib-Puffer in den
@@ -556,8 +606,8 @@ LOCAL BOOLEAN Try_Write( CONN_ID Idx )
                /* Fehler! */
                if( errno != EINTR )
                {
-                       Log( LOG_ALERT, "select(): %s!", strerror( errno ));
-                       Conn_Close( Idx, NULL );
+                       Log( LOG_ALERT, "select() failed: %s!", strerror( errno ));
+                       Conn_Close( Idx, "Server error!", NULL, FALSE );
                        return FALSE;
                }
        }
@@ -616,8 +666,8 @@ LOCAL BOOLEAN Handle_Write( CONN_ID Idx )
        if( len < 0 )
        {
                /* Oops, ein Fehler! */
-               Log( LOG_ALERT, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
-               Conn_Close( Idx, NULL );
+               Log( LOG_ERR, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
+               Conn_Close( Idx, "Write error (buffer)!", NULL, FALSE );
                return FALSE;
        }
 
@@ -638,7 +688,7 @@ LOCAL VOID New_Connection( INT Sock )
        INT new_sock, new_sock_len;
        RES_STAT *s;
        CONN_ID idx;
-
+       
        assert( Sock >= 0 );
 
        new_sock_len = sizeof( new_addr );
@@ -659,7 +709,7 @@ LOCAL VOID New_Connection( INT Sock )
        }
 
        /* Client-Struktur initialisieren */
-       if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN ))
+       if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE ))
        {
                Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
                close( new_sock );
@@ -720,8 +770,8 @@ LOCAL VOID Read_Request( CONN_ID Idx )
        if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 )
        {
                /* Der Lesepuffer ist voll */
-               Log( LOG_ALERT, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
-               Conn_Close( Idx, "Read buffer overflow!" );
+               Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
+               Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
                return;
        }
 
@@ -731,15 +781,15 @@ LOCAL VOID Read_Request( CONN_ID Idx )
        {
                /* Socket wurde geschlossen */
                Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
-               Conn_Close( Idx, NULL );
+               Conn_Close( Idx, "Socket closed.", NULL, FALSE );
                return;
        }
 
        if( len < 0 )
        {
                /* Fehler beim Lesen */
-               Log( LOG_ALERT, "Read error on connection %d: %s!", Idx, strerror( errno ));
-               Conn_Close( Idx, NULL );
+               Log( LOG_ERR, "Read error on connection %d: %s!", Idx, strerror( errno ));
+               Conn_Close( Idx, "Read error!", NULL, FALSE );
                return;
        }
 
@@ -791,8 +841,8 @@ LOCAL VOID 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_ALERT, "Request too long (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
-                       Conn_Close( Idx, "Request too long!" );
+                       Log( LOG_ERR, "Request too long (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
+                       Conn_Close( Idx, NULL, "Request too long", TRUE );
                        return;
                }
                
@@ -832,8 +882,8 @@ LOCAL VOID Check_Connections( VOID )
                                if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
                                {
                                        /* Timeout */
-                                       Log( LOG_INFO, "Connection %d: PING timeout.", i );
-                                       Conn_Close( i, "PING timeout" );
+                                       Log( LOG_DEBUG, "Connection %d: Ping timeout.", i );
+                                       Conn_Close( i, NULL, "Ping timeout", TRUE );
                                }
                        }
                        else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
@@ -851,7 +901,7 @@ LOCAL VOID Check_Connections( VOID )
                        {
                                /* Timeout */
                                Log( LOG_INFO, "Connection %d: Timeout.", i );
-                               Conn_Close( i, "Timeout" );
+                               Conn_Close( i, NULL, "Timeout", TRUE );
                        }
                }
        }
@@ -865,6 +915,9 @@ LOCAL VOID Check_Servers( VOID )
 
        INT idx, i, n;
        RES_STAT *s;
+
+       /* Wenn "Passive-Mode" aktiv: nicht verbinden */
+       if( NGIRCd_Passive ) return;
        
        for( i = 0; i < Conf_Server_Count; i++ )
        {
@@ -942,7 +995,7 @@ LOCAL VOID New_Server( INT Server, CONN_ID Idx )
                return;
        }
        
-       Log( LOG_INFO, "Establishing connection to \"%s\", %s (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Idx );
+       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 );
 
        if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
        {
@@ -961,19 +1014,19 @@ LOCAL VOID New_Server( INT Server, CONN_ID Idx )
        if ( new_sock < 0 )
        {
                Init_Conn_Struct( Idx );
-               Log( LOG_ALERT, "Can't create socket: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
                return;
        }
        if( connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )) < 0)
        {
                close( new_sock );
                Init_Conn_Struct( Idx );
-               Log( LOG_ALERT, "Can't connect socket: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
                return;
        }
 
        /* Client-Struktur initialisieren */
-       c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER );
+       c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
        if( ! c )
        {
                close( new_sock );
@@ -993,7 +1046,7 @@ LOCAL VOID New_Server( INT Server, CONN_ID Idx )
        if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
 
        /* PASS und SERVER verschicken */
-       Conn_WriteStr( Idx, "PASS %s "PROTOVER""PROTOSUFFIX" IRC|"PACKAGE"-"VERSION" P", Conf_Server[Server].pwd );
+       Conn_WriteStr( Idx, "PASS %s "PASSSERVERADD, Conf_Server[Server].pwd );
        Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
 } /* New_Server */
 
@@ -1029,7 +1082,7 @@ LOCAL RES_STAT *ResolveAddr( struct sockaddr_in *Addr )
        s = malloc( sizeof( RES_STAT ));
        if( ! s )
        {
-               Log( LOG_ALERT, "Resolver: Can't alloc memory!" );
+               Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
                return NULL;
        }
 
@@ -1064,7 +1117,7 @@ LOCAL RES_STAT *ResolveAddr( struct sockaddr_in *Addr )
        {
                /* Fehler */
                free( s );
-               Log( LOG_ALERT, "Resolver: Can't fork: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
                return NULL;
        }
 } /* ResolveAddr */
@@ -1083,7 +1136,7 @@ LOCAL RES_STAT *ResolveName( CHAR *Host )
        s = malloc( sizeof( RES_STAT ));
        if( ! s )
        {
-               Log( LOG_ALERT, "Resolver: Can't alloc memory!" );
+               Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
                return NULL;
        }
 
@@ -1118,7 +1171,7 @@ LOCAL RES_STAT *ResolveName( CHAR *Host )
        {
                /* Fehler */
                free( s );
-               Log( LOG_ALERT, "Resolver: Can't fork: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
                return NULL;
        }
 } /* ResolveName */
@@ -1145,7 +1198,7 @@ LOCAL VOID Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
        /* Antwort an Parent schreiben */
        if( write( w_fd, hostname, strlen( hostname ) + 1 ) != ( strlen( hostname ) + 1 ))
        {
-               Log_Resolver( LOG_ALERT, "Resolver: Can't write to parent: %s!", strerror( errno ));
+               Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
                close( w_fd );
                return;
        }
@@ -1180,7 +1233,7 @@ LOCAL VOID Do_ResolveName( CHAR *Host, INT w_fd )
        /* Antwort an Parent schreiben */
        if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
        {
-               Log_Resolver( LOG_ALERT, "Resolver: Can't write to parent: %s!", strerror( errno ));
+               Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
                close( w_fd );
                return;
        }
@@ -1206,7 +1259,7 @@ LOCAL VOID Read_Resolver_Result( INT r_fd )
        {
                /* Fehler beim Lesen aus der Pipe */
                close( r_fd );
-               Log( LOG_ALERT, "Resolver: Can't read result: %s!", strerror( errno ));
+               Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
                return;
        }
        result[len] = '\0';
@@ -1256,7 +1309,7 @@ LOCAL CHAR *Resolv_Error( INT H_Error )
        {
                case HOST_NOT_FOUND:
                        return "host not found";
-               case NO_ADDRESS:
+               case NO_DATA:
                        return "name valid but no IP address defined";
                case NO_RECOVERY:
                        return "name server error";