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 return sh->session_array[session_id].session;
354 * Get an available session ID for the given session object.
356 * @param sh our handle
357 * @param session session object
358 * @param peer peer the session belongs to
359 * @return the session id
362 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
363 const struct GNUNET_PeerIdentity *peer)
368 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
369 "Get session ID for session %p from peer %s in %p\n", session,
370 GNUNET_i2s (peer), sh);
375 for (i = 1; i < sh->session_array_size; i++)
377 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
382 f = sh->session_array_size;
383 GNUNET_array_grow (sh->session_array, sh->session_array_size,
384 sh->session_array_size * 2);
386 GNUNET_assert (f > 0);
387 sh->session_array[f].session = session;
388 sh->session_array[f].peer = *peer;
389 sh->session_array[f].slot_used = GNUNET_YES;
391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
392 "Assigning session ID %u for session %p of peer %s in %p\n", f,
393 session, GNUNET_i2s (peer), sh);
400 * Get the ID for the given session object.
402 * @param sh our handle
403 * @param session session object
404 * @param peer peer the session belongs to
405 * @return the session id or NOT_FOUND for error
408 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
409 const struct GNUNET_PeerIdentity *peer)
414 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
415 "Get session ID for session %p from peer %s in %p\n", session,
416 GNUNET_i2s (peer), sh);
420 for (i = 1; i < sh->session_array_size; i++)
422 if (session == sh->session_array[i].session)
424 if (0 != memcmp (peer, &sh->session_array[i].peer,
425 sizeof (struct GNUNET_PeerIdentity)))
427 p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
428 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
429 "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
430 session, GNUNET_i2s (peer), p2);
442 * Remove the session of the given session ID from the session
443 * table (it is no longer valid).
445 * @param sh our handle
446 * @param session_id identifies session that is no longer valid
447 * @param peer peer the session belongs to
450 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
451 const struct GNUNET_PeerIdentity *peer)
453 GNUNET_assert (peer != NULL);
454 GNUNET_assert (sh != NULL);
456 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
457 "Release sessionID %u from peer %s in %p\n",
458 (unsigned int) session_id, GNUNET_i2s (peer), sh);
463 GNUNET_assert (session_id < sh->session_array_size);
464 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
465 GNUNET_assert (0 == memcmp (peer,
466 &sh->session_array[session_id].peer,
467 sizeof (struct GNUNET_PeerIdentity)));
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
469 "Session %p for peer `%s' removed from slot %u \n",
470 sh->session_array[session_id].session,
473 sh->session_array[session_id].session = NULL;
479 * Release the session slot from the session table (ATS service is
480 * also done using it).
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 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
488 const struct GNUNET_PeerIdentity *peer)
491 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
492 "Release sessionID %u from peer %s in %p\n",
493 (unsigned int) session_id, GNUNET_i2s (peer), sh);
495 if (session_id >= sh->session_array_size)
498 sh->reconnect = GNUNET_YES;
502 /* this slot should have been removed from remove_session before */
503 GNUNET_assert (sh->session_array[session_id].session == NULL);
506 memcmp (peer, &sh->session_array[session_id].peer,
507 sizeof (struct GNUNET_PeerIdentity)))
510 sh->reconnect = GNUNET_YES;
513 sh->session_array[session_id].slot_used = GNUNET_NO;
514 memset (&sh->session_array[session_id].peer, 0,
515 sizeof (struct GNUNET_PeerIdentity));
520 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
521 const struct SessionReleaseMessage *srm)
523 release_session (sh, ntohl (srm->session_id), &srm->peer);
528 * Type of a function to call when we receive a message
531 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
532 * @param msg message received, NULL on timeout or fatal error
535 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
537 struct GNUNET_ATS_SchedulingHandle *sh = cls;
538 const struct AddressSuggestionMessage *m;
539 const struct GNUNET_ATS_Information *atsi;
540 const char *plugin_address;
541 const char *plugin_name;
542 uint16_t plugin_address_length;
543 uint16_t plugin_name_length;
545 struct GNUNET_HELLO_Address address;
550 force_reconnect (sh);
553 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
554 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
556 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
557 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
558 GNUNET_TIME_UNIT_FOREVER_REL);
559 if (GNUNET_YES == sh->reconnect)
560 force_reconnect (sh);
563 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
564 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
567 force_reconnect (sh);
570 m = (const struct AddressSuggestionMessage *) msg;
571 ats_count = ntohl (m->ats_count);
572 plugin_address_length = ntohs (m->address_length);
573 atsi = (const struct GNUNET_ATS_Information *) &m[1];
574 plugin_address = (const char *) &atsi[ats_count];
575 plugin_name = &plugin_address[plugin_address_length];
576 plugin_name_length = ntohs (m->plugin_name_length);
577 if ((plugin_address_length + plugin_name_length +
578 ats_count * sizeof (struct GNUNET_ATS_Information) +
579 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
581 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
582 || (plugin_name[plugin_name_length - 1] != '\0'))
585 force_reconnect (sh);
588 uint32_t session_id = ntohl (m->session_id);
594 s = find_session (sh, session_id, &m->peer);
598 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
599 "ATS tries to use outdated session `%s'\n",
600 GNUNET_i2s (&m->peer));
601 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
602 GNUNET_TIME_UNIT_FOREVER_REL);
606 address.peer = m->peer;
607 address.address = plugin_address;
608 address.address_length = plugin_address_length;
609 address.transport_name = plugin_name;
611 if ((s == NULL) && (0 == address.address_length))
613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
614 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
615 GNUNET_i2s (&address.peer), address.transport_name,
616 plugin_address_length, session_id);
618 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
619 GNUNET_TIME_UNIT_FOREVER_REL);
623 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
624 m->bandwidth_in, atsi, ats_count);
626 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
627 GNUNET_TIME_UNIT_FOREVER_REL);
628 if (GNUNET_YES == sh->reconnect)
629 force_reconnect (sh);
634 * Re-establish the connection to the ATS service.
636 * @param sh handle to use to re-connect.
639 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
641 struct PendingMessage *p;
642 struct ClientStartMessage *init;
644 GNUNET_assert (NULL == sh->client);
645 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
646 GNUNET_assert (NULL != sh->client);
647 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
649 p = GNUNET_malloc (sizeof (struct PendingMessage) +
650 sizeof (struct ClientStartMessage));
651 p->size = sizeof (struct ClientStartMessage);
652 p->is_init = GNUNET_YES;
653 init = (struct ClientStartMessage *) &p[1];
654 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
655 init->header.size = htons (sizeof (struct ClientStartMessage));
656 init->start_flag = htonl (START_FLAG_SCHEDULING);
657 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
664 * delete the current network list
667 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
669 struct ATS_Network * cur = sh->net_head;
672 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
680 interface_proc (void *cls, const char *name,
682 const struct sockaddr *
684 const struct sockaddr *
686 const struct sockaddr *
687 netmask, socklen_t addrlen)
689 struct GNUNET_ATS_SchedulingHandle * sh = cls;
690 /* Calculate network */
691 struct ATS_Network *net = NULL;
693 /* Skipping IPv4 loopback addresses since we have special check */
694 if (addr->sa_family == AF_INET)
696 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
698 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
701 /* Skipping IPv6 loopback addresses since we have special check */
702 if (addr->sa_family == AF_INET6)
704 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
705 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
709 if (addr->sa_family == AF_INET)
711 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
712 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
713 struct sockaddr_in *tmp = NULL;
714 struct sockaddr_in network4;
716 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
717 tmp = (struct sockaddr_in *) &net[1];
718 net->network = (struct sockaddr *) &tmp[0];
719 net->netmask = (struct sockaddr *) &tmp[1];
720 net->length = addrlen;
722 memset (&network4, 0, sizeof (network4));
723 network4.sin_family = AF_INET;
724 #if HAVE_SOCKADDR_IN_SIN_LEN
725 network4.sin_len = sizeof (network4);
727 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
729 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
730 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
733 if (addr->sa_family == AF_INET6)
735 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
736 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
737 struct sockaddr_in6 * tmp = NULL;
738 struct sockaddr_in6 network6;
740 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
741 tmp = (struct sockaddr_in6 *) &net[1];
742 net->network = (struct sockaddr *) &tmp[0];
743 net->netmask = (struct sockaddr *) &tmp[1];
744 net->length = addrlen;
746 memset (&network6, 0, sizeof (network6));
747 network6.sin6_family = AF_INET6;
748 #if HAVE_SOCKADDR_IN_SIN_LEN
749 network6.sin6_len = sizeof (network6);
752 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
753 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
754 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
755 for (c = 0; c < 4; c++)
756 net_elem[c] = addr_elem[c] & mask_elem[c];
758 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
759 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
766 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
768 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
770 GNUNET_free (netmask);
772 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
779 * Periodically get list of addresses
781 * @param tc Task context
784 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
786 struct GNUNET_ATS_SchedulingHandle * sh = cls;
787 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
788 delete_networks (sh);
789 GNUNET_OS_network_interfaces_list(interface_proc, sh);
790 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
797 * Returns where the address is located: LAN or WAN or ...
799 * @param sh the scheduling handle
800 * @param addr address
801 * @param addrlen address length
802 * @return location as GNUNET_ATS_Information
804 struct GNUNET_ATS_Information
805 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
807 GNUNET_assert (sh != NULL);
808 struct GNUNET_ATS_Information ats;
809 struct ATS_Network * cur = sh->net_head;
810 int type = GNUNET_ATS_NET_UNSPECIFIED;
812 if (addr->sa_family == AF_UNIX)
814 type = GNUNET_ATS_NET_LOOPBACK;
817 /* IPv4 loopback check */
818 if (addr->sa_family == AF_INET)
820 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
822 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
823 type = GNUNET_ATS_NET_LOOPBACK;
825 /* IPv6 loopback check */
826 if (addr->sa_family == AF_INET6)
828 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
829 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
830 type = GNUNET_ATS_NET_LOOPBACK;
833 /* Check local networks */
834 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
836 if (addrlen != cur->length)
842 if (addr->sa_family == AF_INET)
844 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
845 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
846 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
848 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
850 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
852 "`%s' is in network `%s'\n",
853 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
856 type = GNUNET_ATS_NET_LAN;
859 if (addr->sa_family == AF_INET6)
861 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
862 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
863 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
865 int res = GNUNET_YES;
867 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
868 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
869 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
870 for (c = 0; c < 4; c++)
871 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
874 if (res == GNUNET_YES)
876 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
878 GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
881 type = GNUNET_ATS_NET_LAN;
887 /* no local network found for this address, default: WAN */
888 if (type == GNUNET_ATS_NET_UNSPECIFIED)
889 type = GNUNET_ATS_NET_WAN;
890 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
891 ats.value = htonl (type);
892 return (const struct GNUNET_ATS_Information) ats;
897 * Initialize the ATS subsystem.
899 * @param cfg configuration to use
900 * @param suggest_cb notification to call whenever the suggestation changed
901 * @param suggest_cb_cls closure for 'suggest_cb'
902 * @return ats context
904 struct GNUNET_ATS_SchedulingHandle *
905 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
906 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
907 void *suggest_cb_cls)
909 struct GNUNET_ATS_SchedulingHandle *sh;
911 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
913 sh->suggest_cb = suggest_cb;
914 sh->suggest_cb_cls = suggest_cb_cls;
915 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
916 GNUNET_OS_network_interfaces_list(interface_proc, sh);
917 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
926 * Client is done with ATS scheduling, release resources.
928 * @param sh handle to release
931 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
933 struct PendingMessage *p;
935 while (NULL != (p = sh->pending_head))
937 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
940 if (NULL != sh->client)
942 GNUNET_CLIENT_disconnect (sh->client);
945 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
947 GNUNET_SCHEDULER_cancel (sh->task);
948 sh->task = GNUNET_SCHEDULER_NO_TASK;
951 delete_networks (sh);
952 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
954 GNUNET_SCHEDULER_cancel(sh->interface_task);
955 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
957 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
963 * We would like to reset the address suggestion block time for this
967 * @param peer identity of the peer we want to reset
970 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
971 const struct GNUNET_PeerIdentity *peer)
973 struct PendingMessage *p;
974 struct ResetBackoffMessage *m;
976 p = GNUNET_malloc (sizeof (struct PendingMessage) +
977 sizeof (struct ResetBackoffMessage));
978 p->size = sizeof (struct ResetBackoffMessage);
979 p->is_init = GNUNET_NO;
980 m = (struct ResetBackoffMessage *) &p[1];
981 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
982 m->header.size = htons (sizeof (struct ResetBackoffMessage));
983 m->reserved = htonl (0);
985 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
990 * We would like to establish a new connection with a peer. ATS
991 * should suggest a good address to begin with.
994 * @param peer identity of the peer we need an address for
997 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
998 const struct GNUNET_PeerIdentity *peer)
1000 struct PendingMessage *p;
1001 struct RequestAddressMessage *m;
1003 // FIXME: ATS needs to remember this in case of
1005 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1006 sizeof (struct RequestAddressMessage));
1007 p->size = sizeof (struct RequestAddressMessage);
1008 p->is_init = GNUNET_NO;
1009 m = (struct RequestAddressMessage *) &p[1];
1010 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1011 m->header.size = htons (sizeof (struct RequestAddressMessage));
1012 m->reserved = htonl (0);
1014 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1020 * We would like to stop receiving address updates for this peer
1023 * @param peer identity of the peer
1026 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1027 const struct GNUNET_PeerIdentity *peer)
1029 struct PendingMessage *p;
1030 struct RequestAddressMessage *m;
1032 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1033 sizeof (struct RequestAddressMessage));
1034 p->size = sizeof (struct RequestAddressMessage);
1035 p->is_init = GNUNET_NO;
1036 m = (struct RequestAddressMessage *) &p[1];
1037 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1038 m->header.size = htons (sizeof (struct RequestAddressMessage));
1039 m->reserved = htonl (0);
1041 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1047 * We have a new address ATS should know. Addresses have to be added with this
1048 * function before they can be: updated, set in use and destroyed
1051 * @param address the address
1052 * @param session session handle (if available)
1053 * @param ats performance data for the address
1054 * @param ats_count number of performance records in 'ats'
1055 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1058 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1059 const struct GNUNET_HELLO_Address *address,
1060 struct Session *session,
1061 const struct GNUNET_ATS_Information *ats,
1065 struct PendingMessage *p;
1066 struct AddressUpdateMessage *m;
1067 struct GNUNET_ATS_Information *am;
1073 if (address == NULL)
1076 return GNUNET_SYSERR;
1078 if ((address == NULL) && (session == NULL))
1081 return GNUNET_SYSERR;
1085 (address->transport_name ==
1086 NULL) ? 0 : strlen (address->transport_name) + 1;
1088 sizeof (struct AddressUpdateMessage) + address->address_length +
1089 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1090 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1091 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1092 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1094 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1097 return GNUNET_SYSERR;
1100 if (NULL != session)
1102 s = find_session_id (sh, session, &address->peer);
1105 /* Already existing, nothing todo */
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1108 GNUNET_i2s (&address->peer),
1109 address->transport_name, session, s);
1110 return GNUNET_SYSERR;
1112 s = find_empty_session_slot (sh, session, &address->peer);
1113 GNUNET_break (NOT_FOUND != s);
1116 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1118 p->is_init = GNUNET_NO;
1119 m = (struct AddressUpdateMessage *) &p[1];
1120 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1121 m->header.size = htons (msize);
1122 m->ats_count = htonl (ats_count);
1123 m->peer = address->peer;
1124 m->address_length = htons (address->address_length);
1125 m->plugin_name_length = htons (namelen);
1126 m->session_id = htonl (s);
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1130 GNUNET_i2s (&address->peer),
1131 address->transport_name, session, s);
1133 am = (struct GNUNET_ATS_Information *) &m[1];
1134 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1135 pm = (char *) &am[ats_count];
1136 memcpy (pm, address->address, address->address_length);
1137 memcpy (&pm[address->address_length], address->transport_name, namelen);
1138 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1146 * We have updated performance statistics for a given address. Note
1147 * that this function can be called for addresses that are currently
1148 * in use as well as addresses that are valid but not actively in use.
1149 * Furthermore, the peer may not even be connected to us right now (in
1150 * which case the call may be ignored or the information may be stored
1151 * for later use). Update bandwidth assignments.
1154 * @param address the address
1155 * @param session session handle (if available)
1156 * @param ats performance data for the address
1157 * @param ats_count number of performance records in 'ats'
1160 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1161 const struct GNUNET_HELLO_Address *address,
1162 struct Session *session,
1163 const struct GNUNET_ATS_Information *ats,
1166 struct PendingMessage *p;
1167 struct AddressUpdateMessage *m;
1168 struct GNUNET_ATS_Information *am;
1174 if (address == NULL)
1179 if ((address == NULL) && (session == NULL))
1186 (address->transport_name ==
1187 NULL) ? 0 : strlen (address->transport_name) + 1;
1189 sizeof (struct AddressUpdateMessage) + address->address_length +
1190 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1191 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1192 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1193 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1195 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1201 if (NULL != session)
1203 s = find_session_id (sh, session, &address->peer);
1206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1207 "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1208 GNUNET_i2s (&address->peer),
1209 address->transport_name, session, s);
1216 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1218 p->is_init = GNUNET_NO;
1219 m = (struct AddressUpdateMessage *) &p[1];
1220 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1221 m->header.size = htons (msize);
1222 m->ats_count = htonl (ats_count);
1223 m->peer = address->peer;
1224 m->address_length = htons (address->address_length);
1225 m->plugin_name_length = htons (namelen);
1227 m->session_id = htonl (s);
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1231 GNUNET_i2s (&address->peer),
1232 address->transport_name, session, s);
1234 am = (struct GNUNET_ATS_Information *) &m[1];
1235 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1236 pm = (char *) &am[ats_count];
1237 memcpy (pm, address->address, address->address_length);
1238 memcpy (&pm[address->address_length], address->transport_name, namelen);
1239 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1246 * An address is now in use or not used any more.
1249 * @param address the address
1250 * @param session session handle
1251 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1252 * if address is not used any more
1255 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1256 const struct GNUNET_HELLO_Address *address,
1257 struct Session *session, int in_use)
1259 struct PendingMessage *p;
1260 struct AddressUseMessage *m;
1266 GNUNET_assert (NULL != address);
1268 (address->transport_name ==
1269 NULL) ? 0 : strlen (address->transport_name) + 1;
1270 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1271 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1272 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1273 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1279 if (session != NULL)
1281 s = find_session_id (sh, session, &address->peer);
1282 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1284 /* trying to set unknown address to NO */
1285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1286 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1287 GNUNET_i2s (&address->peer), address->transport_name, session);
1291 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1293 /* trying to set new address to YES */
1294 s = find_empty_session_slot (sh, session, &address->peer);
1295 GNUNET_assert (NOT_FOUND != s);
1299 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1301 p->is_init = GNUNET_NO;
1302 m = (struct AddressUseMessage *) &p[1];
1303 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1304 m->header.size = htons (msize);
1305 m->peer = address->peer;
1306 m->in_use = htons (in_use);
1307 m->address_length = htons (address->address_length);
1308 m->plugin_name_length = htons (namelen);
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1312 (GNUNET_YES == in_use) ? "YES" : "NO",
1313 GNUNET_i2s (&address->peer), address->transport_name, session);
1315 m->session_id = htonl (s);
1316 pm = (char *) &m[1];
1317 memcpy (pm, address->address, address->address_length);
1318 memcpy (&pm[address->address_length], address->transport_name, namelen);
1319 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1326 * A session got destroyed, stop including it as a valid address.
1329 * @param address the address
1330 * @param session session handle that is no longer valid
1333 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1334 const struct GNUNET_HELLO_Address *address,
1335 struct Session *session)
1337 struct PendingMessage *p;
1338 struct AddressDestroyedMessage *m;
1344 GNUNET_assert (address->transport_name != NULL);
1345 namelen = strlen (address->transport_name) + 1;
1346 GNUNET_assert (namelen > 1);
1348 sizeof (struct AddressDestroyedMessage) + address->address_length +
1350 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1351 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1352 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1358 s = find_session_id (sh, session, &address->peer);
1359 if ((NULL != session) && (NOT_FOUND == s))
1361 /* trying to delete unknown address */
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1364 GNUNET_i2s (&address->peer), address->transport_name, session);
1368 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1370 p->is_init = GNUNET_NO;
1371 m = (struct AddressDestroyedMessage *) &p[1];
1372 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1373 m->header.size = htons (msize);
1374 m->reserved = htonl (0);
1375 m->peer = address->peer;
1376 m->address_length = htons (address->address_length);
1377 m->plugin_name_length = htons (namelen);
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Deleting address for peer `%s', plugin `%s', session %p\n",
1381 GNUNET_i2s (&address->peer), address->transport_name, session);
1383 m->session_id = htonl (s);
1384 pm = (char *) &m[1];
1385 memcpy (pm, address->address, address->address_length);
1386 memcpy (&pm[address->address_length], address->transport_name, namelen);
1387 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1389 remove_session (sh, s, &address->peer);
1392 /* end of ats_api_scheduling.c */