]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
Minimal changes (needed for merging resolver changes)
[ngircd-alex.git] / src / ngircd / conn.c
index 3f7b1518be561004ce51a8c28e32abb4b890aa05..8f04a1bb4534160fa16a0d26b224ba248f907f00 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.174 2005/08/28 16:51:20 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.180 2005/09/11 11:42:48 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,50 @@ 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);
+#ifdef DEBUG
+               Log(LOG_DEBUG,
+                   "Handle_Write() on closed socket, connection %d", Idx);
+#endif
                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;
        }
 
@@ -1230,9 +1242,10 @@ Handle_Buffer( CONN_ID Idx )
 
                array_moveleft(&My_Connections[Idx].rbuf, 1, len);
 #ifdef DEBUG
-               Log(LOG_DEBUG, "%u 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 ))
@@ -1270,7 +1283,8 @@ Check_Connections( void )
        CONN_ID i;
 
        for( i = 0; i < Pool_Size; i++ ) {
-               if( My_Connections[i].sock == NONE ) continue;
+               if (My_Connections[i].sock < 0)
+                       continue;
 
                c = Client_GetFromConn( i );
                if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
@@ -1325,9 +1339,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? */
@@ -1335,33 +1348,35 @@ 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? */
-               if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
+               if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry )
+                       continue;
 
                /* Okay, try to connect now */
                Conf_Server[i].lasttry = time( NULL );
 
                /* Search free connection structure */
                for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
-               if( idx >= Pool_Size )
-               {
-                       Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
+               if (idx >= Pool_Size) {
+                       Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!",
+                                                                                       Pool_Size );
                        return;
                }
 #ifdef DEBUG
@@ -1400,11 +1415,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 )
@@ -1544,15 +1562,13 @@ 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 ))
                        break;
        }
-       if( i >= Pool_Size )
-       {
+       if( i >= Pool_Size ) {
                /* Ops, none found? Probably the connection has already
                 * been closed!? We'll ignore that ... */
                io_close( r_fd );
@@ -1691,8 +1707,7 @@ Count_Connections( struct sockaddr_in addr_in )
        int i, cnt;
        
        cnt = 0;
-       for( i = 0; i < Pool_Size; i++ )
-       {
+       for( i = 0; i < Pool_Size; i++ ) {
                if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].addr.sin_addr.s_addr == addr_in.sin_addr.s_addr )) cnt++;
        }
        return cnt;