aboutsummaryrefslogtreecommitdiff
path: root/src/core/gnunet-service-core_typemap.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-10-06 19:27:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-10-06 19:27:35 +0000
commit0babf19b445e521fdb78f0e1a67d5548e28405b9 (patch)
treed6713ae7b6e46feeae3af5eb2625e68a99771c46 /src/core/gnunet-service-core_typemap.c
parentd7591587eb28a40e44a3065c30ec1b60a9bcba68 (diff)
downloadgnunet-0babf19b445e521fdb78f0e1a67d5548e28405b9.tar.gz
gnunet-0babf19b445e521fdb78f0e1a67d5548e28405b9.zip
drafting typemap API
Diffstat (limited to 'src/core/gnunet-service-core_typemap.c')
-rw-r--r--src/core/gnunet-service-core_typemap.c171
1 files changed, 88 insertions, 83 deletions
diff --git a/src/core/gnunet-service-core_typemap.c b/src/core/gnunet-service-core_typemap.c
index dfd0b8888..5feb65a53 100644
--- a/src/core/gnunet-service-core_typemap.c
+++ b/src/core/gnunet-service-core_typemap.c
@@ -1,3 +1,33 @@
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
1 31
2/** 32/**
3 * A type map describing which messages a given neighbour is able 33 * A type map describing which messages a given neighbour is able
@@ -16,6 +46,58 @@ static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
16 46
17 47
18/** 48/**
49 * Compute a type map message for this peer.
50 *
51 * @return this peers current type map message.
52 */
53static struct GNUNET_MessageHeader *
54compute_type_map_message ()
55{
56 char *tmp;
57 uLongf dlen;
58 struct GNUNET_MessageHeader *hdr;
59
60#ifdef compressBound
61 dlen = compressBound (sizeof (my_type_map));
62#else
63 dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
64 /* documentation says 100.1% oldSize + 12 bytes, but we
65 * should be able to overshoot by more to be safe */
66#endif
67 hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
68 hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
69 tmp = (char *) &hdr[1];
70 if ((Z_OK !=
71 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
72 sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
73 {
74 dlen = sizeof (my_type_map);
75 memcpy (tmp, my_type_map, sizeof (my_type_map));
76 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
77 }
78 else
79 {
80 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
81 }
82 return hdr;
83}
84
85
86/**
87 * Send my type map to all connected peers (it got changed).
88 */
89static void
90broadcast_my_type_map ()
91{
92 struct GNUNET_MessageHeader *hdr;
93
94 hdr = compute_type_map_message ();
95 GSC_SESSIONS_broadcast (hdr);x
96 GNUNET_free (hdr);
97}
98
99
100/**
19 * Add a set of types to our type map. 101 * Add a set of types to our type map.
20 */ 102 */
21void 103void
@@ -63,97 +145,20 @@ int
63GSC_TYPEMAP_test_match (struct GSC_TypeMap *tmap, 145GSC_TYPEMAP_test_match (struct GSC_TypeMap *tmap,
64 const uint16_t *types, 146 const uint16_t *types,
65 unsigned int tcnt) 147 unsigned int tcnt)
66{ 148{
67 return GNUNET_YES; /* FIXME */ 149 return GNUNET_YES; /* FIXME */
68} 150}
69 151
70 152
71/** 153void
72 * Compute a type map message for this peer. 154GSC_TYPEMAP_init ()
73 *
74 * @return this peers current type map message.
75 */
76static struct GNUNET_MessageHeader *
77compute_type_map_message ()
78{
79 char *tmp;
80 uLongf dlen;
81 struct GNUNET_MessageHeader *hdr;
82
83#ifdef compressBound
84 dlen = compressBound (sizeof (my_type_map));
85#else
86 dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
87 /* documentation says 100.1% oldSize + 12 bytes, but we
88 * should be able to overshoot by more to be safe */
89#endif
90 hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
91 hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
92 tmp = (char *) &hdr[1];
93 if ((Z_OK !=
94 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
95 sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
96 {
97 dlen = sizeof (my_type_map);
98 memcpy (tmp, my_type_map, sizeof (my_type_map));
99 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
100 }
101 else
102 {
103 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
104 }
105 return hdr;
106}
107
108
109/**
110 * Send a type map message to the neighbour.
111 *
112 * @param cls the type map message
113 * @param key neighbour's identity
114 * @param value 'struct Neighbour' of the target
115 * @return always GNUNET_OK
116 */
117static int
118send_type_map_to_neighbour (void *cls, const GNUNET_HashCode * key, void *value)
119{ 155{
120 struct GNUNET_MessageHeader *hdr = cls;
121 struct Neighbour *n = value;
122 struct MessageEntry *m;
123 uint16_t size;
124
125 if (n == &self)
126 return GNUNET_OK;
127 size = ntohs (hdr->size);
128 m = GNUNET_malloc (sizeof (struct MessageEntry) + size);
129 memcpy (&m[1], hdr, size);
130 m->deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
131 m->slack_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
132 m->priority = UINT_MAX;
133 m->sender_status = n->status;
134 m->size = size;
135 m->next = n->messages;
136 n->messages = m;
137 return GNUNET_OK;
138} 156}
139 157
140 158
141 159void
142/** 160GSC_TYPEMAP_done ()
143 * Send my type map to all connected peers (it got changed).
144 */
145static void
146broadcast_my_type_map ()
147{ 161{
148 struct GNUNET_MessageHeader *hdr;
149
150 if (NULL == neighbours)
151 return;
152 hdr = compute_type_map_message ();
153 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
154 &send_type_map_to_neighbour, hdr);
155 GNUNET_free (hdr);
156} 162}
157 163
158 164/* end of gnunet-service-core_typemap.c */
159