]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
Don't wait for the network when read buffers possibly hold commands
[ngircd-alex.git] / src / ngircd / conn.c
index 16cf13cf0ef6478e1932688e8da82baed6e1bdb7..c304fdb574ffc497552f76fec4c531bb5c359abe 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
+ * Copyright (c)2001-2019 Alexander Barton (alex@barton.de) and Contributors.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -10,6 +10,7 @@
  */
 
 #define CONN_MODULE
+#define CONN_MODULE_GLOBAL_INIT
 
 #include "portab.h"
 
@@ -556,8 +557,8 @@ InitSinaddrListenAddr(ng_ipaddr_t *addr, const char *listen_addrstr, UINT16 Port
        if (!ret) {
                assert(listen_addrstr);
                Log(LOG_CRIT,
-                   "Can't bind to [%s]:%u: can't convert ip address \"%s\"!",
-                   listen_addrstr, Port, listen_addrstr);
+                   "Can't listen on [%s]:%u: Failed to parse IP address!",
+                   listen_addrstr, Port);
        }
        return ret;
 }
@@ -659,9 +660,14 @@ Conn_Handler(void)
        size_t wdatalen;
        struct timeval tv;
        time_t t;
+       bool command_available;
+
+       Log(LOG_NOTICE, "Server \"%s\" (on \"%s\") ready.",
+           Client_ID(Client_ThisServer()), Client_Hostname(Client_ThisServer()));
 
        while (!NGIRCd_SignalQuit && !NGIRCd_SignalRestart) {
                t = time(NULL);
+               command_available = false;
 
                /* Check configured servers and established links */
                Check_Servers();
@@ -730,16 +736,31 @@ Conn_Handler(void)
                                continue;
                        }
 
+                       if (array_bytes(&My_Connections[i].rbuf) >= COMMAND_LEN) {
+                               /* There is still more data in the read buffer
+                                * than a single valid command can get long:
+                                * so either there is a complete command, or
+                                * invalid data. Therefore don't try to read in
+                                * even more data from the network but wait for
+                                * this command(s) to be handled first! */
+                               io_event_del(My_Connections[i].sock,
+                                            IO_WANTREAD);
+                               command_available = true;
+                               continue;
+                       }
+
                        io_event_add(My_Connections[i].sock, IO_WANTREAD);
                }
 
-               /* Set the timeout for reading from the network to 1 second,
-                * which is the granularity with witch we handle "penalty
-                * times" for example.
+               /* Don't wait for data when there is still at least one command
+                * available in a read buffer which can be handled immediately;
+                * set the timeout for reading from the network to 1 second
+                * otherwise, which is the granularity with witch we handle
+                * "penalty times" for example.
                 * Note: tv_sec/usec are undefined(!) after io_dispatch()
                 * returns, so we have to set it before each call to it! */
                tv.tv_usec = 0;
-               tv.tv_sec = 1;
+               tv.tv_sec = command_available ? 0 : 1;
 
                /* Wait for activity ... */
                i = io_dispatch(&tv);
@@ -1081,9 +1102,9 @@ Conn_Close(CONN_ID Idx, const char *LogMsg, const char *FwdMsg, bool InformClien
                 * 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)
+               if (in_z_k <= 0)
                        in_z_k = in_k;
-               if (! out_z_k)
+               if (out_z_k <= 0)
                        out_z_k = out_k;
                in_p = (int)(( in_k * 100 ) / in_z_k );
                out_p = (int)(( out_k * 100 ) / out_z_k );
@@ -1268,6 +1289,9 @@ Handle_Write( CONN_ID Idx )
                if (errno == EAGAIN || errno == EINTR)
                        return true;
 
+               /* Log write errors but do not close the connection yet.
+                * Calling Conn_Close() now could result in too many recursive calls.
+                */
                if (!Conn_OPTION_ISSET(&My_Connections[Idx], CONN_ISCLOSING))
                        Log(LOG_ERR,
                            "Write error on connection %d (socket %d): %s!",
@@ -1275,7 +1299,7 @@ Handle_Write( CONN_ID Idx )
                else
                        LogDebug("Recursive write error on connection %d (socket %d): %s!",
                                 Idx, My_Connections[Idx].sock, strerror(errno));
-               Conn_Close(Idx, "Write error", NULL, false);
+
                return false;
        }
 
