- simplify timing out of old hellos
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_peer.c
index 32d2513968e05248ea195793fa3c852ca7eda8c7..569eda84bccce534e62d4b54ae234842bc1472e2 100644 (file)
@@ -22,6 +22,7 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
+#include "gnunet_transport_service.h"
 #include "gnunet_core_service.h"
 #include "gnunet_statistics_service.h"
 
@@ -163,7 +164,7 @@ struct MeshPeer
   /**
    * Hello message.
    */
-  const struct GNUNET_HELLO_Message* hello;
+  struct GNUNET_HELLO_Message* hello;
 };
 
 
@@ -206,6 +207,8 @@ static unsigned long long drop_percent;
  */
 static struct GNUNET_CORE_Handle *core_handle;
 
+static struct GNUNET_TRANSPORT_Handle *transport_handle;
+
 
 /******************************************************************************/
 /***************************** CORE CALLBACKS *********************************/
@@ -1070,7 +1073,7 @@ GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
   int priority;
   int call_core;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
+  LOG (GNUNET_ERROR_TYPE_INFO,
        "queue add %s %s towards %s (size %u) on c %p (%s)\n",
        GM_f2s (fwd),  GM_m2s (type), GMP_2s(peer),
        size, c, GMC_2s (c));
@@ -1292,12 +1295,17 @@ GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
                                      NULL,      /* Don't notify about all outbound messages */
                                      GNUNET_NO, /* For header-only out notification */
                                      core_handlers);    /* Register these handlers */
-  if (NULL == core_handle)
+  transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id,
+                                        NULL, /* cls */
+                                        NULL, NULL, NULL); /* Notify callbacks */
+
+  if (NULL == core_handle || NULL == transport_handle)
   {
     GNUNET_break (0);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
+
 }
 
 /**
@@ -1313,6 +1321,11 @@ GMP_shutdown (void)
     GNUNET_CORE_disconnect (core_handle);
     core_handle = NULL;
   }
+  if (transport_handle != NULL)
+  {
+    GNUNET_TRANSPORT_disconnect (transport_handle);
+    transport_handle = NULL;
+  }
   GNUNET_PEER_change_rc (myid, -1);
 }
 
@@ -1362,6 +1375,24 @@ GMP_get_short (const GNUNET_PEER_Id peer)
 }
 
 
+/**
+ * Try to connect to a peer on transport level.
+ *
+ * @param cls Closure (peer).
+ * @param tc TaskContext.
+ */
+static void
+try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct MeshPeer *peer = cls;
+
+  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
+    return;
+
+  GNUNET_TRANSPORT_try_connect (transport_handle,
+                                GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
+}
+
 /**
  * Try to establish a new connection to this peer (in its tunnel).
  * If the peer doesn't have any path to it yet, try to get one.
@@ -1375,9 +1406,21 @@ GMP_connect (struct MeshPeer *peer)
   struct MeshTunnel3 *t;
   struct MeshPeerPath *p;
   struct MeshConnection *c;
+  struct GNUNET_HELLO_Message *hello;
   int rerun_search;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GMP_2s (peer));
+
+  /* If we have a current hello, try to connect using it. */
+  hello = GMP_get_hello (peer);
+  if (NULL != hello)
+  {
+    struct GNUNET_MessageHeader *mh;
+
+    mh = GNUNET_HELLO_get_header (hello);
+    GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
+  }
+
   t = peer->tunnel;
   c = NULL;
   rerun_search = GNUNET_NO;
@@ -1842,24 +1885,22 @@ GMP_get_tunnel (const struct MeshPeer *peer)
 void
 GMP_set_hello (struct MeshPeer *peer, const struct GNUNET_HELLO_Message *hello)
 {
-  struct GNUNET_TIME_Absolute expiration;
-  struct GNUNET_TIME_Relative remaining;
+  struct GNUNET_HELLO_Message *old;
+  size_t size;
 
-  if (NULL == peer->hello)
+  old = GMP_get_hello (peer);
+  if (NULL == old)
   {
-    peer->hello = hello;
+    size = GNUNET_HELLO_size (hello);
+    peer->hello = GNUNET_malloc (size);
+    memcpy (peer->hello, hello, size);
     return;
   }
-
-  expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
-  remaining = GNUNET_TIME_absolute_get_remaining (expiration);
-  if (0 == remaining.rel_value_us)
+  else
   {
-    GNUNET_free (peer->hello);
-    peer->hello = hello;
+    peer->hello = GNUNET_HELLO_merge (old, hello);
+    GNUNET_free (old);
   }
-  else
-    peer->hello = GNUNET_HELLO_merge (peer->hello, hello);
 }
 
 
@@ -1870,9 +1911,22 @@ GMP_set_hello (struct MeshPeer *peer, const struct GNUNET_HELLO_Message *hello)
  *
  * @return Hello message.
  */
-const struct GNUNET_HELLO_Message *
+struct GNUNET_HELLO_Message *
 GMP_get_hello (struct MeshPeer *peer)
 {
+  struct GNUNET_TIME_Absolute expiration;
+  struct GNUNET_TIME_Relative remaining;
+
+  if (NULL == peer->hello)
+    return NULL;
+
+  expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
+  remaining = GNUNET_TIME_absolute_get_remaining (expiration);
+  if (0 == remaining.rel_value_us)
+  {
+    GNUNET_free (peer->hello);
+    peer->hello = NULL;
+  }
   return peer->hello;
 }