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_scheduling.c
22 * @brief automatic transport selection and outbound bandwidth determination
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
30 #define DEBUG_ATS GNUNET_EXTRA_LOGGING
32 #define INTERFACE_PROCESSING_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 * Message in linked list we should send to the ATS service. The
36 * actual binary message follows this struct.
44 struct PendingMessage *next;
49 struct PendingMessage *prev;
52 * Size of the message.
57 * Is this the 'ATS_START' message?
64 * Information we track per session.
69 * Identity of the peer (just needed for error checking).
71 struct GNUNET_PeerIdentity peer;
76 struct Session *session;
79 * Set to GNUNET_YES if the slot is used.
87 struct ATS_Network * next;
89 struct ATS_Network * prev;
91 struct sockaddr *network;
92 struct sockaddr *netmask;
99 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
101 struct GNUNET_ATS_SchedulingHandle
107 const struct GNUNET_CONFIGURATION_Handle *cfg;
110 * Callback to invoke on suggestions.
112 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
115 * Closure for 'suggest_cb'.
117 void *suggest_cb_cls;
120 * Connection to ATS service.
122 struct GNUNET_CLIENT_Connection *client;
125 * Head of list of messages for the ATS service.
127 struct PendingMessage *pending_head;
130 * Tail of list of messages for the ATS service
132 struct PendingMessage *pending_tail;
135 * Current request for transmission to ATS.
137 struct GNUNET_CLIENT_TransmitHandle *th;
140 * Head of network list
142 struct ATS_Network * net_head;
145 * Tail of network list
147 struct ATS_Network * net_tail;
151 * Array of session objects (we need to translate them to numbers and back
152 * for the protocol; the offset in the array is the session number on the
153 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
154 * Unused entries are also NULL.
156 struct SessionRecord *session_array;
159 * Task to trigger reconnect.
161 GNUNET_SCHEDULER_TaskIdentifier task;
164 * Task retrieving interfaces from the system
167 GNUNET_SCHEDULER_TaskIdentifier interface_task;
171 * Size of the session array.
173 unsigned int session_array_size;
176 * Should we reconnect to ATS due to some serious error?
183 * Re-establish the connection to the ATS service.
185 * @param sh handle to use to re-connect.
188 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
192 * Re-establish the connection to the ATS service.
194 * @param cls handle to use to re-connect.
195 * @param tc scheduler context
198 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 struct GNUNET_ATS_SchedulingHandle *sh = cls;
202 sh->task = GNUNET_SCHEDULER_NO_TASK;
208 * Disconnect from ATS and then reconnect.
210 * @param sh our handle
213 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
215 sh->reconnect = GNUNET_NO;
216 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
219 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
225 * Transmit messages from the message queue to the service
226 * (if there are any, and if we are not already trying).
228 * @param sh handle to use
231 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
235 * Type of a function to call when we receive a message
238 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
239 * @param msg message received, NULL on timeout or fatal error
242 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
246 * We can now transmit a message to ATS. Do it.
248 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
249 * @param size number of bytes we can transmit to ATS
250 * @param buf where to copy the messages
251 * @return number of bytes copied into buf
254 transmit_message_to_ats (void *cls, size_t size, void *buf)
256 struct GNUNET_ATS_SchedulingHandle *sh = cls;
257 struct PendingMessage *p;
262 if ((size == 0) || (buf == NULL))
264 force_reconnect (sh);
269 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
271 memcpy (&cbuf[ret], &p[1], p->size);
274 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
275 if (GNUNET_YES == p->is_init)
276 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
277 GNUNET_TIME_UNIT_FOREVER_REL);
286 * Transmit messages from the message queue to the service
287 * (if there are any, and if we are not already trying).
289 * @param sh handle to use
292 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
294 struct PendingMessage *p;
298 if (NULL == (p = sh->pending_head))
300 if (NULL == sh->client)
301 return; /* currently reconnecting */
303 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
304 GNUNET_TIME_UNIT_FOREVER_REL,
305 GNUNET_NO, &transmit_message_to_ats,
311 * Find the session object corresponding to the given session ID.
313 * @param sh our handle
314 * @param session_id current session ID
315 * @param peer peer the session belongs to
316 * @return the session object (or NULL)
318 static struct Session *
319 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
320 const struct GNUNET_PeerIdentity *peer)
323 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
324 "Find session %u from peer %s in %p\n",
325 (unsigned int) session_id, GNUNET_i2s (peer), sh);
327 if (session_id >= sh->session_array_size)
334 if (sh->session_array[session_id].session == NULL)
337 memcmp (peer, &sh->session_array[session_id].peer,
338 sizeof (struct GNUNET_PeerIdentity)));
343 memcmp (peer, &sh->session_array[session_id].peer,
344 sizeof (struct GNUNET_PeerIdentity)))
347 sh->reconnect = GNUNET_YES;
350 return sh->session_array[session_id].session;
355 * Get the ID for the given session object. If we do not have an ID for
356 * the given session object, allocate one.
358 * @param sh our handle
359 * @param session session object
360 * @param peer peer the session belongs to
361 * @return the session id
364 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
365 const struct GNUNET_PeerIdentity *peer)
371 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
372 "Get session ID for session %p from peer %s in %p\n", session,
373 GNUNET_i2s (peer), sh);
378 for (i = 1; i < sh->session_array_size; i++)
380 if (session == sh->session_array[i].session)
383 memcmp (peer, &sh->session_array[i].peer,
384 sizeof (struct GNUNET_PeerIdentity)));
387 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
392 f = sh->session_array_size;
393 GNUNET_array_grow (sh->session_array, sh->session_array_size,
394 sh->session_array_size * 2);
396 GNUNET_assert (f > 0);
397 sh->session_array[f].session = session;
398 sh->session_array[f].peer = *peer;
399 sh->session_array[f].slot_used = GNUNET_YES;
401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
402 "Assigning session ID %u for session %p of peer %s in %p\n", f,
403 session, GNUNET_i2s (peer), sh);
410 * Remove the session of the given session ID from the session
411 * table (it is no longer valid).
413 * @param sh our handle
414 * @param session_id identifies session that is no longer valid
415 * @param peer peer the session belongs to
418 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
419 const struct GNUNET_PeerIdentity *peer)
421 GNUNET_assert (peer != NULL);
422 GNUNET_assert (sh != NULL);
424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
425 "Release sessionID %u from peer %s in %p\n",
426 (unsigned int) session_id, GNUNET_i2s (peer), sh);
430 GNUNET_assert (session_id < sh->session_array_size);
431 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
433 memcmp (peer, &sh->session_array[session_id].peer,
434 sizeof (struct GNUNET_PeerIdentity)));
435 sh->session_array[session_id].session = NULL;
440 * Release the session slot from the session table (ATS service is
441 * also done using it).
443 * @param sh our handle
444 * @param session_id identifies session that is no longer valid
445 * @param peer peer the session belongs to
448 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
449 const struct GNUNET_PeerIdentity *peer)
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
453 "Release sessionID %u from peer %s in %p\n",
454 (unsigned int) session_id, GNUNET_i2s (peer), sh);
456 if (session_id >= sh->session_array_size)
459 sh->reconnect = GNUNET_YES;
463 /* this slot should have been removed from remove_session before */
464 GNUNET_assert (sh->session_array[session_id].session == NULL);
467 memcmp (peer, &sh->session_array[session_id].peer,
468 sizeof (struct GNUNET_PeerIdentity)))
471 sh->reconnect = GNUNET_YES;
474 sh->session_array[session_id].slot_used = GNUNET_NO;
475 memset (&sh->session_array[session_id].peer, 0,
476 sizeof (struct GNUNET_PeerIdentity));
481 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
482 const struct SessionReleaseMessage *srm)
484 release_session (sh, ntohl (srm->session_id), &srm->peer);
489 * Type of a function to call when we receive a message
492 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
493 * @param msg message received, NULL on timeout or fatal error
496 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
498 struct GNUNET_ATS_SchedulingHandle *sh = cls;
499 const struct AddressSuggestionMessage *m;
500 const struct GNUNET_ATS_Information *atsi;
501 const char *plugin_address;
502 const char *plugin_name;
503 uint16_t plugin_address_length;
504 uint16_t plugin_name_length;
506 struct GNUNET_HELLO_Address address;
511 force_reconnect (sh);
514 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
515 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
517 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
518 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
519 GNUNET_TIME_UNIT_FOREVER_REL);
520 if (GNUNET_YES == sh->reconnect)
521 force_reconnect (sh);
524 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
525 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
528 force_reconnect (sh);
531 m = (const struct AddressSuggestionMessage *) msg;
532 ats_count = ntohl (m->ats_count);
533 plugin_address_length = ntohs (m->address_length);
534 atsi = (const struct GNUNET_ATS_Information *) &m[1];
535 plugin_address = (const char *) &atsi[ats_count];
536 plugin_name = &plugin_address[plugin_address_length];
537 plugin_name_length = ntohs (m->plugin_name_length);
538 if ((plugin_address_length + plugin_name_length +
539 ats_count * sizeof (struct GNUNET_ATS_Information) +
540 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
542 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
543 || (plugin_name[plugin_name_length - 1] != '\0'))
546 force_reconnect (sh);
549 uint32_t session_id = ntohl (m->session_id);
555 s = find_session (sh, session_id, &m->peer);
559 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
560 "ATS tries to use outdated session `%s'\n",
561 GNUNET_i2s (&m->peer));
562 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
563 GNUNET_TIME_UNIT_FOREVER_REL);
567 address.peer = m->peer;
568 address.address = plugin_address;
569 address.address_length = plugin_address_length;
570 address.transport_name = plugin_name;
572 if ((s == NULL) && (0 == address.address_length))
574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
575 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
576 GNUNET_i2s (&address.peer), address.transport_name,
577 plugin_address_length, session_id);
579 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
580 GNUNET_TIME_UNIT_FOREVER_REL);
584 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
585 m->bandwidth_in, atsi, ats_count);
587 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
588 GNUNET_TIME_UNIT_FOREVER_REL);
589 if (GNUNET_YES == sh->reconnect)
590 force_reconnect (sh);
595 * Re-establish the connection to the ATS service.
597 * @param sh handle to use to re-connect.
600 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
602 struct PendingMessage *p;
603 struct ClientStartMessage *init;
605 GNUNET_assert (NULL == sh->client);
606 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
607 GNUNET_assert (NULL != sh->client);
608 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
610 p = GNUNET_malloc (sizeof (struct PendingMessage) +
611 sizeof (struct ClientStartMessage));
612 p->size = sizeof (struct ClientStartMessage);
613 p->is_init = GNUNET_YES;
614 init = (struct ClientStartMessage *) &p[1];
615 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
616 init->header.size = htons (sizeof (struct ClientStartMessage));
617 init->start_flag = htonl (START_FLAG_SCHEDULING);
618 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
624 * delete the current network list
628 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
630 struct ATS_Network * cur = sh->net_head;
633 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
641 interface_proc (void *cls, const char *name,
643 const struct sockaddr *
645 const struct sockaddr *
647 const struct sockaddr *
648 netmask, socklen_t addrlen)
650 struct GNUNET_ATS_SchedulingHandle * sh = cls;
651 /* Calculate network */
652 struct ATS_Network *net = NULL;
654 /* Skipping IPv4 loopback addresses since we have special check */
655 if (addr->sa_family == AF_INET)
657 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
659 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
662 /* Skipping IPv6 loopback addresses since we have special check */
663 if (addr->sa_family == AF_INET6)
665 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
666 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
670 if (addr->sa_family == AF_INET)
672 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
673 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
674 struct sockaddr_in *tmp = NULL;
675 struct sockaddr_in network4;
677 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
678 tmp = (struct sockaddr_in *) &net[1];
679 net->network = (struct sockaddr *) &tmp[0];
680 net->netmask = (struct sockaddr *) &tmp[1];
681 net->length = addrlen;
683 memset (&network4, 0, sizeof (network4));
684 network4.sin_family = AF_INET;
685 #if HAVE_SOCKADDR_IN_SIN_LEN
686 network4.sin_len = sizeof (network4);
688 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
690 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
691 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
694 if (addr->sa_family == AF_INET6)
696 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
697 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
698 struct sockaddr_in6 * tmp = NULL;
699 struct sockaddr_in6 network6;
701 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
702 tmp = (struct sockaddr_in6 *) &net[1];
703 net->network = (struct sockaddr *) &tmp[0];
704 net->netmask = (struct sockaddr *) &tmp[1];
705 net->length = addrlen;
707 memset (&network6, 0, sizeof (network6));
708 network6.sin6_family = AF_INET6;
709 #if HAVE_SOCKADDR_IN_SIN_LEN
710 network6.sin6_len = sizeof (network6);
713 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
714 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
715 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
716 for (c = 0; c < 4; c++)
717 net_elem[c] = addr_elem[c] & mask_elem[c];
719 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
720 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
727 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
729 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
731 GNUNET_free (netmask);
733 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
741 * Periodically get list of addresses
743 * @param tc Task context
746 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
748 struct GNUNET_ATS_SchedulingHandle * sh = cls;
749 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
750 delete_networks (sh);
751 GNUNET_OS_network_interfaces_list(interface_proc, sh);
752 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
758 * Returns where the address is located: LAN or WAN or ...
759 * @param sh the scheduling handle
760 * @param addr address
761 * @param addrlen address length
762 * @return location as GNUNET_ATS_Information
765 const struct GNUNET_ATS_Information
766 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
768 GNUNET_assert (sh != NULL);
769 struct GNUNET_ATS_Information ats;
770 struct ATS_Network * cur = sh->net_head;
771 int type = GNUNET_ATS_NET_UNSPECIFIED;
773 if (addr->sa_family == AF_UNIX)
775 type = GNUNET_ATS_NET_LOOPBACK;
778 /* IPv4 loopback check */
779 if (addr->sa_family == AF_INET)
781 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
783 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
784 type = GNUNET_ATS_NET_LOOPBACK;
786 /* IPv6 loopback check */
787 if (addr->sa_family == AF_INET6)
789 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
790 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
791 type = GNUNET_ATS_NET_LOOPBACK;
794 /* Check local networks */
795 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
797 if (addrlen != cur->length)
803 if (addr->sa_family == AF_INET)
805 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
806 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
807 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
809 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
811 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
813 "`%s' is in network `%s'\n",
814 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
817 type = GNUNET_ATS_NET_LAN;
820 if (addr->sa_family == AF_INET6)
822 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
823 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
824 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
826 int res = GNUNET_YES;
828 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
829 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
830 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
831 for (c = 0; c < 4; c++)
832 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
835 if (res == GNUNET_YES)
837 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
839 GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
842 type = GNUNET_ATS_NET_LAN;
848 /* no local network found for this address, default: WAN */
849 if (type == GNUNET_ATS_NET_UNSPECIFIED)
850 type = GNUNET_ATS_NET_WAN;
855 case GNUNET_ATS_NET_WAN:
858 case GNUNET_ATS_NET_LAN:
861 case GNUNET_ATS_NET_LOOPBACK:
870 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
871 ats.value = htonl (type);
872 return (const struct GNUNET_ATS_Information) ats;
876 * Initialize the ATS subsystem.
878 * @param cfg configuration to use
879 * @param suggest_cb notification to call whenever the suggestation changed
880 * @param suggest_cb_cls closure for 'suggest_cb'
881 * @return ats context
883 struct GNUNET_ATS_SchedulingHandle *
884 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
885 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
886 void *suggest_cb_cls)
888 struct GNUNET_ATS_SchedulingHandle *sh;
890 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
892 sh->suggest_cb = suggest_cb;
893 sh->suggest_cb_cls = suggest_cb_cls;
894 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
895 GNUNET_OS_network_interfaces_list(interface_proc, sh);
896 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
905 * Client is done with ATS scheduling, release resources.
907 * @param sh handle to release
910 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
912 struct PendingMessage *p;
914 while (NULL != (p = sh->pending_head))
916 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
919 if (NULL != sh->client)
921 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
924 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
926 GNUNET_SCHEDULER_cancel (sh->task);
927 sh->task = GNUNET_SCHEDULER_NO_TASK;
930 delete_networks (sh);
931 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
933 GNUNET_SCHEDULER_cancel(sh->interface_task);
934 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
936 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
943 * We would like to establish a new connection with a peer. ATS
944 * should suggest a good address to begin with.
947 * @param peer identity of the peer we need an address for
950 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
951 const struct GNUNET_PeerIdentity *peer)
953 struct PendingMessage *p;
954 struct RequestAddressMessage *m;
956 p = GNUNET_malloc (sizeof (struct PendingMessage) +
957 sizeof (struct RequestAddressMessage));
958 p->size = sizeof (struct RequestAddressMessage);
959 p->is_init = GNUNET_NO;
960 m = (struct RequestAddressMessage *) &p[1];
961 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
962 m->header.size = htons (sizeof (struct RequestAddressMessage));
963 m->reserved = htonl (0);
965 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
971 * We would like to stop receiving address updates for this peer
974 * @param peer identity of the peer
977 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
978 const struct GNUNET_PeerIdentity *peer)
980 struct PendingMessage *p;
981 struct RequestAddressMessage *m;
983 p = GNUNET_malloc (sizeof (struct PendingMessage) +
984 sizeof (struct RequestAddressMessage));
985 p->size = sizeof (struct RequestAddressMessage);
986 p->is_init = GNUNET_NO;
987 m = (struct RequestAddressMessage *) &p[1];
988 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
989 m->header.size = htons (sizeof (struct RequestAddressMessage));
990 m->reserved = htonl (0);
992 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
997 * We have updated performance statistics for a given address. Note
998 * that this function can be called for addresses that are currently
999 * in use as well as addresses that are valid but not actively in use.
1000 * Furthermore, the peer may not even be connected to us right now (in
1001 * which case the call may be ignored or the information may be stored
1002 * for later use). Update bandwidth assignments.
1005 * @param address the address
1006 * @param session session handle (if available)
1007 * @param ats performance data for the address
1008 * @param ats_count number of performance records in 'ats'
1011 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1012 const struct GNUNET_HELLO_Address *address,
1013 struct Session *session,
1014 const struct GNUNET_ATS_Information *ats,
1017 struct PendingMessage *p;
1018 struct AddressUpdateMessage *m;
1019 struct GNUNET_ATS_Information *am;
1024 if (address == NULL)
1029 if ((address == NULL) && (session == NULL))
1036 (address->transport_name ==
1037 NULL) ? 0 : strlen (address->transport_name) + 1;
1039 sizeof (struct AddressUpdateMessage) + address->address_length +
1040 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1041 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1042 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1043 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1045 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1051 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1053 p->is_init = GNUNET_NO;
1054 m = (struct AddressUpdateMessage *) &p[1];
1055 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1056 m->header.size = htons (msize);
1057 m->ats_count = htonl (ats_count);
1058 m->peer = address->peer;
1059 m->address_length = htons (address->address_length);
1060 m->plugin_name_length = htons (namelen);
1061 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1062 am = (struct GNUNET_ATS_Information *) &m[1];
1063 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1064 pm = (char *) &am[ats_count];
1065 memcpy (pm, address->address, address->address_length);
1066 memcpy (&pm[address->address_length], address->transport_name, namelen);
1067 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1073 * An address is now in use or not used any more.
1076 * @param address the address
1077 * @param session session handle
1078 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1079 * if address is not used any more
1082 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1083 const struct GNUNET_HELLO_Address *address,
1084 struct Session *session, int in_use)
1086 struct PendingMessage *p;
1087 struct AddressUseMessage *m;
1092 GNUNET_assert (NULL != address);
1094 (address->transport_name ==
1095 NULL) ? 0 : strlen (address->transport_name) + 1;
1096 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1097 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1098 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1099 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1105 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1107 p->is_init = GNUNET_NO;
1108 m = (struct AddressUseMessage *) &p[1];
1109 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1110 m->header.size = htons (msize);
1111 m->peer = address->peer;
1112 m->in_use = htons (in_use);
1113 m->address_length = htons (address->address_length);
1114 m->plugin_name_length = htons (namelen);
1115 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1116 pm = (char *) &m[1];
1117 memcpy (pm, address->address, address->address_length);
1118 memcpy (&pm[address->address_length], address->transport_name, namelen);
1119 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1125 * A session got destroyed, stop including it as a valid address.
1128 * @param address the address
1129 * @param session session handle that is no longer valid
1132 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1133 const struct GNUNET_HELLO_Address *address,
1134 struct Session *session)
1136 struct PendingMessage *p;
1137 struct AddressDestroyedMessage *m;
1141 uint32_t session_id;
1143 GNUNET_assert (address->transport_name != NULL);
1144 namelen = strlen (address->transport_name) + 1;
1145 GNUNET_assert (namelen > 1);
1147 sizeof (struct AddressDestroyedMessage) + address->address_length +
1149 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1150 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1151 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1157 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1159 p->is_init = GNUNET_NO;
1160 m = (struct AddressDestroyedMessage *) &p[1];
1161 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1162 m->header.size = htons (msize);
1163 m->reserved = htonl (0);
1164 m->peer = address->peer;
1165 m->address_length = htons (address->address_length);
1166 m->plugin_name_length = htons (namelen);
1167 session_id = get_session_id (sh, session, &address->peer);
1168 m->session_id = htonl (session_id);
1169 pm = (char *) &m[1];
1170 memcpy (pm, address->address, address->address_length);
1171 memcpy (&pm[address->address_length], address->transport_name, namelen);
1172 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1174 remove_session (sh, session_id, &address->peer);
1177 /* end of ats_api_scheduling.c */