-dce
[oweals/gnunet.git] / src / ats / test_ats_api_common.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file ats/test_ats_api_common.c
22  * @brief shared functions for ats test
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26
27 #include "test_ats_api_common.h"
28 #define BIG_M_STRING "unlimited"
29
30
31 void
32 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
33 {
34
35   dest->plugin = GNUNET_strdup (plugin);
36   dest->session = session;
37   if (addrlen > 0)
38   {
39     dest->addr = GNUNET_malloc (addrlen);
40     memcpy (dest->addr, addr, addrlen);
41   }
42   else
43       dest->addr = NULL;
44   dest->addr_len = addrlen;
45 }
46
47 void
48 free_test_address (struct Test_Address *dest)
49 {
50   GNUNET_free_non_null (dest->plugin);
51   dest->plugin = NULL;
52   GNUNET_free_non_null (dest->addr);
53   dest->addr = NULL;
54 }
55
56 int
57 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
58                    const struct GNUNET_HELLO_Address *address2, void *session2)
59 {
60   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
61   {
62       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
63       return GNUNET_SYSERR;
64   }
65   if (0 != strcmp (address1->transport_name, address2->transport_name))
66   {
67       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
68       return GNUNET_SYSERR;
69   }
70   if (address1->address_length != address2->address_length)
71   {
72       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length\n");
73       return GNUNET_SYSERR;
74
75   }
76   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
77   {
78       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address\n");
79       return GNUNET_SYSERR;
80   }
81   if (session1 != session2)
82   {
83       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
84                   session1, session2);
85       return GNUNET_SYSERR;
86
87   }
88   return GNUNET_OK;
89 }
90
91
92 int
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)
95 {
96   unsigned int c_o;
97   unsigned int c_i;
98   uint32_t type1;
99   uint32_t type2;
100   uint32_t val1;
101   uint32_t val2;
102   int res = GNUNET_OK;
103
104   for (c_o = 0; c_o < ats_count_is; c_o++)
105   {
106     for (c_i = 0; c_i < ats_count_should; c_i++)
107     {
108         type1 = ntohl(ats_is[c_o].type);
109         type2 = ntohl(ats_should[c_i].type);
110         if (type1 == type2)
111         {
112             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113                         "ATS type `%s'\n",
114                         GNUNET_ATS_print_property_type (type1));
115             val1 = ntohl(ats_is[c_o].value);
116             val2 = ntohl(ats_should[c_i].value);
117             if (val1 != val2)
118             {
119                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120                             "ATS value `%s' not equal: %u != %u\n",
121                             GNUNET_ATS_print_property_type (type1),
122                             val1, val2);
123                 res = GNUNET_SYSERR;
124             }
125             else
126             {
127               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128                           "ATS value `%s' equal: %u == %u\n",
129                           GNUNET_ATS_print_property_type (type1),
130                           val1, val2);
131             }
132         }
133     }
134   }
135   return res;
136 }
137
138
139 /**
140  * Load quotas for networks from configuration
141  *
142  * @param cfg configuration handle
143  * @param out_dest where to write outbound quotas
144  * @param in_dest where to write inbound quotas
145  * @param dest_length length of inbound and outbound arrays
146  * @return number of networks loaded
147  */
148 unsigned int
149 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
150                                                  unsigned long long *out_dest,
151                                                  unsigned long long *in_dest,
152                                                  int dest_length)
153 {
154   char *entry_in = NULL;
155   char *entry_out = NULL;
156   char *quota_out_str;
157   char *quota_in_str;
158   int c;
159   int res;
160
161   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
162   {
163     in_dest[c] = 0;
164     out_dest[c] = 0;
165     GNUNET_asprintf (&entry_out,
166                      "%s_QUOTA_OUT",
167                      GNUNET_ATS_print_network_type (c));
168     GNUNET_asprintf (&entry_in,
169                      "%s_QUOTA_IN",
170                      GNUNET_ATS_print_network_type (c));
171
172     /* quota out */
173     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
174     {
175       res = GNUNET_NO;
176       if (0 == strcmp(quota_out_str, BIG_M_STRING))
177       {
178         out_dest[c] = GNUNET_ATS_MaxBandwidth;
179         res = GNUNET_YES;
180       }
181       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
182         res = GNUNET_YES;
183       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
184          res = GNUNET_YES;
185
186       if (GNUNET_NO == res)
187       {
188         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189                     _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
190                     GNUNET_ATS_print_network_type (c),
191                     quota_out_str,
192                     GNUNET_ATS_DefaultBandwidth);
193           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
194       }
195       else
196       {
197           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198                       "Outbound quota configure for network `%s' is %llu\n",
199                       GNUNET_ATS_print_network_type (c),
200                       out_dest[c]);
201       }
202       GNUNET_free (quota_out_str);
203     }
204     else
205     {
206       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
207                   _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
208                   GNUNET_ATS_print_network_type (c),
209                   GNUNET_ATS_DefaultBandwidth);
210       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
211     }
212
213     /* quota in */
214     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
215     {
216       res = GNUNET_NO;
217       if (0 == strcmp(quota_in_str, BIG_M_STRING))
218       {
219         in_dest[c] = GNUNET_ATS_MaxBandwidth;
220         res = GNUNET_YES;
221       }
222       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
223         res = GNUNET_YES;
224       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
225          res = GNUNET_YES;
226
227       if (GNUNET_NO == res)
228       {
229           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                       _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
231                       GNUNET_ATS_print_network_type (c),
232                       quota_in_str,
233                       GNUNET_ATS_DefaultBandwidth);
234           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
235       }
236       else
237       {
238           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239                       "Inbound quota configured for network `%s' is %llu\n",
240                       GNUNET_ATS_print_network_type (c),
241                       in_dest[c]);
242       }
243       GNUNET_free (quota_in_str);
244     }
245     else
246     {
247       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
248                   _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
249                   GNUNET_ATS_print_network_type (c),
250                   GNUNET_ATS_DefaultBandwidth);
251       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
252     }
253     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254                 "Loaded quota for network `%s' (in/out): %llu %llu\n",
255                 GNUNET_ATS_print_network_type (c),
256                 in_dest[c],
257                 out_dest[c]);
258     GNUNET_free (entry_out);
259     GNUNET_free (entry_in);
260   }
261   return GNUNET_ATS_NetworkTypeCount;
262 }
263
264 /**
265  * Create a ATS_address with the given information
266  * @param peer peer
267  * @param plugin_name plugin
268  * @param plugin_addr address
269  * @param plugin_addr_len address length
270  * @param session_id session
271  * @return the ATS_Address
272  */
273 struct ATS_Address *
274 create_address (const struct GNUNET_PeerIdentity *peer,
275                 const char *plugin_name,
276                 const void *plugin_addr, size_t plugin_addr_len,
277                 uint32_t session_id)
278 {
279   struct ATS_Address *aa = NULL;
280
281   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
282   aa->peer = *peer;
283   aa->addr_len = plugin_addr_len;
284   aa->addr = &aa[1];
285   aa->plugin = (char *) &aa[1] + plugin_addr_len;
286   memcpy (&aa[1], plugin_addr, plugin_addr_len);
287   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
288   aa->session_id = session_id;
289   aa->active = GNUNET_NO;
290   aa->used = GNUNET_NO;
291   aa->solver_information = NULL;
292   aa->assigned_bw_in = 0;
293   aa->assigned_bw_out = 0;
294   return aa;
295 }
296
297 /* end of file test_ats_api_common.c */