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