silccore: packet injection and stream wrap improvements
[silc.git] / lib / silccore / silcpacket.c
index df3e8be7e38a88fbe8faccb7328217381b617c8f..e24832e1ba676e4158839e42a910b74508277ece 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2007 Pekka Riikonen
+  Copyright (C) 1997 - 2014 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -40,7 +40,7 @@ struct SilcPacketEngineStruct {
   SilcMutex lock;                       /* Engine lock */
   SilcRng rng;                          /* RNG for engine */
   SilcHashTable contexts;               /* Per scheduler contexts */
-  SilcPacketCallbacks *callbacks;       /* Packet callbacks */
+  const SilcPacketCallbacks *callbacks;         /* Packet callbacks */
   void *callback_context;               /* Context for callbacks */
   SilcList streams;                     /* All streams in engine */
   SilcList packet_pool;                 /* Free list for received packets */
@@ -51,7 +51,7 @@ struct SilcPacketEngineStruct {
 /* Packet processor context */
 typedef struct SilcPacketProcessStruct {
   SilcPacketType *types;                /* Packets to process */
-  SilcPacketCallbacks *callbacks;       /* Callbacks or NULL */
+  const SilcPacketCallbacks *callbacks;         /* Callbacks or NULL */
   void *callback_context;
   SilcInt32 priority;                   /* Priority */
 } *SilcPacketProcess;
@@ -81,7 +81,7 @@ struct SilcPacketStreamStruct {
   unsigned char *dst_id;                /* Destination ID */
   SilcUInt32 send_psn;                  /* Sending sequence */
   SilcUInt32 receive_psn;               /* Receiving sequence */
-  SilcAtomic8 refcnt;                   /* Reference counter */
+  SilcAtomic32 refcnt;                  /* Reference counter */
   SilcUInt8 sid;                        /* Security ID, set if IV included */
   unsigned int src_id_len  : 6;
   unsigned int src_id_type : 2;
@@ -162,7 +162,8 @@ do {                                                                        \
                                    (s)->stream_context);               \
 } while(0)
 
-static SilcBool silc_packet_dispatch(SilcPacket packet);
+static SilcBool silc_packet_dispatch(SilcPacket packet,
+                                    SilcPacketReceiveCb ignore_handler);
 static void silc_packet_read_process(SilcPacketStream stream);
 static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
                                            SilcPacketType type,
@@ -191,7 +192,7 @@ SILC_TASK_CALLBACK(silc_packet_stream_inject_packet)
 
   silc_mutex_lock(stream->lock);
   if (!stream->destroyed)
-    silc_packet_dispatch(packet);
+    silc_packet_dispatch(packet, NULL);
   silc_mutex_unlock(stream->lock);
   silc_packet_stream_unref(stream);
 }
@@ -306,7 +307,7 @@ static inline SilcBool silc_packet_stream_read(SilcPacketStream ps,
     inbuf = silc_dlist_get(ps->sc->inbufs);
     if (!inbuf) {
       /* Allocate new data input buffer */
-      inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 31);
+      inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 65);
       if (!inbuf) {
         silc_mutex_unlock(ps->lock);
         return FALSE;
@@ -336,7 +337,6 @@ static inline SilcBool silc_packet_stream_read(SilcPacketStream ps,
        silc_mutex_unlock(ps->lock);
        if (ret == -1) {
          /* Cannot read now, do it later. */
-         silc_buffer_pull(inbuf, silc_buffer_len(inbuf));
          return FALSE;
        }
 
@@ -394,7 +394,6 @@ static inline SilcBool silc_packet_stream_read(SilcPacketStream ps,
 
     if (ret == -1) {
       /* Cannot read now, do it later. */
-      silc_buffer_pull(inbuf, silc_buffer_len(inbuf));
       return FALSE;
     }
 
@@ -542,7 +541,7 @@ static void silc_packet_engine_context_destr(void *key, void *context,
 
 SilcPacketEngine
 silc_packet_engine_start(SilcRng rng, SilcBool router,
-                        SilcPacketCallbacks *callbacks,
+                        const SilcPacketCallbacks *callbacks,
                         void *callback_context)
 {
   SilcPacketEngine engine;
@@ -623,6 +622,60 @@ void silc_packet_engine_stop(SilcPacketEngine engine)
   silc_free(engine);
 }
 
+static const char * const packet_error[] = {
+  "Cannot read from stream",
+  "Cannot write to stream",
+  "Packet MAC failed",
+  "Packet decryption failed",
+  "Unknown SID",
+  "Packet is malformed",
+  "System out of memory",
+};
+
+/* Return packet error string */
+
+const char *silc_packet_error_string(SilcPacketError error)
+{
+  if (error < SILC_PACKET_ERR_READ || error > SILC_PACKET_ERR_NO_MEMORY)
+    return "<invalid error code>";
+  return packet_error[error];
+}
+
+/* Return list of packet streams in the engine */
+
+SilcDList silc_packet_engine_get_streams(SilcPacketEngine engine)
+{
+  SilcDList list;
+  SilcPacketStream ps;
+
+  list = silc_dlist_init();
+  if (!list)
+    return NULL;
+
+  silc_mutex_lock(engine->lock);
+  silc_list_start(engine->streams);
+  while ((ps = silc_list_get(engine->streams))) {
+    silc_packet_stream_ref(ps);
+    silc_dlist_add(list, ps);
+  }
+  silc_mutex_unlock(engine->lock);
+
+  return list;
+}
+
+/* Free list returned by silc_packet_engine_get_streams */
+
+void silc_packet_engine_free_streams_list(SilcDList streams)
+{
+  SilcPacketStream ps;
+
+  silc_dlist_start(streams);
+  while ((ps = silc_dlist_get(streams)))
+    silc_packet_stream_unref(ps);
+
+  silc_dlist_uninit(streams);
+}
+
 /* Create new packet stream */
 
 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
@@ -643,7 +696,7 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
     return NULL;
 
   ps->stream = stream;
-  silc_atomic_init8(&ps->refcnt, 1);
+  silc_atomic_init32(&ps->refcnt, 1);
   silc_mutex_alloc(&ps->lock);
 
   /* Allocate out buffer */
@@ -658,6 +711,7 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
   /* Initialize packet procesors list */
   ps->process = silc_dlist_init();
   if (!ps->process) {
+    ps->stream = NULL;
     silc_packet_stream_destroy(ps);
     return NULL;
   }
@@ -669,20 +723,22 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
                            (void *)&ps->sc)) {
     ps->sc = silc_calloc(1, sizeof(*ps->sc));
     if (!ps->sc) {
-      silc_packet_stream_destroy(ps);
       silc_mutex_unlock(engine->lock);
+      ps->stream = NULL;
+      silc_packet_stream_destroy(ps);
       return NULL;
     }
     ps->sc->engine = engine;
     ps->sc->schedule = schedule;
 
     /* Allocate data input buffer */
-    inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 31);
+    inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 65);
     if (!inbuf) {
       silc_free(ps->sc);
       ps->sc = NULL;
-      silc_packet_stream_destroy(ps);
       silc_mutex_unlock(engine->lock);
+      ps->stream = NULL;
+      silc_packet_stream_destroy(ps);
       return NULL;
     }
     silc_buffer_reset(inbuf);
@@ -692,8 +748,9 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
       silc_buffer_free(inbuf);
       silc_free(ps->sc);
       ps->sc = NULL;
-      silc_packet_stream_destroy(ps);
       silc_mutex_unlock(engine->lock);
+      ps->stream = NULL;
+      silc_packet_stream_destroy(ps);
       return NULL;
     }
     silc_dlist_add(ps->sc->inbufs, inbuf);
@@ -704,8 +761,9 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
       silc_dlist_del(ps->sc->inbufs, inbuf);
       silc_free(ps->sc);
       ps->sc = NULL;
-      silc_packet_stream_destroy(ps);
       silc_mutex_unlock(engine->lock);
+      ps->stream = NULL;
+      silc_packet_stream_destroy(ps);
       return NULL;
     }
   }
