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 * Update address in solver
129 GAS_solver_address_update s_update;
132 * Get address from solver
134 GAS_solver_get_preferred_address s_get;
137 * Delete address in solver
139 GAS_solver_address_delete s_del;
142 * Change preference for quality in solver
144 GAS_solver_address_change_preference s_pref;
149 GAS_solver_done s_done;
156 struct GAS_Addresses_Handle *handle;
160 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
162 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
163 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
166 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
167 ats[0].value = aa->atsp_utilization_out.value__;
168 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
169 ats[1].value = aa->atsp_utilization_in.value__;
170 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
171 ats[2].value = ntohl(aa->atsp_network_type);
172 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
173 ats[3].value = ntohl(aa->atsp_latency.rel_value);
174 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
175 ats[4].value = ntohl(aa->atsp_distance);
176 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
177 ats[5].value = ntohl (aa->atsp_cost_wan);
178 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
179 ats[6].value = ntohl (aa->atsp_cost_lan);
180 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
181 ats[7].value = ntohl (aa->atsp_cost_wlan);
186 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
188 struct ATS_Address *dest)
192 for (i = 0; i < ats_count; i++)
193 switch (ntohl (src[i].type))
195 case GNUNET_ATS_UTILIZATION_UP:
196 dest->atsp_utilization_out.value__ = src[i].value;
199 case GNUNET_ATS_UTILIZATION_DOWN:
200 dest->atsp_utilization_in.value__ = src[i].value;
203 case GNUNET_ATS_QUALITY_NET_DELAY:
204 dest->atsp_latency.rel_value = ntohl (src[i].value);
207 case GNUNET_ATS_QUALITY_NET_DISTANCE:
208 dest->atsp_distance = ntohl (src[i].value);
211 case GNUNET_ATS_COST_WAN:
212 dest->atsp_cost_wan = ntohl (src[i].value);
215 case GNUNET_ATS_COST_LAN:
216 dest->atsp_cost_lan = ntohl (src[i].value);
219 case GNUNET_ATS_COST_WLAN:
220 dest->atsp_cost_wlan = ntohl (src[i].value);
223 case GNUNET_ATS_NETWORK_TYPE:
224 dest->atsp_network_type = ntohl (src[i].value);
227 case GNUNET_ATS_ARRAY_TERMINATOR:
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231 "Received unsupported ATS type %u\n", ntohl (src[i].type));
239 * Free the given address
240 * @param addr address to destroy
243 free_address (struct ATS_Address *addr)
245 GNUNET_free (addr->plugin);
250 * Create a ATS_address with the given information
252 * @param plugin_name plugin
253 * @param plugin_addr address
254 * @param plugin_addr_len address length
255 * @param session_id session
256 * @return the ATS_Address
258 static struct ATS_Address *
259 create_address (const struct GNUNET_PeerIdentity *peer,
260 const char *plugin_name,
261 const void *plugin_addr, size_t plugin_addr_len,
264 struct ATS_Address *aa = NULL;
266 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
268 aa->addr_len = plugin_addr_len;
270 memcpy (&aa[1], plugin_addr, plugin_addr_len);
271 aa->plugin = GNUNET_strdup (plugin_name);
272 aa->session_id = session_id;
278 * Destroy the given address.
280 * @param addr address to destroy
281 * @return GNUNET_YES if bandwidth allocations should be recalcualted
284 destroy_address (struct ATS_Address *addr)
289 GNUNET_assert (GNUNET_YES ==
290 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
291 &addr->peer.hashPubKey,
294 handle->s_del (handle->solver, handle->addresses, addr);
300 struct CompareAddressContext
302 const struct ATS_Address *search;
304 /* exact_address != NULL if address and session is equal */
305 struct ATS_Address *exact_address;
306 /* exact_address != NULL if address and session is 0 */
307 struct ATS_Address *base_address;
312 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
314 struct CompareAddressContext *cac = cls;
315 struct ATS_Address *aa = value;
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
319 aa->addr_len, aa->session_id,
320 cac->search->addr_len, cac->search->session_id);
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);
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Found exact address: %s base address: %s\n",
406 (cac.exact_address != NULL) ? "YES" : "NO",
407 (cac.base_address != NULL) ? "YES" : "NO");
409 if (cac.exact_address == NULL)
410 return cac.base_address;
411 return cac.exact_address;
415 static struct ATS_Address *
416 lookup_address (const struct GNUNET_PeerIdentity *peer,
417 const char *plugin_name, const void *plugin_addr,
418 size_t plugin_addr_len, uint32_t session_id,
419 const struct GNUNET_ATS_Information *atsi,
422 struct ATS_Address *aa;
423 struct ATS_Address *old;
425 aa = create_address (peer,
427 plugin_addr, plugin_addr_len,
430 aa->mlp_information = NULL;
432 /* Get existing address or address with session == 0 */
433 old = find_address (peer, aa);
439 else if (old->session_id != session_id)
449 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
451 struct CompareAddressContext *cac = cls;
452 struct ATS_Address *aa = value;
454 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
456 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
458 cac->exact_address = aa;
467 * Find an existing equivalent address record.
468 * Compares by peer identity and network address AND by session ID
469 * (one of the two must match).
471 * @param peer peer to lookup addresses for
472 * @param addr existing address record
473 * @return existing address record, NULL for none
475 static struct ATS_Address *
476 find_exact_address (const struct GNUNET_PeerIdentity *peer,
477 const struct ATS_Address *addr)
479 struct CompareAddressContext cac;
481 cac.exact_address = NULL;
483 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
484 &compare_address_session_it, &cac);
485 return cac.exact_address;
490 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
491 const char *plugin_name, const void *plugin_addr,
492 size_t plugin_addr_len, uint32_t session_id,
493 const struct GNUNET_ATS_Information *atsi,
496 struct ATS_Address *aa;
497 struct ATS_Address *old;
498 unsigned int ats_res;
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Received `%s' for peer `%s'\n",
505 if (GNUNET_NO == handle->running)
508 GNUNET_assert (NULL != handle->addresses);
510 aa = create_address (peer,
512 plugin_addr, plugin_addr_len,
514 aa->mlp_information = NULL;
515 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "While adding address: had %u ATS elements to add, could only add %u\n",
519 atsi_count, ats_res);
522 /* Get existing address or address with session == 0 */
523 old = find_address (peer, aa);
526 /* We have a new address */
527 GNUNET_assert (GNUNET_OK ==
528 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
529 &peer->hashPubKey, aa,
530 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
532 GNUNET_i2s (peer), session_id, aa);
533 /* Tell solver about update */
534 handle->s_update (handle->solver, handle->addresses, aa);
538 if (old->session_id != 0)
540 /* This address and session is already existing */
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
543 GNUNET_i2s (peer), plugin_name, session_id);
548 /* We have an address without an session, update this address */
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Updated existing address for peer `%s' %p with new session %u\n",
551 GNUNET_i2s (peer), old, session_id);
552 old->session_id = session_id;
553 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, old)))
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "While updating address: had %u ATS elements to add, could only add %u\n",
557 atsi_count, ats_res);
559 GNUNET_free (aa->plugin);
561 handle->s_update (handle->solver, handle->addresses, old);
566 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
567 const char *plugin_name, const void *plugin_addr,
568 size_t plugin_addr_len, uint32_t session_id,
569 const struct GNUNET_ATS_Information *atsi,
572 struct ATS_Address *old;
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Received `%s' for peer `%s'\n",
580 if (GNUNET_NO == handle->running)
583 GNUNET_assert (NULL != handle->addresses);
585 /* Get existing address */
586 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
587 session_id, atsi, atsi_count);
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
591 GNUNET_i2s (peer), plugin_name, session_id);
596 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, old)))
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "While adding address: had %u ATS elements to add, could only add %u\n",
600 atsi_count, ats_res);
603 /* Tell solver about update */
604 handle->s_update (handle->solver, handle->addresses, old);
611 * If session != 0, just the session is deleted, the address itself still exists
612 * If session == 0, remove full address
613 * If session == 0 and addrlen == 0, destroy inbound address
617 * @param value the 'struct ATS_Address'
618 * @return GNUNET_OK (continue to iterate)
621 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
623 const struct ATS_Address *info = cls;
624 struct ATS_Address *aa = value;
627 memcmp (&aa->peer, &info->peer,
628 sizeof (struct GNUNET_PeerIdentity)));
629 /* session == 0, remove full address */
630 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
631 (aa->addr_len == info->addr_len) &&
632 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Deleting address for peer `%s': `%s' %u\n",
637 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
639 destroy_address (aa);
640 // FIXME if (GNUNET_YES == destroy_address (aa))recalculate_assigned_bw ();
643 /* session != 0, just remove session */
644 if (aa->session_id != info->session_id)
645 return GNUNET_OK; /* irrelevant */
646 if (aa->session_id != 0)
647 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Deleting session for peer `%s': `%s' %u\n",
651 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
654 if (GNUNET_YES == aa->active)
656 aa->active = GNUNET_NO;
657 //FIXME recalculate_assigned_bw ();
660 /* session == 0 and addrlen == 0 : destroy address */
661 if (aa->addr_len == 0)
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Deleting session and address for peer `%s': `%s' %u\n",
665 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
666 (void) destroy_address (aa);
670 /* session was set to 0, update address */
672 if (handle->ats_mode == MODE_MLP)
673 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
682 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
683 const char *plugin_name, const void *plugin_addr,
684 size_t plugin_addr_len, uint32_t session_id)
686 struct ATS_Address *aa;
687 struct ATS_Address *old;
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Received `%s' for peer `%s'\n",
694 if (GNUNET_NO == handle->running)
697 /* Get existing address */
698 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
699 session_id, NULL, 0);
702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
703 GNUNET_i2s (peer), plugin_name, session_id);
707 GNUNET_break (0 < strlen (plugin_name));
708 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
709 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
710 &destroy_by_session_id, aa);
716 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
717 const char *plugin_name, const void *plugin_addr,
718 size_t plugin_addr_len, uint32_t session_id, int in_use)
720 struct ATS_Address *old;
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Received `%s' for peer `%s'\n",
727 if (GNUNET_NO == handle->running)
728 return GNUNET_SYSERR;
730 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
734 "Trying to set unknown address `%s', %s %u %s \n",
736 plugin_name, session_id,
737 (GNUNET_NO == in_use) ? "NO" : "YES");
739 return GNUNET_SYSERR;
741 if (old->used == in_use)
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
745 "Address in use called multiple times for peer `%s': %s -> %s \n",
747 (GNUNET_NO == old->used) ? "NO" : "YES",
748 (GNUNET_NO == in_use) ? "NO" : "YES");
749 return GNUNET_SYSERR;
753 /* Tell solver about update */
754 handle->s_update (handle->solver, handle->addresses, old);
761 * Cancel address suggestions for a peer
763 * @param peer the respective peer
766 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
768 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
775 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
782 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
783 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
786 GAS_addresses_handle_backoff_reset (peer);
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
789 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
795 * Add an address suggestions for a peer
797 * @param peer the respective peer
800 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
802 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
803 struct ATS_Address *aa;
804 struct GNUNET_ATS_Information *ats;
805 unsigned int ats_count;
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Received `%s' for peer `%s'\n",
812 if (GNUNET_NO == handle->running)
816 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
817 break; /* already suggesting */
822 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
824 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
827 /* Get prefered address from solver */
828 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
839 ats_count = assemble_ats_information (aa, &ats);
840 GAS_scheduling_transmit_address_suggestion (peer,
842 aa->addr, aa->addr_len,
848 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
849 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Address %p ready for suggestion, block interval now %llu \n",
853 aa, aa->block_interval);
861 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
863 struct ATS_Address *aa = value;
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Resetting interval for peer `%s' address %p from %llu to 0\n",
867 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
869 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
870 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
876 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Received `%s' for peer `%s'\n",
883 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
891 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
892 enum GNUNET_ATS_PreferenceKind kind,
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Received `%s' for peer `%s'\n",
900 if (GNUNET_NO == handle->running)
903 /* Tell solver about update */
904 handle->s_pref (handle->solver, peer, kind, score);
908 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
910 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
911 char * entry_in = NULL;
912 char * entry_out = NULL;
913 char * quota_out_str;
917 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
922 case GNUNET_ATS_NET_UNSPECIFIED:
923 entry_out = "UNSPECIFIED_QUOTA_OUT";
924 entry_in = "UNSPECIFIED_QUOTA_IN";
926 case GNUNET_ATS_NET_LOOPBACK:
927 entry_out = "LOOPBACK_QUOTA_OUT";
928 entry_in = "LOOPBACK_QUOTA_IN";
930 case GNUNET_ATS_NET_LAN:
931 entry_out = "LAN_QUOTA_OUT";
932 entry_in = "LAN_QUOTA_IN";
934 case GNUNET_ATS_NET_WAN:
935 entry_out = "WAN_QUOTA_OUT";
936 entry_in = "WAN_QUOTA_IN";
938 case GNUNET_ATS_NET_WLAN:
939 entry_out = "WLAN_QUOTA_OUT";
940 entry_in = "WLAN_QUOTA_IN";
946 if ((entry_in == NULL) || (entry_out == NULL))
950 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
952 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
953 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
954 out_dest[c] = UINT32_MAX;
956 GNUNET_free (quota_out_str);
957 quota_out_str = NULL;
959 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
960 out_dest[c] = UINT32_MAX;
962 out_dest[c] = UINT32_MAX;
965 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
967 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
968 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
969 in_dest[c] = UINT32_MAX;
971 GNUNET_free (quota_in_str);
974 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
976 in_dest[c] = UINT32_MAX;
980 in_dest[c] = UINT32_MAX;
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
985 return GNUNET_ATS_NetworkTypeCount;
991 * Initialize address subsystem.
993 * @param cfg configuration to use
994 * @param stats the statistics handle to use
996 struct GAS_Addresses_Handle *
997 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
998 const struct GNUNET_STATISTICS_Handle *stats)
1000 struct GAS_Addresses_Handle *ah;
1001 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1002 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1003 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1008 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1010 handle->running = GNUNET_NO;
1012 /* Initialize the addresses database */
1013 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1014 GNUNET_assert (NULL != ah->addresses);
1016 /* Figure out configured solution method */
1017 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1020 ah->ats_mode = MODE_SIMPLISTIC;
1024 for (c = 0; c < strlen (mode_str); c++)
1025 mode_str[c] = toupper (mode_str[c]);
1026 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1028 ah->ats_mode = MODE_SIMPLISTIC;
1030 else if (0 == strcmp (mode_str, "MLP"))
1032 ah->ats_mode = MODE_MLP;
1034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1035 ah->ats_mode = MODE_SIMPLISTIC;
1040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1041 ah->ats_mode = MODE_SIMPLISTIC;
1043 GNUNET_free (mode_str);
1045 /* Start configured solution method */
1046 switch (ah->ats_mode)
1049 /* Init the MLP solver with default values */
1051 ah->ats_mode = MODE_MLP;
1052 ah->s_init = &GAS_mlp_init;
1053 ah->s_update = &GAS_mlp_address_update;
1054 ah->s_get = &GAS_mlp_get_preferred_address;
1055 ah->s_pref = &GAS_mlp_address_change_preference;
1056 ah->s_del = &GAS_mlp_address_delete;
1057 ah->s_done = &GAS_mlp_done;
1063 case MODE_SIMPLISTIC:
1064 /* Init the simplistic solver with default values */
1065 ah->ats_mode = MODE_SIMPLISTIC;
1066 ah->s_init = &GAS_simplistic_init;
1067 ah->s_update = &GAS_simplistic_address_update;
1068 ah->s_get = &GAS_simplistic_get_preferred_address;
1069 ah->s_pref = &GAS_simplistic_address_change_preference;
1070 ah->s_del = &GAS_simplistic_address_delete;
1071 ah->s_done = &GAS_simplistic_done;
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1079 GNUNET_assert (NULL != ah->s_init);
1080 GNUNET_assert (NULL != ah->s_update);
1081 GNUNET_assert (NULL != ah->s_get);
1082 GNUNET_assert (NULL != ah->s_pref);
1083 GNUNET_assert (NULL != ah->s_del);
1084 GNUNET_assert (NULL != ah->s_done);
1086 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1088 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count);
1089 if (NULL == ah->solver)
1091 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1096 /* up and running */
1097 ah->running = GNUNET_YES;
1103 * Free memory of address.
1106 * @param key peer identity (unused)
1107 * @param value the 'struct ATS_Address' to free
1108 * @return GNUNET_OK (continue to iterate)
1111 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1113 struct ATS_Address *aa = value;
1115 destroy_address (aa);
1121 GAS_addresses_destroy_all ()
1123 if (GNUNET_NO == handle->running)
1126 if (handle->addresses != NULL)
1127 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1132 * Shutdown address subsystem.
1135 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1137 struct GAS_Addresses_Suggestion_Requests *cur;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Shutting down addresses\n");
1141 GNUNET_assert (NULL != handle);
1142 GAS_addresses_destroy_all ();
1143 handle->running = GNUNET_NO;
1144 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1145 handle->addresses = NULL;
1146 while (NULL != (cur = handle->r_head))
1148 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1152 GNUNET_free (handle);
1153 /* Stop configured solution method */
1157 struct PeerIteratorContext
1159 GNUNET_ATS_Peer_Iterator it;
1161 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1166 const struct GNUNET_HashCode * key,
1169 struct PeerIteratorContext *ip_ctx = cls;
1170 struct GNUNET_PeerIdentity tmp;
1172 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1174 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1175 tmp.hashPubKey = (*key);
1176 ip_ctx->it (ip_ctx->it_cls, &tmp);
1183 * Return all peers currently known to ATS
1185 * @param p_it the iterator to call for every peer, callbach with id == NULL
1187 * @param p_it_cls the closure for the iterator
1190 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1192 struct PeerIteratorContext ip_ctx;
1197 GNUNET_assert (NULL != handle->addresses);
1199 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1203 ip_ctx.it_cls = p_it_cls;
1204 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1205 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1206 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1208 p_it (p_it_cls, NULL);
1211 struct PeerInfoIteratorContext
1213 GNUNET_ATS_PeerInfo_Iterator it;
1219 peerinfo_it (void *cls,
1220 const struct GNUNET_HashCode * key,
1223 struct PeerInfoIteratorContext *pi_ctx = cls;
1224 struct ATS_Address *addr = (struct ATS_Address *) value;
1225 struct GNUNET_ATS_Information *ats;
1228 if (NULL != pi_ctx->it)
1230 ats_count = assemble_ats_information (addr, &ats);
1232 pi_ctx->it (pi_ctx->it_cls,
1235 addr->addr, addr->addr_len,
1238 addr->assigned_bw_out,
1239 addr->assigned_bw_in);
1247 * Return all peers currently known to ATS
1249 * @param peer the respective peer
1250 * @param pi_it the iterator to call for every peer
1251 * @param pi_it_cls the closure for the iterator
1254 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1256 struct PeerInfoIteratorContext pi_ctx;
1257 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1258 GNUNET_assert (NULL != peer);
1259 GNUNET_assert (NULL != handle->addresses);
1261 return; /* does not make sense without callback */
1263 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1265 pi_ctx.it_cls = pi_it_cls;
1267 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1270 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1275 /* end of gnunet-service-ats_addresses.c */