- fix doxygen
[oweals/gnunet.git] / src / ats / ats_api_performance.c
index cf948da4845e7ec1d8c8a22aae934a20479a8854..848c7ec4dc59c5fa09bf9bb5ad1b3403e4ea190f 100644 (file)
@@ -37,12 +37,12 @@ struct PendingMessage
 
   /**
    * Kept in a DLL.
-   */ 
+   */
   struct PendingMessage *next;
 
   /**
    * Kept in a DLL.
-   */ 
+   */
   struct PendingMessage *prev;
 
   /**
@@ -52,7 +52,7 @@ struct PendingMessage
 
   /**
    * Is this the 'ATS_START' message?
-   */ 
+   */
   int is_init;
 };
 
@@ -65,19 +65,19 @@ struct GNUNET_ATS_ReservationContext
 
   /**
    * Kept in a DLL.
-   */ 
+   */
   struct GNUNET_ATS_ReservationContext *next;
 
   /**
    * Kept in a DLL.
-   */ 
+   */
   struct GNUNET_ATS_ReservationContext *prev;
 
   /**
    * Target peer.
    */
   struct GNUNET_PeerIdentity peer;
-                           
+
   /**
    * Desired reservation
    */
@@ -86,12 +86,12 @@ struct GNUNET_ATS_ReservationContext
   /**
    * Function to call on result.
    */
-  GNUNET_ATS_ReservationCallback info;
+  GNUNET_ATS_ReservationCallback rcb;
 
   /**
-   * Closure for 'info'
+   * Closure for 'rcb'
    */
-  void *info_cls;
+  void *rcb_cls;
 
   /**
    * Do we need to undo this reservation if it succeeded?  Set to
@@ -110,7 +110,7 @@ struct GNUNET_ATS_ReservationContext
  */
 struct GNUNET_ATS_PerformanceHandle
 {
+
   /**
    * Our configuration.
    */
@@ -120,7 +120,7 @@ struct GNUNET_ATS_PerformanceHandle
    * Callback to invoke on performance changes.
    */
   GNUNET_ATS_PeerInformationCallback infocb;
-  
+
   /**
    * Closure for 'infocb'.
    */
@@ -156,23 +156,44 @@ struct GNUNET_ATS_PerformanceHandle
    */
   struct GNUNET_CLIENT_TransmitHandle *th;
 
+  /**
+   * Task to trigger reconnect.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier task;
+
 };
 
 
 /**
  * Re-establish the connection to the ATS service.
  *
- * @param sh handle to use to re-connect.
+ * @param ph handle to use to re-connect.
  */
 static void
 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
 
 
+/**
+ * Re-establish the connection to the ATS service.
+ *
+ * @param cls handle to use to re-connect.
+ * @param tc scheduler context
+ */
+static void
+reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_ATS_PerformanceHandle *ph = cls;
+
+  ph->task = GNUNET_SCHEDULER_NO_TASK;
+  reconnect (ph);
+}
+
+
 /**
  * Transmit messages from the message queue to the service
  * (if there are any, and if we are not already trying).
  *
- * @param sh handle to use
+ * @param ph handle to use
  */
 static void
 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
@@ -187,9 +208,7 @@ do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
  * @return number of bytes copied into buf
  */
 static size_t
