aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-service-transport_blacklist.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-03-27 19:52:26 +0000
committerChristian Grothoff <christian@grothoff.org>2013-03-27 19:52:26 +0000
commitff8b1fd6a1de28ca439270b286e2d446fc40de15 (patch)
tree0df06a2b5510ca57de93eb440a200836fb8ff4b2 /src/transport/gnunet-service-transport_blacklist.c
parent0ba623e7e8dc6cfccd0954ba0a0c541ae9686d1c (diff)
downloadgnunet-ff8b1fd6a1de28ca439270b286e2d446fc40de15.tar.gz
gnunet-ff8b1fd6a1de28ca439270b286e2d446fc40de15.zip
-code cleanup
Diffstat (limited to 'src/transport/gnunet-service-transport_blacklist.c')
-rw-r--r--src/transport/gnunet-service-transport_blacklist.c252
1 files changed, 45 insertions, 207 deletions
diff --git a/src/transport/gnunet-service-transport_blacklist.c b/src/transport/gnunet-service-transport_blacklist.c
index 361bf9c4e..3f9616e91 100644
--- a/src/transport/gnunet-service-transport_blacklist.c
+++ b/src/transport/gnunet-service-transport_blacklist.c
@@ -207,171 +207,6 @@ client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
207 207
208 208
209/** 209/**
210 * Read the blacklist file, containing transport:peer entries.
211 * Provided the transport is loaded, set up hashmap with these
212 * entries to blacklist peers by transport.
213 *
214 */
215static void
216read_blacklist_file ()
217{
218 char *fn;
219 char *data;
220 size_t pos;
221 size_t colon_pos;
222 int tsize;
223 struct GNUNET_PeerIdentity pid;
224 uint64_t fsize;
225 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
226 unsigned int entries_found;
227 char *transport_name;
228
229 if (GNUNET_OK !=
230 GNUNET_CONFIGURATION_get_value_filename (GST_cfg, "TRANSPORT",
231 "BLACKLIST_FILE", &fn))
232 {
233 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_DEBUG,
234 "transport", "BLACKLIST_FILE");
235 return;
236 }
237 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
238 {
239 GNUNET_free (fn);
240 return; /* no blacklist */
241 }
242 if (GNUNET_OK != GNUNET_DISK_file_size (fn,
243 &fsize, GNUNET_NO, GNUNET_YES))
244 {
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 _("Could not read blacklist file `%s'\n"), fn);
247 GNUNET_free (fn);
248 return;
249 }
250 if (0 == fsize)
251 {
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist file `%s' is empty.\n",
253 fn);
254 GNUNET_free (fn);
255 return;
256 }
257 /* FIXME: use mmap */
258 data = GNUNET_malloc_large (fsize);
259 GNUNET_assert (data != NULL);
260 if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
261 {
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263 _("Failed to read blacklist from `%s'\n"), fn);
264 GNUNET_free (fn);
265 GNUNET_free (data);
266 return;
267 }
268 entries_found = 0;
269 pos = 0;
270 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
271 pos++;
272 while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
273 (pos <=
274 fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
275 {
276 colon_pos = pos;
277 while ((colon_pos < fsize) && (data[colon_pos] != ':') &&
278 (!isspace ((unsigned char) data[colon_pos])))
279 colon_pos++;
280 if (colon_pos >= fsize)
281 {
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
283 _
284 ("Syntax error in blacklist file at offset %llu, giving up!\n"),
285 (unsigned long long) colon_pos);
286 GNUNET_free (fn);
287 GNUNET_free (data);
288 return;
289 }
290
291 if (isspace ((unsigned char) data[colon_pos]))
292 {
293 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
294 _
295 ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
296 (unsigned long long) colon_pos);
297 pos = colon_pos;
298 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
299 pos++;
300 continue;
301 }
302 tsize = colon_pos - pos;
303 if ((pos >= fsize) || (pos + tsize >= fsize) ||
304 (tsize == 0))
305 {
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
307 _
308 ("Syntax error in blacklist file at offset %llu, giving up!\n"),
309 (unsigned long long) colon_pos);
310 GNUNET_free (fn);
311 GNUNET_free (data);
312 return;
313 }
314
315 if (tsize < 1)
316 continue;
317
318 transport_name = GNUNET_malloc (tsize + 1);
319 memcpy (transport_name, &data[pos], tsize);
320 pos = colon_pos + 1;
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Read transport name `%s' in blacklist file.\n",
323 transport_name);
324 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
325 if (!isspace
326 ((unsigned char)
327 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
328 {
329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
330 _
331 ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
332 (unsigned long long) pos);
333 pos++;
334 while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
335 pos++;
336 GNUNET_free_non_null (transport_name);
337 continue;
338 }
339 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
340 if (GNUNET_OK !=
341 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
342 {
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
344 _
345 ("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
346 (unsigned long long) pos, &enc);
347 }
348 else
349 {
350 if (0 !=
351 memcmp (&pid, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
352 {
353 entries_found++;
354 GST_blacklist_add_peer (&pid, transport_name);
355 }
356 else
357 {
358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
359 _("Found myself `%s' in blacklist (useless, ignored)\n"),
360 GNUNET_i2s (&pid));
361 }
362 }
363 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
364 GNUNET_free_non_null (transport_name);
365 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
366 pos++;
367 }
368 GNUNET_STATISTICS_update (GST_stats, "# Transport entries blacklisted",
369 entries_found, GNUNET_NO);
370 GNUNET_free (data);
371 GNUNET_free (fn);
372}
373
374/**
375 * Function to iterate over options in the blacklisting section for a peer. 210 * Function to iterate over options in the blacklisting section for a peer.
376 * 211 *
377 * @param cls closure 212 * @param cls closure
@@ -379,44 +214,44 @@ read_blacklist_file ()
379 * @param option name of the option 214 * @param option name of the option
380 * @param value value of the option 215 * @param value value of the option
381 */ 216 */
382void blacklist_cfg_iter (void *cls, const char *section, 217static void
383 const char *option, 218blacklist_cfg_iter (void *cls, const char *section,
384 const char *value) 219 const char *option,
220 const char *value)
385{ 221{
386 struct GNUNET_PeerIdentity peer; 222 unsigned int *res = cls;
223 struct GNUNET_PeerIdentity peer;
387 char *plugs; 224 char *plugs;
388 char *pos; 225 char *pos;
389 int *res = cls;
390
391 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string2(option,
392 strlen (option), &peer.hashPubKey))
393 return;
394
395 if ((NULL == value) || (0 == strcmp(value, "")))
396 {
397 /* Blacklist whole peer */
398 GST_blacklist_add_peer (&peer, NULL);
399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
400 _("Adding blacklisting entry for peer `%s'\n"), GNUNET_i2s (&peer));
401 }
402 else
403 {
404 plugs = GNUNET_strdup (value);
405 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
406 {
407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
408 _("Adding blacklisting entry for peer `%s':`%s'\n"),
409 GNUNET_i2s (&peer), pos);
410 GST_blacklist_add_peer (&peer, pos);
411 }
412 GNUNET_free (plugs);
413 }
414 (*res)++;
415 226
227 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string2 (option,
228 strlen (option),
229 &peer.hashPubKey))
230 return;
231
232 if ((NULL == value) || (0 == strcmp(value, "")))
233 {
234 /* Blacklist whole peer */
235 GST_blacklist_add_peer (&peer, NULL);
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 _("Adding blacklisting entry for peer `%s'\n"), GNUNET_i2s (&peer));
238 }
239 else
240 {
241 plugs = GNUNET_strdup (value);
242 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
243 {
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245 _("Adding blacklisting entry for peer `%s':`%s'\n"),
246 GNUNET_i2s (&peer), pos);
247 GST_blacklist_add_peer (&peer, pos);
248 }
249 GNUNET_free (plugs);
250 }
251 (*res)++;
416} 252}
417 253
418 254
419
420/** 255/**
421 * Read blacklist configuration 256 * Read blacklist configuration
422 * 257 *
@@ -425,17 +260,21 @@ void blacklist_cfg_iter (void *cls, const char *section,
425 */ 260 */
426static void 261static void
427read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg, 262read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
428 const struct GNUNET_PeerIdentity *my_id) 263 const struct GNUNET_PeerIdentity *my_id)
429{ 264{
430 char *cfg_sect; 265 char cfg_sect[512];
431 int res = 0; 266 unsigned int res = 0;
432 GNUNET_asprintf (&cfg_sect, "transport-blacklist-%s", GNUNET_i2s_full (my_id)); 267
433 GNUNET_CONFIGURATION_iterate_section_values (cfg, cfg_sect, &blacklist_cfg_iter, &res); 268 GNUNET_snprintf (cfg_sect,
269 sizeof (cfg_sect),
270 "transport-blacklist-%s",
271 GNUNET_i2s_full (my_id));
272 GNUNET_CONFIGURATION_iterate_section_values (cfg, cfg_sect, &blacklist_cfg_iter, &res);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Loaded %u blacklisting entries from configuration\n", res); 274 "Loaded %u blacklisting entries from configuration\n", res);
436 GNUNET_free (cfg_sect);
437} 275}
438 276
277
439/** 278/**
440 * Start blacklist subsystem. 279 * Start blacklist subsystem.
441 * 280 *
@@ -445,12 +284,11 @@ read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
445 */ 284 */
446void 285void
447GST_blacklist_start (struct GNUNET_SERVER_Handle *server, 286GST_blacklist_start (struct GNUNET_SERVER_Handle *server,
448 const struct GNUNET_CONFIGURATION_Handle *cfg, 287 const struct GNUNET_CONFIGURATION_Handle *cfg,
449 const struct GNUNET_PeerIdentity *my_id) 288 const struct GNUNET_PeerIdentity *my_id)
450{ 289{
451 GNUNET_assert (NULL != cfg); 290 GNUNET_assert (NULL != cfg);
452 GNUNET_assert (NULL != my_id); 291 GNUNET_assert (NULL != my_id);
453 //read_blacklist_file ();
454 read_blacklist_configuration (cfg, my_id); 292 read_blacklist_configuration (cfg, my_id);
455 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification, 293 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
456 NULL); 294 NULL);