updates
[silc.git] / lib / silccore / silcpacket.c
index d05c0bd104196fd37b6217ed7b9ef27d6cfc8b4e..190682868381958e12b1e795c889226101e94b14 100644 (file)
@@ -323,7 +323,7 @@ int silc_packet_receive(SilcSocketConnection sock)
    SilcPacketParserContext will indicate also whether the received
    packet was normal or special packet. */
 
-void silc_packet_receive_process(SilcSocketConnection sock,
+bool silc_packet_receive_process(SilcSocketConnection sock,
                                 bool local_is_router,
                                 SilcCipher cipher, SilcHmac hmac,
                                 uint32 sequence,
@@ -332,11 +332,14 @@ void silc_packet_receive_process(SilcSocketConnection sock,
 {
   SilcPacketParserContext *parse_ctx;
   int packetlen, paddedlen, mac_len = 0;
-  int block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
   bool cont = TRUE;
-  
+
+  /* Do not process for disconnected connection */
+  if (SILC_IS_DISCONNECTED(sock))
+    return TRUE;
+
   if (sock->inbuf->len < SILC_PACKET_MIN_HEADER_LEN)
-    return;
+    return TRUE;
 
   if (hmac)
     mac_len = silc_hmac_len(hmac);
@@ -344,6 +347,11 @@ void silc_packet_receive_process(SilcSocketConnection sock,
   /* Parse the packets from the data */
   while (sock->inbuf->len > 0 && cont) {
 
+    if (sock->inbuf->len < SILC_PACKET_MIN_HEADER_LEN) {
+      SILC_LOG_DEBUG(("Partial packet in queue, waiting for the rest"));
+      return TRUE;
+    }
+
     /* Decrypt first 16 bytes of the packet */
     if (!SILC_IS_INBUF_PENDING(sock) && cipher)
       silc_cipher_decrypt(cipher, sock->inbuf->data, sock->inbuf->data, 
@@ -356,20 +364,21 @@ void silc_packet_receive_process(SilcSocketConnection sock,
     if (packetlen < SILC_PACKET_MIN_LEN) {
       SILC_LOG_DEBUG(("Received invalid packet, dropped"));
       silc_buffer_clear(sock->inbuf);
-      return;
+      return FALSE;
     }
 
     if (sock->inbuf->len < paddedlen + mac_len) {
       SILC_LOG_DEBUG(("Received partial packet, waiting for the rest"
                      "(%d < %d)", sock->inbuf->len, paddedlen + mac_len));
       SILC_SET_INBUF_PENDING(sock);
-      return;
+      return TRUE;
     }
 
     SILC_UNSET_INBUF_PENDING(sock);
     parse_ctx = silc_calloc(1, sizeof(*parse_ctx));
     parse_ctx->packet = silc_packet_context_alloc();
     parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
+    parse_ctx->packet->type = sock->inbuf->data[3];
     parse_ctx->packet->padlen = sock->inbuf->data[4];
     parse_ctx->packet->sequence = sequence++;
     parse_ctx->sock = sock;
@@ -381,7 +390,8 @@ void silc_packet_receive_process(SilcSocketConnection sock,
                    paddedlen + mac_len);
 
     SILC_LOG_HEXDUMP(("Incoming packet (%d) (%dB decrypted), len %d", 
-                     sequence - 1, block_len, paddedlen + mac_len),
+                     sequence - 1, SILC_PACKET_MIN_HEADER_LEN, 
+                     paddedlen + mac_len),
                     sock->inbuf->data, paddedlen + mac_len);
 
     /* Check whether this is normal or special packet */
@@ -423,10 +433,11 @@ void silc_packet_receive_process(SilcSocketConnection sock,
   }
 
   if (cont == FALSE && sock->inbuf->len > 0)
-    return;
+    return TRUE;
 
   SILC_LOG_DEBUG(("Clearing inbound buffer"));
   silc_buffer_clear(sock->inbuf);
+  return TRUE;
 }
 
 /* Checks MAC in the packet. Returns TRUE if MAC is Ok. This is called
@@ -452,7 +463,7 @@ static int silc_packet_check_mac(SilcHmac hmac, SilcBuffer buffer,
     silc_hmac_final(hmac, mac, &mac_len);
 
     /* Compare the HMAC's (buffer->tail has the packet's HMAC) */
-    if (memcmp(mac, buffer->tail, mac_len)) {
+    if (memcmp(buffer->tail, mac, mac_len)) {
       SILC_LOG_ERROR(("MAC failed"));
       return FALSE;
     }