2 This file is part of GNUnet.
3 (C) 2010,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.
21 * @file ats/test_ats_api_common.c
22 * @brief shared functions for ats test
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "test_ats_api_common.h"
28 #define BIG_M_STRING "unlimited"
32 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
35 dest->plugin = GNUNET_strdup (plugin);
36 dest->session = session;
39 dest->addr = GNUNET_malloc (addrlen);
40 memcpy (dest->addr, addr, addrlen);
44 dest->addr_len = addrlen;
48 free_test_address (struct Test_Address *dest)
50 GNUNET_free_non_null (dest->plugin);
52 GNUNET_free_non_null (dest->addr);
57 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
58 const struct GNUNET_HELLO_Address *address2, void *session2)
60 if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
65 if (0 != strcmp (address1->transport_name, address2->transport_name))
67 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
70 if (address1->address_length != address2->address_length)
72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length\n");
76 else if (0 != memcmp (address1->address, address2->address, address2->address_length))
78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address\n");
81 if (session1 != session2)
83 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
93 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
94 const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
98 char *prop[] = GNUNET_ATS_PropertyStrings;
105 for (c_o = 0; c_o < ats_count_is; c_o++)
107 for (c_i = 0; c_i < ats_count_should; c_i++)
109 type1 = ntohl(ats_is[c_o].type);
110 type2 = ntohl(ats_should[c_i].type);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
115 val1 = ntohl(ats_is[c_o].value);
116 val2 = ntohl(ats_should[c_i].value);
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
138 * Load quotas for networks from configuration
140 * @param cfg configuration handle
141 * @param out_dest where to write outbound quotas
142 * @param in_dest where to write inbound quotas
143 * @param dest_length length of inbound and outbound arrays
144 * @return number of networks loaded
147 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
148 unsigned long long *out_dest,
149 unsigned long long *in_dest,
152 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
153 char * entry_in = NULL;
154 char * entry_out = NULL;
155 char * quota_out_str;
160 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
164 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
165 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
168 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
171 if (0 == strcmp(quota_out_str, BIG_M_STRING))
173 out_dest[c] = GNUNET_ATS_MaxBandwidth;
176 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
178 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out, &out_dest[c])))
181 if (GNUNET_NO == res)
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
184 network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
185 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
189 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
190 network_str[c], out_dest[c]);
192 GNUNET_free (quota_out_str);
196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
197 network_str[c], GNUNET_ATS_DefaultBandwidth);
198 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
202 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
205 if (0 == strcmp(quota_in_str, BIG_M_STRING))
207 in_dest[c] = GNUNET_ATS_MaxBandwidth;
210 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
212 if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in, &in_dest[c])))
215 if (GNUNET_NO == res)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
218 network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
219 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
224 network_str[c], in_dest[c]);
226 GNUNET_free (quota_in_str);
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
231 network_str[c], GNUNET_ATS_DefaultBandwidth);
232 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
235 GNUNET_free (entry_out);
236 GNUNET_free (entry_in);
238 return GNUNET_ATS_NetworkTypeCount;
242 * Create a ATS_address with the given information
244 * @param plugin_name plugin
245 * @param plugin_addr address
246 * @param plugin_addr_len address length
247 * @param session_id session
248 * @return the ATS_Address
251 create_address (const struct GNUNET_PeerIdentity *peer,
252 const char *plugin_name,
253 const void *plugin_addr, size_t plugin_addr_len,
256 struct ATS_Address *aa = NULL;
258 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
260 aa->addr_len = plugin_addr_len;
262 aa->plugin = (char *) &aa[1] + plugin_addr_len;
263 memcpy (&aa[1], plugin_addr, plugin_addr_len);
264 memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
265 aa->session_id = session_id;
266 aa->active = GNUNET_NO;
267 aa->used = GNUNET_NO;
268 aa->solver_information = NULL;
269 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
270 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
274 /* end of file test_ats_api_common.c */