- stop using message types of payload on service side
[oweals/gnunet.git] / src / mesh / mesh2_api.c
index 98359dff68d084c7c0d51fa90af4fa113e8b1947..57a5ce1ed30b33d577eafecae66dc1778704ebe2 100644 (file)
@@ -117,6 +117,21 @@ struct GNUNET_MESH_Handle
      */
   const struct GNUNET_MESH_MessageHandler *message_handlers;
 
+  /**
+   * Number of handlers in the handlers array.
+   */
+  unsigned int n_handlers;
+
+  /**
+   * Ports open.
+   */
+  uint32_t *ports;
+
+  /**
+   * Number of ports.
+   */
+  unsigned int n_ports;
+
     /**
      * Double linked list of the tunnels this client is connected to, head.
      */
@@ -162,11 +177,6 @@ struct GNUNET_MESH_Handle
      */
   MESH_TunnelNumber next_tid;
 
-    /**
-     * Number of handlers in the handlers array.
-     */
-  unsigned int n_handlers;
-
     /**
      * Have we started the task to receive messages from the service
      * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
@@ -208,16 +218,6 @@ struct GNUNET_MESH_Handle
    */
   void *tunnel_cls;
 
-  /**
-   * All the peer in the tunnel so far.
-   */
-  struct GNUNET_PeerIdentity *peers;
-
-  /**
-   * How many peers we have in this tunnel so far.
-   */
-  unsigned int tunnel_npeers;
-
 #if DEBUG_ACK
   unsigned int acks_sent;
   unsigned int acks_recv;
@@ -274,6 +274,11 @@ struct GNUNET_MESH_Tunnel
      */
   MESH_TunnelNumber tid;
 
+    /**
+     * Port number.
+     */
+  uint32_t port;
+
     /**
      * Other end of the tunnel.
      */
@@ -638,28 +643,27 @@ send_connect (struct GNUNET_MESH_Handle *h)
   size_t size;
 
   size = sizeof (struct GNUNET_MESH_ClientConnect);
-  size += h->n_handlers * sizeof (uint16_t);
+  size += h->n_ports * sizeof (uint32_t);
   {
     char buf[size] GNUNET_ALIGN;
     struct GNUNET_MESH_ClientConnect *msg;
-    uint16_t *types;
-    uint16_t ntypes;
+    uint32_t *ports;
+    uint16_t i;
 
     /* build connection packet */
     msg = (struct GNUNET_MESH_ClientConnect *) buf;
     msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
     msg->header.size = htons (size);
-    types = (uint16_t *) & msg[1];
-    for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
+    ports = (uint32_t *) &msg[1];
+    for (i = 0; i < h->n_ports; i++)
     {
-      types[ntypes] = htons (h->message_handlers[ntypes].type);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
-           h->message_handlers[ntypes].type);
+      ports[i] = htonl (h->ports[i]);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
+           h->ports[i]);
     }
-    msg->types = htons (ntypes);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Sending %lu bytes long message %d types\n",
-         ntohs (msg->header.size), ntypes);
+         "Sending %lu bytes long message with %u ports\n",
+         ntohs (msg->header.size), h->n_ports);
     send_packet (h, &msg->header, NULL);
   }
 }
@@ -813,12 +817,13 @@ process_tunnel_created (struct GNUNET_MESH_Handle *h,
     GNUNET_PEER_change_rc (t->peer, 1);
     t->mesh = h;
     t->tid = tid;
+    t->port = ntohl (msg->port);
     if ((msg->opt & MESH_TUNNEL_OPT_NOBUFFER) != 0)
       t->buffering = GNUNET_NO;
     else
       t->buffering = GNUNET_YES;
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created tunnel %p\n", t);
-    t->ctx = h->new_tunnel (h->cls, t, &msg->peer);
+    t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
   }
   else
@@ -879,8 +884,8 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
   const struct GNUNET_MessageHeader *payload;
   const struct GNUNET_MESH_MessageHandler *handler;
   const struct GNUNET_PeerIdentity *peer;
+  struct GNUNET_PeerIdentity id;
   struct GNUNET_MESH_Unicast *ucast;
-  struct GNUNET_MESH_Multicast *mcast;
   struct GNUNET_MESH_ToOrigin *to_orig;
   struct GNUNET_MESH_Tunnel *t;
   unsigned int i;
@@ -901,20 +906,12 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  ucast on tunnel %s [%X]\n",
          GNUNET_i2s (peer), ntohl (ucast->tid));
     break;
-  case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
-    mcast = (struct GNUNET_MESH_Multicast *) message;
-    t = retrieve_tunnel (h, ntohl (mcast->tid));
-    payload = (struct GNUNET_MessageHeader *) &mcast[1];
-    peer = &mcast->oid;
-    pid = ntohl (mcast->pid);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  mcast on tunnel %s [%X]\n",
-         GNUNET_i2s (peer), ntohl (mcast->tid));
-    break;
   case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
     to_orig = (struct GNUNET_MESH_ToOrigin *) message;
     t = retrieve_tunnel (h, ntohl (to_orig->tid));
     payload = (struct GNUNET_MessageHeader *) &to_orig[1];
-    peer = &to_orig->sender;
+    GNUNET_PEER_resolve (t->peer, &id);
+    peer = &id;
     pid = ntohl (to_orig->pid);
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  torig on tunnel %s [%X]\n",
          GNUNET_i2s (peer), ntohl (to_orig->tid));
