updates.
[silc.git] / lib / silccore / silcpacket.c
index 2e7b9cbf006b1929687356a2d6a001e1ee591f60..d05c0bd104196fd37b6217ed7b9ef27d6cfc8b4e 100644 (file)
@@ -187,7 +187,8 @@ void silc_packet_assemble(SilcPacketContext *ctx, SilcCipher cipher)
 
   /* Get random padding */
 #if 1
-  for (i = 0; i < ctx->padlen; i++) tmppad[i] = silc_rng_global_get_byte();
+  for (i = 0; i < ctx->padlen; i++) tmppad[i] = 
+                                     silc_rng_global_get_byte_fast();
 #else
   /* XXX: For testing - to be removed */
   memset(tmppad, 65, sizeof(tmppad));
@@ -332,7 +333,8 @@ 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;
+  
   if (sock->inbuf->len < SILC_PACKET_MIN_HEADER_LEN)
     return;
 
@@ -340,7 +342,7 @@ void silc_packet_receive_process(SilcSocketConnection sock,
     mac_len = silc_hmac_len(hmac);
 
   /* Parse the packets from the data */
-  while (sock->inbuf->len > 0) {
+  while (sock->inbuf->len > 0 && cont) {
 
     /* Decrypt first 16 bytes of the packet */
     if (!SILC_IS_INBUF_PENDING(sock) && cipher)
@@ -378,7 +380,7 @@ void silc_packet_receive_process(SilcSocketConnection sock,
     silc_buffer_put(parse_ctx->packet->buffer, sock->inbuf->data, 
                    paddedlen + mac_len);
 
-    SILC_LOG_HEXDUMP(("Incoming packet (%d) (%d bytes decrypted), len %d", 
+    SILC_LOG_HEXDUMP(("Incoming packet (%d) (%dB decrypted), len %d", 
                      sequence - 1, block_len, paddedlen + mac_len),
                     sock->inbuf->data, paddedlen + mac_len);
 
@@ -401,18 +403,28 @@ void silc_packet_receive_process(SilcSocketConnection sock,
 
     /* Decrypt rest of the packet */
     if (cipher)
-      silc_packet_decrypt(cipher, hmac, parse_ctx->packet->sequence, 
-                         parse_ctx->packet->buffer, parse_ctx->normal);
-
-    /* Call the parser */
-    if (parser)
-      (*parser)(parse_ctx, parser_context);
+      if (silc_packet_decrypt(cipher, hmac, parse_ctx->packet->sequence, 
+                             parse_ctx->packet->buffer, 
+                             parse_ctx->normal) == -1) {
+       SILC_LOG_WARNING(("Packet decryption failed %s:%d [%s]", 
+                         sock->hostname, sock->port,
+                         (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
+                          sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
+                          sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
+                          "Router")));
+      }
 
     /* Pull the packet from inbuf thus we'll get the next one
        in the inbuf. */
     silc_buffer_pull(sock->inbuf, paddedlen + mac_len);
+
+    /* Call the parser */
+    cont = (*parser)(parse_ctx, parser_context);
   }
 
+  if (cont == FALSE && sock->inbuf->len > 0)
+    return;
+
   SILC_LOG_DEBUG(("Clearing inbound buffer"));
   silc_buffer_clear(sock->inbuf);
 }