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"
41 * Available ressource assignment modes
48 * Assign each peer an equal amount of bandwidth (bw)
50 * bw_per_peer = bw_total / #active addresses
57 * Solve ressource assignment as an optimization problem
58 * Uses an mixed integer programming solver
64 * Handle for ATS address component
66 struct GAS_Addresses_Suggestion_Requests
68 struct GAS_Addresses_Suggestion_Requests *next;
69 struct GAS_Addresses_Suggestion_Requests *prev;
71 struct GNUNET_PeerIdentity id;
75 * Handle for ATS address component
77 struct GAS_Addresses_Handle
80 * A multihashmap to store all addresses
82 struct GNUNET_CONTAINER_MultiHashMap *addresses;
85 * Configure WAN quota in
87 unsigned long long wan_quota_in;
90 * Configure WAN quota out
92 unsigned long long wan_quota_out;
95 * Is ATS addresses running
100 * Configured ATS solver
110 * Address suggestion requests DLL head
112 struct GAS_Addresses_Suggestion_Requests *r_head;
115 * Address suggestion requests DLL tail
117 struct GAS_Addresses_Suggestion_Requests *r_tail;
119 /* Solver functions */
124 GAS_solver_init s_init;
127 * Add an address to the solver
129 GAS_solver_address_add s_add;
132 * Update address in solver
134 GAS_solver_address_update s_update;
137 * Get address from solver
139 GAS_solver_get_preferred_address s_get;
142 * Delete address in solver
144 GAS_solver_address_delete s_del;
147 * Change preference for quality in solver
149 GAS_solver_address_change_preference s_pref;
154 GAS_solver_done s_done;
161 struct GAS_Addresses_Handle *handle;
165 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
167 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
168 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
171 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
172 ats[0].value = aa->atsp_utilization_out.value__;
173 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
174 ats[1].value = aa->atsp_utilization_in.value__;
175 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
176 ats[2].value = ntohl(aa->atsp_network_type);
177 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
178 ats[3].value = ntohl(aa->atsp_latency.rel_value);
179 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
180 ats[4].value = ntohl(aa->atsp_distance);
181 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
182 ats[5].value = ntohl (aa->atsp_cost_wan);
183 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
184 ats[6].value = ntohl (aa->atsp_cost_lan);
185 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
186 ats[7].value = ntohl (aa->atsp_cost_wlan);
191 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
193 struct ATS_Address *dest)
197 for (i = 0; i < ats_count; i++)
198 switch (ntohl (src[i].type))
200 case GNUNET_ATS_UTILIZATION_UP:
201 dest->atsp_utilization_out.value__ = src[i].value;
204 case GNUNET_ATS_UTILIZATION_DOWN:
205 dest->atsp_utilization_in.value__ = src[i].value;
208 case GNUNET_ATS_QUALITY_NET_DELAY:
209 dest->atsp_latency.rel_value = ntohl (src[i].value);
212 case GNUNET_ATS_QUALITY_NET_DISTANCE:
213 dest->atsp_distance = ntohl (src[i].value);
216 case GNUNET_ATS_COST_WAN:
217 dest->atsp_cost_wan = ntohl (src[i].value);
220 case GNUNET_ATS_COST_LAN:
221 dest->atsp_cost_lan = ntohl (src[i].value);
224 case GNUNET_ATS_COST_WLAN:
225 dest->atsp_cost_wlan = ntohl (src[i].value);
228 case GNUNET_ATS_NETWORK_TYPE:
229 dest->atsp_network_type = ntohl (src[i].value);
232 case GNUNET_ATS_ARRAY_TERMINATOR:
235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236 "Received unsupported ATS type %u\n", ntohl (src[i].type));
244 * Free the given address
245 * @param addr address to destroy
248 free_address (struct ATS_Address *addr)
250 GNUNET_free (addr->plugin);
255 * Create a ATS_address with the given information
257 * @param plugin_name plugin
258 * @param plugin_addr address
259 * @param plugin_addr_len address length
260 * @param session_id session
261 * @return the ATS_Address
263 static struct ATS_Address *
264 create_address (const struct GNUNET_PeerIdentity *peer,
265 const char *plugin_name,
266 const void *plugin_addr, size_t plugin_addr_len,
269 struct ATS_Address *aa = NULL;
271 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
273 aa->addr_len = plugin_addr_len;
275 memcpy (&aa[1], plugin_addr, plugin_addr_len);
276 aa->plugin = GNUNET_strdup (plugin_name);
277 aa->session_id = session_id;
283 * Destroy the given address.
285 * @param addr address to destroy
286 * @return GNUNET_YES if bandwidth allocations should be recalcualted
289 destroy_address (struct ATS_Address *addr)
294 GNUNET_assert (GNUNET_YES ==
295 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
296 &addr->peer.hashPubKey,
299 handle->s_del (handle->solver, handle->addresses, addr);
305 struct CompareAddressContext
307 const struct ATS_Address *search;
309 /* exact_address != NULL if address and session is equal */
310 struct ATS_Address *exact_address;
311 /* exact_address != NULL if address and session is 0 */
312 struct ATS_Address *base_address;
317 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
319 struct CompareAddressContext *cac = cls;
320 struct ATS_Address *aa = value;
322 /* Find an matching exact address:
325 * aa->addr_len == cac->search->addr_len
326 * aa->plugin == cac->search->plugin
327 * aa->addr == cac->search->addr
328 * aa->session == cac->search->session
330 * return as exact address
332 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
334 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
335 cac->exact_address = aa;
338 /* Find an matching base address:
342 * aa->session_id == 0
345 * aa->addr_len == cac->search->addr_len
346 * aa->plugin == cac->search->plugin
347 * aa->addr == cac->search->addr
349 * return as base address
351 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
353 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
354 cac->base_address = aa;
357 /* Find an matching exact address based on session:
361 * cac->search->addr_len == 0
364 * aa->plugin == cac->search->plugin
365 * aa->session_id == cac->search->session_id
367 * return as exact address
369 if (0 == cac->search->addr_len)
371 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
372 cac->exact_address = aa;
375 if (cac->exact_address == NULL)
376 return GNUNET_YES; /* Continue iteration to find exact address */
378 return GNUNET_NO; /* Stop iteration since we have an exact address */
383 * Find an existing equivalent address record.
384 * Compares by peer identity and network address OR by session ID
385 * (one of the two must match).
387 * @param peer peer to lookup addresses for
388 * @param addr existing address record
389 * @return existing address record, NULL for none
392 find_address (const struct GNUNET_PeerIdentity *peer,
393 const struct ATS_Address *addr)
395 struct CompareAddressContext cac;
397 cac.exact_address = NULL;
398 cac.base_address = NULL;
400 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
401 &compare_address_it, &cac);
403 if (cac.exact_address == NULL)
404 return cac.base_address;
405 return cac.exact_address;
409 static struct ATS_Address *
410 lookup_address (const struct GNUNET_PeerIdentity *peer,
411 const char *plugin_name, const void *plugin_addr,
412 size_t plugin_addr_len, uint32_t session_id,
413 const struct GNUNET_ATS_Information *atsi,
416 struct ATS_Address *aa;
417 struct ATS_Address *old;
419 aa = create_address (peer,
421 plugin_addr, plugin_addr_len,
424 aa->mlp_information = NULL;
426 /* Get existing address or address with session == 0 */
427 old = find_address (peer, aa);
433 else if (old->session_id != session_id)
442 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
443 const char *plugin_name, const void *plugin_addr,
444 size_t plugin_addr_len, uint32_t session_id,
445 const struct GNUNET_ATS_Information *atsi,
448 struct ATS_Address *aa;
449 struct ATS_Address *old;
450 unsigned int ats_res;
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Received `%s' for peer `%s'\n",
457 if (GNUNET_NO == handle->running)
460 GNUNET_assert (NULL != handle->addresses);
462 aa = create_address (peer,
464 plugin_addr, plugin_addr_len,
466 aa->mlp_information = NULL;
467 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "While adding address: had %u ATS elements to add, could only add %u\n",
471 atsi_count, ats_res);
474 /* Get existing address or address with session == 0 */
475 old = find_address (peer, aa);
478 /* We have a new address */
479 GNUNET_assert (GNUNET_OK ==
480 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
481 &peer->hashPubKey, aa,
482 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
484 GNUNET_i2s (peer), session_id, aa);
485 /* Tell solver about update */
486 handle->s_add (handle->solver, handle->addresses, aa);
490 if (old->session_id != 0)
492 /* This address and session is already existing */
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
494 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
495 GNUNET_i2s (peer), plugin_name, session_id);
500 /* We have an address without an session, update this address */
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Updated existing address for peer `%s' %p with new session %u\n",
503 GNUNET_i2s (peer), old, session_id);
504 old->session_id = session_id;
505 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, old)))
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "While updating address: had %u ATS elements to add, could only add %u\n",
509 atsi_count, ats_res);
511 GNUNET_free (aa->plugin);
513 handle->s_add (handle->solver, handle->addresses, old);
518 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
519 const char *plugin_name, const void *plugin_addr,
520 size_t plugin_addr_len, uint32_t session_id,
521 const struct GNUNET_ATS_Information *atsi,
524 struct ATS_Address *old;
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Received `%s' for peer `%s'\n",
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 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, old)))
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "While adding address: had %u ATS elements to add, could only add %u\n",
552 atsi_count, ats_res);
555 /* Tell solver about update */
556 handle->s_update (handle->solver, handle->addresses, old);
563 * If session != 0, just the session is deleted, the address itself still exists
564 * If session == 0, remove full address
565 * If session == 0 and addrlen == 0, destroy inbound address
569 * @param value the 'struct ATS_Address'
570 * @return GNUNET_OK (continue to iterate)
573 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
575 const struct ATS_Address *info = cls;
576 struct ATS_Address *aa = value;
579 memcmp (&aa->peer, &info->peer,
580 sizeof (struct GNUNET_PeerIdentity)));
581 /* session == 0, remove full address */
582 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
583 (aa->addr_len == info->addr_len) &&
584 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Deleting address for peer `%s': `%s' %u\n",
589 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
591 destroy_address (aa);
592 // FIXME if (GNUNET_YES == destroy_address (aa))recalculate_assigned_bw ();
595 /* session != 0, just remove session */
596 if (aa->session_id != info->session_id)
597 return GNUNET_OK; /* irrelevant */
598 if (aa->session_id != 0)
599 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Deleting session for peer `%s': `%s' %u\n",
603 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
606 if (GNUNET_YES == aa->active)
608 aa->active = GNUNET_NO;
609 //FIXME recalculate_assigned_bw ();
612 /* session == 0 and addrlen == 0 : destroy address */
613 if (aa->addr_len == 0)
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Deleting session and address for peer `%s': `%s' %u\n",
617 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
618 (void) destroy_address (aa);
622 /* session was set to 0, update address */
624 if (handle->ats_mode == MODE_MLP)
625 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
634 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
635 const char *plugin_name, const void *plugin_addr,
636 size_t plugin_addr_len, uint32_t session_id)
638 struct ATS_Address *aa;
639 struct ATS_Address *old;
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Received `%s' for peer `%s'\n",
646 if (GNUNET_NO == handle->running)
649 /* Get existing address */
650 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
651 session_id, NULL, 0);
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
655 GNUNET_i2s (peer), plugin_name, session_id);
659 GNUNET_break (0 < strlen (plugin_name));
660 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
661 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
662 &destroy_by_session_id, aa);
668 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
669 const char *plugin_name, const void *plugin_addr,
670 size_t plugin_addr_len, uint32_t session_id, int in_use)
672 struct ATS_Address *old;
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Received `%s' for peer `%s'\n",
679 if (GNUNET_NO == handle->running)
680 return GNUNET_SYSERR;
682 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
686 "Trying to set unknown address `%s', %s %u %s \n",
688 plugin_name, session_id,
689 (GNUNET_NO == in_use) ? "NO" : "YES");
691 return GNUNET_SYSERR;
693 if (old->used == in_use)
696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
697 "Address in use called multiple times for peer `%s': %s -> %s \n",
699 (GNUNET_NO == old->used) ? "NO" : "YES",
700 (GNUNET_NO == in_use) ? "NO" : "YES");
701 return GNUNET_SYSERR;
705 /* Tell solver about update */
706 handle->s_update (handle->solver, handle->addresses, old);
713 * Cancel address suggestions for a peer
715 * @param peer the respective peer
718 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
720 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
727 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
735 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
738 GAS_addresses_handle_backoff_reset (peer);
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
741 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
747 * Add an address suggestions for a peer
749 * @param peer the respective peer
752 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
754 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
755 struct ATS_Address *aa;
756 struct GNUNET_ATS_Information *ats;
757 unsigned int ats_count;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Received `%s' for peer `%s'\n",
764 if (GNUNET_NO == handle->running)
768 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
769 break; /* already suggesting */
774 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
776 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
779 /* Get prefered address from solver */
780 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
791 ats_count = assemble_ats_information (aa, &ats);
792 GAS_scheduling_transmit_address_suggestion (peer,
794 aa->addr, aa->addr_len,
800 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
801 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804 "Address %p ready for suggestion, block interval now %llu \n",
805 aa, aa->block_interval);
813 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
815 struct ATS_Address *aa = value;
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Resetting interval for peer `%s' address %p from %llu to 0\n",
819 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
821 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
822 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
828 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Received `%s' for peer `%s'\n",
835 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
843 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
844 enum GNUNET_ATS_PreferenceKind kind,
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Received `%s' for peer `%s'\n",
852 if (GNUNET_NO == handle->running)
855 /* Tell solver about update */
856 handle->s_pref (handle->solver, peer, kind, score);
860 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
862 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
863 char * entry_in = NULL;
864 char * entry_out = NULL;
865 char * quota_out_str;
869 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
874 case GNUNET_ATS_NET_UNSPECIFIED:
875 entry_out = "UNSPECIFIED_QUOTA_OUT";
876 entry_in = "UNSPECIFIED_QUOTA_IN";
878 case GNUNET_ATS_NET_LOOPBACK:
879 entry_out = "LOOPBACK_QUOTA_OUT";
880 entry_in = "LOOPBACK_QUOTA_IN";
882 case GNUNET_ATS_NET_LAN:
883 entry_out = "LAN_QUOTA_OUT";
884 entry_in = "LAN_QUOTA_IN";
886 case GNUNET_ATS_NET_WAN:
887 entry_out = "WAN_QUOTA_OUT";
888 entry_in = "WAN_QUOTA_IN";
890 case GNUNET_ATS_NET_WLAN:
891 entry_out = "WLAN_QUOTA_OUT";
892 entry_in = "WLAN_QUOTA_IN";
898 if ((entry_in == NULL) || (entry_out == NULL))
902 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
904 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
905 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
906 out_dest[c] = UINT32_MAX;
908 GNUNET_free (quota_out_str);
909 quota_out_str = NULL;
911 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
912 out_dest[c] = UINT32_MAX;
914 out_dest[c] = UINT32_MAX;
917 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
919 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
920 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
921 in_dest[c] = UINT32_MAX;
923 GNUNET_free (quota_in_str);
926 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
928 in_dest[c] = UINT32_MAX;
932 in_dest[c] = UINT32_MAX;
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
937 return GNUNET_ATS_NetworkTypeCount;
943 * Initialize address subsystem.
945 * @param cfg configuration to use
946 * @param stats the statistics handle to use
948 struct GAS_Addresses_Handle *
949 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
950 const struct GNUNET_STATISTICS_Handle *stats)
952 struct GAS_Addresses_Handle *ah;
953 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
954 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
955 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
960 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
962 handle->running = GNUNET_NO;
964 /* Initialize the addresses database */
965 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
966 GNUNET_assert (NULL != ah->addresses);
968 /* Figure out configured solution method */
969 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
971 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
972 ah->ats_mode = MODE_SIMPLISTIC;
976 for (c = 0; c < strlen (mode_str); c++)
977 mode_str[c] = toupper (mode_str[c]);
978 if (0 == strcmp (mode_str, "SIMPLISTIC"))
980 ah->ats_mode = MODE_SIMPLISTIC;
982 else if (0 == strcmp (mode_str, "MLP"))
984 ah->ats_mode = MODE_MLP;
986 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
987 ah->ats_mode = MODE_SIMPLISTIC;
992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
993 ah->ats_mode = MODE_SIMPLISTIC;
995 GNUNET_free (mode_str);
997 /* Start configured solution method */
998 switch (ah->ats_mode)
1001 /* Init the MLP solver with default values */
1003 ah->ats_mode = MODE_MLP;
1004 ah->s_init = &GAS_mlp_init;
1005 ah->s_add = &GAS_mlp_address_add;
1006 ah->s_update = &GAS_mlp_address_update;
1007 ah->s_get = &GAS_mlp_get_preferred_address;
1008 ah->s_pref = &GAS_mlp_address_change_preference;
1009 ah->s_del = &GAS_mlp_address_delete;
1010 ah->s_done = &GAS_mlp_done;
1016 case MODE_SIMPLISTIC:
1017 /* Init the simplistic solver with default values */
1018 ah->ats_mode = MODE_SIMPLISTIC;
1019 ah->s_init = &GAS_simplistic_init;
1020 ah->s_add = &GAS_simplistic_address_add;
1021 ah->s_update = &GAS_simplistic_address_update;
1022 ah->s_get = &GAS_simplistic_get_preferred_address;
1023 ah->s_pref = &GAS_simplistic_address_change_preference;
1024 ah->s_del = &GAS_simplistic_address_delete;
1025 ah->s_done = &GAS_simplistic_done;
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1033 GNUNET_assert (NULL != ah->s_init);
1034 GNUNET_assert (NULL != ah->s_add);
1035 GNUNET_assert (NULL != ah->s_update);
1036 GNUNET_assert (NULL != ah->s_get);
1037 GNUNET_assert (NULL != ah->s_pref);
1038 GNUNET_assert (NULL != ah->s_del);
1039 GNUNET_assert (NULL != ah->s_done);
1041 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1043 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count);
1044 if (NULL == ah->solver)
1046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1051 /* up and running */
1052 ah->running = GNUNET_YES;
1058 * Free memory of address.
1061 * @param key peer identity (unused)
1062 * @param value the 'struct ATS_Address' to free
1063 * @return GNUNET_OK (continue to iterate)
1066 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1068 struct ATS_Address *aa = value;
1070 destroy_address (aa);
1076 GAS_addresses_destroy_all ()
1078 if (GNUNET_NO == handle->running)
1081 if (handle->addresses != NULL)
1082 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1087 * Shutdown address subsystem.
1090 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1092 struct GAS_Addresses_Suggestion_Requests *cur;
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "Shutting down addresses\n");
1096 GNUNET_assert (NULL != handle);
1097 GAS_addresses_destroy_all ();
1098 handle->running = GNUNET_NO;
1099 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1100 handle->addresses = NULL;
1101 while (NULL != (cur = handle->r_head))
1103 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1106 handle->s_done (handle->solver);
1107 GNUNET_free (handle);
1108 /* Stop configured solution method */
1112 struct PeerIteratorContext
1114 GNUNET_ATS_Peer_Iterator it;
1116 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1121 const struct GNUNET_HashCode * key,
1124 struct PeerIteratorContext *ip_ctx = cls;
1125 struct GNUNET_PeerIdentity tmp;
1127 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1129 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1130 tmp.hashPubKey = (*key);
1131 ip_ctx->it (ip_ctx->it_cls, &tmp);
1138 * Return all peers currently known to ATS
1140 * @param p_it the iterator to call for every peer, callbach with id == NULL
1142 * @param p_it_cls the closure for the iterator
1145 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1147 struct PeerIteratorContext ip_ctx;
1152 GNUNET_assert (NULL != handle->addresses);
1154 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1158 ip_ctx.it_cls = p_it_cls;
1159 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1160 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1161 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1163 p_it (p_it_cls, NULL);
1166 struct PeerInfoIteratorContext
1168 GNUNET_ATS_PeerInfo_Iterator it;
1174 peerinfo_it (void *cls,
1175 const struct GNUNET_HashCode * key,
1178 struct PeerInfoIteratorContext *pi_ctx = cls;
1179 struct ATS_Address *addr = (struct ATS_Address *) value;
1180 struct GNUNET_ATS_Information *ats;
1183 if (NULL != pi_ctx->it)
1185 ats_count = assemble_ats_information (addr, &ats);
1187 pi_ctx->it (pi_ctx->it_cls,
1190 addr->addr, addr->addr_len,
1193 addr->assigned_bw_out,
1194 addr->assigned_bw_in);
1202 * Return all peers currently known to ATS
1204 * @param peer the respective peer
1205 * @param pi_it the iterator to call for every peer
1206 * @param pi_it_cls the closure for the iterator
1209 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1211 struct PeerInfoIteratorContext pi_ctx;
1212 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1213 GNUNET_assert (NULL != peer);
1214 GNUNET_assert (NULL != handle->addresses);
1216 return; /* does not make sense without callback */
1218 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1220 pi_ctx.it_cls = pi_it_cls;
1222 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1225 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1230 /* end of gnunet-service-ats_addresses.c */