-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / core / gnunet-service-core_typemap.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-2014 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file core/gnunet-service-core_typemap.c
21  * @brief management of map that specifies which message types this peer supports
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_transport_service.h"
27 #include "gnunet-service-core.h"
28 #include "gnunet-service-core_sessions.h"
29 #include "gnunet-service-core_typemap.h"
30 #include <zlib.h>
31
32
33 /**
34  * A type map describing which messages a given neighbour is able
35  * to process.
36  */
37 struct GSC_TypeMap
38 {
39   uint32_t bits[(UINT16_MAX + 1) / 32];
40 };
41
42 /**
43  * Bitmap of message types this peer is able to handle.
44  */
45 static struct GSC_TypeMap my_type_map;
46
47 /**
48  * Counters for message types this peer is able to handle.
49  */
50 static uint8_t map_counters[UINT16_MAX + 1];
51
52 /**
53  * Current hash of our (uncompressed) type map.
54  * Lazily computed when needed.
55  */
56 static struct GNUNET_HashCode my_tm_hash;
57
58 /**
59  * Is #my_tm_hash() current with respect to our type map?
60  */
61 static int hash_current;
62
63
64 /**
65  * Our type map changed, recompute its hash.
66  */
67 static void
68 rehash_typemap ()
69 {
70   hash_current = GNUNET_NO;
71 }
72
73
74 /**
75  * Hash the contents of a type map.
76  *
77  * @param tm map to hash
78  * @param hc where to store the hash code
79  */
80 void
81 GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm,
82                   struct GNUNET_HashCode *hc)
83 {
84   GNUNET_CRYPTO_hash (tm,
85                       sizeof (struct GSC_TypeMap),
86                       hc);
87 }
88
89
90 /**
91  * Check if the given hash matches our current type map.
92  *
93  * @param hc hash code to check if it matches our type map
94  * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not
95  */
96 int
97 GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
98 {
99   if (GNUNET_NO == hash_current)
100   {
101     GSC_TYPEMAP_hash (&my_type_map,
102                       &my_tm_hash);
103     hash_current = GNUNET_YES;
104   }
105   return (0 == memcmp (hc, &my_tm_hash, sizeof (struct GNUNET_HashCode)))
106     ? GNUNET_YES : GNUNET_NO;
107 }
108
109
110 /**
111  * Compute a type map message for this peer.
112  *
113  * @return this peers current type map message.
114  */
115 struct GNUNET_MessageHeader *
116 GSC_TYPEMAP_compute_type_map_message ()
117 {
118   char *tmp;
119   uLongf dlen;
120   struct GNUNET_MessageHeader *hdr;
121
122 #ifdef compressBound
123   dlen = compressBound (sizeof (my_type_map));
124 #else
125   dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
126   /* documentation says 100.1% oldSize + 12 bytes, but we
127    * should be able to overshoot by more to be safe */
128 #endif
129   hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
130   tmp = (char *) &hdr[1];
131   if ((Z_OK !=
132        compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
133                   sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
134   {
135     /* compression failed, use uncompressed map */
136     dlen = sizeof (my_type_map);
137     GNUNET_memcpy (tmp, &my_type_map, sizeof (my_type_map));
138     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
139   }
140   else
141   {
142     /* compression worked, use compressed map */
143     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
144   }
145   hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
146   return hdr;
147 }
148
149
150 /**
151  * Extract a type map from a TYPE_MAP message.
152  *
153  * @param msg a type map message
154  * @return NULL on error
155  */
156 struct GSC_TypeMap *
157 GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
158 {
159   struct GSC_TypeMap *ret;
160   uint16_t size;
161   uLongf dlen;
162
163   size = ntohs (msg->size);
164   switch (ntohs (msg->type))
165   {
166   case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
167     GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# type maps received"),
168                               1, GNUNET_NO);
169     if (size != sizeof (struct GSC_TypeMap))
170     {
171       GNUNET_break_op (0);
172       return NULL;
173     }
174     ret = GNUNET_new (struct GSC_TypeMap);
175     GNUNET_memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap));
176     return ret;
177   case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
178     GNUNET_STATISTICS_update (GSC_stats,
179                               gettext_noop ("# type maps received"),
180                               1,
181                               GNUNET_NO);
182     ret = GNUNET_new (struct GSC_TypeMap);
183     dlen = sizeof (struct GSC_TypeMap);
184     if ((Z_OK !=
185          uncompress ((Bytef *) ret, &dlen, (const Bytef *) &msg[1],
186                      (uLong) size)) || (dlen != sizeof (struct GSC_TypeMap)))
187     {
188       GNUNET_break_op (0);
189       GNUNET_free (ret);
190       return NULL;
191     }
192     return ret;
193   default:
194     GNUNET_break (0);
195     return NULL;
196   }
197 }
198
199
200 /**
201  * Send my type map to all connected peers (it got changed).
202  */
203 static void
204 broadcast_my_type_map ()
205 {
206   struct GNUNET_MessageHeader *hdr;
207
208   hdr = GSC_TYPEMAP_compute_type_map_message ();
209   GNUNET_STATISTICS_update (GSC_stats,
210                             gettext_noop ("# updates to my type map"),
211                             1,
212                             GNUNET_NO);
213   GSC_SESSIONS_broadcast_typemap (hdr);
214   GNUNET_free (hdr);
215 }
216
217
218 /**
219  * Add a set of types to our type map.
220  *
221  * @param types array of message types supported by this peer
222  * @param tlen number of entries in @a types
223  */
224 void
225 GSC_TYPEMAP_add (const uint16_t *types,
226                  unsigned int tlen)
227 {
228   unsigned int i;
229   int changed;
230
231   changed = GNUNET_NO;
232   for (i = 0; i < tlen; i++)
233   {
234     if (0 == map_counters[types[i]]++)
235     {
236       my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
237       changed = GNUNET_YES;
238     }
239   }
240   if (GNUNET_YES == changed)
241   {
242     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243                 "Typemap changed, broadcasting!\n");
244     rehash_typemap ();
245     broadcast_my_type_map ();
246   }
247 }
248
249
250 /**
251  * Remove a set of types from our type map.
252  *
253  * @param types array of types to remove
254  * @param tlen length of the @a types array
255  */
256 void
257 GSC_TYPEMAP_remove (const uint16_t *types,
258                     unsigned int tlen)
259 {
260   int changed;
261
262   changed = GNUNET_NO;
263   for (unsigned int i = 0; i < tlen; i++)
264   {
265     if (0 == --map_counters[types[i]])
266     {
267       my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
268       changed = GNUNET_YES;
269     }
270   }
271   if (GNUNET_YES == changed)
272   {
273     rehash_typemap ();
274     broadcast_my_type_map ();
275   }
276 }
277
278
279 /**
280  * Test if any of the types from the types array is in the
281  * given type map.
282  *
283  * @param tmap map to test
284  * @param types array of types
285  * @param tcnt number of entries in @a types
286  * @return #GNUNET_YES if a type is in the map, #GNUNET_NO if not
287  */
288 int
289 GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
290                         const uint16_t *types,
291                         unsigned int tcnt)
292 {
293   if (NULL == tmap)
294     return GNUNET_NO;
295   if (0 == tcnt)
296     return GNUNET_YES;          /* matches all */
297   for (unsigned int i = 0; i < tcnt; i++)
298     if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
299       return GNUNET_YES;
300   return GNUNET_NO;
301 }
302
303
304 /**
305  * Add additional types to a given typemap.
306  *
307  * @param tmap map to extend (not changed)
308  * @param types array of types to add
309  * @param tcnt number of entries in @a types
310  * @return updated type map (fresh copy)
311  */
312 struct GSC_TypeMap *
313 GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap,
314                     const uint16_t *types,
315                     unsigned int tcnt)
316 {
317   struct GSC_TypeMap *ret;
318
319   ret = GNUNET_new (struct GSC_TypeMap);
320   if (NULL != tmap)
321     GNUNET_memcpy (ret, tmap, sizeof (struct GSC_TypeMap));
322   for (unsigned int i = 0; i < tcnt; i++)
323     ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
324   return ret;
325 }
326
327
328 /**
329  * Create an empty type map.
330  *
331  * @return an empty type map
332  */
333 struct GSC_TypeMap *
334 GSC_TYPEMAP_create ()
335 {
336   return GNUNET_new (struct GSC_TypeMap);
337 }
338
339
340 /**
341  * Free the given type map.
342  *
343  * @param tmap a type map
344  */
345 void
346 GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
347 {
348   GNUNET_free (tmap);
349 }
350
351
352 /**
353  * Initialize typemap subsystem.
354  */
355 void
356 GSC_TYPEMAP_init ()
357 {
358   /* nothing to do */
359 }
360
361
362 /**
363  * Shutdown typemap subsystem.
364  */
365 void
366 GSC_TYPEMAP_done ()
367 {
368   /* nothing to do */
369 }
370
371 /* end of gnunet-service-core_typemap.c */