@@ -1022,7 +1019,6 @@ process_get_tunnels (struct GNUNET_MESH_Handle *h,
                      const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_LocalMonitor *msg;
-  uint32_t npeers;
 
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
 
@@ -1033,24 +1029,21 @@ process_get_tunnels (struct GNUNET_MESH_Handle *h,
   }
 
   msg = (struct GNUNET_MESH_LocalMonitor *) message;
-  npeers = ntohl (msg->npeers);
   if (ntohs (message->size) !=
       (sizeof (struct GNUNET_MESH_LocalMonitor) +
-       npeers * sizeof (struct GNUNET_PeerIdentity)))
+       sizeof (struct GNUNET_PeerIdentity)))
   {
     GNUNET_break_op (0);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Get tunnels message: size %hu - expected %u (%u peers)\n",
+                "Get tunnels message: size %hu - expected %u\n",
                 ntohs (message->size),
-                sizeof (struct GNUNET_MESH_LocalMonitor) +
-                npeers * sizeof (struct GNUNET_PeerIdentity),
-                npeers);
+                sizeof (struct GNUNET_MESH_LocalMonitor));
     return;
   }
   h->tunnels_cb (h->tunnels_cls,
                  ntohl (msg->tunnel_id),
                  &msg->owner,
-                 (struct GNUNET_PeerIdentity *) &msg[1]);
+                 &msg->destination);
 }
 
 
@@ -1066,11 +1059,7 @@ process_show_tunnel (struct GNUNET_MESH_Handle *h,
                      const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MESH_LocalMonitor *msg;
-  struct GNUNET_PeerIdentity *new_peers;
-  uint32_t *new_parents;
   size_t esize;
-  uint32_t npeers;
-  unsigned int i;
 
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
 
@@ -1082,40 +1071,25 @@ process_show_tunnel (struct GNUNET_MESH_Handle *h,
 
   /* Verify message sanity */
   msg = (struct GNUNET_MESH_LocalMonitor *) message;
-  npeers = ntohl (msg->npeers);
   esize = sizeof (struct GNUNET_MESH_LocalMonitor);
-  esize += npeers * (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t));
   if (ntohs (message->size) != esize)
   {
     GNUNET_break_op (0);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Show tunnel message: size %hu - expected %u (%u peers)\n",
+                "Show tunnel message: size %hu - expected %u\n",
                 ntohs (message->size),
-                esize,
-                npeers);
+                esize);
 
     h->tunnel_cb (h->tunnel_cls, NULL, NULL);
     h->tunnel_cb = NULL;
     h->tunnel_cls = NULL;
-    h->tunnel_npeers = 0;
-    GNUNET_free_non_null (h->peers);
-    h->peers = NULL;
 
     return;
   }
 
-  new_peers = (struct GNUNET_PeerIdentity *) &msg[1];
-  new_parents = (uint32_t *) &new_peers[npeers];
-
-  h->peers = GNUNET_realloc (h->peers, h->tunnel_npeers + npeers);
-  memcpy (&h->peers[h->tunnel_npeers],
-          new_peers,
-          npeers * sizeof (struct GNUNET_PeerIdentity));
-  h->tunnel_npeers += npeers;
-  for (i = 0; i < npeers; i++)
-    h->tunnel_cb (h->tunnel_cls,
-                  &new_peers[i],
-                  &h->peers[new_parents[i]]);
+  h->tunnel_cb (h->tunnel_cls,
+                &msg->destination,
+                &msg->owner);
 }
 
 
@@ -1259,7 +1233,6 @@ send_callback (void *cls, size_t size, void *buf)
           to.pid = htonl (t->next_send_pid);
           to.ttl = 0;
           memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
-          memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
           memcpy (cbuf, &to, sizeof (to));
         }
       }
@@ -1382,7 +1355,8 @@ struct GNUNET_MESH_Handle *
 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
                      GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
                      GNUNET_MESH_TunnelEndHandler cleaner,
-                     const struct GNUNET_MESH_MessageHandler *handlers)
+                     const struct GNUNET_MESH_MessageHandler *handlers,
+                     uint32_t *ports)
 {
   struct GNUNET_MESH_Handle *h;
 
@@ -1401,6 +1375,7 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
   }
   h->cls = cls;
   h->message_handlers = handlers;
+  h->ports = ports;
   h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
   h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
@@ -1409,6 +1384,9 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
   for (h->n_handlers = 0;
        handlers && handlers[h->n_handlers].type;
        h->n_handlers++) ;
+  for (h->n_ports = 0;
+       ports && ports[h->n_ports];
+       h->n_ports++) ;
   send_connect (h);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
   return h;
@@ -1497,7 +1475,8 @@ GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
 struct GNUNET_MESH_Tunnel *
 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, 
                            void *tunnel_ctx,
-                           const struct GNUNET_PeerIdentity *peer)
+                           const struct GNUNET_PeerIdentity *peer,
+                           uint32_t port)
 {
   struct GNUNET_MESH_Tunnel *t;
   struct GNUNET_MESH_TunnelMessage msg;
@@ -1510,6 +1489,8 @@ GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
   msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
   msg.tunnel_id = htonl (t->tid);
+  msg.port = htonl (port);
+  msg.peer = *peer;
   send_packet (h, &msg.header, t);
   return t;
 }
@@ -1712,6 +1693,7 @@ GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
  * Request information about a specific tunnel of the running mesh peer.
  *
  * WARNING: unstable API, likely to change in the future!
+ * FIXME Add destination option.
  *
  * @param h Handle to the mesh peer.
  * @param initiator ID of the owner of the tunnel.
@@ -1730,7 +1712,6 @@ GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
 
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
-  msg.npeers = htonl (0);
   msg.owner = *initiator;
   msg.tunnel_id = htonl (tunnel_number);
   msg.reserved = 0;