aboutsummaryrefslogtreecommitdiff
path: root/src/core/gnunet-service-core_typemap.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-10-05 13:26:24 +0000
committerChristian Grothoff <christian@grothoff.org>2011-10-05 13:26:24 +0000
commit0f29195adbd56ae10dea70c2951333c13e765f88 (patch)
tree83247d0f38a2cba50209af2325bad3d74890eb71 /src/core/gnunet-service-core_typemap.c
parentf39c4e7141b1fbb4830cb24ff630a879337f98d4 (diff)
downloadgnunet-0f29195adbd56ae10dea70c2951333c13e765f88.tar.gz
gnunet-0f29195adbd56ae10dea70c2951333c13e765f88.zip
towards new core service implementation -- breaking core up into smaller modules
Diffstat (limited to 'src/core/gnunet-service-core_typemap.c')
-rw-r--r--src/core/gnunet-service-core_typemap.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/core/gnunet-service-core_typemap.c b/src/core/gnunet-service-core_typemap.c
new file mode 100644
index 000000000..3aa652999
--- /dev/null
+++ b/src/core/gnunet-service-core_typemap.c
@@ -0,0 +1,96 @@
1
2/**
3 * Bitmap of message types this peer is able to handle.
4 */
5static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
6
7
8/**
9 * Compute a type map message for this peer.
10 *
11 * @return this peers current type map message.
12 */
13static struct GNUNET_MessageHeader *
14compute_type_map_message ()
15{
16 char *tmp;
17 uLongf dlen;
18 struct GNUNET_MessageHeader *hdr;
19
20#ifdef compressBound
21 dlen = compressBound (sizeof (my_type_map));
22#else
23 dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
24 /* documentation says 100.1% oldSize + 12 bytes, but we
25 * should be able to overshoot by more to be safe */
26#endif
27 hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
28 hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
29 tmp = (char *) &hdr[1];
30 if ((Z_OK !=
31 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
32 sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
33 {
34 dlen = sizeof (my_type_map);
35 memcpy (tmp, my_type_map, sizeof (my_type_map));
36 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
37 }
38 else
39 {
40 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
41 }
42 return hdr;
43}
44
45
46/**
47 * Send a type map message to the neighbour.
48 *
49 * @param cls the type map message
50 * @param key neighbour's identity
51 * @param value 'struct Neighbour' of the target
52 * @return always GNUNET_OK
53 */
54static int
55send_type_map_to_neighbour (void *cls, const GNUNET_HashCode * key, void *value)
56{
57 struct GNUNET_MessageHeader *hdr = cls;
58 struct Neighbour *n = value;
59 struct MessageEntry *m;
60 uint16_t size;
61
62 if (n == &self)
63 return GNUNET_OK;
64 size = ntohs (hdr->size);
65 m = GNUNET_malloc (sizeof (struct MessageEntry) + size);
66 memcpy (&m[1], hdr, size);
67 m->deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
68 m->slack_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
69 m->priority = UINT_MAX;
70 m->sender_status = n->status;
71 m->size = size;
72 m->next = n->messages;
73 n->messages = m;
74 return GNUNET_OK;
75}
76
77
78
79/**
80 * Send my type map to all connected peers (it got changed).
81 */
82static void
83broadcast_my_type_map ()
84{
85 struct GNUNET_MessageHeader *hdr;
86
87 if (NULL == neighbours)
88 return;
89 hdr = compute_type_map_message ();
90 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
91 &send_type_map_to_neighbour, hdr);
92 GNUNET_free (hdr);
93}
94
95
96