@@ -727,10 +785,13 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
   if (!silc_stream_set_notifier(ps->stream, schedule,
                                silc_packet_stream_io, ps)) {
     SILC_LOG_DEBUG(("Cannot set stream notifier for packet stream"));
+    ps->stream = NULL;
     silc_packet_stream_destroy(ps);
     return NULL;
   }
 
+  SILC_LOG_DEBUG(("Created packet stream %p", ps));
+
   return ps;
 }
 
@@ -762,7 +823,7 @@ SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
     return NULL;
   ps->sc = stream->sc;
 
-  silc_atomic_init8(&ps->refcnt, 1);
+  silc_atomic_init32(&ps->refcnt, 1);
   silc_mutex_alloc(&ps->lock);
 
   /* Set the UDP packet stream as underlaying stream */
@@ -811,16 +872,25 @@ SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
 
   if (packet) {
     /* Inject packet to the new stream */
-    packet->stream = ps;
-    silc_packet_stream_ref(ps);
-    silc_schedule_task_add_timeout(silc_stream_get_schedule(stream->stream),
-                                  silc_packet_stream_inject_packet, packet,
-                                  0, 0);
+    silc_packet_stream_inject(ps, packet);
   }
 
   return ps;
 }
 
+/* Inject packet to packet stream */
+
+SilcBool silc_packet_stream_inject(SilcPacketStream stream,
+                                  SilcPacket packet)
+{
+  packet->stream = stream;
+  silc_packet_stream_ref(stream);
+  return !!silc_schedule_task_add_timeout(
+                               silc_stream_get_schedule(stream->stream),
+                               silc_packet_stream_inject_packet, packet,
+                               0, 0);
+}
+
 /* Destroy packet stream */
 
 void silc_packet_stream_destroy(SilcPacketStream stream)
@@ -830,9 +900,13 @@ void silc_packet_stream_destroy(SilcPacketStream stream)
   if (!stream)
     return;
 
