GNUnet  0.10.x
gnunet-service-core_typemap.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
29 #include "gnunet-service-core.h"
32 #include <zlib.h>
33 
34 
40 {
41  uint32_t bits[(UINT16_MAX + 1) / 32];
42 };
43 
47 static struct GSC_TypeMap my_type_map;
48 
52 static uint8_t map_counters[UINT16_MAX + 1];
53 
59 
63 static int hash_current;
64 
65 
69 static void
71 {
73 }
74 
75 
82 void
83 GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
84 {
85  GNUNET_CRYPTO_hash (tm, sizeof (struct GSC_TypeMap), hc);
86 }
87 
88 
95 int
97 {
98  if (GNUNET_NO == hash_current)
99  {
102  }
103  return (0 == memcmp (hc, &my_tm_hash, sizeof (struct GNUNET_HashCode)))
104  ? GNUNET_YES
105  : GNUNET_NO;
106 }
107 
108 
114 struct GNUNET_MessageHeader *
116 {
117  char *tmp;
118  uLongf dlen;
119  struct GNUNET_MessageHeader *hdr;
120 
121 #ifdef compressBound
122  dlen = compressBound (sizeof (my_type_map));
123 #else
124  dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
125  /* documentation says 100.1% oldSize + 12 bytes, but we
126  * should be able to overshoot by more to be safe */
127 #endif
128  hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
129  tmp = (char *) &hdr[1];
130  if ((Z_OK != compress2 ((Bytef *) tmp,
131  &dlen,
132  (const Bytef *) &my_type_map,
133  sizeof (my_type_map),
134  9)) ||
135  (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));
141  }
142  else
143  {
144  /* compression worked, use compressed map */
146  }
147  hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
148  return hdr;
149 }
150 
151 
158 struct GSC_TypeMap *
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  {
170  gettext_noop ("# type maps received"),
171  1,
172  GNUNET_NO);
173  if (size != sizeof (struct GSC_TypeMap))
174  {
175  GNUNET_break_op (0);
176  return NULL;
177  }
178  ret = GNUNET_new (struct GSC_TypeMap);
179  GNUNET_memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap));
180  return ret;
183  gettext_noop ("# type maps received"),
184  1,
185  GNUNET_NO);
186  ret = GNUNET_new (struct GSC_TypeMap);
187  dlen = sizeof (struct GSC_TypeMap);
188  if ((Z_OK != uncompress ((Bytef *) ret,
189  &dlen,
190  (const Bytef *) &msg[1],
191  (uLong) size)) ||
192  (dlen != sizeof (struct GSC_TypeMap)))
193  {
194  GNUNET_break_op (0);
195  GNUNET_free (ret);
196  return NULL;
197  }
198  return ret;
199  default:
200  GNUNET_break (0);
201  return NULL;
202  }
203 }
204 
205 
209 static void
211 {
212  struct GNUNET_MessageHeader *hdr;
213 
216  gettext_noop ("# updates to my type map"),
217  1,
218  GNUNET_NO);
220  GNUNET_free (hdr);
221 }
222 
223 
230 void
231 GSC_TYPEMAP_add (const uint16_t *types, unsigned int tlen)
232 {
233  unsigned int i;
234  int changed;
235 
236  changed = GNUNET_NO;
237  for (i = 0; i < tlen; i++)
238  {
239  if (0 == map_counters[types[i]]++)
240  {
241  my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
242  changed = GNUNET_YES;
243  }
244  }
245  if (GNUNET_YES == changed)
246  {
247  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Typemap changed, broadcasting!\n");
248  rehash_typemap ();
250  }
251 }
252 
253 
260 void
261 GSC_TYPEMAP_remove (const uint16_t *types, unsigned int tlen)
262 {
263  int changed;
264 
265  changed = GNUNET_NO;
266  for (unsigned int i = 0; i < tlen; i++)
267  {
268  if (0 == --map_counters[types[i]])
269  {
270  my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
271  changed = GNUNET_YES;
272  }
273  }
274  if (GNUNET_YES == changed)
275  {
276  rehash_typemap ();
278  }
279 }
280 
281 
291 int
293  const uint16_t *types,
294  unsigned int tcnt)
295 {
296  if (NULL == tmap)
297  return GNUNET_NO;
298  if (0 == tcnt)
299  return GNUNET_YES; /* matches all */
300  for (unsigned int i = 0; i < tcnt; i++)
301  if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
302  return GNUNET_YES;
303  return GNUNET_NO;
304 }
305 
306 
315 struct GSC_TypeMap *
316 GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap,
317  const uint16_t *types,
318  unsigned int tcnt)
319 {
320  struct GSC_TypeMap *ret;
321 
322  ret = GNUNET_new (struct GSC_TypeMap);
323  if (NULL != tmap)
324  GNUNET_memcpy (ret, tmap, sizeof (struct GSC_TypeMap));
325  for (unsigned int i = 0; i < tcnt; i++)
326  ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
327  return ret;
328 }
329 
330 
336 struct GSC_TypeMap *
338 {
339  return GNUNET_new (struct GSC_TypeMap);
340 }
341 
342 
348 void
350 {
351  GNUNET_free (tmap);
352 }
353 
354 
358 void
360 {
361  /* nothing to do */
362 }
363 
364 
368 void
370 {
371  /* nothing to do */
372 }
373 
374 /* end of gnunet-service-core_typemap.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MessageHeader * GSC_TYPEMAP_compute_type_map_message()
Compute a type map message for this peer.
static uint8_t map_counters[UINT16_MAX+1]
Counters for message types this peer is able to handle.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static struct GSC_TypeMap my_type_map
Bitmap of message types this peer is able to handle.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_STATISTICS_Handle * GSC_stats
For creating statistics.
static int ret
Final status code.
Definition: gnunet-arm.c:89
static int hash_current
Is my_tm_hash() current with respect to our type map?
#define GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP
gzip-compressed type map of the sender
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
management of map that specifies which message types this peer supports
static struct GNUNET_HashCode my_tm_hash
Current hash of our (uncompressed) type map.
uint32_t bits[(UINT16_MAX+1)/32]
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_memcpy(dst, src, n)
static void rehash_typemap()
Our type map changed, recompute its hash.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
void GSC_TYPEMAP_destroy(struct GSC_TypeMap *tmap)
Free the given type map.
A 512-bit hashcode.
Globals for gnunet-service-core.
void GSC_TYPEMAP_init()
Initialize typemap subsystem.
struct GSC_TypeMap * GSC_TYPEMAP_create()
Create an empty type map.
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP
uncompressed type map of the sender
int GSC_TYPEMAP_check_hash(const struct GNUNET_HashCode *hc)
Check if the given hash matches our current type map.
int GSC_TYPEMAP_test_match(const struct GSC_TypeMap *tmap, const uint16_t *types, unsigned int tcnt)
Test if any of the types from the types array is in the given type map.
void GSC_TYPEMAP_done()
Shutdown typemap subsystem.
void GSC_TYPEMAP_hash(const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
Hash the contents of a type map.
struct GSC_TypeMap * GSC_TYPEMAP_extend(const struct GSC_TypeMap *tmap, const uint16_t *types, unsigned int tcnt)
Add additional types to a given typemap.
#define GNUNET_log(kind,...)
A type map describing which messages a given neighbour is able to process.
void GSC_TYPEMAP_remove(const uint16_t *types, unsigned int tlen)
Remove a set of types from our type map.
Header for all communications.
#define GNUNET_YES
Definition: gnunet_common.h:80
void GSC_SESSIONS_broadcast_typemap(const struct GNUNET_MessageHeader *msg)
Broadcast an updated typemap message to all neighbours.
static void broadcast_my_type_map()
Send my type map to all connected peers (it got changed).
struct GSC_TypeMap * GSC_TYPEMAP_get_from_message(const struct GNUNET_MessageHeader *msg)
Extract a type map from a TYPE_MAP message.
#define GNUNET_malloc(size)
Wrapper around malloc.
void GSC_TYPEMAP_add(const uint16_t *types, unsigned int tlen)
Add a set of types to our type map.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69