updates.
[silc.git] / lib / silccore / silcpacket.c
index b879c42927573e86cf71870e67b9f33897e6dcb3..e1637c26d2032d906f1b5a0b8d0988cdcbc1db82 100644 (file)
@@ -109,10 +109,10 @@ int silc_packet_send(SilcSocketConnection sock, int force_send)
    cannot be used. */
 
 void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, 
-                        SilcBuffer buffer, unsigned int len)
+                        SilcBuffer buffer, uint32 len)
 {
   unsigned char mac[32];
-  unsigned int mac_len;
+  uint32 mac_len;
 
   /* Compute HMAC. This assumes that HMAC is created from the entire
      data area thus this uses the length found in buffer, not the length
@@ -162,23 +162,23 @@ void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac,
 
    Packet construct is as follows (* = won't be encrypted):
 
-   x bytes       SILC Header
+   n bytes       SILC Header
       2 bytes     Payload length  (*)
       1 byte      Flags
       1 byte      Packet type
-      1 byte      Source ID Type
       2 bytes     Source ID Length
-      x bytes     Source ID
-      1 byte      Destination ID Type
       2 bytes     Destination ID Length
-      x bytes     Destination ID
+      1 byte      Source ID Type
+      n bytes     Source ID
+      1 byte      Destination ID Type
+      n bytes     Destination ID
 
    1 - 16 bytes    Padding
 
-   x bytes        Data payload
+   n bytes        Data payload
 
    All fields in the packet will be authenticated by MAC. The MAC is
-   not computed here, it must be computed differently before encrypting
+   not computed here, it must be computed separately before encrypting
    the packet.
 
 */
@@ -243,9 +243,9 @@ void silc_packet_assemble(SilcPacketContext *ctx)
    outgoing buffer in SilcSocketConnection object. */
 
 void silc_packet_send_prepare(SilcSocketConnection sock,
-                             unsigned int header_len,
-                             unsigned int padlen,
-                             unsigned int data_len)
+                             uint32 header_len,
+                             uint32 padlen,
+                             uint32 data_len)
 {
   int totlen, oldlen;
 
@@ -264,10 +264,12 @@ void silc_packet_send_prepare(SilcSocketConnection sock,
       /* There is some pending data in the buffer. */
 
       /* Allocate more space if needed */
-      if ((sock->outbuf->end - sock->outbuf->tail) < data_len) {
+      if ((sock->outbuf->end - sock->outbuf->tail) < 
+         (totlen + 20)) {
        SILC_LOG_DEBUG(("Reallocating outgoing data buffer"));
        sock->outbuf = silc_buffer_realloc(sock->outbuf, 
-                                          sock->outbuf->truelen + totlen);
+                                          sock->outbuf->truelen +
+                                          (totlen * 2));
       }
 
       oldlen = sock->outbuf->len;
@@ -276,6 +278,15 @@ void silc_packet_send_prepare(SilcSocketConnection sock,
     } else {
       /* Buffer is free for use */
       silc_buffer_clear(sock->outbuf);
+
+      /* Allocate more space if needed */
+      if ((sock->outbuf->end - sock->outbuf->tail) < (totlen + 20)) {
+       SILC_LOG_DEBUG(("Reallocating outgoing data buffer"));
+       sock->outbuf = silc_buffer_realloc(sock->outbuf, 
+                                          sock->outbuf->truelen + 
+                                          (totlen * 2));
+      }
+
       silc_buffer_pull_tail(sock->outbuf, totlen);
       silc_buffer_pull(sock->outbuf, header_len + padlen);
     }
@@ -302,56 +313,38 @@ void silc_packet_send_prepare(SilcSocketConnection sock,
    This returns amount of bytes read or -1 on error or -2 on case where
    all of the data could not be read at once. */
 
-int silc_packet_read(int sock, SilcBuffer dest)
+int silc_packet_read(int fd, SilcSocketConnection sock)
 {
   int len = 0;
   unsigned char buf[SILC_PACKET_READ_SIZE];
 
-  SILC_LOG_DEBUG(("Reading data from socket %d", sock));
+  SILC_LOG_DEBUG(("Reading data from socket %d", fd));
 
   /* Read the data from the socket. */
-  len = read(sock, buf, sizeof(buf));
+  len = read(fd, buf, sizeof(buf));
   if (len < 0) {
     if (errno == EAGAIN || errno == EINTR) {
       SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
       return -2;
     }
-    SILC_LOG_ERROR(("Cannot read from socket: %d:%s", sock, strerror(errno)));
+    SILC_LOG_ERROR(("Cannot read from socket: %d:%s", fd, strerror(errno)));
     return -1;
   }
 
   if (!len)
     return 0;
 
-  /* Insert the data to the buffer. If the data doesn't fit to the 
-     buffer space is allocated for the buffer. */
-  /* XXX: This may actually be bad thing as if there is pending data in
-     the buffer they will be lost! */
-  if (dest) {
-
-    /* If the data doesn't fit we just have to allocate a whole new 
-       data area */
-    if (dest->truelen <= len) {
-
-      /* Free the old buffer */
-      memset(dest->head, 'F', dest->truelen);
-      silc_free(dest->head);
-
-      /* Allocate new data area */
-      len += SILC_PACKET_DEFAULT_SIZE;
-      dest->data = silc_calloc(len, sizeof(char));
-      dest->truelen = len;
-      dest->len = 0;
-      dest->head = dest->data;
-      dest->data = dest->data;
-      dest->tail = dest->data;
-      dest->end = dest->data + dest->truelen;
-      len -= SILC_PACKET_DEFAULT_SIZE;
-    }
+  /* Insert the data to the buffer. */
 
-    silc_buffer_put_tail(dest, buf, len);
-    silc_buffer_pull_tail(dest, len);
-  }
+  if (!sock->inbuf)
+    sock->inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE);
+  
+  /* If the data does not fit to the buffer reallocate it */
+  if ((sock->inbuf->end - sock->inbuf->tail) < len)
+    sock->inbuf = silc_buffer_realloc(sock->inbuf, sock->inbuf->truelen + 
+                                     (len * 2));
+  silc_buffer_put_tail(sock->inbuf, buf, len);
+  silc_buffer_pull_tail(sock->inbuf, len);
 
   SILC_LOG_DEBUG(("Read %d bytes", len));
 
@@ -390,6 +383,7 @@ 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;
     }
 
@@ -402,8 +396,6 @@ void silc_packet_receive_process(SilcSocketConnection sock,
     parse_ctx->packet = silc_packet_context_alloc();
     parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
     parse_ctx->sock = sock;
-    parse_ctx->cipher = cipher;
-    parse_ctx->hmac = hmac;
     parse_ctx->context = context;
 
     silc_buffer_pull_tail(parse_ctx->packet->buffer, 
@@ -427,6 +419,7 @@ void silc_packet_receive_process(SilcSocketConnection sock,
       silc_buffer_pull(sock->inbuf, mac_len);
   }
 
+  SILC_LOG_DEBUG(("Clearing inbound buffer"));
   silc_buffer_clear(sock->inbuf);
 }
 
@@ -448,12 +441,8 @@ int silc_packet_receive(SilcSocketConnection sock)
                   sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
                   "Router")));
 
