some documentation
[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 (dest->plugin);
51   if (NULL != dest->addr)
52     GNUNET_free (dest->addr);
53 }
54
55 int
56 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
57                    const struct GNUNET_HELLO_Address *address2, void *session2)
58 {
59   if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
60   {
61       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
62       return GNUNET_SYSERR;
63   }
64   if (0 != strcmp (address1->transport_name, address2->transport_name))
65   {
66       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
67       return GNUNET_SYSERR;
68   }
69   if (address1->address_length != address2->address_length)
70   {
71       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
72       return GNUNET_SYSERR;
73
74   }
75   else if (0 != memcmp (address1->address, address2->address, address2->address_length))
76   {
77       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
78       return GNUNET_SYSERR;
79   }
80   if (session1 != session2)
81   {
82       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
83                   session1, session2);
84       return GNUNET_SYSERR;
85
86   }
87   return GNUNET_OK;
88 }
89
90
91 int
92 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
93              const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
94 {
95   unsigned int c_o;
96   unsigned int c_i;
97   char *prop[] = GNUNET_ATS_PropertyStrings;
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, "ATS type `%s'\n",
113                         prop[type1]);
114             val1 = ntohl(ats_is[c_o].value);
115             val2 = ntohl(ats_should[c_i].value);
116             if (val1 != val2)
117             {
118                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
119                             prop[type1],
120                             val1, val2);
121                 res = GNUNET_SYSERR;
122             }
123             else
124             {
125               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
126                           prop[type1],
127                           val1, val2);
128             }
129         }
130     }
131   }
132   return res;
133 }
134
135
136 /**
137  * Load quotas for networks from configuration
138  *
139  * @param cfg configuration handle
140  * @param out_dest where to write outbound quotas
141  * @param in_dest where to write inbound quotas
142  * @param dest_length length of inbound and outbound arrays
143  * @return number of networks loaded
144  */
145 unsigned int
146 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
147                                                  unsigned long long *out_dest,
148                                                  unsigned long long *in_dest,
149                                                  int dest_length)
150 {
151   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
152   char * entry_in = NULL;
153   char * entry_out = NULL;
154   char * quota_out_str;
155   char * quota_in_str;
156   int c;
157   int res;
158
159   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
160   {
161     in_dest[c] = 0;
162     out_dest[c] = 0;
163     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
164     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
165
166     /* quota out */
167     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
168     {
169       res = GNUNET_NO;
170       if (0 == strcmp(quota_out_str, BIG_M_STRING))
171       {
172         out_dest[c] = GNUNET_ATS_MaxBandwidth;
173         res = GNUNET_YES;
174       }
175       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
176         res = GNUNET_YES;
177       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
178          res = GNUNET_YES;
179
180       if (GNUNET_NO == res)
181       {
182           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
183               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
184           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
185       }
186       else
187       {
188           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
189               network_str[c], out_dest[c]);
190       }
191       GNUNET_free (quota_out_str);
192     }
193     else
194     {
195       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
196           network_str[c], GNUNET_ATS_DefaultBandwidth);
197       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
198     }
199
200     /* quota in */
201     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
202     {
203       res = GNUNET_NO;
204       if (0 == strcmp(quota_in_str, BIG_M_STRING))
205       {
206         in_dest[c] = GNUNET_ATS_MaxBandwidth;
207         res = GNUNET_YES;
208       }
209       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
210         res = GNUNET_YES;
211       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
212          res = GNUNET_YES;
213
214       if (GNUNET_NO == res)
215       {
216           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
217               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
218           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
219       }
220       else
221       {
222           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
223               network_str[c], in_dest[c]);
224       }
225       GNUNET_free (quota_in_str);
226     }
227     else
228     {
229       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
230           network_str[c], GNUNET_ATS_DefaultBandwidth);
231       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
232     }
233     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]);
234     GNUNET_free (entry_out);
235     GNUNET_free (entry_in);
236   }
237   return GNUNET_ATS_NetworkTypeCount;
238 }
239
240 /**
241  * Create a ATS_address with the given information
242  * @param peer peer
243  * @param plugin_name plugin
244  * @param plugin_addr address
245  * @param plugin_addr_len address length
246  * @param session_id session
247  * @return the ATS_Address
248  */
249 struct ATS_Address *
250 create_address (const struct GNUNET_PeerIdentity *peer,
251                 const char *plugin_name,
252                 const void *plugin_addr, size_t plugin_addr_len,
253                 uint32_t session_id)
254 {
255   struct ATS_Address *aa = NULL;
256
257   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len + strlen (plugin_name) + 1);
258   aa->peer = *peer;
259   aa->addr_len = plugin_addr_len;
260   aa->addr = &aa[1];
261   aa->plugin = (char *) &aa[1] + plugin_addr_len;
262   memcpy (&aa[1], plugin_addr, plugin_addr_len);
263   memcpy (aa->plugin, plugin_name, strlen (plugin_name) + 1);
264   aa->session_id = session_id;
265   aa->active = GNUNET_NO;
266   aa->used = GNUNET_NO;
267   aa->solver_information = NULL;
268   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
269   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
270   return aa;
271 }
272
273 /* end of file test_ats_api_common.c */