c54e446c67f94c7f006bbfd20c7453f15b2e614b
[oweals/gnunet.git] / src / core / gnunet-service-core_typemap.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 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 /**
56  * Compute a type map message for this peer.
57  *
58  * @return this peers current type map message.
59  */
60 struct GNUNET_MessageHeader *
61 GSC_TYPEMAP_compute_type_map_message ()
62 {
63   char *tmp;
64   uLongf dlen;
65   struct GNUNET_MessageHeader *hdr;
66
67 #ifdef compressBound
68   dlen = compressBound (sizeof (my_type_map));
69 #else
70   dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
71   /* documentation says 100.1% oldSize + 12 bytes, but we
72    * should be able to overshoot by more to be safe */
73 #endif
74   hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
75   tmp = (char *) &hdr[1];
76   if ((Z_OK !=
77        compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
78                   sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
79   {
80     dlen = sizeof (my_type_map);
81     memcpy (tmp, &my_type_map, sizeof (my_type_map));
82     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
83   }
84   else
85   {
86     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
87   }
88   hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
89   return hdr;
90 }
91
92
93 /**
94  * Extract a type map from a TYPE_MAP message.
95  *
96  * @param msg a type map message
97  * @return NULL on error
98  */
99 struct GSC_TypeMap *
100 GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
101 {
102   struct GSC_TypeMap *ret;
103   uint16_t size;
104   uLongf dlen;
105
106   size = ntohs (msg->size);
107   switch (ntohs (msg->type))
108   {
109   case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
110     GNUNET_STATISTICS_update (GSC_stats, 
111                               gettext_noop ("# type maps received"),
112                               1,
113                               GNUNET_NO);
114     if (size != sizeof (struct GSC_TypeMap))
115     {
116       GNUNET_break_op (0);
117       return NULL;
118     }
119     ret = GNUNET_malloc (sizeof (struct GSC_TypeMap));
120     memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap));
121     return ret;
122   case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
123     GNUNET_STATISTICS_update (GSC_stats, 
124                               gettext_noop ("# type maps received"),
125                               1,
126                               GNUNET_NO);
127     ret = GNUNET_malloc (sizeof (struct GSC_TypeMap));
128     dlen = sizeof (struct GSC_TypeMap);
129     if ( (Z_OK !=
130           uncompress ((Bytef*) ret, &dlen,
131                       (const Bytef*) &msg[1], (uLong) size)) ||
132          (dlen != sizeof (struct GSC_TypeMap) ) )
133     {
134       GNUNET_break_op (0);
135       GNUNET_free (ret);
136       return NULL;
137     }
138     return ret;
139   default:
140     GNUNET_break (0);
141     return NULL;
142   }
143 }
144
145
146 /**
147  * Send my type map to all connected peers (it got changed).
148  */
149 static void
150 broadcast_my_type_map ()
151 {
152   struct GNUNET_MessageHeader *hdr;
153
154   hdr = GSC_TYPEMAP_compute_type_map_message ();
155   GNUNET_STATISTICS_update (GSC_stats, 
156                             gettext_noop ("# updates to my type map"),
157                             1,
158                             GNUNET_NO);
159   GSC_SESSIONS_broadcast (hdr);
160   GNUNET_free (hdr);
161 }
162
163
164 /**
165  * Add a set of types to our type map.
166  */
167 void
168 GSC_TYPEMAP_add (const uint16_t *types,
169                  unsigned int tlen)
170 {
171   unsigned int i;
172   int changed;
173
174   changed = GNUNET_NO;
175   for (i=0;i<tlen;i++)
176   {
177     if (0 == map_counters[types[i]]++)
178     {
179       my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
180       changed = GNUNET_YES;
181     }
182   }
183   if (GNUNET_YES == changed)
184     broadcast_my_type_map ();
185 }
186
187
188 /**
189  * Remove a set of types from our type map.
190  */
191 void
192 GSC_TYPEMAP_remove (const uint16_t *types,
193                     unsigned int tlen)
194 {
195   unsigned int i;
196   int changed;
197
198   changed = GNUNET_NO;
199   for (i=0;i<tlen;i++)
200   {
201     if (0 == --map_counters[types[i]])
202     {
203       my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
204       changed = GNUNET_YES;
205     }
206   }
207   if (GNUNET_YES == changed)
208     broadcast_my_type_map ();
209 }
210
211
212 /**
213  * Test if any of the types from the types array is in the
214  * given type map.
215  *
216  * @param map map to test
217  * @param types array of types
218  * @param tcnt number of entries in types
219  * @return GNUNET_YES if a type is in the map, GNUNET_NO if not
220  */ 
221 int
222 GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
223                         const uint16_t *types,
224                         unsigned int tcnt)
225 {  
226   unsigned int i;
227
228   if (NULL == tmap)
229     return GNUNET_NO;
230   if (0 == tcnt)
231     return GNUNET_YES; /* matches all */
232   for (i=0;i<tcnt;i++) 
233     if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
234       return GNUNET_YES;
235   return GNUNET_NO;
236 }
237
238
239 /**
240  * Add additional types to a given typemap.
241  *
242  * @param map map to extend (not changed)
243  * @param types array of types to add
244  * @param tcnt number of entries in types
245  * @return updated type map (fresh copy)
246  */ 
247 struct GSC_TypeMap *
248 GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap,
249                     const uint16_t *types,
250                     unsigned int tcnt)
251 {
252   struct GSC_TypeMap *ret;
253   unsigned int i;
254
255   ret = GNUNET_malloc (sizeof (struct GSC_TypeMap));
256   if (NULL != tmap)
257     memcpy (ret, tmap, sizeof (struct GSC_TypeMap));
258   for (i=0;i<tcnt;i++)
259     ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
260   return ret;
261 }
262
263
264 /**
265  * Create an empty type map.
266  *
267  * @param map a type map
268  */
269 struct GSC_TypeMap *
270 GSC_TYPEMAP_create ()
271 {
272   return GNUNET_malloc (sizeof (struct GSC_TypeMap));
273 }
274
275
276 /**
277  * Free the given type map.
278  *
279  * @param map a type map
280  */
281 void
282 GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
283 {
284   GNUNET_free (tmap);
285 }
286
287
288 /**
289  * Initialize typemap subsystem.
290  */
291 void
292 GSC_TYPEMAP_init ()
293 {
294   /* nothing to do */
295 }
296
297
298 /**
299  * Shutdown typemap subsystem.
300  */
301 void
302 GSC_TYPEMAP_done ()
303 {
304   /* nothing to do */
305 }
306
307 /* end of gnunet-service-core_typemap.c */