]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
- Conn_Close() eingefuehrt: war die lokale Funktion Close_Connection().
[ngircd-alex.git] / src / ngircd / conn.c
index 2e896a28187d33f6d2dcadc876f49284078a3350..6182857cfeffdfd3a321af6d6ff3759c860c3137 100644 (file)
@@ -9,11 +9,18 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: conn.c,v 1.8 2001/12/23 22:02:54 alex Exp $
+ * $Id: conn.c,v 1.10 2001/12/25 22:03:47 alex Exp $
  *
  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
  *
  * $Log: conn.c,v $
+ * Revision 1.10  2001/12/25 22:03:47  alex
+ * - Conn_Close() eingefuehrt: war die lokale Funktion Close_Connection().
+ *
+ * Revision 1.9  2001/12/24 01:32:33  alex
+ * - in Conn_WriteStr() wurde das CR+LF nicht angehaengt!
+ * - Fehler-Ausgaben vereinheitlicht.
+ *
  * Revision 1.8  2001/12/23 22:02:54  alex
  * - Conn_WriteStr() nimmt nun variable Parameter,
  * - diverse kleinere Aenderungen.
@@ -102,7 +109,6 @@ LOCAL VOID Handle_Read( INT sock );
 LOCAL BOOLEAN Handle_Write( CONN_ID Idx );
 LOCAL VOID New_Connection( INT Sock );
 LOCAL CONN_ID Socket2Index( INT Sock );
-LOCAL VOID Close_Connection( CONN_ID Idx, CHAR *Msg );
 LOCAL VOID Read_Request( CONN_ID Idx );
 LOCAL BOOLEAN Try_Write( CONN_ID Idx );
 
@@ -144,7 +150,7 @@ GLOBAL VOID Conn_Exit( VOID )
                        {
                                if( My_Connections[idx].sock == i ) break;
                        }
-                       if( idx < MAX_CONNECTIONS ) Close_Connection( idx, "Server going down ..." );
+                       if( idx < MAX_CONNECTIONS ) Conn_Close( idx, "Server going down ..." );
                        else if( FD_ISSET( i, &My_Listener ))
                        {
                                close( i );
@@ -178,27 +184,27 @@ GLOBAL BOOLEAN Conn_New_Listener( 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_ALERT, "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_ALERT, "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_CRIT, "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_ALERT, "Can't bind socket: %s!", strerror( errno ));
                close( sock );
                return FALSE;
        }
@@ -206,7 +212,7 @@ GLOBAL BOOLEAN Conn_New_Listener( 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_ALERT, "Can't listen on soecket: %s!", strerror( errno ));
                close( sock );
                return FALSE;
        }
@@ -247,7 +253,7 @@ GLOBAL VOID Conn_Handler( INT Timeout )
        read_sockets = My_Sockets;
        if( select( My_Max_Fd + 1, &read_sockets, &write_sockets, NULL, &tv ) == -1 )
        {
-               if( errno != EINTR ) Log( LOG_ALERT, "select(): %s", strerror( errno ));
+               if( errno != EINTR ) Log( LOG_ALERT, "select(): %s!", strerror( errno ));
                return;
        }
        
@@ -276,17 +282,19 @@ GLOBAL BOOLEAN Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
        va_list ap;
 
        va_start( ap, Format );
-       if( vsnprintf( buffer, MAX_CMDLEN, Format, ap ) == MAX_CMDLEN )
+       if( vsnprintf( buffer, MAX_CMDLEN - 2, Format, ap ) == MAX_CMDLEN - 2 )
        {
                Log( LOG_ALERT, "String too long to send (connection %d)!", Idx );
-               Close_Connection( Idx, "Server error: String too long to send!" );
+               Conn_Close( Idx, "Server error: String too long to send!" );
                return FALSE;
        }
-       ok = Conn_Write( Idx, buffer, strlen( buffer ));
 
-#ifdef DEBUG
+#ifdef SNIFFER
        Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
 #endif
+       
+       strcat( buffer, "\r\n" );
+       ok = Conn_Write( Idx, buffer, strlen( buffer ));
 
        va_end( ap );
        return ok;
