X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=710abac70f849a2c0b58c36c3a72bcf617bc12b6;hp=c05f820f58a01a2fda2447a0f6afe37a98441cc1;hb=b9f005af75befc6f731456377ea6784e121d227e;hpb=54e487d424c65ead3f94486a926dcd9b8bfd82e9 diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index c05f820f..710abac7 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -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.27 2002/01/03 02:25:36 alex Exp $ + * $Id: conn.c,v 1.36 2002/02/11 01:00:50 alex Exp $ * * connect.h: Verwaltung aller Netz-Verbindungen ("connections") * * $Log: conn.c,v $ + * 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. + * + * Revision 1.28 2002/01/04 01:20:23 alex + * - Client-Strukruren werden nur noch ueber Funktionen angesprochen. + * * Revision 1.27 2002/01/03 02:25:36 alex * - diverse Aenderungen und Umsetellungen fuer Server-Links. * @@ -126,9 +155,14 @@ #include #include #include -#include #include +#ifdef HAVE_ARPA_INET_H +#include +#else +#define PF_INET AF_INET +#endif + #ifdef HAVE_STDINT_H #include /* u.a. fuer Mac OS X */ #endif @@ -220,6 +254,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 )) @@ -228,11 +263,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 { @@ -263,27 +298,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; } @@ -291,7 +326,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; } @@ -380,8 +415,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? */ @@ -412,13 +452,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" ); @@ -451,7 +491,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; } @@ -469,7 +509,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. */ @@ -479,9 +519,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; } @@ -491,11 +532,11 @@ GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg ) } else { - Log( LOG_NOTICE, "Connection %d with %s:%d closed.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port )); + 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 ); + if( c ) Client_Destroy( c, LogMsg, FwdMsg ); if( My_Connections[Idx].res_stat ) { @@ -532,6 +573,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 @@ -550,8 +600,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; } } @@ -610,8 +660,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; } @@ -632,7 +682,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 ); @@ -653,7 +703,7 @@ LOCAL VOID New_Connection( INT Sock ) } /* Client-Struktur initialisieren */ - if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ))) + 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 ); @@ -669,7 +719,7 @@ LOCAL VOID New_Connection( INT Sock ) FD_SET( new_sock, &My_Sockets ); if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock; - Log( LOG_NOTICE, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), 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 */ s = ResolveAddr( &new_addr ); @@ -714,8 +764,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; } @@ -725,15 +775,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; } @@ -785,8 +835,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; } @@ -817,7 +867,7 @@ LOCAL VOID Check_Connections( VOID ) if( My_Connections[i].sock == NONE ) continue; c = Client_GetFromConn( i ); - if( c && (( c->type == CLIENT_USER ) || ( c->type == CLIENT_SERVER ) || ( c->type == CLIENT_SERVICE ))) + if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE ))) { /* verbundener User, Server oder Service */ if( My_Connections[i].lastping > My_Connections[i].lastdata ) @@ -826,8 +876,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 ) @@ -835,7 +885,7 @@ LOCAL VOID Check_Connections( VOID ) /* es muss ein PING gesendet werden */ Log( LOG_DEBUG, "Connection %d: sending PING ...", i ); My_Connections[i].lastping = time( NULL ); - Conn_WriteStr( i, "PING :%s", This_Server->nick ); + Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( ))); } } else @@ -845,7 +895,7 @@ LOCAL VOID Check_Connections( VOID ) { /* Timeout */ Log( LOG_INFO, "Connection %d: Timeout.", i ); - Conn_Close( i, "Timeout" ); + Conn_Close( i, NULL, "Timeout", TRUE ); } } } @@ -955,19 +1005,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 )); + c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE ); if( ! c ) { close( new_sock ); @@ -975,7 +1025,7 @@ LOCAL VOID New_Server( INT Server, CONN_ID Idx ) Log( LOG_ALERT, "Can't establish connection: can't create client structure!" ); return; } - c->type = CLIENT_UNKNOWNSERVER; + Client_SetIntroducer( c, c ); /* Verbindung registrieren */ My_Connections[Idx].sock = new_sock; @@ -987,7 +1037,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 */ @@ -1023,7 +1073,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; } @@ -1058,7 +1108,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 */ @@ -1077,7 +1127,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; } @@ -1112,7 +1162,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 */ @@ -1139,7 +1189,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; } @@ -1174,7 +1224,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; } @@ -1200,7 +1250,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'; @@ -1250,7 +1300,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";