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