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"
35 #include "gnunet-service-ats_addresses_mlp.h"
37 #include "gnunet-service-ats_addresses_simplistic.h"
40 #define BIG_M_VALUE (UINT32_MAX) /10
41 #define BIG_M_STRING "unlimited"
44 * Available ressource assignment modes
51 * Assign each peer an equal amount of bandwidth (bw)
53 * bw_per_peer = bw_total / #active addresses
60 * Solve ressource assignment as an optimization problem
61 * Uses an mixed integer programming solver
67 * Handle for ATS address component
69 struct GAS_Addresses_Suggestion_Requests
71 struct GAS_Addresses_Suggestion_Requests *next;
72 struct GAS_Addresses_Suggestion_Requests *prev;
74 struct GNUNET_PeerIdentity id;
78 * Handle for ATS address component
80 struct GAS_Addresses_Handle
83 * A multihashmap to store all addresses
85 struct GNUNET_CONTAINER_MultiHashMap *addresses;
88 * Configure WAN quota in
90 unsigned long long wan_quota_in;
93 * Configure WAN quota out
95 unsigned long long wan_quota_out;
98 * Number of active addresses
100 unsigned int active_addr_count;
103 * Is ATS addresses running
108 * Configured ATS solver
118 * Address suggestion requests DLL head
120 struct GAS_Addresses_Suggestion_Requests *r_head;
123 * Address suggestion requests DLL tail
125 struct GAS_Addresses_Suggestion_Requests *r_tail;
127 /* Solver functions */
132 GAS_solver_init s_init;
135 * Update address in solver
137 GAS_solver_address_update s_update;
140 * Get address from solver
142 GAS_solver_get_preferred_address s_get;
145 * Delete address in solver
147 GAS_solver_address_delete s_del;
150 * Change preference for quality in solver
152 GAS_solver_address_change_preference s_pref;
157 GAS_solver_done s_done;
164 struct GAS_Addresses_Handle *handle;
168 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
170 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
171 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
174 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
175 ats[0].value = aa->atsp_utilization_out.value__;
176 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
177 ats[1].value = aa->atsp_utilization_in.value__;
178 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
179 ats[2].value = ntohl(aa->atsp_network_type);
180 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
181 ats[3].value = ntohl(aa->atsp_latency.rel_value);
182 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
183 ats[4].value = ntohl(aa->atsp_distance);
184 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
185 ats[5].value = ntohl (aa->atsp_cost_wan);
186 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
187 ats[6].value = ntohl (aa->atsp_cost_lan);
188 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
189 ats[7].value = ntohl (aa->atsp_cost_wlan);
194 * Free the given address
195 * @param addr address to destroy
198 free_address (struct ATS_Address *addr)
200 GNUNET_free_non_null (addr->ats);
201 GNUNET_free (addr->plugin);
206 * Create a ATS_address with the given information
208 * @param plugin_name plugin
209 * @param plugin_addr address
210 * @param plugin_addr_len address length
211 * @param session_id session
212 * @return the ATS_Address
214 static struct ATS_Address *
215 create_address (const struct GNUNET_PeerIdentity *peer,
216 const char *plugin_name,
217 const void *plugin_addr, size_t plugin_addr_len,
220 struct ATS_Address *aa = NULL;
222 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
224 aa->addr_len = plugin_addr_len;
226 memcpy (&aa[1], plugin_addr, plugin_addr_len);
227 aa->plugin = GNUNET_strdup (plugin_name);
228 aa->session_id = session_id;
234 * Destroy the given address.
236 * @param addr address to destroy
237 * @return GNUNET_YES if bandwidth allocations should be recalcualted
240 destroy_address (struct ATS_Address *addr)
245 GNUNET_assert (GNUNET_YES ==
246 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
247 &addr->peer.hashPubKey,
251 if (handle->ats_mode == MODE_MLP)
252 GAS_mlp_address_delete (handle->solver, handle->addresses, addr);
255 if (GNUNET_YES == addr->active)
257 handle->active_addr_count--;
258 addr->active = GNUNET_NO;
266 struct CompareAddressContext
268 const struct ATS_Address *search;
270 /* exact_address != NULL if address and session is equal */
271 struct ATS_Address *exact_address;
272 /* exact_address != NULL if address and session is 0 */
273 struct ATS_Address *base_address;
278 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
280 struct CompareAddressContext *cac = cls;
281 struct ATS_Address *aa = value;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
285 aa->addr_len, aa->session_id,
286 cac->search->addr_len, cac->search->session_id);
288 /* Find an matching exact address:
291 * aa->addr_len == cac->search->addr_len
292 * aa->plugin == cac->search->plugin
293 * aa->addr == cac->search->addr
294 * aa->session == cac->search->session
296 * return as exact address
298 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
300 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
301 cac->exact_address = aa;
304 /* Find an matching base address:
308 * aa->session_id == 0
311 * aa->addr_len == cac->search->addr_len
312 * aa->plugin == cac->search->plugin
313 * aa->addr == cac->search->addr
315 * return as base address
317 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
319 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
320 cac->base_address = aa;
323 /* Find an matching exact address based on session:
327 * cac->search->addr_len == 0
330 * aa->plugin == cac->search->plugin
331 * aa->session_id == cac->search->session_id
333 * return as exact address
335 if (0 == cac->search->addr_len)
337 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
338 cac->exact_address = aa;
341 if (cac->exact_address == NULL)
342 return GNUNET_YES; /* Continue iteration to find exact address */
344 return GNUNET_NO; /* Stop iteration since we have an exact address */
349 * Find an existing equivalent address record.
350 * Compares by peer identity and network address OR by session ID
351 * (one of the two must match).
353 * @param peer peer to lookup addresses for
354 * @param addr existing address record
355 * @return existing address record, NULL for none
358 find_address (const struct GNUNET_PeerIdentity *peer,
359 const struct ATS_Address *addr)
361 struct CompareAddressContext cac;
363 cac.exact_address = NULL;
364 cac.base_address = NULL;
366 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
367 &compare_address_it, &cac);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Found exact address: %s base address: %s\n",
372 (cac.exact_address != NULL) ? "YES" : "NO",
373 (cac.base_address != NULL) ? "YES" : "NO");
375 if (cac.exact_address == NULL)
376 return cac.base_address;
377 return cac.exact_address;
381 static struct ATS_Address *
382 lookup_address (const struct GNUNET_PeerIdentity *peer,
383 const char *plugin_name, const void *plugin_addr,
384 size_t plugin_addr_len, uint32_t session_id,
385 const struct GNUNET_ATS_Information *atsi,
388 struct ATS_Address *aa;
389 struct ATS_Address *old;
391 aa = create_address (peer,
393 plugin_addr, plugin_addr_len,
396 aa->mlp_information = NULL;
397 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
398 aa->ats_count = atsi_count;
399 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
401 /* Get existing address or address with session == 0 */
402 old = find_address (peer, aa);
408 else if (old->session_id != session_id)
418 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
420 struct CompareAddressContext *cac = cls;
421 struct ATS_Address *aa = value;
423 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
425 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
427 cac->exact_address = aa;
436 * Find an existing equivalent address record.
437 * Compares by peer identity and network address AND by session ID
438 * (one of the two must match).
440 * @param peer peer to lookup addresses for
441 * @param addr existing address record
442 * @return existing address record, NULL for none
444 static struct ATS_Address *
445 find_exact_address (const struct GNUNET_PeerIdentity *peer,
446 const struct ATS_Address *addr)
448 struct CompareAddressContext cac;
450 cac.exact_address = NULL;
452 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
453 &compare_address_session_it, &cac);
454 return cac.exact_address;
459 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
460 const char *plugin_name, const void *plugin_addr,
461 size_t plugin_addr_len, uint32_t session_id,
462 const struct GNUNET_ATS_Information *atsi,
465 struct ATS_Address *aa;
466 struct ATS_Address *old;
468 if (GNUNET_NO == handle->running)
471 GNUNET_assert (NULL != handle->addresses);
473 aa = create_address (peer,
475 plugin_addr, plugin_addr_len,
478 aa->mlp_information = NULL;
479 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
480 aa->ats_count = atsi_count;
481 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
483 /* Get existing address or address with session == 0 */
484 old = find_address (peer, aa);
487 /* We have a new address */
488 GNUNET_assert (GNUNET_OK ==
489 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
490 &peer->hashPubKey, aa,
491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
493 GNUNET_i2s (peer), session_id, aa);
497 if (old->session_id == 0)
499 /* We have a base address with out an session, update this address */
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Updated existing address for peer `%s' %p with new session %u\n",
502 GNUNET_i2s (peer), old, session_id);
503 GNUNET_free_non_null (old->ats);
504 old->session_id = session_id;
508 old->ats_count = aa->ats_count;
509 GNUNET_free (aa->plugin);
514 /* This address and session is already existing */
515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
516 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
517 GNUNET_i2s (peer), plugin_name, session_id);
523 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
524 const char *plugin_name, const void *plugin_addr,
525 size_t plugin_addr_len, uint32_t session_id,
526 const struct GNUNET_ATS_Information *atsi,
529 struct ATS_Address *old;
532 if (GNUNET_NO == handle->running)
535 GNUNET_assert (NULL != handle->addresses);
537 /* Get existing address */
538 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
539 session_id, atsi, atsi_count);
542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
543 GNUNET_i2s (peer), plugin_name, session_id);
548 for (i = 0; i < atsi_count; i++)
549 switch (ntohl (atsi[i].type))
551 case GNUNET_ATS_UTILIZATION_UP:
552 old->atsp_utilization_out.value__ = atsi[i].value;
554 case GNUNET_ATS_UTILIZATION_DOWN:
555 old->atsp_utilization_in.value__ = atsi[i].value;
557 case GNUNET_ATS_QUALITY_NET_DELAY:
558 old->atsp_latency.rel_value = ntohl (atsi[i].value);
560 case GNUNET_ATS_QUALITY_NET_DISTANCE:
561 old->atsp_distance = ntohl (atsi[i].value);
563 case GNUNET_ATS_COST_WAN:
564 old->atsp_cost_wan = ntohl (atsi[i].value);
566 case GNUNET_ATS_COST_LAN:
567 old->atsp_cost_lan = ntohl (atsi[i].value);
569 case GNUNET_ATS_COST_WLAN:
570 old->atsp_cost_wlan = ntohl (atsi[i].value);
572 case GNUNET_ATS_NETWORK_TYPE:
573 old->atsp_network_type = ntohl (atsi[i].value);
577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
578 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
583 /* Tell solver about update */
584 handle->s_update (handle->solver, handle->addresses, old);
591 * If session != 0, just the session is deleted, the address itself still exists
592 * If session == 0, remove full address
593 * If session == 0 and addrlen == 0, destroy inbound address
597 * @param value the 'struct ATS_Address'
598 * @return GNUNET_OK (continue to iterate)
601 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
603 const struct ATS_Address *info = cls;
604 struct ATS_Address *aa = value;
607 memcmp (&aa->peer, &info->peer,
608 sizeof (struct GNUNET_PeerIdentity)));
609 /* session == 0, remove full address */
610 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
611 (aa->addr_len == info->addr_len) &&
612 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Deleting address for peer `%s': `%s' %u\n",
617 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
619 if (GNUNET_YES == destroy_address (aa))
620 recalculate_assigned_bw (); */
623 /* session != 0, just remove session */
624 if (aa->session_id != info->session_id)
625 return GNUNET_OK; /* irrelevant */
626 if (aa->session_id != 0)
627 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 "Deleting session for peer `%s': `%s' %u\n",
631 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
634 if (GNUNET_YES == aa->active)
636 aa->active = GNUNET_NO;
637 handle->active_addr_count--;
638 //FIXME recalculate_assigned_bw ();
641 /* session == 0 and addrlen == 0 : destroy address */
642 if (aa->addr_len == 0)
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Deleting session and address for peer `%s': `%s' %u\n",
646 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
647 (void) destroy_address (aa);
651 /* session was set to 0, update address */
653 if (handle->ats_mode == MODE_MLP)
654 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
663 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
664 const char *plugin_name, const void *plugin_addr,
665 size_t plugin_addr_len, uint32_t session_id)
667 struct ATS_Address *aa;
668 struct ATS_Address *old;
670 if (GNUNET_NO == handle->running)
673 /* Get existing address */
674 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
675 session_id, NULL, 0);
678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
679 GNUNET_i2s (peer), plugin_name, session_id);
684 GNUNET_break (0 < strlen (plugin_name));
685 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
687 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
688 &destroy_by_session_id, aa);
695 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
696 const char *plugin_name, const void *plugin_addr,
697 size_t plugin_addr_len, uint32_t session_id, int in_use)
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
702 GNUNET_i2s (peer), in_use);
705 struct ATS_Address *old;
707 if (GNUNET_NO == handle->running)
708 return GNUNET_SYSERR;
710 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
714 "Trying to set unknown address `%s', %s %u %s \n",
716 plugin_name, session_id,
717 (GNUNET_NO == in_use) ? "NO" : "YES");
719 return GNUNET_SYSERR;
721 if (old->used == in_use)
724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
725 "Address in use called multiple times for peer `%s': %s -> %s \n",
727 (GNUNET_NO == old->used) ? "NO" : "YES",
728 (GNUNET_NO == in_use) ? "NO" : "YES");
729 return GNUNET_SYSERR;
733 /* Tell solver about update */
734 handle->s_update (handle->solver, handle->addresses, old);
741 * Cancel address suggestions for a peer
743 * @param peer the respective peer
746 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
748 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
751 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
761 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
767 * Add an address suggestions for a peer
769 * @param peer the respective peer
772 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
774 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
775 const struct ATS_Address *aa;
776 struct GNUNET_ATS_Information *ats;
777 unsigned int ats_count;
779 if (GNUNET_NO == handle->running)
783 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
784 break; /* already suggesting */
789 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
791 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
794 /* Get prefered address from solver */
795 aa = handle->s_get (handle->solver, handle->addresses, peer);
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
803 ats_count = assemble_ats_information (aa, &ats);
804 GAS_scheduling_transmit_address_suggestion (peer,
806 aa->addr, aa->addr_len,
817 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
819 struct ATS_Address *aa = value;
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Resetting interval for peer `%s' address %p from %llu to 0\n",
823 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
825 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
826 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
832 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
834 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
842 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
843 enum GNUNET_ATS_PreferenceKind kind,
846 if (GNUNET_NO == handle->running)
849 /* Tell solver about update */
850 handle->s_pref (handle->solver, peer, kind, score);
854 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
856 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
857 char * entry_in = NULL;
858 char * entry_out = NULL;
859 char * quota_out_str;
863 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
868 case GNUNET_ATS_NET_UNSPECIFIED:
869 entry_out = "UNSPECIFIED_QUOTA_OUT";
870 entry_in = "UNSPECIFIED_QUOTA_IN";
872 case GNUNET_ATS_NET_LOOPBACK:
873 entry_out = "LOOPBACK_QUOTA_OUT";
874 entry_in = "LOOPBACK_QUOTA_IN";
876 case GNUNET_ATS_NET_LAN:
877 entry_out = "LAN_QUOTA_OUT";
878 entry_in = "LAN_QUOTA_IN";
880 case GNUNET_ATS_NET_WAN:
881 entry_out = "WAN_QUOTA_OUT";
882 entry_in = "WAN_QUOTA_IN";
884 case GNUNET_ATS_NET_WLAN:
885 entry_out = "WLAN_QUOTA_OUT";
886 entry_in = "WLAN_QUOTA_IN";
892 if ((entry_in == NULL) || (entry_out == NULL))
896 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
898 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
899 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
900 out_dest[c] = UINT32_MAX;
902 GNUNET_free (quota_out_str);
903 quota_out_str = NULL;
905 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
906 out_dest[c] = UINT32_MAX;
908 out_dest[c] = UINT32_MAX;
911 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
913 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
914 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
915 in_dest[c] = UINT32_MAX;
917 GNUNET_free (quota_in_str);
920 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
922 in_dest[c] = UINT32_MAX;
926 in_dest[c] = UINT32_MAX;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
931 return GNUNET_ATS_NetworkTypeCount;
937 * Initialize address subsystem.
939 * @param cfg configuration to use
940 * @param stats the statistics handle to use
942 struct GAS_Addresses_Handle *
943 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
944 const struct GNUNET_STATISTICS_Handle *stats)
946 struct GAS_Addresses_Handle *ah;
947 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
948 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
949 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
954 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
956 handle->running = GNUNET_NO;
958 /* Initialize the addresses database */
959 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
960 GNUNET_assert (NULL != ah->addresses);
962 /* Figure out configured solution method */
963 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
965 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
966 ah->ats_mode = MODE_SIMPLISTIC;
970 for (c = 0; c < strlen (mode_str); c++)
971 mode_str[c] = toupper (mode_str[c]);
972 if (0 == strcmp (mode_str, "SIMPLISTIC"))
974 ah->ats_mode = MODE_SIMPLISTIC;
976 else if (0 == strcmp (mode_str, "MLP"))
978 ah->ats_mode = MODE_MLP;
980 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
981 ah->ats_mode = MODE_SIMPLISTIC;
986 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
987 ah->ats_mode = MODE_SIMPLISTIC;
989 GNUNET_free (mode_str);
991 /* Start configured solution method */
992 switch (ah->ats_mode)
995 /* Init the MLP solver with default values */
997 ah->ats_mode = MODE_MLP;
998 ah->s_init = &GAS_mlp_init;
999 ah->s_update = &GAS_mlp_address_update;
1000 ah->s_get = &GAS_mlp_get_preferred_address;
1001 ah->s_pref = &GAS_mlp_address_change_preference;
1002 ah->s_del = &GAS_mlp_address_delete;
1003 ah->s_done = &GAS_mlp_done;
1009 case MODE_SIMPLISTIC:
1010 /* Init the simplistic solver with default values */
1011 ah->ats_mode = MODE_SIMPLISTIC;
1012 ah->s_init = &GAS_simplistic_init;
1013 ah->s_update = &GAS_simplistic_address_update;
1014 ah->s_get = &GAS_simplistic_get_preferred_address;
1015 ah->s_pref = &GAS_simplistic_address_change_preference;
1016 ah->s_del = &GAS_simplistic_address_delete;
1017 ah->s_done = &GAS_simplistic_done;
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1025 GNUNET_assert (NULL != ah->s_init);
1026 GNUNET_assert (NULL != ah->s_update);
1027 GNUNET_assert (NULL != ah->s_get);
1028 GNUNET_assert (NULL != ah->s_pref);
1029 GNUNET_assert (NULL != ah->s_del);
1030 GNUNET_assert (NULL != ah->s_done);
1032 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1034 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count);
1035 if (NULL == ah->solver)
1037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1042 /* up and running */
1043 ah->running = GNUNET_YES;
1049 * Free memory of address.
1052 * @param key peer identity (unused)
1053 * @param value the 'struct ATS_Address' to free
1054 * @return GNUNET_OK (continue to iterate)
1057 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1059 struct ATS_Address *aa = value;
1061 destroy_address (aa);
1067 GAS_addresses_destroy_all ()
1069 if (GNUNET_NO == handle->running)
1072 if (handle->addresses != NULL)
1073 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1074 GNUNET_assert (handle->active_addr_count == 0);
1079 * Shutdown address subsystem.
1082 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1084 struct GAS_Addresses_Suggestion_Requests *cur;
1086 GNUNET_assert (NULL != handle);
1087 GAS_addresses_destroy_all ();
1088 handle->running = GNUNET_NO;
1089 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1090 handle->addresses = NULL;
1091 while (NULL != (cur = handle->r_head))
1093 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1097 GNUNET_free (handle);
1098 /* Stop configured solution method */
1102 struct PeerIteratorContext
1104 GNUNET_ATS_Peer_Iterator it;
1106 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1111 const struct GNUNET_HashCode * key,
1114 struct PeerIteratorContext *ip_ctx = cls;
1115 struct GNUNET_PeerIdentity tmp;
1117 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1119 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1120 tmp.hashPubKey = (*key);
1121 ip_ctx->it (ip_ctx->it_cls, &tmp);
1128 * Return all peers currently known to ATS
1130 * @param p_it the iterator to call for every peer, callbach with id == NULL
1132 * @param p_it_cls the closure for the iterator
1135 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1137 struct PeerIteratorContext ip_ctx;
1142 GNUNET_assert (NULL != handle->addresses);
1144 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1148 ip_ctx.it_cls = p_it_cls;
1149 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1150 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1151 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1153 p_it (p_it_cls, NULL);
1156 struct PeerInfoIteratorContext
1158 GNUNET_ATS_PeerInfo_Iterator it;
1164 peerinfo_it (void *cls,
1165 const struct GNUNET_HashCode * key,
1168 struct PeerInfoIteratorContext *pi_ctx = cls;
1169 struct ATS_Address *addr = (struct ATS_Address *) value;
1170 struct GNUNET_ATS_Information *ats;
1173 if (NULL != pi_ctx->it)
1175 ats_count = assemble_ats_information (addr, &ats);
1177 pi_ctx->it (pi_ctx->it_cls,
1180 addr->addr, addr->addr_len,
1183 addr->assigned_bw_out,
1184 addr->assigned_bw_in);
1192 * Return all peers currently known to ATS
1194 * @param peer the respective peer
1195 * @param pi_it the iterator to call for every peer
1196 * @param pi_it_cls the closure for the iterator
1199 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1201 struct PeerInfoIteratorContext pi_ctx;
1202 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1203 GNUNET_assert (NULL != peer);
1204 GNUNET_assert (NULL != handle->addresses);
1206 return; /* does not make sense without callback */
1208 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1210 pi_ctx.it_cls = pi_it_cls;
1212 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1215 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1220 /* end of gnunet-service-ats_addresses.c */