-skeletons for transport-ng
[oweals/gnunet.git] / src / ats / ats_api_performance.c
1 /*
2   This file is part of GNUnet.
3   Copyright (C) 2010, 2011, 2016 GNUnet e.V.
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 3, or (at your
8   option) any later version.
9
10   GNUnet is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with GNUnet; see the file COPYING.  If not, write to the
17   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   Boston, MA 02110-1301, USA.
19  */
20 /**
21  * @file ats/ats_api_performance.c
22  * @brief automatic transport selection and outbound bandwidth determination
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "ats.h"
29
30
31 #define LOG(kind,...) GNUNET_log_from(kind, "ats-performance-api", __VA_ARGS__)
32
33
34 /**
35  * Linked list of pending reservations.
36  */
37 struct GNUNET_ATS_ReservationContext
38 {
39
40   /**
41    * Kept in a DLL.
42    */
43   struct GNUNET_ATS_ReservationContext *next;
44
45   /**
46    * Kept in a DLL.
47    */
48   struct GNUNET_ATS_ReservationContext *prev;
49
50   /**
51    * Target peer.
52    */
53   struct GNUNET_PeerIdentity peer;
54
55   /**
56    * Desired reservation
57    */
58   int32_t size;
59
60   /**
61    * Function to call on result.
62    */
63   GNUNET_ATS_ReservationCallback rcb;
64
65   /**
66    * Closure for @e rcb
67    */
68   void *rcb_cls;
69
70   /**
71    * Do we need to undo this reservation if it succeeded?  Set to
72    * #GNUNET_YES if a reservation is cancelled.  (at that point, 'info'
73    * is also set to NULL; however, info will ALSO be NULL for the
74    * reservation context that is created to undo the original request,
75    * so 'info' being NULL cannot be used to check if undo is
76    * required).
77    */
78   int undo;
79 };
80
81
82 /**
83  * Linked list of pending reservations.
84  */
85 struct GNUNET_ATS_AddressListHandle
86 {
87
88   /**
89    * Kept in a DLL.
90    */
91   struct GNUNET_ATS_AddressListHandle *next;
92
93   /**
94    * Kept in a DLL.
95    */
96   struct GNUNET_ATS_AddressListHandle *prev;
97
98   /**
99    * Performance handle
100    */
101   struct GNUNET_ATS_PerformanceHandle *ph;
102
103   /**
104    * Callback
105    */
106   GNUNET_ATS_AddressInformationCallback cb;
107
108   /**
109    * Callback closure for @e cb
110    */
111   void *cb_cls;
112
113   /**
114    * Target peer.
115    */
116   struct GNUNET_PeerIdentity peer;
117
118   /**
119    * Return all or specific peer only
120    */
121   int all_peers;
122
123   /**
124    * Return all or used address only
125    */
126   int all_addresses;
127
128   /**
129    * Request multiplexing
130    */
131   uint32_t id;
132 };
133
134
135 /**
136  * ATS Handle to obtain and/or modify performance information.
137  */
138 struct GNUNET_ATS_PerformanceHandle
139 {
140
141   /**
142    * Our configuration.
143    */
144   const struct GNUNET_CONFIGURATION_Handle *cfg;
145
146   /**
147    * Callback to invoke when an address has performance changes.
148    */
149   GNUNET_ATS_AddressInformationCallback addr_info_cb;
150
151   /**
152    * Closure for @e addr_info_cb.
153    */
154   void *addr_info_cb_cls;
155
156   /**
157    * Connection to ATS service.
158    */
159   struct GNUNET_MQ_Handle *mq;
160
161   /**
162    * Head of linked list of pending reservation requests.
163    */
164   struct GNUNET_ATS_ReservationContext *reservation_head;
165
166   /**
167    * Tail of linked list of pending reservation requests.
168    */
169   struct GNUNET_ATS_ReservationContext *reservation_tail;
170
171   /**
172    * Head of linked list of pending address list requests.
173    */
174   struct GNUNET_ATS_AddressListHandle *addresslist_head;
175
176   /**
177    * Tail of linked list of pending address list requests.
178    */
179   struct GNUNET_ATS_AddressListHandle *addresslist_tail;
180
181   /**
182    * Current request for transmission to ATS.
183    */
184   struct GNUNET_CLIENT_TransmitHandle *th;
185
186   /**
187    * Task to trigger reconnect.
188    */
189   struct GNUNET_SCHEDULER_Task *task;
190
191   /**
192    * Reconnect backoff delay.
193    */
194   struct GNUNET_TIME_Relative backoff;
195
196   /**
197    * Monitor request multiplexing
198    */
199   uint32_t monitor_id;
200
201   /**
202    * Request multiplexing
203    */
204   uint32_t id;
205
206   /**
207    * Is the receive loop active?
208    */
209   int in_receive;
210 };
211
212 /**
213  * Re-establish the connection to the ATS service.
214  *
215  * @param ph handle to use to re-connect.
216  */
217 static void
218 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
219
220
221 /**
222  * Re-establish the connection to the ATS service.
223  *
224  * @param cls handle to use to re-connect.
225  */
226 static void
227 reconnect_task (void *cls)
228 {
229   struct GNUNET_ATS_PerformanceHandle *ph = cls;
230
231   ph->task = NULL;
232   reconnect (ph);
233 }
234
235
236 /**
237  * Reconnect to the ATS service, something went wrong.
238  *
239  * @param ph handle to reconnect
240  */
241 static void
242 do_reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
243 {
244   struct GNUNET_ATS_ReservationContext *rc;
245   struct GNUNET_ATS_AddressListHandle *alh;
246   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
247
248   if (NULL != ph->mq)
249   {
250     GNUNET_MQ_destroy (ph->mq);
251     ph->mq = NULL;
252   }
253   while (NULL != (rc = ph->reservation_head))
254   {
255     GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
256                                  ph->reservation_tail,
257                                  rc);
258     if (NULL != rc->rcb)
259       rc->rcb (rc->rcb_cls,
260                NULL,
261                0,
262                GNUNET_TIME_UNIT_FOREVER_REL);
263     GNUNET_free (rc);
264   }
265   bandwidth_zero.value__ = htonl (0);
266   while (NULL != (alh = ph->addresslist_head))
267   {
268     GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
269                                  ph->addresslist_tail,
270                                  alh);
271     if (NULL != alh->cb)
272       alh->cb (alh->cb_cls,
273                NULL,
274                GNUNET_NO,
275                bandwidth_zero,
276                bandwidth_zero,
277                NULL);
278     GNUNET_free (alh);
279   }
280   if (NULL != ph->addr_info_cb)
281   {
282     /* Indicate reconnect */
283     ph->addr_info_cb (ph->addr_info_cb_cls,
284                       NULL,
285                       GNUNET_NO,
286                       bandwidth_zero,
287                       bandwidth_zero,
288                       NULL);
289   }
290   ph->backoff = GNUNET_TIME_STD_BACKOFF (ph->backoff);
291   ph->task = GNUNET_SCHEDULER_add_delayed (ph->backoff,
292                                            &reconnect_task,
293                                            ph);
294 }
295
296
297 /**
298  * We received a peer information message.  Validate and process it.
299  *
300  * @param cls our context with the callback
301  * @param pi the message
302  * @return #GNUNET_OK if the message was well-formed
303  */
304 static int
305 check_peer_information (void *cls,
306                         const struct PeerInformationMessage *pi)
307 {
308   const char *plugin_address;
309   const char *plugin_name;
310   uint16_t plugin_address_length;
311   uint16_t plugin_name_length;
312
313   plugin_address_length = ntohs (pi->address_length);
314   plugin_name_length = ntohs (pi->plugin_name_length);
315   plugin_address = (const char *) &pi[1];
316   plugin_name = &plugin_address[plugin_address_length];
317   if ( (plugin_address_length + plugin_name_length
318         + sizeof(struct PeerInformationMessage) != ntohs (pi->header.size)) ||
319        (plugin_name[plugin_name_length - 1] != '\0'))
320   {
321     GNUNET_break(0);
322     return GNUNET_SYSERR;
323   }
324   return GNUNET_OK;
325 }
326
327
328 /**
329  * We received a peer information message.  Validate and process it.
330  *
331  * @param cls our context with the callback
332  * @param pi the message
333  * @return #GNUNET_OK if the message was well-formed
334  */
335 static void
336 handle_peer_information (void *cls,
337                          const struct PeerInformationMessage *pi)
338 {
339   struct GNUNET_ATS_PerformanceHandle *ph = cls;
340   const char *plugin_address;
341   const char *plugin_name;
342   struct GNUNET_HELLO_Address address;
343   uint16_t plugin_address_length;
344   int addr_active;
345   struct GNUNET_ATS_Properties prop;
346
347   if (NULL == ph->addr_info_cb)
348     return;
349   plugin_address_length = ntohs (pi->address_length);
350   addr_active = (int) ntohl (pi->address_active);
351   plugin_address = (const char *) &pi[1];
352   plugin_name = &plugin_address[plugin_address_length];
353
354   GNUNET_ATS_properties_ntoh (&prop,
355                               &pi->properties);
356   address.peer = pi->peer;
357   address.local_info = (enum GNUNET_HELLO_AddressInfo) ntohl (pi->address_local_info);
358   address.address = plugin_address;
359   address.address_length = plugin_address_length;
360   address.transport_name = plugin_name;
361   ph->addr_info_cb (ph->addr_info_cb_cls,
362                     &address,
363                     addr_active,
364                     pi->bandwidth_out,
365                     pi->bandwidth_in,
366                     &prop);
367 }
368
369
370 /**
371  * We received a reservation result message.  Validate and process it.
372  *
373  * @param cls our context with the callback
374  * @param rr the message
375  */
376 static void
377 handle_reservation_result (void *cls,
378                            const struct ReservationResultMessage *rr)
379 {
380   struct GNUNET_ATS_PerformanceHandle *ph = cls;
381   struct GNUNET_ATS_ReservationContext *rc;
382   int32_t amount;
383
384   amount = ntohl (rr->amount);
385   rc = ph->reservation_head;
386   if (0 != memcmp (&rr->peer,
387                    &rc->peer,
388                    sizeof(struct GNUNET_PeerIdentity)))
389   {
390     GNUNET_break(0);
391     reconnect (ph);
392     return;
393   }
394   GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
395                                ph->reservation_tail,
396                                rc);
397   if ( (0 == amount) ||
398        (NULL != rc->rcb) )
399   {
400     /* tell client if not cancelled */
401     if (NULL != rc->rcb)
402       rc->rcb (rc->rcb_cls,
403                &rr->peer,
404                amount,
405                GNUNET_TIME_relative_ntoh (rr->res_delay));
406     GNUNET_free (rc);
407     return;
408   }
409   /* amount non-zero, but client cancelled, consider undo! */
410   if (GNUNET_YES != rc->undo)
411   {
412     GNUNET_free (rc);
413     return; /* do not try to undo failed undos or negative amounts */
414   }
415   GNUNET_free (rc);
416   (void) GNUNET_ATS_reserve_bandwidth (ph,
417                                        &rr->peer,
418                                        -amount,
419                                        NULL, NULL);
420 }
421
422
423 /**
424  * We received a PeerInformationMessage.  Validate it.
425  *
426  * @param cls our context with the callback
427  * @param msg the message
428  * @return #GNUNET_OK if the message was well-formed
429  */
430 static int
431 check_address_list (void *cls,
432                     const struct PeerInformationMessage *pi)
433 {
434   const char *plugin_address;
435   const char *plugin_name;
436   uint16_t plugin_address_length;
437   uint16_t plugin_name_length;
438
439   plugin_address_length = ntohs (pi->address_length);
440   plugin_name_length = ntohs (pi->plugin_name_length);
441   plugin_address = (const char *) &pi[1];
442   plugin_name = &plugin_address[plugin_address_length];
443   if ( (plugin_address_length + plugin_name_length
444         + sizeof (struct PeerInformationMessage) != ntohs (pi->header.size)) ||
445        (plugin_name[plugin_name_length - 1] != '\0') )
446   {
447     GNUNET_break(0);
448     return GNUNET_SYSERR;
449   }
450   return GNUNET_OK;
451 }
452
453
454 /**
455  * We received a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE.
456  * Process it.
457  *
458  * @param cls our context with the callback
459  * @param msg the message
460  */
461 static void
462 handle_address_list (void *cls,
463                      const struct PeerInformationMessage *pi)
464 {
465   struct GNUNET_ATS_PerformanceHandle *ph = cls;
466   struct GNUNET_ATS_AddressListHandle *alh;
467   struct GNUNET_ATS_AddressListHandle *next;
468   const char *plugin_address;
469   const char *plugin_name;
470   struct GNUNET_HELLO_Address address;
471   struct GNUNET_PeerIdentity allzeros;
472   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
473   struct GNUNET_ATS_Properties prop;
474   uint16_t plugin_address_length;
475   uint16_t plugin_name_length;
476   uint32_t active;
477   uint32_t id;
478
479   id = ntohl (pi->id);
480   active = ntohl (pi->address_active);
481   plugin_address_length = ntohs (pi->address_length);
482   plugin_name_length = ntohs (pi->plugin_name_length);
483   plugin_address = (const char *) &pi[1];
484   plugin_name = &plugin_address[plugin_address_length];
485   LOG (GNUNET_ERROR_TYPE_DEBUG,
486        "Received ATS_ADDRESSLIST_RESPONSE message for peer %s and plugin %s\n",
487        GNUNET_i2s (&pi->peer),
488        plugin_name);
489
490   next = ph->addresslist_head;
491   while (NULL != (alh = next))
492   {
493     next = alh->next;
494     if (alh->id == id)
495       break;
496   }
497   if (NULL == alh)
498     return; /* was canceled */
499
500   memset (&allzeros, '\0', sizeof (allzeros));
501   if ( (0 == memcmp (&allzeros, &pi->peer, sizeof(allzeros))) &&
502        (0 == plugin_name_length) &&
503        (0 == plugin_address_length) )
504   {
505     /* Done */
506     LOG (GNUNET_ERROR_TYPE_DEBUG,
507          "Received last message for ATS_ADDRESSLIST_RESPONSE\n");
508     bandwidth_zero.value__ = htonl (0);
509     GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
510                                  ph->addresslist_tail,
511                                  alh);
512     if (NULL != alh->cb)
513       alh->cb (alh->cb_cls,
514                NULL,
515                GNUNET_NO,
516                bandwidth_zero,
517                bandwidth_zero,
518                NULL);
519     GNUNET_free (alh);
520     return;
521   }
522
523   address.peer = pi->peer;
524   address.address = plugin_address;
525   address.address_length = plugin_address_length;
526   address.transport_name = plugin_name;
527   if ( ( (GNUNET_YES == alh->all_addresses) ||
528          (GNUNET_YES == active) ) &&
529        (NULL != alh->cb) )
530   {
531     GNUNET_ATS_properties_ntoh (&prop,
532                                 &pi->properties);
533     alh->cb (alh->cb_cls,
534              &address,
535              active,
536              pi->bandwidth_out,
537              pi->bandwidth_in,
538              &prop);
539   }
540 }
541
542
543 /**
544  * Generic error handler, called with the appropriate error code and
545  * the same closure specified at the creation of the message queue.
546  * Not every message queue implementation supports an error handler.
547  *
548  * @param cls closure with the `struct GNUNET_ATS_PerformanceHandle *`
549  * @param error error code
550  */
551 static void
552 mq_error_handler (void *cls,
553                   enum GNUNET_MQ_Error error)
554 {
555   struct GNUNET_ATS_PerformanceHandle *ph = cls;
556
557   do_reconnect (ph);
558 }
559
560
561 /**
562  * Re-establish the connection to the ATS service.
563  *
564  * @param ph handle to use to re-connect.
565  */
566 static void
567 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
568 {
569   GNUNET_MQ_hd_var_size (peer_information,
570                          GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION,
571                          struct PeerInformationMessage);
572   GNUNET_MQ_hd_fixed_size (reservation_result,
573                            GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT,
574                            struct ReservationResultMessage);
575   GNUNET_MQ_hd_var_size (address_list,
576                          GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE,
577                          struct PeerInformationMessage);
578   struct GNUNET_MQ_MessageHandler handlers[] = {
579     make_peer_information_handler (ph),
580     make_reservation_result_handler (ph),
581     make_address_list_handler (ph),
582     GNUNET_MQ_handler_end ()
583   };
584   struct GNUNET_MQ_Envelope *env;
585   struct ClientStartMessage *init;
586
587   GNUNET_assert (NULL == ph->mq);
588   ph->mq = GNUNET_CLIENT_connecT (ph->cfg,
589                                   "ats",
590                                   handlers,
591                                   &mq_error_handler,
592                                   ph);
593   if (NULL == ph->mq)
594     return;
595   env = GNUNET_MQ_msg (init,
596                        GNUNET_MESSAGE_TYPE_ATS_START);
597   init->start_flag = htonl ( (NULL == ph->addr_info_cb)
598                              ? START_FLAG_PERFORMANCE_NO_PIC
599                              : START_FLAG_PERFORMANCE_WITH_PIC);
600   GNUNET_MQ_send (ph->mq,
601                   env);
602 }
603
604
605 /**
606  * Get handle to access performance API of the ATS subsystem.
607  *
608  * @param cfg configuration to use
609  * @param addr_info_cb callback called when performance characteristics for
610  *      an address change
611  * @param addr_info_cb_cls closure for @a addr_info_cb
612  * @return ats performance context
613  */
614 struct GNUNET_ATS_PerformanceHandle *
615 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
616                              GNUNET_ATS_AddressInformationCallback addr_info_cb,
617                              void *addr_info_cb_cls)
618 {
619   struct GNUNET_ATS_PerformanceHandle *ph;
620
621   ph = GNUNET_new (struct GNUNET_ATS_PerformanceHandle);
622   ph->cfg = cfg;
623   ph->addr_info_cb = addr_info_cb;
624   ph->addr_info_cb_cls = addr_info_cb_cls;
625   reconnect (ph);
626   if (NULL == ph->mq)
627   {
628     GNUNET_free (ph);
629     return NULL;
630   }
631   return ph;
632 }
633
634
635 /**
636  * Client is done using the ATS performance subsystem, release resources.
637  *
638  * @param ph handle
639  */
640 void
641 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
642 {
643   struct GNUNET_ATS_ReservationContext *rc;
644   struct GNUNET_ATS_AddressListHandle *alh;
645
646   while (NULL != (alh = ph->addresslist_head))
647   {
648     GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
649                                  ph->addresslist_tail,
650                                  alh);
651     GNUNET_free (alh);
652   }
653   while (NULL != (rc = ph->reservation_head))
654   {
655     GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
656                                  ph->reservation_tail,
657                                  rc);
658     GNUNET_break (NULL == rc->rcb);
659     GNUNET_free (rc);
660   }
661   if (NULL != ph->task)
662   {
663     GNUNET_SCHEDULER_cancel (ph->task);
664     ph->task = NULL;
665   }
666   if (NULL != ph->mq)
667   {
668     GNUNET_MQ_destroy (ph->mq);
669     ph->mq = NULL;
670   }
671   GNUNET_free (ph);
672 }
673
674
675 /**
676  * Reserve inbound bandwidth from the given peer.  ATS will look at
677  * the current amount of traffic we receive from the peer and ensure
678  * that the peer could add @a amount of data to its stream.
679  *
680  * @param ph performance handle
681  * @param peer identifies the peer
682  * @param amount reserve N bytes for receiving, negative
683  *                amounts can be used to undo a (recent) reservation;
684  * @param rcb function to call with the resulting reservation information
685  * @param rcb_cls closure for @a rcb
686  * @return NULL on error
687  * @deprecated will be replaced soon
688  */
689 struct GNUNET_ATS_ReservationContext *
690 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
691                               const struct GNUNET_PeerIdentity *peer,
692                               int32_t amount,
693                               GNUNET_ATS_ReservationCallback rcb,
694                               void *rcb_cls)
695 {
696   struct GNUNET_ATS_ReservationContext *rc;
697   struct GNUNET_MQ_Envelope *env;
698   struct ReservationRequestMessage *m;
699
700   if (NULL == ph->mq)
701     return NULL;
702   rc = GNUNET_new (struct GNUNET_ATS_ReservationContext);
703   rc->size = amount;
704   rc->peer = *peer;
705   rc->rcb = rcb;
706   rc->rcb_cls = rcb_cls;
707   if ( (NULL != rcb) &&
708        (amount > 0) )
709     rc->undo = GNUNET_YES;
710   GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
711                                     ph->reservation_tail,
712                                     rc);
713   env = GNUNET_MQ_msg (m,
714                        GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
715   m->amount = htonl (amount);
716   m->peer = *peer;
717   GNUNET_MQ_send (ph->mq,
718                   env);
719   return rc;
720 }
721
722
723 /**
724  * Cancel request for reserving bandwidth.
725  *
726  * @param rc context returned by the original #GNUNET_ATS_reserve_bandwidth() call
727  */
728 void
729 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
730 {
731   rc->rcb = NULL;
732 }
733
734
735 /**
736  * Get information about addresses known to the ATS subsystem.
737  *
738  * @param ph the performance handle to use
739  * @param peer peer idm can be NULL for all peers
740  * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
741  *        get only address currently used
742  * @param infocb callback to call with the addresses,
743  *        will callback with address == NULL when done
744  * @param infocb_cls closure for @a infocb
745  * @return ats performance context
746  */
747 struct GNUNET_ATS_AddressListHandle*
748 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
749                                        const struct GNUNET_PeerIdentity *peer,
750                                        int all,
751                                        GNUNET_ATS_AddressInformationCallback infocb,
752                                        void *infocb_cls)
753 {
754   struct GNUNET_ATS_AddressListHandle *alh;
755   struct GNUNET_MQ_Envelope *env;
756   struct AddressListRequestMessage *m;
757
758   if (NULL == ph->mq)
759     return NULL;
760   if (NULL == infocb)
761   {
762     GNUNET_break (0);
763     return NULL;
764   }
765   alh = GNUNET_new (struct GNUNET_ATS_AddressListHandle);
766   alh->id = ph->id++;
767   alh->cb = infocb;
768   alh->cb_cls = infocb_cls;
769   alh->ph = ph;
770   alh->all_addresses = all;
771   if (NULL == peer)
772   {
773     alh->all_peers = GNUNET_YES;
774   }
775   else
776   {
777     alh->all_peers = GNUNET_NO;
778     alh->peer = *peer;
779   }
780   GNUNET_CONTAINER_DLL_insert (ph->addresslist_head,
781                                ph->addresslist_tail,
782                                alh);
783   env = GNUNET_MQ_msg (m,
784                        GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
785   m->all = htonl (all);
786   m->id = htonl (alh->id);
787   if (NULL != peer)
788     m->peer = *peer;
789   GNUNET_MQ_send (ph->mq,
790                   env);
791   return alh;
792 }
793
794
795 /**
796  * Cancel a pending address listing operation
797  *
798  * @param alh the handle of the request to cancel
799  */
800 void
801 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh)
802 {
803   struct GNUNET_ATS_PerformanceHandle *ph = alh->ph;
804
805   GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
806                                ph->addresslist_tail,
807                                alh);
808   GNUNET_free (alh);
809 }
810
811
812 /**
813  * Convert a `enum GNUNET_ATS_PreferenceType` to a string
814  *
815  * @param type the preference type
816  * @return a string or NULL if invalid
817  */
818 const char *
819 GNUNET_ATS_print_preference_type (uint32_t type)
820 {
821   const char *prefs[] = GNUNET_ATS_PreferenceTypeString;
822
823   if (type < GNUNET_ATS_PREFERENCE_END)
824     return prefs[type];
825   return NULL;
826 }
827
828
829 /**
830  * Change preferences for the given peer. Preference changes are forgotten if peers
831  * disconnect.
832  *
833  * @param ph performance handle
834  * @param peer identifies the peer
835  * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
836  */
837 void
838 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
839                                           const struct GNUNET_PeerIdentity *peer,
840                                           ...)
841 {
842   struct GNUNET_MQ_Envelope *env;
843   struct ChangePreferenceMessage *m;
844   uint32_t count;
845   struct PreferenceInformation *pi;
846   va_list ap;
847   enum GNUNET_ATS_PreferenceKind kind;
848
849   if (NULL == ph->mq)
850     return;
851   count = 0;
852   va_start(ap, peer);
853   while (GNUNET_ATS_PREFERENCE_END !=
854          (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
855   {
856     switch (kind)
857     {
858     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
859       count++;
860       (void) va_arg (ap, double);
861       break;
862     case GNUNET_ATS_PREFERENCE_LATENCY:
863       count++;
864       (void) va_arg (ap, double);
865       break;
866     default:
867       GNUNET_assert(0);
868     }
869   }
870   va_end(ap);
871   env = GNUNET_MQ_msg_extra (m,
872                              count * sizeof(struct PreferenceInformation),
873                              GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
874   m->num_preferences = htonl (count);
875   m->peer = *peer;
876   pi = (struct PreferenceInformation *) &m[1];
877   count = 0;
878   va_start(ap, peer);
879   while (GNUNET_ATS_PREFERENCE_END != (kind =
880       va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
881   {
882     pi[count].preference_kind = htonl (kind);
883     switch (kind)
884     {
885     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
886       pi[count].preference_value = (float) va_arg (ap, double);
887
888       count++;
889       break;
890     case GNUNET_ATS_PREFERENCE_LATENCY:
891       pi[count].preference_value = (float) va_arg (ap, double);
892
893       count++;
894       break;
895     default:
896       GNUNET_assert(0);
897     }
898   }
899   va_end(ap);
900   GNUNET_MQ_send (ph->mq,
901                   env);
902 }
903
904
905 /**
906  * Send feedback to ATS on how good a the requirements for a peer and a
907  * preference is satisfied by ATS
908  *
909  * @param ph performance handle
910  * @param scope the time interval this valid for: [now - scope .. now]
911  * @param peer identifies the peer
912  * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
913  */
914 void
915 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
916                                       const struct GNUNET_PeerIdentity *peer,
917                                       const struct GNUNET_TIME_Relative scope,
918                                       ...)
919 {
920   struct GNUNET_MQ_Envelope *env;
921   struct FeedbackPreferenceMessage *m;
922   uint32_t count;
923   struct PreferenceInformation *pi;
924   va_list ap;
925   enum GNUNET_ATS_PreferenceKind kind;
926
927   if (NULL == ph->mq)
928     return;
929   count = 0;
930   va_start(ap, scope);
931   while (GNUNET_ATS_PREFERENCE_END !=
932          (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
933   {
934     switch (kind)
935     {
936     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
937       count++;
938       (void) va_arg (ap, double);
939       break;
940     case GNUNET_ATS_PREFERENCE_LATENCY:
941       count++;
942       (void) va_arg (ap, double);
943       break;
944     default:
945       GNUNET_assert(0);
946     }
947   }
948   va_end(ap);
949   env = GNUNET_MQ_msg_extra (m,
950                              count * sizeof(struct PreferenceInformation),
951                              GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK);
952   m->scope = GNUNET_TIME_relative_hton (scope);
953   m->num_feedback = htonl (count);
954   m->peer = *peer;
955   pi = (struct PreferenceInformation *) &m[1];
956   count = 0;
957   va_start(ap, scope);
958   while (GNUNET_ATS_PREFERENCE_END != (kind =
959       va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
960   {
961     pi[count].preference_kind = htonl (kind);
962     switch (kind)
963     {
964     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
965       pi[count].preference_value = (float) va_arg (ap, double);
966
967       count++;
968       break;
969     case GNUNET_ATS_PREFERENCE_LATENCY:
970       pi[count].preference_value = (float) va_arg (ap, double);
971
972       count++;
973       break;
974     default:
975       GNUNET_assert(0);
976     }
977   }
978   va_end(ap);
979   GNUNET_MQ_send (ph->mq,
980                   env);
981 }
982
983 /* end of ats_api_performance.c */