@@ -1536,16 +1560,21 @@ Socket2Index( int Sock )
  * @param Idx  Connection index.
  */
 static void
-Read_Request( CONN_ID Idx )
+Read_Request(CONN_ID Idx)
 {
        ssize_t len;
        static const unsigned int maxbps = COMMAND_LEN / 2;
        char readbuf[READBUFFER_LEN];
        time_t t;
        CLIENT *c;
-       assert( Idx > NONE );
-       assert( My_Connections[Idx].sock > NONE );
 
+       assert(Idx > NONE);
+       assert(My_Connections[Idx].sock > NONE);
+
+       /* Check if the read buffer is "full". Basically this shouldn't happen
+        * here, because as long as there possibly are commands in the read
+        * buffer (buffer usage > COMMAND_LEN), the socket shouldn't be
+        * scheduled for reading in Conn_Handler() at all ... */
 #ifdef ZLIB
        if ((array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN) ||
                (array_bytes(&My_Connections[Idx].zip.rbuf) >= READBUFFER_LEN))
@@ -1553,7 +1582,6 @@ Read_Request( CONN_ID Idx )
        if (array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN)
 #endif
        {
-               /* Read buffer is full */
                Log(LOG_ERR,
                    "Receive buffer space exhausted (connection %d): %d/%d bytes",
                    Idx, array_bytes(&My_Connections[Idx].rbuf), READBUFFER_LEN);
@@ -1561,12 +1589,14 @@ Read_Request( CONN_ID Idx )
                return;
        }
 
+       /* Now read new data from the network, up to READBUFFER_LEN bytes ... */
 #ifdef SSL_SUPPORT
        if (Conn_OPTION_ISSET(&My_Connections[Idx], CONN_SSL))
-               len = ConnSSL_Read( &My_Connections[Idx], readbuf, sizeof(readbuf));
+               len = ConnSSL_Read(&My_Connections[Idx], readbuf, sizeof(readbuf));
        else
 #endif
-       len = read(My_Connections[Idx].sock, readbuf, sizeof(readbuf));
+               len = read(My_Connections[Idx].sock, readbuf, sizeof(readbuf));
+
        if (len == 0) {
                LogDebug("Client \"%s:%u\" is closing connection %d ...",
                         My_Connections[Idx].host,
@@ -1576,13 +1606,20 @@ Read_Request( CONN_ID Idx )
        }
 
        if (len < 0) {
-               if( errno == EAGAIN ) return;
+               if (errno == EAGAIN)
+                       return;
+
                Log(LOG_ERR, "Read error on connection %d (socket %d): %s!",
                    Idx, My_Connections[Idx].sock, strerror(errno));
                Conn_Close(Idx, "Read error", "Client closed connection",
                           false);
                return;
        }
+
+       /* Now append the newly received data to the connection buffer.
+        * NOTE: This can lead to connection read buffers being bigger(!) than
+        * READBUFFER_LEN bytes, as we add up to READBUFFER_LEN new bytes to a
+        * buffer possibly being "almost" READBUFFER_LEN bytes already! */
 #ifdef ZLIB
        if (Conn_OPTION_ISSET(&My_Connections[Idx], CONN_ZIP)) {
                if (!array_catb(&My_Connections[Idx].zip.rbuf, readbuf,
@@ -1918,8 +1955,11 @@ Check_Servers(void)
                Conf_Server[i].lasttry = time_now;
                Conf_Server[i].conn_id = SERVER_WAIT;
                assert(Proc_GetPipeFd(&Conf_Server[i].res_stat) < 0);
-               Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host,
-                            cb_Connect_to_Server);
+
+               /* Start resolver subprocess ... */
+               if (!Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host,
+                                 cb_Connect_to_Server))
+                       Conf_Server[i].conn_id = NONE;
        }
 } /* Check_Servers */
 
@@ -2399,7 +2439,7 @@ Conn_GetFromProc(int fd)
  * @param Reason The reason, see THROTTLE_xxx constants.
  * @param Idx The connection index.
  * @param Client The client of this connection.
- * @param Seconds The time to delay this connection.
+ * @param Value The time to delay this connection.
  */
 static void
 Throttle_Connection(const CONN_ID Idx, CLIENT *Client, const int Reason,