-preparatory steps for transport API change
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file core/gnunet-service-core_typemap.c
23  * @brief management of map that specifies which message types this peer supports
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet-service-core.h"
30 #include "gnunet-service-core_sessions.h"
31 #include "gnunet-service-core_typemap.h"
32 #include <zlib.h>
33
34
35 /**
36  * A type map describing which messages a given neighbour is able
37  * to process.
38  */
39 struct GSC_TypeMap
40 {
41   uint32_t bits[(UINT16_MAX + 1) / 32];
42 };
43
44 /**
45  * Bitmap of message types this peer is able to handle.
46  */
47 static struct GSC_TypeMap my_type_map;
48
49 /**
50  * Counters for message types this peer is able to handle.
51  */
52 static uint8_t map_counters[UINT16_MAX + 1];
53
54 /**
55  * Current hash of our (uncompressed) type map.
56  * Lazily computed when needed.
57  */
58 static struct GNUNET_HashCode my_tm_hash;
59
60 /**
61  * Is #my_tm_hash() current with respect to our type map?
62  */
63 static int hash_current;
64
65
66 /**
67  * Our type map changed, recompute its hash.
68  */
69 static void
70 rehash_typemap ()
71 {
72   hash_current = GNUNET_NO;
73 }
74
75
76 /**
77  * Hash the contents of a type map.
78  *
79  * @param tm map to hash
80  * @param hc where to store the hash code
81  */
82 void
83 GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm,
84                   struct GNUNET_HashCode *hc)
85 {
86   GNUNET_CRYPTO_hash (tm,
87                       sizeof (struct GSC_TypeMap),
88                       hc);
89 }
90
91
92 /**
93  * Check if the given hash matches our current type map.
94  *
95  * @param hc hash code to check if it matches our type map
96  * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not
97  */
98 int
99 GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
100 {
101   if (GNUNET_NO == hash_current)
102   {
103     GSC_TYPEMAP_hash (&my_type_map,
104                       &my_tm_hash);
105     hash_current = GNUNET_YES;
106   }
107   return (0 == memcmp (hc, &my_tm_hash, sizeof (struct GNUNET_HashCode)))
108     ? GNUNET_YES : GNUNET_NO;
109 }
110
111
112 /**
113  * Compute a type map message for this peer.
114  *
115  * @return this peers current type map message.
116  */
117 struct GNUNET_MessageHeader *
118 GSC_TYPEMAP_compute_type_map_message ()
119 {
120   char *tmp;
121   uLongf dlen;
122   struct GNUNET_MessageHeader *hdr;
123
124 #ifdef compressBound
125   dlen = compressBound (sizeof (my_type_map));
126 #else
127   dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
128   /* documentation says 100.1% oldSize + 12 bytes, but we
129    * should be able to overshoot by more to be safe */
130 #endif
131   hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
132   tmp = (char *) &hdr[1];
133   if ((Z_OK !=
134        compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
135                   sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
136   {
137     /* compression failed, use uncompressed map */
138     dlen = sizeof (my_type_map);
139     GNUNET_memcpy (tmp, &my_type_map, sizeof (my_type_map));
140     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
141   }
142   else
143   {
144     /* compression worked, use compressed map */
145     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
146   }
147   hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
148   return hdr;
149 }
150
151
152 /**
153  * Extract a type map from a TYPE_MAP message.
154  *
155  * @param msg a type map message
156  * @return NULL on error
157  */
158 struct GSC_TypeMap *
159 GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
160 {
161   struct GSC_TypeMap *ret;
162   uint16_t size;
163   uLongf dlen;
164
165   size = ntohs (msg->size);
166   switch (ntohs (msg->type))
167   {
168   case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
169     GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# type maps received"),
170                               1, GNUNET_NO);
171     if (size != sizeof (struct GSC_TypeMap))
172     {
173       GNUNET_break_op (0);
174       return NULL;
175     }
176     ret = GNUNET_new (struct GSC_TypeMap);
177     GNUNET_memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap));
178     return ret;
179   case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
180     GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# type maps received"),
181                               1, 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"), 1,
211                             GNUNET_NO);
212   GSC_SESSIONS_broadcast_typemap (hdr);
213   GNUNET_free (hdr);
214 }
215
216
217 /**
218  * Add a set of types to our type map.
219  *
220  * @param types array of message types supported by this peer
221  * @param tlen number of entries in @a types
222  */
223 void
224 GSC_TYPEMAP_add (const uint16_t *types,
225                  unsigned int tlen)
226 {
227   unsigned int i;
228   int changed;
229
230   changed = GNUNET_NO;
231   for (i = 0; i < tlen; i++)
232   {
233     if (0 == map_counters[types[i]]++)
234     {
235       my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
236       changed = GNUNET_YES;
237     }
238   }
239   if (GNUNET_YES == changed)
240   {
241     rehash_typemap ();
242     broadcast_my_type_map ();
243   }
244 }
245
246
247 /**
248  * Remove a set of types from our type map.
249  *
250  * @param types array of types to remove
251  * @param tlen length of the @a types array
252  */
253 void
254 GSC_TYPEMAP_remove (const uint16_t *types,
255                     unsigned int tlen)
256 {
257   unsigned int i;
258   int changed;
259
260   changed = GNUNET_NO;
261   for (i = 0; i < tlen; i++)
262   {
263     if (0 == --map_counters[types[i]])
264     {
265       my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
266       changed = GNUNET_YES;
267     }
268   }
269   if (GNUNET_YES == changed)
270   {
271     rehash_typemap ();
272     broadcast_my_type_map ();
273   }
274 }
275
276
277 /**
278  * Test if any of the types from the types array is in the
279  * given type map.
280  *
281  * @param tmap map to test
282  * @param types array of types
283  * @param tcnt number of entries in @a types
284  * @return #GNUNET_YES if a type is in the map, #GNUNET_NO if not
285  */
286 int
287 GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
288                         const uint16_t *types,
289                         unsigned int tcnt)
290 {
291   unsigned int i;
292
293   if (NULL == tmap)
294     return GNUNET_NO;
295   if (0 == tcnt)
296     return GNUNET_YES;          /* matches all */
297   for (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   unsigned int i;
319
320   ret = GNUNET_new (struct GSC_TypeMap);
321   if (NULL != tmap)
322     GNUNET_memcpy (ret, tmap, sizeof (struct GSC_TypeMap));
323   for (i = 0; i < tcnt; i++)
324     ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
325   return ret;
326 }
327
328
329 /**
330  * Create an empty type map.
331  *
332  * @return an empty type map
333  */
334 struct GSC_TypeMap *
335 GSC_TYPEMAP_create ()
336 {
337   return GNUNET_new (struct GSC_TypeMap);
338 }
339
340
341 /**
342  * Free the given type map.
343  *
344  * @param tmap a type map
345  */
346 void
347 GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
348 {
349   GNUNET_free (tmap);
350 }
351
352
353 /**
354  * Initialize typemap subsystem.
355  */
356 void
357 GSC_TYPEMAP_init ()
358 {
359   /* nothing to do */
360 }
361
362
363 /**
364  * Shutdown typemap subsystem.
365  */
366 void
367 GSC_TYPEMAP_done ()
368 {
369   /* nothing to do */
370 }
371
372 /* end of gnunet-service-core_typemap.c */