Added transition code for new MESH API
[oweals/gnunet.git] / src / mesh / mesh_api_new.c
index a85173e5446c559baa320007ada2260d84d0f5e7..1d25868255317b70bd62aaed6b53888d980b380a 100644 (file)
@@ -45,7 +45,7 @@ extern "C"
 #include "mesh.h"
 #include "mesh_protocol.h"
 
-#define DEBUG GNUNET_YES
+#define MESH_API_DEBUG GNUNET_YES
 
 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
 
@@ -618,7 +618,7 @@ send_connect (struct GNUNET_MESH_Handle *h)
       types[ntypes] = htons (h->message_handlers[ntypes].type);
     msg->applications = htons (napps);
     msg->types = htons (ntypes);
-#if DEBUG
+#if MESH_API_DEBUG
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "mesh: Sending %lu bytes long message %d types and %d apps\n",
          ntohs (msg->header.size), ntypes, napps);
@@ -642,7 +642,7 @@ reconnect (struct GNUNET_MESH_Handle *h)
   struct GNUNET_MESH_Tunnel *t;
   unsigned int i;
 
-#if DEBUG
+#if MESH_API_DEBUG
   LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh: *****************************\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh: *******   RECONNECT   *******\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh: *****************************\n");
@@ -683,6 +683,14 @@ reconnect (struct GNUNET_MESH_Handle *h)
     struct GNUNET_MESH_TunnelMessage tmsg;
     struct GNUNET_MESH_PeerControl pmsg;
 
+    if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+    {
+      /* Tunnel was created by service (incoming tunnel) */
+      /* TODO: Notify service of missing tunnel, to request
+       * creator to recreate path (find a path to him via DHT?)
+       */
+      continue;
+    }
     tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
     tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
     tmsg.tunnel_id = htonl (t->tid);
@@ -749,11 +757,11 @@ process_tunnel_created (struct GNUNET_MESH_Handle *h,
                         const struct GNUNET_MESH_TunnelNotification *msg)
 {
   struct GNUNET_MESH_Tunnel *t;
-  struct GNUNET_TRANSPORT_ATS_Information atsi;
+  struct GNUNET_ATS_Information atsi;
   MESH_TunnelNumber tid;
 
   tid = ntohl (msg->tunnel_id);
-  if (tid <= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
+  if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
   {
     GNUNET_break (0);
     return;
@@ -796,7 +804,6 @@ process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
 
   if (NULL == t)
   {
-    GNUNET_break (0);
     return;
   }
   if (0 == t->owner)
@@ -821,7 +828,7 @@ process_peer_event (struct GNUNET_MESH_Handle *h,
 {
   struct GNUNET_MESH_Tunnel *t;
   struct GNUNET_MESH_Peer *p;
-  struct GNUNET_TRANSPORT_ATS_Information atsi;
+  struct GNUNET_ATS_Information atsi;
   GNUNET_PEER_Id id;
   uint16_t size;
 
@@ -928,7 +935,7 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
     handler = &h->message_handlers[i];
     if (handler->type == type)
     {
-      struct GNUNET_TRANSPORT_ATS_Information atsi;
+      struct GNUNET_ATS_Information atsi;
 
       atsi.type = 0;
       atsi.value = 0;
@@ -939,7 +946,7 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
         GNUNET_MESH_disconnect (h);
         return;
       }
-#if DEBUG
+#if MESH_API_DEBUG
       else
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1036,12 +1043,6 @@ send_callback (void *cls, size_t size, void *buf)
   tsize = 0;
   while ((NULL != (th = h->th_head)) && (size >= th->size))
   {
-#if DEBUG
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh:     type: %u\n",
-         ntohs (((struct GNUNET_MessageHeader *) &th[1])->type));
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh:     size: %u\n",
-         ntohs (((struct GNUNET_MessageHeader *) &th[1])->size));
-#endif
     if (NULL != th->notify)
     {
       if (th->tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
@@ -1059,31 +1060,38 @@ send_callback (void *cls, size_t size, void *buf)
               ntohs (mh->type));
         if (psize > 0)
         {
-          to.header.size = htons (th->size);
+          psize += sizeof (to);
+          GNUNET_assert (size >= psize);
+          to.header.size = htons (psize);
           to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
           to.tid = htonl (th->tunnel->tid);
           memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
           memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
           memcpy (cbuf, &to, sizeof (to));
-          psize += sizeof (to);
         }
       }
       else if (th->target == 0)
       {
         /* multicast */
         struct GNUNET_MESH_Multicast mc;
+        struct GNUNET_MessageHeader *mh;
 
         GNUNET_assert (size >= th->size);
+        mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (mc)];
         psize =
-            th->notify (th->notify_cls, size - sizeof (mc), &cbuf[sizeof (mc)]);
+            th->notify (th->notify_cls, size - sizeof (mc), mh);
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                    "mesh:   multicast, type %u\n",
+                    ntohs (mh->type));
         if (psize > 0)
         {
-          mc.header.size = htons (sizeof (mc) + th->size);
+          psize += sizeof (mc);
+          GNUNET_assert (size >= psize);
+          mc.header.size = htons (psize);
           mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
           mc.tid = htonl (th->tunnel->tid);
           memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
           memcpy (cbuf, &mc, sizeof (mc));
-          psize += sizeof (mc);
         }
       }
       else
@@ -1101,13 +1109,14 @@ send_callback (void *cls, size_t size, void *buf)
               ntohs (mh->type));
         if (psize > 0)
         {
-          uc.header.size = htons (th->size);
+          psize += sizeof (uc);
+          GNUNET_assert (size >= psize);
+          uc.header.size = htons (psize);
           uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
           uc.tid = htonl (th->tunnel->tid);
           memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
           GNUNET_PEER_resolve (th->target, &uc.destination);
           memcpy (cbuf, &uc, sizeof (uc));
-          psize += sizeof (uc);
         }
       }
     }
@@ -1346,6 +1355,7 @@ GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
   {
     if (tunnel->peers[i]->id == peer_id)
     {
+      /* Peer already exists in tunnel */
       GNUNET_PEER_change_rc (peer_id, -1);
       GNUNET_break (0);
       return;
@@ -1468,12 +1478,17 @@ GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
   uint32_t least_priority;
   size_t overhead;
 
+#if MESH_API_DEBUG
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "mesh: mesh notify transmit ready called\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "mesh:     target %s\n",
-              GNUNET_i2s (target));
-
+  if (NULL != target)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "mesh:     target %s\n",
+                GNUNET_i2s (target));
+  else
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "mesh:     target multicast\n");
+#endif
   GNUNET_assert (NULL != notify);
   if (tunnel->mesh->npackets >= tunnel->mesh->max_queue_size &&
       tunnel->npackets > 0)
@@ -1560,6 +1575,25 @@ GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
 }
 
 
+/**
+ * Transition API for tunnel ctx management
+ */
+void
+GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
+{
+  tunnel->ctx = data;
+}
+
+/**
+ * Transition API for tunnel ctx management
+ */
+void *
+GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
+{
+  return tunnel->ctx;
+}
+
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif