GNUnet  0.10.x
Functions
gnunet-service-core_typemap.h File Reference

management of map that specifies which message types this peer supports More...

#include "gnunet_util_lib.h"
#include "gnunet_transport_service.h"
Include dependency graph for gnunet-service-core_typemap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void GSC_TYPEMAP_add (const uint16_t *types, unsigned int tlen)
 Add a set of types to our type map. More...
 
void GSC_TYPEMAP_remove (const uint16_t *types, unsigned int tlen)
 Remove a set of message types from our type map. More...
 
struct GNUNET_MessageHeaderGSC_TYPEMAP_compute_type_map_message (void)
 Compute a type map message for this peer. More...
 
int GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
 Check if the given hash matches our current type map. More...
 
void GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
 Hash the contents of a type map. More...
 
struct GSC_TypeMapGSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
 Extract a type map from a #GNUNET_MESSAGE_TYPE_CORE_COMRESSED_TYPE_MAP or GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP message. More...
 
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. More...
 
struct GSC_TypeMapGSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap, const uint16_t *types, unsigned int tcnt)
 Add additional types to a given typemap. More...
 
struct GSC_TypeMapGSC_TYPEMAP_create (void)
 Create an empty type map. More...
 
void GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
 Free the given type map. More...
 
void GSC_TYPEMAP_init (void)
 Initialize typemap subsystem. More...
 
void GSC_TYPEMAP_done (void)
 Shutdown typemap subsystem. More...
 

Detailed Description

management of map that specifies which message types this peer supports

Author
Christian Grothoff

Definition in file gnunet-service-core_typemap.h.

Function Documentation

◆ GSC_TYPEMAP_add()

void GSC_TYPEMAP_add ( const uint16_t *  types,
unsigned int  tlen 
)

Add a set of types to our type map.

Parameters
typesarray of message types supported by this peer
tlennumber of entries in types

Definition at line 232 of file gnunet-service-core_typemap.c.

References GSC_TypeMap::bits, broadcast_my_type_map(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_YES, map_counters, my_type_map, and rehash_typemap().

Referenced by handle_client_init().

233 {
234  unsigned int i;
235  int changed;
236 
237  changed = GNUNET_NO;
238  for (i = 0; i < tlen; i++)
239  {
240  if (0 == map_counters[types[i]]++)
241  {
242  my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
243  changed = GNUNET_YES;
244  }
245  }
246  if (GNUNET_YES == changed)
247  {
248  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Typemap changed, broadcasting!\n");
249  rehash_typemap();
251  }
252 }
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:78
static struct GSC_TypeMap my_type_map
Bitmap of message types this peer is able to handle.
uint32_t bits[(UINT16_MAX+1)/32]
static void rehash_typemap()
Our type map changed, recompute its hash.
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:77
static void broadcast_my_type_map()
Send my type map to all connected peers (it got changed).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GSC_TYPEMAP_remove()

void GSC_TYPEMAP_remove ( const uint16_t *  types,
unsigned int  tlen 
)

Remove a set of message types from our type map.

Parameters
typesarray of message types no longer supported by this peer
tlennumber of entries in types

Remove a set of message types from our type map.

Parameters
typesarray of types to remove
tlenlength of the types array

Definition at line 262 of file gnunet-service-core_typemap.c.

References GSC_TypeMap::bits, broadcast_my_type_map(), GNUNET_NO, GNUNET_YES, map_counters, my_type_map, and rehash_typemap().

Referenced by client_disconnect_cb().

263 {
264  int changed;
265 
266  changed = GNUNET_NO;
267  for (unsigned int i = 0; i < tlen; i++)
268  {
269  if (0 == --map_counters[types[i]])
270  {
271  my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
272  changed = GNUNET_YES;
273  }
274  }
275  if (GNUNET_YES == changed)
276  {
277  rehash_typemap();
279  }
280 }
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:78
static struct GSC_TypeMap my_type_map
Bitmap of message types this peer is able to handle.
uint32_t bits[(UINT16_MAX+1)/32]
static void rehash_typemap()
Our type map changed, recompute its hash.
#define GNUNET_YES
Definition: gnunet_common.h:77
static void broadcast_my_type_map()
Send my type map to all connected peers (it got changed).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GSC_TYPEMAP_compute_type_map_message()

struct GNUNET_MessageHeader* GSC_TYPEMAP_compute_type_map_message ( void  )

Compute a type map message for this peer.

Returns
this peers current type map message.

Definition at line 114 of file gnunet-service-core_typemap.c.

References GNUNET_malloc, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP, GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP, my_type_map, GNUNET_MessageHeader::size, and GNUNET_MessageHeader::type.

Referenced by broadcast_my_type_map(), and transmit_typemap_task().

115 {
116  char *tmp;
117  uLongf dlen;
118  struct GNUNET_MessageHeader *hdr;
119 
120 #ifdef compressBound
121  dlen = compressBound(sizeof(my_type_map));
122 #else
123  dlen = sizeof(my_type_map) + (sizeof(my_type_map) / 100) + 20;
124  /* documentation says 100.1% oldSize + 12 bytes, but we
125  * should be able to overshoot by more to be safe */
126 #endif
127  hdr = GNUNET_malloc(dlen + sizeof(struct GNUNET_MessageHeader));
128  tmp = (char *)&hdr[1];
129  if ((Z_OK != compress2((Bytef *)tmp,
130  &dlen,
131  (const Bytef *)&my_type_map,
132  sizeof(my_type_map),
133  9)) ||
134  (dlen >= sizeof(my_type_map)))
135  {
136  /* compression failed, use uncompressed map */
137  dlen = sizeof(my_type_map);
138  GNUNET_memcpy(tmp, &my_type_map, sizeof(my_type_map));
140  }
141  else
142  {
143  /* compression worked, use compressed map */
145  }
146  hdr->size = htons((uint16_t)dlen + sizeof(struct GNUNET_MessageHeader));
147  return hdr;
148 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
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.
#define GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP
gzip-compressed type map of the sender
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP
uncompressed type map of the sender
Header for all communications.
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ GSC_TYPEMAP_check_hash()

int GSC_TYPEMAP_check_hash ( const struct GNUNET_HashCode hc)

Check if the given hash matches our current type map.

Parameters
hchash code to check if it matches our type map
Returns
GNUNET_YES if the hash matches, GNUNET_NO if not

Definition at line 95 of file gnunet-service-core_typemap.c.

References GNUNET_NO, GNUNET_YES, GSC_TYPEMAP_hash(), hash_current, my_tm_hash, and my_type_map.

Referenced by GSC_SESSIONS_confirm_typemap().

96 {
97  if (GNUNET_NO == hash_current)
98  {
101  }
102  return (0 == memcmp(hc, &my_tm_hash, sizeof(struct GNUNET_HashCode)))
103  ? GNUNET_YES
104  : GNUNET_NO;
105 }
#define GNUNET_NO
Definition: gnunet_common.h:78
static struct GSC_TypeMap my_type_map
Bitmap of message types this peer is able to handle.
static int hash_current
Is my_tm_hash() current with respect to our type map?
static struct GNUNET_HashCode my_tm_hash
Current hash of our (uncompressed) type map.
A 512-bit hashcode.
void GSC_TYPEMAP_hash(const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
Hash the contents of a type map.
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GSC_TYPEMAP_hash()

void GSC_TYPEMAP_hash ( const struct GSC_TypeMap tm,
struct GNUNET_HashCode hc 
)

Hash the contents of a type map.

Parameters
tmmap to hash
hcwhere to store the hash code

Definition at line 82 of file gnunet-service-core_typemap.c.

References GNUNET_CRYPTO_hash().

Referenced by GSC_SESSIONS_set_typemap(), and GSC_TYPEMAP_check_hash().

83 {
84  GNUNET_CRYPTO_hash(tm, sizeof(struct GSC_TypeMap), hc);
85 }
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
A type map describing which messages a given neighbour is able to process.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GSC_TYPEMAP_get_from_message()

struct GSC_TypeMap* GSC_TYPEMAP_get_from_message ( const struct GNUNET_MessageHeader msg)

Extract a type map from a #GNUNET_MESSAGE_TYPE_CORE_COMRESSED_TYPE_MAP or GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP message.

Parameters
msga type map message
Returns
NULL on error

Extract a type map from a #GNUNET_MESSAGE_TYPE_CORE_COMRESSED_TYPE_MAP or GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP message.

Parameters
msga type map message
Returns
NULL on error

Definition at line 158 of file gnunet-service-core_typemap.c.

References gettext_noop, GNUNET_break, GNUNET_break_op, GNUNET_free, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP, GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP, GNUNET_new, GNUNET_NO, GNUNET_STATISTICS_update(), GSC_stats, ret, size, GNUNET_MessageHeader::size, and GNUNET_MessageHeader::type.

Referenced by GSC_SESSIONS_set_typemap().

159 {
160  struct GSC_TypeMap *ret;
161  uint16_t size;
162  uLongf dlen;
163 
164  size = ntohs(msg->size);
165  switch (ntohs(msg->type))
166  {
169  gettext_noop("# type maps received"),
170  1,
171  GNUNET_NO);
172  if (size != sizeof(struct GSC_TypeMap))
173  {
174  GNUNET_break_op(0);
175  return NULL;
176  }
177  ret = GNUNET_new(struct GSC_TypeMap);
178  GNUNET_memcpy(ret, &msg[1], sizeof(struct GSC_TypeMap));
179  return ret;
180 
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 
200  default:
201  GNUNET_break(0);
202  return NULL;
203  }
204 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
#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...
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
#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...
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
static unsigned int size
Size of the "table".
Definition: peer.c:66
#define GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP
uncompressed type map of the sender
A type map describing which messages a given neighbour is able to process.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GSC_TYPEMAP_test_match()

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.

Parameters
tmapmap to test
typesarray of types
tcntnumber of entries in types
Returns
GNUNET_YES if a type is in the map, GNUNET_NO if not

Definition at line 293 of file gnunet-service-core_typemap.c.

References GSC_TypeMap::bits, GNUNET_NO, and GNUNET_YES.

Referenced by GSC_CLIENTS_notify_client_about_neighbour(), and GSC_SESSIONS_add_to_typemap().

296 {
297  if (NULL == tmap)
298  return GNUNET_NO;
299  if (0 == tcnt)
300  return GNUNET_YES; /* matches all */
301  for (unsigned int i = 0; i < tcnt; i++)
302  if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
303  return GNUNET_YES;
304  return GNUNET_NO;
305 }
#define GNUNET_NO
Definition: gnunet_common.h:78
uint32_t bits[(UINT16_MAX+1)/32]
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the caller graph for this function:

◆ GSC_TYPEMAP_extend()

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.

Parameters
tmapmap to extend (not changed)
typesarray of types to add
tcntnumber of entries in types
Returns
updated type map (fresh copy)

Definition at line 317 of file gnunet-service-core_typemap.c.

References GSC_TypeMap::bits, GNUNET_memcpy, GNUNET_new, and ret.

Referenced by GSC_SESSIONS_add_to_typemap().

320 {
321  struct GSC_TypeMap *ret;
322 
323  ret = GNUNET_new(struct GSC_TypeMap);
324  if (NULL != tmap)
325  GNUNET_memcpy(ret, tmap, sizeof(struct GSC_TypeMap));
326  for (unsigned int i = 0; i < tcnt; i++)
327  ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
328  return ret;
329 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int ret
Final status code.
Definition: gnunet-arm.c:89
uint32_t bits[(UINT16_MAX+1)/32]
A type map describing which messages a given neighbour is able to process.
Here is the caller graph for this function:

◆ GSC_TYPEMAP_create()

struct GSC_TypeMap* GSC_TYPEMAP_create ( void  )

Create an empty type map.

Returns
an empty type map

Definition at line 338 of file gnunet-service-core_typemap.c.

References GNUNET_new.

Referenced by GSC_SESSIONS_create().

339 {
340  return GNUNET_new(struct GSC_TypeMap);
341 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
A type map describing which messages a given neighbour is able to process.
Here is the caller graph for this function:

◆ GSC_TYPEMAP_destroy()

void GSC_TYPEMAP_destroy ( struct GSC_TypeMap tmap)

Free the given type map.

Parameters
tmapa type map

Definition at line 350 of file gnunet-service-core_typemap.c.

References GNUNET_free.

Referenced by GSC_SESSIONS_add_to_typemap(), GSC_SESSIONS_end(), and GSC_SESSIONS_set_typemap().

351 {
352  GNUNET_free(tmap);
353 }
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GSC_TYPEMAP_init()

void GSC_TYPEMAP_init ( void  )

Initialize typemap subsystem.

Definition at line 360 of file gnunet-service-core_typemap.c.

Referenced by run().

361 {
362  /* nothing to do */
363 }
Here is the caller graph for this function:

◆ GSC_TYPEMAP_done()

void GSC_TYPEMAP_done ( void  )

Shutdown typemap subsystem.

Definition at line 370 of file gnunet-service-core_typemap.c.

Referenced by shutdown_task().

371 {
372  /* nothing to do */
373 }
Here is the caller graph for this function: