remove speed bumps
[oweals/gnunet.git] / src / cadet / cadet_api.c
index decf473a9c11679bf0811eabcfb907e042ac5613..366e31405a8903c43a28a28a8fb8e827c9675a25 100644 (file)
@@ -326,7 +326,7 @@ destroy_channel (struct GNUNET_CADET_Channel *ch)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Destroying channel %X of %p\n",
-       ch->ccn,
+       htonl (ch->ccn.channel_of_client),
        h);
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multihashmap32_remove (h->channels,
@@ -823,6 +823,29 @@ handle_mq_error (void *cls,
 }
 
 
+/**
+ * Check that message received from CADET service is well-formed.
+ *
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
+ */
+static int
+check_get_peers (void *cls,
+                 const struct GNUNET_MessageHeader *message)
+{
+  size_t esize;
+
+  esize = ntohs (message->size);
+  if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize)
+    return GNUNET_OK;
+  if (sizeof (struct GNUNET_MessageHeader) == esize)
+    return GNUNET_OK;
+  return GNUNET_SYSERR;
+}
+
+
 /**
  * Process a local reply about info on all tunnels, pass info to the user.
  *
@@ -831,17 +854,26 @@ handle_mq_error (void *cls,
  */
 static void
 handle_get_peers (void *cls,
-                  const struct GNUNET_CADET_LocalInfoPeer *msg)
+                  const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_CADET_Handle *h = cls;
+  const struct GNUNET_CADET_LocalInfoPeer *info =
+    (const struct GNUNET_CADET_LocalInfoPeer *) msg;
 
   if (NULL == h->info_cb.peers_cb)
     return;
-  h->info_cb.peers_cb (h->info_cls,
-                       &msg->destination,
-                       (int) ntohs (msg->tunnel),
-                       (unsigned int) ntohs (msg->paths),
-                       0);
+  if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
+    h->info_cb.peers_cb (h->info_cls,
+                         &info->destination,
+                         (int) ntohs (info->tunnel),
+                         (unsigned int) ntohs (info->paths),
+                         0);
+  else
+    h->info_cb.peers_cb (h->info_cls,
+                         NULL,
+                         0,
+                         0,
+                         0);
 }
 
 
@@ -945,6 +977,29 @@ handle_get_peer (void *cls,
 }
 
 
+/**
+ * Check that message received from CADET service is well-formed.
+ *
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
+ */
+static int
+check_get_tunnels (void *cls,
+                   const struct GNUNET_MessageHeader *message)
+{
+  size_t esize;
+
+  esize = ntohs (message->size);
+  if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize)
+    return GNUNET_OK;
+  if (sizeof (struct GNUNET_MessageHeader) == esize)
+    return GNUNET_OK;
+  return GNUNET_SYSERR;
+}
+
+
 /**
  * Process a local reply about info on all tunnels, pass info to the user.
  *
@@ -953,19 +1008,28 @@ handle_get_peer (void *cls,
  */
 static void
 handle_get_tunnels (void *cls,
-                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
+                    const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_CADET_Handle *h = cls;
+  const struct GNUNET_CADET_LocalInfoTunnel *info =
+    (const struct GNUNET_CADET_LocalInfoTunnel *) msg;
 
   if (NULL == h->info_cb.tunnels_cb)
     return;
-  h->info_cb.tunnels_cb (h->info_cls,
-                         &msg->destination,
-                         ntohl (msg->channels),
-                         ntohl (msg->connections),
-                         ntohs (msg->estate),
-                         ntohs (msg->cstate));
-
+  if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size))
+    h->info_cb.tunnels_cb (h->info_cls,
+                           &info->destination,
+                           ntohl (info->channels),
+                           ntohl (info->connections),
+                           ntohs (info->estate),
+                           ntohs (info->cstate));
+  else
+    h->info_cb.tunnels_cb (h->info_cls,
+                           NULL,
+                           0,
+                           0,
+                           0,
+                           0);
 }
 
 
@@ -1075,18 +1139,18 @@ reconnect (struct GNUNET_CADET_Handle *h)
                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
                              struct GNUNET_CADET_LocalAck,
                              h),
-    GNUNET_MQ_hd_fixed_size (get_peers,
-                             GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
-                             struct GNUNET_CADET_LocalInfoPeer,
-                             h),
+    GNUNET_MQ_hd_var_size (get_peers,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
+                           struct GNUNET_MessageHeader,
+                           h),
     GNUNET_MQ_hd_var_size (get_peer,
                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
                            struct GNUNET_CADET_LocalInfoPeer,
                            h),
-    GNUNET_MQ_hd_fixed_size (get_tunnels,
-                             GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
-                             struct GNUNET_CADET_LocalInfoTunnel,
-                             h),
+    GNUNET_MQ_hd_var_size (get_tunnels,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
+                           struct GNUNET_MessageHeader,
+                           h),
     GNUNET_MQ_hd_var_size (get_tunnel,
                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
                            struct GNUNET_CADET_LocalInfoTunnel,
@@ -1094,6 +1158,7 @@ reconnect (struct GNUNET_CADET_Handle *h)
     GNUNET_MQ_handler_end ()
   };
 
+  GNUNET_assert (NULL == h->mq);
   h->mq = GNUNET_CLIENT_connect (h->cfg,
                                  "cadet",
                                  handlers,
@@ -1583,7 +1648,7 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  * @param window_changes Function called when the transmit window size changes.
  * @param disconnects Function called when a channel is disconnected.
  * @param handlers Callbacks for messages we care about, NULL-terminated.
- * @return Port handle.
+ * @return Port handle, NULL if port is in use
  */
 struct GNUNET_CADET_Port *
 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
@@ -1600,21 +1665,28 @@ GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
 
   GNUNET_assert (NULL != connects);
   GNUNET_assert (NULL != disconnects);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Listening to CADET port %s\n",
+             GNUNET_h2s (port));
+  
   p = GNUNET_new (struct GNUNET_CADET_Port);
   p->cadet = h;
   p->id = *port;
+  if (GNUNET_OK !=
+      GNUNET_CONTAINER_multihashmap_put (h->ports,
+                                         &p->id,
+                                         p,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+  {
+    GNUNET_free (p);
+    return NULL;
+  }
   p->connects = connects;
   p->cls = connects_cls;
   p->window_changes = window_changes;
   p->disconnects = disconnects;
   p->handlers = GNUNET_MQ_copy_handlers (handlers);
 
-  GNUNET_assert (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put (h->ports,
-                                                   &p->id,
-                                                   p,
-                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
 
   env = GNUNET_MQ_msg (msg,
                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
@@ -1659,6 +1731,10 @@ GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
   struct GNUNET_MQ_Envelope *env;
 
   GNUNET_assert (NULL != disconnects);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Creating channel to peer %s at port %s\n",
+             GNUNET_i2s (destination),
+             GNUNET_h2s (port));
   ch = create_channel (h,
                        NULL);
   ch->ctx = channel_cls;