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)
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
416 "Get session ID for session %p from peer %s in %p\n", session,
417 GNUNET_i2s (peer), sh);
422 for (i = 1; i < sh->session_array_size; i++)
424 if (session == sh->session_array[i].session)
426 if (0 != memcmp (peer, &sh->session_array[i].peer,
427 sizeof (struct GNUNET_PeerIdentity)))
429 p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
430 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
431 "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
432 session, GNUNET_i2s (peer), p2);
444 * Remove the session of the given session ID from the session
445 * table (it is no longer valid).
447 * @param sh our handle
448 * @param session_id identifies session that is no longer valid
449 * @param peer peer the session belongs to
452 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
453 const struct GNUNET_PeerIdentity *peer)
455 GNUNET_assert (peer != NULL);
456 GNUNET_assert (sh != NULL);
458 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
459 "Release sessionID %u from peer %s in %p\n",
460 (unsigned int) session_id, GNUNET_i2s (peer), sh);
465 GNUNET_assert (session_id < sh->session_array_size);
466 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
467 GNUNET_assert (0 == memcmp (peer,
468 &sh->session_array[session_id].peer,
469 sizeof (struct GNUNET_PeerIdentity)));
470 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
471 "Session %p for peer `%s' removed from slot %u \n",
472 sh->session_array[session_id].session,
475 sh->session_array[session_id].session = NULL;
481 * Release the session slot from the session table (ATS service is
482 * also done using it).
484 * @param sh our handle
485 * @param session_id identifies session that is no longer valid
486 * @param peer peer the session belongs to
489 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
490 const struct GNUNET_PeerIdentity *peer)
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);
497 if (session_id >= sh->session_array_size)
500 sh->reconnect = GNUNET_YES;
504 /* this slot should have been removed from remove_session before */
505 GNUNET_assert (sh->session_array[session_id].session == NULL);
508 memcmp (peer, &sh->session_array[session_id].peer,
509 sizeof (struct GNUNET_PeerIdentity)))
512 sh->reconnect = GNUNET_YES;
515 sh->session_array[session_id].slot_used = GNUNET_NO;
516 memset (&sh->session_array[session_id].peer, 0,
517 sizeof (struct GNUNET_PeerIdentity));
522 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
523 const struct SessionReleaseMessage *srm)
525 release_session (sh, ntohl (srm->session_id), &srm->peer);
530 * Type of a function to call when we receive a message
533 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
534 * @param msg message received, NULL on timeout or fatal error
537 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
539 struct GNUNET_ATS_SchedulingHandle *sh = cls;
540 const struct AddressSuggestionMessage *m;
541 const struct GNUNET_ATS_Information *atsi;
542 const char *plugin_address;
543 const char *plugin_name;
544 uint16_t plugin_address_length;
545 uint16_t plugin_name_length;
547 struct GNUNET_HELLO_Address address;
552 force_reconnect (sh);
555 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
556 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
558 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
559 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
560 GNUNET_TIME_UNIT_FOREVER_REL);
561 if (GNUNET_YES == sh->reconnect)
562 force_reconnect (sh);
565 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
566 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
569 force_reconnect (sh);
572 m = (const struct AddressSuggestionMessage *) msg;
573 ats_count = ntohl (m->ats_count);
574 plugin_address_length = ntohs (m->address_length);
575 atsi = (const struct GNUNET_ATS_Information *) &m[1];
576 plugin_address = (const char *) &atsi[ats_count];
577 plugin_name = &plugin_address[plugin_address_length];
578 plugin_name_length = ntohs (m->plugin_name_length);
579 if ((plugin_address_length + plugin_name_length +
580 ats_count * sizeof (struct GNUNET_ATS_Information) +
581 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
583 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
584 || (plugin_name[plugin_name_length - 1] != '\0'))
587 force_reconnect (sh);
590 uint32_t session_id = ntohl (m->session_id);
596 s = find_session (sh, session_id, &m->peer);
600 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
601 "ATS tries to use outdated session `%s'\n",
602 GNUNET_i2s (&m->peer));
603 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
604 GNUNET_TIME_UNIT_FOREVER_REL);
608 address.peer = m->peer;
609 address.address = plugin_address;
610 address.address_length = plugin_address_length;
611 address.transport_name = plugin_name;
613 if ((s == NULL) && (0 == address.address_length))
615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
616 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
617 GNUNET_i2s (&address.peer), address.transport_name,
618 plugin_address_length, session_id);
620 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
621 GNUNET_TIME_UNIT_FOREVER_REL);
625 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
626 m->bandwidth_in, atsi, ats_count);
628 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
629 GNUNET_TIME_UNIT_FOREVER_REL);
630 if (GNUNET_YES == sh->reconnect)
631 force_reconnect (sh);
636 * Re-establish the connection to the ATS service.
638 * @param sh handle to use to re-connect.
641 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
643 struct PendingMessage *p;
644 struct ClientStartMessage *init;
646 GNUNET_assert (NULL == sh->client);
647 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
648 GNUNET_assert (NULL != sh->client);
649 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
651 p = GNUNET_malloc (sizeof (struct PendingMessage) +
652 sizeof (struct ClientStartMessage));
653 p->size = sizeof (struct ClientStartMessage);
654 p->is_init = GNUNET_YES;
655 init = (struct ClientStartMessage *) &p[1];
656 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
657 init->header.size = htons (sizeof (struct ClientStartMessage));
658 init->start_flag = htonl (START_FLAG_SCHEDULING);
659 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
666 * delete the current network list
669 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
671 struct ATS_Network * cur = sh->net_head;
674 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
682 interface_proc (void *cls, const char *name,
684 const struct sockaddr *
686 const struct sockaddr *
688 const struct sockaddr *
689 netmask, socklen_t addrlen)
691 struct GNUNET_ATS_SchedulingHandle * sh = cls;
692 /* Calculate network */
693 struct ATS_Network *net = NULL;
695 /* Skipping IPv4 loopback addresses since we have special check */
696 if (addr->sa_family == AF_INET)
698 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
700 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
703 /* Skipping IPv6 loopback addresses since we have special check */
704 if (addr->sa_family == AF_INET6)
706 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
707 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
711 if (addr->sa_family == AF_INET)
713 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
714 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
715 struct sockaddr_in *tmp = NULL;
716 struct sockaddr_in network4;
718 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
719 tmp = (struct sockaddr_in *) &net[1];
720 net->network = (struct sockaddr *) &tmp[0];
721 net->netmask = (struct sockaddr *) &tmp[1];
722 net->length = addrlen;
724 memset (&network4, 0, sizeof (network4));
725 network4.sin_family = AF_INET;
726 #if HAVE_SOCKADDR_IN_SIN_LEN
727 network4.sin_len = sizeof (network4);
729 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
731 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
732 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
735 if (addr->sa_family == AF_INET6)
737 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
738 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
739 struct sockaddr_in6 * tmp = NULL;
740 struct sockaddr_in6 network6;
742 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
743 tmp = (struct sockaddr_in6 *) &net[1];
744 net->network = (struct sockaddr *) &tmp[0];
745 net->netmask = (struct sockaddr *) &tmp[1];
746 net->length = addrlen;
748 memset (&network6, 0, sizeof (network6));
749 network6.sin6_family = AF_INET6;
750 #if HAVE_SOCKADDR_IN_SIN_LEN
751 network6.sin6_len = sizeof (network6);
754 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
755 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
756 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
757 for (c = 0; c < 4; c++)
758 net_elem[c] = addr_elem[c] & mask_elem[c];
760 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
761 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
768 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
770 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
772 GNUNET_free (netmask);
774 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
781 * Periodically get list of addresses
783 * @param tc Task context
786 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
788 struct GNUNET_ATS_SchedulingHandle * sh = cls;
789 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
790 delete_networks (sh);
791 GNUNET_OS_network_interfaces_list(interface_proc, sh);
792 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
799 * Returns where the address is located: LAN or WAN or ...
801 * @param sh the scheduling handle
802 * @param addr address
803 * @param addrlen address length
804 * @return location as GNUNET_ATS_Information
806 struct GNUNET_ATS_Information
807 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
809 GNUNET_assert (sh != NULL);
810 struct GNUNET_ATS_Information ats;
811 struct ATS_Network * cur = sh->net_head;
812 int type = GNUNET_ATS_NET_UNSPECIFIED;
814 if (addr->sa_family == AF_UNIX)
816 type = GNUNET_ATS_NET_LOOPBACK;
819 /* IPv4 loopback check */
820 if (addr->sa_family == AF_INET)
822 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
824 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
825 type = GNUNET_ATS_NET_LOOPBACK;
827 /* IPv6 loopback check */
828 if (addr->sa_family == AF_INET6)
830 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
831 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
832 type = GNUNET_ATS_NET_LOOPBACK;
835 /* Check local networks */
836 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
838 if (addrlen != cur->length)
844 if (addr->sa_family == AF_INET)
846 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
847 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
848 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
850 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
852 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
853 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
854 "`%s' is in network `%s'\n",
855 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
858 type = GNUNET_ATS_NET_LAN;
861 if (addr->sa_family == AF_INET6)
863 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
864 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
865 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
867 int res = GNUNET_YES;
869 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
870 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
871 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
872 for (c = 0; c < 4; c++)
873 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
876 if (res == GNUNET_YES)
878 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
880 GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
883 type = GNUNET_ATS_NET_LAN;
889 /* no local network found for this address, default: WAN */
890 if (type == GNUNET_ATS_NET_UNSPECIFIED)
891 type = GNUNET_ATS_NET_WAN;
892 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
893 ats.value = htonl (type);
894 return (const struct GNUNET_ATS_Information) ats;
899 * Initialize the ATS subsystem.
901 * @param cfg configuration to use
902 * @param suggest_cb notification to call whenever the suggestation changed
903 * @param suggest_cb_cls closure for 'suggest_cb'
904 * @return ats context
906 struct GNUNET_ATS_SchedulingHandle *
907 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
908 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
909 void *suggest_cb_cls)
911 struct GNUNET_ATS_SchedulingHandle *sh;
913 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
915 sh->suggest_cb = suggest_cb;
916 sh->suggest_cb_cls = suggest_cb_cls;
917 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
918 GNUNET_OS_network_interfaces_list(interface_proc, sh);
919 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
928 * Client is done with ATS scheduling, release resources.
930 * @param sh handle to release
933 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
935 struct PendingMessage *p;
937 while (NULL != (p = sh->pending_head))
939 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
942 if (NULL != sh->client)
944 GNUNET_CLIENT_disconnect (sh->client);
947 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
949 GNUNET_SCHEDULER_cancel (sh->task);
950 sh->task = GNUNET_SCHEDULER_NO_TASK;
953 delete_networks (sh);
954 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
956 GNUNET_SCHEDULER_cancel(sh->interface_task);
957 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
959 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
965 * We would like to reset the address suggestion block time for this
969 * @param peer identity of the peer we want to reset
972 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
973 const struct GNUNET_PeerIdentity *peer)
975 struct PendingMessage *p;
976 struct ResetBackoffMessage *m;
978 p = GNUNET_malloc (sizeof (struct PendingMessage) +
979 sizeof (struct ResetBackoffMessage));
980 p->size = sizeof (struct ResetBackoffMessage);
981 p->is_init = GNUNET_NO;
982 m = (struct ResetBackoffMessage *) &p[1];
983 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
984 m->header.size = htons (sizeof (struct ResetBackoffMessage));
985 m->reserved = htonl (0);
987 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
992 * We would like to establish a new connection with a peer. ATS
993 * should suggest a good address to begin with.
996 * @param peer identity of the peer we need an address for
999 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1000 const struct GNUNET_PeerIdentity *peer)
1002 struct PendingMessage *p;
1003 struct RequestAddressMessage *m;
1005 // FIXME: ATS needs to remember this in case of
1007 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1008 sizeof (struct RequestAddressMessage));
1009 p->size = sizeof (struct RequestAddressMessage);
1010 p->is_init = GNUNET_NO;
1011 m = (struct RequestAddressMessage *) &p[1];
1012 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1013 m->header.size = htons (sizeof (struct RequestAddressMessage));
1014 m->reserved = htonl (0);
1016 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1022 * We would like to stop receiving address updates for this peer
1025 * @param peer identity of the peer
1028 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1029 const struct GNUNET_PeerIdentity *peer)
1031 struct PendingMessage *p;
1032 struct RequestAddressMessage *m;
1034 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1035 sizeof (struct RequestAddressMessage));
1036 p->size = sizeof (struct RequestAddressMessage);
1037 p->is_init = GNUNET_NO;
1038 m = (struct RequestAddressMessage *) &p[1];
1039 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1040 m->header.size = htons (sizeof (struct RequestAddressMessage));
1041 m->reserved = htonl (0);
1043 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1049 * We have a new address ATS should know. Addresses have to be added with this
1050 * function before they can be: updated, set in use and destroyed
1053 * @param address the address
1054 * @param session session handle (if available)
1055 * @param ats performance data for the address
1056 * @param ats_count number of performance records in 'ats'
1057 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1060 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1061 const struct GNUNET_HELLO_Address *address,
1062 struct Session *session,
1063 const struct GNUNET_ATS_Information *ats,
1067 struct PendingMessage *p;
1068 struct AddressUpdateMessage *m;
1069 struct GNUNET_ATS_Information *am;
1075 if (address == NULL)
1078 return GNUNET_SYSERR;
1080 if ((address == NULL) && (session == NULL))
1083 return GNUNET_SYSERR;
1087 (address->transport_name ==
1088 NULL) ? 0 : strlen (address->transport_name) + 1;
1090 sizeof (struct AddressUpdateMessage) + address->address_length +
1091 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1092 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1093 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1094 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1096 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1099 return GNUNET_SYSERR;
1102 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1104 p->is_init = GNUNET_NO;
1105 m = (struct AddressUpdateMessage *) &p[1];
1106 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1107 m->header.size = htons (msize);
1108 m->ats_count = htonl (ats_count);
1109 m->peer = address->peer;
1110 m->address_length = htons (address->address_length);
1111 m->plugin_name_length = htons (namelen);
1112 if (NULL != session)
1114 s = find_session_id (sh, session, &address->peer);
1117 /* Already existing, nothing todo */
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1120 GNUNET_i2s (&address->peer),
1121 address->transport_name, session, s);
1122 return GNUNET_SYSERR;
1124 s = find_empty_session_slot (sh, session, &address->peer);
1125 GNUNET_break (NOT_FOUND != s);
1127 m->session_id = htonl (s);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1131 GNUNET_i2s (&address->peer),
1132 address->transport_name, session, s);
1134 am = (struct GNUNET_ATS_Information *) &m[1];
1135 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1136 pm = (char *) &am[ats_count];
1137 memcpy (pm, address->address, address->address_length);
1138 memcpy (&pm[address->address_length], address->transport_name, namelen);
1139 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1147 * We have updated performance statistics for a given address. Note
1148 * that this function can be called for addresses that are currently
1149 * in use as well as addresses that are valid but not actively in use.
1150 * Furthermore, the peer may not even be connected to us right now (in
1151 * which case the call may be ignored or the information may be stored
1152 * for later use). Update bandwidth assignments.
1155 * @param address the address
1156 * @param session session handle (if available)
1157 * @param ats performance data for the address
1158 * @param ats_count number of performance records in 'ats'
1161 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1162 const struct GNUNET_HELLO_Address *address,
1163 struct Session *session,
1164 const struct GNUNET_ATS_Information *ats,
1167 struct PendingMessage *p;
1168 struct AddressUpdateMessage *m;
1169 struct GNUNET_ATS_Information *am;
1175 if (address == NULL)
1180 if ((address == NULL) && (session == NULL))
1187 (address->transport_name ==
1188 NULL) ? 0 : strlen (address->transport_name) + 1;
1190 sizeof (struct AddressUpdateMessage) + address->address_length +
1191 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1192 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1193 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1194 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1196 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1202 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1204 p->is_init = GNUNET_NO;
1205 m = (struct AddressUpdateMessage *) &p[1];
1206 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1207 m->header.size = htons (msize);
1208 m->ats_count = htonl (ats_count);
1209 m->peer = address->peer;
1210 m->address_length = htons (address->address_length);
1211 m->plugin_name_length = htons (namelen);
1212 if (NULL != session)
1214 s = find_session_id (sh, session, &address->peer);
1217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1218 "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1219 GNUNET_i2s (&address->peer),
1220 address->transport_name, session, s);
1226 m->session_id = htonl (s);
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229 "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1230 GNUNET_i2s (&address->peer),
1231 address->transport_name, session, s);
1233 am = (struct GNUNET_ATS_Information *) &m[1];
1234 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1235 pm = (char *) &am[ats_count];
1236 memcpy (pm, address->address, address->address_length);
1237 memcpy (&pm[address->address_length], address->transport_name, namelen);
1238 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1245 * An address is now in use or not used any more.
1248 * @param address the address
1249 * @param session session handle
1250 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1251 * if address is not used any more
1254 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1255 const struct GNUNET_HELLO_Address *address,
1256 struct Session *session, int in_use)
1258 struct PendingMessage *p;
1259 struct AddressUseMessage *m;
1265 GNUNET_assert (NULL != address);
1267 (address->transport_name ==
1268 NULL) ? 0 : strlen (address->transport_name) + 1;
1269 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1270 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1271 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1272 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1278 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1280 p->is_init = GNUNET_NO;
1281 m = (struct AddressUseMessage *) &p[1];
1282 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1283 m->header.size = htons (msize);
1284 m->peer = address->peer;
1285 m->in_use = htons (in_use);
1286 m->address_length = htons (address->address_length);
1287 m->plugin_name_length = htons (namelen);
1288 if (session != NULL)
1290 s = find_session_id (sh, session, &address->peer);
1291 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1293 /* trying to set unknown address to NO */
1294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1295 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1296 GNUNET_i2s (&address->peer), address->transport_name, session);
1300 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1302 /* trying to set new address to YES */
1303 s = find_empty_session_slot (sh, session, &address->peer);
1304 GNUNET_assert (NOT_FOUND != s);
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1310 (GNUNET_YES == in_use) ? "YES" : "NO",
1311 GNUNET_i2s (&address->peer), address->transport_name, session);
1313 m->session_id = htonl (s);
1314 pm = (char *) &m[1];
1315 memcpy (pm, address->address, address->address_length);
1316 memcpy (&pm[address->address_length], address->transport_name, namelen);
1317 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1324 * A session got destroyed, stop including it as a valid address.
1327 * @param address the address
1328 * @param session session handle that is no longer valid
1331 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1332 const struct GNUNET_HELLO_Address *address,
1333 struct Session *session)
1335 struct PendingMessage *p;
1336 struct AddressDestroyedMessage *m;
1342 GNUNET_assert (address->transport_name != NULL);
1343 namelen = strlen (address->transport_name) + 1;
1344 GNUNET_assert (namelen > 1);
1346 sizeof (struct AddressDestroyedMessage) + address->address_length +
1348 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1349 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1350 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1356 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1358 p->is_init = GNUNET_NO;
1359 m = (struct AddressDestroyedMessage *) &p[1];
1360 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1361 m->header.size = htons (msize);
1362 m->reserved = htonl (0);
1363 m->peer = address->peer;
1364 m->address_length = htons (address->address_length);
1365 m->plugin_name_length = htons (namelen);
1367 s = find_session_id (sh, session, &address->peer);
1368 if ((NULL != session) && (NOT_FOUND == s))
1370 /* trying to delete unknown address */
1371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1372 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1373 GNUNET_i2s (&address->peer), address->transport_name, session);
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);
1384 m->session_id = htonl (s);
1385 pm = (char *) &m[1];
1386 memcpy (pm, address->address, address->address_length);
1387 memcpy (&pm[address->address_length], address->transport_name, namelen);
1388 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1390 remove_session (sh, s, &address->peer);
1393 /* end of ats_api_scheduling.c */