-transmit_message_to_ats (void *cls,
-                        size_t size,
-                        void *buf)
+transmit_message_to_ats (void *cls, size_t size, void *buf)
 {
   struct GNUNET_ATS_PerformanceHandle *ph = cls;
   struct PendingMessage *p;
@@ -199,14 +218,12 @@ transmit_message_to_ats (void *cls,
   ph->th = NULL;
   ret = 0;
   cbuf = buf;
-  while ( (NULL != (p = ph->pending_head)) &&
-         (p->size <= size) )
+  while ((NULL != (p = ph->pending_head)) && (p->size <= size))
   {
-    memcpy (&cbuf[ret], &p[1], p->size);    
+    memcpy (&cbuf[ret], &p[1], p->size);
     ret += p->size;
-    GNUNET_CONTAINER_DLL_remove (ph->pending_head,
-                                ph->pending_tail,
-                                p);
+    size -= p->size;
+    GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
     GNUNET_free (p);
   }
   do_transmit (ph);
@@ -229,11 +246,120 @@ do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
     return;
   if (NULL == (p = ph->pending_head))
     return;
-  ph->th = GNUNET_CLIENT_notify_transmit_ready (ph->client,
-                                               p->size,
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               GNUNET_YES,
-                                               &transmit_message_to_ats, ph);
+  if (NULL == ph->client)
+    return;                     /* currently reconnecting */
+  ph->th =
+      GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_YES, &transmit_message_to_ats,
+                                           ph);
+}
+
+
+/**
+ * We received a peer information message.  Validate and process it.
+ *
+ * @param ph our context with the callback
+ * @param msg the message
+ * @return GNUNET_OK if the message was well-formed
+ */
+static int
+process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
+                    const struct GNUNET_MessageHeader *msg)
+{
+  const struct PeerInformationMessage *pi;
+  const struct GNUNET_ATS_Information *atsi;
+  const char *plugin_address;
+  const char *plugin_name;
+  struct GNUNET_HELLO_Address address;
+  uint16_t plugin_address_length;
+  uint16_t plugin_name_length;
+  uint32_t ats_count;
+
+  if (ph->infocb == NULL)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  pi = (const struct PeerInformationMessage *) msg;
+  ats_count = ntohl (pi->ats_count);
+  plugin_address_length = ntohs (pi->address_length);
+  plugin_name_length = ntohs (pi->plugin_name_length);
+  atsi = (const struct GNUNET_ATS_Information *) &pi[1];
+  plugin_address = (const char *) &atsi[ats_count];
+  plugin_name = &plugin_address[plugin_address_length];
+  if ((plugin_address_length + plugin_name_length +
+       ats_count * sizeof (struct GNUNET_ATS_Information) +
+       sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
+      (ats_count >
+       GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
+      || (plugin_name[plugin_name_length - 1] != '\0'))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  address.peer = pi->peer;
+  address.address = plugin_address;
+  address.address_length = plugin_address_length;
+  address.transport_name = plugin_name;
+  ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
+              atsi, ats_count);
+  return GNUNET_OK;
+}
+
+
+/**
+ * We received a reservation result message.  Validate and process it.
+ *
+ * @param ph our context with the callback
+ * @param msg the message
+ * @return GNUNET_OK if the message was well-formed
+ */
+static int
+process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
+                    const struct GNUNET_MessageHeader *msg)
+{
+  const struct ReservationResultMessage *rr;
+  struct GNUNET_ATS_ReservationContext *rc;
+  int32_t amount;
+
+  if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  rr = (const struct ReservationResultMessage *) msg;
+  amount = ntohl (rr->amount);
+  rc = ph->reservation_head;
+  if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
+  if ((amount == 0) || (rc->rcb != NULL))
+  {
+    /* tell client if not cancelled */
+    if (rc->rcb != NULL)
+      rc->rcb (rc->rcb_cls, &rr->peer, amount,
+               GNUNET_TIME_relative_ntoh (rr->res_delay));
+    GNUNET_free (rc);
+    return GNUNET_OK;
+  }
+  /* amount non-zero, but client cancelled, consider undo! */
+  if (GNUNET_YES != rc->undo)
+  {
+    GNUNET_free (rc);
+    return GNUNET_OK;           /* do not try to undo failed undos or negative amounts */
+  }
+  GNUNET_free (rc);
+  (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
+  return GNUNET_OK;
 }
 
 
@@ -245,31 +371,35 @@ do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
  * @param msg message received, NULL on timeout or fatal error
  */
 static void
-process_ats_message (void *cls,
-                    const struct GNUNET_MessageHeader *msg)
+process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_ATS_PerformanceHandle *ph = cls;
 
-  if (NULL == msg) 
-  {
-    GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
-    ph->client = NULL;
-    reconnect (ph);
-    return;
-  }
+  if (NULL == msg)
+    goto reconnect;
   switch (ntohs (msg->type))
   {
-    // FIXME
+  case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
+    if (GNUNET_OK != process_pi_message (ph, msg))
+      goto reconnect;
+    break;
+  case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
+    if (GNUNET_OK != process_rr_message (ph, msg))
+      goto reconnect;
+    break;
   default:
     GNUNET_break (0);
-    GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
-    ph->client = NULL;
-    reconnect (ph);
-    return;
+    goto reconnect;
   }
-  GNUNET_CLIENT_receive (ph->client,
-                        &process_ats_message, ph,
-                        GNUNET_TIME_UNIT_FOREVER_REL);
+  GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
+  return;
+reconnect:
+  GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
+  ph->client = NULL;
+  ph->task =
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
+                                    ph);
 }
 
 
