WiP
[oweals/gnunet.git] / src / mesh / mesh_api.c
index 12759f2a749dc7d04204fd179a0069d856fdfcc4..8b1655dde501554967adae4b43e0c0424d4472da 100644 (file)
@@ -141,6 +141,17 @@ send_self_connect(void* cls,
   GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
 }
 
+static void
+call_connect_handler (void *cls,
+                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_MESH_Tunnel *tunnel = cls;
+
+  tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer,
+                           NULL);
+  GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
+}
+
 static void
 core_startup (void *cls,
              struct GNUNET_CORE_Handle *core,
@@ -171,8 +182,7 @@ send_hello_message (void *cls, size_t size, void *buf)
 /**
  * Core calls this if we are connected to a new peer.
  *
- * If core tells us that we are connected to ourself, we ignore it. Otherwise, the
- * peer is added to the connected_peers-list.
+ * The peer is added to the connected_peers-list.
  *
  */
 static void
@@ -181,18 +191,25 @@ core_connect (void *cls,
              const struct GNUNET_TRANSPORT_ATS_Information *atsi)
 {
   struct GNUNET_MESH_Handle *handle = cls;
-  /* Check for connect-to-self-message, which we ignore */
-  if (0 ==
-      memcmp (peer, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
-    return;
 
+  /* Send a hello to this peer */
+  GNUNET_CORE_notify_transmit_ready(handle->core,
+                                    GNUNET_NO,
+                                    42,
+                                    GNUNET_TIME_UNIT_SECONDS,
+                                    peer,
+                                    sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
+                                    &send_hello_message,
+                                    cls);
 
   /* put the new peer into the list of connected peers */
   struct peer_list_element *element =
     GNUNET_malloc (sizeof (struct peer_list_element));
   memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&element->atsi, atsi,
-         sizeof (struct GNUNET_TRANSPORT_ATS_Information));
+
+  if (NULL != atsi)
+    memcpy (&element->atsi, atsi,
+            sizeof (struct GNUNET_TRANSPORT_ATS_Information));
 
   GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
                                     handle->connected_peers.tail,
@@ -220,15 +237,6 @@ core_connect (void *cls,
       else
        tunnel = tunnel->next;
     }
-  GNUNET_CORE_notify_transmit_ready(handle->core,
-                                    GNUNET_NO,
-                                    42,
-                                    GNUNET_TIME_UNIT_SECONDS,
-                                    peer,
-                                    sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
-                                    &send_hello_message,
-                                    cls);
-
 }
 
 /**
@@ -313,6 +321,8 @@ receive_hello (void *cls,
       element = element->next;
     }
 
+  /* TODO: add, not replace! */
+  /* TODO: if this changes anything: send new hello */
   element->num_types = *num;
   element->types = GNUNET_malloc (*num * sizeof (GNUNET_MESH_ApplicationType));
 
@@ -512,7 +522,7 @@ GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
                                         handle->established_tunnels.tail,
                                         handle->established_tunnels.tail,
                                         tunnel);
-      connect_handler (handler_cls, &element->peer, &element->atsi);
+      GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel);
     }
   else if (0 ==
           memcmp (peers, &handle->myself,
@@ -535,7 +545,7 @@ GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
       (void) GNUNET_CORE_peer_request_connect (handle->core,
                                               timeout,
                                               peers,
-                                              NULL, NULL);                                     
+                                              NULL, NULL);
     }
 
   return &tunnel->tunnel;
@@ -625,6 +635,23 @@ GNUNET_MESH_notify_transmit_ready (struct
   return (struct GNUNET_MESH_TransmitHandle*) 1;
 }
 
+void build_hello_message(struct GNUNET_MESH_Handle* handle, int num)
+{
+  handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */
+    num * sizeof(uint16_t); /* For the types */
+
+  uint16_t *nums = GNUNET_malloc(handle->hello_message_size);
+  uint16_t *types = nums + 1;
+
+  *nums = num;
+
+  unsigned int i;
+  for(i = 0; i < num; i++)
+    types[i] = handle->handlers[i].type;
+
+  handle->hello_message = nums;
+}
+
 
 struct GNUNET_MESH_Handle *
 GNUNET_MESH_connect (const struct
@@ -657,6 +684,8 @@ GNUNET_MESH_connect (const struct
   memcpy (ret->handlers, handlers,
          len * sizeof (struct GNUNET_MESH_MessageHandler));
 
+  build_hello_message(ret, len);
+
   const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
     {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
     {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
@@ -679,12 +708,14 @@ void
 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
 {
   GNUNET_free (handle->handlers);
+  GNUNET_free (handle->hello_message);
   GNUNET_CORE_disconnect (handle->core);
 
   struct peer_list_element *element = handle->connected_peers.head;
   while (element != NULL)
     {
       struct peer_list_element *next = element->next;
+      GNUNET_free_non_null(element->types);
       GNUNET_free (element);
       element = next;
     }