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"
30 #define DEBUG_ATS GNUNET_EXTRA_LOGGING
32 #define INTERFACE_PROCESSING_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 * Message in linked list we should send to the ATS service. The
36 * actual binary message follows this struct.
44 struct PendingMessage *next;
49 struct PendingMessage *prev;
52 * Size of the message.
57 * Is this the 'ATS_START' message?
64 * Information we track per session.
69 * Identity of the peer (just needed for error checking).
71 struct GNUNET_PeerIdentity peer;
76 struct Session *session;
79 * Set to GNUNET_YES if the slot is used.
87 struct ATS_Network * next;
89 struct ATS_Network * prev;
91 struct sockaddr *network;
92 struct sockaddr *netmask;
99 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
101 struct GNUNET_ATS_SchedulingHandle
107 const struct GNUNET_CONFIGURATION_Handle *cfg;
110 * Callback to invoke on suggestions.
112 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
115 * Closure for 'suggest_cb'.
117 void *suggest_cb_cls;
120 * Connection to ATS service.
122 struct GNUNET_CLIENT_Connection *client;
125 * Head of list of messages for the ATS service.
127 struct PendingMessage *pending_head;
130 * Tail of list of messages for the ATS service
132 struct PendingMessage *pending_tail;
135 * Current request for transmission to ATS.
137 struct GNUNET_CLIENT_TransmitHandle *th;
140 * Head of network list
142 struct ATS_Network * net_head;
145 * Tail of network list
147 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
167 GNUNET_SCHEDULER_TaskIdentifier interface_task;
171 * Size of the session array.
173 unsigned int session_array_size;
176 * Should we reconnect to ATS due to some serious error?
183 * Re-establish the connection to the ATS service.
185 * @param sh handle to use to re-connect.
188 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
192 * Re-establish the connection to the ATS service.
194 * @param cls handle to use to re-connect.
195 * @param tc scheduler context
198 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 struct GNUNET_ATS_SchedulingHandle *sh = cls;
202 sh->task = GNUNET_SCHEDULER_NO_TASK;
208 * Disconnect from ATS and then reconnect.
210 * @param sh our handle
213 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
215 sh->reconnect = GNUNET_NO;
216 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
219 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
225 * Transmit messages from the message queue to the service
226 * (if there are any, and if we are not already trying).
228 * @param sh handle to use
231 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
235 * Type of a function to call when we receive a message
238 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
239 * @param msg message received, NULL on timeout or fatal error
242 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
246 * We can now transmit a message to ATS. Do it.
248 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
249 * @param size number of bytes we can transmit to ATS
250 * @param buf where to copy the messages
251 * @return number of bytes copied into buf
254 transmit_message_to_ats (void *cls, size_t size, void *buf)
256 struct GNUNET_ATS_SchedulingHandle *sh = cls;
257 struct PendingMessage *p;
262 if ((size == 0) || (buf == NULL))
264 force_reconnect (sh);
269 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
271 memcpy (&cbuf[ret], &p[1], p->size);
274 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
275 if (GNUNET_YES == p->is_init)
276 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
277 GNUNET_TIME_UNIT_FOREVER_REL);
286 * Transmit messages from the message queue to the service
287 * (if there are any, and if we are not already trying).
289 * @param sh handle to use
292 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
294 struct PendingMessage *p;
298 if (NULL == (p = sh->pending_head))
300 if (NULL == sh->client)
301 return; /* currently reconnecting */
303 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
304 GNUNET_TIME_UNIT_FOREVER_REL,
305 GNUNET_NO, &transmit_message_to_ats,
311 * Find the session object corresponding to the given session ID.
313 * @param sh our handle
314 * @param session_id current session ID
315 * @param peer peer the session belongs to
316 * @return the session object (or NULL)
318 static struct Session *
319 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
320 const struct GNUNET_PeerIdentity *peer)
323 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
324 "Find session %u from peer %s in %p\n",
325 (unsigned int) session_id, GNUNET_i2s (peer), sh);
327 if (session_id >= sh->session_array_size)
334 if (sh->session_array[session_id].session == NULL)
337 memcmp (peer, &sh->session_array[session_id].peer,
338 sizeof (struct GNUNET_PeerIdentity)));
343 memcmp (peer, &sh->session_array[session_id].peer,
344 sizeof (struct GNUNET_PeerIdentity)))
347 sh->reconnect = GNUNET_YES;
350 return sh->session_array[session_id].session;
355 * Get the ID for the given session object. If we do not have an ID for
356 * the given session object, allocate one.
358 * @param sh our handle
359 * @param session session object
360 * @param peer peer the session belongs to
361 * @return the session id
364 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
365 const struct GNUNET_PeerIdentity *peer)
371 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
372 "Get session ID for session %p from peer %s in %p\n", session,
373 GNUNET_i2s (peer), sh);
378 for (i = 1; i < sh->session_array_size; i++)
380 if (session == sh->session_array[i].session)
383 memcmp (peer, &sh->session_array[i].peer,
384 sizeof (struct GNUNET_PeerIdentity)));
387 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
392 f = sh->session_array_size;
393 GNUNET_array_grow (sh->session_array, sh->session_array_size,
394 sh->session_array_size * 2);
396 GNUNET_assert (f > 0);
397 sh->session_array[f].session = session;
398 sh->session_array[f].peer = *peer;
399 sh->session_array[f].slot_used = GNUNET_YES;
401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
402 "Assigning session ID %u for session %p of peer %s in %p\n", f,
403 session, GNUNET_i2s (peer), sh);
410 * Remove the session of the given session ID from the session
411 * table (it is no longer valid).
413 * @param sh our handle
414 * @param session_id identifies session that is no longer valid
415 * @param peer peer the session belongs to
418 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
419 const struct GNUNET_PeerIdentity *peer)
421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
422 "Remove sessionID %u from peer %s in %p\n", "ats-scheduling-api",
423 (unsigned int) session_id, GNUNET_i2s (peer), sh);
426 GNUNET_assert (session_id < sh->session_array_size);
427 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
429 memcmp (peer, &sh->session_array[session_id].peer,
430 sizeof (struct GNUNET_PeerIdentity)));
431 sh->session_array[session_id].session = NULL;
436 * Release the session slot from the session table (ATS service is
437 * also done using it).
439 * @param sh our handle
440 * @param session_id identifies session that is no longer valid
441 * @param peer peer the session belongs to
444 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
445 const struct GNUNET_PeerIdentity *peer)
448 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
449 "Release sessionID %u from peer %s in %p\n",
450 (unsigned int) session_id, GNUNET_i2s (peer), sh);
452 if (session_id >= sh->session_array_size)
455 sh->reconnect = GNUNET_YES;
459 /* this slot should have been removed from remove_session before */
460 GNUNET_assert (sh->session_array[session_id].session == NULL);
463 memcmp (peer, &sh->session_array[session_id].peer,
464 sizeof (struct GNUNET_PeerIdentity)))
467 sh->reconnect = GNUNET_YES;
470 sh->session_array[session_id].slot_used = GNUNET_NO;
471 memset (&sh->session_array[session_id].peer, 0,
472 sizeof (struct GNUNET_PeerIdentity));
477 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
478 const struct SessionReleaseMessage *srm)
480 release_session (sh, ntohl (srm->session_id), &srm->peer);
485 * Type of a function to call when we receive a message
488 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
489 * @param msg message received, NULL on timeout or fatal error
492 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
494 struct GNUNET_ATS_SchedulingHandle *sh = cls;
495 const struct AddressSuggestionMessage *m;
496 const struct GNUNET_ATS_Information *atsi;
497 const char *plugin_address;
498 const char *plugin_name;
499 uint16_t plugin_address_length;
500 uint16_t plugin_name_length;
502 struct GNUNET_HELLO_Address address;
507 force_reconnect (sh);
510 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
511 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
513 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
514 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
515 GNUNET_TIME_UNIT_FOREVER_REL);
516 if (GNUNET_YES == sh->reconnect)
517 force_reconnect (sh);
520 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
521 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
524 force_reconnect (sh);
527 m = (const struct AddressSuggestionMessage *) msg;
528 ats_count = ntohl (m->ats_count);
529 plugin_address_length = ntohs (m->address_length);
530 atsi = (const struct GNUNET_ATS_Information *) &m[1];
531 plugin_address = (const char *) &atsi[ats_count];
532 plugin_name = &plugin_address[plugin_address_length];
533 plugin_name_length = ntohs (m->plugin_name_length);
534 if ((plugin_address_length + plugin_name_length +
535 ats_count * sizeof (struct GNUNET_ATS_Information) +
536 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
538 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
539 || (plugin_name[plugin_name_length - 1] != '\0'))
542 force_reconnect (sh);
545 uint32_t session_id = ntohl (m->session_id);
551 s = find_session (sh, session_id, &m->peer);
555 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
556 "ATS tries to use outdated session `%s'\n",
557 GNUNET_i2s (&m->peer));
558 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
559 GNUNET_TIME_UNIT_FOREVER_REL);
563 address.peer = m->peer;
564 address.address = plugin_address;
565 address.address_length = plugin_address_length;
566 address.transport_name = plugin_name;
568 if ((s == NULL) && (0 == address.address_length))
570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
571 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
572 GNUNET_i2s (&address.peer), address.transport_name,
573 plugin_address_length, session_id);
575 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
576 GNUNET_TIME_UNIT_FOREVER_REL);
580 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
581 m->bandwidth_in, atsi, ats_count);
583 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
584 GNUNET_TIME_UNIT_FOREVER_REL);
585 if (GNUNET_YES == sh->reconnect)
586 force_reconnect (sh);
591 * Re-establish the connection to the ATS service.
593 * @param sh handle to use to re-connect.
596 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
598 struct PendingMessage *p;
599 struct ClientStartMessage *init;
601 GNUNET_assert (NULL == sh->client);
602 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
603 GNUNET_assert (NULL != sh->client);
604 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
606 p = GNUNET_malloc (sizeof (struct PendingMessage) +
607 sizeof (struct ClientStartMessage));
608 p->size = sizeof (struct ClientStartMessage);
609 p->is_init = GNUNET_YES;
610 init = (struct ClientStartMessage *) &p[1];
611 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
612 init->header.size = htons (sizeof (struct ClientStartMessage));
613 init->start_flag = htonl (START_FLAG_SCHEDULING);
614 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
620 * delete the current network list
624 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
626 struct ATS_Network * cur = sh->net_head;
629 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
637 interface_proc (void *cls, const char *name,
639 const struct sockaddr *
641 const struct sockaddr *
643 const struct sockaddr *
644 netmask, socklen_t addrlen)
646 struct GNUNET_ATS_SchedulingHandle * sh = cls;
647 /* Calculate network */
648 struct ATS_Network *net = NULL;
650 /* Skipping IPv4 loopback addresses since we have special check */
651 if (addr->sa_family == AF_INET)
653 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
655 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
658 /* Skipping IPv6 loopback addresses since we have special check */
659 if (addr->sa_family == AF_INET6)
661 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
662 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
666 if (addr->sa_family == AF_INET)
668 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
669 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
670 struct sockaddr_in *tmp = NULL;
671 struct sockaddr_in network4;
673 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
674 tmp = (struct sockaddr_in *) &net[1];
675 net->network = (struct sockaddr *) &tmp[0];
676 net->netmask = (struct sockaddr *) &tmp[1];
677 net->length = addrlen;
679 memset (&network4, 0, sizeof (network4));
680 network4.sin_family = AF_INET;
681 #if HAVE_SOCKADDR_IN_SIN_LEN
682 network4.sin_len = sizeof (network4);
684 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
686 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
687 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
690 if (addr->sa_family == AF_INET6)
692 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
693 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
694 struct sockaddr_in6 * tmp = NULL;
695 struct sockaddr_in6 network6;
697 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
698 tmp = (struct sockaddr_in6 *) &net[1];
699 net->network = (struct sockaddr *) &tmp[0];
700 net->netmask = (struct sockaddr *) &tmp[1];
701 net->length = addrlen;
703 memset (&network6, 0, sizeof (network6));
704 network6.sin6_family = AF_INET6;
705 #if HAVE_SOCKADDR_IN_SIN_LEN
706 network6.sin6_len = sizeof (network6);
709 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
710 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
711 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
712 for (c = 0; c < 4; c++)
713 net_elem[c] = addr_elem[c] & mask_elem[c];
715 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
716 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
723 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
725 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
727 GNUNET_free (netmask);
729 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
737 * Periodically get list of addresses
739 * @param tc Task context
742 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
744 struct GNUNET_ATS_SchedulingHandle * sh = cls;
745 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
746 delete_networks (sh);
747 GNUNET_OS_network_interfaces_list(interface_proc, sh);
748 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
754 * Returns where the address is located: LAN or WAN or ...
755 * @param sh the scheduling handle
756 * @param addr address
757 * @param addrlen address length
758 * @return location as GNUNET_ATS_Information
761 const struct GNUNET_ATS_Information
762 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
764 GNUNET_assert (sh != NULL);
765 struct GNUNET_ATS_Information ats;
766 struct ATS_Network * cur = sh->net_head;
767 int type = GNUNET_ATS_NET_UNSPECIFIED;
769 if (addr->sa_family == AF_UNIX)
771 type = GNUNET_ATS_NET_LOOPBACK;
774 /* IPv4 loopback check */
775 if (addr->sa_family == AF_INET)
777 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
779 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
780 type = GNUNET_ATS_NET_LOOPBACK;
782 /* IPv6 loopback check */
783 if (addr->sa_family == AF_INET6)
785 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
786 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
787 type = GNUNET_ATS_NET_LOOPBACK;
790 /* Check local networks */
791 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
793 if (addrlen != cur->length)
799 if (addr->sa_family == AF_INET)
801 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
802 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
803 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
805 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
807 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
809 "`%s' is in network `%s'\n",
810 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
813 type = GNUNET_ATS_NET_LAN;
816 if (addr->sa_family == AF_INET6)
818 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
819 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
820 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
822 int res = GNUNET_YES;
824 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
825 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
826 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
827 for (c = 0; c < 4; c++)
828 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
831 if (res == GNUNET_YES)
833 char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
835 GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
838 type = GNUNET_ATS_NET_LAN;
844 /* no local network found for this address, default: WAN */
845 if (type == GNUNET_ATS_NET_UNSPECIFIED)
846 type = GNUNET_ATS_NET_WAN;
851 case GNUNET_ATS_NET_WAN:
854 case GNUNET_ATS_NET_LAN:
857 case GNUNET_ATS_NET_LOOPBACK:
866 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
867 ats.value = htonl (type);
868 return (const struct GNUNET_ATS_Information) ats;
872 * Initialize the ATS subsystem.
874 * @param cfg configuration to use
875 * @param suggest_cb notification to call whenever the suggestation changed
876 * @param suggest_cb_cls closure for 'suggest_cb'
877 * @return ats context
879 struct GNUNET_ATS_SchedulingHandle *
880 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
881 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
882 void *suggest_cb_cls)
884 struct GNUNET_ATS_SchedulingHandle *sh;
886 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
888 sh->suggest_cb = suggest_cb;
889 sh->suggest_cb_cls = suggest_cb_cls;
890 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
891 GNUNET_OS_network_interfaces_list(interface_proc, sh);
892 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
901 * Client is done with ATS scheduling, release resources.
903 * @param sh handle to release
906 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
908 struct PendingMessage *p;
910 while (NULL != (p = sh->pending_head))
912 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
915 if (NULL != sh->client)
917 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
920 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
922 GNUNET_SCHEDULER_cancel (sh->task);
923 sh->task = GNUNET_SCHEDULER_NO_TASK;
926 delete_networks (sh);
927 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
929 GNUNET_SCHEDULER_cancel(sh->interface_task);
930 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
932 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
939 * We would like to establish a new connection with a peer. ATS
940 * should suggest a good address to begin with.
943 * @param peer identity of the peer we need an address for
946 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
947 const struct GNUNET_PeerIdentity *peer)
949 struct PendingMessage *p;
950 struct RequestAddressMessage *m;
952 p = GNUNET_malloc (sizeof (struct PendingMessage) +
953 sizeof (struct RequestAddressMessage));
954 p->size = sizeof (struct RequestAddressMessage);
955 p->is_init = GNUNET_NO;
956 m = (struct RequestAddressMessage *) &p[1];
957 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
958 m->header.size = htons (sizeof (struct RequestAddressMessage));
959 m->reserved = htonl (0);
961 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
967 * We would like to stop receiving address updates for this peer
970 * @param peer identity of the peer
973 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
974 const struct GNUNET_PeerIdentity *peer)
976 struct PendingMessage *p;
977 struct RequestAddressMessage *m;
979 p = GNUNET_malloc (sizeof (struct PendingMessage) +
980 sizeof (struct RequestAddressMessage));
981 p->size = sizeof (struct RequestAddressMessage);
982 p->is_init = GNUNET_NO;
983 m = (struct RequestAddressMessage *) &p[1];
984 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
985 m->header.size = htons (sizeof (struct RequestAddressMessage));
986 m->reserved = htonl (0);
988 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
993 * We have updated performance statistics for a given address. Note
994 * that this function can be called for addresses that are currently
995 * in use as well as addresses that are valid but not actively in use.
996 * Furthermore, the peer may not even be connected to us right now (in
997 * which case the call may be ignored or the information may be stored
998 * for later use). Update bandwidth assignments.
1001 * @param address the address
1002 * @param session session handle (if available)
1003 * @param ats performance data for the address
1004 * @param ats_count number of performance records in 'ats'
1007 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1008 const struct GNUNET_HELLO_Address *address,
1009 struct Session *session,
1010 const struct GNUNET_ATS_Information *ats,
1013 struct PendingMessage *p;
1014 struct AddressUpdateMessage *m;
1015 struct GNUNET_ATS_Information *am;
1020 if (address == NULL)
1025 if ((address == NULL) && (session == NULL))
1032 (address->transport_name ==
1033 NULL) ? 0 : strlen (address->transport_name) + 1;
1035 sizeof (struct AddressUpdateMessage) + address->address_length +
1036 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1037 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1038 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1039 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1041 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1047 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1049 p->is_init = GNUNET_NO;
1050 m = (struct AddressUpdateMessage *) &p[1];
1051 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1052 m->header.size = htons (msize);
1053 m->ats_count = htonl (ats_count);
1054 m->peer = address->peer;
1055 m->address_length = htons (address->address_length);
1056 m->plugin_name_length = htons (namelen);
1057 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1058 am = (struct GNUNET_ATS_Information *) &m[1];
1059 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1060 pm = (char *) &am[ats_count];
1061 memcpy (pm, address->address, address->address_length);
1062 memcpy (&pm[address->address_length], address->transport_name, namelen);
1063 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1069 * An address is now in use or not used any more.
1072 * @param address the address
1073 * @param session session handle
1074 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1075 * if address is not used any more
1078 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1079 const struct GNUNET_HELLO_Address *address,
1080 struct Session *session, int in_use)
1082 struct PendingMessage *p;
1083 struct AddressUseMessage *m;
1088 GNUNET_assert (NULL != address);
1090 (address->transport_name ==
1091 NULL) ? 0 : strlen (address->transport_name) + 1;
1092 msize = sizeof (struct AddressUseMessage) + address->address_length + 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))
1101 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1103 p->is_init = GNUNET_NO;
1104 m = (struct AddressUseMessage *) &p[1];
1105 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1106 m->header.size = htons (msize);
1107 m->peer = address->peer;
1108 m->in_use = htons (in_use);
1109 m->address_length = htons (address->address_length);
1110 m->plugin_name_length = htons (namelen);
1111 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1112 pm = (char *) &m[1];
1113 memcpy (pm, address->address, address->address_length);
1114 memcpy (&pm[address->address_length], address->transport_name, namelen);
1115 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1121 * A session got destroyed, stop including it as a valid address.
1124 * @param address the address
1125 * @param session session handle that is no longer valid
1128 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1129 const struct GNUNET_HELLO_Address *address,
1130 struct Session *session)
1132 struct PendingMessage *p;
1133 struct AddressDestroyedMessage *m;
1137 uint32_t session_id;
1139 GNUNET_assert (address->transport_name != NULL);
1140 namelen = strlen (address->transport_name) + 1;
1141 GNUNET_assert (namelen > 1);
1143 sizeof (struct AddressDestroyedMessage) + address->address_length +
1145 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1146 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1147 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1153 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1155 p->is_init = GNUNET_NO;
1156 m = (struct AddressDestroyedMessage *) &p[1];
1157 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1158 m->header.size = htons (msize);
1159 m->reserved = htonl (0);
1160 m->peer = address->peer;
1161 m->address_length = htons (address->address_length);
1162 m->plugin_name_length = htons (namelen);
1163 session_id = get_session_id (sh, session, &address->peer);
1164 m->session_id = htonl (session_id);
1165 pm = (char *) &m[1];
1166 memcpy (pm, address->address, address->address_length);
1167 memcpy (&pm[address->address_length], address->transport_name, namelen);
1168 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1170 remove_session (sh, session_id, &address->peer);
1173 /* end of ats_api_scheduling.c */