X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=cbef7f3e6ccf438395b3d0e9a470e32bae815e54;hb=7bbdfb3d847034d7ee1881027164a087b9480300;hp=db7bccb4467b06153e504d0d5e78bc5435ba2a6c;hpb=5b25c8ccd0420b7e0834ef6ec186b57453f2156a;p=ngircd-alex.git diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index db7bccb4..cbef7f3e 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -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.85 2002/10/22 23:25:29 alex Exp $ + * $Id: conn.c,v 1.89 2002/11/11 00:54:25 alex Exp $ * * connect.h: Verwaltung aller Netz-Verbindungen ("connections") */ @@ -76,6 +76,7 @@ typedef struct _Connection time_t lastprivmsg; /* Letzte PRIVMSG */ time_t delaytime; /* Nicht beachten bis ("penalty") */ LONG bytes_in, bytes_out; /* Counter fuer Statistik */ + INT flag; /* Channel-Flag (vgl. "irc-write"-Modul) */ } CONNECTION; @@ -88,7 +89,7 @@ LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx )); LOCAL VOID Handle_Buffer PARAMS(( CONN_ID Idx )); LOCAL VOID Check_Connections PARAMS(( VOID )); LOCAL VOID Check_Servers PARAMS(( VOID )); -LOCAL VOID Init_Conn_Struct PARAMS(( INT Idx )); +LOCAL VOID Init_Conn_Struct PARAMS(( LONG 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 )); @@ -98,7 +99,8 @@ LOCAL fd_set My_Listeners; LOCAL fd_set My_Sockets; LOCAL fd_set My_Connects; -LOCAL CONNECTION My_Connections[MAX_CONNECTIONS]; +LOCAL CONNECTION *My_Connections; +LOCAL LONG Pool_Size; GLOBAL VOID @@ -108,15 +110,32 @@ Conn_Init( VOID ) CONN_ID i; + /* Speicher fuer Verbindungs-Pool anfordern */ + Pool_Size = CONNECTION_POOL; + if( Conf_MaxConnections > 0 ) + { + /* konfiguriertes Limit beachten */ + if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections; + } + My_Connections = malloc( sizeof( CONNECTION ) * Pool_Size ); + if( ! My_Connections ) + { + /* Speicher konnte nicht alloziert werden! */ + Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" ); + exit( 1 ); + } + Log( LOG_DEBUG, "Allocted connection pool for %ld items.", Pool_Size ); + /* zu Beginn haben wir keine Verbindungen */ FD_ZERO( &My_Listeners ); FD_ZERO( &My_Sockets ); FD_ZERO( &My_Connects ); + /* Groesster File-Descriptor fuer select() */ Conn_MaxFD = 0; /* Connection-Struktur initialisieren */ - for( i = 0; i < MAX_CONNECTIONS; i++ ) Init_Conn_Struct( i ); + for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i ); } /* Conn_Init */ @@ -135,7 +154,7 @@ Conn_Exit( VOID ) { if( FD_ISSET( i, &My_Sockets )) { - for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) + for( idx = 0; idx < Pool_Size; idx++ ) { if( My_Connections[idx].sock == i ) break; } @@ -149,7 +168,7 @@ Conn_Exit( VOID ) close( i ); Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i ); } - else if( idx < MAX_CONNECTIONS ) + else if( idx < Pool_Size ) { if( NGIRCd_Restart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE ); else Conn_Close( idx, NULL, "Server going down", TRUE ); @@ -161,6 +180,10 @@ Conn_Exit( VOID ) } } } + + free( My_Connections ); + My_Connections = NULL; + Pool_Size = 0; } /* Conn_Exit */ @@ -236,7 +259,7 @@ Conn_Handler( VOID ) fd_set read_sockets, write_sockets; struct timeval tv; time_t start, t; - INT i, idx; + LONG i, idx; start = time( NULL ); while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart )) @@ -246,7 +269,7 @@ Conn_Handler( VOID ) Check_Connections( ); /* noch volle Lese-Buffer suchen */ - for( i = 0; i < MAX_CONNECTIONS; i++ ) + for( i = 0; i < Pool_Size; i++ ) { if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 )) { @@ -257,7 +280,7 @@ Conn_Handler( VOID ) /* noch volle Schreib-Puffer suchen */ FD_ZERO( &write_sockets ); - for( i = 0; i < MAX_CONNECTIONS; i++ ) + for( i = 0; i < Pool_Size; i++ ) { if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 )) { @@ -266,7 +289,7 @@ Conn_Handler( VOID ) } } /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */ - for( i = 0; i < MAX_CONNECTIONS; i++ ) + for( i = 0; i < Pool_Size; i++ ) { if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets ); } @@ -274,7 +297,7 @@ Conn_Handler( VOID ) /* von welchen Sockets koennte gelesen werden? */ t = time( NULL ); read_sockets = My_Sockets; - for( i = 0; i < MAX_CONNECTIONS; i++ ) + for( i = 0; i < Pool_Size; i++ ) { if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' )) { @@ -370,7 +393,7 @@ va_dcl BOOLEAN ok; va_list ap; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( Format != NULL ); #ifdef PROTOTYPES @@ -403,7 +426,7 @@ Conn_Write( CONN_ID Idx, CHAR *Data, INT Len ) /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird * der Client disconnectiert und FALSE geliefert. */ - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( Data != NULL ); assert( Len > 0 ); @@ -456,7 +479,7 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) CLIENT *c; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); c = Client_GetFromConn( Idx ); @@ -501,7 +524,7 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) } /* 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 )) + if(( My_Connections[Idx].our_server > NONE ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL ) - Conf_ConnectRetry )) { /* Okay, die Verbindung stand schon "genuegend lange": * lasttry-Zeitpunkt so setzen, dass der naechste @@ -520,7 +543,7 @@ Conn_UpdateIdle( CONN_ID Idx ) { /* Idle-Timer zuruecksetzen */ - assert( Idx >= 0 ); + assert( Idx > NONE ); My_Connections[Idx].lastprivmsg = time( NULL ); } @@ -530,7 +553,7 @@ Conn_GetIdle( CONN_ID Idx ) { /* Idle-Time einer Verbindung liefern (in Sekunden) */ - assert( Idx >= 0 ); + assert( Idx > NONE ); return time( NULL ) - My_Connections[Idx].lastprivmsg; } /* Conn_GetIdle */ @@ -540,7 +563,7 @@ Conn_LastPing( CONN_ID Idx ) { /* Zeitpunkt des letzten PING liefern */ - assert( Idx >= 0 ); + assert( Idx > NONE ); return My_Connections[Idx].lastping; } /* Conn_LastPing */ @@ -555,7 +578,7 @@ Conn_SetPenalty( CONN_ID Idx, time_t Seconds ) time_t t; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( Seconds >= 0 ); t = time( NULL ) + Seconds; @@ -566,11 +589,89 @@ Conn_SetPenalty( CONN_ID Idx, time_t Seconds ) GLOBAL VOID Conn_ResetPenalty( CONN_ID Idx ) { - assert( Idx >= 0 ); + assert( Idx > NONE ); My_Connections[Idx].delaytime = 0; } /* Conn_ResetPenalty */ +GLOBAL VOID +Conn_ClearFlags( VOID ) +{ + /* Alle Connection auf "nicht-markiert" setzen */ + + LONG i; + + for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0; +} /* Conn_ClearFlags */ + + +GLOBAL INT +Conn_Flag( CONN_ID Idx ) +{ + /* Ist eine Connection markiert (TRUE) oder nicht? */ + + assert( Idx > NONE ); + return My_Connections[Idx].flag; +} /* Conn_Flag */ + + +GLOBAL VOID +Conn_SetFlag( CONN_ID Idx, INT Flag ) +{ + /* Connection markieren */ + + assert( Idx > NONE ); + My_Connections[Idx].flag = Flag; +} /* Conn_SetFlag */ + + +GLOBAL CONN_ID +Conn_First( VOID ) +{ + /* Connection-Struktur der ersten Verbindung liefern; + * Ist keine Verbindung vorhanden, wird NONE geliefert. */ + + LONG i; + + for( i = 0; i < Pool_Size; i++ ) + { + if( My_Connections[i].sock != NONE ) return i; + } + return NONE; +} /* Conn_First */ + + +GLOBAL CONN_ID +Conn_Next( CONN_ID Idx ) +{ + /* Naechste Verbindungs-Struktur liefern; existiert keine + * weitere, so wird NONE geliefert. */ + + LONG i = NONE; + + assert( Idx > NONE ); + + for( i = Idx + 1; i < Pool_Size; i++ ) + { + if( My_Connections[i].sock != NONE ) return i; + } + return NONE; +} /* Conn_Next */ + + +GLOBAL VOID +Conn_SetServer( CONN_ID Idx, INT ConfServer ) +{ + /* Connection als Server markieren: Index des konfigurierten + * Servers speichern. Verbindung muss bereits bestehen! */ + + assert( Idx > NONE ); + assert( My_Connections[Idx].sock > NONE ); + + My_Connections[Idx].our_server = ConfServer; +} /* Conn_SetServer */ + + LOCAL BOOLEAN Try_Write( CONN_ID Idx ) { @@ -579,7 +680,7 @@ Try_Write( CONN_ID Idx ) fd_set write_socket; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); assert( My_Connections[Idx].wdatalen > 0 ); @@ -611,7 +712,7 @@ Handle_Read( INT Sock ) CONN_ID idx; - assert( Sock >= 0 ); + assert( Sock > NONE ); if( FD_ISSET( Sock, &My_Listeners )) { @@ -716,8 +817,10 @@ New_Connection( INT Sock ) RES_STAT *s; CONN_ID idx; CLIENT *c; + POINTER *ptr; + LONG new_size; - assert( Sock >= 0 ); + assert( Sock > NONE ); new_sock_len = sizeof( new_addr ); new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len ); @@ -728,12 +831,52 @@ New_Connection( INT Sock ) } /* Freie Connection-Struktur suchen */ - for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break; - if( idx >= MAX_CONNECTIONS ) + for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break; + if( idx >= Pool_Size ) { - Log( LOG_ALERT, "Can't accept connection: limit reached (%d)!", MAX_CONNECTIONS ); - close( new_sock ); - return; + 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 */ + if( Pool_Size >= Conf_MaxConnections ) + { + /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */ + Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size ); + close( new_sock ); + return; + } + if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections; + } + + /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen + * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich + * dynamische Verwaltung waere wohl _deutlich_ besser ...) */ + ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size ); + if( ! ptr ) + { + /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */ + ptr = malloc( sizeof( CONNECTION ) * new_size ); + if( ! ptr ) + { + /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */ + Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" ); + close( new_sock ); + return; + } + + /* Struktur umkopieren ... */ + memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size ); + + Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [malloc()/memcpy()]", new_size ); + } + else Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [realloc()]", new_size ); + + My_Connections = ptr; + Pool_Size = new_size; } /* Client-Struktur initialisieren */ @@ -783,11 +926,11 @@ Socket2Index( INT Sock ) CONN_ID idx; - assert( Sock >= 0 ); + assert( Sock > NONE ); - for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break; + for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break; - if( idx >= MAX_CONNECTIONS ) + if( idx >= Pool_Size ) { /* die Connection wurde vermutlich (wegen eines * Fehlers) bereits wieder abgebaut ... */ @@ -806,7 +949,7 @@ Read_Request( CONN_ID Idx ) INT len; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 ) @@ -916,9 +1059,9 @@ Check_Connections( VOID ) * auch das nicht "hilft", Client disconnectieren. */ CLIENT *c; - INT i; + LONG i; - for( i = 0; i < MAX_CONNECTIONS; i++ ) + for( i = 0; i < Pool_Size; i++ ) { if( My_Connections[i].sock == NONE ) continue; @@ -964,8 +1107,9 @@ Check_Servers( VOID ) /* Pruefen, ob Server-Verbindungen aufgebaut werden * muessen bzw. koennen */ - INT idx, i, n; RES_STAT *s; + LONG idx, n; + INT i; /* Wenn "Passive-Mode" aktiv: nicht verbinden */ if( NGIRCd_Passive ) return; @@ -976,7 +1120,7 @@ Check_Servers( VOID ) if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue; /* Haben wir schon eine Verbindung? */ - for( n = 0; n < MAX_CONNECTIONS; n++ ) + for( n = 0; n < Pool_Size; n++ ) { if( My_Connections[n].sock == NONE ) continue; @@ -996,7 +1140,7 @@ Check_Servers( VOID ) if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break; } } - if( n < MAX_CONNECTIONS ) continue; + if( n < Pool_Size ) continue; /* Wann war der letzte Connect-Versuch? */ if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue; @@ -1005,10 +1149,10 @@ Check_Servers( VOID ) Conf_Server[i].lasttry = time( NULL ); /* Freie Connection-Struktur suschen */ - for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break; - if( idx >= MAX_CONNECTIONS ) + for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break; + if( idx >= Pool_Size ) { - Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", MAX_CONNECTIONS ); + Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size ); return; } Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host ); @@ -1042,11 +1186,11 @@ New_Server( INT Server, CONN_ID Idx ) struct sockaddr_in new_addr; struct in_addr inaddr; - INT new_sock; + INT res, new_sock; CLIENT *c; - assert( Server >= 0 ); - assert( Idx >= 0 ); + assert( Server > NONE ); + assert( Idx > NONE ); /* Wurde eine gueltige IP-Adresse gefunden? */ if( ! Conf_Server[Server].ip[0] ) @@ -1088,13 +1232,12 @@ New_Server( INT Server, CONN_ID Idx ) if( ! Init_Socket( new_sock )) return; - connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); - if( errno != EINPROGRESS ) + res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); + if(( res != 0 ) && ( errno != EINPROGRESS )) { - + Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); close( new_sock ); Init_Conn_Struct( Idx ); - Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); return; } @@ -1125,7 +1268,7 @@ New_Server( INT Server, CONN_ID Idx ) LOCAL VOID -Init_Conn_Struct( INT Idx ) +Init_Conn_Struct( LONG Idx ) { /* Connection-Struktur initialisieren */ @@ -1143,6 +1286,7 @@ Init_Conn_Struct( INT Idx ) My_Connections[Idx].delaytime = 0; My_Connections[Idx].bytes_in = 0; My_Connections[Idx].bytes_out = 0; + My_Connections[Idx].flag = 0; } /* Init_Conn_Struct */ @@ -1195,11 +1339,11 @@ Read_Resolver_Result( INT r_fd ) result[len] = '\0'; /* zugehoerige Connection suchen */ - for( i = 0; i < MAX_CONNECTIONS; i++ ) + for( i = 0; i < Pool_Size; i++ ) { if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break; } - if( i >= MAX_CONNECTIONS ) + if( i >= Pool_Size ) { /* Opsa! Keine passende Connection gefunden!? Vermutlich * wurde sie schon wieder geschlossen. */ @@ -1227,7 +1371,7 @@ Read_Resolver_Result( INT r_fd ) else { /* Ausgehende Verbindung (=Server): IP setzen */ - assert( My_Connections[i].our_server >= 0 ); + assert( My_Connections[i].our_server > NONE ); strcpy( Conf_Server[My_Connections[i].our_server].ip, result ); }