@@ -315,7 +323,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 );
-               Close_Connection( Idx, NULL );
+               Conn_Close( Idx, NULL );
                return FALSE;
        }
 
@@ -333,6 +341,32 @@ GLOBAL BOOLEAN Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
 } /* Conn_Write */
 
 
+GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg )
+{
+       /* Verbindung schliessen */
+
+       assert( Idx >= 0 );
+       assert( My_Connections[Idx].sock >= 0 );
+
+       if( Msg ) Conn_WriteStr( Idx, "ERROR :%s", Msg );
+
+       if( close( My_Connections[Idx].sock ) != 0 )
+       {
+               Log( LOG_ERR, "Error closing connection %d with %s:%d - %s!", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+               return;
+       }
+       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 ));
+       }
+
+       Client_Destroy( Client_GetFromConn( Idx ));
+
+       FD_CLR( My_Connections[Idx].sock, &My_Sockets );
+       My_Connections[Idx].sock = NONE;
+} /* Conn_Close */
+
+
 LOCAL BOOLEAN Try_Write( CONN_ID Idx )
 {
        /* Versuchen, Daten aus dem Schreib-Puffer in den
@@ -351,8 +385,8 @@ LOCAL BOOLEAN Try_Write( CONN_ID Idx )
                /* Fehler! */
                if( errno != EINTR )
                {
-                       Log( LOG_ALERT, "select(): %s", strerror( errno ));
-                       Close_Connection( Idx, NULL );
+                       Log( LOG_ALERT, "select(): %s!", strerror( errno ));
+                       Conn_Close( Idx, NULL );
                        return FALSE;
                }
        }
@@ -402,8 +436,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 ));
-               Close_Connection( Idx, NULL );
+               Log( LOG_ALERT, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
+               Conn_Close( Idx, NULL );
                return FALSE;
        }
        
@@ -430,7 +464,7 @@ LOCAL VOID New_Connection( INT Sock )
        new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
        if( new_sock < 0 )
        {
-               Log( LOG_CRIT, "Can't accept connection: %s", strerror( errno ));
+               Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
                return;
        }
                
@@ -481,31 +515,6 @@ LOCAL CONN_ID Socket2Index( INT Sock )
 } /* Socket2Index */
 
 
-LOCAL VOID Close_Connection( CONN_ID Idx, CHAR *Msg )
-{
-       /* Verbindung schlie§en */
-
-       assert( Idx >= 0 );
-       assert( My_Connections[Idx].sock >= 0 );
-       
-       if( Msg ) Conn_WriteStr( Idx, "ERROR :%s", Msg );
-
-       if( close( My_Connections[Idx].sock ) != 0 )
-       {
-               Log( LOG_ERR, "Error closing connection %d with %s:%d - %s", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
-       }
-       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 ));
-       }
-
-       Client_Destroy( Client_GetFromConn( Idx ));
-       
-       FD_CLR( My_Connections[Idx].sock, &My_Sockets );
-       My_Connections[Idx].sock = NONE;
-} /* Close_Connection */
-
-
 LOCAL VOID Read_Request( CONN_ID Idx )
 {
        /* Daten von Socket einlesen und entsprechend behandeln.
@@ -524,15 +533,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));
-               Close_Connection( Idx, NULL );
+               Conn_Close( Idx, NULL );
                return;
        }
 
        if( len < 0 )
        {
                /* Fehler beim Lesen */
-               Log( LOG_ALERT, "Read error on connection %d!", Idx );
-               Close_Connection( Idx, "Read error!" );
+               Log( LOG_ALERT, "Read error on connection %d: %s!", Idx, strerror( errno ));
+               Conn_Close( Idx, "Read error!" );
                return;
        }
 
@@ -546,7 +555,7 @@ LOCAL VOID Read_Request( CONN_ID Idx )
                 * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
                 * empfangen wird, wird der Client disconnectiert. */
                Log( LOG_ALERT, "Request too long (connection %d)!", Idx );
-               Close_Connection( Idx, "Request too long!" );
+               Conn_Close( Idx, "Request too long!" );
                return;
        }