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
33 * Message in linked list we should send to the ATS service. The
34 * actual binary message follows this struct.
42 struct PendingMessage *next;
47 struct PendingMessage *prev;
50 * Size of the message.
55 * Is this the 'ATS_START' message?
62 * Information we track per session.
67 * Identity of the peer (just needed for error checking).
69 struct GNUNET_PeerIdentity peer;
74 struct Session *session;
77 * Set to GNUNET_YES if the slot is used.
85 struct ATS_Network * next;
87 struct ATS_Network * prev;
89 struct sockaddr *network;
90 struct sockaddr *netmask;
97 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
99 struct GNUNET_ATS_SchedulingHandle
105 const struct GNUNET_CONFIGURATION_Handle *cfg;
108 * Callback to invoke on suggestions.
110 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
113 * Closure for 'suggest_cb'.
115 void *suggest_cb_cls;
118 * Connection to ATS service.
120 struct GNUNET_CLIENT_Connection *client;
123 * Head of list of messages for the ATS service.
125 struct PendingMessage *pending_head;
128 * Tail of list of messages for the ATS service
130 struct PendingMessage *pending_tail;
133 * Current request for transmission to ATS.
135 struct GNUNET_CLIENT_TransmitHandle *th;
138 * Head of network list
140 struct ATS_Network * net_head;
143 * Tail of network list
145 struct ATS_Network * net_tail;
149 * Array of session objects (we need to translate them to numbers and back
150 * for the protocol; the offset in the array is the session number on the
151 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
152 * Unused entries are also NULL.
154 struct SessionRecord *session_array;
157 * Task to trigger reconnect.
159 GNUNET_SCHEDULER_TaskIdentifier task;
162 * Task retrieving interfaces from the system
165 GNUNET_SCHEDULER_TaskIdentifier interface_task;
169 * Size of the session array.
171 unsigned int session_array_size;
174 * Should we reconnect to ATS due to some serious error?
181 * Re-establish the connection to the ATS service.
183 * @param sh handle to use to re-connect.
186 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
190 * Re-establish the connection to the ATS service.
192 * @param cls handle to use to re-connect.
193 * @param tc scheduler context
196 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 struct GNUNET_ATS_SchedulingHandle *sh = cls;
200 sh->task = GNUNET_SCHEDULER_NO_TASK;
206 * Disconnect from ATS and then reconnect.
208 * @param sh our handle
211 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
213 sh->reconnect = GNUNET_NO;
214 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
217 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
223 * Transmit messages from the message queue to the service
224 * (if there are any, and if we are not already trying).
226 * @param sh handle to use
229 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
233 * Type of a function to call when we receive a message
236 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
237 * @param msg message received, NULL on timeout or fatal error
240 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
244 * We can now transmit a message to ATS. Do it.
246 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
247 * @param size number of bytes we can transmit to ATS
248 * @param buf where to copy the messages
249 * @return number of bytes copied into buf
252 transmit_message_to_ats (void *cls, size_t size, void *buf)
254 struct GNUNET_ATS_SchedulingHandle *sh = cls;
255 struct PendingMessage *p;
260 if ((size == 0) || (buf == NULL))
262 force_reconnect (sh);
267 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
269 memcpy (&cbuf[ret], &p[1], p->size);
272 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
273 if (GNUNET_YES == p->is_init)
274 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
275 GNUNET_TIME_UNIT_FOREVER_REL);
284 * Transmit messages from the message queue to the service
285 * (if there are any, and if we are not already trying).
287 * @param sh handle to use
290 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
292 struct PendingMessage *p;
296 if (NULL == (p = sh->pending_head))
298 if (NULL == sh->client)
299 return; /* currently reconnecting */
301 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
302 GNUNET_TIME_UNIT_FOREVER_REL,
303 GNUNET_NO, &transmit_message_to_ats,
309 * Find the session object corresponding to the given session ID.
311 * @param sh our handle
312 * @param session_id current session ID
313 * @param peer peer the session belongs to
314 * @return the session object (or NULL)
316 static struct Session *
317 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
318 const struct GNUNET_PeerIdentity *peer)
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Find session %u from peer %s in %p\n",
322 (unsigned int) session_id, GNUNET_i2s (peer), sh);
324 if (session_id >= sh->session_array_size)
331 if (sh->session_array[session_id].session == NULL)
334 memcmp (peer, &sh->session_array[session_id].peer,
335 sizeof (struct GNUNET_PeerIdentity)));
340 memcmp (peer, &sh->session_array[session_id].peer,
341 sizeof (struct GNUNET_PeerIdentity)))
344 sh->reconnect = GNUNET_YES;
347 return sh->session_array[session_id].session;
352 * Get the ID for the given session object. If we do not have an ID for
353 * the given session object, allocate one.
355 * @param sh our handle
356 * @param session session object
357 * @param peer peer the session belongs to
358 * @return the session id
361 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
362 const struct GNUNET_PeerIdentity *peer)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Get session ID for session %p from peer %s in %p\n", session,
370 GNUNET_i2s (peer), sh);
375 for (i = 1; i < sh->session_array_size; i++)
377 if (session == sh->session_array[i].session)
380 memcmp (peer, &sh->session_array[i].peer,
381 sizeof (struct GNUNET_PeerIdentity)));
384 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
389 f = sh->session_array_size;
390 GNUNET_array_grow (sh->session_array, sh->session_array_size,
391 sh->session_array_size * 2);
393 GNUNET_assert (f > 0);
394 sh->session_array[f].session = session;
395 sh->session_array[f].peer = *peer;
396 sh->session_array[f].slot_used = GNUNET_YES;
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Assigning session ID %u for session %p of peer %s in %p\n", f,
400 session, GNUNET_i2s (peer), sh);
407 * Remove the session of the given session ID from the session
408 * table (it is no longer valid).
410 * @param sh our handle
411 * @param session_id identifies session that is no longer valid
412 * @param peer peer the session belongs to
415 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
416 const struct GNUNET_PeerIdentity *peer)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Remove sessionID %u from peer %s in %p\n",
421 (unsigned int) session_id, GNUNET_i2s (peer), sh);
425 GNUNET_assert (session_id < sh->session_array_size);
426 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
428 memcmp (peer, &sh->session_array[session_id].peer,
429 sizeof (struct GNUNET_PeerIdentity)));
430 sh->session_array[session_id].session = NULL;
435 * Release the session slot from the session table (ATS service is
436 * also done using it).
438 * @param sh our handle
439 * @param session_id identifies session that is no longer valid
440 * @param peer peer the session belongs to
443 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
444 const struct GNUNET_PeerIdentity *peer)
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Release sessionID %u from peer %s in %p\n",
449 (unsigned int) session_id, GNUNET_i2s (peer), sh);
451 if (session_id >= sh->session_array_size)
454 sh->reconnect = GNUNET_YES;
458 /* this slot should have been removed from remove_session before */
459 GNUNET_assert (sh->session_array[session_id].session == NULL);
462 memcmp (peer, &sh->session_array[session_id].peer,
463 sizeof (struct GNUNET_PeerIdentity)))
466 sh->reconnect = GNUNET_YES;
469 sh->session_array[session_id].slot_used = GNUNET_NO;
470 memset (&sh->session_array[session_id].peer, 0,
471 sizeof (struct GNUNET_PeerIdentity));
476 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
477 const struct SessionReleaseMessage *srm)
479 release_session (sh, ntohl (srm->session_id), &srm->peer);
484 * Type of a function to call when we receive a message
487 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
488 * @param msg message received, NULL on timeout or fatal error
491 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
493 struct GNUNET_ATS_SchedulingHandle *sh = cls;
494 const struct AddressSuggestionMessage *m;
495 const struct GNUNET_ATS_Information *atsi;
496 const char *plugin_address;
497 const char *plugin_name;
498 uint16_t plugin_address_length;
499 uint16_t plugin_name_length;
501 struct GNUNET_HELLO_Address address;
506 force_reconnect (sh);
509 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
510 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
512 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
513 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
514 GNUNET_TIME_UNIT_FOREVER_REL);
515 if (GNUNET_YES == sh->reconnect)
516 force_reconnect (sh);
519 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
520 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
523 force_reconnect (sh);
526 m = (const struct AddressSuggestionMessage *) msg;
527 ats_count = ntohl (m->ats_count);
528 plugin_address_length = ntohs (m->address_length);
529 atsi = (const struct GNUNET_ATS_Information *) &m[1];
530 plugin_address = (const char *) &atsi[ats_count];
531 plugin_name = &plugin_address[plugin_address_length];
532 plugin_name_length = ntohs (m->plugin_name_length);
533 if ((plugin_address_length + plugin_name_length +
534 ats_count * sizeof (struct GNUNET_ATS_Information) +
535 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
537 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
538 || (plugin_name[plugin_name_length - 1] != '\0'))
541 force_reconnect (sh);
544 uint32_t session_id = ntohl (m->session_id);
550 s = find_session (sh, session_id, &m->peer);
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "ATS tries to use outdated session `%s'\n",
556 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 network4.sin_family = AF_INET;
680 network4.sin_port = htons (0);
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 network6.sin6_family = AF_INET6;
704 network6.sin6_port = htons (0);
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));
722 char * netmask = strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
724 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
726 GNUNET_free (netmask);
727 GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
735 * Periodically get list of addresses
737 * @param tc Task context
740 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
742 struct GNUNET_ATS_SchedulingHandle * sh = cls;
743 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
744 delete_networks (sh);
745 GNUNET_OS_network_interfaces_list(interface_proc, sh);
747 sh->interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, sh);
751 * Returns where the address is located: LAN or WAN or ...
752 * @param addr address
753 * @param addrlen address length
754 * @return location as GNUNET_ATS_Information
757 struct GNUNET_ATS_Information
758 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
760 struct GNUNET_ATS_Information ats;
761 struct ATS_Network * cur = sh->net_head;
762 int type = GNUNET_ATS_NET_UNSPECIFIED;
764 /* IPv4 loopback check */
765 if (addr->sa_family == AF_INET)
767 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
769 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
770 type = GNUNET_ATS_NET_LOOPBACK;
772 /* IPv6 loopback check */
773 if (addr->sa_family == AF_INET6)
775 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
776 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
777 type = GNUNET_ATS_NET_LOOPBACK;
780 /* Check local networks */
781 while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
783 if (addrlen != cur->length)
789 if (addr->sa_family == AF_INET)
791 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
792 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
793 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
795 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
797 char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
799 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
802 type = GNUNET_ATS_NET_LAN;
805 if (addr->sa_family == AF_INET6)
807 struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
808 struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
809 struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
811 int res = GNUNET_YES;
813 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
814 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
815 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
816 for (c = 0; c < 4; c++)
817 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
820 if (res == GNUNET_YES)
822 char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
824 GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
827 type = GNUNET_ATS_NET_LAN;
833 /* local network found for this address, default: WAN */
834 if (type == GNUNET_ATS_NET_UNSPECIFIED)
835 type = GNUNET_ATS_NET_WAN;
837 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
838 ats.value = htonl (type);
843 * Initialize the ATS subsystem.
845 * @param cfg configuration to use
846 * @param suggest_cb notification to call whenever the suggestation changed
847 * @param suggest_cb_cls closure for 'suggest_cb'
848 * @return ats context
850 struct GNUNET_ATS_SchedulingHandle *
851 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
852 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
853 void *suggest_cb_cls)
855 struct GNUNET_ATS_SchedulingHandle *sh;
857 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
859 sh->suggest_cb = suggest_cb;
860 sh->suggest_cb_cls = suggest_cb_cls;
861 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
862 GNUNET_OS_network_interfaces_list(interface_proc, sh);
863 sh->interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, sh);
870 * Client is done with ATS scheduling, release resources.
872 * @param sh handle to release
875 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
877 struct PendingMessage *p;
879 while (NULL != (p = sh->pending_head))
881 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
884 if (NULL != sh->client)
886 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
889 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
891 GNUNET_SCHEDULER_cancel (sh->task);
892 sh->task = GNUNET_SCHEDULER_NO_TASK;
895 delete_networks (sh);
896 if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
898 GNUNET_SCHEDULER_cancel(sh->interface_task);
899 sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
902 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
908 * We would like to establish a new connection with a peer. ATS
909 * should suggest a good address to begin with.
912 * @param peer identity of the peer we need an address for
915 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
916 const struct GNUNET_PeerIdentity *peer)
918 struct PendingMessage *p;
919 struct RequestAddressMessage *m;
921 p = GNUNET_malloc (sizeof (struct PendingMessage) +
922 sizeof (struct RequestAddressMessage));
923 p->size = sizeof (struct RequestAddressMessage);
924 p->is_init = GNUNET_NO;
925 m = (struct RequestAddressMessage *) &p[1];
926 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
927 m->header.size = htons (sizeof (struct RequestAddressMessage));
928 m->reserved = htonl (0);
930 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
936 * We would like to stop receiving address updates for this peer
939 * @param peer identity of the peer
942 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
943 const struct GNUNET_PeerIdentity *peer)
945 struct PendingMessage *p;
946 struct RequestAddressMessage *m;
948 p = GNUNET_malloc (sizeof (struct PendingMessage) +
949 sizeof (struct RequestAddressMessage));
950 p->size = sizeof (struct RequestAddressMessage);
951 p->is_init = GNUNET_NO;
952 m = (struct RequestAddressMessage *) &p[1];
953 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
954 m->header.size = htons (sizeof (struct RequestAddressMessage));
955 m->reserved = htonl (0);
957 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
962 * We have updated performance statistics for a given address. Note
963 * that this function can be called for addresses that are currently
964 * in use as well as addresses that are valid but not actively in use.
965 * Furthermore, the peer may not even be connected to us right now (in
966 * which case the call may be ignored or the information may be stored
967 * for later use). Update bandwidth assignments.
970 * @param address the address
971 * @param session session handle (if available)
972 * @param ats performance data for the address
973 * @param ats_count number of performance records in 'ats'
976 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
977 const struct GNUNET_HELLO_Address *address,
978 struct Session *session,
979 const struct GNUNET_ATS_Information *ats,
982 struct PendingMessage *p;
983 struct AddressUpdateMessage *m;
984 struct GNUNET_ATS_Information *am;
990 (address->transport_name ==
991 NULL) ? 0 : strlen (address->transport_name) + 1;
993 sizeof (struct AddressUpdateMessage) + address->address_length +
994 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
995 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
996 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
997 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
999 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1005 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1007 p->is_init = GNUNET_NO;
1008 m = (struct AddressUpdateMessage *) &p[1];
1009 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1010 m->header.size = htons (msize);
1011 m->ats_count = htonl (ats_count);
1012 m->peer = address->peer;
1013 m->address_length = htons (address->address_length);
1014 m->plugin_name_length = htons (namelen);
1015 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1016 am = (struct GNUNET_ATS_Information *) &m[1];
1017 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1018 pm = (char *) &am[ats_count];
1019 memcpy (pm, address->address, address->address_length);
1020 memcpy (&pm[address->address_length], address->transport_name, namelen);
1021 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1027 * An address is now in use or not used any more.
1030 * @param address the address
1031 * @param session session handle
1032 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1033 * if address is not used any more
1036 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1037 const struct GNUNET_HELLO_Address *address,
1038 struct Session *session, int in_use)
1040 struct PendingMessage *p;
1041 struct AddressUseMessage *m;
1046 GNUNET_assert (NULL != address);
1048 (address->transport_name ==
1049 NULL) ? 0 : strlen (address->transport_name) + 1;
1050 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1051 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1052 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1053 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1059 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1061 p->is_init = GNUNET_NO;
1062 m = (struct AddressUseMessage *) &p[1];
1063 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1064 m->header.size = htons (msize);
1065 m->peer = address->peer;
1066 m->in_use = htons (in_use);
1067 m->address_length = htons (address->address_length);
1068 m->plugin_name_length = htons (namelen);
1069 m->session_id = htonl (get_session_id (sh, session, &address->peer));
1070 pm = (char *) &m[1];
1071 memcpy (pm, address->address, address->address_length);
1072 memcpy (&pm[address->address_length], address->transport_name, namelen);
1073 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1079 * A session got destroyed, stop including it as a valid address.
1082 * @param address the address
1083 * @param session session handle that is no longer valid
1086 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1087 const struct GNUNET_HELLO_Address *address,
1088 struct Session *session)
1090 struct PendingMessage *p;
1091 struct AddressDestroyedMessage *m;
1095 uint32_t session_id;
1097 GNUNET_assert (address->transport_name != NULL);
1098 namelen = strlen (address->transport_name) + 1;
1099 GNUNET_assert (namelen > 1);
1101 sizeof (struct AddressDestroyedMessage) + address->address_length +
1103 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1104 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1105 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1111 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1113 p->is_init = GNUNET_NO;
1114 m = (struct AddressDestroyedMessage *) &p[1];
1115 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1116 m->header.size = htons (msize);
1117 m->reserved = htonl (0);
1118 m->peer = address->peer;
1119 m->address_length = htons (address->address_length);
1120 m->plugin_name_length = htons (namelen);
1121 session_id = get_session_id (sh, session, &address->peer);
1122 m->session_id = htonl (session_id);
1123 pm = (char *) &m[1];
1124 memcpy (pm, address->address, address->address_length);
1125 memcpy (&pm[address->address_length], address->transport_name, namelen);
1126 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1128 remove_session (sh, session_id, &address->peer);
1131 /* end of ats_api_scheduling.c */