Avoid trying to disconnect a neighbour twice
[oweals/gnunet.git] / src / transport / transport_api.c
index a2649db8f0b7fcf5d4c4ff893daaa92532f8c222..6e47b269ba9217fdb67591b97c0dddef8a939a8d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -355,7 +355,7 @@ struct GNUNET_TRANSPORT_Handle
    * The current HELLO message for this peer.  Updated
    * whenever transports change their addresses.
    */
-  struct GNUNET_HELLO_Message *my_hello;
+  struct GNUNET_MessageHeader *my_hello;
 
   /**
    * My client connection to the transport service.
@@ -655,7 +655,8 @@ demultiplexer (void *cls,
       break;
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
+         "Receiving (my own) HELLO message (%u bytes), I am `%4s'.\n",
+         (unsigned int) size,
          GNUNET_i2s (&me));
     GNUNET_free_non_null (h->my_hello);
     h->my_hello = NULL;
@@ -664,14 +665,13 @@ demultiplexer (void *cls,
       GNUNET_break (0);
       break;
     }
-    h->my_hello = GNUNET_malloc (size);
-    memcpy (h->my_hello, msg, size);
+    h->my_hello = GNUNET_copy_message (msg);
     hwl = h->hwl_head;
     while (NULL != hwl)
     {
       next_hwl = hwl->next;
       hwl->rec (hwl->rec_cls,
-                (const struct GNUNET_MessageHeader *) h->my_hello);
+                h->my_hello);
       hwl = next_hwl;
     }
     break;
@@ -689,8 +689,8 @@ demultiplexer (void *cls,
       break;
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving `%s' message for `%4s'.\n",
-         "CONNECT", GNUNET_i2s (&cim->id));
+         "Receiving CONNECT message for `%4s'.\n",
+         GNUNET_i2s (&cim->id));
     n = neighbour_find (h, &cim->id);
     if (NULL != n)
     {
@@ -699,8 +699,7 @@ demultiplexer (void *cls,
     }
     n = neighbour_add (h, &cim->id);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving `%s' message for `%4s' with quota %u\n",
-         "CONNECT",
+         "Receiving CONNECT message for `%4s' with quota %u\n",
          GNUNET_i2s (&cim->id),
          ntohl (cim->quota_out.value__));
     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
@@ -717,8 +716,8 @@ demultiplexer (void *cls,
     dim = (const struct DisconnectInfoMessage *) msg;
     GNUNET_break (ntohl (dim->reserved) == 0);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving `%s' message for `%4s'.\n",
-         "DISCONNECT", GNUNET_i2s (&dim->peer));
+         "Receiving DISCONNECT message for `%4s'.\n",
+         GNUNET_i2s (&dim->peer));
     n = neighbour_find (h, &dim->peer);
     if (NULL == n)
     {
@@ -766,9 +765,6 @@ demultiplexer (void *cls,
     }
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving `%s' message.\n",
-         "RECV");
     if (size <
         sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
     {
@@ -795,8 +791,6 @@ demultiplexer (void *cls,
       h->rec (h->cls, &im->peer, imm);
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving `%s' message.\n", "SET_QUOTA");
     if (size != sizeof (struct QuotaSetMessage))
     {
       GNUNET_break (0);
@@ -807,16 +801,18 @@ demultiplexer (void *cls,
     if (NULL == n)
       break;
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving `%s' message for `%4s' with quota %u\n",
-         "SET_QUOTA",
+         "Receiving SET_QUOTA message for `%4s' with quota %u\n",
          GNUNET_i2s (&qm->peer),
          ntohl (qm->quota.value__));
-    GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
+    GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
+                                           qm->quota);
     break;
   default:
     LOG (GNUNET_ERROR_TYPE_ERROR,
          _("Received unexpected message of type %u in %s:%u\n"),
-         ntohs (msg->type), __FILE__, __LINE__);
+         ntohs (msg->type),
+         __FILE__,
+         __LINE__);
     GNUNET_break (0);
     break;
   }
@@ -880,11 +876,14 @@ transport_notify_ready (void *cls, size_t size, void *buf)
   /* first send control messages */
   while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
   {
-    GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
+    GNUNET_CONTAINER_DLL_remove (h->control_head,
+                                 h->control_tail,
+                                 th);
     nret = th->notify (th->notify_cls, size, &cbuf[ret]);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Added %u bytes of control message at %u\n",
-         nret, ret);
+         nret,
+         ret);
     GNUNET_free (th);
     ret += nret;
     size -= nret;
@@ -1055,7 +1054,8 @@ schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
  * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
  */
 static struct GNUNET_TRANSPORT_TransmitHandle *
-schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
+schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
+                           size_t size,
                            GNUNET_TRANSPORT_TransmitReadyNotify notify,
                            void *notify_cls)
 {
@@ -1093,13 +1093,11 @@ send_start (void *cls, size_t size, void *buf)
   {
     /* Can only be shutdown, just give up */
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Shutdown while trying to transmit `%s' request.\n",
-         "START");
+         "Shutdown while trying to transmit START request.\n");
     return 0;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Transmitting `%s' request.\n",
-       "START");
+       "Transmitting START request.\n");
   GNUNET_assert (size >= sizeof (struct StartMessage));
   s.header.size = htons (sizeof (struct StartMessage));
   s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
@@ -1479,7 +1477,9 @@ send_hello (void *cls, size_t size, void *buf)
  * @return number of bytes copied to @a buf
  */
 static size_t
-send_metric (void *cls, size_t size, void *buf)
+send_metric (void *cls,
+             size_t size,
+             void *buf)
 {
   struct TrafficMetricMessage *msg = cls;
   uint16_t ssize;
@@ -1487,14 +1487,12 @@ send_metric (void *cls, size_t size, void *buf)
   if (NULL == buf)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Timeout while trying to transmit `%s' request.\n",
-         "TRAFFIC_METRIC");
+         "Timeout while trying to transmit TRAFFIC_METRIC request.\n");
     GNUNET_free (msg);
     return 0;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Transmitting `%s' request.\n",
-       "TRAFFIC_METRIC");
+       "Transmitting TRAFFIC_METRIC request.\n");
   ssize = ntohs (msg->header.size);
   GNUNET_assert (size >= ssize);
   memcpy (buf, msg, ssize);
@@ -1508,56 +1506,35 @@ send_metric (void *cls, size_t size, void *buf)
  *
  * @param handle transport handle
  * @param peer the peer to set the metric for
- * @param inbound set inbound direction (#GNUNET_YES or #GNUNET_NO)
- * @param outbound set outbound direction (#GNUNET_YES or #GNUNET_NO)
- * @param ats the metric as ATS information
- * @param ats_count the number of metrics
- *
- * Supported ATS values:
- * #GNUNET_ATS_QUALITY_NET_DELAY  (value in ms)
- * #GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
+ * @param prop the performance metrics to set
+ * @param delay_in inbound delay to introduce
+ * @param delay_out outbound delay to introduce
  *
- * Example:
- * To enforce a delay of 10 ms for peer p1 in sending direction use:
- * <code>
- * struct GNUNET_ATS_Information ats;
- * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
- * ats.value = ntohl (10);
- * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
- * </code>
- * Note:
- * Delay restrictions in receiving direction will be enforced with
- * 1 message delay.
+ * Note: Delay restrictions in receiving direction will be enforced
+ * with one message delay.
  */
 void
 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
-                                     const struct GNUNET_PeerIdentity *peer,
-                                     int inbound,
-                                     int outbound,
-                                     const struct GNUNET_ATS_Information *ats,
-                                     size_t ats_count)
+                                    const struct GNUNET_PeerIdentity *peer,
+                                    const struct GNUNET_ATS_Properties *prop,
+                                     struct GNUNET_TIME_Relative delay_in,
+                                     struct GNUNET_TIME_Relative delay_out)
 {
   struct TrafficMetricMessage *msg;
 
-  GNUNET_assert ((outbound == GNUNET_YES) || (outbound == GNUNET_NO));
-  GNUNET_assert ((inbound == GNUNET_YES) || (inbound == GNUNET_NO));
-  if ((GNUNET_NO == inbound) && (GNUNET_NO == outbound))
-    return;
-  if (0 == ats_count)
-    return;
-
-  size_t len = sizeof (struct TrafficMetricMessage) +
-    ats_count * sizeof (struct GNUNET_ATS_Information);
-
-  msg = GNUNET_malloc (len);
-  msg->header.size = htons (len);
+  msg = GNUNET_new (struct TrafficMetricMessage);
+  msg->header.size = htons (sizeof (struct TrafficMetricMessage));
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
-  msg->direction = htons (0 + outbound + 2 * inbound);
-  msg->ats_count = htons (ats_count);
-  msg->peer = (*peer);
-  memcpy (&msg[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
-  schedule_control_transmit (handle, len,
-                             &send_metric, msg);
+  msg->reserved = htonl (0);
+  msg->peer = *peer;
+  GNUNET_ATS_properties_hton (&msg->properties,
+                              prop);
+  msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
+  msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
+  schedule_control_transmit (handle,
+                             sizeof (struct TrafficMetricMessage),
+                             &send_metric,
+                             msg);
 }
 
 
@@ -1668,7 +1645,7 @@ call_hello_update_cb_async (void *cls,
   GNUNET_assert (NULL != ghh->notify_task);
   ghh->notify_task = NULL;
   ghh->rec (ghh->rec_cls,
-            (const struct GNUNET_MessageHeader *) ghh->handle->my_hello);
+            ghh->handle->my_hello);
 }
 
 
@@ -1696,7 +1673,7 @@ GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
   hwl->rec_cls = rec_cls;
   hwl->handle = handle;
   GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
-  if (handle->my_hello != NULL)
+  if (NULL != handle->my_hello)
     hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
                                                  hwl);
   return hwl;