disable LAN/loopback for AC
[oweals/gnunet.git] / src / transport / transport_api_core.c
index de18b7339e6f4d2edd94609b7c377c5b1eb6c5a7..9c29d4908ec4a57447b651386d13db9b9009cca0 100644 (file)
@@ -121,7 +121,7 @@ struct Neighbour
  * Handle for the transport service (includes all of the
  * state for the transport service).
  */
-struct GNUNET_TRANSPORT_Handle
+struct GNUNET_TRANSPORT_CoreHandle
 {
 
   /**
@@ -190,16 +190,6 @@ struct GNUNET_TRANSPORT_Handle
 };
 
 
-/**
- * Schedule the task to send one message, either from the control
- * list or the peer message queues  to the service.
- *
- * @param h transport service to schedule a transmission for
- */
-static void
-schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
-
-
 /**
  * Function that will schedule the job that will try
  * to connect us again to the client.
@@ -207,7 +197,7 @@ schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
  * @param h transport service to reconnect
  */
 static void
-disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
+disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
 
 
 /**
@@ -218,7 +208,7 @@ disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
  * @return NULL if no such peer entry exists
  */
 static struct Neighbour *
-neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
+neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
                 const struct GNUNET_PeerIdentity *peer)
 {
   return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
@@ -236,18 +226,23 @@ static void
 notify_excess_cb (void *cls)
 {
   struct Neighbour *n = cls;
-  struct GNUNET_TRANSPORT_Handle *h = n->h;
+  struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Notifying CORE that more bandwidth is available for %s\n",
+       GNUNET_i2s (&n->id));
 
   if (NULL != h->neb_cb)
     h->neb_cb (h->cls,
-               &n->id);
+               &n->id,
+               n->handlers_cls);
 }
 
 
 /**
  * Iterator over hash map entries, for deleting state of a neighbour.
  *
- * @param cls the `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param key peer identity
  * @param value value in the hash map, the neighbour entry to delete
  * @return #GNUNET_YES if we should continue to
@@ -259,7 +254,7 @@ neighbour_delete (void *cls,
                  const struct GNUNET_PeerIdentity *key,
                   void *value)
 {
-  struct GNUNET_TRANSPORT_Handle *handle = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
   struct Neighbour *n = value;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -275,12 +270,12 @@ neighbour_delete (void *cls,
     GNUNET_SCHEDULER_cancel (n->timeout_task);
     n->timeout_task = NULL;
   }
-  GNUNET_MQ_destroy (n->mq);
   if (NULL != n->env)
   {
     GNUNET_MQ_send_cancel (n->env);
     n->env = NULL;
   }
+  GNUNET_MQ_destroy (n->mq);
   GNUNET_assert (NULL == n->mq);
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
@@ -297,14 +292,14 @@ neighbour_delete (void *cls,
  * the message queue.
  * Not every message queue implementation supports an error handler.
  *
- * @param cls closure with the `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param error error code
  */
 static void
 mq_error_handler (void *cls,
                   enum GNUNET_MQ_Error error)
 {
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Error receiving from transport service, disconnecting temporarily.\n");
@@ -315,7 +310,7 @@ mq_error_handler (void *cls,
 /**
  * Function we use for checking incoming HELLO messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param msg message received
  * @return #GNUNET_OK if message is well-formed
  */
@@ -339,7 +334,7 @@ check_hello (void *cls,
 /**
  * Function we use for handling incoming HELLO messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param msg message received
  */
 static void
@@ -350,6 +345,25 @@ handle_hello (void *cls,
 }
 
 
+/**
+ * A message from the handler's message queue to a neighbour was
+ * transmitted.  Now trigger (possibly delayed) notification of the
+ * neighbour's message queue that we are done and thus ready for
+ * the next message.
+ *
+ * @param cls the `struct Neighbour` where the message was sent
+ */
+static void
+notify_send_done_fin (void *cls)
+{
+  struct Neighbour *n = cls;
+
+  n->timeout_task = NULL;
+  n->is_ready = GNUNET_YES;
+  GNUNET_MQ_impl_send_continue (n->mq);
+}
+
+
 /**
  * A message from the handler's message queue to a neighbour was
  * transmitted.  Now trigger (possibly delayed) notification of the
@@ -365,24 +379,26 @@ notify_send_done (void *cls)
   struct GNUNET_TIME_Relative delay;
 
   n->timeout_task = NULL;
-  if (NULL != env)
+  if (NULL != n->env)
   {
     GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
                                       n->env_size + n->traffic_overhead);
-    n->traffic_overhead = 0;
     n->env = NULL;
+    n->traffic_overhead = 0;
   }
   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
                                               128);
   if (0 == delay.rel_value_us)
   {
     n->is_ready = GNUNET_YES;
-    GNUNET_MQ_impl_send_continue (mq);
+    GNUNET_MQ_impl_send_continue (n->mq);
     return;
   }
+  GNUNET_MQ_impl_send_in_flight (n->mq);
   /* cannot send even a small message without violating
-     quota, wait a before notifying MQ */
-  n->timeout_task = GNUNET_SCHEDULER_add_delayed (&notify_send_done,
+     quota, wait a before allowing MQ to send next message */
+  n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
+                                                  &notify_send_done_fin,
                                                   n);
 }
 
@@ -405,33 +421,35 @@ mq_send_impl (struct GNUNET_MQ_Handle *mq,
   struct Neighbour *n = impl_state;
   struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
   struct OutboundMessage *obm;
-  struct GNUNET_MQ_Envelope *env;
   uint16_t msize;
 
   GNUNET_assert (GNUNET_YES == n->is_ready);
   msize = ntohs (msg->size);
-  if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*obm))
+  if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*obm))
   {
     GNUNET_break (0);
     GNUNET_MQ_impl_send_continue (mq);
     return;
   }
