no merging just replacing
[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 static void
65 destroy_address (struct ATS_Address * addr)
66 {
67   GNUNET_free_non_null (addr->ats);
68   GNUNET_free (addr->plugin);
69   GNUNET_free (addr);
70 }
71
72 static int 
73 compare_address_it (void *cls,
74                     const GNUNET_HashCode * key,
75                     void *value)
76 {
77   struct CompareAddressContext * cac = cls;
78   struct ATS_Address * aa = (struct ATS_Address *) value;
79
80   /* compare sessions */
81   if ((aa->session_client != cac->search->session_client) ||
82       (aa->session_id != cac->search->session_id))
83     return GNUNET_YES;
84
85   if (0 == strcmp(aa->plugin, cac->search->plugin))
86   {
87     if (aa->addr_len != cac->search->addr_len)
88       return GNUNET_YES;
89     if (aa->addr_len == 0)
90       return GNUNET_YES;
91     if (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
92       cac->result = aa;
93     return GNUNET_NO;
94   }
95   return GNUNET_YES;
96 }
97
98 struct ATS_Address *
99 find_address (const struct GNUNET_PeerIdentity *peer,
100               struct ATS_Address * addr)
101 {
102   struct CompareAddressContext cac;
103   cac.result = NULL;
104   cac.search = addr;
105
106   GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
107          &peer->hashPubKey,
108          compare_address_it,
109          &cac);
110
111   return cac.result;
112 }
113
114 void
115 GAS_address_update (const struct GNUNET_PeerIdentity *peer,
116                     const char *plugin_name,
117                     const void *plugin_addr, size_t plugin_addr_len,
118                     struct GNUNET_SERVER_Client *session_client,
119                     uint32_t session_id,
120                     const struct GNUNET_TRANSPORT_ATS_Information *atsi,
121                     uint32_t atsi_count)
122 {
123   struct ATS_Address * aa;
124   struct ATS_Address * old;
125
126   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
127   aa->ats = GNUNET_malloc(atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
128
129   aa->peer = *peer;
130   aa->addr_len = plugin_addr_len;
131   aa->ats_count = atsi_count;
132   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
133   aa->addr = &aa[1];
134   memcpy (aa->addr, plugin_addr, plugin_addr_len);
135   aa->plugin = GNUNET_strdup (plugin_name);
136   aa->session_client = session_client;
137   aa->session_id = session_id;
138
139   old = find_address (peer, aa);
140   if (old == NULL)
141   {
142     GNUNET_assert (GNUNET_OK ==
143                    GNUNET_CONTAINER_multihashmap_put(addresses,
144                                                      &peer->hashPubKey,
145                                                      aa,
146                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
147     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148       "Added new address for peer `%s' %X\n",
149       GNUNET_i2s (peer), aa);
150   }
151   else
152   {
153     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154       "Updated existing address for peer `%s' %X \n",
155       GNUNET_i2s (peer), old);
156     GNUNET_free_non_null(old->ats);
157     old->ats = NULL;
158     old->ats_count = 0;
159     old->ats = aa->ats;
160     old->ats_count = aa->ats_count;
161     GNUNET_free (aa->plugin);
162     GNUNET_free (aa);
163   }
164
165 }
166
167 static int
168 remove_client (void *cls,
169                const GNUNET_HashCode * key,
170                void *value)
171 {
172   struct GNUNET_SERVER_Client *client = cls;
173   struct ATS_Address * aa = (struct ATS_Address * ) value;
174
175   if (aa->session_client == client)
176   {
177     GNUNET_CONTAINER_multihashmap_remove(addresses, key, value);
178     destroy_address (aa);
179   }
180   return GNUNET_OK;
181 }
182
183
184
185 void
186 GAS_address_client_disconnected (struct GNUNET_SERVER_Client *client)
187 {
188   if (addresses != NULL)
189     GNUNET_CONTAINER_multihashmap_iterate(addresses, &remove_client, client);
190 }
191
192
193 void
194 GAS_address_destroyed (const struct GNUNET_PeerIdentity *peer,
195                        const char *plugin_name,
196                        const void *plugin_addr, size_t plugin_addr_len,
197                        struct GNUNET_SERVER_Client *session_client,
198                        uint32_t session_id)
199 {
200
201   struct ATS_Address *aa;
202   struct ATS_Address *res;
203
204   aa = GNUNET_malloc (sizeof (struct ATS_Address) +
205                     plugin_addr_len);
206
207   aa->peer = *peer;
208   aa->addr_len = plugin_addr_len;
209   aa->addr = &aa[1];
210   memcpy (aa->addr, plugin_addr, plugin_addr_len);
211   aa->plugin = GNUNET_strdup (plugin_name);
212   aa->session_client = session_client;
213   aa->session_id = session_id;
214
215   res = find_address (peer, aa);
216   GNUNET_assert (res != 0);
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219     "Deleting address for peer `%s'\n",
220     GNUNET_i2s (peer));
221
222   GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(addresses, &peer->hashPubKey, res));
223   destroy_address (aa);
224   destroy_address (res);
225 }
226
227
228 void
229 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
230 {
231   struct ATS_Address * aa = NULL;
232   aa = GNUNET_CONTAINER_multihashmap_get (addresses, &peer->hashPubKey);
233   if (aa != NULL)
234     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);
235 }
236
237
238 /**
239  * Initialize address subsystem.
240  */
241 void
242 GAS_addresses_init ()
243 {
244   addresses = GNUNET_CONTAINER_multihashmap_create(128);
245 }
246
247
248 /**
249  * Free memory of address.
250  *
251  * @param cls NULL
252  * @param key peer identity (unused)
253  * @param value the 'struct ATS_Address' to free
254  * @return GNUNET_OK (continue to iterate)
255  */
256 static int 
257 free_address_it (void *cls,
258                  const GNUNET_HashCode * key,
259                  void *value)
260 {
261   struct ATS_Address * aa = value;
262   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263     "Freeing address for peer `%s' %X\n",
264     GNUNET_i2s (&aa->peer), aa);
265   GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
266   destroy_address (aa);
267   return GNUNET_OK;
268 }
269
270
271
272 /**
273  * Shutdown address subsystem.
274  */
275 void
276 GAS_addresses_done ()
277 {
278   GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
279   GNUNET_CONTAINER_multihashmap_destroy (addresses);
280   addresses = NULL;
281 }
282
283
284 /* end of gnunet-service-ats_addresses.c */