@@ -287,25 +417,22 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
   GNUNET_assert (NULL == ph->client);
   ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
   GNUNET_assert (NULL != ph->client);
-  GNUNET_CLIENT_receive (ph->client,
-                        &process_ats_message, ph,
-                        GNUNET_TIME_UNIT_FOREVER_REL);
-  if ( (NULL == (p = ph->pending_head)) ||
-       (GNUNET_YES != p->is_init) )
+  GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
+  if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
   {
     p = GNUNET_malloc (sizeof (struct PendingMessage) +
-                      sizeof (struct ClientStartMessage));
+                       sizeof (struct ClientStartMessage));
     p->size = sizeof (struct ClientStartMessage);
     p->is_init = GNUNET_YES;
     init = (struct ClientStartMessage *) &p[1];
     init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
     init->header.size = htons (sizeof (struct ClientStartMessage));
-    init->start_flag = htonl ((ph->infocb == NULL) 
-                             ? START_FLAG_PERFORMANCE_NO_PIC 
-                             : START_FLAG_PERFORMANCE_WITH_PIC);
-    GNUNET_CONTAINER_DLL_insert (ph->pending_head,
-                                ph->pending_tail,
-                                p);
+    init->start_flag =
+        htonl ((ph->infocb ==
+                NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
+               START_FLAG_PERFORMANCE_WITH_PIC);
+    GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
   }
   do_transmit (ph);
 }
@@ -322,8 +449,8 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
  */
 struct GNUNET_ATS_PerformanceHandle *
 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                            GNUNET_ATS_PeerInformationCallback infocb,
-                            void *infocb_cls)
+                             GNUNET_ATS_PeerInformationCallback infocb,
+                             void *infocb_cls)
 {
   struct GNUNET_ATS_PerformanceHandle *ph;
 
@@ -346,23 +473,29 @@ GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
 {
   struct PendingMessage *p;
   struct GNUNET_ATS_ReservationContext *rc;
-  
+
   while (NULL != (p = ph->pending_head))
   {
-    GNUNET_CONTAINER_DLL_remove (ph->pending_head,
-                                ph->pending_tail,
-                                p);
+    GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
     GNUNET_free (p);
   }
   while (NULL != (rc = ph->reservation_head))
   {
-    GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
-                                ph->reservation_tail,
-                                rc);
-    GNUNET_break (NULL == rc->info);
-    GNUNET_free (p);
-  }  
-  GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
+    GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
+                                 rc);
+    GNUNET_break (NULL == rc->rcb);
+    GNUNET_free (rc);
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != ph->task)
+  {
+    GNUNET_SCHEDULER_cancel (ph->task);
+    ph->task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  if (NULL != ph->client)
+  {
+    GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
+    ph->client = NULL;
+  }
   GNUNET_free (ph);
 }
 
@@ -376,17 +509,16 @@ GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
  * @param peer identifies the peer
  * @param amount reserve N bytes for receiving, negative
  *                amounts can be used to undo a (recent) reservation;
- * @param info function to call with the resulting reservation information
- * @param info_cls closure for info
+ * @param rcb function to call with the resulting reservation information
+ * @param rcb_cls closure for info
  * @return NULL on error
  * @deprecated will be replaced soon
  */
 struct GNUNET_ATS_ReservationContext *
 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