+  GNUNET_assert (NULL == n->env);
   n->env = GNUNET_MQ_msg_nested_mh (obm,
                                     GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
                                     msg);
   obm->reserved = htonl (0);
-  obm->timeout =
-    GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
-                               (th->timeout));
+  obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
   obm->peer = n->id;
   GNUNET_assert (NULL == n->timeout_task);
   n->is_ready = GNUNET_NO;
   n->env_size = ntohs (msg->size);
-  GNUNET_MQ_notify_sent (env,
+  GNUNET_MQ_notify_sent (n->env,
                          &notify_send_done,
                          n);
   GNUNET_MQ_send (h->mq,
-                  env);
+                  n->env);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Queued message of type %u for neighbour `%s'.\n",
+       ntohs (msg->type),
+       GNUNET_i2s (&n->id));
 }
 
 
@@ -489,12 +507,35 @@ static void
 peer_mq_error_handler (void *cls,
                        enum GNUNET_MQ_Error error)
 {
-  struct Neighbour *n = cls;
+  /* struct Neighbour *n = cls; */
 
   GNUNET_break_op (0);
 }
 
 
+/**
+ * The outbound quota has changed in a way that may require
+ * us to reset the timeout.  Update the timeout.
+ *
+ * @param cls the `struct Neighbour` for which the timeout changed
+ */
+static void
+outbound_bw_tracker_update (void *cls)
+{
+  struct Neighbour *n = cls;
+  struct GNUNET_TIME_Relative delay;
+
+  if (NULL == n->timeout_task)
+    return;
+  delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
+                                              128);
+  GNUNET_SCHEDULER_cancel (n->timeout_task);
+  n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
+                                                  &notify_send_done,
+                                                  n);
+}
+
+
 /**
  * Function we use for handling incoming connect messages.
  *
@@ -505,7 +546,7 @@ static void
 handle_connect (void *cls,
                 const struct ConnectInfoMessage *cim)
 {
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
   struct Neighbour *n;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -560,14 +601,14 @@ handle_connect (void *cls,
 /**
  * Function we use for handling incoming disconnect messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param dim message received
  */
 static void
 handle_disconnect (void *cls,
                    const struct DisconnectInfoMessage *dim)
 {
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
   struct Neighbour *n;
 
   GNUNET_break (ntohl (dim->reserved) == 0);
@@ -591,14 +632,14 @@ handle_disconnect (void *cls,
 /**
  * Function we use for handling incoming send-ok messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param okm message received
  */
 static void
 handle_send_ok (void *cls,
                 const struct SendOkMessage *okm)
 {
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
   struct Neighbour *n;
   uint32_t bytes_msg;
   uint32_t bytes_physical;
@@ -633,7 +674,7 @@ handle_send_ok (void *cls,
 /**
  * Function we use for checking incoming "inbound" messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param im message received
  */
 static int
@@ -643,15 +684,14 @@ check_recv (void *cls,
   const struct GNUNET_MessageHeader *imm;
   uint16_t size;
 
-  size = ntohs (im->header.size);
-  if (size <
-      sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
+  size = ntohs (im->header.size) - sizeof (*im);
+  if (size < sizeof (struct GNUNET_MessageHeader))
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
   imm = (const struct GNUNET_MessageHeader *) &im[1];
-  if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
+  if (ntohs (imm->size) != size)
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
@@ -663,14 +703,14 @@ check_recv (void *cls,
 /**
  * Function we use for handling incoming messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param im message received
  */
 static void
 handle_recv (void *cls,
              const struct InboundMessage *im)
 {
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
   const struct GNUNET_MessageHeader *imm
     = (const struct GNUNET_MessageHeader *) &im[1];
   struct Neighbour *n;
@@ -695,17 +735,18 @@ handle_recv (void *cls,
 /**
  * Function we use for handling incoming set quota messages.
  *
- * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
+ * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  * @param msg message received
  */
 static void
 handle_set_quota (void *cls,
                   const struct QuotaSetMessage *qm)
 {
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
   struct Neighbour *n;
 
-  n = neighbour_find (h, &qm->peer);
+  n = neighbour_find (h,
+                     &qm->peer);
   if (NULL == n)
   {
     GNUNET_break (0);
@@ -729,32 +770,32 @@ handle_set_quota (void *cls,
 static void
 reconnect (void *cls)
 {
-  GNUNET_MQ_hd_var_size (hello,
-                         GNUNET_MESSAGE_TYPE_HELLO,
-                         struct GNUNET_MessageHeader);
-  GNUNET_MQ_hd_fixed_size (connect,
-                           GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
-                           struct ConnectInfoMessage);
-  GNUNET_MQ_hd_fixed_size (disconnect,
-                           GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
-                           struct DisconnectInfoMessage);
-  GNUNET_MQ_hd_fixed_size (send_ok,
-                           GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
-                           struct SendOkMessage);
-  GNUNET_MQ_hd_var_size (recv,
-                         GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
-                         struct InboundMessage);
-  GNUNET_MQ_hd_fixed_size (set_quota,
-                           GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
-                           struct QuotaSetMessage);
-  struct GNUNET_TRANSPORT_Handle *h = cls;
+  struct GNUNET_TRANSPORT_CoreHandle *h = cls;
   struct GNUNET_MQ_MessageHandler handlers[] = {
-    make_hello_handler (h),
-    make_connect_handler (h),
-    make_disconnect_handler (h),
-    make_send_ok_handler (h),
-    make_recv_handler (h),
-    make_set_quota_handler (h),
+    GNUNET_MQ_hd_var_size (hello,
+                           GNUNET_MESSAGE_TYPE_HELLO,
+                           struct GNUNET_MessageHeader,
+                           h),
+    GNUNET_MQ_hd_fixed_size (connect,
+                             GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
+                             struct ConnectInfoMessage,
+                             h),
+    GNUNET_MQ_hd_fixed_size (disconnect,
+                             GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
+                             struct DisconnectInfoMessage,
+                             h),
+    GNUNET_MQ_hd_fixed_size (send_ok,
+                             GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
+                             struct SendOkMessage,
+                             h),
+    GNUNET_MQ_hd_var_size (recv,
+                           GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
+                           struct InboundMessage,
+                           h),
+    GNUNET_MQ_hd_fixed_size (set_quota,
+                             GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
+                             struct QuotaSetMessage,
+                             h),
     GNUNET_MQ_handler_end ()
   };
   struct GNUNET_MQ_Envelope *env;
@@ -765,7 +806,7 @@ reconnect (void *cls)
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Connecting to transport service.\n");
   GNUNET_assert (NULL == h->mq);
-  h->mq = GNUNET_CLIENT_connecT (h->cfg,
+  h->mq = GNUNET_CLIENT_connect (h->cfg,
                                  "transport",
                                  handlers,
                                  &mq_error_handler,
@@ -777,7 +818,7 @@ reconnect (void *cls)
   options = 0;
   if (h->check_self)
     options |= 1;
-  if (NULL != h->rec)
+  if (NULL != h->handlers)
     options |= 2;
   s->options = htonl (options);
   s->self = h->self;
@@ -793,22 +834,17 @@ reconnect (void *cls)
  * @param h transport service to reconnect
  */
 static void
-disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
+disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
 {
   GNUNET_assert (NULL == h->reconnect_task);
-  if (NULL != h->mq)
-  {
-    GNUNET_MQ_destroy (h->mq);
-    h->mq = NULL;
-  }
   /* Forget about all neighbours that we used to be connected to */
   GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
                                          &neighbour_delete,
                                          h);
-  if (NULL != h->quota_task)
+  if (NULL != h->mq)
   {
-    GNUNET_SCHEDULER_cancel (h->quota_task);
-    h->quota_task = NULL;
+    GNUNET_MQ_destroy (h->mq);
+    h->mq = NULL;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Scheduling task to reconnect to transport service in %s.\n",
@@ -830,7 +866,7 @@ disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
  * @return NULL if disconnected, otherwise message queue for @a peer
  */
 struct GNUNET_MQ_Handle *
-GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_Handle *handle,
+GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
                               const struct GNUNET_PeerIdentity *peer)
 {
   struct Neighbour *n;
@@ -858,13 +894,13 @@ GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_Handle *handle,
  * @return NULL on error
  */
 struct GNUNET_TRANSPORT_CoreHandle *
-GNUNET_TRANSPORT_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                          const struct GNUNET_PeerIdentity *self,
-                          const struct GNUNET_MQ_MessageHandler *handlers,
-                          void *cls,
-                          GNUNET_TRANSPORT_NotifyConnect nc,
-                          GNUNET_TRANSPORT_NotifyDisconnect nd,
-                          GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
+GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                              const struct GNUNET_PeerIdentity *self,
+                              const struct GNUNET_MQ_MessageHandler *handlers,
+                              void *cls,
+                              GNUNET_TRANSPORT_NotifyConnecT nc,
+                              GNUNET_TRANSPORT_NotifyDisconnecT nd,
+                              GNUNET_TRANSPORT_NotifyExcessBandwidtH neb)
 {
   struct GNUNET_TRANSPORT_CoreHandle *h;
   unsigned int i;
@@ -881,22 +917,23 @@ GNUNET_TRANSPORT_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
   h->nd_cb = nd;
   h->neb_cb = neb;
   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Connecting to transport service.\n");
-  reconnect (h);
-  if (NULL == h->mq)
-  {
-    GNUNET_free (h);
-    return NULL;
-  }
   if (NULL != handlers)
   {
     for (i=0;NULL != handlers[i].cb; i++) ;
     h->handlers = GNUNET_new_array (i + 1,
                                     struct GNUNET_MQ_MessageHandler);
     GNUNET_memcpy (h->handlers,
-           handlers,
-           i * sizeof (struct GNUNET_MQ_MessageHandler));
+                  handlers,
+                  i * sizeof (struct GNUNET_MQ_MessageHandler));
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Connecting to transport service\n");
+  reconnect (h);
+  if (NULL == h->mq)
+  {
+    GNUNET_free_non_null (h->handlers);
+    GNUNET_free (h);
+    return NULL;
   }
   h->neighbours =
     GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
@@ -908,7 +945,7 @@ GNUNET_TRANSPORT_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
 /**
  * Disconnect from the transport service.
  *
- * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
+ * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
  */
 void
 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
@@ -926,11 +963,6 @@ GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
   }
   GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
   handle->neighbours = NULL;
-  if (NULL != handle->quota_task)
-  {
-    GNUNET_SCHEDULER_cancel (handle->quota_task);
-    handle->quota_task = NULL;
-  }
   GNUNET_free_non_null (handle->handlers);
   handle->handlers = NULL;
   GNUNET_free (handle);