]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conn.c
Handle commands in the read buffer before reading more data
[ngircd-alex.git] / src / ngircd / conn.c
index e14e6c08037aa53e57fd40825bba446313ef1b68..ca7030f4ebae1e487910310da2278e96f8287252 100644 (file)
@@ -734,6 +734,18 @@ 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);
+                               continue;
+                       }
+
                        io_event_add(My_Connections[i].sock, IO_WANTREAD);
                }
 
@@ -1272,6 +1284,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!",
@@ -1279,7 +1294,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;
        }
 
@@ -1540,16 +1555,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))
@@ -1557,7 +1577,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);
@@ -1565,12 +1584,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,
@@ -1580,13 +1601,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,