]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
small formatting change
[ngircd-alex.git] / src / ngircd / conn.c
index 8f343ea418814102b3a0d2f02167860f33031c02..13dbadeca11157d96fc75b088fcb248c41950573 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.172 2005/08/27 23:42:23 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.178 2005/09/02 17:01:23 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -741,8 +741,9 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
                /* 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_ID(Client_ThisServer()), NOTICE_TXTPREFIX,
+                        ":%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);
                }
@@ -855,39 +856,49 @@ Handle_Write( CONN_ID Idx )
        int len;
        unsigned int wdatalen;
 
-       Log(LOG_DEBUG, "Handle_Write");
        assert( Idx > NONE );
        if ( My_Connections[Idx].sock < 0 ) {
-               Log(LOG_WARNING, "Handle_Write() on closed socket, idx %d", Idx);
+               Log(LOG_WARNING,
+                   "Handle_Write() on closed socket, connection %d", Idx);
                return false;
        }
        assert( My_Connections[Idx].sock > NONE );
 
+#ifdef DEBUG
+       Log(LOG_DEBUG, "Handle_Write() called for connection %d ...", Idx);
+#endif
+
        wdatalen = array_bytes(&My_Connections[Idx].wbuf );
 #ifdef ZLIB
-       if(( wdatalen == 0 ) && ( ! array_bytes(&My_Connections[Idx].zip.wbuf))) {
+       if (wdatalen == 0 && !array_bytes(&My_Connections[Idx].zip.wbuf)) {
                io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
                return true;
        }
 
-       /* write buffer empty, but not compression buf? -> flush compression buf. */
-       if( wdatalen == 0 ) Zip_Flush( Idx );
+       /* write buffer empty, but not compression buffer?
+         * -> flush compression buffer! */
+       if (wdatalen == 0)
+               Zip_Flush(Idx);
 #else
-       if( wdatalen == 0 ) {
+       if (wdatalen == 0) {
                io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
                return true;
        }
 #endif
 
-       wdatalen = array_bytes(&My_Connections[Idx].wbuf ); /* Zip_Flush may change wbuf  */
-       len = write( My_Connections[Idx].sock, array_start(&My_Connections[Idx].wbuf), wdatalen );
+       /* Zip_Flush() may have changed the write buffer ... */
+       wdatalen = array_bytes(&My_Connections[Idx].wbuf);
+
+       len = write(My_Connections[Idx].sock,
+                   array_start(&My_Connections[Idx].wbuf), wdatalen );
+
        if( len < 0 ) {
-               iferrno == EAGAIN || errno == EINTR)
+               if (errno == EAGAIN || errno == EINTR)
                        return true;
 
-               Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx,
-                                       My_Connections[Idx].sock, strerror( errno ));
-               Conn_Close( Idx, "Write error!", NULL, false );
+               Log(LOG_ERR, "Write error on connection %d (socket %d): %s!",
+                   Idx, My_Connections[Idx].sock, strerror(errno));
+               Conn_Close(Idx, "Write error!", NULL, false);
                return false;
        }
 
@@ -1158,13 +1169,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
@@ -1196,7 +1207,6 @@ Handle_Buffer( CONN_ID Idx )
                }
 #endif
 
-               action = false;
                if( ! ptr )
                        break;
 
@@ -1214,8 +1224,8 @@ Handle_Buffer( CONN_ID Idx )
                        return false;
                }
 
-               if (len < 3) { /* request was empty (only '\r\n') */
-                       array_trunc(&My_Connections[Idx].rbuf);
+               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
@@ -1223,28 +1233,28 @@ Handle_Buffer( CONN_ID Idx )
                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,
+                   "Connection %d: %d bytes left in read buffer.",
+                   Idx, 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 ))
@@ -1252,12 +1262,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 */
@@ -1327,9 +1337,8 @@ Check_Servers( void )
        CONN_ID idx;
        int i, n;
 
-       /* Serach all connections, are there results from the resolver? */
-       for( idx = 0; idx < Pool_Size; idx++ )
-       {
+       /* Search all connections, are there results from the resolver? */
+       for( idx = 0; idx < Pool_Size; idx++ ) {
                if( My_Connections[idx].sock != SERVER_WAIT ) continue;
 
                /* IP resolved? */
@@ -1337,20 +1346,21 @@ Check_Servers( void )
        }
 
        /* Check all configured servers */
-       for( i = 0; i < MAX_SERVERS; i++ )
-       {
+       for( i = 0; i < MAX_SERVERS; i++ ) {
                /* Valid outgoing server which isn't already connected or disabled? */
-               if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) || ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED )) continue;
+               if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) ||
+                       ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED ))
+                               continue;
 
                /* Is there already a connection in this group? */
-               if( Conf_Server[i].group > NONE )
-               {
-                       for( n = 0; n < MAX_SERVERS; n++ )
-                       {
-                               if( n == i ) continue;
-                               if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
+               if( Conf_Server[i].group > NONE ) {
+                       for (n = 0; n < MAX_SERVERS; n++) {
+                               if (n == i) continue;
+                               if ((Conf_Server[n].conn_id > NONE) &&
+                                       (Conf_Server[n].group == Conf_Server[i].group))
+                                               break;
                        }
-                       if( n < MAX_SERVERS ) continue;
+                       if (n < MAX_SERVERS) continue;
                }
 
                /* Check last connect attempt? */
@@ -1402,11 +1412,14 @@ New_Server( int Server, CONN_ID Idx )
        /* Did we get a valid IP address? */
        if( ! Conf_Server[Server].ip[0] ) {
                /* No. Free connection structure and abort: */
-               Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
-               goto out;
+               Log( LOG_ERR, "Can't connect to \"%s\": ip address unknown!", Conf_Server[Server].host );
+               Init_Conn_Struct( Idx );
+               Conf_Server[Server].conn_id = NONE;
+               return;
        }
 
-       Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Conf_Server[Server].port, Idx );
+       Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d ... ", Conf_Server[Server].host,
+                                                       Conf_Server[Server].ip, Conf_Server[Server].port );
 
 #ifdef HAVE_INET_ATON
        if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
@@ -1546,8 +1559,7 @@ void Read_Resolver_Result( int r_fd )
 
        Log( LOG_DEBUG, "Resolver: started, fd %d", r_fd );
        /* Search associated connection ... */
-       for( i = 0; i < Pool_Size; i++ )
-       {
+       for( i = 0; i < Pool_Size; i++ ) {
                if(( My_Connections[i].sock != NONE )
                  && ( My_Connections[i].res_stat != NULL )
                  && ( My_Connections[i].res_stat->pipe[0] == r_fd ))