2 This file is part of GNUnet.
3 (C) 2010-2015 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"
31 #define INTERFACE_PROCESSING_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
36 * Message in linked list we should send to the ATS service. The
37 * actual binary message follows this struct.
45 struct PendingMessage *next;
50 struct PendingMessage *prev;
53 * Size of the message.
58 * Is this the 'ATS_START' message?
65 * Information we track per session.
70 * Identity of the peer (just needed for error checking).
72 struct GNUNET_PeerIdentity peer;
77 struct Session *session;
80 * Set to #GNUNET_YES if the slot is used.
88 struct ATS_Network * next;
90 struct ATS_Network * prev;
92 struct sockaddr *network;
94 struct sockaddr *netmask;
101 * Handle for address suggestions
103 struct GNUNET_ATS_SuggestHandle
105 struct GNUNET_ATS_SuggestHandle *prev;
107 struct GNUNET_ATS_SuggestHandle *next;
109 struct GNUNET_PeerIdentity id;
114 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
116 struct GNUNET_ATS_SchedulingHandle
122 const struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Callback to invoke on suggestions.
127 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
130 * Closure for @e suggest_cb.
132 void *suggest_cb_cls;
135 * DLL for suggestions head
137 struct GNUNET_ATS_SuggestHandle *sug_head;
140 * DLL for suggestions tail
142 struct GNUNET_ATS_SuggestHandle *sug_tail;
145 * Connection to ATS service.
147 struct GNUNET_CLIENT_Connection *client;
150 * Head of list of messages for the ATS service.
152 struct PendingMessage *pending_head;
155 * Tail of list of messages for the ATS service
157 struct PendingMessage *pending_tail;
160 * Current request for transmission to ATS.
162 struct GNUNET_CLIENT_TransmitHandle *th;
165 * Head of network list
167 struct ATS_Network *net_head;
170 * Tail of network list
172 struct ATS_Network *net_tail;
175 * Array of session objects (we need to translate them to numbers and back
176 * for the protocol; the offset in the array is the session number on the
177 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
178 * Unused entries are also NULL.
180 struct SessionRecord *session_array;
183 * Task to trigger reconnect.
185 struct GNUNET_SCHEDULER_Task *task;
188 * Task retrieving interfaces from the system
190 struct GNUNET_SCHEDULER_Task *interface_task;
193 * Size of the @e session_array.
195 unsigned int session_array_size;
198 * Should we reconnect to ATS due to some serious error?
205 * Re-establish the connection to the ATS service.
207 * @param sh handle to use to re-connect.
210 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
214 * Re-establish the connection to the ATS service.
216 * @param cls handle to use to re-connect.
217 * @param tc scheduler context
220 reconnect_task (void *cls,
221 const struct GNUNET_SCHEDULER_TaskContext *tc)
223 struct GNUNET_ATS_SchedulingHandle *sh = cls;
231 * Disconnect from ATS and then reconnect.
233 * @param sh our handle
236 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
238 sh->reconnect = GNUNET_NO;
239 GNUNET_CLIENT_disconnect (sh->client);
241 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
248 * Transmit messages from the message queue to the service
249 * (if there are any, and if we are not already trying).
251 * @param sh handle to use
254 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
258 * Type of a function to call when we receive a message
261 * @param cls the `struct GNUNET_ATS_SchedulingHandle`
262 * @param msg message received, NULL on timeout or fatal error
265 process_ats_message (void *cls,
266 const struct GNUNET_MessageHeader *msg);
270 * We can now transmit a message to ATS. Do it.
272 * @param cls the `struct GNUNET_ATS_SchedulingHandle`
273 * @param size number of bytes we can transmit to ATS
274 * @param buf where to copy the messages
275 * @return number of bytes copied into @a buf
278 transmit_message_to_ats (void *cls,
282 struct GNUNET_ATS_SchedulingHandle *sh = cls;
283 struct PendingMessage *p;
288 if ((0 == size) || (NULL == buf))
290 force_reconnect (sh);
295 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
302 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
313 * Transmit messages from the message queue to the service
314 * (if there are any, and if we are not already trying).
316 * @param sh handle to use
319 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
321 struct PendingMessage *p;
325 if (NULL == (p = sh->pending_head))
327 if (NULL == sh->client)
328 return; /* currently reconnecting */
330 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
331 GNUNET_TIME_UNIT_FOREVER_REL,
332 GNUNET_NO, &transmit_message_to_ats,
338 * Find the session object corresponding to the given session ID.
340 * @param sh our handle
341 * @param session_id current session ID
342 * @param peer peer the session belongs to
343 * @return the session object (or NULL)
345 static struct Session *
346 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
348 const struct GNUNET_PeerIdentity *peer)
350 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
351 "Find session %u from peer %s in %p\n",
352 (unsigned int) session_id, GNUNET_i2s (peer), sh);
354 if (session_id >= sh->session_array_size)
361 if (sh->session_array[session_id].session == NULL)
364 memcmp (peer, &sh->session_array[session_id].peer,
365 sizeof (struct GNUNET_PeerIdentity)));
370 memcmp (peer, &sh->session_array[session_id].peer,
371 sizeof (struct GNUNET_PeerIdentity)))
374 sh->reconnect = GNUNET_YES;
377 /* This check exploits the fact that first field of a session object
381 memcmp (peer, sh->session_array[session_id].session,
382 sizeof (struct GNUNET_PeerIdentity)))
384 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
385 "Session %p belongs to peer `%s'\n",
386 sh->session_array[session_id].session, GNUNET_i2s_full ((struct GNUNET_PeerIdentity *) &sh->session_array[session_id].peer));
389 sh->reconnect = GNUNET_YES;
393 return sh->session_array[session_id].session;
398 * Get an available session ID for the given session object.
400 * @param sh our handle
401 * @param session session object
402 * @param peer peer the session belongs to
403 * @return the session id
406 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh,
407 struct Session *session,
408 const struct GNUNET_PeerIdentity *peer)
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
414 "ats-scheduling-api",
415 "Get session ID for session %p from peer %s in %p\n",
422 for (i = 1; i < sh->session_array_size; i++)
424 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
429 f = sh->session_array_size;
430 GNUNET_array_grow (sh->session_array, sh->session_array_size,
431 sh->session_array_size * 2);
433 GNUNET_assert (f > 0);
434 sh->session_array[f].session = session;
435 sh->session_array[f].peer = *peer;
436 sh->session_array[f].slot_used = GNUNET_YES;
438 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
439 "Assigning session ID %u for session %p of peer %s in %p\n", f,
440 session, GNUNET_i2s (peer), sh);
447 * Get the ID for the given session object.
449 * @param sh our handle
450 * @param session session object
451 * @param peer peer the session belongs to
452 * @return the session id or NOT_FOUND for error
455 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
456 struct Session *session,
457 const struct GNUNET_PeerIdentity *peer)
462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
463 "Get session ID for session %p from peer %s in %p\n", session,
464 GNUNET_i2s (peer), sh);
468 for (i = 1; i < sh->session_array_size; i++)
470 if (session == sh->session_array[i].session)
472 if (0 != memcmp (peer, &sh->session_array[i].peer,
473 sizeof (struct GNUNET_PeerIdentity)))
475 p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
476 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
477 "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
478 session, GNUNET_i2s (peer), p2);
490 * Remove the session of the given session ID from the session
491 * table (it is no longer valid).
493 * @param sh our handle
494 * @param session_id identifies session that is no longer valid
495 * @param peer peer the session belongs to
498 remove_session (struct GNUNET_ATS_SchedulingHandle *sh,
500 const struct GNUNET_PeerIdentity *peer)
502 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
503 "ats-scheduling-api",
504 "Release sessionID %u from peer %s in %p\n",
505 (unsigned int) session_id,
512 GNUNET_assert (session_id < sh->session_array_size);
513 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
514 GNUNET_assert (0 == memcmp (peer,
515 &sh->session_array[session_id].peer,
516 sizeof (struct GNUNET_PeerIdentity)));
517 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
518 "Session %p for peer `%s' removed from slot %u \n",
519 sh->session_array[session_id].session,
522 sh->session_array[session_id].session = NULL;
527 * Release the session slot from the session table (ATS service is
528 * also done using it).
530 * @param sh our handle
531 * @param session_id identifies session that is no longer valid
532 * @param peer peer the session belongs to
535 release_session (struct GNUNET_ATS_SchedulingHandle *sh,
537 const struct GNUNET_PeerIdentity *peer)
540 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
541 "Release sessionID %u from peer %s in %p\n",
542 (unsigned int) session_id, GNUNET_i2s (peer), sh);
544 if (session_id >= sh->session_array_size)
547 sh->reconnect = GNUNET_YES;
551 /* this slot should have been removed from remove_session before */
552 GNUNET_assert (sh->session_array[session_id].session == NULL);
555 memcmp (peer, &sh->session_array[session_id].peer,
556 sizeof (struct GNUNET_PeerIdentity)))
559 sh->reconnect = GNUNET_YES;
562 sh->session_array[session_id].slot_used = GNUNET_NO;
563 memset (&sh->session_array[session_id].peer, 0,
564 sizeof (struct GNUNET_PeerIdentity));
569 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
570 const struct SessionReleaseMessage *srm)
572 release_session (sh, ntohl (srm->session_id), &srm->peer);
577 * Type of a function to call when we receive a message
580 * @param cls the `struct GNUNET_ATS_SchedulingHandle`
581 * @param msg message received, NULL on timeout or fatal error
584 process_ats_message (void *cls,
585 const struct GNUNET_MessageHeader *msg)
587 struct GNUNET_ATS_SchedulingHandle *sh = cls;
588 const struct AddressSuggestionMessage *m;
589 const struct GNUNET_ATS_Information *atsi;
590 const char *plugin_address;
591 const char *plugin_name;
592 uint16_t plugin_address_length;
593 uint16_t plugin_name_length;
595 struct GNUNET_HELLO_Address address;
600 force_reconnect (sh);
603 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
604 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
606 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
607 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
608 GNUNET_TIME_UNIT_FOREVER_REL);
609 if (GNUNET_YES == sh->reconnect)
610 force_reconnect (sh);
613 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
614 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
617 force_reconnect (sh);
620 m = (const struct AddressSuggestionMessage *) msg;
621 ats_count = ntohl (m->ats_count);
622 plugin_address_length = ntohs (m->address_length);
623 atsi = (const struct GNUNET_ATS_Information *) &m[1];
624 plugin_address = (const char *) &atsi[ats_count];
625 plugin_name = &plugin_address[plugin_address_length];
626 plugin_name_length = ntohs (m->plugin_name_length);
627 if ((plugin_address_length + plugin_name_length +
628 ats_count * sizeof (struct GNUNET_ATS_Information) +
629 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
631 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
632 || (plugin_name[plugin_name_length - 1] != '\0'))
635 force_reconnect (sh);
638 uint32_t session_id = ntohl (m->session_id);
644 s = find_session (sh, session_id, &m->peer);
648 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
649 "ATS tries to use outdated session `%s'\n",
650 GNUNET_i2s (&m->peer));
651 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
652 GNUNET_TIME_UNIT_FOREVER_REL);
657 if (NULL == sh->suggest_cb)
660 address.peer = m->peer;
661 address.address = plugin_address;
662 address.address_length = plugin_address_length;
663 address.transport_name = plugin_name;
664 address.local_info = ntohl(m->address_local_info);
666 if ((s == NULL) && (0 == address.address_length))
668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
669 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
670 GNUNET_i2s (&address.peer), address.transport_name,
671 plugin_address_length, session_id);
673 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
674 GNUNET_TIME_UNIT_FOREVER_REL);
678 sh->suggest_cb (sh->suggest_cb_cls,
679 (const struct GNUNET_PeerIdentity *) &m->peer,
680 &address, s, m->bandwidth_out,
681 m->bandwidth_in, atsi, ats_count);
683 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
684 GNUNET_TIME_UNIT_FOREVER_REL);
685 if (GNUNET_YES == sh->reconnect)
686 force_reconnect (sh);
691 * Re-establish the connection to the ATS service.
693 * @param sh handle to use to re-connect.
696 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
698 struct PendingMessage *p;
699 struct ClientStartMessage *init;
701 GNUNET_assert (NULL == sh->client);
702 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
703 GNUNET_assert (NULL != sh->client);
704 GNUNET_CLIENT_receive (sh->client,
705 &process_ats_message, sh,
706 GNUNET_TIME_UNIT_FOREVER_REL);
707 if ( (NULL == (p = sh->pending_head)) ||
708 (GNUNET_YES != p->is_init) )
710 p = GNUNET_malloc (sizeof (struct PendingMessage) +
711 sizeof (struct ClientStartMessage));
712 p->size = sizeof (struct ClientStartMessage);
713 p->is_init = GNUNET_YES;
714 init = (struct ClientStartMessage *) &p[1];
715 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
716 init->header.size = htons (sizeof (struct ClientStartMessage));
717 init->start_flag = htonl (START_FLAG_SCHEDULING);
718 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
727 * Delete the current network list.
729 * @param sh scheduling handle to clean up
732 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
734 struct ATS_Network *cur;
736 while (NULL != (cur = sh->net_head))
738 GNUNET_CONTAINER_DLL_remove (sh->net_head,
747 interface_proc (void *cls,
750 const struct sockaddr *addr,
751 const struct sockaddr *broadcast_addr,
752 const struct sockaddr *netmask,
755 struct GNUNET_ATS_SchedulingHandle *sh = cls;
756 /* Calculate network */
757 struct ATS_Network *net = NULL;
759 /* Skipping IPv4 loopback addresses since we have special check */
760 if (addr->sa_family == AF_INET)
762 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
764 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
767 /* Skipping IPv6 loopback addresses since we have special check */
768 if (addr->sa_family == AF_INET6)
770 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
771 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
775 if (addr->sa_family == AF_INET)
777 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
778 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
779 struct sockaddr_in *tmp = NULL;
780 struct sockaddr_in network4;
782 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
783 tmp = (struct sockaddr_in *) &net[1];
784 net->network = (struct sockaddr *) &tmp[0];
785 net->netmask = (struct sockaddr *) &tmp[1];
786 net->length = addrlen;
788 memset (&network4, 0, sizeof (network4));
789 network4.sin_family = AF_INET;
790 #if HAVE_SOCKADDR_IN_SIN_LEN
791 network4.sin_len = sizeof (network4);
793 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
795 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
796 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
799 if (addr->sa_family == AF_INET6)
801 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
802 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
803 struct sockaddr_in6 * tmp = NULL;
804 struct sockaddr_in6 network6;
806 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
807 tmp = (struct sockaddr_in6 *) &net[1];
808 net->network = (struct sockaddr *) &tmp[0];
809 net->netmask = (struct sockaddr *) &tmp[1];
810 net->length = addrlen;
812 memset (&network6, 0, sizeof (network6));
813 network6.sin6_family = AF_INET6;
814 #if HAVE_SOCKADDR_IN_SIN_LEN
815 network6.sin6_len = sizeof (network6);
818 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
819 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
820 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
821 for (c = 0; c < 4; c++)
822 net_elem[c] = addr_elem[c] & mask_elem[c];
824 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
825 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
832 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
834 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
836 GNUNET_free (netmask);
838 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
845 * Periodically get list of network addresses from our interfaces.
848 * @param tc Task context
851 get_addresses (void *cls,
852 const struct GNUNET_SCHEDULER_TaskContext *tc)
854 struct GNUNET_ATS_SchedulingHandle *sh = cls;
856 sh->interface_task = NULL;
857 delete_networks (sh);
858 GNUNET_OS_network_interfaces_list (&interface_proc,
860 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
867 * Convert a `enum GNUNET_ATS_Network_Type` to a string
869 * @param net the network type
870 * @return a string or NULL if invalid
873 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net)
877 case GNUNET_ATS_NET_UNSPECIFIED:
878 return "UNSPECIFIED";
879 case GNUNET_ATS_NET_LOOPBACK:
881 case GNUNET_ATS_NET_LAN:
883 case GNUNET_ATS_NET_WAN:
885 case GNUNET_ATS_NET_WLAN:
887 case GNUNET_ATS_NET_BT:
896 * Convert a ATS property to a string
898 * @param type the property type
899 * @return a string or NULL if invalid
902 GNUNET_ATS_print_property_type (enum GNUNET_ATS_Property type)
906 case GNUNET_ATS_ARRAY_TERMINATOR:
908 case GNUNET_ATS_UTILIZATION_OUT:
909 return "UTILIZATION_UP";
910 case GNUNET_ATS_UTILIZATION_IN:
911 return "UTILIZATION_DOWN";
912 case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT:
913 return "UTILIZATION_PAYLOAD_UP";
914 case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
915 return "UTILIZATION_PAYLOAD_DOWN";
916 case GNUNET_ATS_NETWORK_TYPE:
917 return "NETWORK_TYPE";
918 case GNUNET_ATS_QUALITY_NET_DELAY:
920 case GNUNET_ATS_QUALITY_NET_DISTANCE:
922 case GNUNET_ATS_COST_WAN:
924 case GNUNET_ATS_COST_LAN:
926 case GNUNET_ATS_COST_WLAN:
935 * Returns where the address is located: LAN or WAN or ...
937 * @param sh the scheduling handle
938 * @param addr address
939 * @param addrlen address length
940 * @return type of the network the address belongs to
942 enum GNUNET_ATS_Network_Type
943 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle *sh,
944 const struct sockaddr *addr,
947 struct ATS_Network *cur = sh->net_head;
948 enum GNUNET_ATS_NetworkType type = GNUNET_ATS_NET_UNSPECIFIED;
950 switch (addr->sa_family)
953 type = GNUNET_ATS_NET_LOOPBACK;
957 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
959 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
960 type = GNUNET_ATS_NET_LOOPBACK;
965 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
967 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
968 type = GNUNET_ATS_NET_LOOPBACK;
976 /* Check local networks */
977 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
979 if (addrlen != cur->length)
984 if (addr->sa_family == AF_INET)
986 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
987 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
988 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
990 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
991 type = GNUNET_ATS_NET_LAN;
993 if (addr->sa_family == AF_INET6)
995 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
996 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
997 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
999 int res = GNUNET_YES;
1001 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
1002 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
1003 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
1004 for (c = 0; c < 4; c++)
1005 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
1008 if (res == GNUNET_YES)
1009 type = GNUNET_ATS_NET_LAN;
1014 /* no local network found for this address, default: WAN */
1015 if (type == GNUNET_ATS_NET_UNSPECIFIED)
1016 type = GNUNET_ATS_NET_WAN;
1017 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1018 "ats-scheduling-api",
1019 "`%s' is in network `%s'\n",
1022 GNUNET_ATS_print_network_type (type));
1028 * Initialize the ATS subsystem.
1030 * @param cfg configuration to use
1031 * @param suggest_cb notification to call whenever the suggestation changed
1032 * @param suggest_cb_cls closure for @a suggest_cb
1033 * @return ats context
1035 struct GNUNET_ATS_SchedulingHandle *
1036 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1037 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
1038 void *suggest_cb_cls)
1040 struct GNUNET_ATS_SchedulingHandle *sh;
1042 sh = GNUNET_new (struct GNUNET_ATS_SchedulingHandle);
1044 sh->suggest_cb = suggest_cb;
1045 sh->suggest_cb_cls = suggest_cb_cls;
1046 GNUNET_array_grow (sh->session_array,
1047 sh->session_array_size,
1049 GNUNET_OS_network_interfaces_list (&interface_proc,
1051 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
1060 * Client is done with ATS scheduling, release resources.
1062 * @param sh handle to release
1065 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
1067 struct PendingMessage *p;
1068 struct GNUNET_ATS_SuggestHandle *cur;
1070 while (NULL != (p = sh->pending_head))
1072 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
1077 if (NULL != sh->client)
1079 GNUNET_CLIENT_disconnect (sh->client);
1082 if (NULL != sh->task)
1084 GNUNET_SCHEDULER_cancel (sh->task);
1087 while (NULL != (cur = sh->sug_head))
1089 GNUNET_CONTAINER_DLL_remove (sh->sug_head,
1094 delete_networks (sh);
1095 if (NULL != sh->interface_task)
1097 GNUNET_SCHEDULER_cancel (sh->interface_task);
1098 sh->interface_task = NULL;
1100 GNUNET_array_grow (sh->session_array,
1101 sh->session_array_size,
1108 * We would like to reset the address suggestion block time for this
1112 * @param peer identity of the peer we want to reset
1115 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
1116 const struct GNUNET_PeerIdentity *peer)
1118 struct PendingMessage *p;
1119 struct ResetBackoffMessage *m;
1121 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1122 sizeof (struct ResetBackoffMessage));
1123 p->size = sizeof (struct ResetBackoffMessage);
1124 p->is_init = GNUNET_NO;
1125 m = (struct ResetBackoffMessage *) &p[1];
1126 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
1127 m->header.size = htons (sizeof (struct ResetBackoffMessage));
1128 m->reserved = htonl (0);
1130 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
1138 * We would like to receive address suggestions for a peer. ATS will
1139 * respond with a call to the continuation immediately containing an address or
1140 * no address if none is available. ATS can suggest more addresses until we call
1141 * #GNUNET_ATS_suggest_address_cancel().
1144 * @param peer identity of the peer we need an address for
1145 * @return suggest handle
1147 struct GNUNET_ATS_SuggestHandle *
1148 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1149 const struct GNUNET_PeerIdentity *peer)
1151 struct PendingMessage *p;
1152 struct RequestAddressMessage *m;
1153 struct GNUNET_ATS_SuggestHandle *s;
1155 // FIXME: ATS needs to remember this in case of
1157 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1158 sizeof (struct RequestAddressMessage));
1159 p->size = sizeof (struct RequestAddressMessage);
1160 m = (struct RequestAddressMessage *) &p[1];
1161 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1162 m->header.size = htons (sizeof (struct RequestAddressMessage));
1163 m->reserved = htonl (0);
1165 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
1169 s = GNUNET_new (struct GNUNET_ATS_SuggestHandle);
1171 GNUNET_CONTAINER_DLL_insert_tail (sh->sug_head,
1179 * We would like to stop receiving address updates for this peer
1182 * @param peer identity of the peer
1185 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1186 const struct GNUNET_PeerIdentity *peer)
1188 struct PendingMessage *p;
1189 struct RequestAddressMessage *m;
1190 struct GNUNET_ATS_SuggestHandle *s;
1192 for (s = sh->sug_head; NULL != s; s = s->next)
1193 if (0 == memcmp (peer, &s->id, sizeof (s->id)))
1200 GNUNET_CONTAINER_DLL_remove (sh->sug_head,
1205 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1206 sizeof (struct RequestAddressMessage));
1207 p->size = sizeof (struct RequestAddressMessage);
1208 p->is_init = GNUNET_NO;
1209 m = (struct RequestAddressMessage *) &p[1];
1210 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1211 m->header.size = htons (sizeof (struct RequestAddressMessage));
1212 m->reserved = htonl (0);
1214 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
1222 * Test if a address and a session is known to ATS
1224 * @param sh the scheduling handle
1225 * @param address the address
1226 * @param session the session
1227 * @return #GNUNET_YES or #GNUNET_NO
1230 GNUNET_ATS_session_known (struct GNUNET_ATS_SchedulingHandle *sh,
1231 const struct GNUNET_HELLO_Address *address,
1232 struct Session *session)
1234 if (NULL == session)
1236 if (NOT_FOUND != find_session_id (sh,
1239 return GNUNET_YES; /* Exists */
1245 * We have a new address ATS should know. Addresses have to be added with this
1246 * function before they can be: updated, set in use and destroyed
1249 * @param address the address
1250 * @param session session handle, can be NULL
1251 * @param ats performance data for the address
1252 * @param ats_count number of performance records in @a ats
1253 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1256 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1257 const struct GNUNET_HELLO_Address *address,
1258 struct Session *session,
1259 const struct GNUNET_ATS_Information *ats,
1263 struct PendingMessage *p;
1264 struct AddressUpdateMessage *m;
1265 struct GNUNET_ATS_Information *am;
1271 if (NULL == address)
1274 return GNUNET_SYSERR;
1277 namelen = (NULL == address->transport_name)
1279 : strlen (address->transport_name) + 1;
1281 msize = sizeof (struct AddressUpdateMessage) + address->address_length +
1282 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1283 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1284 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1285 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1287 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1290 return GNUNET_SYSERR;
1293 if (NULL != session)
1295 if (NOT_FOUND != (s = find_session_id (sh, session, &address->peer)))
1297 /* Already existing, nothing todo */
1298 return GNUNET_SYSERR;
1300 s = find_empty_session_slot (sh,
1303 GNUNET_break (NOT_FOUND != s);
1306 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1308 p->is_init = GNUNET_NO;
1309 m = (struct AddressUpdateMessage *) &p[1];
1310 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1311 m->header.size = htons (msize);
1312 m->ats_count = htonl (ats_count);
1313 m->peer = address->peer;
1314 m->address_length = htons (address->address_length);
1315 m->address_local_info = htonl ((uint32_t) address->local_info);
1316 m->plugin_name_length = htons (namelen);
1317 m->session_id = htonl (s);
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1321 GNUNET_i2s (&address->peer),
1322 address->transport_name,
1326 am = (struct GNUNET_ATS_Information *) &m[1];
1327 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1328 pm = (char *) &am[ats_count];
1329 memcpy (pm, address->address, address->address_length);
1330 if (NULL != address->transport_name)
1331 memcpy (&pm[address->address_length],
1332 address->transport_name,
1334 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
1343 * We have updated performance statistics for a given address. Note
1344 * that this function can be called for addresses that are currently
1345 * in use as well as addresses that are valid but not actively in use.
1346 * Furthermore, the peer may not even be connected to us right now (in
1347 * which case the call may be ignored or the information may be stored
1348 * for later use). Update bandwidth assignments.
1351 * @param address the address
1352 * @param session session handle, can be NULL
1353 * @param ats performance data for the address
1354 * @param ats_count number of performance records in @a ats
1355 * @return #GNUNET_YES on success, #GNUNET_NO if address or session are unknown,
1356 * #GNUNET_SYSERR on hard failure
1359 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1360 const struct GNUNET_HELLO_Address *address,
1361 struct Session *session,
1362 const struct GNUNET_ATS_Information *ats,
1365 struct PendingMessage *p;
1366 struct AddressUpdateMessage *m;
1367 struct GNUNET_ATS_Information *am;
1373 if (NULL == address)
1376 return GNUNET_SYSERR;
1381 return GNUNET_SYSERR;
1384 namelen = (address->transport_name ==
1385 NULL) ? 0 : strlen (address->transport_name) + 1;
1387 sizeof (struct AddressUpdateMessage) + address->address_length +
1388 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1389 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1390 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1391 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1393 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1396 return GNUNET_SYSERR;
1399 if (NULL != session)
1401 s = find_session_id (sh, session, &address->peer);
1406 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1408 p->is_init = GNUNET_NO;
1409 m = (struct AddressUpdateMessage *) &p[1];
1410 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1411 m->header.size = htons (msize);
1412 m->ats_count = htonl (ats_count);
1413 m->peer = address->peer;
1414 m->address_length = htons (address->address_length);
1415 m->address_local_info = htonl ((uint32_t) address->local_info);
1416 m->plugin_name_length = htons (namelen);
1418 m->session_id = htonl (s);
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1422 GNUNET_i2s (&address->peer),
1423 address->transport_name,
1427 am = (struct GNUNET_ATS_Information *) &m[1];
1430 ats_count * sizeof (struct GNUNET_ATS_Information));
1431 pm = (char *) &am[ats_count];
1432 memcpy (pm, address->address, address->address_length);
1433 memcpy (&pm[address->address_length], address->transport_name, namelen);
1434 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1441 * An address is now in use or not used any more.
1444 * @param address the address
1445 * @param session session handle, can be NULL
1446 * @param in_use #GNUNET_YES if this address is now used, #GNUNET_NO
1447 * if address is not used any more
1450 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1451 const struct GNUNET_HELLO_Address *address,
1452 struct Session *session,
1455 struct PendingMessage *p;
1456 struct AddressUseMessage *m;
1462 GNUNET_assert (NULL != address);
1464 (address->transport_name ==
1465 NULL) ? 0 : strlen (address->transport_name) + 1;
1466 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1467 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1468 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1469 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1475 if (session != NULL)
1477 s = find_session_id (sh, session, &address->peer);
1478 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1480 /* trying to set unknown address to NO */
1481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1482 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1483 GNUNET_i2s (&address->peer), address->transport_name, session);
1487 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1489 /* trying to set new address to YES */
1490 s = find_empty_session_slot (sh, session, &address->peer);
1491 GNUNET_assert (NOT_FOUND != s);
1495 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1497 p->is_init = GNUNET_NO;
1498 m = (struct AddressUseMessage *) &p[1];
1499 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1500 m->header.size = htons (msize);
1501 m->peer = address->peer;
1502 m->in_use = htons (in_use);
1503 m->address_length = htons (address->address_length);
1504 m->address_local_info = htonl ((uint32_t) address->local_info);
1505 m->plugin_name_length = htons (namelen);
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1509 (GNUNET_YES == in_use) ? "YES" : "NO",
1510 GNUNET_i2s (&address->peer), address->transport_name, session);
1512 m->session_id = htonl (s);
1513 pm = (char *) &m[1];
1514 memcpy (pm, address->address, address->address_length);
1515 memcpy (&pm[address->address_length], address->transport_name, namelen);
1516 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1522 * An address got destroyed, stop including it as a valid address.
1524 * If a session is given, only the session will be removed, if no session is
1525 * given the full address will be deleted.
1528 * @param address the address
1529 * @param session session handle that is no longer valid, can be NULL
1532 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1533 const struct GNUNET_HELLO_Address *address,
1534 struct Session *session)
1536 struct PendingMessage *p;
1537 struct AddressDestroyedMessage *m;
1543 if (NULL == address)
1549 GNUNET_assert (address->transport_name != NULL);
1550 namelen = strlen (address->transport_name) + 1;
1551 GNUNET_assert (namelen > 1);
1553 sizeof (struct AddressDestroyedMessage) + address->address_length +
1555 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1556 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1557 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1563 s = find_session_id (sh, session, &address->peer);
1564 if ((NULL != session) && (NOT_FOUND == s))
1566 /* trying to delete unknown address */
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1569 GNUNET_i2s (&address->peer), address->transport_name, session);
1573 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1575 p->is_init = GNUNET_NO;
1576 m = (struct AddressDestroyedMessage *) &p[1];
1577 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1578 m->header.size = htons (msize);
1579 m->reserved = htonl (0);
1580 m->peer = address->peer;
1581 m->address_length = htons (address->address_length);
1582 m->address_local_info = htonl ((uint32_t) address->local_info);
1583 m->plugin_name_length = htons (namelen);
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Deleting address for peer `%s', plugin `%s', session %p\n",
1587 GNUNET_i2s (&address->peer), address->transport_name, session);
1589 m->session_id = htonl (s);
1590 pm = (char *) &m[1];
1591 memcpy (pm, address->address, address->address_length);
1592 memcpy (&pm[address->address_length], address->transport_name, namelen);
1593 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1595 remove_session (sh, s, &address->peer);
1598 /* end of ats_api_scheduling.c */