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;
93 struct sockaddr *netmask;
100 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
102 struct GNUNET_ATS_SchedulingHandle
108 const struct GNUNET_CONFIGURATION_Handle *cfg;
111 * Callback to invoke on suggestions.
113 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
116 * Closure for 'suggest_cb'.
118 void *suggest_cb_cls;
121 * Connection to ATS service.
123 struct GNUNET_CLIENT_Connection *client;
126 * Head of list of messages for the ATS service.
128 struct PendingMessage *pending_head;
131 * Tail of list of messages for the ATS service
133 struct PendingMessage *pending_tail;
136 * Current request for transmission to ATS.
138 struct GNUNET_CLIENT_TransmitHandle *th;
141 * Head of network list
143 struct ATS_Network * net_head;
146 * Tail of network list
148 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
166 GNUNET_SCHEDULER_TaskIdentifier interface_task;
170 * Size of the session array.
172 unsigned int session_array_size;
175 * Should we reconnect to ATS due to some serious error?
182 * Re-establish the connection to the ATS service.
184 * @param sh handle to use to re-connect.
187 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
191 * Re-establish the connection to the ATS service.
193 * @param cls handle to use to re-connect.
194 * @param tc scheduler context
197 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 struct GNUNET_ATS_SchedulingHandle *sh = cls;
201 sh->task = GNUNET_SCHEDULER_NO_TASK;
207 * Disconnect from ATS and then reconnect.
209 * @param sh our handle
212 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
214 sh->reconnect = GNUNET_NO;
215 GNUNET_CLIENT_disconnect (sh->client);
218 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
224 * Transmit messages from the message queue to the service
225 * (if there are any, and if we are not already trying).
227 * @param sh handle to use
230 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
234 * Type of a function to call when we receive a message
237 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
238 * @param msg message received, NULL on timeout or fatal error
241 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
245 * We can now transmit a message to ATS. Do it.
247 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
248 * @param size number of bytes we can transmit to ATS
249 * @param buf where to copy the messages
250 * @return number of bytes copied into buf
253 transmit_message_to_ats (void *cls, size_t size, void *buf)
255 struct GNUNET_ATS_SchedulingHandle *sh = cls;
256 struct PendingMessage *p;
261 if ((size == 0) || (buf == NULL))
263 force_reconnect (sh);
268 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
270 memcpy (&cbuf[ret], &p[1], p->size);
273 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
274 if (GNUNET_YES == p->is_init)
275 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
276 GNUNET_TIME_UNIT_FOREVER_REL);
285 * Transmit messages from the message queue to the service
286 * (if there are any, and if we are not already trying).
288 * @param sh handle to use
291 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
293 struct PendingMessage *p;
297 if (NULL == (p = sh->pending_head))
299 if (NULL == sh->client)
300 return; /* currently reconnecting */
302 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
303 GNUNET_TIME_UNIT_FOREVER_REL,
304 GNUNET_NO, &transmit_message_to_ats,
310 * Find the session object corresponding to the given session ID.
312 * @param sh our handle
313 * @param session_id current session ID
314 * @param peer peer the session belongs to
315 * @return the session object (or NULL)
317 static struct Session *
318 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
319 const struct GNUNET_PeerIdentity *peer)
322 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
323 "Find session %u from peer %s in %p\n",
324 (unsigned int) session_id, GNUNET_i2s (peer), sh);
326 if (session_id >= sh->session_array_size)
333 if (sh->session_array[session_id].session == NULL)
336 memcmp (peer, &sh->session_array[session_id].peer,
337 sizeof (struct GNUNET_PeerIdentity)));
342 memcmp (peer, &sh->session_array[session_id].peer,
343 sizeof (struct GNUNET_PeerIdentity)))
346 sh->reconnect = GNUNET_YES;
349 /* This check exploits the fact that first field of a session object
353 memcmp (peer, sh->session_array[session_id].session,
354 sizeof (struct GNUNET_PeerIdentity)))
356 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
357 "Session %p belongs to peer `%s'\n",
358 sh->session_array[session_id].session, GNUNET_i2s_full ((struct GNUNET_PeerIdentity *) &sh->session_array[session_id].peer));
361 sh->reconnect = GNUNET_YES;
365 return sh->session_array[session_id].session;
370 * Get an available session ID for the given session object.
372 * @param sh our handle
373 * @param session session object
374 * @param peer peer the session belongs to
375 * @return the session id
378 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
379 const struct GNUNET_PeerIdentity *peer)
384 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
385 "Get session ID for session %p from peer %s in %p\n", session,
386 GNUNET_i2s (peer), sh);
391 for (i = 1; i < sh->session_array_size; i++)
393 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
398 f = sh->session_array_size;
399 GNUNET_array_grow (sh->session_array, sh->session_array_size,
400 sh->session_array_size * 2);
402 GNUNET_assert (f > 0);
403 sh->session_array[f].session = session;
404 sh->session_array[f].peer = *peer;
405 sh->session_array[f].slot_used = GNUNET_YES;
407 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
408 "Assigning session ID %u for session %p of peer %s in %p\n", f,
409 session, GNUNET_i2s (peer), sh);
416 * Get the ID for the given session object.
418 * @param sh our handle
419 * @param session session object
420 * @param peer peer the session belongs to
421 * @return the session id or NOT_FOUND for error
424 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
425 const struct GNUNET_PeerIdentity *peer)
430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
431 "Get session ID for session %p from peer %s in %p\n", session,
432 GNUNET_i2s (peer), sh);
436 for (i = 1; i < sh->session_array_size; i++)
438 if (session == sh->session_array[i].session)
440 if (0 != memcmp (peer, &sh->session_array[i].peer,
441 sizeof (struct GNUNET_PeerIdentity)))
443 p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
444 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
445 "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
446 session, GNUNET_i2s (peer), p2);
458 * Remove the session of the given session ID from the session
459 * table (it is no longer valid).
461 * @param sh our handle
462 * @param session_id identifies session that is no longer valid
463 * @param peer peer the session belongs to
466 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
467 const struct GNUNET_PeerIdentity *peer)
469 GNUNET_assert (peer != NULL);
470 GNUNET_assert (sh != NULL);
472 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
473 "Release sessionID %u from peer %s in %p\n",
474 (unsigned int) session_id, GNUNET_i2s (peer), sh);
479 GNUNET_assert (session_id < sh->session_array_size);
480 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
481 GNUNET_assert (0 == memcmp (peer,
482 &sh->session_array[session_id].peer,
483 sizeof (struct GNUNET_PeerIdentity)));
484 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
485 "Session %p for peer `%s' removed from slot %u \n",
486 sh->session_array[session_id].session,
489 sh->session_array[session_id].session = NULL;
495 * Release the session slot from the session table (ATS service is
496 * also done using it).
498 * @param sh our handle
499 * @param session_id identifies session that is no longer valid
500 * @param peer peer the session belongs to
503 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
504 const struct GNUNET_PeerIdentity *peer)
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
508 "Release sessionID %u from peer %s in %p\n",
509 (unsigned int) session_id, GNUNET_i2s (peer), sh);
511 if (session_id >= sh->session_array_size)
514 sh->reconnect = GNUNET_YES;
518 /* this slot should have been removed from remove_session before */
519 GNUNET_assert (sh->session_array[session_id].session == NULL);
522 memcmp (peer, &sh->session_array[session_id].peer,
523 sizeof (struct GNUNET_PeerIdentity)))
526 sh->reconnect = GNUNET_YES;
529 sh->session_array[session_id].slot_used = GNUNET_NO;
530 memset (&sh->session_array[session_id].peer, 0,
531 sizeof (struct GNUNET_PeerIdentity));
536 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
537 const struct SessionReleaseMessage *srm)
539 release_session (sh, ntohl (srm->session_id), &srm->peer);
544 * Type of a function to call when we receive a message
547 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
548 * @param msg message received, NULL on timeout or fatal error
551 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
553 struct GNUNET_ATS_SchedulingHandle *sh = cls;
554 const struct AddressSuggestionMessage *m;
555 const struct GNUNET_ATS_Information *atsi;
556 const char *plugin_address;
557 const char *plugin_name;
558 uint16_t plugin_address_length;
559 uint16_t plugin_name_length;
561 struct GNUNET_HELLO_Address address;
566 force_reconnect (sh);
569 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
570 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
572 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
573 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
574 GNUNET_TIME_UNIT_FOREVER_REL);
575 if (GNUNET_YES == sh->reconnect)
576 force_reconnect (sh);
579 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
580 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
583 force_reconnect (sh);
586 m = (const struct AddressSuggestionMessage *) msg;
587 ats_count = ntohl (m->ats_count);
588 plugin_address_length = ntohs (m->address_length);
589 atsi = (const struct GNUNET_ATS_Information *) &m[1];
590 plugin_address = (const char *) &atsi[ats_count];
591 plugin_name = &plugin_address[plugin_address_length];
592 plugin_name_length = ntohs (m->plugin_name_length);
593 if ((plugin_address_length + plugin_name_length +
594 ats_count * sizeof (struct GNUNET_ATS_Information) +
595 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
597 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
598 || (plugin_name[plugin_name_length - 1] != '\0'))
601 force_reconnect (sh);
604 uint32_t session_id = ntohl (m->session_id);
610 s = find_session (sh, session_id, &m->peer);
614 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
615 "ATS tries to use outdated session `%s'\n",
616 GNUNET_i2s (&m->peer));
617 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
618 GNUNET_TIME_UNIT_FOREVER_REL);
622 address.peer = m->peer;
623 address.address = plugin_address;
624 address.address_length = plugin_address_length;
625 address.transport_name = plugin_name;
627 if ((s == NULL) && (0 == address.address_length))
629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
630 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
631 GNUNET_i2s (&address.peer), address.transport_name,
632 plugin_address_length, session_id);
634 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
635 GNUNET_TIME_UNIT_FOREVER_REL);
639 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
640 m->bandwidth_in, atsi, ats_count);
642 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
643 GNUNET_TIME_UNIT_FOREVER_REL);
644 if (GNUNET_YES == sh->reconnect)
645 force_reconnect (sh);
650 * Re-establish the connection to the ATS service.
652 * @param sh handle to use to re-connect.
655 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
657 struct PendingMessage *p;
658 struct ClientStartMessage *init;
660 GNUNET_assert (NULL == sh->client);
661 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
662 GNUNET_assert (NULL != sh->client);
663 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
665 p = GNUNET_malloc (sizeof (struct PendingMessage) +
666 sizeof (struct ClientStartMessage));
667 p->size = sizeof (struct ClientStartMessage);
668 p->is_init = GNUNET_YES;
669 init = (struct ClientStartMessage *) &p[1];
670 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
671 init->header.size = htons (sizeof (struct ClientStartMessage));
672 init->start_flag = htonl (START_FLAG_SCHEDULING);
673 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
680 * delete the current network list
683 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
685 struct ATS_Network * cur = sh->net_head;
688 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
696 interface_proc (void *cls, const char *name,
698 const struct sockaddr *
700 const struct sockaddr *
702 const struct sockaddr *
703 netmask, socklen_t addrlen)
705 struct GNUNET_ATS_SchedulingHandle * sh = cls;
706 /* Calculate network */
707 struct ATS_Network *net = NULL;
709 /* Skipping IPv4 loopback addresses since we have special check */
710 if (addr->sa_family == AF_INET)
712 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
714 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
717 /* Skipping IPv6 loopback addresses since we have special check */
718 if (addr->sa_family == AF_INET6)
720 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
721 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
725 if (addr->sa_family == AF_INET)
727 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
728 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
729 struct sockaddr_in *tmp = NULL;
730 struct sockaddr_in network4;
732 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
733 tmp = (struct sockaddr_in *) &net[1];
734 net->network = (struct sockaddr *) &tmp[0];
735 net->netmask = (struct sockaddr *) &tmp[1];
736 net->length = addrlen;
738 memset (&network4, 0, sizeof (network4));
739 network4.sin_family = AF_INET;
740 #if HAVE_SOCKADDR_IN_SIN_LEN
741 network4.sin_len = sizeof (network4);
743 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
745 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
746 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
749 if (addr->sa_family == AF_INET6)
751 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
752 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
753 struct sockaddr_in6 * tmp = NULL;
754 struct sockaddr_in6 network6;
756 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
757 tmp = (struct sockaddr_in6 *) &net[1];
758 net->network = (struct sockaddr *) &tmp[0];
759 net->netmask = (struct sockaddr *) &tmp[1];
760 net->length = addrlen;
762 memset (&network6, 0, sizeof (network6));
763 network6.sin6_family = AF_INET6;
764 #if HAVE_SOCKADDR_IN_SIN_LEN
765 network6.sin6_len = sizeof (network6);
768 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
769 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
770 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
771 for (c = 0; c < 4; c++)
772 net_elem[c] = addr_elem[c] & mask_elem[c];
774 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
775 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
782 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
784 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
786 GNUNET_free (netmask);
788 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
795 * Periodically get list of addresses
797 * @param tc Task context
800 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
802 struct GNUNET_ATS_SchedulingHandle * sh = cls;
803 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
804 delete_networks (sh);
805 GNUNET_OS_network_interfaces_list(interface_proc, sh);
806 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
812 * Convert a GNUNET_ATS_NetworkType to a string
814 * @param net the network type
815 * @return a string or NULL if invalid
818 GNUNET_ATS_print_network_type (uint32_t net)
820 char *networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
821 if (net < GNUNET_ATS_NetworkTypeCount)
822 return networks[net];
828 * Returns where the address is located: LAN or WAN or ...
830 * @param sh the scheduling handle
831 * @param addr address
832 * @param addrlen address length
833 * @return location as GNUNET_ATS_Information
835 struct GNUNET_ATS_Information
836 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
838 GNUNET_assert (sh != NULL);
839 struct ATS_Network * cur = sh->net_head;
841 int type = GNUNET_ATS_NET_UNSPECIFIED;
842 struct GNUNET_ATS_Information ats;
844 if (addr->sa_family == AF_UNIX)
846 type = GNUNET_ATS_NET_LOOPBACK;
849 /* IPv4 loopback check */
850 if (addr->sa_family == AF_INET)
852 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
854 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
855 type = GNUNET_ATS_NET_LOOPBACK;
857 /* IPv6 loopback check */
858 if (addr->sa_family == AF_INET6)
860 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
861 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
862 type = GNUNET_ATS_NET_LOOPBACK;
865 /* Check local networks */
866 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
868 if (addrlen != cur->length)
874 if (addr->sa_family == AF_INET)
876 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
877 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
878 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
880 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
881 type = GNUNET_ATS_NET_LAN;
883 if (addr->sa_family == AF_INET6)
885 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
886 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
887 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
889 int res = GNUNET_YES;
891 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
892 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
893 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
894 for (c = 0; c < 4; c++)
895 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
898 if (res == GNUNET_YES)
899 type = GNUNET_ATS_NET_LAN;
904 /* no local network found for this address, default: WAN */
905 if (type == GNUNET_ATS_NET_UNSPECIFIED)
906 type = GNUNET_ATS_NET_WAN;
907 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
908 ats.value = htonl (type);
910 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
911 "`%s' is in network `%s'\n",
912 GNUNET_a2s ((const struct sockaddr *) addr, addrlen),
913 GNUNET_ATS_print_network_type(type));
919 * Initialize the ATS subsystem.
921 * @param cfg configuration to use
922 * @param suggest_cb notification to call whenever the suggestation changed
923 * @param suggest_cb_cls closure for 'suggest_cb'
924 * @return ats context
926 struct GNUNET_ATS_SchedulingHandle *
927 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
928 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
929 void *suggest_cb_cls)
931 struct GNUNET_ATS_SchedulingHandle *sh;
933 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
935 sh->suggest_cb = suggest_cb;
936 sh->suggest_cb_cls = suggest_cb_cls;
937 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
938 GNUNET_OS_network_interfaces_list(interface_proc, sh);
939 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
948 * Client is done with ATS scheduling, release resources.
950 * @param sh handle to release
953 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
955 struct PendingMessage *p;
957 while (NULL != (p = sh->pending_head))
959 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
962 if (NULL != sh->client)
964 GNUNET_CLIENT_disconnect (sh->client);
967 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
969 GNUNET_SCHEDULER_cancel (sh->task);
970 sh->task = GNUNET_SCHEDULER_NO_TASK;
973 delete_networks (sh);
974 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
976 GNUNET_SCHEDULER_cancel(sh->interface_task);
977 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
979 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
985 * We would like to reset the address suggestion block time for this
989 * @param peer identity of the peer we want to reset
992 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
993 const struct GNUNET_PeerIdentity *peer)
995 struct PendingMessage *p;
996 struct ResetBackoffMessage *m;
998 p = GNUNET_malloc (sizeof (struct PendingMessage) +
999 sizeof (struct ResetBackoffMessage));
1000 p->size = sizeof (struct ResetBackoffMessage);
1001 p->is_init = GNUNET_NO;
1002 m = (struct ResetBackoffMessage *) &p[1];
1003 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
1004 m->header.size = htons (sizeof (struct ResetBackoffMessage));
1005 m->reserved = htonl (0);
1007 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1012 * We would like to establish a new connection with a peer. ATS
1013 * should suggest a good address to begin with.
1016 * @param peer identity of the peer we need an address for
1019 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1020 const struct GNUNET_PeerIdentity *peer)
1022 struct PendingMessage *p;
1023 struct RequestAddressMessage *m;
1025 // FIXME: ATS needs to remember this in case of
1027 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1028 sizeof (struct RequestAddressMessage));
1029 p->size = sizeof (struct RequestAddressMessage);
1030 p->is_init = GNUNET_NO;
1031 m = (struct RequestAddressMessage *) &p[1];
1032 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1033 m->header.size = htons (sizeof (struct RequestAddressMessage));
1034 m->reserved = htonl (0);
1036 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1042 * We would like to stop receiving address updates for this peer
1045 * @param peer identity of the peer
1048 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1049 const struct GNUNET_PeerIdentity *peer)
1051 struct PendingMessage *p;
1052 struct RequestAddressMessage *m;
1054 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1055 sizeof (struct RequestAddressMessage));
1056 p->size = sizeof (struct RequestAddressMessage);
1057 p->is_init = GNUNET_NO;
1058 m = (struct RequestAddressMessage *) &p[1];
1059 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1060 m->header.size = htons (sizeof (struct RequestAddressMessage));
1061 m->reserved = htonl (0);
1063 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1069 * We have a new address ATS should know. Addresses have to be added with this
1070 * function before they can be: updated, set in use and destroyed
1073 * @param address the address
1074 * @param session session handle, can be NULL
1075 * @param ats performance data for the address
1076 * @param ats_count number of performance records in 'ats'
1077 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1080 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1081 const struct GNUNET_HELLO_Address *address,
1082 struct Session *session,
1083 const struct GNUNET_ATS_Information *ats,
1087 struct PendingMessage *p;
1088 struct AddressUpdateMessage *m;
1089 struct GNUNET_ATS_Information *am;
1095 if (address == NULL)
1098 return GNUNET_SYSERR;
1102 (address->transport_name ==
1103 NULL) ? 0 : strlen (address->transport_name) + 1;
1105 sizeof (struct AddressUpdateMessage) + address->address_length +
1106 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1107 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1108 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1109 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1111 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1114 return GNUNET_SYSERR;
1117 if (NULL != session)
1119 s = find_session_id (sh, session, &address->peer);
1122 /* Already existing, nothing todo */
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1125 GNUNET_i2s (&address->peer),
1126 address->transport_name, session, s);
1127 return GNUNET_SYSERR;
1129 s = find_empty_session_slot (sh, session, &address->peer);
1130 GNUNET_break (NOT_FOUND != s);
1133 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1135 p->is_init = GNUNET_NO;
1136 m = (struct AddressUpdateMessage *) &p[1];
1137 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1138 m->header.size = htons (msize);
1139 m->ats_count = htonl (ats_count);
1140 m->peer = address->peer;
1141 m->address_length = htons (address->address_length);
1142 m->plugin_name_length = htons (namelen);
1143 m->session_id = htonl (s);
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1147 GNUNET_i2s (&address->peer),
1148 address->transport_name, session, s);
1150 am = (struct GNUNET_ATS_Information *) &m[1];
1151 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1152 pm = (char *) &am[ats_count];
1153 memcpy (pm, address->address, address->address_length);
1154 if (NULL != address->transport_name)
1155 memcpy (&pm[address->address_length], address->transport_name, namelen);
1156 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1164 * We have updated performance statistics for a given address. Note
1165 * that this function can be called for addresses that are currently
1166 * in use as well as addresses that are valid but not actively in use.
1167 * Furthermore, the peer may not even be connected to us right now (in
1168 * which case the call may be ignored or the information may be stored
1169 * for later use). Update bandwidth assignments.
1172 * @param address the address
1173 * @param session session handle, can be NULL
1174 * @param ats performance data for the address
1175 * @param ats_count number of performance records in 'ats'
1178 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1179 const struct GNUNET_HELLO_Address *address,
1180 struct Session *session,
1181 const struct GNUNET_ATS_Information *ats,
1184 struct PendingMessage *p;
1185 struct AddressUpdateMessage *m;
1186 struct GNUNET_ATS_Information *am;
1192 if (address == NULL)
1199 (address->transport_name ==
1200 NULL) ? 0 : strlen (address->transport_name) + 1;
1202 sizeof (struct AddressUpdateMessage) + address->address_length +
1203 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1204 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1205 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1206 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1208 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1214 if (NULL != session)
1216 s = find_session_id (sh, session, &address->peer);
1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220 "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1221 GNUNET_i2s (&address->peer),
1222 address->transport_name, session, s);
1229 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1231 p->is_init = GNUNET_NO;
1232 m = (struct AddressUpdateMessage *) &p[1];
1233 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1234 m->header.size = htons (msize);
1235 m->ats_count = htonl (ats_count);
1236 m->peer = address->peer;
1237 m->address_length = htons (address->address_length);
1238 m->plugin_name_length = htons (namelen);
1240 m->session_id = htonl (s);
1242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1243 "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1244 GNUNET_i2s (&address->peer),
1245 address->transport_name, session, s);
1247 am = (struct GNUNET_ATS_Information *) &m[1];
1248 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1249 pm = (char *) &am[ats_count];
1250 memcpy (pm, address->address, address->address_length);
1251 memcpy (&pm[address->address_length], address->transport_name, namelen);
1252 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1259 * An address is now in use or not used any more.
1262 * @param address the address
1263 * @param session session handle, can be NULL
1264 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1265 * if address is not used any more
1268 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1269 const struct GNUNET_HELLO_Address *address,
1270 struct Session *session, int in_use)
1272 struct PendingMessage *p;
1273 struct AddressUseMessage *m;
1279 GNUNET_assert (NULL != address);
1281 (address->transport_name ==
1282 NULL) ? 0 : strlen (address->transport_name) + 1;
1283 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1284 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1285 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1286 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1292 if (session != NULL)
1294 s = find_session_id (sh, session, &address->peer);
1295 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1297 /* trying to set unknown address to NO */
1298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1300 GNUNET_i2s (&address->peer), address->transport_name, session);
1304 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1306 /* trying to set new address to YES */
1307 s = find_empty_session_slot (sh, session, &address->peer);
1308 GNUNET_assert (NOT_FOUND != s);
1312 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1314 p->is_init = GNUNET_NO;
1315 m = (struct AddressUseMessage *) &p[1];
1316 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1317 m->header.size = htons (msize);
1318 m->peer = address->peer;
1319 m->in_use = htons (in_use);
1320 m->address_length = htons (address->address_length);
1321 m->plugin_name_length = htons (namelen);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1325 (GNUNET_YES == in_use) ? "YES" : "NO",
1326 GNUNET_i2s (&address->peer), address->transport_name, session);
1328 m->session_id = htonl (s);
1329 pm = (char *) &m[1];
1330 memcpy (pm, address->address, address->address_length);
1331 memcpy (&pm[address->address_length], address->transport_name, namelen);
1332 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1339 * A session got destroyed, stop including it as a valid address.
1342 * @param address the address
1343 * @param session session handle that is no longer valid, can be NULL
1346 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1347 const struct GNUNET_HELLO_Address *address,
1348 struct Session *session)
1350 struct PendingMessage *p;
1351 struct AddressDestroyedMessage *m;
1357 if (address == NULL)
1363 GNUNET_assert (address->transport_name != NULL);
1364 namelen = strlen (address->transport_name) + 1;
1365 GNUNET_assert (namelen > 1);
1367 sizeof (struct AddressDestroyedMessage) + address->address_length +
1369 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1370 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1371 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1377 s = find_session_id (sh, session, &address->peer);
1378 if ((NULL != session) && (NOT_FOUND == s))
1380 /* trying to delete unknown address */
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1383 GNUNET_i2s (&address->peer), address->transport_name, session);
1387 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1389 p->is_init = GNUNET_NO;
1390 m = (struct AddressDestroyedMessage *) &p[1];
1391 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1392 m->header.size = htons (msize);
1393 m->reserved = htonl (0);
1394 m->peer = address->peer;
1395 m->address_length = htons (address->address_length);
1396 m->plugin_name_length = htons (namelen);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 "Deleting address for peer `%s', plugin `%s', session %p\n",
1400 GNUNET_i2s (&address->peer), address->transport_name, session);
1402 m->session_id = htonl (s);
1403 pm = (char *) &m[1];
1404 memcpy (pm, address->address, address->address_length);
1405 memcpy (&pm[address->address_length], address->transport_name, namelen);
1406 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1408 remove_session (sh, s, &address->peer);
1411 /* end of ats_api_scheduling.c */