-  if (silc_atomic_sub_int8(&stream->refcnt, 1) > 0) {
+  if (silc_atomic_sub_int32(&stream->refcnt, 1) > 0) {
+    if (stream->destroyed)
+      return;
     stream->destroyed = TRUE;
 
+    SILC_LOG_DEBUG(("Marking packet stream %p destroyed", stream));
+
     /* Close the underlaying stream */
     if (!stream->udp && stream->stream)
       silc_stream_close(stream->stream);
@@ -843,17 +917,18 @@ void silc_packet_stream_destroy(SilcPacketStream stream)
 
   if (!stream->udp) {
     /* Delete from engine */
-    engine = stream->sc->engine;
-    silc_mutex_lock(engine->lock);
-    silc_list_del(engine->streams, stream);
-
-    /* Remove per scheduler context, if it is not used anymore */
     if (stream->sc) {
+      engine = stream->sc->engine;
+      silc_mutex_lock(engine->lock);
+      silc_list_del(engine->streams, stream);
+
+      /* Remove per scheduler context, if it is not used anymore */
       stream->sc->stream_count--;
       if (!stream->sc->stream_count)
        silc_hash_table_del(engine->contexts, stream->sc->schedule);
+
+      silc_mutex_unlock(engine->lock);
     }
-    silc_mutex_unlock(engine->lock);
 
     /* Destroy the underlaying stream */
     if (stream->stream)
@@ -913,11 +988,18 @@ void silc_packet_stream_destroy(SilcPacketStream stream)
   silc_free(stream->src_id);
   silc_free(stream->dst_id);
 
-  silc_atomic_uninit8(&stream->refcnt);
+  silc_atomic_uninit32(&stream->refcnt);
   silc_mutex_free(stream->lock);
   silc_free(stream);
 }
 
+/* Return TRUE if the stream is valid */
+
+SilcBool silc_packet_stream_is_valid(SilcPacketStream stream)
+{
+  return stream->destroyed == FALSE;
+}
+
 /* Marks as router stream */
 
 void silc_packet_stream_set_router(SilcPacketStream stream)
@@ -935,7 +1017,7 @@ void silc_packet_stream_set_iv_included(SilcPacketStream stream)
 /* Links `callbacks' to `stream' for specified packet types */
 
 static SilcBool silc_packet_stream_link_va(SilcPacketStream stream,
-                                          SilcPacketCallbacks *callbacks,
+                                          const SilcPacketCallbacks *callbacks,
                                           void *callback_context,
                                           int priority, va_list ap)
 {
@@ -964,6 +1046,7 @@ static SilcBool silc_packet_stream_link_va(SilcPacketStream stream,
     stream->process = silc_dlist_init();
     if (!stream->process) {
       silc_mutex_unlock(stream->lock);
+      silc_free(p);
       return FALSE;
     }
   }
@@ -1013,7 +1096,7 @@ static SilcBool silc_packet_stream_link_va(SilcPacketStream stream,
 /* Links `callbacks' to `stream' for specified packet types */
 
 SilcBool silc_packet_stream_link(SilcPacketStream stream,
-                                SilcPacketCallbacks *callbacks,
+                                const SilcPacketCallbacks *callbacks,
                                 void *callback_context,
                                 int priority, ...)
 {
@@ -1031,7 +1114,7 @@ SilcBool silc_packet_stream_link(SilcPacketStream stream,
 /* Unlinks `callbacks' from `stream'. */
 
 void silc_packet_stream_unlink(SilcPacketStream stream,
-                              SilcPacketCallbacks *callbacks,
+                              const SilcPacketCallbacks *callbacks,
                               void *callback_context)
 {
   SilcPacketProcess p;
@@ -1087,10 +1170,10 @@ SilcBool silc_packet_get_sender(SilcPacket packet,
 
 void silc_packet_stream_ref(SilcPacketStream stream)
 {
-  silc_atomic_add_int8(&stream->refcnt, 1);
+  silc_atomic_add_int32(&stream->refcnt, 1);
   SILC_LOG_DEBUG(("Stream %p, refcnt %d->%d", stream,
-                 silc_atomic_get_int8(&stream->refcnt) - 1,
-                 silc_atomic_get_int8(&stream->refcnt)));
+                 silc_atomic_get_int32(&stream->refcnt) - 1,
+                 silc_atomic_get_int32(&stream->refcnt)));
 }
 
 /* Unreference packet stream */
@@ -1098,11 +1181,11 @@ void silc_packet_stream_ref(SilcPacketStream stream)
 void silc_packet_stream_unref(SilcPacketStream stream)
 {
   SILC_LOG_DEBUG(("Stream %p, refcnt %d->%d", stream,
-                 silc_atomic_get_int8(&stream->refcnt),
-                 silc_atomic_get_int8(&stream->refcnt) - 1));
-  if (silc_atomic_sub_int8(&stream->refcnt, 1) > 0)
+                 silc_atomic_get_int32(&stream->refcnt),
+                 silc_atomic_get_int32(&stream->refcnt) - 1));
+  if (silc_atomic_sub_int32(&stream->refcnt, 1) > 0)
     return;
-  silc_atomic_add_int8(&stream->refcnt, 1);
+  silc_atomic_add_int32(&stream->refcnt, 1);
   silc_packet_stream_destroy(stream);
 }
 
@@ -1257,40 +1340,45 @@ SilcBool silc_packet_set_ids(SilcPacketStream stream,
 {
   SilcUInt32 len;
   unsigned char tmp[32];
+  void *tmp_id;
 
   if (!src_id && !dst_id)
     return FALSE;
 
-  SILC_LOG_DEBUG(("Setting new IDs to packet stream"));
-
   silc_mutex_lock(stream->lock);
 
   if (src_id) {
-    silc_free(stream->src_id);
+    SILC_LOG_DEBUG(("Setting source ID to packet stream %p", stream));
+
     if (!silc_id_id2str(src_id, src_id_type, tmp, sizeof(tmp), &len)) {
       silc_mutex_unlock(stream->lock);
       return FALSE;
     }
-    stream->src_id = silc_memdup(tmp, len);
-    if (!stream->src_id) {
+    tmp_id = silc_memdup(tmp, len);
+    if (!tmp_id) {
       silc_mutex_unlock(stream->lock);
       return FALSE;
     }
+    silc_free(stream->src_id);
+    stream->src_id = tmp_id;
     stream->src_id_type = src_id_type;
     stream->src_id_len = len;
   }
 
   if (dst_id) {
-    silc_free(stream->dst_id);
+    SILC_LOG_DEBUG(("Setting destination ID to packet stream %p", stream));
+
     if (!silc_id_id2str(dst_id, dst_id_type, tmp, sizeof(tmp), &len)) {
       silc_mutex_unlock(stream->lock);
       return FALSE;
     }
-    stream->dst_id = silc_memdup(tmp, len);
-    if (!stream->dst_id) {
+    tmp_id = silc_memdup(tmp, len);
+    if (!tmp_id) {
       silc_mutex_unlock(stream->lock);
       return FALSE;
     }
+    silc_free(stream->dst_id);
+    stream->dst_id = tmp_id;
     stream->dst_id_type = dst_id_type;
     stream->dst_id_len = len;
   }
@@ -1300,6 +1388,31 @@ SilcBool silc_packet_set_ids(SilcPacketStream stream,
   return TRUE;
 }
 
+/* Return IDs from the packet stream */
+
+SilcBool silc_packet_get_ids(SilcPacketStream stream,
+                            SilcBool *src_id_set, SilcID *src_id,
+                            SilcBool *dst_id_set, SilcID *dst_id)
+{
+  if (src_id && stream->src_id)
+    if (!silc_id_str2id2(stream->src_id, stream->src_id_len,
+                        stream->src_id_type, src_id))
+      return FALSE;
+
+  if (stream->src_id && src_id_set)
+    *src_id_set = TRUE;
+
+  if (dst_id && stream->dst_id)
+    if (!silc_id_str2id2(stream->dst_id, stream->dst_id_len,
+                        stream->dst_id_type, dst_id))
+      return FALSE;
+
+  if (stream->dst_id && dst_id_set)
+    *dst_id_set = TRUE;
+
+  return TRUE;
+}
+
 /* Adds Security ID (SID) */
 
 SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid)
@@ -1379,14 +1492,6 @@ static inline void silc_packet_send_ctr_increment(SilcPacketStream stream,
   unsigned char *iv = silc_cipher_get_iv(cipher);
   SilcUInt32 pc1, pc2;
 
-  /* Increment 64-bit packet counter */
-  SILC_GET32_MSB(pc1, iv + 4);
-  SILC_GET32_MSB(pc2, iv + 8);
-  if (++pc2 == 0)
-    ++pc1;
-  SILC_PUT32_MSB(pc1, iv + 4);
-  SILC_PUT32_MSB(pc2, iv + 8);
-
   /* Reset block counter */
   memset(iv + 12, 0, 4);
 
@@ -1397,11 +1502,24 @@ static inline void silc_packet_send_ctr_increment(SilcPacketStream stream,
     ret_iv[1] = ret_iv[0] + iv[4];
     ret_iv[2] = ret_iv[0] ^ ret_iv[1];
     ret_iv[3] = ret_iv[0] + ret_iv[2];
-    SILC_PUT32_MSB(pc2, ret_iv + 4);
+
+    /* Increment 32-bit packet counter */
+    SILC_GET32_MSB(pc1, iv + 8);
+    pc1++;
+    SILC_PUT32_MSB(pc1, ret_iv + 4);
+
     SILC_LOG_HEXDUMP(("IV"), ret_iv, 8);
 
     /* Set new nonce to counter block */
-    memcpy(iv + 4, ret_iv, 4);
+    memcpy(iv + 4, ret_iv, 8);
+  } else {
+    /* Increment 64-bit packet counter */
+    SILC_GET32_MSB(pc1, iv + 4);
+    SILC_GET32_MSB(pc2, iv + 8);
+    if (++pc2 == 0)
+      ++pc1;
+    SILC_PUT32_MSB(pc1, iv + 4);
+    SILC_PUT32_MSB(pc2, iv + 8);
   }
 
   SILC_LOG_HEXDUMP(("Counter Block"), iv, 16);
@@ -1468,10 +1586,8 @@ static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
      type and flags, and calculate correct length.  Private messages with
      private keys and channel messages are special packets as their
      payload is encrypted already. */
-  if ((type == SILC_PACKET_PRIVATE_MESSAGE &&
-       flags & SILC_PACKET_FLAG_PRIVMSG_KEY) ||
-      type == SILC_PACKET_CHANNEL_MESSAGE) {
-
+  if (type == SILC_PACKET_PRIVATE_MESSAGE &&
+      flags & SILC_PACKET_FLAG_PRIVMSG_KEY) {
     /* Padding is calculated from header + IDs */
     if (!ctr)
       SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN + src_id_len + dst_id_len +
@@ -1480,8 +1596,26 @@ static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
     /* Length to encrypt, header + IDs + padding. */
     enclen = (SILC_PACKET_HEADER_LEN + src_id_len + dst_id_len +
              padlen + psnlen);
-  } else {
 
+  } else if (type == SILC_PACKET_CHANNEL_MESSAGE) {
+    if (stream->sc->engine->local_is_router && stream->is_router) {
+      /* Channel messages between routers are encrypted as normal packets.
+        Padding is calculated from true length of the packet. */
+      if (!ctr)
+       SILC_PACKET_PADLEN(truelen + psnlen, block_len, padlen);
+
+      enclen += padlen + psnlen;
+    } else {
+      /* Padding is calculated from header + IDs */
+      if (!ctr)
+       SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN + src_id_len + dst_id_len +
+                           psnlen), block_len, padlen);
+
+      /* Length to encrypt, header + IDs + padding. */
+      enclen = (SILC_PACKET_HEADER_LEN + src_id_len + dst_id_len +
+               padlen + psnlen);
+    }
+  } else {
     /* Padding is calculated from true length of the packet */
     if (flags & SILC_PACKET_FLAG_LONG_PAD)
       SILC_PACKET_PADLEN_MAX(truelen + psnlen, block_len, padlen);
@@ -1503,6 +1637,7 @@ static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
   /* Get packet pointer from the outgoing buffer */
   if (silc_unlikely(!silc_packet_send_prepare(stream, truelen + padlen + ivlen
                                              + psnlen, hmac, &packet))) {
+    SILC_LOG_ERROR(("Error preparing for packet sending"));
     silc_mutex_unlock(stream->lock);
     return FALSE;
   }
@@ -1529,6 +1664,7 @@ static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
                         SILC_STR_DATA(data, data_len),
                         SILC_STR_END);
   if (silc_unlikely(i < 0)) {
+    SILC_LOG_ERROR(("Error encoding outgoing packet"));
     silc_mutex_unlock(stream->lock);
     return FALSE;
   }
@@ -1679,7 +1815,7 @@ SilcBool silc_packet_send_va_ext(SilcPacketStream stream,
   silc_buffer_purge(&buf);
   va_end(va);
 
-  return TRUE;
+  return ret;
 }
 
 /***************************** Packet Receiving *****************************/
@@ -1751,6 +1887,14 @@ static inline void silc_packet_receive_ctr_increment(SilcPacketStream stream,
   SILC_LOG_HEXDUMP(("Counter Block"), iv, 16);
 }
 
+/* Return special packet's encrypted length */
+
+static inline int silc_packet_special_len(unsigned char *data)
+{
+  return (((SilcUInt8)data[4] + (SilcUInt8)data[6] +
+          (SilcUInt8)data[7] + SILC_PACKET_HEADER_LEN));
+}
+
 /* Decrypts SILC packet.  Handles both normal and special packet decryption.
    Return 0 when packet is normal and 1 when it it special, -1 on error. */
 
@@ -1780,9 +1924,7 @@ static inline int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
       /* Padding length + src id len + dst id len + header length - 16
         bytes already decrypted, gives the rest of the encrypted packet */
       silc_buffer_push(buffer, block_len);
-      len = (((SilcUInt8)buffer->data[4] + (SilcUInt8)buffer->data[6] +
-             (SilcUInt8)buffer->data[7] + SILC_PACKET_HEADER_LEN) -
-            block_len);
+      len = silc_packet_special_len(buffer->data) - block_len;
       silc_buffer_pull(buffer, block_len);
 
       if (silc_unlikely(len > silc_buffer_len(buffer))) {
@@ -1868,8 +2010,8 @@ static inline SilcBool silc_packet_parse(SilcPacket packet)
                   silc_buffer_len(buffer)), buffer->head,
                   silc_buffer_headlen(buffer) + silc_buffer_len(buffer));
 
-  SILC_LOG_DEBUG(("Incoming packet type: %d (%s)", packet->type,
-                 silc_get_packet_name(packet->type)));
+  SILC_LOG_DEBUG(("Incoming packet type: %d (%s), flags %d", packet->type,
+                 silc_get_packet_name(packet->type), packet->flags));
 
   return TRUE;
 }
@@ -1877,7 +2019,8 @@ static inline SilcBool silc_packet_parse(SilcPacket packet)
 /* Dispatch packet to application.  Called with stream->lock locked.
    Returns FALSE if the stream was destroyed while dispatching a packet. */
 
-static SilcBool silc_packet_dispatch(SilcPacket packet)
+static SilcBool silc_packet_dispatch(SilcPacket packet,
+                                    SilcPacketReceiveCb ignore_handler)
 {
   SilcPacketStream stream = packet->stream;
   SilcPacketProcess p;
@@ -1933,7 +2076,8 @@ static SilcBool silc_packet_dispatch(SilcPacket packet)
     } else {
       /* Send specific types */
       for (pt = p->types; *pt; pt++) {
-       if (*pt != packet->type)
+       if (*pt != packet->type || 
+           ignore_handler == p->callbacks->packet_receive)
          continue;
        SILC_LOG_DEBUG(("Dispatching packet to %p callbacks", p->callbacks));
        silc_mutex_unlock(stream->lock);
@@ -1977,7 +2121,7 @@ static void silc_packet_read_process(SilcPacketStream stream)
   SilcCipher cipher;
   SilcHmac hmac;
   SilcPacket packet;
-  SilcUInt8 sid;
+  SilcUInt8 sid, flags, type;
   SilcUInt16 packetlen;
   SilcUInt32 paddedlen, mac_len, block_len, ivlen, psnlen;
   unsigned char tmp[SILC_PACKET_MIN_HEADER_LEN], *header;
@@ -2079,10 +2223,33 @@ static void silc_packet_read_process(SilcPacketStream stream)
     /* Get packet length and full packet length with padding */
     SILC_PACKET_LENGTH(header, packetlen, paddedlen);
 
-    /* Sanity checks */
-    if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
-      if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
-       SILC_LOG_ERROR(("Received too short packet"));
+    /* Parse packet header */
+    flags = (SilcPacketFlags)header[2];
+    type = (SilcPacketType)header[3];
+
+    if (stream->sc->engine->local_is_router) {
+      if (type == SILC_PACKET_PRIVATE_MESSAGE &&
+         (flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
+       normal = FALSE;
+      else if (type != SILC_PACKET_CHANNEL_MESSAGE ||
+              (type == SILC_PACKET_CHANNEL_MESSAGE &&
+               stream->is_router == TRUE))
+       normal = TRUE;
+    } else {
+      if (type == SILC_PACKET_PRIVATE_MESSAGE &&
+         (flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
+       normal = FALSE;
+      else if (type != SILC_PACKET_CHANNEL_MESSAGE)
+       normal = TRUE;
+    }
+
+    /* Padding sanity checks */
+    if (cipher && silc_cipher_get_mode(cipher) != SILC_CIPHER_MODE_CTR &&
+       ((normal && block_len && paddedlen % block_len != 0) ||
+        (!normal && block_len &&
+          silc_packet_special_len(header) % block_len != 0))) {
+      SILC_LOG_DEBUG(("Packet length %d not multiple by cipher block length",
+                     paddedlen));
       silc_mutex_unlock(stream->lock);
       SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
       silc_mutex_lock(stream->lock);
@@ -2091,8 +2258,10 @@ static void silc_packet_read_process(SilcPacketStream stream)
     }
 
     if (silc_buffer_len(inbuf) < paddedlen + ivlen + mac_len) {
-      SILC_LOG_DEBUG(("Received partial packet, waiting for the rest "
-                     "(%d bytes)",
+      SILC_LOG_DEBUG(("Received partial packet (%d %s flags:%x normal:%d "
+                     "len:%u paddedlen:%u), waiting for the rest (%d bytes)",
+                     type, silc_get_packet_name(type), flags,
+                     normal, packetlen, paddedlen,
                      paddedlen + mac_len - silc_buffer_len(inbuf)));
       memset(tmp, 0, sizeof(tmp));
       silc_dlist_del(stream->sc->inbufs, inbuf);
@@ -2113,6 +2282,17 @@ static void silc_packet_read_process(SilcPacketStream stream)
       goto out;
     }
 
+    /* Sanity checks */
+    if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
+      if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
+       SILC_LOG_ERROR(("Received too short packet"));
+      silc_mutex_unlock(stream->lock);
+      SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
+      silc_mutex_lock(stream->lock);
+      memset(tmp, 0, sizeof(tmp));
+      goto out;
+    }
+
     /* Get packet */
     packet = silc_packet_alloc(stream->sc->engine);
     if (silc_unlikely(!packet)) {
@@ -2123,6 +2303,8 @@ static void silc_packet_read_process(SilcPacketStream stream)
       goto out;
     }
     packet->stream = stream;
+    packet->flags = flags;
+    packet->type = type;
 
     /* Allocate more space to packet buffer, if needed */
     if (silc_unlikely(silc_buffer_truelen(&packet->buffer) < paddedlen)) {
@@ -2139,26 +2321,6 @@ static void silc_packet_read_process(SilcPacketStream stream)
       }
     }
 
-    /* Parse packet header */
-    packet->flags = (SilcPacketFlags)header[2];
-    packet->type = (SilcPacketType)header[3];
-
-    if (stream->sc->engine->local_is_router) {
-      if (packet->type == SILC_PACKET_PRIVATE_MESSAGE &&
-         (packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
-       normal = FALSE;
-      else if (packet->type != SILC_PACKET_CHANNEL_MESSAGE ||
-              (packet->type == SILC_PACKET_CHANNEL_MESSAGE &&
-               stream->is_router == TRUE))
-       normal = TRUE;
-    } else {
-      if (packet->type == SILC_PACKET_PRIVATE_MESSAGE &&
-         (packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
-       normal = FALSE;
-      else if (packet->type != SILC_PACKET_CHANNEL_MESSAGE)
-       normal = TRUE;
-    }
-
     SILC_LOG_HEXDUMP(("Incoming packet (%d) len %d",
                      stream->receive_psn, paddedlen + ivlen + mac_len),
                     inbuf->data, paddedlen + ivlen + mac_len);
@@ -2201,7 +2363,7 @@ static void silc_packet_read_process(SilcPacketStream stream)
     }
 
     /* Dispatch the packet to application */
-    if (!silc_packet_dispatch(packet))
+    if (!silc_packet_dispatch(packet, NULL))
       break;
   }
 
@@ -2226,7 +2388,7 @@ silc_packet_wait_packet_receive(SilcPacketEngine engine,
                                void *stream_context);
 
 /* Packet waiting callbacks */
-static SilcPacketCallbacks silc_packet_wait_cbs =
+static const SilcPacketCallbacks silc_packet_wait_cbs =
 {
   silc_packet_wait_packet_receive, NULL, NULL
 };
@@ -2236,6 +2398,9 @@ typedef struct {
   SilcMutex wait_lock;
   SilcCond wait_cond;
   SilcList packet_queue;
+  unsigned char id[28];
+  unsigned int id_type     : 2;
+  unsigned int id_len      : 5;
   unsigned int stopped     : 1;
 } *SilcPacketWait;
 
@@ -2250,6 +2415,13 @@ silc_packet_wait_packet_receive(SilcPacketEngine engine,
 {
   SilcPacketWait pw = callback_context;
 
+  /* If source ID is specified check for it */
+  if (pw->id_len) {
+    if (pw->id_type != packet->src_id_type ||
+       memcmp(pw->id, packet->src_id, pw->id_len))
+      return FALSE;
+  }
+
   /* Signal the waiting thread for a new packet */
   silc_mutex_lock(pw->wait_lock);
 
@@ -2268,7 +2440,8 @@ silc_packet_wait_packet_receive(SilcPacketEngine engine,
 
 /* Initialize packet waiting */
 
-void *silc_packet_wait_init(SilcPacketStream stream, ...)
+void *silc_packet_wait_init(SilcPacketStream stream,
+                           const SilcID *source_id, ...)
 {
   SilcPacketWait pw;
   SilcBool ret;
@@ -2290,7 +2463,7 @@ void *silc_packet_wait_init(SilcPacketStream stream, ...)
   }
 
   /* Link to the packet stream for the requested packet types */
-  va_start(ap, stream);
+  va_start(ap, source_id);
   ret = silc_packet_stream_link_va(stream, &silc_packet_wait_cbs, pw,
                                   10000000, ap);
   va_end(ap);
@@ -2304,6 +2477,14 @@ void *silc_packet_wait_init(SilcPacketStream stream, ...)
   /* Initialize packet queue */
   silc_list_init(pw->packet_queue, struct SilcPacketStruct, next);
 
+  if (source_id) {
+    SilcUInt32 id_len;
+    silc_id_id2str(SILC_ID_GET_ID(*source_id), source_id->type, pw->id,
+                  sizeof(pw->id), &id_len);
+    pw->id_type = source_id->type;
+    pw->id_len = id_len;
+  }
+
   return (void *)pw;
 }
 
@@ -2319,6 +2500,7 @@ void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream)
   pw->stopped = TRUE;
   silc_cond_broadcast(pw->wait_cond);
   silc_mutex_unlock(pw->wait_lock);
+  silc_thread_yield();
 
   /* Re-acquire lock and free resources */
   silc_mutex_lock(pw->wait_lock);
@@ -2389,13 +2571,17 @@ typedef struct {
   SilcList in_queue;
   SilcPacketType type;
   SilcPacketFlags flags;
+  void *src_id;
+  void *dst_id;
+  SilcIdType src_id_type;
+  SilcIdType dst_id_type;
   unsigned int closed        : 1;
   unsigned int blocking      : 1;
   unsigned int read_more     : 1;
 } *SilcPacketWrapperStream;
 
 /* Packet wrapper callbacks */
-static SilcPacketCallbacks silc_packet_wrap_cbs =
+static const SilcPacketCallbacks silc_packet_wrap_cbs =
 {
   silc_packet_wrap_packet_receive, NULL, NULL
 };
@@ -2410,10 +2596,24 @@ silc_packet_wrap_packet_receive(SilcPacketEngine engine,
                                void *stream_context)
 {
   SilcPacketWrapperStream pws = callback_context;
+  SilcID id;
 
   if (pws->closed || !pws->callback)
     return FALSE;
 
+  /* If dst_id was set, the incoming packet must use that id as its
+     source id.  This will not work if the id is channel id because
+     the source is never the channel id, but will work with other ids. */
+  if ((pws->dst_id && pws->dst_id_type != SILC_ID_CHANNEL)) {
+    silc_id_str2id2(packet->src_id, packet->src_id_len,
+                   packet->src_id_type, &id);
+    if (!SILC_ID_COMPARE_TYPE(pws->dst_id, SILC_ID_GET_ID(id),
+                             packet->src_id_len)) {
+      SILC_LOG_DEBUG(("Packet is not from wanted sender"));
+      return FALSE;
+    }
+  }
+
   silc_mutex_lock(pws->lock);
   silc_list_add(pws->in_queue, packet);
   silc_mutex_unlock(pws->lock);
@@ -2444,7 +2644,7 @@ int silc_packet_wrap_read(SilcStream stream, unsigned char *buf,
 {
   SilcPacketWrapperStream pws = stream;
   SilcPacket packet;
-  SilcBool read_more = FALSE;
+  SilcBool read_more = FALSE, ret = TRUE;
   int len;
 
   if (pws->closed)
@@ -2472,8 +2672,17 @@ int silc_packet_wrap_read(SilcStream stream, unsigned char *buf,
 
   /* Call decoder if set */
   if (pws->coder && !pws->read_more)
-    pws->coder(stream, SILC_STREAM_CAN_READ, &packet->buffer,
-              pws->coder_context);
+    ret = pws->coder(stream, SILC_STREAM_CAN_READ, &packet->buffer,
+                    pws->coder_context);
+
+  if (!ret) {
+    /* If error occurred during decoding (or handler doesn't want this
+       packet), we'll reprocess this packet and try to give it to some
+       other handler that may want it.  For this stream nothing was
+       received. */
+    silc_packet_dispatch(packet, silc_packet_wrap_packet_receive);
+    return -1;
+  }
 
   len = silc_buffer_len(&packet->buffer);
   if (len > buf_len) {
@@ -2505,29 +2714,39 @@ int silc_packet_wrap_write(SilcStream stream, const unsigned char *data,
                           SilcUInt32 data_len)
 {
   SilcPacketWrapperStream pws = stream;
-  SilcBool ret = FALSE;
+  SilcBool ret = TRUE;
 
-  /* Call decoder if set */
-  if (pws->coder) {
-    silc_buffer_reset(pws->encbuf);
-    ret = pws->coder(stream, SILC_STREAM_CAN_WRITE, pws->encbuf,
-                    pws->coder_context);
+  if (!pws->coder) {
+    if (!silc_packet_send_ext(pws->stream, pws->type, pws->flags,
+                             pws->src_id_type, pws->src_id,
+                             pws->dst_id_type, pws->dst_id,
+                             data, data_len, NULL, NULL))
+      return -2;
+    return data_len;
   }
 
+  silc_buffer_reset(pws->encbuf);
+  if (!silc_buffer_enlarge(pws->encbuf, data_len + 16))
+    return -2;
+  silc_buffer_pull(pws->encbuf, 16);    /* Room for adding headers */
+  silc_buffer_put(pws->encbuf, data, data_len);
+
+  ret = pws->coder(stream, SILC_STREAM_CAN_WRITE, pws->encbuf,
+                  pws->coder_context);
+
   /* Send the SILC packet */
   if (ret) {
-    if (!silc_packet_send_va(pws->stream, pws->type, pws->flags,
-                            SILC_STR_DATA(silc_buffer_data(pws->encbuf),
-                                          silc_buffer_len(pws->encbuf)),
-                            SILC_STR_DATA(data, data_len),
-                            SILC_STR_END))
-      return -2;
-  } else {
-    if (!silc_packet_send(pws->stream, pws->type, pws->flags, data, data_len))
+    if (!silc_packet_send_ext(pws->stream, pws->type, pws->flags,
+                             pws->src_id_type, pws->src_id,
+                             pws->dst_id_type, pws->dst_id,
+                             silc_buffer_datalen(pws->encbuf),
+                             NULL, NULL))
       return -2;
+    return data_len;
   }
 
-  return data_len;
+  /* Error */
+  return -2;
 }
 
 /* Close stream */
@@ -2604,7 +2823,8 @@ SilcBool silc_packet_wrap_notifier(SilcStream stream,
 
 SilcSchedule silc_packet_wrap_get_schedule(SilcStream stream)
 {
-  return NULL;
+  SilcPacketWrapperStream pws = stream;
+  return silc_stream_get_schedule(pws->stream->stream);
 }
 
 /* Wraps packet stream into SilcStream. */
@@ -2613,6 +2833,8 @@ SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
                                    SilcPacketType type,
                                    SilcPacketFlags flags,
                                   SilcBool blocking_mode,
+                                  SilcIdType src_id_type, void *src_id,
+                                  SilcIdType dst_id_type, void *dst_id,
                                   SilcPacketWrapCoder coder,
                                   void *context)
 {
@@ -2632,13 +2854,31 @@ SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
   pws->coder = coder;
   pws->coder_context = context;
 
+  if (src_id) {
+    pws->src_id = silc_id_dup(src_id, src_id_type);
+    if (!pws->src_id) {
+      silc_free(pws);
+      return NULL;
+    }
+    pws->src_id_type = src_id_type;
+  }
+
+  if (dst_id) {
+    pws->dst_id = silc_id_dup(dst_id, dst_id_type);
+    if (!pws->dst_id) {
+      silc_free(pws);
+      return NULL;
+    }
+    pws->dst_id_type = dst_id_type;
+  }
+
   /* Allocate small amount for encoder buffer. */
   if (pws->coder)
     pws->encbuf = silc_buffer_alloc(8);
 
   if (pws->blocking) {
     /* Blocking mode.  Use packet waiter to do the thing. */
-    pws->waiter = silc_packet_wait_init(pws->stream, pws->type, -1);
+    pws->waiter = silc_packet_wait_init(pws->stream, NULL, pws->type, -1);
     if (!pws->waiter) {
       silc_free(pws);
       return NULL;