(no commit message)
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 /**
22  * @file ats/gnunet-service-ats_addresses.c
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_scheduling.h"
30
31 struct ATS_Address
32 {
33   struct GNUNET_PeerIdentity peer;
34
35   size_t addr_len;
36
37   struct GNUNET_SERVER_Client *session_client;
38                    
39   uint32_t session_id;
40
41   uint32_t ats_count;
42
43   void * addr;
44
45   char * plugin;
46
47   struct GNUNET_TRANSPORT_ATS_Information * ats;
48
49   struct GNUNET_BANDWIDTH_Value32NBO bw_in;
50
51   struct GNUNET_BANDWIDTH_Value32NBO bw_out;
52 };
53
54
55 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
56
57
58 struct CompareAddressContext
59 {
60   struct ATS_Address * search;
61   struct ATS_Address * result;
62 };
63
64
65 static int 
66 compare_address_it (void *cls,
67                     const GNUNET_HashCode * key,
68                     void *value)
69 {
70   struct CompareAddressContext * cac = cls;
71   struct ATS_Address * aa = (struct ATS_Address *) value;
72
73   /* compare sessions */
74   if ((aa->session_client != cac->search->session_client) ||
75       (aa->session_id != cac->search->session_id))
76     return GNUNET_YES;
77
78   if (0 == strcmp(aa->plugin, cac->search->plugin))
79   {
80     if (aa->addr_len != cac->search->addr_len)
81       return GNUNET_YES;
82     if (aa->addr_len == 0)
83       return GNUNET_YES;
84     if (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
85       cac->result = aa;
86     return GNUNET_NO;
87   }
88   return GNUNET_YES;
89 }
90
91 struct ATS_Address *
92 find_address (const struct GNUNET_PeerIdentity *peer,
93               struct ATS_Address * addr)
94 {
95   struct CompareAddressContext cac;
96   cac.result = NULL;
97   cac.search = addr;
98
99   GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
100          &peer->hashPubKey,
101          compare_address_it,
102          &cac);
103
104   return cac.result;
105 }
106
107 static void
108 merge_ats (struct ATS_Address * dest, struct ATS_Address * source)
109 {
110   int c_src = 0;
111   int c_dest = 0;
112   struct GNUNET_TRANSPORT_ATS_Information * a_src = source->ats;
113   struct GNUNET_TRANSPORT_ATS_Information * a_dest = dest->ats;
114   struct ATS_Address * bigger = NULL;
115
116   bigger = (dest->ats_count > source->ats_count) ? dest : source;
117   int new_entries = bigger->ats_count;
118
119   if (new_entries == 0)
120     return;
121
122   for (c_dest = 0; c_dest < dest->ats_count; c_dest ++)
123   {
124     for (c_src = 0; c_src < source->ats_count; c_src ++)
125     {
126       if (a_src[c_src].type == a_dest[c_dest].type)
127         new_entries--;
128     }
129   }
130
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132     "Have %u entries to update and %u new entries\n",bigger->ats_count,
133     new_entries);
134 }
135
136 void
137 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
138                     const char *plugin_name,
139                     const void *plugin_addr, size_t plugin_addr_len,
140                     struct GNUNET_SERVER_Client *session_client,
141                     uint32_t session_id,
142                     const struct GNUNET_TRANSPORT_ATS_Information *atsi,
143                     uint32_t atsi_count)
144 {
145   struct ATS_Address * aa;
146   struct ATS_Address * old;
147
148   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
149   aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
150
151   aa->peer = *peer;
152   aa->addr_len = plugin_addr_len;
153   aa->ats_count = atsi_count;
154   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
155   aa->addr = &aa[1];
156   memcpy (aa->addr, plugin_addr, plugin_addr_len);
157   aa->plugin = GNUNET_strdup (plugin_name);
158   aa->session_client = session_client;
159   aa->session_id = session_id;
160
161   old = find_address (peer, aa);
162   if (old == NULL)
163   {
164     GNUNET_assert (GNUNET_OK ==
165                    GNUNET_CONTAINER_multihashmap_put(addresses,
166                                                      &peer->hashPubKey,
167                                                      aa,
168                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
169     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170       "Added new address for peer `%s' %X\n",
171       GNUNET_i2s (peer), aa);
172   }
173   else
174   {
175     merge_ats (old, aa);
176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177       "Updated existing address for peer `%s' %X \n",
178       GNUNET_i2s (peer), old);
179     GNUNET_free (aa->ats);
180     GNUNET_free (aa->plugin);
181     GNUNET_free (aa);
182   }
183
184 }
185
186
187 void
188 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
189                        const char *plugin_name,
190                        const void *plugin_addr, size_t plugin_addr_len,
191                        struct GNUNET_SERVER_Client *session_client,
192                        uint32_t session_id)
193 {
194
195   struct ATS_Address *aa;
196   struct ATS_Address *res;
197
198   aa = GNUNET_malloc (sizeof (struct ATS_Address) +
199                     plugin_addr_len);
200
201   aa->peer = *peer;
202   aa->addr_len = plugin_addr_len;
203   aa->addr = &aa[1];
204   memcpy (aa->addr, plugin_addr, plugin_addr_len);
205   aa->plugin = GNUNET_strdup (plugin_name);
206   aa->session_client = session_client;
207   aa->session_id = session_id;
208
209   res = find_address (peer, aa);
210   GNUNET_assert (res != 0);
211
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213     "Deleting address for peer `%s'\n",
214     GNUNET_i2s (peer));
215
216   GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(addresses, &peer->hashPubKey, res));
217   GNUNET_free (res->plugin);
218   GNUNET_free_non_null (res->ats);
219   GNUNET_free (res);
220
221 }
222
223
224 void
225 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
226 {
227   struct ATS_Address * aa = NULL;
228   aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
229   if (aa != NULL)
230     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr, aa->addr_len, aa->session_client, aa->session_id, aa->ats, aa->ats_count, aa->bw_out, aa->bw_in);
231 }
232
233
234 /**
235  * Initialize address subsystem.
236  */
237 void
238 GAS_addresses_init ()
239 {
240   addresses = GNUNET_CONTAINER_multihashmap_create(128);
241 }
242
243
244 /**
245  * Free memory of address.
246  *
247  * @param cls NULL
248  * @param key peer identity (unused)
249  * @param value the 'struct ATS_Address' to free
250  * @return GNUNET_OK (continue to iterate)
251  */
252 static int 
253 free_address_it (void *cls,
254                  const GNUNET_HashCode * key,
255                  void *value)
256 {
257   struct ATS_Address * aa = value;
258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259     "Freeing address for peer `%s' %X\n",
260     GNUNET_i2s (&aa->peer), aa);
261   GNUNET_free (aa->plugin);
262   GNUNET_free_non_null (aa->ats);
263   GNUNET_free (aa);
264   return GNUNET_OK;
265 }
266
267
268
269 /**
270  * Shutdown address subsystem.
271  */
272 void
273 GAS_addresses_done ()
274 {
275   GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
276   GNUNET_CONTAINER_multihashmap_destroy (addresses);
277 }
278
279
280 /* end of gnunet-service-ats_addresses.c */