-  /* Allocate the incoming data buffer if not done already. */
-  if (!sock->inbuf)
-    sock->inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE);
-
   /* Read some data from connection */
-  ret = silc_packet_read(sock->sock, sock->inbuf);
+  ret = silc_packet_read(sock->sock, sock);
 
   return ret;
 }
@@ -466,7 +455,7 @@ static int silc_packet_check_mac(SilcHmac hmac, SilcBuffer buffer)
   /* Check MAC */
   if (hmac) {
     unsigned char mac[32];
-    unsigned int mac_len;
+    uint32 mac_len;
     
     SILC_LOG_DEBUG(("Verifying MAC"));
 
@@ -476,7 +465,7 @@ static int silc_packet_check_mac(SilcHmac hmac, SilcBuffer buffer)
 
     /* Compare the HMAC's (buffer->tail has the packet's HMAC) */
     if (memcmp(mac, buffer->tail, mac_len)) {
-      SILC_LOG_DEBUG(("MAC failed"));
+      SILC_LOG_ERROR(("MAC failed"));
       return FALSE;
     }
     
@@ -535,7 +524,7 @@ static int silc_packet_decrypt_rest_special(SilcCipher cipher,
 {
   /* Decrypt rest of the header plus padding */
   if (cipher) {
-    unsigned short truelen, len1, len2, padlen;
+    uint16 truelen, len1, len2, padlen;
 
     /* Pull MAC from packet before decryption */
     if (hmac) {
@@ -605,7 +594,7 @@ int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
 
     /* Check MAC */
     if (!silc_packet_check_mac(hmac, buffer))
-      return FALSE;
+      return -1;
 
     return 0;
   } else {
@@ -616,7 +605,7 @@ int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
 
     /* Check MAC */
     if (!silc_packet_check_mac(hmac, buffer))
-      return FALSE;
+      return -1;
 
     return 1;
   }