]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
- replaced a lot of strcat() calls with strlcat() which is more secure.
[ngircd-alex.git] / src / ngircd / conn.c
index ab48e8506d114d006dc96d2e4e3127b5756145f3..43625fe0d22c85ccb76f8226fdcc2cf7cad9629e 100644 (file)
@@ -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.101 2002/12/02 13:19:37 alex Exp $
- *
- * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
+ * Connection management
  */
 
 
 #include "portab.h"
 
+static char UNUSED id[] = "$Id: conn.c,v 1.108 2002/12/26 16:48:14 alex Exp $";
+
 #include "imp.h"
 #include <assert.h>
 #include <stdarg.h>
@@ -130,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
@@ -154,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 );
@@ -166,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 */
 
 
@@ -200,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
@@ -330,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 */
@@ -446,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 */
 
 
@@ -487,7 +495,7 @@ 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++;
 
@@ -549,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 */
 
@@ -606,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 );
                }
        }
        
@@ -907,10 +918,10 @@ Conn_SendQ( CONN_ID Idx )
 
        assert( Idx > NONE );
 #ifdef USE_ZLIB
-       return My_Connections[Idx].zip.wdatalen;
-#else
-       return My_Connections[Idx].wdatalen;
+       if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.wdatalen;
+       else
 #endif
+       return My_Connections[Idx].wdatalen;
 } /* Conn_SendQ */
 
 
@@ -941,10 +952,10 @@ Conn_RecvQ( CONN_ID Idx )
 
        assert( Idx > NONE );
 #ifdef USE_ZLIB
-       return My_Connections[Idx].zip.rdatalen;
-#else
-       return My_Connections[Idx].rdatalen;
+       if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.rdatalen;
+       else
 #endif
+       return My_Connections[Idx].rdatalen;
 } /* Conn_RecvQ */
 
 
@@ -968,6 +979,20 @@ Conn_RecvBytes( CONN_ID Idx )
 } /* Conn_RecvBytes */
 
 
+GLOBAL VOID
+Conn_ResetWCounter( VOID )
+{
+       WCounter = 0;
+} /* Conn_ResetWCounter */
+
+
+GLOBAL LONG
+Conn_WCounter( VOID )
+{
+       return WCounter;
+} /* Conn_WCounter */
+
+
 LOCAL BOOLEAN
 Try_Write( CONN_ID Idx )
 {
@@ -1190,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;
        }
 
@@ -1305,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;
        }