X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fats%2Fgnunet-service-ats_addresses.c;h=fb9bad037049020cc516ffe003e49c3aab27bf06;hb=82c4c89493dcbfc6ee7fd1232a9088d02e2cd0a2;hp=c1975e68647ee76b2290930cde664273f0c6abb7;hpb=7e2f0d10d9ac29559fb05e856621d8af38273a37;p=oweals%2Fgnunet.git diff --git a/src/ats/gnunet-service-ats_addresses.c b/src/ats/gnunet-service-ats_addresses.c index c1975e686..fb9bad037 100644 --- a/src/ats/gnunet-service-ats_addresses.c +++ b/src/ats/gnunet-service-ats_addresses.c @@ -31,73 +31,40 @@ #include "gnunet-service-ats_performance.h" #include "gnunet-service-ats_scheduling.h" #include "gnunet-service-ats_reservations.h" +#if HAVE_LIBGLPK +#include "gnunet-service-ats_addresses_mlp.h" +#endif -struct ATS_Address -{ - struct GNUNET_PeerIdentity peer; - - size_t addr_len; - - uint32_t session_id; - - uint32_t ats_count; - - const void *addr; - - char *plugin; - - struct GNUNET_ATS_Information *ats; - - struct GNUNET_TIME_Relative atsp_latency; - - struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_in; - - struct GNUNET_BANDWIDTH_Value32NBO atsp_utilization_out; - - uint32_t atsp_distance; - - uint32_t atsp_cost_wan; - - uint32_t atsp_cost_lan; - - uint32_t atsp_cost_wlan; - - struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_in; - - struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_out; - - /** - * Is this the active address for this peer? - */ - int active; - -}; +#define VERBOSE GNUNET_NO -struct ATS_Network +enum ATS_Mode { - struct ATS_Network * next; - - struct ATS_Network * prev; + /* + * Assign each peer an equal amount of bandwidth (bw) + * + * bw_per_peer = bw_total / #active addresses + */ + SIMPLE, - struct sockaddr *network; - struct sockaddr *netmask; - socklen_t length; + /* + * Use MLP solver to assign bandwidth + */ + MLP }; - -struct ATS_Network * net_head; - -struct ATS_Network * net_tail; - static struct GNUNET_CONTAINER_MultiHashMap *addresses; +#if HAVE_LIBGLPK +static struct GAS_MLP_Handle *mlp; +#endif + static unsigned long long wan_quota_in; static unsigned long long wan_quota_out; static unsigned int active_addr_count; -static GNUNET_SCHEDULER_TaskIdentifier interface_task; +static int ats_mode; /** @@ -110,15 +77,19 @@ static GNUNET_SCHEDULER_TaskIdentifier interface_task; * @return GNUNET_OK (continue to iterate) */ static int -update_bw_it (void *cls, const GNUNET_HashCode * key, void *value) +update_bw_simple_it (void *cls, const GNUNET_HashCode * key, void *value) { struct ATS_Address *aa = value; if (GNUNET_YES != aa->active) return GNUNET_OK; GNUNET_assert (active_addr_count > 0); + + + /* Simple method */ aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count); aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n", GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__), ntohl (aa->assigned_bw_out.value__)); @@ -148,7 +119,50 @@ recalculate_assigned_bw () 1, GNUNET_NO); GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count, GNUNET_NO); - GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL); + + GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL); +} + +/** + * Free the given address + * @param addr address to destroy + */ +static void +free_address (struct ATS_Address *addr) +{ + GNUNET_free_non_null (addr->ats); + GNUNET_free (addr->plugin); + GNUNET_free (addr); +} + +/** + * Create a ATS_address with the given information + * @param peer peer + * @param plugin_name plugin + * @param plugin_addr address + * @param plugin_addr_len address length + * @param session_id session + * @return the ATS_Address + */ +static struct ATS_Address * +create_address (const struct GNUNET_PeerIdentity *peer, + const char *plugin_name, + const void *plugin_addr, size_t plugin_addr_len, + uint32_t session_id) +{ + struct ATS_Address *aa = NULL; + + aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len); + aa->peer = *peer; + aa->addr_len = plugin_addr_len; + aa->addr = &aa[1]; + memcpy (&aa[1], plugin_addr, plugin_addr_len); + aa->plugin = GNUNET_strdup (plugin_name); + aa->session_id = session_id; + aa->mlp_information = NULL; + aa->next = NULL; + aa->prev = NULL; + return aa; } @@ -168,15 +182,19 @@ destroy_address (struct ATS_Address *addr) GNUNET_CONTAINER_multihashmap_remove (addresses, &addr->peer.hashPubKey, addr)); + +#if HAVE_LIBGLPK + if (ats_mode == MLP) + GAS_mlp_address_delete (mlp, addresses, addr); +#endif + if (GNUNET_YES == addr->active) { active_addr_count--; addr->active = GNUNET_NO; ret = GNUNET_YES; } - GNUNET_free_non_null (addr->ats); - GNUNET_free (addr->plugin); - GNUNET_free (addr); + free_address (addr); return ret; } @@ -184,7 +202,11 @@ destroy_address (struct ATS_Address *addr) struct CompareAddressContext { const struct ATS_Address *search; - struct ATS_Address *result; + + /* exact_address != NULL if address and session is equal */ + struct ATS_Address *exact_address; + /* exact_address != NULL if address and session is 0 */ + struct ATS_Address *base_address; }; @@ -193,15 +215,35 @@ compare_address_it (void *cls, const GNUNET_HashCode * key, void *value) { struct CompareAddressContext *cac = cls; struct ATS_Address *aa = value; +/* + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Comparing to: %s %s %u session %u\n", + GNUNET_i2s (&aa->peer), aa->plugin, aa->addr_len, aa->session_id); + +*/ + /* find an exact matching address: aa->addr == cac->search->addr && aa->session == cac->search->session */ + if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin))) + { + if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id)) + { + cac->exact_address = aa; + } + } + + /* find an matching address: aa->addr == cac->search->addr && aa->session == 0 */ + /* this address can be used to be updated */ + if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin))) + { + if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0)) + { + cac->base_address = aa; + } + } - if (((aa->addr_len != cac->search->addr_len) || - (0 != strcmp (aa->plugin, cac->search->plugin)) || - (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) && - ((aa->session_id != cac->search->session_id) || - (cac->search->session_id == 0))) + if (cac->exact_address == NULL) return GNUNET_YES; - cac->result = aa; - return GNUNET_NO; + else + return GNUNET_NO; } @@ -220,11 +262,62 @@ find_address (const struct GNUNET_PeerIdentity *peer, { struct CompareAddressContext cac; - cac.result = NULL; + cac.exact_address = NULL; + cac.base_address = NULL; cac.search = addr; GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, &compare_address_it, &cac); - return cac.result; + +/* + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "exact address: %s base address: %s\n", + (cac.exact_address != NULL) ? "YES" : "NO", + (cac.base_address != NULL) ? "YES" : "NO"); +*/ + if (cac.exact_address == NULL) + return cac.base_address; + return cac.exact_address; +} + + +static int +compare_address_session_it (void *cls, const GNUNET_HashCode * key, void *value) +{ + struct CompareAddressContext *cac = cls; + struct ATS_Address *aa = value; + + if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin))) + { + if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id)) + { + cac->exact_address = aa; + return GNUNET_NO; + } + } + return GNUNET_YES; +} + + +/** + * Find an existing equivalent address record. + * Compares by peer identity and network address AND by session ID + * (one of the two must match). + * + * @param peer peer to lookup addresses for + * @param addr existing address record + * @return existing address record, NULL for none + */ +struct ATS_Address * +find_exact_address (const struct GNUNET_PeerIdentity *peer, + const struct ATS_Address *addr) +{ + struct CompareAddressContext cac; + + cac.exact_address = NULL; + cac.search = addr; + GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, + &compare_address_session_it, &cac); + return cac.exact_address; } @@ -239,16 +332,22 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer, struct ATS_Address *old; uint32_t i; - aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len); + aa = create_address (peer, + plugin_name, + plugin_addr, plugin_addr_len, + session_id); + + aa->mlp_information = NULL; aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information)); - aa->peer = *peer; - aa->addr_len = plugin_addr_len; aa->ats_count = atsi_count; memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information)); - aa->addr = &aa[1]; - memcpy (&aa[1], plugin_addr, plugin_addr_len); - aa->plugin = GNUNET_strdup (plugin_name); - aa->session_id = session_id; + +#if DEBUG_ATS + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n", + GNUNET_i2s (peer), + session_id); +#endif + /* Get existing address or address with session == 0 */ old = find_address (peer, aa); if (old == NULL) { @@ -256,15 +355,20 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer, GNUNET_CONTAINER_multihashmap_put (addresses, &peer->hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); +#if DEBUG_ATS GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n", GNUNET_i2s (peer), aa); +#endif old = aa; } else { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Updated existing address for peer `%s' %X \n", - GNUNET_i2s (peer), old); +#if DEBUG_ATS + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Updated existing address for peer `%s' %p old session %u new session %u\n", + GNUNET_i2s (peer), old, + old->session_id, session_id); +#endif GNUNET_free_non_null (old->ats); old->session_id = session_id; old->ats = NULL; @@ -298,18 +402,29 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer, case GNUNET_ATS_COST_WLAN: old->atsp_cost_wlan = ntohl (atsi[i].value); break; + case GNUNET_ATS_NETWORK_TYPE: + old->atsp_network_type = ntohl (atsi[i].value); + break; + default: GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Received unsupported ATS type %u\n", ntohl (atsi[i].type)); GNUNET_break (0); break; } +#if HAVE_LIBGLPK + if (ats_mode == MLP) + GAS_mlp_address_update (mlp, addresses, old); +#endif } /** - * Update a bandwidth assignment for a peer. This trivial method currently - * simply assigns the same share to all active connections. + * Delete an address + * + * If session != 0, just the session is deleted, the address itself still exists + * If session == 0, remove full address + * If session == 0 and addrlen == 0, destroy inbound address * * @param cls unused * @param key unused @@ -325,22 +440,31 @@ destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value) GNUNET_assert (0 == memcmp (&aa->peer, &info->peer, sizeof (struct GNUNET_PeerIdentity))); + /* session == 0, remove full address */ if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) && (aa->addr_len == info->addr_len) && (0 == memcmp (info->addr, aa->addr, aa->addr_len))) { +#if VERBOSE GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Deleting address for peer `%s': `%s'\n", - GNUNET_i2s (&aa->peer), aa->plugin); + "Deleting address for peer `%s': `%s' %u\n", + GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id); +#endif if (GNUNET_YES == destroy_address (aa)) recalculate_assigned_bw (); return GNUNET_OK; } + /* session != 0, just remove session */ if (aa->session_id != info->session_id) return GNUNET_OK; /* irrelevant */ if (aa->session_id != 0) GNUNET_break (0 == strcmp (info->plugin, aa->plugin)); /* session died */ +#if VERBOSE + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Deleting session for peer `%s': `%s' %u\n", + GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id); +#endif aa->session_id = 0; if (GNUNET_YES == aa->active) @@ -352,27 +476,40 @@ destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value) /* session == 0 and addrlen == 0 : destroy address */ if (aa->addr_len == 0) + { +#if VERBOSE + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Deleting session and address for peer `%s': `%s' %u\n", + GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id); +#endif (void) destroy_address (aa); + } + else + { + /* session was set to 0, update address */ +#if HAVE_LIBGLPK + if (ats_mode == MLP) + GAS_mlp_address_update (mlp, addresses, aa); +#endif + } return GNUNET_OK; } - void GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer, const char *plugin_name, const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id) { - struct ATS_Address aa; + struct ATS_Address *aa; GNUNET_break (0 < strlen (plugin_name)); - aa.peer = *peer; - aa.addr_len = plugin_addr_len; - aa.addr = plugin_addr; - aa.plugin = (char *) plugin_name; - aa.session_id = session_id; + aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id); + GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, - &destroy_by_session_id, &aa); + &destroy_by_session_id, aa); + + free_address (aa); } @@ -427,20 +564,48 @@ GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer, const char *plugin_name, const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id, int in_use) { - +#if DEBUG_ATS GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE", GNUNET_i2s (peer), in_use); +#endif + + struct ATS_Address *aa; + struct ATS_Address *old; + + + aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id); + old = find_exact_address (peer, aa); + free_address (aa); + + GNUNET_assert (old != NULL); + GNUNET_assert (old->used != in_use); + old->used = in_use; + +#if HAVE_LIBGLPK + if (ats_mode == MLP) + GAS_mlp_address_update (mlp, addresses, old); +#endif } -void -GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer) + +void request_address_mlp (const struct GNUNET_PeerIdentity *peer) { struct ATS_Address *aa; - aa = NULL; - GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, - &find_address_it, &aa); + +#if HAVE_GLPK + /* Get preferred address from MLP */ + struct ATS_PreferedAddress * paddr = NULL; + paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer); + aa = paddr->address; + aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out); + /* FIXME use bw in value */ + paddr->bandwidth_in = paddr->bandwidth_out; + aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in); + GNUNET_free (paddr); +#endif + if (aa == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -451,7 +616,19 @@ GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer) { aa->active = GNUNET_YES; active_addr_count++; - recalculate_assigned_bw (); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n", + GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__), + ntohl (aa->assigned_bw_out.value__)); + GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr, + aa->addr_len, aa->session_id, + aa->ats, aa->ats_count, + aa->assigned_bw_out, + aa->assigned_bw_in); + GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in); + GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len, + aa->ats, aa->ats_count, aa->assigned_bw_out, + aa->assigned_bw_in); } else { @@ -462,225 +639,83 @@ GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer) aa->assigned_bw_out, aa->assigned_bw_in); } -} - -// FIXME: this function should likely end up in the LP-subsystem and -// not with 'addresses' in the future... -void -GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer, - enum GNUNET_ATS_PreferenceKind kind, - float score) -{ - // do nothing for now... } -/** - * Returns where the address is located: LAN or WAN or ... - * @param addr address - * @param addrlen address length - * @return location as GNUNET_ATS_Information - */ - -struct GNUNET_ATS_Information -GAS_addresses_type (const struct sockaddr * addr, socklen_t addrlen) +void request_address_simple (const struct GNUNET_PeerIdentity *peer) { - struct GNUNET_ATS_Information ats; - struct ATS_Network * cur = net_head; - int type = GNUNET_ATS_NET_UNSPECIFIED; - - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Checking type of address `%s'\n", GNUNET_a2s(addr, addrlen)); - - /* IPv4 loopback check */ - if (addr->sa_family == AF_INET) - { - struct sockaddr_in * a4 = (struct sockaddr_in *) addr; + struct ATS_Address *aa; + aa = NULL; - if (((a4->sin_addr.s_addr & htonl(0xff000000)) & htonl (0x7f000000)) == htonl (0x7f000000)) - type = GNUNET_ATS_NET_LOOPBACK; - } - /* IPv6 loopback check */ - if (addr->sa_family == AF_INET6) + /* Get address with: stick to current address, lower distance, lower latency */ + GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, + &find_address_it, &aa); + if (aa == NULL) { - struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr; - if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr)) - type = GNUNET_ATS_NET_LOOPBACK; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer)); + return; } - /* Check local networks */ - while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED)) + if (aa->active == GNUNET_NO) { - if (addrlen != cur->length) - { - cur = cur->next; - continue; - } - - if (addr->sa_family == AF_INET) - { - struct sockaddr_in * a4 = (struct sockaddr_in *) addr; - struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network; - struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask; - - if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr) & net4->sin_addr.s_addr) == net4->sin_addr.s_addr) - { - char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n", - GNUNET_a2s ((const struct sockaddr *)a4, addrlen), - net); - GNUNET_free (net); - type = GNUNET_ATS_NET_LAN; - } - } - if (addr->sa_family == AF_INET6) + aa->active = GNUNET_YES; + active_addr_count++; + if (ats_mode == SIMPLE) { - struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr; - struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network; - struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask; - - int res = GNUNET_YES; - int c = 0; - for (c = 0; c < 4; c++) - { - if (((a6->sin6_addr.__in6_u.__u6_addr32[c] & mask6->sin6_addr.__in6_u.__u6_addr32[c]) | net6->sin6_addr.__in6_u.__u6_addr32[c]) != net6->sin6_addr.__in6_u.__u6_addr32[c]) - res = GNUNET_NO; - } - - if (res == GNUNET_YES) - { - char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n", - GNUNET_a2s ((const struct sockaddr *) a6, addrlen), - net); - GNUNET_free (net); - type = GNUNET_ATS_NET_LAN; - } + recalculate_assigned_bw (); } - cur = cur->next; } - - /* local network found for this address, default: WAN */ - if (type == GNUNET_ATS_NET_UNSPECIFIED) - type = GNUNET_ATS_NET_WAN; - - ats.type = htonl (GNUNET_ATS_NETWORK_TYPE); - ats.value = htonl (type); - return ats; + else + { + /* just to be sure... */ + GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr, + aa->addr_len, aa->session_id, + aa->ats, aa->ats_count, + aa->assigned_bw_out, + aa->assigned_bw_in); + } } -static int -interface_proc (void *cls, const char *name, - int isDefault, - const struct sockaddr * - addr, - const struct sockaddr * - broadcast_addr, - const struct sockaddr * - netmask, socklen_t addrlen) + +void +GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer) { - /* Calculate network */ - struct ATS_Network *net = NULL; - if (addr->sa_family == AF_INET) + if (ats_mode == SIMPLE) { - struct sockaddr_in *addr4 = (struct sockaddr_in *) addr; - struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask; - struct sockaddr_in *tmp = NULL; - struct sockaddr_in network4; - - net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in)); - tmp = (struct sockaddr_in *) &net[1]; - net->network = (struct sockaddr *) &tmp[0]; - net->netmask = (struct sockaddr *) &tmp[1]; - net->length = addrlen; - - network4.sin_family = AF_INET; - network4.sin_port = htons (0); -#if HAVE_SOCKADDR_IN_SIN_LEN - network4.sin_len = sizeof (network4); -#endif - network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr); - - memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in)); - memcpy (net->network, &network4, sizeof (struct sockaddr_in)); - - char * netmask = strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n", - GNUNET_a2s((struct sockaddr *) net->network, addrlen), - netmask); - GNUNET_free (netmask); - + request_address_simple (peer); } - - if (addr->sa_family == AF_INET6) + if (ats_mode == MLP) { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr; - struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask; - struct sockaddr_in6 * tmp = NULL; - struct sockaddr_in6 network6; - - net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6)); - tmp = (struct sockaddr_in6 *) &net[1]; - net->network = (struct sockaddr *) &tmp[0]; - net->netmask = (struct sockaddr *) &tmp[1]; - net->length = addrlen; - - network6.sin6_family = AF_INET6; - network6.sin6_port = htons (0); -#if HAVE_SOCKADDR_IN_SIN_LEN - network6.sin6_len = sizeof (network6); -#endif - int c = 0; - for (c = 0; c < 4; c++) - { - network6.sin6_addr.__in6_u.__u6_addr32[c] = addr6->sin6_addr.__in6_u.__u6_addr32[c] & netmask6->sin6_addr.__in6_u.__u6_addr32[c]; - } - - memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6)); - memcpy (net->network, &network6, sizeof (struct sockaddr_in6)); - - char * netmask = strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n", - GNUNET_a2s((struct sockaddr *) net->network, addrlen), - netmask); - GNUNET_free (netmask); + request_address_mlp(peer); } - - /* Store in list */ - if (net != NULL) - GNUNET_CONTAINER_DLL_insert(net_head, net_tail, net); - - return GNUNET_OK; } -static void -delete_networks () + +// FIXME: this function should likely end up in the LP-subsystem and +// not with 'addresses' in the future... +void +GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer, + enum GNUNET_ATS_PreferenceKind kind, + float score) { - struct ATS_Network * cur = net_head; - while (cur != NULL) - { - GNUNET_CONTAINER_DLL_remove(net_head, net_tail, cur); - GNUNET_free (cur); - cur = net_head; - } +#if HAVE_LIBGLPK + if (ats_mode == MLP) + GAS_mlp_address_change_preference (mlp, peer, kind, score); +#endif } -static void -get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - interface_task = GNUNET_SCHEDULER_NO_TASK; - delete_networks (); - GNUNET_OS_network_interfaces_list(interface_proc, NULL); - interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, NULL); -} /** * Initialize address subsystem. * * @param cfg configuration to use + * @param stats the statistics handle to use */ void -GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg) +GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg, + const struct GNUNET_STATISTICS_Handle *stats) { GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", @@ -690,9 +725,34 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg) GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "WAN_QUOTA_OUT", &wan_quota_out)); - addresses = GNUNET_CONTAINER_multihashmap_create (128); - interface_task = GNUNET_SCHEDULER_add_now(get_addresses, NULL); + switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP")) + { + /* MLP = YES */ + case GNUNET_YES: +#if HAVE_LIBGLPK + ats_mode = MLP; + /* Init the MLP solver with default values */ + mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS); + break; +#else + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode"); + ats_mode = SIMPLE; + break; +#endif + /* MLP = NO */ + case GNUNET_NO: + ats_mode = SIMPLE; + break; + /* No configuration value */ + case GNUNET_SYSERR: + ats_mode = SIMPLE; + break; + default: + break; + } + + addresses = GNUNET_CONTAINER_multihashmap_create (128); } @@ -729,15 +789,16 @@ GAS_addresses_destroy_all () void GAS_addresses_done () { - delete_networks (); - if (interface_task != GNUNET_SCHEDULER_NO_TASK) - { - GNUNET_SCHEDULER_cancel(interface_task); - interface_task = GNUNET_SCHEDULER_NO_TASK; - } GAS_addresses_destroy_all (); GNUNET_CONTAINER_multihashmap_destroy (addresses); addresses = NULL; +#if HAVE_LIBGLPK + if (ats_mode == MLP) + { + GAS_mlp_done (mlp); + } +#endif + }