2 This file is part of GNUnet.
3 (C) 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.
22 * @file ats/gnunet-service-ats_addresses.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_performance.h"
32 #include "gnunet-service-ats_scheduling.h"
33 #include "gnunet-service-ats_reservations.h"
37 struct GNUNET_PeerIdentity peer;
49 struct GNUNET_ATS_Information *ats;
51 struct GNUNET_TIME_Relative atsp_latency;
53 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_in;
55 struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_out;
57 uint32_t atsp_distance;
59 uint32_t atsp_cost_wan;
61 uint32_t atsp_cost_lan;
63 uint32_t atsp_cost_wlan;
65 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_in;
67 struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_out;
70 * Is this the active address for this peer?
78 struct ATS_Network * next;
80 struct ATS_Network * prev;
82 struct sockaddr *network;
83 struct sockaddr *netmask;
88 struct ATS_Network * net_head;
90 struct ATS_Network * net_tail;
92 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
94 static unsigned long long wan_quota_in;
96 static unsigned long long wan_quota_out;
98 static unsigned int active_addr_count;
100 static GNUNET_SCHEDULER_TaskIdentifier interface_task;
104 * Update a bandwidth assignment for a peer. This trivial method currently
105 * simply assigns the same share to all active connections.
109 * @param value the 'struct ATS_Address'
110 * @return GNUNET_OK (continue to iterate)
113 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
115 struct ATS_Address *aa = value;
117 if (GNUNET_YES != aa->active)
119 GNUNET_assert (active_addr_count > 0);
120 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
121 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
123 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
124 ntohl (aa->assigned_bw_out.value__));
125 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
126 aa->addr_len, aa->session_id,
127 aa->ats, aa->ats_count,
130 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
131 GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
132 aa->ats, aa->ats_count, aa->assigned_bw_out,
139 * Some (significant) input changed, recalculate bandwidth assignment
143 recalculate_assigned_bw ()
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Recalculating bandwidth for all active connections\n");
147 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
149 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
151 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
156 * Destroy the given address.
158 * @param addr address to destroy
159 * @return GNUNET_YES if bandwidth allocations should be recalcualted
162 destroy_address (struct ATS_Address *addr)
167 GNUNET_assert (GNUNET_YES ==
168 GNUNET_CONTAINER_multihashmap_remove (addresses,
169 &addr->peer.hashPubKey,
171 if (GNUNET_YES == addr->active)
174 addr->active = GNUNET_NO;
177 GNUNET_free_non_null (addr->ats);
178 GNUNET_free (addr->plugin);
184 struct CompareAddressContext
186 const struct ATS_Address *search;
187 struct ATS_Address *result;
192 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
194 struct CompareAddressContext *cac = cls;
195 struct ATS_Address *aa = value;
197 if (((aa->addr_len != cac->search->addr_len) ||
198 (0 != strcmp (aa->plugin, cac->search->plugin)) ||
199 (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
200 ((aa->session_id != cac->search->session_id) ||
201 (cac->search->session_id == 0)))
209 * Find an existing equivalent address record.
210 * Compares by peer identity and network address OR by session ID
211 * (one of the two must match).
213 * @param peer peer to lookup addresses for
214 * @param addr existing address record
215 * @return existing address record, NULL for none
218 find_address (const struct GNUNET_PeerIdentity *peer,
219 const struct ATS_Address *addr)
221 struct CompareAddressContext cac;
225 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
226 &compare_address_it, &cac);
232 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
233 const char *plugin_name, const void *plugin_addr,
234 size_t plugin_addr_len, uint32_t session_id,
235 const struct GNUNET_ATS_Information *atsi,
238 struct ATS_Address *aa;
239 struct ATS_Address *old;
242 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
243 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
245 aa->addr_len = plugin_addr_len;
246 aa->ats_count = atsi_count;
247 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
249 memcpy (&aa[1], plugin_addr, plugin_addr_len);
250 aa->plugin = GNUNET_strdup (plugin_name);
251 aa->session_id = session_id;
252 old = find_address (peer, aa);
255 GNUNET_assert (GNUNET_OK ==
256 GNUNET_CONTAINER_multihashmap_put (addresses,
257 &peer->hashPubKey, aa,
258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
260 GNUNET_i2s (peer), aa);
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Updated existing address for peer `%s' %X \n",
267 GNUNET_i2s (peer), old);
268 GNUNET_free_non_null (old->ats);
269 old->session_id = session_id;
273 old->ats_count = aa->ats_count;
274 GNUNET_free (aa->plugin);
277 for (i = 0; i < atsi_count; i++)
278 switch (ntohl (atsi[i].type))
280 case GNUNET_ATS_UTILIZATION_UP:
281 old->atsp_utilization_out.value__ = atsi[i].value;
283 case GNUNET_ATS_UTILIZATION_DOWN:
284 old->atsp_utilization_in.value__ = atsi[i].value;
286 case GNUNET_ATS_QUALITY_NET_DELAY:
287 old->atsp_latency.rel_value = ntohl (atsi[i].value);
289 case GNUNET_ATS_QUALITY_NET_DISTANCE:
290 old->atsp_distance = ntohl (atsi[i].value);
292 case GNUNET_ATS_COST_WAN:
293 old->atsp_cost_wan = ntohl (atsi[i].value);
295 case GNUNET_ATS_COST_LAN:
296 old->atsp_cost_lan = ntohl (atsi[i].value);
298 case GNUNET_ATS_COST_WLAN:
299 old->atsp_cost_wlan = ntohl (atsi[i].value);
302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
303 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
311 * Update a bandwidth assignment for a peer. This trivial method currently
312 * simply assigns the same share to all active connections.
316 * @param value the 'struct ATS_Address'
317 * @return GNUNET_OK (continue to iterate)
320 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
322 const struct ATS_Address *info = cls;
323 struct ATS_Address *aa = value;
326 memcmp (&aa->peer, &info->peer,
327 sizeof (struct GNUNET_PeerIdentity)));
328 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
329 (aa->addr_len == info->addr_len) &&
330 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Deleting address for peer `%s': `%s'\n",
334 GNUNET_i2s (&aa->peer), aa->plugin);
335 if (GNUNET_YES == destroy_address (aa))
336 recalculate_assigned_bw ();
339 if (aa->session_id != info->session_id)
340 return GNUNET_OK; /* irrelevant */
341 if (aa->session_id != 0)
342 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
346 if (GNUNET_YES == aa->active)
348 aa->active = GNUNET_NO;
350 recalculate_assigned_bw ();
353 /* session == 0 and addrlen == 0 : destroy address */
354 if (aa->addr_len == 0)
355 (void) destroy_address (aa);
362 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
363 const char *plugin_name, const void *plugin_addr,
364 size_t plugin_addr_len, uint32_t session_id)
366 struct ATS_Address aa;
368 GNUNET_break (0 < strlen (plugin_name));
370 aa.addr_len = plugin_addr_len;
371 aa.addr = plugin_addr;
372 aa.plugin = (char *) plugin_name;
373 aa.session_id = session_id;
374 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
375 &destroy_by_session_id, &aa);
380 * Find a "good" address to use for a peer. If we already have an existing
381 * address, we stick to it. Otherwise, we pick by lowest distance and then
384 * @param cls the 'struct ATS_Address**' where we store the result
386 * @param value another 'struct ATS_Address*' to consider using
387 * @return GNUNET_OK (continue to iterate)
390 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
392 struct ATS_Address **ap = cls;
393 struct ATS_Address *aa = (struct ATS_Address *) value;
394 struct ATS_Address *ab = *ap;
401 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
402 (ntohl (aa->assigned_bw_in.value__) > 0))
404 /* stick to existing connection */
408 if (ab->atsp_distance > aa->atsp_distance)
410 /* user shorter distance */
414 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
416 /* user lower latency */
426 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
427 const char *plugin_name, const void *plugin_addr,
428 size_t plugin_addr_len, uint32_t session_id, int in_use)
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
433 GNUNET_i2s (peer), in_use);
437 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
439 struct ATS_Address *aa;
442 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
443 &find_address_it, &aa);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
450 if (aa->active == GNUNET_NO)
452 aa->active = GNUNET_YES;
454 recalculate_assigned_bw ();
458 /* just to be sure... */
459 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
460 aa->addr_len, aa->session_id,
461 aa->ats, aa->ats_count,
468 // FIXME: this function should likely end up in the LP-subsystem and
469 // not with 'addresses' in the future...
471 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
472 enum GNUNET_ATS_PreferenceKind kind,
475 // do nothing for now...
479 * Returns where the address is located: LAN or WAN or ...
480 * @param addr address
481 * @param addrlen address length
482 * @return location as GNUNET_ATS_Information
485 struct GNUNET_ATS_Information
486 GAS_addresses_type (const struct sockaddr * addr, socklen_t addrlen)
488 struct GNUNET_ATS_Information ats;
489 struct ATS_Network * cur = net_head;
490 int is_lan = GNUNET_NO;
492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Checking type of address `%s'\n", GNUNET_a2s(addr, addrlen));
496 if (addrlen != cur->length)
502 if (addr->sa_family == AF_INET)
504 struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
505 struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
506 struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
508 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr) & net4->sin_addr.s_addr) == net4->sin_addr.s_addr)
510 char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' in network `%s': YES \n",
512 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
519 char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' in network `%s': NO \n",
521 GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
527 if (addr->sa_family == AF_INET6)
532 if (is_lan == GNUNET_YES)
537 if (is_lan == GNUNET_NO)
539 ats.type = ntohl(GNUNET_ATS_ARRAY_TERMINATOR);
540 ats.value = ntohl(GNUNET_ATS_ARRAY_TERMINATOR);
544 ats.type = ntohl(GNUNET_ATS_ARRAY_TERMINATOR);
545 ats.value = ntohl(GNUNET_ATS_ARRAY_TERMINATOR);
551 interface_proc (void *cls, const char *name,
553 const struct sockaddr *
555 const struct sockaddr *
557 const struct sockaddr *
558 netmask, socklen_t addrlen)
560 /* Calculate network */
561 struct ATS_Network *net = NULL;
562 if (addr->sa_family == AF_INET)
564 struct ATS_Network *net = NULL;
565 struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
566 struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
567 struct sockaddr * tmp = NULL;
568 struct sockaddr_in network4;
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding %s address: %s\n", (addr->sa_family == AF_INET) ? "IPv4" : "IPv6" ,GNUNET_a2s(addr, addrlen));
572 net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
573 tmp = (struct sockaddr *) &net[1];
574 net->network = &tmp[0];
575 net->netmask = &tmp[1];
576 net->length = addrlen;
578 network4.sin_family = AF_INET;
579 #if HAVE_SOCKADDR_IN_SIN_LEN
580 network4.sin_len = sizeof (network4);
582 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
584 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
585 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
587 char * netmask = strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
589 GNUNET_a2s((struct sockaddr *) net->network, addrlen),
591 GNUNET_free (netmask);
594 if (addr->sa_family == AF_INET6)
601 GNUNET_CONTAINER_DLL_insert(net_head, net_tail, net);
603 //GAS_addresses_type (addr, addrlen);
611 struct ATS_Network * cur = net_head;
614 GNUNET_CONTAINER_DLL_remove(net_head, net_tail, cur);
621 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
623 interface_task = GNUNET_SCHEDULER_NO_TASK;
625 GNUNET_OS_network_interfaces_list(interface_proc, NULL);
627 interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, NULL);
631 * Initialize address subsystem.
633 * @param cfg configuration to use
636 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
638 GNUNET_assert (GNUNET_OK ==
639 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
642 GNUNET_assert (GNUNET_OK ==
643 GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
646 addresses = GNUNET_CONTAINER_multihashmap_create (128);
648 interface_task = GNUNET_SCHEDULER_add_now(get_addresses, NULL);
653 * Free memory of address.
656 * @param key peer identity (unused)
657 * @param value the 'struct ATS_Address' to free
658 * @return GNUNET_OK (continue to iterate)
661 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
663 struct ATS_Address *aa = value;
665 destroy_address (aa);
671 GAS_addresses_destroy_all ()
673 if (addresses != NULL)
674 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
675 GNUNET_assert (active_addr_count == 0);
680 * Shutdown address subsystem.
683 GAS_addresses_done ()
686 if (interface_task != GNUNET_SCHEDULER_NO_TASK)
688 GNUNET_SCHEDULER_cancel(interface_task);
689 interface_task = GNUNET_SCHEDULER_NO_TASK;
691 GAS_addresses_destroy_all ();
692 GNUNET_CONTAINER_multihashmap_destroy (addresses);
697 /* end of gnunet-service-ats_addresses.c */