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