2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/ats_api_performance.c
22 * @brief automatic transport selection and outbound bandwidth determination
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
32 * Message in linked list we should send to the ATS service. The
33 * actual binary message follows this struct.
41 struct PendingMessage *next;
46 struct PendingMessage *prev;
49 * Size of the message.
54 * Is this the 'ATS_START' message?
61 * Linked list of pending reservations.
63 struct GNUNET_ATS_ReservationContext
69 struct GNUNET_ATS_ReservationContext *next;
74 struct GNUNET_ATS_ReservationContext *prev;
79 struct GNUNET_PeerIdentity peer;
87 * Function to call on result.
89 GNUNET_ATS_ReservationCallback rcb;
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
109 * Linked list of pending reservations.
111 struct GNUNET_ATS_AddressListHandle
117 struct GNUNET_ATS_AddressListHandle *next;
122 struct GNUNET_ATS_AddressListHandle *prev;
127 struct GNUNET_ATS_PerformanceHandle *ph;
132 GNUNET_ATS_AddressInformationCallback cb;
142 struct GNUNET_PeerIdentity peer;
145 * Return all or specific peer only
150 * Return all or used address only
155 * Request multiplexing
162 struct GNUNET_ATS_PerformanceMonitorHandle
164 struct GNUNET_ATS_PerformanceMonitorHandle *next;
165 struct GNUNET_ATS_PerformanceMonitorHandle *prev;
167 struct GNUNET_ATS_PerformanceHandle * ph;
169 GNUNET_ATS_PerformanceMonitorCb moncb;
177 * ATS Handle to obtain and/or modify performance information.
179 struct GNUNET_ATS_PerformanceHandle
185 const struct GNUNET_CONFIGURATION_Handle *cfg;
188 * Callback to invoke on performance changes.
190 GNUNET_ATS_AddressInformationCallback infocb;
193 * Closure for 'infocb'.
198 * Connection to ATS service.
200 struct GNUNET_CLIENT_Connection *client;
203 * Head of list of messages for the ATS service.
205 struct PendingMessage *pending_head;
208 * Tail of list of messages for the ATS service
210 struct PendingMessage *pending_tail;
213 * Head of linked list of pending reservation requests.
215 struct GNUNET_ATS_ReservationContext *reservation_head;
218 * Tail of linked list of pending reservation requests.
220 struct GNUNET_ATS_ReservationContext *reservation_tail;
223 * Head of linked list of pending address list requests.
225 struct GNUNET_ATS_AddressListHandle *addresslist_head;
228 * Tail of linked list of pending address list requests.
230 struct GNUNET_ATS_AddressListHandle *addresslist_tail;
233 * Head of linked list of pending performance monitors.
235 struct GNUNET_ATS_PerformanceMonitorHandle *monitor_head;
238 * Tail of linked list of pending performance monitors.
240 struct GNUNET_ATS_PerformanceMonitorHandle *monitor_tail;
243 * Current request for transmission to ATS.
245 struct GNUNET_CLIENT_TransmitHandle *th;
248 * Task to trigger reconnect.
250 GNUNET_SCHEDULER_TaskIdentifier task;
253 * Monitor request multiplexing
258 * Request multiplexing
265 * Re-establish the connection to the ATS service.
267 * @param ph handle to use to re-connect.
270 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
274 * Re-establish the connection to the ATS service.
276 * @param cls handle to use to re-connect.
277 * @param tc scheduler context
280 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 struct GNUNET_ATS_PerformanceHandle *ph = cls;
284 ph->task = GNUNET_SCHEDULER_NO_TASK;
290 * Transmit messages from the message queue to the service
291 * (if there are any, and if we are not already trying).
293 * @param ph handle to use
296 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
300 * We can now transmit a message to ATS. Do it.
302 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
303 * @param size number of bytes we can transmit to ATS
304 * @param buf where to copy the messages
305 * @return number of bytes copied into buf
308 transmit_message_to_ats (void *cls, size_t size, void *buf)
310 struct GNUNET_ATS_PerformanceHandle *ph = cls;
311 struct PendingMessage *p;
318 while ((NULL != (p = ph->pending_head)) && (p->size <= size))
320 memcpy (&cbuf[ret], &p[1], p->size);
323 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
332 * Transmit messages from the message queue to the service
333 * (if there are any, and if we are not already trying).
335 * @param ph handle to use
338 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
340 struct PendingMessage *p;
344 if (NULL == (p = ph->pending_head))
346 if (NULL == ph->client)
347 return; /* currently reconnecting */
349 GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
350 GNUNET_TIME_UNIT_FOREVER_REL,
351 GNUNET_YES, &transmit_message_to_ats,
357 * We received a peer information message. Validate and process it.
359 * @param ph our context with the callback
360 * @param msg the message
361 * @return GNUNET_OK if the message was well-formed
364 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
365 const struct GNUNET_MessageHeader *msg)
367 const struct PeerInformationMessage *pi;
368 const struct GNUNET_ATS_Information *atsi;
369 const char *plugin_address;
370 const char *plugin_name;
371 struct GNUNET_HELLO_Address address;
372 uint16_t plugin_address_length;
373 uint16_t plugin_name_length;
376 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
379 return GNUNET_SYSERR;
382 pi = (const struct PeerInformationMessage *) msg;
383 ats_count = ntohl (pi->ats_count);
384 plugin_address_length = ntohs (pi->address_length);
385 plugin_name_length = ntohs (pi->plugin_name_length);
386 atsi = (const struct GNUNET_ATS_Information *) &pi[1];
387 plugin_address = (const char *) &atsi[ats_count];
388 plugin_name = &plugin_address[plugin_address_length];
389 if ((plugin_address_length + plugin_name_length +
390 ats_count * sizeof (struct GNUNET_ATS_Information) +
391 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
393 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
394 || (plugin_name[plugin_name_length - 1] != '\0'))
397 return GNUNET_SYSERR;
399 if (ph->infocb == NULL)
404 address.peer = pi->peer;
405 address.address = plugin_address;
406 address.address_length = plugin_address_length;
407 address.transport_name = plugin_name;
408 ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
415 * We received a reservation result message. Validate and process it.
417 * @param ph our context with the callback
418 * @param msg the message
419 * @return GNUNET_OK if the message was well-formed
422 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
423 const struct GNUNET_MessageHeader *msg)
425 const struct ReservationResultMessage *rr;
426 struct GNUNET_ATS_ReservationContext *rc;
429 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
432 return GNUNET_SYSERR;
434 rr = (const struct ReservationResultMessage *) msg;
435 amount = ntohl (rr->amount);
436 rc = ph->reservation_head;
437 if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
440 return GNUNET_SYSERR;
442 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
443 if ((amount == 0) || (rc->rcb != NULL))
445 /* tell client if not cancelled */
447 rc->rcb (rc->rcb_cls, &rr->peer, amount,
448 GNUNET_TIME_relative_ntoh (rr->res_delay));
452 /* amount non-zero, but client cancelled, consider undo! */
453 if (GNUNET_YES != rc->undo)
456 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
459 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
465 * We received a reservation result message. Validate and process it.
467 * @param ph our context with the callback
468 * @param msg the message
469 * @return GNUNET_OK if the message was well-formed
472 process_ar_message (struct GNUNET_ATS_PerformanceHandle *ph,
473 const struct GNUNET_MessageHeader *msg)
475 const struct PeerInformationMessage *pi;
476 struct GNUNET_ATS_AddressListHandle *alh;
477 struct GNUNET_ATS_AddressListHandle *next;
478 const struct GNUNET_ATS_Information *atsi;
479 const char *plugin_address;
480 const char *plugin_name;
481 struct GNUNET_HELLO_Address address;
482 struct GNUNET_PeerIdentity allzeros;
483 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
484 uint16_t plugin_address_length;
485 uint16_t plugin_name_length;
490 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
493 return GNUNET_SYSERR;
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 _("Received %s message\n"), "ATS_ADDRESSLIST_RESPONSE");
498 pi = (const struct PeerInformationMessage *) msg;
500 ats_count = ntohl (pi->ats_count);
501 active = ntohl (pi->address_active);
502 plugin_address_length = ntohs (pi->address_length);
503 plugin_name_length = ntohs (pi->plugin_name_length);
504 atsi = (const struct GNUNET_ATS_Information *) &pi[1];
505 plugin_address = (const char *) &atsi[ats_count];
506 plugin_name = &plugin_address[plugin_address_length];
507 if ((plugin_address_length + plugin_name_length +
508 ats_count * sizeof (struct GNUNET_ATS_Information) +
509 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
511 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
512 || (plugin_name[plugin_name_length - 1] != '\0'))
515 return GNUNET_SYSERR;
518 next = ph->addresslist_head;
519 while (NULL != (alh = next))
528 return GNUNET_SYSERR;
531 memset (&allzeros, '\0', sizeof (allzeros));
532 if ((0 == memcmp (&allzeros, &pi->peer, sizeof (allzeros))) &&
533 (0 == plugin_name_length) &&
534 (0 == plugin_address_length) &&
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 _("Received last message for %s \n"), "ATS_ADDRESSLIST_RESPONSE");
540 bandwidth_zero.value__ = htonl (0);
542 alh->cb (ph->infocb_cls,
544 bandwidth_zero, bandwidth_zero,
546 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail, alh);
551 address.peer = pi->peer;
552 address.address = plugin_address;
553 address.address_length = plugin_address_length;
554 address.transport_name = plugin_name;
556 if ((GNUNET_YES == alh->all_addresses) || (GNUNET_YES == active))
559 alh->cb (ph->infocb_cls,
561 pi->bandwidth_out, pi->bandwidth_in,
568 * We received a monitor response message. Validate and process it.
570 * @param ph our context with the callback
571 * @param msg the message
572 * @return GNUNET_OK if the message was well-formed
575 process_mr_message (struct GNUNET_ATS_PerformanceHandle *ph,
576 const struct GNUNET_MessageHeader *msg)
578 struct MonitorResponseMessage *mrm = (struct MonitorResponseMessage *) msg;
579 struct GNUNET_ATS_PerformanceMonitorHandle *cur;
580 struct GNUNET_ATS_Information *ats;
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 _("Received %s message\n"), "ATS_MONITOR_RESPONSE");
588 msg_size = ntohs (msg->size);
589 if (msg_size < sizeof (struct MonitorResponseMessage))
592 return GNUNET_SYSERR;
595 ats_count = ntohl (mrm->ats_count);
596 if (msg_size != (sizeof (struct MonitorResponseMessage) +
597 ats_count * sizeof (struct GNUNET_ATS_Information)))
600 return GNUNET_SYSERR;
603 id = ntohl (mrm->id);
605 for (cur = ph->monitor_head; NULL != cur; cur = cur->next)
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 _("Received %s message for id %u\n"), "ATS_MONITOR_RESPONSE", id);
615 return GNUNET_SYSERR;
618 ats = (struct GNUNET_ATS_Information *) &mrm[1];
619 cur->moncb (cur->moncb_cls, &mrm->peer, ats, ats_count);
626 * Type of a function to call when we receive a message
629 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
630 * @param msg message received, NULL on timeout or fatal error
633 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
635 struct GNUNET_ATS_PerformanceHandle *ph = cls;
639 switch (ntohs (msg->type))
641 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
642 if (GNUNET_OK != process_pi_message (ph, msg))
645 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
646 if (GNUNET_OK != process_rr_message (ph, msg))
649 case GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE:
650 if (GNUNET_OK != process_ar_message (ph, msg))
653 case GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE:
654 if (GNUNET_OK != process_mr_message (ph, msg))
661 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
662 GNUNET_TIME_UNIT_FOREVER_REL);
667 GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
670 GNUNET_CLIENT_disconnect (ph->client);
673 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
679 * Re-establish the connection to the ATS service.
681 * @param ph handle to use to re-connect.
684 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
686 struct PendingMessage *p;
687 struct ClientStartMessage *init;
689 GNUNET_assert (NULL == ph->client);
690 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
691 GNUNET_assert (NULL != ph->client);
692 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
693 GNUNET_TIME_UNIT_FOREVER_REL);
694 if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
696 p = GNUNET_malloc (sizeof (struct PendingMessage) +
697 sizeof (struct ClientStartMessage));
698 p->size = sizeof (struct ClientStartMessage);
699 p->is_init = GNUNET_YES;
700 init = (struct ClientStartMessage *) &p[1];
701 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
702 init->header.size = htons (sizeof (struct ClientStartMessage));
704 htonl ((ph->infocb ==
705 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
706 START_FLAG_PERFORMANCE_WITH_PIC);
707 GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
715 * Get handle to access performance API of the ATS subsystem.
717 * @param cfg configuration to use
718 * @param infocb function to call on allocation changes, can be NULL
719 * @param infocb_cls closure for infocb
720 * @return ats performance context
722 struct GNUNET_ATS_PerformanceHandle *
723 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
724 GNUNET_ATS_AddressInformationCallback infocb,
727 struct GNUNET_ATS_PerformanceHandle *ph;
729 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
732 ph->infocb_cls = infocb_cls;
740 * Start monitoring performance information
742 * @param ph performance handle to use
743 * @param monitor_cb function to call on performance changes
744 * @param monitor_cb_cls closure for infocb
745 * @return a performance monitor handle
747 struct GNUNET_ATS_PerformanceMonitorHandle *
748 GNUNET_ATS_performance_monitor_start (struct GNUNET_ATS_PerformanceHandle * ph,
749 GNUNET_ATS_PerformanceMonitorCb monitor_cb,
750 void * monitor_cb_cls)
752 struct MonitorMessage *m;
753 struct PendingMessage *p;
754 GNUNET_assert (NULL != ph);
756 if (NULL == monitor_cb)
759 struct GNUNET_ATS_PerformanceMonitorHandle *phm =
760 GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceMonitorHandle));
763 phm->id = ph->monitor_id;
765 phm->moncb = monitor_cb;
766 phm->moncb_cls = monitor_cb_cls;
767 GNUNET_CONTAINER_DLL_insert (ph->monitor_head, ph->monitor_tail, phm);
769 p = GNUNET_malloc (sizeof (struct PendingMessage) +
770 sizeof (struct MonitorMessage));
771 p->size = sizeof (struct MonitorMessage);
772 m = (struct MonitorMessage *) &p[1];
773 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR);
774 m->header.size = htons (sizeof (struct MonitorMessage));
775 m->id = htonl (phm->id);
776 m->op = htonl (GNUNET_YES);
777 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
785 * Stop monitoring performance information
787 * @param phm performance monitor handle to use
790 GNUNET_ATS_performance_monitor_stop (struct GNUNET_ATS_PerformanceMonitorHandle * phm)
792 struct MonitorMessage *m;
793 struct PendingMessage *p;
795 GNUNET_assert (NULL != phm);
797 p = GNUNET_malloc (sizeof (struct PendingMessage) +
798 sizeof (struct MonitorMessage));
799 p->size = sizeof (struct MonitorMessage);
800 m = (struct MonitorMessage *) &p[1];
801 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR);
802 m->header.size = htons (sizeof (struct MonitorMessage));
803 m->id = htonl (phm->id);
804 m->op = htonl (GNUNET_NO);
805 GNUNET_CONTAINER_DLL_insert_tail (phm->ph->pending_head, phm->ph->pending_tail, p);
806 do_transmit (phm->ph);
808 GNUNET_CONTAINER_DLL_remove (phm->ph->monitor_head, phm->ph->monitor_tail, phm);
813 * Client is done using the ATS performance subsystem, release resources.
818 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
820 struct PendingMessage *p;
821 struct GNUNET_ATS_ReservationContext *rc;
822 struct GNUNET_ATS_AddressListHandle *alh;
823 struct GNUNET_ATS_PerformanceMonitorHandle *phm;
825 while (NULL != (p = ph->pending_head))
827 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
830 while (NULL != (alh = ph->addresslist_head))
832 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail,
836 while (NULL != (rc = ph->reservation_head))
838 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
840 GNUNET_break (NULL == rc->rcb);
843 while (NULL != (phm = ph->monitor_head))
845 GNUNET_CONTAINER_DLL_remove (ph->monitor_head, ph->monitor_tail, phm);
848 if (GNUNET_SCHEDULER_NO_TASK != ph->task)
850 GNUNET_SCHEDULER_cancel (ph->task);
851 ph->task = GNUNET_SCHEDULER_NO_TASK;
853 if (NULL != ph->client)
855 GNUNET_CLIENT_disconnect (ph->client);
863 * Reserve inbound bandwidth from the given peer. ATS will look at
864 * the current amount of traffic we receive from the peer and ensure
865 * that the peer could add 'amount' of data to its stream.
867 * @param ph performance handle
868 * @param peer identifies the peer
869 * @param amount reserve N bytes for receiving, negative
870 * amounts can be used to undo a (recent) reservation;
871 * @param rcb function to call with the resulting reservation information
872 * @param rcb_cls closure for info
873 * @return NULL on error
874 * @deprecated will be replaced soon
876 struct GNUNET_ATS_ReservationContext *
877 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
878 const struct GNUNET_PeerIdentity *peer,
880 GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
882 struct GNUNET_ATS_ReservationContext *rc;
883 struct PendingMessage *p;
884 struct ReservationRequestMessage *m;
886 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
890 rc->rcb_cls = rcb_cls;
891 if ((rcb != NULL) && (amount > 0))
892 rc->undo = GNUNET_YES;
893 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
896 p = GNUNET_malloc (sizeof (struct PendingMessage) +
897 sizeof (struct ReservationRequestMessage));
898 p->size = sizeof (struct ReservationRequestMessage);
899 p->is_init = GNUNET_NO;
900 m = (struct ReservationRequestMessage *) &p[1];
901 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
902 m->header.size = htons (sizeof (struct ReservationRequestMessage));
903 m->amount = htonl (amount);
905 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
912 * Cancel request for reserving bandwidth.
914 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
917 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
923 * Get information about addresses known to the ATS subsystem.
925 * @param handle the performance handle to use
926 * @param peer peer idm can be NULL for all peers
927 * @param all GNUNET_YES to get information about all addresses or GNUNET_NO to
928 * get only address currently used
929 * @param infocb callback to call with the addresses,
930 * will callback with address == NULL when done
931 * @param infocb_cls closure for infocb
932 * @return ats performance context
934 struct GNUNET_ATS_AddressListHandle*
935 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
936 const struct GNUNET_PeerIdentity *peer,
938 GNUNET_ATS_AddressInformationCallback infocb,
941 struct GNUNET_ATS_AddressListHandle *alh;
942 struct PendingMessage *p;
943 struct AddressListRequestMessage *m;
945 GNUNET_assert (NULL != handle);
949 alh = GNUNET_malloc (sizeof (struct GNUNET_ATS_AddressListHandle));
950 alh->id = handle->id;
953 alh->cb_cls = infocb_cls;
955 alh->all_addresses = all;
957 alh->all_peers = GNUNET_YES;
960 alh->all_peers = GNUNET_NO;
964 GNUNET_CONTAINER_DLL_insert (handle->addresslist_head, handle->addresslist_tail, alh);
966 p = GNUNET_malloc (sizeof (struct PendingMessage) +
967 sizeof (struct AddressListRequestMessage));
968 p->size = sizeof (struct AddressListRequestMessage);
969 m = (struct AddressListRequestMessage *) &p[1];
970 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
971 m->header.size = htons (sizeof (struct AddressListRequestMessage));
972 m->all = htonl (all);
973 m->id = htonl (alh->id);
978 memset (&m->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
980 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail, p);
982 do_transmit (handle);
989 * Cancel a pending address listing operation
991 * @param handle the GNUNET_ATS_AddressListHandle handle to cancel
994 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle)
996 GNUNET_assert (NULL != handle);
998 GNUNET_CONTAINER_DLL_remove (handle->ph->addresslist_head, handle->ph->addresslist_tail, handle);
999 GNUNET_free (handle);
1004 * Convert a GNUNET_ATS_PreferenceType to a string
1006 * @param type the preference type
1007 * @return a string or NULL if invalid
1010 GNUNET_ATS_print_preference_type (uint32_t type)
1012 char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
1013 if (type < GNUNET_ATS_PreferenceCount)
1020 * Change preferences for the given peer. Preference changes are forgotten if peers
1023 * @param ph performance handle
1024 * @param peer identifies the peer
1025 * @param ... 0-terminated specification of the desired changes
1028 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
1029 const struct GNUNET_PeerIdentity *peer, ...)
1031 struct PendingMessage *p;
1032 struct ChangePreferenceMessage *m;
1035 struct PreferenceInformation *pi;
1037 enum GNUNET_ATS_PreferenceKind kind;
1040 va_start (ap, peer);
1041 while (GNUNET_ATS_PREFERENCE_END !=
1042 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
1046 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1048 (void) va_arg (ap, double);
1051 case GNUNET_ATS_PREFERENCE_LATENCY:
1053 (void) va_arg (ap, double);
1062 count * sizeof (struct PreferenceInformation) +
1063 sizeof (struct ChangePreferenceMessage);
1064 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1066 p->is_init = GNUNET_NO;
1067 m = (struct ChangePreferenceMessage *) &p[1];
1068 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
1069 m->header.size = htons (msize);
1070 m->num_preferences = htonl (count);
1072 pi = (struct PreferenceInformation *) &m[1];
1074 va_start (ap, peer);
1075 while (GNUNET_ATS_PREFERENCE_END !=
1076 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
1078 pi[count].preference_kind = htonl (kind);
1081 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1082 pi[count].preference_value = (float) va_arg (ap, double);
1086 case GNUNET_ATS_PREFERENCE_LATENCY:
1087 pi[count].preference_value = (float) va_arg (ap, double);
1096 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
1100 /* end of ats_api_performance.c */