Added source ID checking support to packet waiting API.
authorPekka Riikonen <priikone@silcnet.org>
Tue, 8 May 2007 16:34:04 +0000 (16:34 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Tue, 8 May 2007 16:34:04 +0000 (16:34 +0000)
lib/silccore/silcpacket.c
lib/silccore/silcpacket.h

index ccd821198d9f757f075c265f144911fa3e206973..89e12a476a31a2b588c79a792021eb6c21f3f657 100644 (file)
@@ -306,7 +306,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 * 61);
       if (!inbuf) {
         silc_mutex_unlock(ps->lock);
         return FALSE;
@@ -716,7 +716,7 @@ SilcPacketStream silc_packet_stream_create(SilcPacketEngine 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 * 61);
     if (!inbuf) {
       silc_free(ps->sc);
       ps->sc = NULL;
@@ -2341,6 +2341,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;
 
@@ -2355,6 +2358,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);
 
@@ -2373,7 +2383,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;
@@ -2395,7 +2406,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);
@@ -2409,6 +2420,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;
 }
 
@@ -2744,7 +2763,7 @@ SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
 
   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;
index 47933117af00ca48c3da2001fccedaa900408c51..228a16a16a06b31e55568295d3b45b3f0a9c2600 100644 (file)
@@ -1067,19 +1067,26 @@ SilcBool silc_packet_send_va_ext(SilcPacketStream stream,
  *
  * SYNOPSIS
  *
- *    void *silc_packet_wait_init(SilcPacketStream stream, ...);
+ *    void *silc_packet_wait_init(SilcPacketStream stream,
+ *                                const SilcID *source_id, ...);
  *
  * DESCRIPTION
  *
  *    Initializes a packet waiter for the packet stream `stream' and
  *    for the variable argument list of packet types.  The function
  *    silc_packet_wait can be used to block the thread until a packet
- *    has been received.  This function is used to initialize the waiting
- *    and to give the list of packet types that caller wish to receive.
- *    The variable argument list must end with -1.  To receive all
- *    packets use SILC_PACKET_ANY.  Returns a context that must be given
- *    to the silc_packet_wait function as argument.  Returns NULL on
- *    error.  To uninitialize the waiting call silc_packet_wait_uninit.
+ *    has been received.
+ *
+ *    This function is used to initialize the waiting and to give the list
+ *    of packet types that caller wish to receive.  The variable argument
+ *    list must end with -1.  To receive all packets use SILC_PACKET_ANY.
+ *    If the `source_id' is non-NULL then only packets of the specified
+ *    type from the specified `source_id' are received.  If it is NULL
+ *    then the packet source is ignored.
+ *
+ *    Returns a context that must be given to the silc_packet_wait function
+ *    as argument.  Returns NULL on error.  To uninitialize the waiting
+ *    call silc_packet_wait_uninit.
  *
  * NOTES
  *
@@ -1091,12 +1098,12 @@ SilcBool silc_packet_send_va_ext(SilcPacketStream stream,
  *    void *waiter;
  *
  *    // Will wait for private message packets
- *    waiter = silc_packet_wait_init(stream,
+ *    waiter = silc_packet_wait_init(stream, NULL,
  *                                   SILC_PACKET_PRIVATE_MESSAGE, -1);
  *
- *
  ***/
-void *silc_packet_wait_init(SilcPacketStream stream, ...);
+void *silc_packet_wait_init(SilcPacketStream stream,
+                           const SilcID *source_id, ...);
 
 /****f* silccore/SilcPacketAPI/silc_packet_wait_uninit
  *