X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=95e6d545fd97fb1930f18fec3858a47cc8b9993b;hb=4d2f279d2d4fdb6dca8ac0ec3abd79071c884d64;hp=22fbaaee235aeb91367846c18d32edfa7e07fed6;hpb=a1437f1ad040dfc1c196ec01430d9ad35da81f97;p=ngircd-alex.git diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index 22fbaaee..95e6d545 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -2,21 +2,20 @@ * ngIRCd -- The Next Generation IRC Daemon * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) * - * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen - * der GNU General Public License (GPL), wie von der Free Software Foundation - * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2 - * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version. - * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste - * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS. + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * Please read the file COPYING, README and AUTHORS for more information. * - * $Id: conn.c,v 1.100 2002/11/29 17:36:50 alex Exp $ - * - * connect.h: Verwaltung aller Netz-Verbindungen ("connections") + * Connection management */ #include "portab.h" +static char UNUSED id[] = "$Id: conn.c,v 1.109 2002/12/26 17:04:54 alex Exp $"; + #include "imp.h" #include #include @@ -89,11 +88,13 @@ typedef struct _Connection CHAR wbuf[WRITEBUFFER_LEN]; /* Schreibpuffer */ INT wdatalen; /* Laenge der Daten im Schreibpuffer */ INT our_server; /* wenn von uns zu connectender Server: ID */ + time_t starttime; /* Startzeit des Links */ time_t lastdata; /* Letzte Aktivitaet */ time_t lastping; /* Letzter PING */ time_t lastprivmsg; /* Letzte PRIVMSG */ time_t delaytime; /* Nicht beachten bis ("penalty") */ - LONG bytes_in, bytes_out; /* Counter fuer Statistik */ + LONG bytes_in, bytes_out; /* Empfangene uns gesendete Bytes */ + LONG msg_in, msg_out; /* Empfangene uns gesendete Nachtichten */ INT flag; /* "Markierungs-Flag" (vgl. "irc-write"-Modul) */ INT options; /* Link-Optionen */ #ifdef USE_ZLIB @@ -128,7 +129,7 @@ LOCAL fd_set My_Sockets; LOCAL fd_set My_Connects; LOCAL CONNECTION *My_Connections; -LOCAL LONG Pool_Size; +LOCAL LONG Pool_Size, WCounter; GLOBAL VOID @@ -152,7 +153,7 @@ Conn_Init( VOID ) Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" ); exit( 1 ); } - Log( LOG_DEBUG, "Allocted connection pool for %ld items.", Pool_Size ); + Log( LOG_DEBUG, "Allocted connection pool for %ld items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size ); /* zu Beginn haben wir keine Verbindungen */ FD_ZERO( &My_Listeners ); @@ -164,6 +165,9 @@ Conn_Init( VOID ) /* Connection-Struktur initialisieren */ for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i ); + + /* Global write counter */ + WCounter = 0; } /* Conn_Init */ @@ -198,7 +202,7 @@ Conn_Exit( VOID ) } else if( idx < Pool_Size ) { - if( NGIRCd_Restart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE ); + if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE ); else Conn_Close( idx, NULL, "Server going down", TRUE ); } else @@ -328,12 +332,15 @@ Conn_Handler( VOID ) BOOLEAN timeout; start = time( NULL ); - while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart )) + while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart )) { timeout = TRUE; - - Check_Servers( ); + /* Should the configuration be reloaded? */ + if( NGIRCd_SignalRehash ) NGIRCd_Rehash( ); + + /* Check configured servers and established links */ + Check_Servers( ); Check_Connections( ); /* noch volle Lese-Buffer suchen */ @@ -444,6 +451,9 @@ Conn_Handler( VOID ) if( FD_ISSET( i, &read_sockets )) Handle_Read( i ); } } + + if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" ); + else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" ); } /* Conn_Handler */ @@ -485,8 +495,9 @@ va_dcl if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer ); #endif - strcat( buffer, "\r\n" ); + strlcat( buffer, "\r\n", sizeof( buffer )); ok = Conn_Write( Idx, buffer, strlen( buffer )); + My_Connections[Idx].msg_out++; va_end( ap ); return ok; @@ -546,6 +557,9 @@ Conn_Write( CONN_ID Idx, CHAR *Data, INT Len ) My_Connections[Idx].bytes_out += Len; } + /* Adjust global write counter */ + WCounter += Len; + return TRUE; } /* Conn_Write */ @@ -603,12 +617,12 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024; in_p = (INT)(( in_k * 100 ) / in_z_k ); out_p = (INT)(( out_k * 100 ) / out_z_k ); - Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, in_z_k, in_p, out_k, out_z_k, out_p ); + Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, in_z_k, in_p, out_k, out_z_k, out_p ); } else #endif { - Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k ); + Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k ); } } @@ -864,7 +878,119 @@ Conn_InitZip( CONN_ID Idx ) return TRUE; } /* Conn_InitZip */ + +GLOBAL LONG +Conn_SendBytesZip( CONN_ID Idx ) +{ + /* Anzahl gesendeter Bytes (komprimiert!) liefern */ + + assert( Idx > NONE ); + return My_Connections[Idx].zip.bytes_out; +} /* Conn_SendBytesZip */ + + +GLOBAL LONG +Conn_RecvBytesZip( CONN_ID Idx ) +{ + /* Anzahl gesendeter Bytes (komprimiert!) liefern */ + + assert( Idx > NONE ); + return My_Connections[Idx].zip.bytes_in; +} /* Conn_RecvBytesZip */ + +#endif + + +GLOBAL time_t +Conn_StartTime( CONN_ID Idx ) +{ + /* Zeitpunkt des Link-Starts liefern (in Sekunden) */ + + assert( Idx > NONE ); + return My_Connections[Idx].starttime; +} /* Conn_Uptime */ + + +GLOBAL INT +Conn_SendQ( CONN_ID Idx ) +{ + /* Laenge der Daten im Schreibbuffer liefern */ + + assert( Idx > NONE ); +#ifdef USE_ZLIB + if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.wdatalen; + else +#endif + return My_Connections[Idx].wdatalen; +} /* Conn_SendQ */ + + +GLOBAL LONG +Conn_SendMsg( CONN_ID Idx ) +{ + /* Anzahl gesendeter Nachrichten liefern */ + + assert( Idx > NONE ); + return My_Connections[Idx].msg_out; +} /* Conn_SendMsg */ + + +GLOBAL LONG +Conn_SendBytes( CONN_ID Idx ) +{ + /* Anzahl gesendeter Bytes (unkomprimiert) liefern */ + + assert( Idx > NONE ); + return My_Connections[Idx].bytes_out; +} /* Conn_SendBytes */ + + +GLOBAL INT +Conn_RecvQ( CONN_ID Idx ) +{ + /* Laenge der Daten im Lesebuffer liefern */ + + assert( Idx > NONE ); +#ifdef USE_ZLIB + if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.rdatalen; + else #endif + return My_Connections[Idx].rdatalen; +} /* Conn_RecvQ */ + + +GLOBAL LONG +Conn_RecvMsg( CONN_ID Idx ) +{ + /* Anzahl empfangener Nachrichten liefern */ + + assert( Idx > NONE ); + return My_Connections[Idx].msg_in; +} /* Conn_RecvMsg */ + + +GLOBAL LONG +Conn_RecvBytes( CONN_ID Idx ) +{ + /* Anzahl empfangener Bytes (unkomprimiert) liefern */ + + assert( Idx > NONE ); + return My_Connections[Idx].bytes_in; +} /* Conn_RecvBytes */ + + +GLOBAL VOID +Conn_ResetWCounter( VOID ) +{ + WCounter = 0; +} /* Conn_ResetWCounter */ + + +GLOBAL LONG +Conn_WCounter( VOID ) +{ + return WCounter; +} /* Conn_WCounter */ LOCAL BOOLEAN @@ -1089,11 +1215,18 @@ New_Connection( INT Sock ) /* Struktur umkopieren ... */ memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size ); - Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [malloc()/memcpy()]", new_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. [realloc()]", 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; } @@ -1118,7 +1251,7 @@ New_Connection( INT 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 */ - strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr )); + strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host )); Client_SetHostname( c, My_Connections[idx].host ); s = Resolve_Addr( &new_addr ); if( s ) @@ -1204,7 +1337,7 @@ Read_Request( CONN_ID Idx ) if( len == 0 ) { /* 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)); + Log( LOG_INFO, "%s:%d (%s) is closing the connection ...", My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), inet_ntoa( My_Connections[Idx].addr.sin_addr )); Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE ); return; } @@ -1304,6 +1437,7 @@ Handle_Buffer( CONN_ID Idx ) if( len > delta ) { /* Es wurde ein Request gelesen */ + My_Connections[Idx].msg_in++; if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE; else action = TRUE; } @@ -1454,8 +1588,8 @@ Check_Servers( VOID ) /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den * konfigurierten Text direkt als IP-Adresse zu verwenden ... */ - strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host ); - strcpy( My_Connections[idx].host, Conf_Server[i].host ); + strlcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host, sizeof( Conf_Server[My_Connections[idx].our_server].ip )); + strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host )); s = Resolve_Name( Conf_Server[i].host ); if( s ) { @@ -1543,7 +1677,7 @@ New_Server( INT Server, CONN_ID Idx ) /* Verbindung registrieren */ My_Connections[Idx].sock = new_sock; My_Connections[Idx].addr = new_addr; - strcpy( My_Connections[Idx].host, Conf_Server[Server].host ); + strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host )); /* Neuen Socket registrieren */ FD_SET( new_sock, &My_Sockets ); @@ -1567,12 +1701,15 @@ Init_Conn_Struct( LONG Idx ) My_Connections[Idx].wbuf[0] = '\0'; My_Connections[Idx].wdatalen = 0; My_Connections[Idx].our_server = NONE; + My_Connections[Idx].starttime = time( NULL ); My_Connections[Idx].lastdata = time( NULL ); My_Connections[Idx].lastping = 0; My_Connections[Idx].lastprivmsg = time( NULL ); My_Connections[Idx].delaytime = 0; My_Connections[Idx].bytes_in = 0; My_Connections[Idx].bytes_out = 0; + My_Connections[Idx].msg_in = 0; + My_Connections[Idx].msg_out = 0; My_Connections[Idx].flag = 0; My_Connections[Idx].options = 0; @@ -1662,14 +1799,14 @@ Read_Resolver_Result( INT r_fd ) /* Eingehende Verbindung: Hostnamen setzen */ c = Client_GetFromConn( i ); assert( c != NULL ); - strcpy( My_Connections[i].host, result ); + strlcpy( My_Connections[i].host, result, sizeof( My_Connections[i].host )); Client_SetHostname( c, result ); } else { /* Ausgehende Verbindung (=Server): IP setzen */ assert( My_Connections[i].our_server > NONE ); - strcpy( Conf_Server[My_Connections[i].our_server].ip, result ); + strlcpy( Conf_Server[My_Connections[i].our_server].ip, result, sizeof( Conf_Server[My_Connections[i].our_server].ip )); } /* Penalty-Zeit zurueck setzen */