fix usage of mesh_api
[oweals/gnunet.git] / src / mesh / mesh_api.c
index f1f9b17ab8086bc7658f6784fd379b41612b731d..82fb75c5128a959f8b47be9bba43470b2a158401 100644 (file)
@@ -32,7 +32,7 @@
 
 struct tunnel_id
 {
-  uint32_t id;
+  uint32_t id GNUNET_PACKED;
   struct GNUNET_PeerIdentity initiator;
   struct GNUNET_PeerIdentity target;
 };
@@ -46,6 +46,13 @@ struct tunnel_message
   /* followed by another GNUNET_MessageHeader */
 };
 
+struct notify_cls
+{
+  void* notify_cls;
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
+  struct GNUNET_MESH_Tunnel *tunnel;
+};
+
 struct GNUNET_MESH_Tunnel
 {
   /* The other peer this tunnel leads to; just unicast for the moment! */
@@ -53,9 +60,6 @@ struct GNUNET_MESH_Tunnel
 
   struct tunnel_id id;
 
-  void* notify_cls;
-  GNUNET_CONNECTION_TransmitReadyNotify notify;
-
   /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
   GNUNET_MESH_TunnelConnectHandler connect_handler;
@@ -260,11 +264,11 @@ core_receive (void *cls,
 
   struct GNUNET_MESH_MessageHandler *handler;
 
-  for (handler = handle->handlers; handler != NULL; handler++)
+  for (handler = handle->handlers; handler->callback != NULL; handler++)
     {
-      if (ntohs (rmessage->type) == handler->type
-         && (handler->expected_size == 0
-             || handler->expected_size == ntohs (rmessage->size)))
+      if ( (ntohs (rmessage->type) == handler->type)
+          && ( (handler->expected_size == 0)
+               || (handler->expected_size == ntohs (rmessage->size))) )
        {
          break;
        }
@@ -273,7 +277,7 @@ core_receive (void *cls,
   /* handler->callback handles this message */
 
   /* If no handler was found, drop the message but keep the channel open */
-  if (handler == NULL)
+  if (handler->callback == NULL)
     return GNUNET_OK;
 
   struct tunnel_list_element *tunnel = handle->established_tunnels.head;
@@ -341,6 +345,7 @@ GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
   memcpy (&tunnel->tunnel.id.target, peers,
          sizeof (struct GNUNET_PeerIdentity));
   tunnel->tunnel.id.id = current_id++;
+  memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
 
   struct peer_list_element *element = handle->connected_peers.head;
   while (element != NULL)
@@ -378,6 +383,10 @@ GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
                                         handle->pending_tunnels.tail,
                                         handle->pending_tunnels.tail,
                                         tunnel);
+      (void) GNUNET_CORE_peer_request_connect (handle->core,
+                                              timeout,
+                                              peers,
+                                              NULL, NULL);                                     
     }
 
   return &tunnel->tunnel;
@@ -392,14 +401,17 @@ GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
 static size_t
 core_notify(void* cls, size_t size, void* buf)
 {
-  struct GNUNET_MESH_Tunnel *tunnel = cls;
+  struct notify_cls *ncls = cls;
+  struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
   struct tunnel_message* message = buf;
-  void* cbuf = (void*)(message + 1);
+  void* cbuf = (void*) &message[1];
+  GNUNET_assert(NULL != ncls->notify);
 
-  size_t sent = tunnel->notify(tunnel->notify_cls, size - sizeof(struct tunnel_message), cbuf);
+  size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
 
-  tunnel->notify = NULL;
-  tunnel->notify_cls = NULL;
+  GNUNET_free(ncls);
+
+  if (0 == sent) return 0;
 
   sent += sizeof(struct tunnel_message);
 
@@ -423,18 +435,21 @@ GNUNET_MESH_notify_transmit_ready (struct
                                   GNUNET_CONNECTION_TransmitReadyNotify
                                   notify, void *notify_cls)
 {
-  tunnel->notify_cls = notify_cls;
-  tunnel->notify = notify;
+  struct notify_cls *cls = GNUNET_malloc(sizeof(struct notify_cls));
+  cls->notify_cls = notify_cls;
+  GNUNET_assert(NULL != notify);
+  cls->notify = notify;
+  cls->tunnel = tunnel;
   GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
                                    priority,
                                    maxdelay,
                                    &tunnel->peer,
                                    notify_size + sizeof(struct tunnel_message),
-                                   core_notify,
-                                   (void*)tunnel);
+                                   &core_notify,
+                                   (void*)cls);
 
   /* aborting is not implemented yet */
-  return (struct GNUNET_MESH_TransmitHandle*)1;
+  return (struct GNUNET_MESH_TransmitHandle*) 1;
 }
 
 
@@ -454,7 +469,7 @@ GNUNET_MESH_connect (const struct
   ret->connected_peers.tail = NULL;
   ret->cleaner = cleaner;
   ret->cls = cls;
-
+    
   const struct GNUNET_MESH_MessageHandler *it;
   unsigned int len = 1;
   for (it = handlers; it->callback != NULL; it++)
@@ -464,20 +479,21 @@ GNUNET_MESH_connect (const struct
 
   ret->handlers =
     GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
+  memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
   memcpy (ret->handlers, handlers,
          len * sizeof (struct GNUNET_MESH_MessageHandler));
 
   const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
-    {core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
+    {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
     {NULL, 0, 0}
   };
 
   ret->core = GNUNET_CORE_connect (cfg,
                                   42,
                                   ret,
-                                  core_startup,
-                                  core_connect,
-                                  core_disconnect,
+                                  &core_startup,
+                                  &core_connect,
+                                  &core_disconnect,
                                   NULL,
                                   NULL,
                                   GNUNET_NO, NULL, GNUNET_NO, core_handlers);
@@ -516,4 +532,4 @@ GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
   GNUNET_free (handle);
 }
 
-/* end of core_api.c */
+/* end of mesh_api.c */