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"
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;
194 * Size of the session array.
196 unsigned int session_array_size;
199 * Should we reconnect to ATS due to some serious error?
206 * Re-establish the connection to the ATS service.
208 * @param sh handle to use to re-connect.
211 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
215 * Re-establish the connection to the ATS service.
217 * @param cls handle to use to re-connect.
218 * @param tc scheduler context
221 reconnect_task (void *cls, 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);
242 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
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, const struct GNUNET_MessageHeader *msg);
269 * We can now transmit a message to ATS. Do it.
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
277 transmit_message_to_ats (void *cls, size_t size, void *buf)
279 struct GNUNET_ATS_SchedulingHandle *sh = cls;
280 struct PendingMessage *p;
285 if ((size == 0) || (buf == NULL))
287 force_reconnect (sh);
292 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
294 memcpy (&cbuf[ret], &p[1], p->size);
297 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
306 * Transmit messages from the message queue to the service
307 * (if there are any, and if we are not already trying).
309 * @param sh handle to use
312 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
314 struct PendingMessage *p;
318 if (NULL == (p = sh->pending_head))
320 if (NULL == sh->client)
321 return; /* currently reconnecting */
323 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
324 GNUNET_TIME_UNIT_FOREVER_REL,
325 GNUNET_NO, &transmit_message_to_ats,
331 * Find the session object corresponding to the given session ID.
333 * @param sh our handle
334 * @param session_id current session ID
335 * @param peer peer the session belongs to
336 * @return the session object (or NULL)
338 static struct Session *
339 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
340 const struct GNUNET_PeerIdentity *peer)
343 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
344 "Find session %u from peer %s in %p\n",
345 (unsigned int) session_id, GNUNET_i2s (peer), sh);
347 if (session_id >= sh->session_array_size)
354 if (sh->session_array[session_id].session == NULL)
357 memcmp (peer, &sh->session_array[session_id].peer,
358 sizeof (struct GNUNET_PeerIdentity)));
363 memcmp (peer, &sh->session_array[session_id].peer,
364 sizeof (struct GNUNET_PeerIdentity)))
367 sh->reconnect = GNUNET_YES;
370 /* This check exploits the fact that first field of a session object
374 memcmp (peer, sh->session_array[session_id].session,
375 sizeof (struct GNUNET_PeerIdentity)))
377 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
378 "Session %p belongs to peer `%s'\n",
379 sh->session_array[session_id].session, GNUNET_i2s_full ((struct GNUNET_PeerIdentity *) &sh->session_array[session_id].peer));
382 sh->reconnect = GNUNET_YES;
386 return sh->session_array[session_id].session;
391 * Get an available session ID for the given session object.
393 * @param sh our handle
394 * @param session session object
395 * @param peer peer the session belongs to
396 * @return the session id
399 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
400 const struct GNUNET_PeerIdentity *peer)
405 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
406 "Get session ID for session %p from peer %s in %p\n", session,
407 GNUNET_i2s (peer), sh);
412 for (i = 1; i < sh->session_array_size; i++)
414 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
419 f = sh->session_array_size;
420 GNUNET_array_grow (sh->session_array, sh->session_array_size,
421 sh->session_array_size * 2);
423 GNUNET_assert (f > 0);
424 sh->session_array[f].session = session;
425 sh->session_array[f].peer = *peer;
426 sh->session_array[f].slot_used = GNUNET_YES;
428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
429 "Assigning session ID %u for session %p of peer %s in %p\n", f,
430 session, GNUNET_i2s (peer), sh);
437 * Get the ID for the given session object.
439 * @param sh our handle
440 * @param session session object
441 * @param peer peer the session belongs to
442 * @return the session id or NOT_FOUND for error
445 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
446 const struct GNUNET_PeerIdentity *peer)
451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
452 "Get session ID for session %p from peer %s in %p\n", session,
453 GNUNET_i2s (peer), sh);
457 for (i = 1; i < sh->session_array_size; i++)
459 if (session == sh->session_array[i].session)
461 if (0 != memcmp (peer, &sh->session_array[i].peer,
462 sizeof (struct GNUNET_PeerIdentity)))
464 p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
465 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
466 "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
467 session, GNUNET_i2s (peer), p2);
479 * Remove the session of the given session ID from the session
480 * table (it is no longer valid).
482 * @param sh our handle
483 * @param session_id identifies session that is no longer valid
484 * @param peer peer the session belongs to
487 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
488 const struct GNUNET_PeerIdentity *peer)
490 GNUNET_assert (peer != NULL);
491 GNUNET_assert (sh != NULL);
493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
494 "Release sessionID %u from peer %s in %p\n",
495 (unsigned int) session_id, GNUNET_i2s (peer), sh);
500 GNUNET_assert (session_id < sh->session_array_size);
501 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
502 GNUNET_assert (0 == memcmp (peer,
503 &sh->session_array[session_id].peer,
504 sizeof (struct GNUNET_PeerIdentity)));
505 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
506 "Session %p for peer `%s' removed from slot %u \n",
507 sh->session_array[session_id].session,
510 sh->session_array[session_id].session = NULL;
516 * Release the session slot from the session table (ATS service is
517 * also done using it).
519 * @param sh our handle
520 * @param session_id identifies session that is no longer valid
521 * @param peer peer the session belongs to
524 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
525 const struct GNUNET_PeerIdentity *peer)
528 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
529 "Release sessionID %u from peer %s in %p\n",
530 (unsigned int) session_id, GNUNET_i2s (peer), sh);
532 if (session_id >= sh->session_array_size)
535 sh->reconnect = GNUNET_YES;
539 /* this slot should have been removed from remove_session before */
540 GNUNET_assert (sh->session_array[session_id].session == NULL);
543 memcmp (peer, &sh->session_array[session_id].peer,
544 sizeof (struct GNUNET_PeerIdentity)))
547 sh->reconnect = GNUNET_YES;
550 sh->session_array[session_id].slot_used = GNUNET_NO;
551 memset (&sh->session_array[session_id].peer, 0,
552 sizeof (struct GNUNET_PeerIdentity));
557 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
558 const struct SessionReleaseMessage *srm)
560 release_session (sh, ntohl (srm->session_id), &srm->peer);
565 * Type of a function to call when we receive a message
568 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
569 * @param msg message received, NULL on timeout or fatal error
572 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
574 struct GNUNET_ATS_SchedulingHandle *sh = cls;
575 const struct AddressSuggestionMessage *m;
576 const struct GNUNET_ATS_Information *atsi;
577 const char *plugin_address;
578 const char *plugin_name;
579 uint16_t plugin_address_length;
580 uint16_t plugin_name_length;
582 struct GNUNET_HELLO_Address address;
587 force_reconnect (sh);
590 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
591 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
593 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
594 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
595 GNUNET_TIME_UNIT_FOREVER_REL);
596 if (GNUNET_YES == sh->reconnect)
597 force_reconnect (sh);
600 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
601 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
604 force_reconnect (sh);
607 m = (const struct AddressSuggestionMessage *) msg;
608 ats_count = ntohl (m->ats_count);
609 plugin_address_length = ntohs (m->address_length);
610 atsi = (const struct GNUNET_ATS_Information *) &m[1];
611 plugin_address = (const char *) &atsi[ats_count];
612 plugin_name = &plugin_address[plugin_address_length];
613 plugin_name_length = ntohs (m->plugin_name_length);
614 if ((plugin_address_length + plugin_name_length +
615 ats_count * sizeof (struct GNUNET_ATS_Information) +
616 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
618 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
619 || (plugin_name[plugin_name_length - 1] != '\0'))
622 force_reconnect (sh);
625 uint32_t session_id = ntohl (m->session_id);
631 s = find_session (sh, session_id, &m->peer);
635 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
636 "ATS tries to use outdated session `%s'\n",
637 GNUNET_i2s (&m->peer));
638 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
639 GNUNET_TIME_UNIT_FOREVER_REL);
644 if (NULL == sh->suggest_cb)
647 address.peer = m->peer;
648 address.address = plugin_address;
649 address.address_length = plugin_address_length;
650 address.transport_name = plugin_name;
651 address.local_info = ntohl(m->address_local_info);
653 if ((s == NULL) && (0 == address.address_length))
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
657 GNUNET_i2s (&address.peer), address.transport_name,
658 plugin_address_length, session_id);
660 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
661 GNUNET_TIME_UNIT_FOREVER_REL);
665 sh->suggest_cb (sh->suggest_cb_cls,
666 (const struct GNUNET_PeerIdentity *) &m->peer,
667 &address, s, m->bandwidth_out,
668 m->bandwidth_in, atsi, ats_count);
670 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
671 GNUNET_TIME_UNIT_FOREVER_REL);
672 if (GNUNET_YES == sh->reconnect)
673 force_reconnect (sh);
678 * Re-establish the connection to the ATS service.
680 * @param sh handle to use to re-connect.
683 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
685 struct PendingMessage *p;
686 struct ClientStartMessage *init;
688 GNUNET_assert (NULL == sh->client);
689 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
690 GNUNET_assert (NULL != sh->client);
691 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
692 GNUNET_TIME_UNIT_FOREVER_REL);
693 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
695 p = GNUNET_malloc (sizeof (struct PendingMessage) +
696 sizeof (struct ClientStartMessage));
697 p->size = sizeof (struct ClientStartMessage);
698 p->is_init = GNUNET_YES;
699 init = (struct ClientStartMessage *) &p[1];
700 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
701 init->header.size = htons (sizeof (struct ClientStartMessage));
702 init->start_flag = htonl (START_FLAG_SCHEDULING);
703 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
710 * delete the current network list
713 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
715 struct ATS_Network * cur = sh->net_head;
718 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
726 interface_proc (void *cls, const char *name,
728 const struct sockaddr *
730 const struct sockaddr *
732 const struct sockaddr *
733 netmask, socklen_t addrlen)
735 struct GNUNET_ATS_SchedulingHandle * sh = cls;
736 /* Calculate network */
737 struct ATS_Network *net = NULL;
739 /* Skipping IPv4 loopback addresses since we have special check */
740 if (addr->sa_family == AF_INET)
742 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
744 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
747 /* Skipping IPv6 loopback addresses since we have special check */
748 if (addr->sa_family == AF_INET6)
750 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
751 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
755 if (addr->sa_family == AF_INET)
757 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
758 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
759 struct sockaddr_in *tmp = NULL;
760 struct sockaddr_in network4;
762 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
763 tmp = (struct sockaddr_in *) &net[1];
764 net->network = (struct sockaddr *) &tmp[0];
765 net->netmask = (struct sockaddr *) &tmp[1];
766 net->length = addrlen;
768 memset (&network4, 0, sizeof (network4));
769 network4.sin_family = AF_INET;
770 #if HAVE_SOCKADDR_IN_SIN_LEN
771 network4.sin_len = sizeof (network4);
773 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
775 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
776 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
779 if (addr->sa_family == AF_INET6)
781 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
782 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
783 struct sockaddr_in6 * tmp = NULL;
784 struct sockaddr_in6 network6;
786 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
787 tmp = (struct sockaddr_in6 *) &net[1];
788 net->network = (struct sockaddr *) &tmp[0];
789 net->netmask = (struct sockaddr *) &tmp[1];
790 net->length = addrlen;
792 memset (&network6, 0, sizeof (network6));
793 network6.sin6_family = AF_INET6;
794 #if HAVE_SOCKADDR_IN_SIN_LEN
795 network6.sin6_len = sizeof (network6);
798 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
799 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
800 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
801 for (c = 0; c < 4; c++)
802 net_elem[c] = addr_elem[c] & mask_elem[c];
804 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
805 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
812 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
814 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
816 GNUNET_free (netmask);
818 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
825 * Periodically get list of addresses
827 * @param tc Task context
830 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
832 struct GNUNET_ATS_SchedulingHandle * sh = cls;
833 sh->interface_task = NULL;
834 delete_networks (sh);
835 GNUNET_OS_network_interfaces_list(interface_proc, sh);
836 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
843 * Convert a `enum GNUNET_ATS_Network_Type` to a string
845 * @param net the network type
846 * @return a string or NULL if invalid
849 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net)
853 case GNUNET_ATS_NET_UNSPECIFIED:
854 return "UNSPECIFIED";
855 case GNUNET_ATS_NET_LOOPBACK:
857 case GNUNET_ATS_NET_LAN:
859 case GNUNET_ATS_NET_WAN:
861 case GNUNET_ATS_NET_WLAN:
863 case GNUNET_ATS_NET_BT:
872 * Convert a ATS property to a string
874 * @param type the property type
875 * @return a string or NULL if invalid
878 GNUNET_ATS_print_property_type (enum GNUNET_ATS_Property type)
882 case GNUNET_ATS_ARRAY_TERMINATOR:
884 case GNUNET_ATS_UTILIZATION_OUT:
885 return "UTILIZATION_UP";
886 case GNUNET_ATS_UTILIZATION_IN:
887 return "UTILIZATION_DOWN";
888 case GNUNET_ATS_UTILIZATION_PAYLOAD_OUT:
889 return "UTILIZATION_PAYLOAD_UP";
890 case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
891 return "UTILIZATION_PAYLOAD_DOWN";
892 case GNUNET_ATS_NETWORK_TYPE:
893 return "NETWORK_TYPE";
894 case GNUNET_ATS_QUALITY_NET_DELAY:
896 case GNUNET_ATS_QUALITY_NET_DISTANCE:
898 case GNUNET_ATS_COST_WAN:
900 case GNUNET_ATS_COST_LAN:
902 case GNUNET_ATS_COST_WLAN:
911 * Returns where the address is located: LAN or WAN or ...
913 * @param sh the scheduling handle
914 * @param addr address
915 * @param addrlen address length
916 * @return location as GNUNET_ATS_Information
918 struct GNUNET_ATS_Information
919 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
921 GNUNET_assert (sh != NULL);
922 struct ATS_Network * cur = sh->net_head;
924 int type = GNUNET_ATS_NET_UNSPECIFIED;
925 struct GNUNET_ATS_Information ats;
927 if (addr->sa_family == AF_UNIX)
929 type = GNUNET_ATS_NET_LOOPBACK;
932 /* IPv4 loopback check */
933 if (addr->sa_family == AF_INET)
935 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
937 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
938 type = GNUNET_ATS_NET_LOOPBACK;
940 /* IPv6 loopback check */
941 if (addr->sa_family == AF_INET6)
943 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
944 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
945 type = GNUNET_ATS_NET_LOOPBACK;
948 /* Check local networks */
949 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
951 if (addrlen != cur->length)
957 if (addr->sa_family == AF_INET)
959 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
960 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
961 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
963 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
964 type = GNUNET_ATS_NET_LAN;
966 if (addr->sa_family == AF_INET6)
968 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
969 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
970 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
972 int res = GNUNET_YES;
974 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
975 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
976 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
977 for (c = 0; c < 4; c++)
978 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
981 if (res == GNUNET_YES)
982 type = GNUNET_ATS_NET_LAN;
987 /* no local network found for this address, default: WAN */
988 if (type == GNUNET_ATS_NET_UNSPECIFIED)
989 type = GNUNET_ATS_NET_WAN;
990 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
991 ats.value = htonl (type);
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
994 "`%s' is in network `%s'\n",
995 GNUNET_a2s ((const struct sockaddr *) addr, addrlen),
996 GNUNET_ATS_print_network_type(type));
1002 * Initialize the ATS subsystem.
1004 * @param cfg configuration to use
1005 * @param suggest_cb notification to call whenever the suggestation changed
1006 * @param suggest_cb_cls closure for 'suggest_cb'
1007 * @return ats context
1009 struct GNUNET_ATS_SchedulingHandle *
1010 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1011 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
1012 void *suggest_cb_cls)
1014 struct GNUNET_ATS_SchedulingHandle *sh;
1016 sh = GNUNET_new (struct GNUNET_ATS_SchedulingHandle);
1018 sh->suggest_cb = suggest_cb;
1019 sh->suggest_cb_cls = suggest_cb_cls;
1020 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
1021 GNUNET_OS_network_interfaces_list(interface_proc, sh);
1022 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
1031 * Client is done with ATS scheduling, release resources.
1033 * @param sh handle to release
1036 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
1038 struct PendingMessage *p;
1039 struct GNUNET_ATS_SuggestHandle *cur;
1040 struct GNUNET_ATS_SuggestHandle *next;
1041 while (NULL != (p = sh->pending_head))
1043 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
1046 if (NULL != sh->client)
1048 GNUNET_CLIENT_disconnect (sh->client);
1051 if (NULL != sh->task)
1053 GNUNET_SCHEDULER_cancel (sh->task);
1057 next = sh->sug_head;
1058 while (NULL != (cur = next))
1061 GNUNET_CONTAINER_DLL_remove (sh->sug_head, sh->sug_tail, cur);
1065 delete_networks (sh);
1066 if (sh->interface_task != NULL)
1068 GNUNET_SCHEDULER_cancel(sh->interface_task);
1069 sh->interface_task = NULL;
1071 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
1077 * We would like to reset the address suggestion block time for this
1081 * @param peer identity of the peer we want to reset
1084 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
1085 const struct GNUNET_PeerIdentity *peer)
1087 struct PendingMessage *p;
1088 struct ResetBackoffMessage *m;
1090 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1091 sizeof (struct ResetBackoffMessage));
1092 p->size = sizeof (struct ResetBackoffMessage);
1093 p->is_init = GNUNET_NO;
1094 m = (struct ResetBackoffMessage *) &p[1];
1095 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
1096 m->header.size = htons (sizeof (struct ResetBackoffMessage));
1097 m->reserved = htonl (0);
1099 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1105 * We would like to receive address suggestions for a peer. ATS will
1106 * respond with a call to the continuation immediately containing an address or
1107 * no address if none is available. ATS can suggest more addresses until we call
1108 * #GNUNET_ATS_suggest_address_cancel().
1111 * @param peer identity of the peer we need an address for
1112 * @return suggest handle
1114 struct GNUNET_ATS_SuggestHandle *
1115 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1116 const struct GNUNET_PeerIdentity *peer)
1118 struct PendingMessage *p;
1119 struct RequestAddressMessage *m;
1120 struct GNUNET_ATS_SuggestHandle *s;
1122 // FIXME: ATS needs to remember this in case of
1124 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1125 sizeof (struct RequestAddressMessage));
1126 p->size = sizeof (struct RequestAddressMessage);
1127 m = (struct RequestAddressMessage *) &p[1];
1128 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1129 m->header.size = htons (sizeof (struct RequestAddressMessage));
1130 m->reserved = htonl (0);
1132 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
1136 s = GNUNET_new (struct GNUNET_ATS_SuggestHandle);
1138 GNUNET_CONTAINER_DLL_insert_tail (sh->sug_head,
1146 * We would like to stop receiving address updates for this peer
1149 * @param peer identity of the peer
1152 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1153 const struct GNUNET_PeerIdentity *peer)
1155 struct PendingMessage *p;
1156 struct RequestAddressMessage *m;
1157 struct GNUNET_ATS_SuggestHandle *s;
1159 for (s = sh->sug_head; NULL != s; s = s->next)
1160 if (0 == memcmp(peer, &s->id, sizeof (s->id)))
1169 GNUNET_CONTAINER_DLL_remove (sh->sug_head, sh->sug_tail, s);
1173 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1174 sizeof (struct RequestAddressMessage));
1175 p->size = sizeof (struct RequestAddressMessage);
1176 p->is_init = GNUNET_NO;
1177 m = (struct RequestAddressMessage *) &p[1];
1178 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1179 m->header.size = htons (sizeof (struct RequestAddressMessage));
1180 m->reserved = htonl (0);
1182 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1188 * Test if a address and a session is known to ATS
1190 * @param sh the scheduling handle
1191 * @param address the address
1192 * @param session the session
1193 * @return GNUNET_YES or GNUNET_NO
1196 GNUNET_ATS_session_known (struct GNUNET_ATS_SchedulingHandle *sh,
1197 const struct GNUNET_HELLO_Address *address,
1198 struct Session *session)
1201 if (NULL != session)
1203 if (NOT_FOUND != (s = find_session_id (sh, session, &address->peer)))
1214 * We have a new address ATS should know. Addresses have to be added with this
1215 * function before they can be: updated, set in use and destroyed
1218 * @param address the address
1219 * @param session session handle, can be NULL
1220 * @param ats performance data for the address
1221 * @param ats_count number of performance records in 'ats'
1222 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1225 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1226 const struct GNUNET_HELLO_Address *address,
1227 struct Session *session,
1228 const struct GNUNET_ATS_Information *ats,
1232 struct PendingMessage *p;
1233 struct AddressUpdateMessage *m;
1234 struct GNUNET_ATS_Information *am;
1240 if (address == NULL)
1243 return GNUNET_SYSERR;
1246 namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1;
1248 msize = sizeof (struct AddressUpdateMessage) + address->address_length +
1249 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1250 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1251 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1252 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1254 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1257 return GNUNET_SYSERR;
1260 if (NULL != session)
1262 if (NOT_FOUND != (s = find_session_id (sh, session, &address->peer)))
1264 /* Already existing, nothing todo */
1265 return GNUNET_SYSERR;
1267 s = find_empty_session_slot (sh, session, &address->peer);
1268 GNUNET_break (NOT_FOUND != s);
1271 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1273 p->is_init = GNUNET_NO;
1274 m = (struct AddressUpdateMessage *) &p[1];
1275 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1276 m->header.size = htons (msize);
1277 m->ats_count = htonl (ats_count);
1278 m->peer = address->peer;
1279 m->address_length = htons (address->address_length);
1280 m->address_local_info = htonl ((uint32_t) address->local_info);
1281 m->plugin_name_length = htons (namelen);
1282 m->session_id = htonl (s);
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1286 GNUNET_i2s (&address->peer),
1287 address->transport_name, session, s);
1289 am = (struct GNUNET_ATS_Information *) &m[1];
1290 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1291 pm = (char *) &am[ats_count];
1292 memcpy (pm, address->address, address->address_length);
1293 if (NULL != address->transport_name)
1294 memcpy (&pm[address->address_length], address->transport_name, namelen);
1295 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1303 * We have updated performance statistics for a given address. Note
1304 * that this function can be called for addresses that are currently
1305 * in use as well as addresses that are valid but not actively in use.
1306 * Furthermore, the peer may not even be connected to us right now (in
1307 * which case the call may be ignored or the information may be stored
1308 * for later use). Update bandwidth assignments.
1311 * @param address the address
1312 * @param session session handle, can be NULL
1313 * @param ats performance data for the address
1314 * @param ats_count number of performance records in @a ats
1315 * @return #GNUNET_YES on success, #GNUNET_NO if address or session are unknown,
1316 * #GNUNET_SYSERR on hard failure
1319 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1320 const struct GNUNET_HELLO_Address *address,
1321 struct Session *session,
1322 const struct GNUNET_ATS_Information *ats,
1325 struct PendingMessage *p;
1326 struct AddressUpdateMessage *m;
1327 struct GNUNET_ATS_Information *am;
1333 if (NULL == address)
1336 return GNUNET_SYSERR;
1341 return GNUNET_SYSERR;
1344 namelen = (address->transport_name ==
1345 NULL) ? 0 : strlen (address->transport_name) + 1;
1347 sizeof (struct AddressUpdateMessage) + address->address_length +
1348 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1349 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1350 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1351 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1353 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1356 return GNUNET_SYSERR;
1359 if (NULL != session)
1361 s = find_session_id (sh, session, &address->peer);
1366 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1368 p->is_init = GNUNET_NO;
1369 m = (struct AddressUpdateMessage *) &p[1];
1370 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1371 m->header.size = htons (msize);
1372 m->ats_count = htonl (ats_count);
1373 m->peer = address->peer;
1374 m->address_length = htons (address->address_length);
1375 m->address_local_info = htonl ((uint32_t) address->local_info);
1376 m->plugin_name_length = htons (namelen);
1378 m->session_id = htonl (s);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1382 GNUNET_i2s (&address->peer),
1383 address->transport_name, session, s);
1385 am = (struct GNUNET_ATS_Information *) &m[1];
1386 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1387 pm = (char *) &am[ats_count];
1388 memcpy (pm, address->address, address->address_length);
1389 memcpy (&pm[address->address_length], address->transport_name, namelen);
1390 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1397 * An address is now in use or not used any more.
1400 * @param address the address
1401 * @param session session handle, can be NULL
1402 * @param in_use #GNUNET_YES if this address is now used, #GNUNET_NO
1403 * if address is not used any more
1406 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1407 const struct GNUNET_HELLO_Address *address,
1408 struct Session *session, int in_use)
1410 struct PendingMessage *p;
1411 struct AddressUseMessage *m;
1417 GNUNET_assert (NULL != address);
1419 (address->transport_name ==
1420 NULL) ? 0 : strlen (address->transport_name) + 1;
1421 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1422 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1423 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1424 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1430 if (session != NULL)
1432 s = find_session_id (sh, session, &address->peer);
1433 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1435 /* trying to set unknown address to NO */
1436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1437 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1438 GNUNET_i2s (&address->peer), address->transport_name, session);
1442 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1444 /* trying to set new address to YES */
1445 s = find_empty_session_slot (sh, session, &address->peer);
1446 GNUNET_assert (NOT_FOUND != s);
1450 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1452 p->is_init = GNUNET_NO;
1453 m = (struct AddressUseMessage *) &p[1];
1454 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1455 m->header.size = htons (msize);
1456 m->peer = address->peer;
1457 m->in_use = htons (in_use);
1458 m->address_length = htons (address->address_length);
1459 m->address_local_info = htonl ((uint32_t) address->local_info);
1460 m->plugin_name_length = htons (namelen);
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1463 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1464 (GNUNET_YES == in_use) ? "YES" : "NO",
1465 GNUNET_i2s (&address->peer), address->transport_name, session);
1467 m->session_id = htonl (s);
1468 pm = (char *) &m[1];
1469 memcpy (pm, address->address, address->address_length);
1470 memcpy (&pm[address->address_length], address->transport_name, namelen);
1471 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1478 * An address got destroyed, stop including it as a valid address.
1480 * If a session is given, only the session will be removed, if no session is
1481 * given the full address will be deleted.
1484 * @param address the address
1485 * @param session session handle that is no longer valid, can be NULL
1488 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1489 const struct GNUNET_HELLO_Address *address,
1490 struct Session *session)
1492 struct PendingMessage *p;
1493 struct AddressDestroyedMessage *m;
1499 if (address == NULL)
1505 GNUNET_assert (address->transport_name != NULL);
1506 namelen = strlen (address->transport_name) + 1;
1507 GNUNET_assert (namelen > 1);
1509 sizeof (struct AddressDestroyedMessage) + address->address_length +
1511 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1512 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1513 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1519 s = find_session_id (sh, session, &address->peer);
1520 if ((NULL != session) && (NOT_FOUND == s))
1522 /* trying to delete unknown address */
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1525 GNUNET_i2s (&address->peer), address->transport_name, session);
1529 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1531 p->is_init = GNUNET_NO;
1532 m = (struct AddressDestroyedMessage *) &p[1];
1533 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1534 m->header.size = htons (msize);
1535 m->reserved = htonl (0);
1536 m->peer = address->peer;
1537 m->address_length = htons (address->address_length);
1538 m->address_local_info = htonl ((uint32_t) address->local_info);
1539 m->plugin_name_length = htons (namelen);
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "Deleting address for peer `%s', plugin `%s', session %p\n",
1543 GNUNET_i2s (&address->peer), address->transport_name, session);
1545 m->session_id = htonl (s);
1546 pm = (char *) &m[1];
1547 memcpy (pm, address->address, address->address_length);
1548 memcpy (&pm[address->address_length], address->transport_name, namelen);
1549 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1551 remove_session (sh, s, &address->peer);
1554 /* end of ats_api_scheduling.c */