]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
do not call Conn_Close when io_event_create fails
[ngircd-alex.git] / src / ngircd / conn.c
index 39cec07ad858ecc225423e7381bc60ae177039bb..30b86b1adf8700f7b2b10006b4d67d62a6b1a1c8 100644 (file)
@@ -17,7 +17,7 @@
 #include "portab.h"
 #include "io.h"
 
-static char UNUSED id[] = "$Id: conn.c,v 1.197 2006/07/23 15:22:56 alex Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.199 2006/12/17 22:55:07 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -701,11 +701,16 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        /* Mark link as "closing" */
        Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCLOSING );
                
-       if( LogMsg ) txt = LogMsg;
-       else txt = FwdMsg;
-       if( ! txt ) txt = "Reason unknown";
+       if (LogMsg)
+               txt = LogMsg;
+       else
+               txt = FwdMsg;
+       if (! txt)
+               txt = "Reason unknown";
 
-       Log( LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx, LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ));
+       Log(LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx,
+           LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host,
+           ntohs(My_Connections[Idx].addr.sin_port));
 
        /* Search client, if any */
        c = Conn_GetClient( Idx );
@@ -741,39 +746,57 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
        c = Conn_GetClient( Idx );
 
        /* Shut down socket */
-       if( ! io_close( My_Connections[Idx].sock ))
-       {
+       if (! io_close(My_Connections[Idx].sock)) {
                /* Oops, we can't close the socket!? This is ... ugly! */
-               Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+               Log(LOG_CRIT,
+                   "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)",
+                   Idx, My_Connections[Idx].sock, My_Connections[Idx].host,
+                   ntohs(My_Connections[Idx].addr.sin_port), strerror(errno));
        }
 
        /* Mark socket as invalid: */
        My_Connections[Idx].sock = NONE;
 
        /* If there is still a client, unregister it now */
-       if( c ) Client_Destroy( c, LogMsg, FwdMsg, true );
+       if (c)
+               Client_Destroy(c, LogMsg, FwdMsg, true);
 
        /* Calculate statistics and log information */
        in_k = (double)My_Connections[Idx].bytes_in / 1024;
        out_k = (double)My_Connections[Idx].bytes_out / 1024;
 #ifdef ZLIB
-       if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
+       if (Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP)) {
                in_z_k = (double)My_Connections[Idx].zip.bytes_in / 1024;
                out_z_k = (double)My_Connections[Idx].zip.bytes_out / 1024;
+               /* Make sure that no division by zero can occur during
+                * the calculation of in_p and out_p: in_z_k and out_z_k
+                * are non-zero, that's guaranteed by the protocol until
+                * compression can be enabled. */
+               if (! in_z_k)
+                       in_z_k = in_k;
+               if (! out_z_k)
+                       out_z_k = out_k;
                in_p = (int)(( in_k * 100 ) / in_z_k );
                out_p = (int)(( out_k * 100 ) / out_z_k );
-               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 );
+               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 with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, 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);
        }
 
        /* cancel running resolver */
-       if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat)) {
+       if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat))
                Resolve_Shutdown(&My_Connections[Idx].res_stat);
-       }
 
        /* Servers: Modify time of next connect attempt? */
        Conf_UnsetServer( Idx );
@@ -790,6 +813,7 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
 
        array_free(&My_Connections[Idx].rbuf);
        array_free(&My_Connections[Idx].wbuf);
+
        /* Clean up connection structure (=free it) */
        Init_Conn_Struct( Idx );
 
@@ -974,23 +998,18 @@ New_Connection( int Sock )
        c = Client_NewLocal( new_sock, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
        if( ! c ) {
                Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
-               Simple_Message( new_sock, "ERROR :Internal error" );
-               close( new_sock );
-               return -1;
+               goto out;
        }
 
+       /* register callback */
+       if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver))
+               goto out;
+
        Init_Conn_Struct( new_sock );
        My_Connections[new_sock].sock = new_sock;
        My_Connections[new_sock].addr = new_addr;
        My_Connections[new_sock].client = c;
 
-       /* register callback */
-       if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
-               Simple_Message( new_sock, "ERROR :Internal error" );
-               Conn_Close( new_sock, "io_event_create() failed", NULL, false );
-               return -1;
-       }
-
        Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", new_sock,
                        inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
 
@@ -1006,7 +1025,11 @@ New_Connection( int Sock )
        /* Penalty-Zeit setzen */
        Conn_SetPenalty( new_sock, 4 );
        return new_sock;
-} /* New_Connection */
+ out:
+       Simple_Message( new_sock, "ERROR :Internal error" );
+       close( new_sock );
+       return -1;
+}/* New_Connection */
 
 
 static CONN_ID