aboutsummaryrefslogtreecommitdiff
path: root/src/core/gnunet-service-core_typemap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/gnunet-service-core_typemap.c')
-rw-r--r--src/core/gnunet-service-core_typemap.c223
1 files changed, 112 insertions, 111 deletions
diff --git a/src/core/gnunet-service-core_typemap.c b/src/core/gnunet-service-core_typemap.c
index 55cd9fb14..645127b99 100644
--- a/src/core/gnunet-service-core_typemap.c
+++ b/src/core/gnunet-service-core_typemap.c
@@ -36,7 +36,8 @@
36 * A type map describing which messages a given neighbour is able 36 * A type map describing which messages a given neighbour is able
37 * to process. 37 * to process.
38 */ 38 */
39struct GSC_TypeMap { 39struct GSC_TypeMap
40{
40 uint32_t bits[(UINT16_MAX + 1) / 32]; 41 uint32_t bits[(UINT16_MAX + 1) / 32];
41}; 42};
42 43
@@ -66,7 +67,7 @@ static int hash_current;
66 * Our type map changed, recompute its hash. 67 * Our type map changed, recompute its hash.
67 */ 68 */
68static void 69static void
69rehash_typemap() 70rehash_typemap ()
70{ 71{
71 hash_current = GNUNET_NO; 72 hash_current = GNUNET_NO;
72} 73}
@@ -79,9 +80,9 @@ rehash_typemap()
79 * @param hc where to store the hash code 80 * @param hc where to store the hash code
80 */ 81 */
81void 82void
82GSC_TYPEMAP_hash(const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc) 83GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
83{ 84{
84 GNUNET_CRYPTO_hash(tm, sizeof(struct GSC_TypeMap), hc); 85 GNUNET_CRYPTO_hash (tm, sizeof(struct GSC_TypeMap), hc);
85} 86}
86 87
87 88
@@ -92,14 +93,14 @@ GSC_TYPEMAP_hash(const struct GSC_TypeMap *tm, struct GNUNET_HashCode *hc)
92 * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not 93 * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not
93 */ 94 */
94int 95int
95GSC_TYPEMAP_check_hash(const struct GNUNET_HashCode *hc) 96GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
96{ 97{
97 if (GNUNET_NO == hash_current) 98 if (GNUNET_NO == hash_current)
98 { 99 {
99 GSC_TYPEMAP_hash(&my_type_map, &my_tm_hash); 100 GSC_TYPEMAP_hash (&my_type_map, &my_tm_hash);
100 hash_current = GNUNET_YES; 101 hash_current = GNUNET_YES;
101 } 102 }
102 return (0 == memcmp(hc, &my_tm_hash, sizeof(struct GNUNET_HashCode))) 103 return (0 == memcmp (hc, &my_tm_hash, sizeof(struct GNUNET_HashCode)))
103 ? GNUNET_YES 104 ? GNUNET_YES
104 : GNUNET_NO; 105 : GNUNET_NO;
105} 106}
@@ -111,39 +112,39 @@ GSC_TYPEMAP_check_hash(const struct GNUNET_HashCode *hc)
111 * @return this peers current type map message. 112 * @return this peers current type map message.
112 */ 113 */
113struct GNUNET_MessageHeader * 114struct GNUNET_MessageHeader *
114GSC_TYPEMAP_compute_type_map_message() 115GSC_TYPEMAP_compute_type_map_message ()
115{ 116{
116 char *tmp; 117 char *tmp;
117 uLongf dlen; 118 uLongf dlen;
118 struct GNUNET_MessageHeader *hdr; 119 struct GNUNET_MessageHeader *hdr;
119 120
120#ifdef compressBound 121#ifdef compressBound
121 dlen = compressBound(sizeof(my_type_map)); 122 dlen = compressBound (sizeof(my_type_map));
122#else 123#else
123 dlen = sizeof(my_type_map) + (sizeof(my_type_map) / 100) + 20; 124 dlen = sizeof(my_type_map) + (sizeof(my_type_map) / 100) + 20;
124 /* documentation says 100.1% oldSize + 12 bytes, but we 125 /* documentation says 100.1% oldSize + 12 bytes, but we
125 * should be able to overshoot by more to be safe */ 126 * should be able to overshoot by more to be safe */
126#endif 127#endif
127 hdr = GNUNET_malloc(dlen + sizeof(struct GNUNET_MessageHeader)); 128 hdr = GNUNET_malloc (dlen + sizeof(struct GNUNET_MessageHeader));
128 tmp = (char *)&hdr[1]; 129 tmp = (char *) &hdr[1];
129 if ((Z_OK != compress2((Bytef *)tmp, 130 if ((Z_OK != compress2 ((Bytef *) tmp,
130 &dlen, 131 &dlen,
131 (const Bytef *)&my_type_map, 132 (const Bytef *) &my_type_map,
132 sizeof(my_type_map), 133 sizeof(my_type_map),
133 9)) || 134 9)) ||
134 (dlen >= sizeof(my_type_map))) 135 (dlen >= sizeof(my_type_map)))
135 { 136 {
136 /* compression failed, use uncompressed map */ 137 /* compression failed, use uncompressed map */
137 dlen = sizeof(my_type_map); 138 dlen = sizeof(my_type_map);
138 GNUNET_memcpy(tmp, &my_type_map, sizeof(my_type_map)); 139 GNUNET_memcpy (tmp, &my_type_map, sizeof(my_type_map));
139 hdr->type = htons(GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP); 140 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
140 } 141 }
141 else 142 else
142 { 143 {
143 /* compression worked, use compressed map */ 144 /* compression worked, use compressed map */
144 hdr->type = htons(GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP); 145 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
145 } 146 }
146 hdr->size = htons((uint16_t)dlen + sizeof(struct GNUNET_MessageHeader)); 147 hdr->size = htons ((uint16_t) dlen + sizeof(struct GNUNET_MessageHeader));
147 return hdr; 148 return hdr;
148} 149}
149 150
@@ -155,52 +156,52 @@ GSC_TYPEMAP_compute_type_map_message()
155 * @return NULL on error 156 * @return NULL on error
156 */ 157 */
157struct GSC_TypeMap * 158struct GSC_TypeMap *
158GSC_TYPEMAP_get_from_message(const struct GNUNET_MessageHeader *msg) 159GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
159{ 160{
160 struct GSC_TypeMap *ret; 161 struct GSC_TypeMap *ret;
161 uint16_t size; 162 uint16_t size;
162 uLongf dlen; 163 uLongf dlen;
163 164
164 size = ntohs(msg->size); 165 size = ntohs (msg->size);
165 switch (ntohs(msg->type)) 166 switch (ntohs (msg->type))
167 {
168 case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP:
169 GNUNET_STATISTICS_update (GSC_stats,
170 gettext_noop ("# type maps received"),
171 1,
172 GNUNET_NO);
173 if (size != sizeof(struct GSC_TypeMap))
166 { 174 {
167 case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP: 175 GNUNET_break_op (0);
168 GNUNET_STATISTICS_update(GSC_stats,
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
181 case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
182 GNUNET_STATISTICS_update(GSC_stats,
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; 176 return NULL;
203 } 177 }
178 ret = GNUNET_new (struct GSC_TypeMap);
179 GNUNET_memcpy (ret, &msg[1], sizeof(struct GSC_TypeMap));
180 return ret;
181
182 case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
183 GNUNET_STATISTICS_update (GSC_stats,
184 gettext_noop ("# type maps received"),
185 1,
186 GNUNET_NO);
187 ret = GNUNET_new (struct GSC_TypeMap);
188 dlen = sizeof(struct GSC_TypeMap);
189 if ((Z_OK != uncompress ((Bytef *) ret,
190 &dlen,
191 (const Bytef *) &msg[1],
192 (uLong) size)) ||
193 (dlen != sizeof(struct GSC_TypeMap)))
194 {
195 GNUNET_break_op (0);
196 GNUNET_free (ret);
197 return NULL;
198 }
199 return ret;
200
201 default:
202 GNUNET_break (0);
203 return NULL;
204 }
204} 205}
205 206
206 207
@@ -208,17 +209,17 @@ GSC_TYPEMAP_get_from_message(const struct GNUNET_MessageHeader *msg)
208 * Send my type map to all connected peers (it got changed). 209 * Send my type map to all connected peers (it got changed).
209 */ 210 */
210static void 211static void
211broadcast_my_type_map() 212broadcast_my_type_map ()
212{ 213{
213 struct GNUNET_MessageHeader *hdr; 214 struct GNUNET_MessageHeader *hdr;
214 215
215 hdr = GSC_TYPEMAP_compute_type_map_message(); 216 hdr = GSC_TYPEMAP_compute_type_map_message ();
216 GNUNET_STATISTICS_update(GSC_stats, 217 GNUNET_STATISTICS_update (GSC_stats,
217 gettext_noop("# updates to my type map"), 218 gettext_noop ("# updates to my type map"),
218 1, 219 1,
219 GNUNET_NO); 220 GNUNET_NO);
220 GSC_SESSIONS_broadcast_typemap(hdr); 221 GSC_SESSIONS_broadcast_typemap (hdr);
221 GNUNET_free(hdr); 222 GNUNET_free (hdr);
222} 223}
223 224
224 225
@@ -229,26 +230,26 @@ broadcast_my_type_map()
229 * @param tlen number of entries in @a types 230 * @param tlen number of entries in @a types
230 */ 231 */
231void 232void
232GSC_TYPEMAP_add(const uint16_t *types, unsigned int tlen) 233GSC_TYPEMAP_add (const uint16_t *types, unsigned int tlen)
233{ 234{
234 unsigned int i; 235 unsigned int i;
235 int changed; 236 int changed;
236 237
237 changed = GNUNET_NO; 238 changed = GNUNET_NO;
238 for (i = 0; i < tlen; i++) 239 for (i = 0; i < tlen; i++)
240 {
241 if (0 == map_counters[types[i]]++)
239 { 242 {
240 if (0 == map_counters[types[i]]++) 243 my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
241 { 244 changed = GNUNET_YES;
242 my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
243 changed = GNUNET_YES;
244 }
245 } 245 }
246 }
246 if (GNUNET_YES == changed) 247 if (GNUNET_YES == changed)
247 { 248 {
248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Typemap changed, broadcasting!\n"); 249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Typemap changed, broadcasting!\n");
249 rehash_typemap(); 250 rehash_typemap ();
250 broadcast_my_type_map(); 251 broadcast_my_type_map ();
251 } 252 }
252} 253}
253 254
254 255
@@ -259,24 +260,24 @@ GSC_TYPEMAP_add(const uint16_t *types, unsigned int tlen)
259 * @param tlen length of the @a types array 260 * @param tlen length of the @a types array
260 */ 261 */
261void 262void
262GSC_TYPEMAP_remove(const uint16_t *types, unsigned int tlen) 263GSC_TYPEMAP_remove (const uint16_t *types, unsigned int tlen)
263{ 264{
264 int changed; 265 int changed;
265 266
266 changed = GNUNET_NO; 267 changed = GNUNET_NO;
267 for (unsigned int i = 0; i < tlen; i++) 268 for (unsigned int i = 0; i < tlen; i++)
269 {
270 if (0 == --map_counters[types[i]])
268 { 271 {
269 if (0 == --map_counters[types[i]]) 272 my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
270 { 273 changed = GNUNET_YES;
271 my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
272 changed = GNUNET_YES;
273 }
274 } 274 }
275 }
275 if (GNUNET_YES == changed) 276 if (GNUNET_YES == changed)
276 { 277 {
277 rehash_typemap(); 278 rehash_typemap ();
278 broadcast_my_type_map(); 279 broadcast_my_type_map ();
279 } 280 }
280} 281}
281 282
282 283
@@ -290,9 +291,9 @@ GSC_TYPEMAP_remove(const uint16_t *types, unsigned int tlen)
290 * @return #GNUNET_YES if a type is in the map, #GNUNET_NO if not 291 * @return #GNUNET_YES if a type is in the map, #GNUNET_NO if not
291 */ 292 */
292int 293int
293GSC_TYPEMAP_test_match(const struct GSC_TypeMap *tmap, 294GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
294 const uint16_t *types, 295 const uint16_t *types,
295 unsigned int tcnt) 296 unsigned int tcnt)
296{ 297{
297 if (NULL == tmap) 298 if (NULL == tmap)
298 return GNUNET_NO; 299 return GNUNET_NO;
@@ -314,15 +315,15 @@ GSC_TYPEMAP_test_match(const struct GSC_TypeMap *tmap,
314 * @return updated type map (fresh copy) 315 * @return updated type map (fresh copy)
315 */ 316 */
316struct GSC_TypeMap * 317struct GSC_TypeMap *
317GSC_TYPEMAP_extend(const struct GSC_TypeMap *tmap, 318GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap,
318 const uint16_t *types, 319 const uint16_t *types,
319 unsigned int tcnt) 320 unsigned int tcnt)
320{ 321{
321 struct GSC_TypeMap *ret; 322 struct GSC_TypeMap *ret;
322 323
323 ret = GNUNET_new(struct GSC_TypeMap); 324 ret = GNUNET_new (struct GSC_TypeMap);
324 if (NULL != tmap) 325 if (NULL != tmap)
325 GNUNET_memcpy(ret, tmap, sizeof(struct GSC_TypeMap)); 326 GNUNET_memcpy (ret, tmap, sizeof(struct GSC_TypeMap));
326 for (unsigned int i = 0; i < tcnt; i++) 327 for (unsigned int i = 0; i < tcnt; i++)
327 ret->bits[types[i] / 32] |= (1 << (types[i] % 32)); 328 ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
328 return ret; 329 return ret;
@@ -335,9 +336,9 @@ GSC_TYPEMAP_extend(const struct GSC_TypeMap *tmap,
335 * @return an empty type map 336 * @return an empty type map
336 */ 337 */
337struct GSC_TypeMap * 338struct GSC_TypeMap *
338GSC_TYPEMAP_create() 339GSC_TYPEMAP_create ()
339{ 340{
340 return GNUNET_new(struct GSC_TypeMap); 341 return GNUNET_new (struct GSC_TypeMap);
341} 342}
342 343
343 344
@@ -347,9 +348,9 @@ GSC_TYPEMAP_create()
347 * @param tmap a type map 348 * @param tmap a type map
348 */ 349 */
349void 350void
350GSC_TYPEMAP_destroy(struct GSC_TypeMap *tmap) 351GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
351{ 352{
352 GNUNET_free(tmap); 353 GNUNET_free (tmap);
353} 354}
354 355
355 356
@@ -357,7 +358,7 @@ GSC_TYPEMAP_destroy(struct GSC_TypeMap *tmap)
357 * Initialize typemap subsystem. 358 * Initialize typemap subsystem.
358 */ 359 */
359void 360void
360GSC_TYPEMAP_init() 361GSC_TYPEMAP_init ()
361{ 362{
362 /* nothing to do */ 363 /* nothing to do */
363} 364}
@@ -367,7 +368,7 @@ GSC_TYPEMAP_init()
367 * Shutdown typemap subsystem. 368 * Shutdown typemap subsystem.
368 */ 369 */
369void 370void
370GSC_TYPEMAP_done() 371GSC_TYPEMAP_done ()
371{ 372{
372 /* nothing to do */ 373 /* nothing to do */
373} 374}