2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/gnunet-service-transport_blacklist.c
23 * @brief blacklisting implementation
24 * @author Christian Grothoff
27 #include "gnunet-service-transport.h"
28 #include "gnunet-service-transport_blacklist.h"
29 #include "gnunet-service-transport_neighbours.h"
30 #include "transport.h"
34 * Size of the blacklist hash map.
36 #define TRANSPORT_BLACKLIST_HT_SIZE 64
40 * Context we use when performing a blacklist check.
42 struct GST_BlacklistCheck;
46 * Information kept for each client registered to perform
52 * This is a linked list.
54 struct Blacklisters *next;
57 * This is a linked list.
59 struct Blacklisters *prev;
62 * Client responsible for this entry.
64 struct GNUNET_SERVER_Client *client;
67 * Blacklist check that we're currently performing (or NULL
68 * if we're performing one that has been cancelled).
70 struct GST_BlacklistCheck *bc;
73 * Set to GNUNET_YES if we're currently waiting for a reply.
75 int waiting_for_reply;
82 * Context we use when performing a blacklist check.
84 struct GST_BlacklistCheck
88 * This is a linked list.
90 struct GST_BlacklistCheck *next;
93 * This is a linked list.
95 struct GST_BlacklistCheck *prev;
100 struct GNUNET_PeerIdentity peer;
103 * Continuation to call with the result.
105 GST_BlacklistTestContinuation cont;
113 * Current transmission request handle for this client, or NULL if no
114 * request is pending.
116 struct GNUNET_CONNECTION_TransmitHandle *th;
119 * Our current position in the blacklisters list.
121 struct Blacklisters *bl_pos;
124 * Current task performing the check.
126 GNUNET_SCHEDULER_TaskIdentifier task;
132 * Head of DLL of active blacklisting queries.
134 static struct GST_BlacklistCheck *bc_head;
137 * Tail of DLL of active blacklisting queries.
139 static struct GST_BlacklistCheck *bc_tail;
142 * Head of DLL of blacklisting clients.
144 static struct Blacklisters *bl_head;
147 * Tail of DLL of blacklisting clients.
149 static struct Blacklisters *bl_tail;
152 * Hashmap of blacklisted peers. Values are of type 'char *' (transport names),
153 * can be NULL if we have no static blacklist.
155 static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
159 * Perform next action in the blacklist check.
161 * @param cls the 'struct BlacklistCheck*'
165 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
169 * Called whenever a client is disconnected. Frees our
170 * resources associated with that client.
172 * @param cls closure (unused)
173 * @param client identification of the client
176 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
178 struct Blacklisters *bl;
179 struct GST_BlacklistCheck *bc;
183 for (bl = bl_head; bl != NULL; bl = bl->next)
185 if (bl->client != client)
187 for (bc = bc_head; bc != NULL; bc = bc->next)
189 if (bc->bl_pos != bl)
191 bc->bl_pos = bl->next;
194 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
197 if (bc->task == GNUNET_SCHEDULER_NO_TASK)
198 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
201 GNUNET_CONTAINER_DLL_remove (bl_head, bl_tail, bl);
202 GNUNET_SERVER_client_drop (bl->client);
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.
216 read_blacklist_file ()
223 struct GNUNET_PeerIdentity pid;
225 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
226 unsigned int entries_found;
227 char *transport_name;
230 GNUNET_CONFIGURATION_get_value_filename (GST_cfg, "TRANSPORT",
231 "BLACKLIST_FILE", &fn))
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Option `%s' in section `%s' not specified!\n",
236 "BLACKLIST_FILE", "TRANSPORT");
240 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
241 GNUNET_DISK_fn_write (fn, NULL, 0,
242 GNUNET_DISK_PERM_USER_READ |
243 GNUNET_DISK_PERM_USER_WRITE);
244 if (0 != STAT (fn, &frstat))
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 _("Could not read blacklist file `%s'\n"), fn);
251 if (frstat.st_size == 0)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Blacklist file `%s' is empty.\n"),
260 /* FIXME: use mmap */
261 data = GNUNET_malloc_large (frstat.st_size);
262 GNUNET_assert (data != NULL);
263 if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 _("Failed to read blacklist from `%s'\n"), fn);
273 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
275 while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
277 frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
280 while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') &&
281 (!isspace ((unsigned char) data[colon_pos])))
283 if (colon_pos >= frstat.st_size)
285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 ("Syntax error in blacklist file at offset %llu, giving up!\n"),
288 (unsigned long long) colon_pos);
294 if (isspace ((unsigned char) data[colon_pos]))
296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
298 ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
299 (unsigned long long) colon_pos);
301 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
305 tsize = colon_pos - pos;
306 if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) ||
309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 ("Syntax error in blacklist file at offset %llu, giving up!\n"),
312 (unsigned long long) colon_pos);
321 transport_name = GNUNET_malloc (tsize + 1);
322 memcpy (transport_name, &data[pos], tsize);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Read transport name `%s' in blacklist file.\n",
329 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
332 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
336 ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
337 (unsigned long long) pos);
339 while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
341 GNUNET_free_non_null (transport_name);
344 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
346 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
350 ("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
351 (unsigned long long) pos, &enc);
356 memcmp (&pid, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
359 GST_blacklist_add_peer (&pid, transport_name);
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 _("Found myself `%s' in blacklist (useless, ignored)\n"),
368 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
369 GNUNET_free_non_null (transport_name);
370 while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
373 GNUNET_STATISTICS_update (GST_stats, "# Transport entries blacklisted",
374 entries_found, GNUNET_NO);
381 * Start blacklist subsystem.
383 * @param server server used to accept clients from
386 GST_blacklist_start (struct GNUNET_SERVER_Handle *server)
388 read_blacklist_file ();
389 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
395 * Free the given entry in the blacklist.
398 * @param key host identity (unused)
399 * @param value the blacklist entry
400 * @return GNUNET_OK (continue to iterate)
403 free_blacklist_entry (void *cls, const GNUNET_HashCode * key, void *value)
413 * Stop blacklist subsystem.
416 GST_blacklist_stop ()
418 if (NULL != blacklist)
420 GNUNET_CONTAINER_multihashmap_iterate (blacklist, &free_blacklist_entry,
422 GNUNET_CONTAINER_multihashmap_destroy (blacklist);
429 * Transmit blacklist query to the client.
431 * @param cls the 'struct GST_BlacklistCheck'
432 * @param size number of bytes allowed
433 * @param buf where to copy the message
434 * @return number of bytes copied to buf
437 transmit_blacklist_message (void *cls, size_t size, void *buf)
439 struct GST_BlacklistCheck *bc = cls;
440 struct Blacklisters *bl;
441 struct BlacklistMessage bm;
446 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
447 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
449 "Failed to send blacklist test for peer `%s' to client\n",
450 GNUNET_i2s (&bc->peer));
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Sending blacklist test for peer `%s' to client\n",
456 GNUNET_i2s (&bc->peer));
459 bm.header.size = htons (sizeof (struct BlacklistMessage));
460 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
461 bm.is_allowed = htonl (0);
463 memcpy (buf, &bm, sizeof (bm));
464 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
465 bl->waiting_for_reply = GNUNET_YES;
471 * Perform next action in the blacklist check.
473 * @param cls the 'struct GST_BlacklistCheck*'
477 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 struct GST_BlacklistCheck *bc = cls;
480 struct Blacklisters *bl;
482 bc->task = GNUNET_SCHEDULER_NO_TASK;
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "No other blacklist clients active, will allow neighbour `%s'\n",
489 GNUNET_i2s (&bc->peer));
491 bc->cont (bc->cont_cls, &bc->peer, GNUNET_OK);
495 if ((bl->bc != NULL) || (bl->waiting_for_reply != GNUNET_NO))
496 return; /* someone else busy with this client */
499 GNUNET_SERVER_notify_transmit_ready (bl->client,
500 sizeof (struct BlacklistMessage),
501 GNUNET_TIME_UNIT_FOREVER_REL,
502 &transmit_blacklist_message, bc);
507 * Got the result about an existing connection from a new blacklister.
508 * Shutdown the neighbour if necessary.
511 * @param peer the neighbour that was investigated
512 * @param allowed GNUNET_OK if we can keep it,
513 * GNUNET_NO if we must shutdown the connection
516 confirm_or_drop_neighbour (void *cls, const struct GNUNET_PeerIdentity *peer,
519 if (GNUNET_OK == allowed)
520 return; /* we're done */
521 GNUNET_STATISTICS_update (GST_stats,
522 gettext_noop ("# disconnects due to blacklist"), 1,
524 GST_neighbours_force_disconnect (peer);
529 * Closure for 'test_connection_ok'.
531 struct TestConnectionContext
534 * Is this the first neighbour we're checking?
539 * Handle to the blacklisting client we need to ask.
541 struct Blacklisters *bl;
546 * Test if an existing connection is still acceptable given a new
547 * blacklisting client.
549 * @param cls the 'struct TestConnectionContest'
550 * @param neighbour neighbour's identity
551 * @param ats performance data
552 * @param ats_count number of entries in ats (excluding 0-termination)
553 * @param address the address
556 test_connection_ok (void *cls, const struct GNUNET_PeerIdentity *neighbour,
557 const struct GNUNET_ATS_Information *ats,
559 const struct GNUNET_HELLO_Address *address)
561 struct TestConnectionContext *tcc = cls;
562 struct GST_BlacklistCheck *bc;
564 bc = GNUNET_malloc (sizeof (struct GST_BlacklistCheck));
565 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
566 bc->peer = *neighbour;
567 bc->cont = &confirm_or_drop_neighbour;
569 bc->bl_pos = tcc->bl;
570 if (GNUNET_YES == tcc->first)
572 /* all would wait for the same client, no need to
573 * create more than just the first task right now */
574 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
575 tcc->first = GNUNET_NO;
582 * Initialize a blacklisting client. We got a blacklist-init
583 * message from this client, add him to the list of clients
584 * to query for blacklisting.
587 * @param client the client
588 * @param message the blacklist-init message that was sent
591 GST_blacklist_handle_init (void *cls, struct GNUNET_SERVER_Client *client,
592 const struct GNUNET_MessageHeader *message)
594 struct Blacklisters *bl;
595 struct TestConnectionContext tcc;
600 if (bl->client == client)
603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608 bl = GNUNET_malloc (sizeof (struct Blacklisters));
610 GNUNET_SERVER_client_keep (client);
611 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
613 /* confirm that all existing connections are OK! */
615 tcc.first = GNUNET_YES;
616 GST_neighbours_iterate (&test_connection_ok, &tcc);
621 * A blacklisting client has sent us reply. Process it.
624 * @param client the client
625 * @param message the blacklist-init message that was sent
628 GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
629 const struct GNUNET_MessageHeader *message)
631 const struct BlacklistMessage *msg =
632 (const struct BlacklistMessage *) message;
633 struct Blacklisters *bl;
634 struct GST_BlacklistCheck *bc;
637 while ((bl != NULL) && (bl->client != client))
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist client disconnected\n");
644 /* FIXME: other error handling here!? */
645 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
650 bl->waiting_for_reply = GNUNET_NO;
653 /* only run this if the blacklist check has not been
654 * cancelled in the meantime... */
655 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Blacklist check failed, peer not allowed\n");
661 bc->cont (bc->cont_cls, &bc->peer, GNUNET_NO);
662 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Blacklist check succeeded, continuing with checks\n");
671 bc->bl_pos = bc->bl_pos->next;
672 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
675 /* check if any other bc's are waiting for this blacklister */
677 for (bc = bc_head; bc != NULL; bc = bc->next)
678 if ((bc->bl_pos == bl) && (GNUNET_SCHEDULER_NO_TASK == bc->task))
680 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
687 * Add the given peer to the blacklist (for the given transport).
689 * @param peer peer to blacklist
690 * @param transport_name transport to blacklist for this peer, NULL for all
693 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
694 const char *transport_name)
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Adding peer `%s' with plugin `%s' to blacklist\n",
699 GNUNET_i2s (peer), transport_name);
701 if (blacklist == NULL)
703 GNUNET_CONTAINER_multihashmap_create (TRANSPORT_BLACKLIST_HT_SIZE);
704 GNUNET_CONTAINER_multihashmap_put (blacklist, &peer->hashPubKey,
705 GNUNET_strdup (transport_name),
706 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
711 * Test if the given blacklist entry matches. If so,
712 * abort the iteration.
714 * @param cls the transport name to match (const char*)
715 * @param key the key (unused)
716 * @param value the 'char *' (name of a blacklisted transport)
717 * @return GNUNET_OK if the entry does not match, GNUNET_NO if it matches
720 test_blacklisted (void *cls, const GNUNET_HashCode * key, void *value)
722 const char *transport_name = cls;
725 /* blacklist check for specific no specific transport*/
726 if (transport_name == NULL)
729 /* blacklist check for specific transport */
730 if (0 == strcmp (transport_name, be))
731 return GNUNET_NO; /* abort iteration! */
737 * Test if a peer/transport combination is blacklisted.
739 * @param peer the identity of the peer to test
740 * @param transport_name name of the transport to test, never NULL
741 * @param cont function to call with result
742 * @param cont_cls closure for 'cont'
743 * @return handle to the blacklist check, NULL if the decision
744 * was made instantly and 'cont' was already called
746 struct GST_BlacklistCheck *
747 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
748 const char *transport_name,
749 GST_BlacklistTestContinuation cont, void *cont_cls)
751 struct GST_BlacklistCheck *bc;
753 GNUNET_assert (peer != NULL);
755 if ((blacklist != NULL) &&
757 GNUNET_CONTAINER_multihashmap_get_multiple (blacklist, &peer->hashPubKey,
759 (void *) transport_name)))
761 /* disallowed by config, disapprove instantly */
762 GNUNET_STATISTICS_update (GST_stats,
763 gettext_noop ("# disconnects due to blacklist"),
766 cont (cont_cls, peer, GNUNET_NO);
772 /* no blacklist clients, approve instantly */
774 cont (cont_cls, peer, GNUNET_OK);
778 /* need to query blacklist clients */
779 bc = GNUNET_malloc (sizeof (struct GST_BlacklistCheck));
780 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
783 bc->cont_cls = cont_cls;
784 bc->bl_pos = bl_head;
785 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
791 * Cancel a blacklist check.
793 * @param bc check to cancel
796 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
798 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
799 if (bc->bl_pos != NULL)
801 if (bc->bl_pos->bc == bc)
803 /* we're at the head of the queue, remove us! */
804 bc->bl_pos->bc = NULL;
807 if (GNUNET_SCHEDULER_NO_TASK != bc->task)
809 GNUNET_SCHEDULER_cancel (bc->task);
810 bc->task = GNUNET_SCHEDULER_NO_TASK;
814 GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
821 /* end of file gnunet-service-transport_blacklist.c */