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 (GNUNET_ERROR_TYPE_DEBUG, "Find session %u from peer %s in %p\n",
324 (unsigned int) session_id, GNUNET_i2s (peer), sh);
326 if (session_id >= sh->session_array_size)
333 if (sh->session_array[session_id].session == NULL)
336 memcmp (peer, &sh->session_array[session_id].peer,
337 sizeof (struct GNUNET_PeerIdentity)));
342 memcmp (peer, &sh->session_array[session_id].peer,
343 sizeof (struct GNUNET_PeerIdentity)))
346 sh->reconnect = GNUNET_YES;
349 return sh->session_array[session_id].session;
354 * Get the ID for the given session object. If we do not have an ID for
355 * the given session object, allocate one.
357 * @param sh our handle
358 * @param session session object
359 * @param peer peer the session belongs to
360 * @return the session id
363 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
364 const struct GNUNET_PeerIdentity *peer)
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Get session ID for session %p from peer %s in %p\n", session,
372 GNUNET_i2s (peer), sh);
377 for (i = 1; i < sh->session_array_size; i++)
379 if (session == sh->session_array[i].session)
382 memcmp (peer, &sh->session_array[i].peer,
383 sizeof (struct GNUNET_PeerIdentity)));
386 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
391 f = sh->session_array_size;
392 GNUNET_array_grow (sh->session_array, sh->session_array_size,
393 sh->session_array_size * 2);
395 GNUNET_assert (f > 0);
396 sh->session_array[f].session = session;
397 sh->session_array[f].peer = *peer;
398 sh->session_array[f].slot_used = GNUNET_YES;
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Assigning session ID %u for session %p of peer %s in %p\n", f,
402 session, GNUNET_i2s (peer), sh);
409 * Remove the session of the given session ID from the session
410 * table (it is no longer valid).
412 * @param sh our handle
413 * @param session_id identifies session that is no longer valid
414 * @param peer peer the session belongs to
417 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
418 const struct GNUNET_PeerIdentity *peer)
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Remove sessionID %u from peer %s in %p\n",
423 (unsigned int) session_id, GNUNET_i2s (peer), sh);
427 GNUNET_assert (session_id < sh->session_array_size);
428 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
430 memcmp (peer, &sh->session_array[session_id].peer,
431 sizeof (struct GNUNET_PeerIdentity)));
432 sh->session_array[session_id].session = NULL;
437 * Release the session slot from the session table (ATS service is
438 * also done using it).
440 * @param sh our handle
441 * @param session_id identifies session that is no longer valid
442 * @param peer peer the session belongs to
445 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
446 const struct GNUNET_PeerIdentity *peer)
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Release sessionID %u from peer %s in %p\n",
451 (unsigned int) session_id, GNUNET_i2s (peer), sh);
453 if (session_id >= sh->session_array_size)
456 sh->reconnect = GNUNET_YES;
460 /* this slot should have been removed from remove_session before */
461 GNUNET_assert (sh->session_array[session_id].session == NULL);
464 memcmp (peer, &sh->session_array[session_id].peer,
465 sizeof (struct GNUNET_PeerIdentity)))
468 sh->reconnect = GNUNET_YES;
471 sh->session_array[session_id].slot_used = GNUNET_NO;
472 memset (&sh->session_array[session_id].peer, 0,
473 sizeof (struct GNUNET_PeerIdentity));
478 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
479 const struct SessionReleaseMessage *srm)
481 release_session (sh, ntohl (srm->session_id), &srm->peer);
486 * Type of a function to call when we receive a message
489 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
490 * @param msg message received, NULL on timeout or fatal error
493 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
495 struct GNUNET_ATS_SchedulingHandle *sh = cls;
496 const struct AddressSuggestionMessage *m;
497 const struct GNUNET_ATS_Information *atsi;
498 const char *plugin_address;
499 const char *plugin_name;
500 uint16_t plugin_address_length;
501 uint16_t plugin_name_length;
503 struct GNUNET_HELLO_Address address;
508 force_reconnect (sh);
511 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
512 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
514 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
515 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
516 GNUNET_TIME_UNIT_FOREVER_REL);
517 if (GNUNET_YES == sh->reconnect)
518 force_reconnect (sh);
521 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
522 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
525 force_reconnect (sh);
528 m = (const struct AddressSuggestionMessage *) msg;
529 ats_count = ntohl (m->ats_count);
530 plugin_address_length = ntohs (m->address_length);
531 atsi = (const struct GNUNET_ATS_Information *) &m[1];
532 plugin_address = (const char *) &atsi[ats_count];
533 plugin_name = &plugin_address[plugin_address_length];
534 plugin_name_length = ntohs (m->plugin_name_length);
535 if ((plugin_address_length + plugin_name_length +
536 ats_count * sizeof (struct GNUNET_ATS_Information) +
537 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
539 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
540 || (plugin_name[plugin_name_length - 1] != '\0'))
543 force_reconnect (sh);
546 uint32_t session_id = ntohl (m->session_id);
552 s = find_session (sh, session_id, &m->peer);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "ATS tries to use outdated session `%s'\n",
558 GNUNET_i2s (&m->peer));
560 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
561 GNUNET_TIME_UNIT_FOREVER_REL);
565 address.peer = m->peer;
566 address.address = plugin_address;
567 address.address_length = plugin_address_length;
568 address.transport_name = plugin_name;
570 if ((s == NULL) && (0 == address.address_length))
572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
573 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
574 GNUNET_i2s (&address.peer), address.transport_name,
575 plugin_address_length, session_id);
577 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
578 GNUNET_TIME_UNIT_FOREVER_REL);
582 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
583 m->bandwidth_in, atsi, ats_count);
585 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
586 GNUNET_TIME_UNIT_FOREVER_REL);
587 if (GNUNET_YES == sh->reconnect)
588 force_reconnect (sh);
593 * Re-establish the connection to the ATS service.
595 * @param sh handle to use to re-connect.
598 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
600 struct PendingMessage *p;
601 struct ClientStartMessage *init;
603 GNUNET_assert (NULL == sh->client);
604 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
605 GNUNET_assert (NULL != sh->client);
606 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
608 p = GNUNET_malloc (sizeof (struct PendingMessage) +
609 sizeof (struct ClientStartMessage));
610 p->size = sizeof (struct ClientStartMessage);
611 p->is_init = GNUNET_YES;
612 init = (struct ClientStartMessage *) &p[1];
613 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
614 init->header.size = htons (sizeof (struct ClientStartMessage));
615 init->start_flag = htonl (START_FLAG_SCHEDULING);
616 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
622 * delete the current network list
626 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
628 struct ATS_Network * cur = sh->net_head;
631 GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
639 interface_proc (void *cls, const char *name,
641 const struct sockaddr *
643 const struct sockaddr *
645 const struct sockaddr *
646 netmask, socklen_t addrlen)
648 struct GNUNET_ATS_SchedulingHandle * sh = cls;
649 /* Calculate network */
650 struct ATS_Network *net = NULL;
652 /* Skipping IPv4 loopback addresses since we have special check */
653 if (addr->sa_family == AF_INET)
655 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
657 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
660 /* Skipping IPv6 loopback addresses since we have special check */
661 if (addr->sa_family == AF_INET6)
663 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
664 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
668 if (addr->sa_family == AF_INET)
670 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
671 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
672 struct sockaddr_in *tmp = NULL;
673 struct sockaddr_in network4;
675 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
676 tmp = (struct sockaddr_in *) &net[1];
677 net->network = (struct sockaddr *) &tmp[0];
678 net->netmask = (struct sockaddr *) &tmp[1];
679 net->length = addrlen;
681 network4.sin_family = AF_INET;
682 network4.sin_port = htons (0);
683 #if HAVE_SOCKADDR_IN_SIN_LEN
684 network4.sin_len = sizeof (network4);
686 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
688 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
689 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
692 if (addr->sa_family == AF_INET6)
694 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
695 struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
696 struct sockaddr_in6 * tmp = NULL;
697 struct sockaddr_in6 network6;
699 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
700 tmp = (struct sockaddr_in6 *) &net[1];
701 net->network = (struct sockaddr *) &tmp[0];
702 net->netmask = (struct sockaddr *) &tmp[1];
703 net->length = addrlen;
705 network6.sin6_family = AF_INET6;
706 network6.sin6_port = htons (0);
707 #if HAVE_SOCKADDR_IN_SIN_LEN
708 network6.sin6_len = sizeof (network6);
711 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
712 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
713 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
714 for (c = 0; c < 4; c++)
715 net_elem[c] = addr_elem[c] & mask_elem[c];
717 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
718 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
724 char * netmask = strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
726 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
728 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 addr address
756 * @param addrlen address length
757 * @return location as GNUNET_ATS_Information
760 struct GNUNET_ATS_Information
761 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
763 GNUNET_assert (sh != NULL);
764 struct GNUNET_ATS_Information ats;
765 struct ATS_Network * cur = sh->net_head;
766 int type = GNUNET_ATS_NET_UNSPECIFIED;
768 if (addr->sa_family == AF_UNIX)
770 type = GNUNET_ATS_NET_LOOPBACK;
773 /* IPv4 loopback check */
774 if (addr->sa_family == AF_INET)
776 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
778 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
779 type = GNUNET_ATS_NET_LOOPBACK;
781 /* IPv6 loopback check */
782 if (addr->sa_family == AF_INET6)
784 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
785 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
786 type = GNUNET_ATS_NET_LOOPBACK;
789 /* Check local networks */
790 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
792 if (addrlen != cur->length)
798 if (addr->sa_family == AF_INET)
800 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
801 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
802 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
804 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
806 char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
808 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
811 type = GNUNET_ATS_NET_LAN;
814 if (addr->sa_family == AF_INET6)
816 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
817 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
818 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
820 int res = GNUNET_YES;
822 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
823 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
824 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
825 for (c = 0; c < 4; c++)
826 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
829 if (res == GNUNET_YES)
831 char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
833 GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
836 type = GNUNET_ATS_NET_LAN;
842 /* no local network found for this address, default: WAN */
843 if (type == GNUNET_ATS_NET_UNSPECIFIED)
844 type = GNUNET_ATS_NET_WAN;
849 case GNUNET_ATS_NET_WAN:
852 case GNUNET_ATS_NET_LAN:
855 case GNUNET_ATS_NET_LOOPBACK:
862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "`%s' is in network `%s'\n",
863 GNUNET_a2s ((const struct sockaddr *) addr, addrlen),
867 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
868 ats.value = htonl (type);
873 * Initialize the ATS subsystem.
875 * @param cfg configuration to use
876 * @param suggest_cb notification to call whenever the suggestation changed
877 * @param suggest_cb_cls closure for 'suggest_cb'
878 * @return ats context
880 struct GNUNET_ATS_SchedulingHandle *
881 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
882 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
883 void *suggest_cb_cls)
885 struct GNUNET_ATS_SchedulingHandle *sh;
887 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
889 sh->suggest_cb = suggest_cb;
890 sh->suggest_cb_cls = suggest_cb_cls;
891 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
892 GNUNET_OS_network_interfaces_list(interface_proc, sh);
893 sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
902 * Client is done with ATS scheduling, release resources.
904 * @param sh handle to release
907 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
909 struct PendingMessage *p;
911 while (NULL != (p = sh->pending_head))
913 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
916 if (NULL != sh->client)
918 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
921 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
923 GNUNET_SCHEDULER_cancel (sh->task);
924 sh->task = GNUNET_SCHEDULER_NO_TASK;
927 delete_networks (sh);
928 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
930 GNUNET_SCHEDULER_cancel(sh->interface_task);
931 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
933 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
940 * We would like to establish a new connection with a peer. ATS
941 * should suggest a good address to begin with.
944 * @param peer identity of the peer we need an address for
947 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
948 const struct GNUNET_PeerIdentity *peer)
950 struct PendingMessage *p;
951 struct RequestAddressMessage *m;
953 p = GNUNET_malloc (sizeof (struct PendingMessage) +
954 sizeof (struct RequestAddressMessage));
955 p->size = sizeof (struct RequestAddressMessage);
956 p->is_init = GNUNET_NO;
957 m = (struct RequestAddressMessage *) &p[1];
958 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
959 m->header.size = htons (sizeof (struct RequestAddressMessage));
960 m->reserved = htonl (0);
962 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
968 * We would like to stop receiving address updates for this peer
971 * @param peer identity of the peer
974 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
975 const struct GNUNET_PeerIdentity *peer)
977 struct PendingMessage *p;
978 struct RequestAddressMessage *m;
980 p = GNUNET_malloc (sizeof (struct PendingMessage) +
981 sizeof (struct RequestAddressMessage));
982 p->size = sizeof (struct RequestAddressMessage);
983 p->is_init = GNUNET_NO;
984 m = (struct RequestAddressMessage *) &p[1];
985 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
986 m->header.size = htons (sizeof (struct RequestAddressMessage));
987 m->reserved = htonl (0);
989 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
994 * We have updated performance statistics for a given address. Note
995 * that this function can be called for addresses that are currently
996 * in use as well as addresses that are valid but not actively in use.
997 * Furthermore, the peer may not even be connected to us right now (in
998 * which case the call may be ignored or the information may be stored
999 * for later use). Update bandwidth assignments.
1002 * @param address the address
1003 * @param session session handle (if available)
1004 * @param ats performance data for the address
1005 * @param ats_count number of performance records in 'ats'
1008 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1009 const struct GNUNET_HELLO_Address *address,
1010 struct Session *session,
1011 const struct GNUNET_ATS_Information *ats,
1014 struct PendingMessage *p;
1015 struct AddressUpdateMessage *m;
1016 struct GNUNET_ATS_Information *am;
1022 (address->transport_name ==
1023 NULL) ? 0 : strlen (address->transport_name) + 1;
1025 sizeof (struct AddressUpdateMessage) + address->address_length +
1026 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1027 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1028 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1029 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1031 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1037 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1039 p->is_init = GNUNET_NO;
1040 m = (struct AddressUpdateMessage *) &p[1];
1041 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1042 m->header.size = htons (msize);
1043 m->ats_count = htonl (ats_count);
1044 m->peer = address->peer;
1045 m->address_length = htons (address->address_length);
1046 m->plugin_name_length = htons (namelen);
1047 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1048 am = (struct GNUNET_ATS_Information *) &m[1];
1049 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1050 pm = (char *) &am[ats_count];
1051 memcpy (pm, address->address, address->address_length);
1052 memcpy (&pm[address->address_length], address->transport_name, namelen);
1053 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1059 * An address is now in use or not used any more.
1062 * @param address the address
1063 * @param session session handle
1064 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1065 * if address is not used any more
1068 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1069 const struct GNUNET_HELLO_Address *address,
1070 struct Session *session, int in_use)
1072 struct PendingMessage *p;
1073 struct AddressUseMessage *m;
1078 GNUNET_assert (NULL != address);
1080 (address->transport_name ==
1081 NULL) ? 0 : strlen (address->transport_name) + 1;
1082 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1083 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1084 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1085 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1091 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1093 p->is_init = GNUNET_NO;
1094 m = (struct AddressUseMessage *) &p[1];
1095 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1096 m->header.size = htons (msize);
1097 m->peer = address->peer;
1098 m->in_use = htons (in_use);
1099 m->address_length = htons (address->address_length);
1100 m->plugin_name_length = htons (namelen);
1101 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1102 pm = (char *) &m[1];
1103 memcpy (pm, address->address, address->address_length);
1104 memcpy (&pm[address->address_length], address->transport_name, namelen);
1105 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1111 * A session got destroyed, stop including it as a valid address.
1114 * @param address the address
1115 * @param session session handle that is no longer valid
1118 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1119 const struct GNUNET_HELLO_Address *address,
1120 struct Session *session)
1122 struct PendingMessage *p;
1123 struct AddressDestroyedMessage *m;
1127 uint32_t session_id;
1129 GNUNET_assert (address->transport_name != NULL);
1130 namelen = strlen (address->transport_name) + 1;
1131 GNUNET_assert (namelen > 1);
1133 sizeof (struct AddressDestroyedMessage) + address->address_length +
1135 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1136 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1137 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1143 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1145 p->is_init = GNUNET_NO;
1146 m = (struct AddressDestroyedMessage *) &p[1];
1147 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1148 m->header.size = htons (msize);
1149 m->reserved = htonl (0);
1150 m->peer = address->peer;
1151 m->address_length = htons (address->address_length);
1152 m->plugin_name_length = htons (namelen);
1153 session_id = get_session_id (sh, session, &address->peer);
1154 m->session_id = htonl (session_id);
1155 pm = (char *) &m[1];
1156 memcpy (pm, address->address, address->address_length);
1157 memcpy (&pm[address->address_length], address->transport_name, namelen);
1158 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1160 remove_session (sh, session_id, &address->peer);
1163 /* end of ats_api_scheduling.c */