-add newline
[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    * Request multiplexing
156    */
157   uint32_t id;
158 };
159
160 /**
161  * ATS Handle to obtain and/or modify performance information.
162  */
163 struct GNUNET_ATS_PerformanceHandle
164 {
165
166   /**
167    * Our configuration.
168    */
169   const struct GNUNET_CONFIGURATION_Handle *cfg;
170
171   /**
172    * Callback to invoke on performance changes.
173    */
174   GNUNET_ATS_PeerInformationCallback infocb;
175
176   /**
177    * Closure for 'infocb'.
178    */
179   void *infocb_cls;
180
181   /**
182    * Connection to ATS service.
183    */
184   struct GNUNET_CLIENT_Connection *client;
185
186   /**
187    * Head of list of messages for the ATS service.
188    */
189   struct PendingMessage *pending_head;
190
191   /**
192    * Tail of list of messages for the ATS service
193    */
194   struct PendingMessage *pending_tail;
195
196   /**
197    * Head of linked list of pending reservation requests.
198    */
199   struct GNUNET_ATS_ReservationContext *reservation_head;
200
201   /**
202    * Tail of linked list of pending reservation requests.
203    */
204   struct GNUNET_ATS_ReservationContext *reservation_tail;
205
206   /**
207    * Head of linked list of pending address list requests.
208    */
209   struct GNUNET_ATS_AddressListHandle *addresslist_head;
210
211   /**
212    * Tail of linked list of pending address list requests.
213    */
214   struct GNUNET_ATS_AddressListHandle *addresslist_tail;
215
216   /**
217    * Current request for transmission to ATS.
218    */
219   struct GNUNET_CLIENT_TransmitHandle *th;
220
221   /**
222    * Task to trigger reconnect.
223    */
224   GNUNET_SCHEDULER_TaskIdentifier task;
225
226   /**
227    * Request multiplexing
228    */
229   uint32_t id;
230 };
231
232
233 /**
234  * Re-establish the connection to the ATS service.
235  *
236  * @param ph handle to use to re-connect.
237  */
238 static void
239 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
240
241
242 /**
243  * Re-establish the connection to the ATS service.
244  *
245  * @param cls handle to use to re-connect.
246  * @param tc scheduler context
247  */
248 static void
249 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 {
251   struct GNUNET_ATS_PerformanceHandle *ph = cls;
252
253   ph->task = GNUNET_SCHEDULER_NO_TASK;
254   reconnect (ph);
255 }
256
257
258 /**
259  * Transmit messages from the message queue to the service
260  * (if there are any, and if we are not already trying).
261  *
262  * @param ph handle to use
263  */
264 static void
265 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
266
267
268 /**
269  * We can now transmit a message to ATS. Do it.
270  *
271  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
272  * @param size number of bytes we can transmit to ATS
273  * @param buf where to copy the messages
274  * @return number of bytes copied into buf
275  */
276 static size_t
277 transmit_message_to_ats (void *cls, size_t size, void *buf)
278 {
279   struct GNUNET_ATS_PerformanceHandle *ph = cls;
280   struct PendingMessage *p;
281   size_t ret;
282   char *cbuf;
283
284   ph->th = NULL;
285   ret = 0;
286   cbuf = buf;
287   while ((NULL != (p = ph->pending_head)) && (p->size <= size))
288   {
289     memcpy (&cbuf[ret], &p[1], p->size);
290     ret += p->size;
291     size -= p->size;
292     GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
293     GNUNET_free (p);
294   }
295   do_transmit (ph);
296   return ret;
297 }
298
299
300 /**
301  * Transmit messages from the message queue to the service
302  * (if there are any, and if we are not already trying).
303  *
304  * @param ph handle to use
305  */
306 static void
307 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
308 {
309   struct PendingMessage *p;
310
311   if (NULL != ph->th)
312     return;
313   if (NULL == (p = ph->pending_head))
314     return;
315   if (NULL == ph->client)
316     return;                     /* currently reconnecting */
317   ph->th =
318       GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
319                                            GNUNET_TIME_UNIT_FOREVER_REL,
320                                            GNUNET_YES, &transmit_message_to_ats,
321                                            ph);
322 }
323
324
325 /**
326  * We received a peer information message.  Validate and process it.
327  *
328  * @param ph our context with the callback
329  * @param msg the message
330  * @return GNUNET_OK if the message was well-formed
331  */
332 static int
333 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
334                     const struct GNUNET_MessageHeader *msg)
335 {
336   const struct PeerInformationMessage *pi;
337   const struct GNUNET_ATS_Information *atsi;
338   const char *plugin_address;
339   const char *plugin_name;
340   struct GNUNET_HELLO_Address address;
341   uint16_t plugin_address_length;
342   uint16_t plugin_name_length;
343   uint32_t ats_count;
344
345   if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
346   {
347     GNUNET_break (0);
348     return GNUNET_SYSERR;
349   }
350
351   pi = (const struct PeerInformationMessage *) msg;
352   ats_count = ntohl (pi->ats_count);
353   plugin_address_length = ntohs (pi->address_length);
354   plugin_name_length = ntohs (pi->plugin_name_length);
355   atsi = (const struct GNUNET_ATS_Information *) &pi[1];
356   plugin_address = (const char *) &atsi[ats_count];
357   plugin_name = &plugin_address[plugin_address_length];
358   if ((plugin_address_length + plugin_name_length +
359        ats_count * sizeof (struct GNUNET_ATS_Information) +
360        sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
361       (ats_count >
362        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
363       || (plugin_name[plugin_name_length - 1] != '\0'))
364   {
365     GNUNET_break (0);
366     return GNUNET_SYSERR;
367   }
368   if (ph->infocb == NULL)
369   {
370     return GNUNET_OK;
371   }
372
373   address.peer = pi->peer;
374   address.address = plugin_address;
375   address.address_length = plugin_address_length;
376   address.transport_name = plugin_name;
377   ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
378               atsi, ats_count);
379   return GNUNET_OK;
380 }
381
382
383 /**
384  * We received a reservation result message.  Validate and process it.
385  *
386  * @param ph our context with the callback
387  * @param msg the message
388  * @return GNUNET_OK if the message was well-formed
389  */
390 static int
391 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
392                     const struct GNUNET_MessageHeader *msg)
393 {
394   const struct ReservationResultMessage *rr;
395   struct GNUNET_ATS_ReservationContext *rc;
396   int32_t amount;
397
398   if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
399   {
400     GNUNET_break (0);
401     return GNUNET_SYSERR;
402   }
403   rr = (const struct ReservationResultMessage *) msg;
404   amount = ntohl (rr->amount);
405   rc = ph->reservation_head;
406   if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
407   {
408     GNUNET_break (0);
409     return GNUNET_SYSERR;
410   }
411   GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
412   if ((amount == 0) || (rc->rcb != NULL))
413   {
414     /* tell client if not cancelled */
415     if (rc->rcb != NULL)
416       rc->rcb (rc->rcb_cls, &rr->peer, amount,
417                GNUNET_TIME_relative_ntoh (rr->res_delay));
418     GNUNET_free (rc);
419     return GNUNET_OK;
420   }
421   /* amount non-zero, but client cancelled, consider undo! */
422   if (GNUNET_YES != rc->undo)
423   {
424     GNUNET_free (rc);
425     return GNUNET_OK;           /* do not try to undo failed undos or negative amounts */
426   }
427   GNUNET_free (rc);
428   (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
429   return GNUNET_OK;
430 }
431
432
433 /**
434  * We received a reservation result message.  Validate and process it.
435  *
436  * @param ph our context with the callback
437  * @param msg the message
438  * @return GNUNET_OK if the message was well-formed
439  */
440 static int
441 process_ar_message (struct GNUNET_ATS_PerformanceHandle *ph,
442                     const struct GNUNET_MessageHeader *msg)
443 {
444   const struct PeerInformationMessage *pi;
445   struct GNUNET_ATS_AddressListHandle *alh;
446   struct GNUNET_ATS_AddressListHandle *next;
447   const struct GNUNET_ATS_Information *atsi;
448   const char *plugin_address;
449   const char *plugin_name;
450   struct GNUNET_HELLO_Address address;
451   struct GNUNET_PeerIdentity allzeros;
452   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
453   uint16_t plugin_address_length;
454   uint16_t plugin_name_length;
455   uint32_t ats_count;
456   uint32_t active;
457   uint32_t id;
458
459   if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
460   {
461     GNUNET_break (0);
462     return GNUNET_SYSERR;
463   }
464   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465       _("Received %s message\n"), "ATS_ADDRESSLIST_RESPONSE");
466
467   pi = (const struct PeerInformationMessage *) msg;
468   id = ntohl (pi->id);
469   ats_count = ntohl (pi->ats_count);
470   active = ntohl (pi->address_active);
471   plugin_address_length = ntohs (pi->address_length);
472   plugin_name_length = ntohs (pi->plugin_name_length);
473   atsi = (const struct GNUNET_ATS_Information *) &pi[1];
474   plugin_address = (const char *) &atsi[ats_count];
475   plugin_name = &plugin_address[plugin_address_length];
476   if ((plugin_address_length + plugin_name_length +
477        ats_count * sizeof (struct GNUNET_ATS_Information) +
478        sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
479       (ats_count >
480        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
481       || (plugin_name[plugin_name_length - 1] != '\0'))
482   {
483     GNUNET_break (0);
484     return GNUNET_SYSERR;
485   }
486
487   next = ph->addresslist_head;
488   while (NULL != (alh = next))
489   {
490       next = alh->next;
491       if (alh->id == id)
492         break;
493   }
494   if (NULL == alh)
495   {
496       /* was canceled */
497       return GNUNET_SYSERR;
498   }
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       (0 == ats_count))
505   {
506       /* Done */
507       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508           _("Received last message for %s \n"), "ATS_ADDRESSLIST_RESPONSE");
509       bandwidth_zero.value__ = htonl (0);
510       if (NULL != alh->cb)
511         alh->cb (ph->infocb_cls,
512               NULL,
513               bandwidth_zero, bandwidth_zero,
514               NULL, 0);
515       GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail, alh);
516       GNUNET_free (alh);
517       return GNUNET_OK;
518   }
519
520   address.peer = pi->peer;
521   address.address = plugin_address;
522   address.address_length = plugin_address_length;
523   address.transport_name = plugin_name;
524
525   if ((GNUNET_YES == alh->all_addresses) || (GNUNET_YES == active))
526   {
527     if (NULL != alh->cb)
528       alh->cb (ph->infocb_cls,
529             &address,
530             pi->bandwidth_out, pi->bandwidth_in,
531             atsi, ats_count);
532   }
533   return GNUNET_OK;
534 }
535
536
537 /**
538  * Type of a function to call when we receive a message
539  * from the service.
540  *
541  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
542  * @param msg message received, NULL on timeout or fatal error
543  */
544 static void
545 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
546 {
547   struct GNUNET_ATS_PerformanceHandle *ph = cls;
548
549   if (NULL == msg)
550     goto reconnect;
551   switch (ntohs (msg->type))
552   {
553   case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
554     if (GNUNET_OK != process_pi_message (ph, msg))
555       goto reconnect;
556     break;
557   case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
558     if (GNUNET_OK != process_rr_message (ph, msg))
559       goto reconnect;
560     break;
561   case GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE:
562     if (GNUNET_OK != process_ar_message (ph, msg))
563       goto reconnect;
564     break;
565   default:
566     GNUNET_break (0);
567     goto reconnect;
568   }
569   GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
570                          GNUNET_TIME_UNIT_FOREVER_REL);
571   return;
572 reconnect:
573   if (NULL != ph->th)
574   {
575     GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
576     ph->th = NULL;
577   }
578   GNUNET_CLIENT_disconnect (ph->client);
579   ph->client = NULL;
580   ph->task =
581       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
582                                     ph);
583 }
584
585
586 /**
587  * Re-establish the connection to the ATS service.
588  *
589  * @param ph handle to use to re-connect.
590  */
591 static void
592 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
593 {
594   struct PendingMessage *p;
595   struct ClientStartMessage *init;
596
597   GNUNET_assert (NULL == ph->client);
598   ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
599   GNUNET_assert (NULL != ph->client);
600   GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
601                          GNUNET_TIME_UNIT_FOREVER_REL);
602   if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
603   {
604     p = GNUNET_malloc (sizeof (struct PendingMessage) +
605                        sizeof (struct ClientStartMessage));
606     p->size = sizeof (struct ClientStartMessage);
607     p->is_init = GNUNET_YES;
608     init = (struct ClientStartMessage *) &p[1];
609     init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
610     init->header.size = htons (sizeof (struct ClientStartMessage));
611     init->start_flag =
612         htonl ((ph->infocb ==
613                 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
614                START_FLAG_PERFORMANCE_WITH_PIC);
615     GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
616   }
617   do_transmit (ph);
618 }
619
620
621
622 /**
623  * Get handle to access performance API of the ATS subsystem.
624  *
625  * @param cfg configuration to use
626  * @param infocb function to call on allocation changes, can be NULL
627  * @param infocb_cls closure for infocb
628  * @return ats performance context
629  */
630 struct GNUNET_ATS_PerformanceHandle *
631 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
632                              GNUNET_ATS_PeerInformationCallback infocb,
633                              void *infocb_cls)
634 {
635   struct GNUNET_ATS_PerformanceHandle *ph;
636
637   ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
638   ph->cfg = cfg;
639   ph->infocb = infocb;
640   ph->infocb_cls = infocb_cls;
641   ph->id  = 0;
642   reconnect (ph);
643   return ph;
644 }
645
646
647 /**
648  * Client is done using the ATS performance subsystem, release resources.
649  *
650  * @param ph handle
651  */
652 void
653 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
654 {
655   struct PendingMessage *p;
656   struct GNUNET_ATS_ReservationContext *rc;
657   struct GNUNET_ATS_AddressListHandle *alh;
658
659   while (NULL != (p = ph->pending_head))
660   {
661     GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
662     GNUNET_free (p);
663   }
664   while (NULL != (alh = ph->addresslist_head))
665   {
666     GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail,
667                                  alh);
668     GNUNET_free (alh);
669   }
670   while (NULL != (rc = ph->reservation_head))
671   {
672     GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
673                                  rc);
674     GNUNET_break (NULL == rc->rcb);
675     GNUNET_free (rc);
676   }
677   if (GNUNET_SCHEDULER_NO_TASK != ph->task)
678   {
679     GNUNET_SCHEDULER_cancel (ph->task);
680     ph->task = GNUNET_SCHEDULER_NO_TASK;
681   }
682   if (NULL != ph->client)
683   {
684     GNUNET_CLIENT_disconnect (ph->client);
685     ph->client = NULL;
686   }
687   GNUNET_free (ph);
688 }
689
690
691 /**
692  * Reserve inbound bandwidth from the given peer.  ATS will look at
693  * the current amount of traffic we receive from the peer and ensure
694  * that the peer could add 'amount' of data to its stream.
695  *
696  * @param ph performance handle
697  * @param peer identifies the peer
698  * @param amount reserve N bytes for receiving, negative
699  *                amounts can be used to undo a (recent) reservation;
700  * @param rcb function to call with the resulting reservation information
701  * @param rcb_cls closure for info
702  * @return NULL on error
703  * @deprecated will be replaced soon
704  */
705 struct GNUNET_ATS_ReservationContext *
706 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
707                               const struct GNUNET_PeerIdentity *peer,
708                               int32_t amount,
709                               GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
710 {
711   struct GNUNET_ATS_ReservationContext *rc;
712   struct PendingMessage *p;
713   struct ReservationRequestMessage *m;
714
715   rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
716   rc->size = amount;
717   rc->peer = *peer;
718   rc->rcb = rcb;
719   rc->rcb_cls = rcb_cls;
720   if ((rcb != NULL) && (amount > 0))
721     rc->undo = GNUNET_YES;
722   GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
723                                     rc);
724
725   p = GNUNET_malloc (sizeof (struct PendingMessage) +
726                      sizeof (struct ReservationRequestMessage));
727   p->size = sizeof (struct ReservationRequestMessage);
728   p->is_init = GNUNET_NO;
729   m = (struct ReservationRequestMessage *) &p[1];
730   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
731   m->header.size = htons (sizeof (struct ReservationRequestMessage));
732   m->amount = htonl (amount);
733   m->peer = *peer;
734   GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
735   do_transmit (ph);
736   return rc;
737 }
738
739
740 /**
741  * Cancel request for reserving bandwidth.
742  *
743  * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
744  */
745 void
746 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
747 {
748   rc->rcb = NULL;
749 }
750
751 /**
752  * Get information about addresses known to the ATS subsystem.
753  *
754  * @param handle the performance handle to use
755  * @param peer peer idm can be NULL for all peers
756  * @param all GNUNET_YES to get information about all addresses or GNUNET_NO to
757  *        get only address currently used
758  * @param infocb callback to call with the addresses,
759  *        will callback with address == NULL when done
760  * @param infocb_cls closure for infocb
761  * @return ats performance context
762  */
763 struct GNUNET_ATS_AddressListHandle*
764 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
765                                        const struct GNUNET_PeerIdentity *peer,
766                                        int all,
767                                        GNUNET_ATS_PeerInformationCallback infocb,
768                                        void *infocb_cls)
769 {
770   struct GNUNET_ATS_AddressListHandle *alh;
771   struct PendingMessage *p;
772   struct AddressListRequestMessage *m;
773
774   GNUNET_assert (NULL != handle);
775   if (NULL == infocb)
776     return NULL;
777
778   alh = GNUNET_malloc (sizeof (struct GNUNET_ATS_AddressListHandle));
779   alh->id = handle->id;
780   handle->id ++;
781   alh->cb = infocb;
782   alh->cb_cls = infocb_cls;
783   alh->ph = handle;
784   alh->all_addresses = all;
785   if (NULL == peer)
786     alh->all_peers = GNUNET_YES;
787   else
788   {
789       alh->all_peers = GNUNET_NO;
790       alh->peer = (*peer);
791   }
792
793   GNUNET_CONTAINER_DLL_insert (handle->addresslist_head, handle->addresslist_tail, alh);
794
795   p = GNUNET_malloc (sizeof (struct PendingMessage) +
796                      sizeof (struct AddressListRequestMessage));
797   p->size = sizeof (struct AddressListRequestMessage);
798   m = (struct AddressListRequestMessage *) &p[1];
799   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
800   m->header.size = htons (sizeof (struct AddressListRequestMessage));
801   m->all = htonl (all);
802   m->id = htonl (alh->id);
803   if (NULL != peer)
804     m->peer = *peer;
805   else
806   {
807       memset (&m->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
808   }
809   GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail, p);
810
811   do_transmit (handle);
812
813   return alh;
814 }
815
816
817 /**
818  * Cancel a pending address listing operation
819  *
820  * @param handle the GNUNET_ATS_AddressListHandle handle to cancel
821  */
822 void
823 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle)
824 {
825   GNUNET_assert (NULL != handle);
826
827   GNUNET_CONTAINER_DLL_remove (handle->ph->addresslist_head, handle->ph->addresslist_tail, handle);
828   GNUNET_free (handle);
829 }
830
831
832 /**
833  * Change preferences for the given peer. Preference changes are forgotten if peers
834  * disconnect.
835  *
836  * @param ph performance handle
837  * @param peer identifies the peer
838  * @param ... 0-terminated specification of the desired changes
839  */
840 void
841 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
842                               const struct GNUNET_PeerIdentity *peer, ...)
843 {
844   struct PendingMessage *p;
845   struct ChangePreferenceMessage *m;
846   size_t msize;
847   uint32_t count;
848   struct PreferenceInformation *pi;
849   va_list ap;
850   enum GNUNET_ATS_PreferenceKind kind;
851
852   count = 0;
853   va_start (ap, peer);
854   while (GNUNET_ATS_PREFERENCE_END !=
855          (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
856   {
857     switch (kind)
858     {
859     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
860       count++;
861       (void) va_arg (ap, double);
862
863       break;
864     case GNUNET_ATS_PREFERENCE_LATENCY:
865       count++;
866       (void) va_arg (ap, double);
867
868       break;
869     default:
870       GNUNET_assert (0);
871     }
872   }
873   va_end (ap);
874   msize =
875       count * sizeof (struct PreferenceInformation) +
876       sizeof (struct ChangePreferenceMessage);
877   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
878   p->size = msize;
879   p->is_init = GNUNET_NO;
880   m = (struct ChangePreferenceMessage *) &p[1];
881   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
882   m->header.size = htons (msize);
883   m->num_preferences = htonl (count);
884   m->peer = *peer;
885   pi = (struct PreferenceInformation *) &m[1];
886   count = 0;
887   va_start (ap, peer);
888   while (GNUNET_ATS_PREFERENCE_END !=
889          (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
890   {
891     pi[count].preference_kind = htonl (kind);
892     switch (kind)
893     {
894     case GNUNET_ATS_PREFERENCE_BANDWIDTH:
895       pi[count].preference_value = (float) va_arg (ap, double);
896
897       count++;
898       break;
899     case GNUNET_ATS_PREFERENCE_LATENCY:
900       pi[count].preference_value = (float) va_arg (ap, double);
901
902       count++;
903       break;
904     default:
905       GNUNET_assert (0);
906     }
907   }
908   va_end (ap);
909   GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
910   do_transmit (ph);
911 }
912
913 /* end of ats_api_performance.c */