]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
Added prefix to connection statistic NOTICE.
[ngircd-alex.git] / src / ngircd / conn.c
index 2d0c2fddfbaf95928a38281c23b4666e673cb137..165d4678b472d41b729f90a09113055fda87bd97 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.169 2005/08/02 22:48:57 alex Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.175 2005/08/29 11:11:15 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -736,19 +736,24 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        c = Client_GetFromConn( Idx );
 
        /* Should the client be informed? */
-       if( InformClient )
-       {
+       if (InformClient) {
 #ifndef STRICT_RFC
                /* Send statistics to client if registered as user: */
-               if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
-               {
-                       Conn_WriteStr( Idx, "NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.", Client_ThisServer( ), NOTICE_TXTPREFIX, (double)My_Connections[Idx].bytes_in / 1024,  (double)My_Connections[Idx].bytes_out / 1024 );
+               if ((c != NULL) && (Client_Type(c) == CLIENT_USER)) {
+                       Conn_WriteStr( Idx,
+                        ":%s NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.",
+                        Client_ID(Client_ThisServer()), Client_ID(c),
+                        NOTICE_TXTPREFIX,
+                        (double)My_Connections[Idx].bytes_in / 1024,
+                        (double)My_Connections[Idx].bytes_out / 1024);
                }
 #endif
 
                /* Send ERROR to client (see RFC!) */
-               if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
-               else Conn_WriteStr( Idx, "ERROR :Closing connection." );
+               if (FwdMsg)
+                       Conn_WriteStr(Idx, "ERROR :%s", FwdMsg);
+               else
+                       Conn_WriteStr(Idx, "ERROR :Closing connection.");
        }
 
        /* Try to write out the write buffer */
@@ -1074,6 +1079,7 @@ Read_Request( CONN_ID Idx )
 
        int len;
        char readbuf[1024];
+       CLIENT *c;
 
        assert( Idx > NONE );
        assert( My_Connections[Idx].sock > NONE );
@@ -1125,13 +1131,21 @@ Read_Request( CONN_ID Idx )
                }
        }
 
-       /* Connection-Statistik aktualisieren */
+       /* Update connection statistics */
        My_Connections[Idx].bytes_in += len;
 
-       /* Timestamp aktualisieren */
-       My_Connections[Idx].lastdata = time( NULL );
-
-       Handle_Buffer( Idx );
+       /* Update timestamp of last data received if this connection is
+        * registered as a user, server or service connection. Don't update
+        * otherwise, so users have at least Conf_PongTimeout seconds time to
+        * register with the IRC server -- see Check_Connections(). */
+       c = Client_GetFromConn(Idx);
+       if (c && (Client_Type(c) == CLIENT_USER
+                 || Client_Type(c) == CLIENT_SERVER
+                 || Client_Type(c) == CLIENT_SERVICE))
+               My_Connections[Idx].lastdata = time(NULL);
+
+       /* Look at the data in the (read-) buffer of this connection */
+       Handle_Buffer(Idx);
 } /* Read_Request */
 
 
@@ -1145,13 +1159,13 @@ Handle_Buffer( CONN_ID Idx )
 #endif
        char *ptr;
        int len, delta;
-       bool action, result;
+       bool result;
 #ifdef ZLIB
        bool old_z;
 #endif
 
        result = false;
-       do {
+       for (;;) {
                /* Check penalty */
                if( My_Connections[Idx].delaytime > time( NULL )) return result;
 #ifdef ZLIB
@@ -1183,7 +1197,6 @@ Handle_Buffer( CONN_ID Idx )
                }
 #endif
 
-               action = false;
                if( ! ptr )
                        break;
 
@@ -1201,33 +1214,36 @@ Handle_Buffer( CONN_ID Idx )
                        return false;
                }
 
+               if (len <= 2) { /* request was empty (only '\r\n') */
+                       array_moveleft(&My_Connections[Idx].rbuf, 1, delta); /* delta is either 1 or 2 */
+                       break;
+               }
 #ifdef ZLIB
                /* remember if stream is already compressed */
                old_z = My_Connections[Idx].options & CONN_ZIP;
 #endif
 
-               if( len > delta )
-               {
-                       /* A Request was read */
-                       My_Connections[Idx].msg_in++;
-                       if( ! Parse_Request( Idx, (char*)array_start(&My_Connections[Idx].rbuf) )) return false;
-                       else action = true;
+               My_Connections[Idx].msg_in++;
+               if (!Parse_Request(Idx, (char*)array_start(&My_Connections[Idx].rbuf) ))
+                       return false;
 
-                       array_moveleft(&My_Connections[Idx].rbuf, 1, len);
+               result = true;
+
+               array_moveleft(&My_Connections[Idx].rbuf, 1, len);
 #ifdef DEBUG
-                       Log(LOG_DEBUG, "%d byte left in rbuf", array_bytes(&My_Connections[Idx].rbuf));
+               Log(LOG_DEBUG, "%u byte left in rbuf", array_bytes(&My_Connections[Idx].rbuf));
 #endif
-               }
 
 #ifdef ZLIB
-               if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
+               if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) &&
+                               ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
                {
                        /* The last Command activated Socket-Compression.
                         * Data that was read after that needs to be copied to Unzip-buf
                         * for decompression */
                        if( array_bytes(&My_Connections[Idx].rbuf)> ZREADBUFFER_LEN ) {
-                               /* No space left */
-                               Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", array_bytes(&My_Connections[Idx].rbuf ));
+                               Log( LOG_ALERT, "Connection %d: No space left in unzip buf (need %u bytes)!",
+                                                               Idx, array_bytes(&My_Connections[Idx].rbuf ));
                                return false;
                        }
                        if (!array_copy( &My_Connections[Idx].zip.rbuf, &My_Connections[Idx].rbuf ))
@@ -1235,12 +1251,12 @@ Handle_Buffer( CONN_ID Idx )
 
                        array_trunc(&My_Connections[Idx].rbuf);
 #ifdef DEBUG
-                       Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", array_bytes(&My_Connections[Idx].zip.rbuf));
+                       Log( LOG_DEBUG, "Moved already received data (%u bytes) to uncompression buffer.",
+                                                               array_bytes(&My_Connections[Idx].zip.rbuf));
 #endif /* DEBUG */
                }
 #endif /* ZLIB */
-               if( action ) result = true;
-       } while( action );
+       }
 
        return result;
 } /* Handle_Buffer */
@@ -1282,14 +1298,19 @@ Check_Connections( void )
                }
                else
                {
-                       /* connection is not fully established yet */
-                       if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
-                       {
-                               /* Timeout */
+                       /* The connection is not fully established yet, so
+                        * we don't do the PING-PONG game here but instead
+                        * disconnect the client after "a short time" if it's
+                        * still not registered. */
+
+                       if (My_Connections[i].lastdata <
+                           time(NULL) - Conf_PongTimeout) {
 #ifdef DEBUG
-                               Log( LOG_DEBUG, "Connection %d timed out ...", i );
+                               Log(LOG_DEBUG,
+                                   "Unregistered connection %d timed out ...",
+                                   i);
 #endif
-                               Conn_Close( i, NULL, "Timeout", false );
+                               Conn_Close(i, NULL, "Timeout", false);
                        }
                }
        }