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,
813 * Returns where the address is located: LAN or WAN or ...
815 * @param sh the scheduling handle
816 * @param addr address
817 * @param addrlen address length
818 * @return location as GNUNET_ATS_Information
820 struct GNUNET_ATS_Information
821 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
823 GNUNET_assert (sh != NULL);
824 struct ATS_Network * cur = sh->net_head;
825 char *networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
826 int type = GNUNET_ATS_NET_UNSPECIFIED;
827 struct GNUNET_ATS_Information ats;
829 if (addr->sa_family == AF_UNIX)
831 type = GNUNET_ATS_NET_LOOPBACK;
834 /* IPv4 loopback check */
835 if (addr->sa_family == AF_INET)
837 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
839 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
840 type = GNUNET_ATS_NET_LOOPBACK;
842 /* IPv6 loopback check */
843 if (addr->sa_family == AF_INET6)
845 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
846 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
847 type = GNUNET_ATS_NET_LOOPBACK;
850 /* Check local networks */
851 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
853 if (addrlen != cur->length)
859 if (addr->sa_family == AF_INET)
861 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
862 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
863 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
865 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
866 type = GNUNET_ATS_NET_LAN;
868 if (addr->sa_family == AF_INET6)
870 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
871 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
872 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
874 int res = GNUNET_YES;
876 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
877 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
878 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
879 for (c = 0; c < 4; c++)
880 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
883 if (res == GNUNET_YES)
884 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);
895 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
896 "`%s' is in network `%s'\n",
897 GNUNET_a2s ((const struct sockaddr *) addr, addrlen),
905 * Initialize the ATS subsystem.
907 * @param cfg configuration to use
908 * @param suggest_cb notification to call whenever the suggestation changed
909 * @param suggest_cb_cls closure for 'suggest_cb'
910 * @return ats context
912 struct GNUNET_ATS_SchedulingHandle *
913 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
914 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
915 void *suggest_cb_cls)
917 struct GNUNET_ATS_SchedulingHandle *sh;
919 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
921 sh->suggest_cb = suggest_cb;
922 sh->suggest_cb_cls = suggest_cb_cls;
923 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
924 GNUNET_OS_network_interfaces_list(interface_proc, sh);
925 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
934 * Client is done with ATS scheduling, release resources.
936 * @param sh handle to release
939 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
941 struct PendingMessage *p;
943 while (NULL != (p = sh->pending_head))
945 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
948 if (NULL != sh->client)
950 GNUNET_CLIENT_disconnect (sh->client);
953 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
955 GNUNET_SCHEDULER_cancel (sh->task);
956 sh->task = GNUNET_SCHEDULER_NO_TASK;
959 delete_networks (sh);
960 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
962 GNUNET_SCHEDULER_cancel(sh->interface_task);
963 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
965 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
971 * We would like to reset the address suggestion block time for this
975 * @param peer identity of the peer we want to reset
978 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
979 const struct GNUNET_PeerIdentity *peer)
981 struct PendingMessage *p;
982 struct ResetBackoffMessage *m;
984 p = GNUNET_malloc (sizeof (struct PendingMessage) +
985 sizeof (struct ResetBackoffMessage));
986 p->size = sizeof (struct ResetBackoffMessage);
987 p->is_init = GNUNET_NO;
988 m = (struct ResetBackoffMessage *) &p[1];
989 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
990 m->header.size = htons (sizeof (struct ResetBackoffMessage));
991 m->reserved = htonl (0);
993 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
998 * We would like to establish a new connection with a peer. ATS
999 * should suggest a good address to begin with.
1002 * @param peer identity of the peer we need an address for
1005 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1006 const struct GNUNET_PeerIdentity *peer)
1008 struct PendingMessage *p;
1009 struct RequestAddressMessage *m;
1011 // FIXME: ATS needs to remember this in case of
1013 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1014 sizeof (struct RequestAddressMessage));
1015 p->size = sizeof (struct RequestAddressMessage);
1016 p->is_init = GNUNET_NO;
1017 m = (struct RequestAddressMessage *) &p[1];
1018 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1019 m->header.size = htons (sizeof (struct RequestAddressMessage));
1020 m->reserved = htonl (0);
1022 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1028 * We would like to stop receiving address updates for this peer
1031 * @param peer identity of the peer
1034 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1035 const struct GNUNET_PeerIdentity *peer)
1037 struct PendingMessage *p;
1038 struct RequestAddressMessage *m;
1040 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1041 sizeof (struct RequestAddressMessage));
1042 p->size = sizeof (struct RequestAddressMessage);
1043 p->is_init = GNUNET_NO;
1044 m = (struct RequestAddressMessage *) &p[1];
1045 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1046 m->header.size = htons (sizeof (struct RequestAddressMessage));
1047 m->reserved = htonl (0);
1049 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1055 * We have a new address ATS should know. Addresses have to be added with this
1056 * function before they can be: updated, set in use and destroyed
1059 * @param address the address
1060 * @param session session handle, can be NULL
1061 * @param ats performance data for the address
1062 * @param ats_count number of performance records in 'ats'
1063 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1066 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1067 const struct GNUNET_HELLO_Address *address,
1068 struct Session *session,
1069 const struct GNUNET_ATS_Information *ats,
1073 struct PendingMessage *p;
1074 struct AddressUpdateMessage *m;
1075 struct GNUNET_ATS_Information *am;
1081 if (address == NULL)
1084 return GNUNET_SYSERR;
1088 (address->transport_name ==
1089 NULL) ? 0 : strlen (address->transport_name) + 1;
1091 sizeof (struct AddressUpdateMessage) + address->address_length +
1092 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1093 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1094 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1095 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1097 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1100 return GNUNET_SYSERR;
1103 if (NULL != session)
1105 s = find_session_id (sh, session, &address->peer);
1108 /* Already existing, nothing todo */
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1111 GNUNET_i2s (&address->peer),
1112 address->transport_name, session, s);
1113 return GNUNET_SYSERR;
1115 s = find_empty_session_slot (sh, session, &address->peer);
1116 GNUNET_break (NOT_FOUND != s);
1119 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1121 p->is_init = GNUNET_NO;
1122 m = (struct AddressUpdateMessage *) &p[1];
1123 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1124 m->header.size = htons (msize);
1125 m->ats_count = htonl (ats_count);
1126 m->peer = address->peer;
1127 m->address_length = htons (address->address_length);
1128 m->plugin_name_length = htons (namelen);
1129 m->session_id = htonl (s);
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1133 GNUNET_i2s (&address->peer),
1134 address->transport_name, session, s);
1136 am = (struct GNUNET_ATS_Information *) &m[1];
1137 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1138 pm = (char *) &am[ats_count];
1139 memcpy (pm, address->address, address->address_length);
1140 if (NULL != address->transport_name)
1141 memcpy (&pm[address->address_length], address->transport_name, namelen);
1142 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1150 * We have updated performance statistics for a given address. Note
1151 * that this function can be called for addresses that are currently
1152 * in use as well as addresses that are valid but not actively in use.
1153 * Furthermore, the peer may not even be connected to us right now (in
1154 * which case the call may be ignored or the information may be stored
1155 * for later use). Update bandwidth assignments.
1158 * @param address the address
1159 * @param session session handle, can be NULL
1160 * @param ats performance data for the address
1161 * @param ats_count number of performance records in 'ats'
1164 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1165 const struct GNUNET_HELLO_Address *address,
1166 struct Session *session,
1167 const struct GNUNET_ATS_Information *ats,
1170 struct PendingMessage *p;
1171 struct AddressUpdateMessage *m;
1172 struct GNUNET_ATS_Information *am;
1178 if (address == NULL)
1185 (address->transport_name ==
1186 NULL) ? 0 : strlen (address->transport_name) + 1;
1188 sizeof (struct AddressUpdateMessage) + address->address_length +
1189 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1190 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1191 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1192 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1194 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1200 if (NULL != session)
1202 s = find_session_id (sh, session, &address->peer);
1205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1206 "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1207 GNUNET_i2s (&address->peer),
1208 address->transport_name, session, s);
1215 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1217 p->is_init = GNUNET_NO;
1218 m = (struct AddressUpdateMessage *) &p[1];
1219 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1220 m->header.size = htons (msize);
1221 m->ats_count = htonl (ats_count);
1222 m->peer = address->peer;
1223 m->address_length = htons (address->address_length);
1224 m->plugin_name_length = htons (namelen);
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, can be NULL
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 if (session != NULL)
1280 s = find_session_id (sh, session, &address->peer);
1281 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1283 /* trying to set unknown address to NO */
1284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1285 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1286 GNUNET_i2s (&address->peer), address->transport_name, session);
1290 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1292 /* trying to set new address to YES */
1293 s = find_empty_session_slot (sh, session, &address->peer);
1294 GNUNET_assert (NOT_FOUND != s);
1298 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1300 p->is_init = GNUNET_NO;
1301 m = (struct AddressUseMessage *) &p[1];
1302 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1303 m->header.size = htons (msize);
1304 m->peer = address->peer;
1305 m->in_use = htons (in_use);
1306 m->address_length = htons (address->address_length);
1307 m->plugin_name_length = htons (namelen);
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1311 (GNUNET_YES == in_use) ? "YES" : "NO",
1312 GNUNET_i2s (&address->peer), address->transport_name, session);
1314 m->session_id = htonl (s);
1315 pm = (char *) &m[1];
1316 memcpy (pm, address->address, address->address_length);
1317 memcpy (&pm[address->address_length], address->transport_name, namelen);
1318 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1325 * A session got destroyed, stop including it as a valid address.
1328 * @param address the address
1329 * @param session session handle that is no longer valid, can be NULL
1332 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1333 const struct GNUNET_HELLO_Address *address,
1334 struct Session *session)
1336 struct PendingMessage *p;
1337 struct AddressDestroyedMessage *m;
1343 if (address == NULL)
1349 GNUNET_assert (address->transport_name != NULL);
1350 namelen = strlen (address->transport_name) + 1;
1351 GNUNET_assert (namelen > 1);
1353 sizeof (struct AddressDestroyedMessage) + address->address_length +
1355 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1356 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1357 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1363 s = find_session_id (sh, session, &address->peer);
1364 if ((NULL != session) && (NOT_FOUND == s))
1366 /* trying to delete unknown address */
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1369 GNUNET_i2s (&address->peer), address->transport_name, session);
1373 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1375 p->is_init = GNUNET_NO;
1376 m = (struct AddressDestroyedMessage *) &p[1];
1377 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1378 m->header.size = htons (msize);
1379 m->reserved = htonl (0);
1380 m->peer = address->peer;
1381 m->address_length = htons (address->address_length);
1382 m->plugin_name_length = htons (namelen);
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Deleting address for peer `%s', plugin `%s', session %p\n",
1386 GNUNET_i2s (&address->peer), address->transport_name, session);
1388 m->session_id = htonl (s);
1389 pm = (char *) &m[1];
1390 memcpy (pm, address->address, address->address_length);
1391 memcpy (&pm[address->address_length], address->transport_name, namelen);
1392 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1394 remove_session (sh, s, &address->peer);
1397 /* end of ats_api_scheduling.c */