X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=89f0ae66b024ca4070430a8aa6792a31392c8746;hp=8ef4bc7a6fe1cda7b208ffa54a627330f209e5e2;hb=83194a23a30de5712375e10366bbd4f7b5b3e0c2;hpb=8dadb17f838c8650ed0ef0e518a4f661cc969e6f diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index 8ef4bc7a..89f0ae66 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -1,6 +1,6 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,7 +16,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: conn.c,v 1.114 2002/12/31 16:13:29 alex Exp $"; +static char UNUSED id[] = "$Id: conn.c,v 1.123 2003/04/25 16:47:52 alex Exp $"; #include "imp.h" #include @@ -40,7 +40,11 @@ static char UNUSED id[] = "$Id: conn.c,v 1.114 2002/12/31 16:13:29 alex Exp $"; #endif #ifdef HAVE_STDINT_H -#include /* u.a. fuer Mac OS X */ +#include /* e.g. for Mac OS X */ +#endif + +#ifdef USE_TCPWRAP +#include /* for TCP Wrappers */ #endif #include "defines.h" @@ -59,6 +63,10 @@ static char UNUSED id[] = "$Id: conn.c,v 1.114 2002/12/31 16:13:29 alex Exp $"; #include "parse.h" #include "tool.h" +#ifdef RENDEZVOUS +#include "rendezvous.h" +#endif + #include "exp.h" @@ -78,11 +86,17 @@ LOCAL VOID Init_Conn_Struct PARAMS(( CONN_ID Idx )); LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock )); LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx )); LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd )); +LOCAL VOID Simple_Message PARAMS(( INT Sock, CHAR *Msg )); LOCAL fd_set My_Listeners; LOCAL fd_set My_Sockets; LOCAL fd_set My_Connects; +#ifdef USE_TCPWRAP +INT allow_severity = LOG_INFO; +INT deny_severity = LOG_ERR; +#endif + GLOBAL VOID Conn_Init( VOID ) @@ -132,8 +146,13 @@ Conn_Exit( VOID ) CONN_ID idx; INT i; - /* Sockets schliessen */ Log( LOG_DEBUG, "Shutting down all connections ..." ); + +#ifdef RENDEZVOUS + Rendezvous_UnregisterListeners( ); +#endif + + /* Sockets schliessen */ for( i = 0; i < Conn_MaxFD + 1; i++ ) { if( FD_ISSET( i, &My_Sockets )) @@ -164,7 +183,7 @@ Conn_Exit( VOID ) } } } - + free( My_Connections ); My_Connections = NULL; Pool_Size = 0; @@ -195,6 +214,10 @@ Conn_ExitListeners( VOID ) INT i; +#ifdef RENDEZVOUS + Rendezvous_UnregisterListeners( ); +#endif + Log( LOG_INFO, "Shutting down all listening sockets ..." ); for( i = 0; i < Conn_MaxFD + 1; i++ ) { @@ -214,6 +237,9 @@ Conn_NewListener( CONST UINT Port ) struct sockaddr_in addr; INT sock; +#ifdef RENDEZVOUS + CHAR name[CLIENT_ID_LEN], *info; +#endif /* Server-"Listen"-Socket initialisieren */ memset( &addr, 0, sizeof( addr )); @@ -255,6 +281,34 @@ Conn_NewListener( CONST UINT Port ) Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock ); +#ifdef RENDEZVOUS + /* Get best server description text */ + if( ! Conf_ServerInfo[0] ) info = Conf_ServerName; + else + { + /* Use server info string */ + info = NULL; + if( Conf_ServerInfo[0] == '[' ) + { + /* Cut off leading hostname part in "[]" */ + info = strchr( Conf_ServerInfo, ']' ); + if( info ) + { + info++; + while( *info == ' ' ) info++; + } + } + if( ! info ) info = Conf_ServerInfo; + } + + /* Add port number to description if non-standard */ + if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port ); + else strlcpy( name, info, sizeof( name )); + + /* Register service */ + Rendezvous_Register( name, RENDEZVOUS_TYPE, Port ); +#endif + return TRUE; } /* Conn_NewListener */ @@ -285,6 +339,10 @@ Conn_Handler( VOID ) { timeout = TRUE; +#ifdef RENDEZVOUS + Rendezvous_Handler( ); +#endif + /* Should the configuration be reloaded? */ if( NGIRCd_SignalRehash ) NGIRCd_Rehash( ); @@ -357,7 +415,7 @@ Conn_Handler( VOID ) 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 ); if( i == 0 ) @@ -371,7 +429,7 @@ Conn_Handler( VOID ) if( errno != EINTR ) { Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno )); - Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE ); + Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME ); exit( 1 ); } continue; @@ -385,7 +443,7 @@ Conn_Handler( VOID ) /* Es kann geschrieben werden ... */ idx = Socket2Index( i ); if( idx == NONE ) continue; - + if( ! Handle_Write( idx )) { /* Fehler beim Schreiben! Diesen Socket nun @@ -529,6 +587,17 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); + /* Is this link already shutting down? */ + if( My_Connections[Idx].options & CONN_ISCLOSING ) + { + /* Conn_Close() has been called recursively for this link; + * probabe reason: Try_Write() failed -- see below. */ + return; + } + + /* Mark link as "closing" */ + My_Connections[Idx].options |= CONN_ISCLOSING; + /* Search client, if any */ c = Client_GetFromConn( Idx ); @@ -550,14 +619,14 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) } /* Try to write out the write buffer */ - Try_Write( Idx ); + (VOID)Try_Write( Idx ); /* Shut down socket */ if( close( My_Connections[Idx].sock ) != 0 ) { /* Oops, we can't close the socket!? This is fatal! */ Log( LOG_EMERG, "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 )); - Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE ); + Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME ); exit( 1 ); } @@ -614,6 +683,37 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) } /* Conn_Close */ +GLOBAL VOID +Conn_SyncServerStruct( VOID ) +{ + /* Synchronize server structures (connection IDs): + * connections <-> configuration */ + + CLIENT *client; + CONN_ID i; + INT c; + + for( i = 0; i < Pool_Size; i++ ) + { + /* Established connection? */ + if( My_Connections[i].sock <= NONE ) continue; + + /* Server connection? */ + client = Client_GetFromConn( i ); + if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue; + + for( c = 0; c < MAX_SERVERS; c++ ) + { + /* Configured server? */ + if( ! Conf_Server[c].host[0] ) continue; + + /* Duplicate? */ + if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 ) Conf_Server[c].conn_id = i; + } + } +} /* SyncServerStruct */ + + LOCAL BOOLEAN Try_Write( CONN_ID Idx ) { @@ -698,6 +798,8 @@ Handle_Write( CONN_ID Idx ) /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */ INT len, res, err; + socklen_t sock_len; + CLIENT *c; assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); @@ -710,9 +812,9 @@ Handle_Write( CONN_ID Idx ) FD_CLR( My_Connections[Idx].sock, &My_Connects ); /* Ergebnis des connect() ermitteln */ - len = sizeof( err ); - res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len ); - assert( len == sizeof( err )); + sock_len = sizeof( err ); + res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len ); + assert( sock_len == sizeof( err )); /* Fehler aufgetreten? */ if(( res != 0 ) || ( err != 0 )) @@ -721,8 +823,10 @@ Handle_Write( CONN_ID Idx ) if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno )); else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port, Idx, strerror( err )); - /* Socket etc. pp. aufraeumen */ + /* Clean up socket, connection and client structures */ FD_CLR( My_Connections[Idx].sock, &My_Sockets ); + c = Client_GetFromConn( Idx ); + if( c ) Client_DestroyNow( c ); close( My_Connections[Idx].sock ); Init_Conn_Struct( Idx ); @@ -774,6 +878,9 @@ New_Connection( INT Sock ) /* Neue Client-Verbindung von Listen-Socket annehmen und * CLIENT-Struktur anlegen. */ +#ifdef USE_TCPWRAP + struct request_info req; +#endif struct sockaddr_in new_addr; INT new_sock, new_sock_len; RES_STAT *s; @@ -793,6 +900,19 @@ New_Connection( INT Sock ) return; } +#ifdef USE_TCPWRAP + /* Validate socket using TCP Wrappers */ + request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL ); + if( ! hosts_access( &req )) + { + /* Access denied! */ + Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr )); + Simple_Message( new_sock, "ERROR :Connection refused" ); + close( new_sock ); + return; + } +#endif + /* Socket initialisieren */ Init_Socket( new_sock ); @@ -801,10 +921,10 @@ New_Connection( INT Sock ) if( idx >= Pool_Size ) { new_size = Pool_Size + CONNECTION_POOL; - + /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden. * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */ - + if( Conf_MaxConnections > 0 ) { /* Es ist ein Limit konfiguriert */ @@ -812,6 +932,7 @@ New_Connection( INT Sock ) { /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */ Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size ); + Simple_Message( new_sock, "ERROR :Connection limit reached" ); close( new_sock ); return; } @@ -820,10 +941,11 @@ New_Connection( INT Sock ) if( new_size < Pool_Size ) { Log( LOG_ALERT, "Can't accespt connection: limit (%d) reached -- overflow!", Pool_Size ); + Simple_Message( new_sock, "ERROR :Connection limit reached" ); close( new_sock ); return; } - + /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich * dynamische Verwaltung waere wohl _deutlich_ besser ...) */ @@ -836,24 +958,25 @@ New_Connection( INT Sock ) { /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */ Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" ); + Simple_Message( new_sock, "ERROR: Internal error" ); close( new_sock ); return; } - + /* Struktur umkopieren ... */ memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size ); - + Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size ); } else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size ); - + /* Adjust pointer to new block */ My_Connections = ptr; - + /* Initialize new items */ for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx ); idx = Pool_Size; - + /* Adjust new pool size */ Pool_Size = new_size; } @@ -863,6 +986,7 @@ New_Connection( INT Sock ) if( ! c ) { Log( LOG_ALERT, "Can't accept connection: can't create client structure!" ); + Simple_Message( new_sock, "ERROR :Internal error" ); close( new_sock ); return; } @@ -887,7 +1011,7 @@ New_Connection( INT Sock ) /* Sub-Prozess wurde asyncron gestartet */ My_Connections[idx].res_stat = s; } - + /* Penalty-Zeit setzen */ Conn_SetPenalty( idx, 4 ); } /* New_Connection */ @@ -1018,14 +1142,14 @@ Handle_Buffer( CONN_ID Idx ) if( ! Unzip_Buffer( Idx )) return FALSE; } #endif - + if( My_Connections[Idx].rdatalen < 1 ) break; /* Eine komplette Anfrage muss mit CR+LF enden, vgl. * RFC 2812. Haben wir eine? */ My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0'; ptr = strstr( My_Connections[Idx].rbuf, "\r\n" ); - + if( ptr ) delta = 2; #ifndef STRICT_RFC else @@ -1040,7 +1164,7 @@ Handle_Buffer( CONN_ID Idx ) else if( ptr2 ) ptr = ptr2; } #endif - + action = FALSE; if( ptr ) { @@ -1095,10 +1219,10 @@ Handle_Buffer( CONN_ID Idx ) } #endif } - + if( action ) result = TRUE; } while( action ); - + return result; } /* Handle_Buffer */ @@ -1224,7 +1348,7 @@ Check_Servers( VOID ) LOCAL VOID New_Server( INT Server, CONN_ID Idx ) { - /* Neue Server-Verbindung aufbauen */ + /* Establish new server link */ struct sockaddr_in new_addr; struct in_addr inaddr; @@ -1234,11 +1358,12 @@ New_Server( INT Server, CONN_ID Idx ) assert( Server > NONE ); assert( Idx > NONE ); - /* Wurde eine gueltige IP-Adresse gefunden? */ + /* Did we get a valid IP address? */ if( ! Conf_Server[Server].ip[0] ) { - /* Nein. Verbindung wieder freigeben: */ + /* No. Free connection structure and abort: */ Init_Conn_Struct( Idx ); + Conf_Server[Server].conn_id = NONE; Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx ); return; } @@ -1253,8 +1378,9 @@ New_Server( INT Server, CONN_ID Idx ) if( inaddr.s_addr == (unsigned)-1 ) #endif { - /* Konnte Adresse nicht konvertieren */ + /* Can't convert IP address */ Init_Conn_Struct( Idx ); + Conf_Server[Server].conn_id = NONE; Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): can't convert ip address %s!", Conf_Server[Server].host, Idx, Conf_Server[Server].ip ); return; } @@ -1267,7 +1393,9 @@ New_Server( INT Server, CONN_ID Idx ) new_sock = socket( PF_INET, SOCK_STREAM, 0 ); if ( new_sock < 0 ) { + /* Can't create socket */ Init_Conn_Struct( Idx ); + Conf_Server[Server].conn_id = NONE; Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno )); return; } @@ -1277,9 +1405,11 @@ New_Server( INT Server, CONN_ID Idx ) res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); if(( res != 0 ) && ( errno != EINPROGRESS )) { + /* Can't connect socket */ Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); close( new_sock ); Init_Conn_Struct( Idx ); + Conf_Server[Server].conn_id = NONE; return; } @@ -1287,24 +1417,26 @@ New_Server( INT Server, CONN_ID Idx ) c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE ); if( ! c ) { + /* Can't create new client structure */ close( new_sock ); Init_Conn_Struct( Idx ); + Conf_Server[Server].conn_id = NONE; Log( LOG_ALERT, "Can't establish connection: can't create client structure!" ); return; } Client_SetIntroducer( c, c ); Client_SetToken( c, TOKEN_OUTBOUND ); - /* Verbindung registrieren */ + /* Register connection */ My_Connections[Idx].sock = new_sock; My_Connections[Idx].addr = new_addr; strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host )); - /* Neuen Socket registrieren */ + /* Register new socket */ FD_SET( new_sock, &My_Sockets ); FD_SET( new_sock, &My_Connects ); if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock; - + Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock ); } /* New_Server */ @@ -1407,7 +1539,7 @@ Read_Resolver_Result( INT r_fd ) } 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] ); @@ -1435,4 +1567,19 @@ Read_Resolver_Result( INT r_fd ) } /* Read_Resolver_Result */ +LOCAL VOID +Simple_Message( INT Sock, CHAR *Msg ) +{ + /* Write "simple" message to socket, without using compression + * or even the connection write buffers. Used e.g. for error + * messages by New_Connection(). */ + + assert( Sock > NONE ); + assert( Msg != NULL ); + + (VOID)send( Sock, Msg, strlen( Msg ), 0 ); + (VOID)send( Sock, "\r\n", 2, 0 ); +} /* Simple_Error */ + + /* -eof- */