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;
98 * Handle for address suggestions
100 struct GNUNET_ATS_SuggestHandle
102 struct GNUNET_ATS_SuggestHandle *prev;
103 struct GNUNET_ATS_SuggestHandle *next;
104 struct GNUNET_PeerIdentity id;
109 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
111 struct GNUNET_ATS_SchedulingHandle
117 const struct GNUNET_CONFIGURATION_Handle *cfg;
120 * Callback to invoke on suggestions.
122 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
125 * Closure for 'suggest_cb'.
127 void *suggest_cb_cls;
130 * DLL for suggestions head
132 struct GNUNET_ATS_SuggestHandle *sug_head;
135 * DLL for suggestions tail
137 struct GNUNET_ATS_SuggestHandle *sug_tail;
140 * Connection to ATS service.
142 struct GNUNET_CLIENT_Connection *client;
145 * Head of list of messages for the ATS service.
147 struct PendingMessage *pending_head;
150 * Tail of list of messages for the ATS service
152 struct PendingMessage *pending_tail;
155 * Current request for transmission to ATS.
157 struct GNUNET_CLIENT_TransmitHandle *th;
160 * Head of network list
162 struct ATS_Network * net_head;
165 * Tail of network list
167 struct ATS_Network * net_tail;
170 * Array of session objects (we need to translate them to numbers and back
171 * for the protocol; the offset in the array is the session number on the
172 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
173 * Unused entries are also NULL.
175 struct SessionRecord *session_array;
178 * Task to trigger reconnect.
180 GNUNET_SCHEDULER_TaskIdentifier task;
183 * Task retrieving interfaces from the system
185 GNUNET_SCHEDULER_TaskIdentifier interface_task;
189 * Size of the session array.
191 unsigned int session_array_size;
194 * Should we reconnect to ATS due to some serious error?
201 * Re-establish the connection to the ATS service.
203 * @param sh handle to use to re-connect.
206 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
210 * Re-establish the connection to the ATS service.
212 * @param cls handle to use to re-connect.
213 * @param tc scheduler context
216 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct GNUNET_ATS_SchedulingHandle *sh = cls;
220 sh->task = GNUNET_SCHEDULER_NO_TASK;
226 * Disconnect from ATS and then reconnect.
228 * @param sh our handle
231 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
233 sh->reconnect = GNUNET_NO;
234 GNUNET_CLIENT_disconnect (sh->client);
237 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
243 * Transmit messages from the message queue to the service
244 * (if there are any, and if we are not already trying).
246 * @param sh handle to use
249 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
253 * Type of a function to call when we receive a message
256 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
257 * @param msg message received, NULL on timeout or fatal error
260 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
264 * We can now transmit a message to ATS. Do it.
266 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
267 * @param size number of bytes we can transmit to ATS
268 * @param buf where to copy the messages
269 * @return number of bytes copied into buf
272 transmit_message_to_ats (void *cls, size_t size, void *buf)
274 struct GNUNET_ATS_SchedulingHandle *sh = cls;
275 struct PendingMessage *p;
280 if ((size == 0) || (buf == NULL))
282 force_reconnect (sh);
287 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
289 memcpy (&cbuf[ret], &p[1], p->size);
292 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
293 if (GNUNET_YES == p->is_init)
294 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
295 GNUNET_TIME_UNIT_FOREVER_REL);
304 * Transmit messages from the message queue to the service
305 * (if there are any, and if we are not already trying).
307 * @param sh handle to use
310 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
312 struct PendingMessage *p;
316 if (NULL == (p = sh->pending_head))
318 if (NULL == sh->client)
319 return; /* currently reconnecting */
321 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
322 GNUNET_TIME_UNIT_FOREVER_REL,
323 GNUNET_NO, &transmit_message_to_ats,
329 * Find the session object corresponding to the given session ID.
331 * @param sh our handle
332 * @param session_id current session ID
333 * @param peer peer the session belongs to
334 * @return the session object (or NULL)
336 static struct Session *
337 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
338 const struct GNUNET_PeerIdentity *peer)
341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
342 "Find session %u from peer %s in %p\n",
343 (unsigned int) session_id, GNUNET_i2s (peer), sh);
345 if (session_id >= sh->session_array_size)
352 if (sh->session_array[session_id].session == NULL)
355 memcmp (peer, &sh->session_array[session_id].peer,
356 sizeof (struct GNUNET_PeerIdentity)));
361 memcmp (peer, &sh->session_array[session_id].peer,
362 sizeof (struct GNUNET_PeerIdentity)))
365 sh->reconnect = GNUNET_YES;
368 /* This check exploits the fact that first field of a session object
372 memcmp (peer, sh->session_array[session_id].session,
373 sizeof (struct GNUNET_PeerIdentity)))
375 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
376 "Session %p belongs to peer `%s'\n",
377 sh->session_array[session_id].session, GNUNET_i2s_full ((struct GNUNET_PeerIdentity *) &sh->session_array[session_id].peer));
380 sh->reconnect = GNUNET_YES;
384 return sh->session_array[session_id].session;
389 * Get an available session ID for the given session object.
391 * @param sh our handle
392 * @param session session object
393 * @param peer peer the session belongs to
394 * @return the session id
397 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
398 const struct GNUNET_PeerIdentity *peer)
403 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
404 "Get session ID for session %p from peer %s in %p\n", session,
405 GNUNET_i2s (peer), sh);
410 for (i = 1; i < sh->session_array_size; i++)
412 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
417 f = sh->session_array_size;
418 GNUNET_array_grow (sh->session_array, sh->session_array_size,
419 sh->session_array_size * 2);
421 GNUNET_assert (f > 0);
422 sh->session_array[f].session = session;
423 sh->session_array[f].peer = *peer;
424 sh->session_array[f].slot_used = GNUNET_YES;
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
427 "Assigning session ID %u for session %p of peer %s in %p\n", f,
428 session, GNUNET_i2s (peer), sh);
435 * Get the ID for the given session object.
437 * @param sh our handle
438 * @param session session object
439 * @param peer peer the session belongs to
440 * @return the session id or NOT_FOUND for error
443 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
444 const struct GNUNET_PeerIdentity *peer)
449 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
450 "Get session ID for session %p from peer %s in %p\n", session,
451 GNUNET_i2s (peer), sh);
455 for (i = 1; i < sh->session_array_size; i++)
457 if (session == sh->session_array[i].session)
459 if (0 != memcmp (peer, &sh->session_array[i].peer,
460 sizeof (struct GNUNET_PeerIdentity)))
462 p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
463 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
464 "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
465 session, GNUNET_i2s (peer), p2);
477 * Remove the session of the given session ID from the session
478 * table (it is no longer valid).
480 * @param sh our handle
481 * @param session_id identifies session that is no longer valid
482 * @param peer peer the session belongs to
485 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
486 const struct GNUNET_PeerIdentity *peer)
488 GNUNET_assert (peer != NULL);
489 GNUNET_assert (sh != NULL);
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);
498 GNUNET_assert (session_id < sh->session_array_size);
499 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
500 GNUNET_assert (0 == memcmp (peer,
501 &sh->session_array[session_id].peer,
502 sizeof (struct GNUNET_PeerIdentity)));
503 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
504 "Session %p for peer `%s' removed from slot %u \n",
505 sh->session_array[session_id].session,
508 sh->session_array[session_id].session = NULL;
514 * Release the session slot from the session table (ATS service is
515 * also done using it).
517 * @param sh our handle
518 * @param session_id identifies session that is no longer valid
519 * @param peer peer the session belongs to
522 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
523 const struct GNUNET_PeerIdentity *peer)
526 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
527 "Release sessionID %u from peer %s in %p\n",
528 (unsigned int) session_id, GNUNET_i2s (peer), sh);
530 if (session_id >= sh->session_array_size)
533 sh->reconnect = GNUNET_YES;
537 /* this slot should have been removed from remove_session before */
538 GNUNET_assert (sh->session_array[session_id].session == NULL);
541 memcmp (peer, &sh->session_array[session_id].peer,
542 sizeof (struct GNUNET_PeerIdentity)))
545 sh->reconnect = GNUNET_YES;
548 sh->session_array[session_id].slot_used = GNUNET_NO;
549 memset (&sh->session_array[session_id].peer, 0,
550 sizeof (struct GNUNET_PeerIdentity));
555 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
556 const struct SessionReleaseMessage *srm)
558 release_session (sh, ntohl (srm->session_id), &srm->peer);
563 * Type of a function to call when we receive a message
566 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
567 * @param msg message received, NULL on timeout or fatal error
570 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
572 struct GNUNET_ATS_SchedulingHandle *sh = cls;
573 const struct AddressSuggestionMessage *m;
574 const struct GNUNET_ATS_Information *atsi;
575 const char *plugin_address;
576 const char *plugin_name;
577 uint16_t plugin_address_length;
578 uint16_t plugin_name_length;
580 struct GNUNET_HELLO_Address address;
585 force_reconnect (sh);
588 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
589 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
591 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
592 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
593 GNUNET_TIME_UNIT_FOREVER_REL);
594 if (GNUNET_YES == sh->reconnect)
595 force_reconnect (sh);
598 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
599 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
602 force_reconnect (sh);
605 m = (const struct AddressSuggestionMessage *) msg;
606 ats_count = ntohl (m->ats_count);
607 plugin_address_length = ntohs (m->address_length);
608 atsi = (const struct GNUNET_ATS_Information *) &m[1];
609 plugin_address = (const char *) &atsi[ats_count];
610 plugin_name = &plugin_address[plugin_address_length];
611 plugin_name_length = ntohs (m->plugin_name_length);
612 if ((plugin_address_length + plugin_name_length +
613 ats_count * sizeof (struct GNUNET_ATS_Information) +
614 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
616 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
617 || (plugin_name[plugin_name_length - 1] != '\0'))
620 force_reconnect (sh);
623 uint32_t session_id = ntohl (m->session_id);
629 s = find_session (sh, session_id, &m->peer);
633 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
634 "ATS tries to use outdated session `%s'\n",
635 GNUNET_i2s (&m->peer));
636 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
637 GNUNET_TIME_UNIT_FOREVER_REL);
642 if (NULL == sh->suggest_cb)
645 address.peer = m->peer;
646 address.address = plugin_address;
647 address.address_length = plugin_address_length;
648 address.transport_name = plugin_name;
650 if ((s == NULL) && (0 == address.address_length))
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
654 GNUNET_i2s (&address.peer), address.transport_name,
655 plugin_address_length, session_id);
657 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
658 GNUNET_TIME_UNIT_FOREVER_REL);
662 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
663 m->bandwidth_in, atsi, ats_count);
665 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
666 GNUNET_TIME_UNIT_FOREVER_REL);
667 if (GNUNET_YES == sh->reconnect)
668 force_reconnect (sh);
673 * Re-establish the connection to the ATS service.
675 * @param sh handle to use to re-connect.
678 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
680 struct PendingMessage *p;
681 struct ClientStartMessage *init;
683 GNUNET_assert (NULL == sh->client);
684 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
685 GNUNET_assert (NULL != sh->client);
686 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
688 p = GNUNET_malloc (sizeof (struct PendingMessage) +
689 sizeof (struct ClientStartMessage));
690 p->size = sizeof (struct ClientStartMessage);
691 p->is_init = GNUNET_YES;
692 init = (struct ClientStartMessage *) &p[1];
693 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
694 init->header.size = htons (sizeof (struct ClientStartMessage));
695 init->start_flag = htonl (START_FLAG_SCHEDULING);
696 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
703 * delete the current network list
706 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
708 struct ATS_Network * cur = sh->net_head;
711 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
719 interface_proc (void *cls, const char *name,
721 const struct sockaddr *
723 const struct sockaddr *
725 const struct sockaddr *
726 netmask, socklen_t addrlen)
728 struct GNUNET_ATS_SchedulingHandle * sh = cls;
729 /* Calculate network */
730 struct ATS_Network *net = NULL;
732 /* Skipping IPv4 loopback addresses since we have special check */
733 if (addr->sa_family == AF_INET)
735 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
737 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
740 /* Skipping IPv6 loopback addresses since we have special check */
741 if (addr->sa_family == AF_INET6)
743 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
744 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
748 if (addr->sa_family == AF_INET)
750 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
751 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
752 struct sockaddr_in *tmp = NULL;
753 struct sockaddr_in network4;
755 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
756 tmp = (struct sockaddr_in *) &net[1];
757 net->network = (struct sockaddr *) &tmp[0];
758 net->netmask = (struct sockaddr *) &tmp[1];
759 net->length = addrlen;
761 memset (&network4, 0, sizeof (network4));
762 network4.sin_family = AF_INET;
763 #if HAVE_SOCKADDR_IN_SIN_LEN
764 network4.sin_len = sizeof (network4);
766 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
768 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
769 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
772 if (addr->sa_family == AF_INET6)
774 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
775 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
776 struct sockaddr_in6 * tmp = NULL;
777 struct sockaddr_in6 network6;
779 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
780 tmp = (struct sockaddr_in6 *) &net[1];
781 net->network = (struct sockaddr *) &tmp[0];
782 net->netmask = (struct sockaddr *) &tmp[1];
783 net->length = addrlen;
785 memset (&network6, 0, sizeof (network6));
786 network6.sin6_family = AF_INET6;
787 #if HAVE_SOCKADDR_IN_SIN_LEN
788 network6.sin6_len = sizeof (network6);
791 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
792 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
793 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
794 for (c = 0; c < 4; c++)
795 net_elem[c] = addr_elem[c] & mask_elem[c];
797 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
798 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
805 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
807 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
809 GNUNET_free (netmask);
811 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
818 * Periodically get list of addresses
820 * @param tc Task context
823 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct GNUNET_ATS_SchedulingHandle * sh = cls;
826 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
827 delete_networks (sh);
828 GNUNET_OS_network_interfaces_list(interface_proc, sh);
829 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
835 * Convert a GNUNET_ATS_NetworkType to a string
837 * @param net the network type
838 * @return a string or NULL if invalid
841 GNUNET_ATS_print_network_type (uint32_t net)
843 char *networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
844 if (net < GNUNET_ATS_NetworkTypeCount)
845 return networks[net];
851 * Returns where the address is located: LAN or WAN or ...
853 * @param sh the scheduling handle
854 * @param addr address
855 * @param addrlen address length
856 * @return location as GNUNET_ATS_Information
858 struct GNUNET_ATS_Information
859 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
861 GNUNET_assert (sh != NULL);
862 struct ATS_Network * cur = sh->net_head;
864 int type = GNUNET_ATS_NET_UNSPECIFIED;
865 struct GNUNET_ATS_Information ats;
867 if (addr->sa_family == AF_UNIX)
869 type = GNUNET_ATS_NET_LOOPBACK;
872 /* IPv4 loopback check */
873 if (addr->sa_family == AF_INET)
875 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
877 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
878 type = GNUNET_ATS_NET_LOOPBACK;
880 /* IPv6 loopback check */
881 if (addr->sa_family == AF_INET6)
883 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
884 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
885 type = GNUNET_ATS_NET_LOOPBACK;
888 /* Check local networks */
889 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
891 if (addrlen != cur->length)
897 if (addr->sa_family == AF_INET)
899 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
900 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
901 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
903 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
904 type = GNUNET_ATS_NET_LAN;
906 if (addr->sa_family == AF_INET6)
908 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
909 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
910 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
912 int res = GNUNET_YES;
914 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
915 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
916 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
917 for (c = 0; c < 4; c++)
918 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
921 if (res == GNUNET_YES)
922 type = GNUNET_ATS_NET_LAN;
927 /* no local network found for this address, default: WAN */
928 if (type == GNUNET_ATS_NET_UNSPECIFIED)
929 type = GNUNET_ATS_NET_WAN;
930 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
931 ats.value = htonl (type);
933 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
934 "`%s' is in network `%s'\n",
935 GNUNET_a2s ((const struct sockaddr *) addr, addrlen),
936 GNUNET_ATS_print_network_type(type));
942 * Initialize the ATS subsystem.
944 * @param cfg configuration to use
945 * @param suggest_cb notification to call whenever the suggestation changed
946 * @param suggest_cb_cls closure for 'suggest_cb'
947 * @return ats context
949 struct GNUNET_ATS_SchedulingHandle *
950 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
951 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
952 void *suggest_cb_cls)
954 struct GNUNET_ATS_SchedulingHandle *sh;
956 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
958 sh->suggest_cb = suggest_cb;
959 sh->suggest_cb_cls = suggest_cb_cls;
960 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
961 GNUNET_OS_network_interfaces_list(interface_proc, sh);
962 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
971 * Client is done with ATS scheduling, release resources.
973 * @param sh handle to release
976 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
978 struct PendingMessage *p;
979 struct GNUNET_ATS_SuggestHandle *cur;
980 struct GNUNET_ATS_SuggestHandle *next;
981 while (NULL != (p = sh->pending_head))
983 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
986 if (NULL != sh->client)
988 GNUNET_CLIENT_disconnect (sh->client);
991 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
993 GNUNET_SCHEDULER_cancel (sh->task);
994 sh->task = GNUNET_SCHEDULER_NO_TASK;
998 while (NULL != (cur = next))
1001 GNUNET_CONTAINER_DLL_remove (sh->sug_head, sh->sug_tail, cur);
1005 delete_networks (sh);
1006 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
1008 GNUNET_SCHEDULER_cancel(sh->interface_task);
1009 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
1011 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
1017 * We would like to reset the address suggestion block time for this
1021 * @param peer identity of the peer we want to reset
1024 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
1025 const struct GNUNET_PeerIdentity *peer)
1027 struct PendingMessage *p;
1028 struct ResetBackoffMessage *m;
1030 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1031 sizeof (struct ResetBackoffMessage));
1032 p->size = sizeof (struct ResetBackoffMessage);
1033 p->is_init = GNUNET_NO;
1034 m = (struct ResetBackoffMessage *) &p[1];
1035 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
1036 m->header.size = htons (sizeof (struct ResetBackoffMessage));
1037 m->reserved = htonl (0);
1039 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1044 * We would like to establish a new connection with a peer. ATS
1045 * should suggest a good address to begin with.
1048 * @param peer identity of the peer we need an address for
1049 * @return suggest handle
1051 struct GNUNET_ATS_SuggestHandle *
1052 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1053 const struct GNUNET_PeerIdentity *peer)
1055 struct PendingMessage *p;
1056 struct RequestAddressMessage *m;
1057 struct GNUNET_ATS_SuggestHandle *s;
1059 // FIXME: ATS needs to remember this in case of
1061 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1062 sizeof (struct RequestAddressMessage));
1063 p->size = sizeof (struct RequestAddressMessage);
1064 p->is_init = GNUNET_NO;
1065 m = (struct RequestAddressMessage *) &p[1];
1066 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1067 m->header.size = htons (sizeof (struct RequestAddressMessage));
1068 m->reserved = htonl (0);
1070 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1072 s = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestHandle));
1074 GNUNET_CONTAINER_DLL_insert_tail (sh->sug_head, sh->sug_tail, s);
1080 * We would like to stop receiving address updates for this peer
1083 * @param peer identity of the peer
1086 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1087 const struct GNUNET_PeerIdentity *peer)
1089 struct PendingMessage *p;
1090 struct RequestAddressMessage *m;
1091 struct GNUNET_ATS_SuggestHandle *s;
1093 for (s = sh->sug_head; NULL != s; s = s->next)
1094 if (0 == memcmp(peer, &s->id, sizeof (s->id)))
1103 GNUNET_CONTAINER_DLL_remove (sh->sug_head, sh->sug_tail, s);
1107 p = GNUNET_malloc (sizeof (struct PendingMessage) +
1108 sizeof (struct RequestAddressMessage));
1109 p->size = sizeof (struct RequestAddressMessage);
1110 p->is_init = GNUNET_NO;
1111 m = (struct RequestAddressMessage *) &p[1];
1112 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1113 m->header.size = htons (sizeof (struct RequestAddressMessage));
1114 m->reserved = htonl (0);
1116 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1122 * We have a new address ATS should know. Addresses have to be added with this
1123 * function before they can be: updated, set in use and destroyed
1126 * @param address the address
1127 * @param session session handle, can be NULL
1128 * @param ats performance data for the address
1129 * @param ats_count number of performance records in 'ats'
1130 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1133 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1134 const struct GNUNET_HELLO_Address *address,
1135 struct Session *session,
1136 const struct GNUNET_ATS_Information *ats,
1140 struct PendingMessage *p;
1141 struct AddressUpdateMessage *m;
1142 struct GNUNET_ATS_Information *am;
1148 if (address == NULL)
1151 return GNUNET_SYSERR;
1155 (address->transport_name ==
1156 NULL) ? 0 : strlen (address->transport_name) + 1;
1158 sizeof (struct AddressUpdateMessage) + address->address_length +
1159 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1160 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1161 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1162 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1164 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1167 return GNUNET_SYSERR;
1170 if (NULL != session)
1172 s = find_session_id (sh, session, &address->peer);
1175 /* Already existing, nothing todo */
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1178 GNUNET_i2s (&address->peer),
1179 address->transport_name, session, s);
1180 return GNUNET_SYSERR;
1182 s = find_empty_session_slot (sh, session, &address->peer);
1183 GNUNET_break (NOT_FOUND != s);
1186 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1188 p->is_init = GNUNET_NO;
1189 m = (struct AddressUpdateMessage *) &p[1];
1190 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1191 m->header.size = htons (msize);
1192 m->ats_count = htonl (ats_count);
1193 m->peer = address->peer;
1194 m->address_length = htons (address->address_length);
1195 m->plugin_name_length = htons (namelen);
1196 m->session_id = htonl (s);
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1200 GNUNET_i2s (&address->peer),
1201 address->transport_name, session, s);
1203 am = (struct GNUNET_ATS_Information *) &m[1];
1204 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1205 pm = (char *) &am[ats_count];
1206 memcpy (pm, address->address, address->address_length);
1207 if (NULL != address->transport_name)
1208 memcpy (&pm[address->address_length], address->transport_name, namelen);
1209 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1217 * We have updated performance statistics for a given address. Note
1218 * that this function can be called for addresses that are currently
1219 * in use as well as addresses that are valid but not actively in use.
1220 * Furthermore, the peer may not even be connected to us right now (in
1221 * which case the call may be ignored or the information may be stored
1222 * for later use). Update bandwidth assignments.
1225 * @param address the address
1226 * @param session session handle, can be NULL
1227 * @param ats performance data for the address
1228 * @param ats_count number of performance records in 'ats'
1231 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1232 const struct GNUNET_HELLO_Address *address,
1233 struct Session *session,
1234 const struct GNUNET_ATS_Information *ats,
1237 struct PendingMessage *p;
1238 struct AddressUpdateMessage *m;
1239 struct GNUNET_ATS_Information *am;
1245 if (address == NULL)
1252 (address->transport_name ==
1253 NULL) ? 0 : strlen (address->transport_name) + 1;
1255 sizeof (struct AddressUpdateMessage) + address->address_length +
1256 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1257 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1258 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1259 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1261 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1267 if (NULL != session)
1269 s = find_session_id (sh, session, &address->peer);
1274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1275 "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1276 GNUNET_i2s (&address->peer),
1277 address->transport_name, session, s);
1283 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1285 p->is_init = GNUNET_NO;
1286 m = (struct AddressUpdateMessage *) &p[1];
1287 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1288 m->header.size = htons (msize);
1289 m->ats_count = htonl (ats_count);
1290 m->peer = address->peer;
1291 m->address_length = htons (address->address_length);
1292 m->plugin_name_length = htons (namelen);
1294 m->session_id = htonl (s);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1298 GNUNET_i2s (&address->peer),
1299 address->transport_name, session, s);
1301 am = (struct GNUNET_ATS_Information *) &m[1];
1302 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1303 pm = (char *) &am[ats_count];
1304 memcpy (pm, address->address, address->address_length);
1305 memcpy (&pm[address->address_length], address->transport_name, namelen);
1306 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1313 * An address is now in use or not used any more.
1316 * @param address the address
1317 * @param session session handle, can be NULL
1318 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1319 * if address is not used any more
1322 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1323 const struct GNUNET_HELLO_Address *address,
1324 struct Session *session, int in_use)
1326 struct PendingMessage *p;
1327 struct AddressUseMessage *m;
1333 GNUNET_assert (NULL != address);
1335 (address->transport_name ==
1336 NULL) ? 0 : strlen (address->transport_name) + 1;
1337 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1338 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1339 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1340 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1346 if (session != NULL)
1348 s = find_session_id (sh, session, &address->peer);
1349 if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1351 /* trying to set unknown address to NO */
1352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1353 "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1354 GNUNET_i2s (&address->peer), address->transport_name, session);
1358 if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1360 /* trying to set new address to YES */
1361 s = find_empty_session_slot (sh, session, &address->peer);
1362 GNUNET_assert (NOT_FOUND != s);
1366 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1368 p->is_init = GNUNET_NO;
1369 m = (struct AddressUseMessage *) &p[1];
1370 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1371 m->header.size = htons (msize);
1372 m->peer = address->peer;
1373 m->in_use = htons (in_use);
1374 m->address_length = htons (address->address_length);
1375 m->plugin_name_length = htons (namelen);
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1379 (GNUNET_YES == in_use) ? "YES" : "NO",
1380 GNUNET_i2s (&address->peer), address->transport_name, session);
1382 m->session_id = htonl (s);
1383 pm = (char *) &m[1];
1384 memcpy (pm, address->address, address->address_length);
1385 memcpy (&pm[address->address_length], address->transport_name, namelen);
1386 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1393 * A session got destroyed, stop including it as a valid address.
1396 * @param address the address
1397 * @param session session handle that is no longer valid, can be NULL
1400 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1401 const struct GNUNET_HELLO_Address *address,
1402 struct Session *session)
1404 struct PendingMessage *p;
1405 struct AddressDestroyedMessage *m;
1411 if (address == NULL)
1417 GNUNET_assert (address->transport_name != NULL);
1418 namelen = strlen (address->transport_name) + 1;
1419 GNUNET_assert (namelen > 1);
1421 sizeof (struct AddressDestroyedMessage) + address->address_length +
1423 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1424 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1425 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1431 s = find_session_id (sh, session, &address->peer);
1432 if ((NULL != session) && (NOT_FOUND == s))
1434 /* trying to delete unknown address */
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1437 GNUNET_i2s (&address->peer), address->transport_name, session);
1441 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1443 p->is_init = GNUNET_NO;
1444 m = (struct AddressDestroyedMessage *) &p[1];
1445 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1446 m->header.size = htons (msize);
1447 m->reserved = htonl (0);
1448 m->peer = address->peer;
1449 m->address_length = htons (address->address_length);
1450 m->plugin_name_length = htons (namelen);
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 "Deleting address for peer `%s', plugin `%s', session %p\n",
1454 GNUNET_i2s (&address->peer), address->transport_name, session);
1456 m->session_id = htonl (s);
1457 pm = (char *) &m[1];
1458 memcpy (pm, address->address, address->address_length);
1459 memcpy (&pm[address->address_length], address->transport_name, namelen);
1460 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1462 remove_session (sh, s, &address->peer);
1465 /* end of ats_api_scheduling.c */