Fixed a buf in data reading. The code now assures that all data
authorPekka Riikonen <priikone@silcnet.org>
Sat, 30 Sep 2000 06:06:09 +0000 (06:06 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 30 Sep 2000 06:06:09 +0000 (06:06 +0000)
is read before continuing packet prcoessing.

lib/silccore/silcpacket.c

index 8cb2799552e401223dacd42547815fb18708b6be..5e07ff205fd39a89d0abbd02376a2b2bf5b6a5d8 100644 (file)
 /*
  * Created: Fri Jul 25 18:52:14 1997
  */
-/*
- * $Id$
- * $Log$
- * Revision 1.4  2000/07/18 06:51:58  priikone
- *     Debug version bug fixes.
- *
- * Revision 1.3  2000/07/14 06:10:15  priikone
- *     Moved all the generic packet sending, enryption, reception,
- *     decryption and processing function from client and server to
- *     here as they were duplicated code in the applications. Now they
- *     are generic code over generic API. Some functions were rewritter;
- *     packet reception and HMAC computation and checking is now more
- *     optimized.
- *
- * Revision 1.2  2000/07/05 06:06:35  priikone
- *     Global cosmetic change.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 
@@ -393,12 +372,24 @@ int silc_packet_receive_process(SilcSocketConnection sock,
      errors we either read a whole packet or the read packet is 
      incorrect and will be dropped. */
   SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
-  if (sock->inbuf->len < paddedlen || (packetlen < SILC_PACKET_MIN_LEN)) {
+  if (packetlen < SILC_PACKET_MIN_LEN) {
     SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
     silc_buffer_clear(sock->inbuf);
     return FALSE;
   }
 
+  if (sock->inbuf->len < paddedlen) {
+    /* Two cases: either we haven't read all of the data or this 
+       packet is malformed. Try to read data from the connection.
+       If it fails this packet is malformed. */
+    silc_schedule_with_fd(sock->sock, SILC_TASK_READ, 0, 1);
+    if (silc_packet_receive(sock) < 0) {
+      SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
+      silc_buffer_clear(sock->inbuf);
+      return FALSE;
+    }
+  }
+
   /* Parse the packets from the data */
   if (sock->inbuf->len - 2 > (paddedlen + mac_len)) {
     /* Received possibly many packets at once */
@@ -406,10 +397,17 @@ int silc_packet_receive_process(SilcSocketConnection sock,
     if (hmac)
       mac_len = hmac->hash->hash->hash_len;
 
-    while(sock->inbuf->len > 0) {
+    while (sock->inbuf->len > 0) {
       SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
 
       if (sock->inbuf->len < paddedlen) {
+       /* Two cases: either we haven't read all of the data or this 
+          packet is malformed. Try to read data from the connection.
+          If it fails this packet is malformed. */
+       silc_schedule_with_fd(sock->sock, SILC_TASK_READ, 0, 1);
+       if (silc_packet_receive(sock) > 0) 
+         continue;
+
        SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
        return FALSE;
       }