- changes
[oweals/gnunet.git] / src / ats / ats_api_performance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 /**
32  * Message in linked list we should send to the ATS service.  The
33  * actual binary message follows this struct.
34  */
35 struct PendingMessage
36 {
37
38   /**
39    * Kept in a DLL.
40    */
41   struct PendingMessage *next;
42
43   /**
44    * Kept in a DLL.
45    */
46   struct PendingMessage *prev;
47
48   /**
49    * Size of the message.
50    */
51   size_t size;
52
53   /**
54    * Is this the 'ATS_START' message?
55    */
56   int is_init;
57 };
58
59
60 /**
61  * Linked list of pending reservations.
62  */
63 struct GNUNET_ATS_ReservationContext
64 {
65
66   /**
67    * Kept in a DLL.
68    */
69   struct GNUNET_ATS_ReservationContext *next;
70
71   /**
72    * Kept in a DLL.
73    */
74   struct GNUNET_ATS_ReservationContext *prev;
75
76   /**
77    * Target peer.
78    */
79   struct GNUNET_PeerIdentity peer;
80
81   /**
82    * Desired reservation
83    */
84   int32_t size;
85
86   /**
87    * Function to call on result.
88    */
89   GNUNET_ATS_ReservationCallback rcb;
90
91   /**
92    * Closure for 'rcb'
93    */
94   void *rcb_cls;
95
96   /**
97    * Do we need to undo this reservation if it succeeded?  Set to
98    * GNUNET_YES if a reservation is cancelled.  (at that point, 'info'
99    * is also set to NULL; however, info will ALSO be NULL for the
100    * reservation context that is created to undo the original request,
101    * so 'info' being NULL cannot be used to check if undo is
102    * required).
103    */
104   int undo;
105 };
106
107
108 /**
109  * Linked list of pending reservations.
110  */
111 struct GNUNET_ATS_AddressListHandle
112 {
113
114   /**
115    * Kept in a DLL.
116    */
117   struct GNUNET_ATS_AddressListHandle *next;
118
119   /**
120    * Kept in a DLL.
121    */
122   struct GNUNET_ATS_AddressListHandle *prev;
123
124   /**
125    * Performance handle
126    */
127   struct GNUNET_ATS_PerformanceHandle *ph;
128
129   /**
130    * Callback
131    */
132   GNUNET_ATS_PeerInformationCallback cb;
133
134   /**
135    * Callback closure
136    */
137   void *cb_cls;
138
139   /**
140    * Target peer.
141    */
142   struct GNUNET_PeerIdentity peer;
143
144   /**
145    * Return all or specific peer only
146    */
147   int all_peers;
148
149   /**
150    * Return all or used address only
151    */
152   int all_addresses;
153 };
154
155 /**
156  * ATS Handle to obtain and/or modify performance information.
157  */
158 struct GNUNET_ATS_PerformanceHandle
159 {
160
161   /**
162    * Our configuration.
163    */
164   const struct GNUNET_CONFIGURATION_Handle *cfg;
165
166   /**
167    * Callback to invoke on performance changes.
168    */
169   GNUNET_ATS_PeerInformationCallback infocb;
170
171   /**
172    * Closure for 'infocb'.
173    */
174   void *infocb_cls;
175
176   /**
177    * Connection to ATS service.
178    */
179   struct GNUNET_CLIENT_Connection *client;
180
181   /**
182    * Head of list of messages for the ATS service.
183    */
184   struct PendingMessage *pending_head;
185
186   /**
187    * Tail of list of messages for the ATS service
188    */
189   struct PendingMessage *pending_tail;
190
191   /**
192    * Head of linked list of pending reservation requests.
193    */
194   struct GNUNET_ATS_ReservationContext *reservation_head;
195
196   /**
197    * Tail of linked list of pending reservation requests.
198    */
199   struct GNUNET_ATS_ReservationContext *reservation_tail;
200
201   /**
202    * Head of linked list of pending address list requests.
203    */
204   struct GNUNET_ATS_AddressListHandle *addresslist_head;
205
206   /**
207    * Tail of linked list of pending address list requests.
208    */
209   struct GNUNET_ATS_AddressListHandle *addresslist_tail;
210
211   /**
212    * Current request for transmission to ATS.
213    */
214   struct GNUNET_CLIENT_TransmitHandle *th;
215
216   /**
217    * Task to trigger reconnect.
218    */
219   GNUNET_SCHEDULER_TaskIdentifier task;
220
221 };
222
223
224 /**
225  * Re-establish the connection to the ATS service.
226  *
227  * @param ph handle to use to re-connect.
228  */
229 static void
230 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
231
232
233 /**
234  * Re-establish the connection to the ATS service.
235  *
236  * @param cls handle to use to re-connect.
237  * @param tc scheduler context
238  */
239 static void
240 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 {
242   struct GNUNET_ATS_PerformanceHandle *ph = cls;
243
244   ph->task = GNUNET_SCHEDULER_NO_TASK;
245   reconnect (ph);
246 }
247
248
249 /**
250  * Transmit messages from the message queue to the service
251  * (if there are any, and if we are not already trying).
252  *
253  * @param ph handle to use
254  */
255 static void
256 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
257
258
259 /**
260  * We can now transmit a message to ATS. Do it.
261  *
262  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
263  * @param size number of bytes we can transmit to ATS
264  * @param buf where to copy the messages
265  * @return number of bytes copied into buf
266  */
267 static size_t
268 transmit_message_to_ats (void *cls, size_t size, void *buf)
269 {
270   struct GNUNET_ATS_PerformanceHandle *ph = cls;
271   struct PendingMessage *p;
272   size_t ret;
273   char *cbuf;
274
275   ph->th = NULL;
276   ret = 0;
277   cbuf = buf;
278   while ((NULL != (p = ph->pending_head)) && (p->size <= size))
279   {
280     memcpy (&cbuf[ret], &p[1], p->size);
281     ret += p->size;
282     size -= p->size;
283     GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
284     GNUNET_free (p);
285   }
286   do_transmit (ph);
287   return ret;
288 }
289
290
291 /**
292  * Transmit messages from the message queue to the service
293  * (if there are any, and if we are not already trying).
294  *
295  * @param ph handle to use
296  */
297 static void
298 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
299 {
300   struct PendingMessage *p;
301
302   if (NULL != ph->th)
303     return;
304   if (NULL == (p = ph->pending_head))
305     return;
306   if (NULL == ph->client)
307     return;                     /* currently reconnecting */
308   ph->th =
309       GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
310                                            GNUNET_TIME_UNIT_FOREVER_REL,
311                                            GNUNET_YES, &transmit_message_to_ats,
312                                            ph);
313 }
314
315
316 /**
317  * We received a peer information message.  Validate and process it.
318  *
319  * @param ph our context with the callback
320  * @param msg the message
321  * @return GNUNET_OK if the message was well-formed
322  */
323 static int
324 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
325                     const struct GNUNET_MessageHeader *msg)
326 {
327   const struct PeerInformationMessage *pi;
328   const struct GNUNET_ATS_Information *atsi;
329   const char *plugin_address;
330   const char *plugin_name;
331   struct GNUNET_HELLO_Address address;
332   uint16_t plugin_address_length;
333   uint16_t plugin_name_length;
334   uint32_t ats_count;
335
336   if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
337   {
338     GNUNET_break (0);
339     return GNUNET_SYSERR;
340   }
341   pi = (const struct PeerInformationMessage *) msg;
342   ats_count = ntohl (pi->ats_count);
343   plugin_address_length = ntohs (pi->address_length);
344   plugin_name_length = ntohs (pi->plugin_name_length);
345   atsi = (const struct GNUNET_ATS_Information *) &pi[1];
346   plugin_address = (const char *) &atsi[ats_count];
347   plugin_name = &plugin_address[plugin_address_length];
348   if ((plugin_address_length + plugin_name_length +
349        ats_count * sizeof (struct GNUNET_ATS_Information) +
350        sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
351       (ats_count >
352        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
353       || (plugin_name[plugin_name_length - 1] != '\0'))
354   {
355     GNUNET_break (0);
356     return GNUNET_SYSERR;
357   }
358   if (ph->infocb == NULL)
359   {
360     return GNUNET_OK;
361   }
362   address.peer = pi->peer;
363   address.address = plugin_address;
364   address.address_length = plugin_address_length;
365   address.transport_name = plugin_name;
366   ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
367               atsi, ats_count);
368   return GNUNET_OK;
369 }
370
371
372 /**
373  * We received a reservation result message.  Validate and process it.
374  *
375  * @param ph our context with the callback
376  * @param msg the message
377  * @return GNUNET_OK if the message was well-formed
378  */
379 static int
380 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
381                     const struct GNUNET_MessageHeader *msg)
382 {
383   const struct ReservationResultMessage *rr;
384   struct GNUNET_ATS_ReservationContext *rc;
385   int32_t amount;
386
387   if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
388   {
389     GNUNET_break (0);
390     return GNUNET_SYSERR;
391   }
392   rr = (const struct ReservationResultMessage *) msg;
393   amount = ntohl (rr->amount);
394   rc = ph->reservation_head;
395   if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
396   {
397     GNUNET_break (0);
398     return GNUNET_SYSERR;
399   }
400   GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
401   if ((amount == 0) || (rc->rcb != NULL))
402   {
403     /* tell client if not cancelled */
404     if (rc->rcb != NULL)
405       rc->rcb (rc->rcb_cls, &rr->peer, amount,
406                GNUNET_TIME_relative_ntoh (rr->res_delay));
407     GNUNET_free (rc);
408     return GNUNET_OK;
409   }
410   /* amount non-zero, but client cancelled, consider undo! */
411   if (GNUNET_YES != rc->undo)
412   {
413     GNUNET_free (rc);
414     return GNUNET_OK;           /* do not try to undo failed undos or negative amounts */
415   }
416   GNUNET_free (rc);
417   (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
418   return GNUNET_OK;
419 }
420
421
422 /**
423  * We received a reservation result message.  Validate and process it.
424  *
425  * @param ph our context with the callback
426  * @param msg the message
427  * @return GNUNET_OK if the message was well-formed
428  */
429 static int
430 process_ar_message (struct GNUNET_ATS_PerformanceHandle *ph,
431                     const struct GNUNET_MessageHeader *msg)
432 {
433   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "TO BE IMPLEMENTED\n");
434 # if 0
435   TBD!
436   const struct ReservationResultMessage *rr;
437   struct GNUNET_ATS_ReservationContext *rc;
438   int32_t amount;
439
440   if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
441   {
442     GNUNET_break (0);
443     return GNUNET_SYSERR;
444   }
445   rr = (const struct ReservationResultMessage *) msg;
446   amount = ntohl (rr->amount);
447   rc = ph->reservation_head;
448   if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
449   {
450     GNUNET_break (0);
451     return GNUNET_SYSERR;
452   }
453   GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
454   if ((amount == 0) || (rc->rcb != NULL))
455   {
456     /* tell client if not cancelled */
457     if (rc->rcb != NULL)
458       rc->rcb (rc->rcb_cls, &rr->peer, amount,
459                GNUNET_TIME_relative_ntoh (rr->res_delay));
460     GNUNET_free (rc);
461     return GNUNET_OK;
462   }
463   /* amount non-zero, but client cancelled, consider undo! */
464   if (GNUNET_YES != rc->undo)
465   {
466     GNUNET_free (rc);
467     return GNUNET_OK;           /* do not try to undo failed undos or negative amounts */
468   }
469   GNUNET_free (rc);
470   (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
471 #endif
472   return GNUNET_OK;
473 }
474
475
476 /**
477  * Type of a function to call when we receive a message
478  * from the service.
479  *
480  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
481  * @param msg message received, NULL on timeout or fatal error
482  */
483 static void
484 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
485 {
486   struct GNUNET_ATS_PerformanceHandle *ph = cls;
487
488   if (NULL == msg)
489     goto reconnect;
490   switch (ntohs (msg->type))
491   {
492   case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
493     if (GNUNET_OK != process_pi_message (ph, msg))
494       goto reconnect;
495     break;
496   case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
497     if (GNUNET_OK != process_rr_message (ph, msg))
498       goto reconnect;
499     break;
500   case GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE:
501     if (GNUNET_OK != process_ar_message (ph, msg))
502       goto reconnect;
503     break;
504   default:
505     GNUNET_break (0);
506     goto reconnect;
507   }
508   GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
509                          GNUNET_TIME_UNIT_FOREVER_REL);
510   return;
511 reconnect:
512   if (NULL != ph->th)
513   {
514     GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
515     ph->th = NULL;
516   }
517   GNUNET_CLIENT_disconnect (ph->client);
518   ph->client = NULL;
519   ph->task =
520       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
521                                     ph);
522 }
523
524
525 /**
526  * Re-establish the connection to the ATS service.
527  *
528  * @param ph handle to use to re-connect.
529  */
530 static void
531 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
532 {
533   struct PendingMessage *p;
534   struct ClientStartMessage *init;
535
536   GNUNET_assert (NULL == ph->client);
537   ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
538   GNUNET_assert (NULL != ph->client);
539   GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
540                          GNUNET_TIME_UNIT_FOREVER_REL);
541   if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
542   {
543     p = GNUNET_malloc (sizeof (struct PendingMessage) +
544                        sizeof (struct ClientStartMessage));
545     p->size = sizeof (struct ClientStartMessage);
546     p->is_init = GNUNET_YES;
547     init = (struct ClientStartMessage *) &p[1];
548     init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
549     init->header.size = htons (sizeof (struct ClientStartMessage));
550     init->start_flag =
551         htonl ((ph->infocb ==
552                 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
553                START_FLAG_PERFORMANCE_WITH_PIC);
554     GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
555   }
556   do_transmit (ph);
557 }
558
559
560
561 /**
562  * Get handle to access performance API of the ATS subsystem.
563  *
564  * @param cfg configuration to use
565  * @param infocb function to call on allocation changes, can be NULL
566  * @param infocb_cls closure for infocb
567  * @return ats performance context
568  */
569 struct GNUNET_ATS_PerformanceHandle *
570 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
571                              GNUNET_ATS_PeerInformationCallback infocb,
572                              void *infocb_cls)
573 {
574   struct GNUNET_ATS_PerformanceHandle *ph;
575
576   ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
577   ph->cfg = cfg;
578   ph->infocb = infocb;
579   ph->infocb_cls = infocb_cls;
580   reconnect (ph);
581   return ph;
582 }
583
584
585 /**
586  * Client is done using the ATS performance subsystem, release resources.
587  *
588  * @param ph handle
589  */
590 void
591 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
592 {
593   struct PendingMessage *p;
594   struct GNUNET_ATS_ReservationContext *rc;
595   struct GNUNET_ATS_AddressListHandle *alh;
596
597   while (NULL != (p = ph->pending_head))
598   {
599     GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
600     GNUNET_free (p);
601   }
602   while (NULL != (alh = ph->addresslist_head))
603   {
604     GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail,
605                                  alh);
606     GNUNET_free (alh);
607   }
608   while (NULL != (rc = ph->reservation_head))
609   {
610     GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
611                                  rc);
612     GNUNET_break (NULL == rc->rcb);
613     GNUNET_free (rc);
614   }
615   if (GNUNET_SCHEDULER_NO_TASK != ph->task)
616   {
617     GNUNET_SCHEDULER_cancel (ph->task);
618     ph->task = GNUNET_SCHEDULER_NO_TASK;
619   }
620   if (NULL != ph->client)
621   {
622     GNUNET_CLIENT_disconnect (ph->client);
623     ph->client = NULL;
624   }
625   GNUNET_free (ph);
626 }
627
628
629 /**
630  * Reserve inbound bandwidth from the given peer.  ATS will look at
631  * the current amount of traffic we receive from the peer and ensure
632  * that the peer could add 'amount' of data to its stream.
633  *
634  * @param ph performance handle
635  * @param peer identifies the peer
636  * @param amount reserve N bytes for receiving, negative
637  *                amounts can be used to undo a (recent) reservation;
638  * @param rcb function to call with the resulting reservation information
639  * @param rcb_cls closure for info
640  * @return NULL on error
641  * @deprecated will be replaced soon
642  */
643 struct GNUNET_ATS_ReservationContext *
644 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
645                               const struct GNUNET_PeerIdentity *peer,
646                               int32_t amount,
647                               GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
648 {
649   struct GNUNET_ATS_ReservationContext *rc;
650   struct PendingMessage *p;
651   struct ReservationRequestMessage *m;
652
653   rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
654   rc->size = amount;
655   rc->peer = *peer;
656   rc->rcb = rcb;
657   rc->rcb_cls = rcb_cls;
658   if ((rcb != NULL) && (amount > 0))
659     rc->undo = GNUNET_YES;
660   GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
661                                     rc);
662
663   p = GNUNET_malloc (sizeof (struct PendingMessage) +
664                      sizeof (struct ReservationRequestMessage));
665   p->size = sizeof (struct ReservationRequestMessage);
666   p->is_init = GNUNET_NO;
667   m = (struct ReservationRequestMessage *) &p[1];
668   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
669   m->header.size = htons (sizeof (struct ReservationRequestMessage));
670   m->amount = htonl (amount);
671   m->peer = *peer;
672   GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
673   do_transmit (ph);
674   return rc;
675 }
676
677
678 /**
679  * Cancel request for reserving bandwidth.
680  *
681  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
682  */
683 void
684 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
685 {
686   rc->rcb = NULL;
687 }
688
689 /**
690  * Get information about addresses known to the ATS subsystem.
691  *
692  * @param cfg configuration to use
693  * @param peer peer idm can be NULL for all peers
694  * @param all GNUNET_YES to get information about all addresses or GNUNET_NO to
695  *        get only address currently used
696  * @param infocb callback to call with the addresses,
697  *        will callback with address == NULL when done
698  * @param infocb_cls closure for infocb
699  * @return ats performance context
700  */
701 struct GNUNET_ATS_AddressListHandle*
702 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
703                                        const struct GNUNET_PeerIdentity *peer,
704                                        int all,
705                                        GNUNET_ATS_PeerInformationCallback infocb,
706                                        void *infocb_cls)
707 {
708   struct GNUNET_ATS_AddressListHandle *alh;
709   struct PendingMessage *p;
710   struct AddressListRequestMessage *m;
711
712   GNUNET_assert (NULL != handle);
713
714   alh = GNUNET_malloc (sizeof (struct GNUNET_ATS_AddressListHandle));
715   alh->cb = infocb;
716   alh->cb_cls = infocb_cls;
717   alh->ph = handle;
718   alh->all_addresses = all;
719   if (NULL == peer)
720     alh->all_peers = GNUNET_YES;
721   else
722   {
723       alh->all_peers = GNUNET_NO;
724       alh->peer = (*peer);
725   }
726
727   GNUNET_CONTAINER_DLL_insert (handle->addresslist_head, handle->addresslist_tail, alh);
728
729   p = GNUNET_malloc (sizeof (struct PendingMessage) +
730                      sizeof (struct AddressListRequestMessage));
731   p->size = sizeof (struct AddressListRequestMessage);
732   m = (struct AddressListRequestMessage *) &p[1];
733   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
734   m->header.size = htons (sizeof (struct AddressListRequestMessage));
735   m->all = htonl (all);
736   if (NULL != peer)
737     m->peer = *peer;
738   else
739   {
740       memset (&m->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
741   }
742   GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail, p);
743   do_transmit (handle);
744
745   return alh;
746 }
747
748
749 /**
750  * Cancel a pending address listing operation
751  *
752  * @param handle the GNUNET_ATS_AddressListHandle handle to cancel
753  */
754 void
755 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle)
756 {
757   GNUNET_assert (NULL != handle);
758
759   GNUNET_CONTAINER_DLL_remove (handle->ph->addresslist_head, handle->ph->addresslist_tail, handle);
760   GNUNET_free (handle);
761 }
762
763
764 /**
765  * Change preferences for the given peer. Preference changes are forgotten if peers
766  * disconnect.
767  *
768  * @param ph performance handle
769  * @param peer identifies the peer
770  * @param ... 0-terminated specification of the desired changes
771  */
772 void
773 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
774                               const struct GNUNET_PeerIdentity *peer, ...)
775 {
776   struct PendingMessage *p;
777   struct ChangePreferenceMessage *m;
778   size_t msize;
779   uint32_t count;
780   struct PreferenceInformation *pi;
781   va_list ap;
782   enum GNUNET_ATS_PreferenceKind kind;
783
784   count = 0;
785   va_start (ap, peer);
786   while (GNUNET_ATS_PREFERENCE_END !=
787          (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
788   {
789     switch (kind)
790     {
791     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
792       count++;
793       (void) va_arg (ap, double);
794
795       break;
796     case GNUNET_ATS_PREFERENCE_LATENCY:
797       count++;
798       (void) va_arg (ap, double);
799
800       break;
801     default:
802       GNUNET_assert (0);
803     }
804   }
805   va_end (ap);
806   msize =
807       count * sizeof (struct PreferenceInformation) +
808       sizeof (struct ChangePreferenceMessage);
809   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
810   p->size = msize;
811   p->is_init = GNUNET_NO;
812   m = (struct ChangePreferenceMessage *) &p[1];
813   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
814   m->header.size = htons (msize);
815   m->num_preferences = htonl (count);
816   m->peer = *peer;
817   pi = (struct PreferenceInformation *) &m[1];
818   count = 0;
819   va_start (ap, peer);
820   while (GNUNET_ATS_PREFERENCE_END !=
821          (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
822   {
823     pi[count].preference_kind = htonl (kind);
824     switch (kind)
825     {
826     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
827       pi[count].preference_value = (float) va_arg (ap, double);
828
829       count++;
830       break;
831     case GNUNET_ATS_PREFERENCE_LATENCY:
832       pi[count].preference_value = (float) va_arg (ap, double);
833
834       count++;
835       break;
836     default:
837       GNUNET_assert (0);
838     }
839   }
840   va_end (ap);
841   GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
842   do_transmit (ph);
843 }
844
845 /* end of ats_api_performance.c */