-                             const struct GNUNET_PeerIdentity *peer,
-                             int32_t amount, 
-                             GNUNET_ATS_ReservationCallback info, 
-                             void *info_cls)
+                              const struct GNUNET_PeerIdentity *peer,
+                              int32_t amount,
+                              GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
 {
   struct GNUNET_ATS_ReservationContext *rc;
   struct PendingMessage *p;
@@ -395,24 +527,24 @@ GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
   rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
   rc->size = amount;
   rc->peer = *peer;
-  rc->info = info;
-  rc->info_cls = info_cls;
-  GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
-                                   ph->reservation_tail,
-                                   rc);
-  
-  p = GNUNET_malloc (sizeof (struct PendingMessage) + 
-                    sizeof (struct ReservationRequestMessage));
+  rc->rcb = rcb;
+  rc->rcb_cls = rcb_cls;
+  if ((rcb != NULL) && (amount > 0))
+    rc->undo = GNUNET_YES;
+  GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
+                                    rc);
+
+  p = GNUNET_malloc (sizeof (struct PendingMessage) +
+                     sizeof (struct ReservationRequestMessage));
   p->size = sizeof (struct ReservationRequestMessage);
   p->is_init = GNUNET_NO;
-  m = (struct ReservationRequestMessage*) &p[1];
-  m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
+  m = (struct ReservationRequestMessage *) &p[1];
+  m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
   m->header.size = htons (sizeof (struct ReservationRequestMessage));
   m->amount = htonl (amount);
   m->peer = *peer;
-  GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
-                                   ph->pending_tail,
-                                   p);
+  GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
+  do_transmit (ph);
   return rc;
 }
 
@@ -423,25 +555,23 @@ GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
  */
 void
-GNUNET_ATS_reserve_bandwidth_cancel (struct
-                                    GNUNET_ATS_ReservationContext *rc)
+GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
 {
-  rc->info = NULL;
+  rc->rcb = NULL;
 }
 
 
 /**
  * Change preferences for the given peer. Preference changes are forgotten if peers
  * disconnect.
- * 
+ *
  * @param ph performance handle
  * @param peer identifies the peer
  * @param ... 0-terminated specification of the desired changes
  */
 void
 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
-                             const struct GNUNET_PeerIdentity *peer,
-                             ...)
+                              const struct GNUNET_PeerIdentity *peer, ...)
 {
   struct PendingMessage *p;
   struct ChangePreferenceMessage *m;
@@ -453,59 +583,63 @@ GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
 
   count = 0;
   va_start (ap, peer);
-  while (GNUNET_ATS_PREFERENCE_END != (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
+  while (GNUNET_ATS_PREFERENCE_END !=
+         (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
   {
     switch (kind)
     {
     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
       count++;
       (void) va_arg (ap, double);
+
       break;
     case GNUNET_ATS_PREFERENCE_LATENCY:
       count++;
       (void) va_arg (ap, double);
+
       break;
     default:
-      GNUNET_assert (0);      
+      GNUNET_assert (0);
     }
   }
   va_end (ap);
-  msize = count * sizeof (struct PreferenceInformation) +
-    sizeof (struct ChangePreferenceMessage);
-  p = GNUNET_malloc (sizeof (struct PendingMessage) + 
-                    msize);
+  msize =
+      count * sizeof (struct PreferenceInformation) +
+      sizeof (struct ChangePreferenceMessage);
+  p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
   p->size = msize;
   p->is_init = GNUNET_NO;
-  m = (struct ChangePreferenceMessage*) &p[1];
-  m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
+  m = (struct ChangePreferenceMessage *) &p[1];
+  m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
   m->header.size = htons (msize);
   m->num_preferences = htonl (count);
   m->peer = *peer;
-  pi = (struct PreferenceInformation*) &m[1];
+  pi = (struct PreferenceInformation *) &m[1];
   count = 0;
   va_start (ap, peer);
-  while (GNUNET_ATS_PREFERENCE_END != (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
+  while (GNUNET_ATS_PREFERENCE_END !=
+         (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
   {
     pi[count].preference_kind = htonl (kind);
     switch (kind)
     {
     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
       pi[count].preference_value = (float) va_arg (ap, double);
+
       count++;
       break;
     case GNUNET_ATS_PREFERENCE_LATENCY:
       pi[count].preference_value = (float) va_arg (ap, double);
+
       count++;
       break;
     default:
-      GNUNET_assert (0);      
+      GNUNET_assert (0);
     }
   }
   va_end (ap);
-  GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
-                                   ph->pending_tail,
-                                   p);
+  GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
+  do_transmit (ph);
 }
 
 /* end of ats_api_performance.c */
-