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_SERVER_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_SERVER_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))
233 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_DEBUG,
234 "transport", "BLACKLIST_FILE");
237 if (GNUNET_OK != GNUNET_DISK_file_test (fn))
240 return; /* no blacklist */
242 if (GNUNET_OK != GNUNET_DISK_file_size (fn,
243 &fsize, GNUNET_NO, GNUNET_YES))
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 _("Could not read blacklist file `%s'\n"), fn);
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist file `%s' is empty.\n",
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))
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263 _("Failed to read blacklist from `%s'\n"), fn);
270 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
272 while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
274 fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
277 while ((colon_pos < fsize) && (data[colon_pos] != ':') &&
278 (!isspace ((unsigned char) data[colon_pos])))
280 if (colon_pos >= fsize)
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
284 ("Syntax error in blacklist file at offset %llu, giving up!\n"),
285 (unsigned long long) colon_pos);
291 if (isspace ((unsigned char) data[colon_pos]))
293 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295 ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
296 (unsigned long long) colon_pos);
298 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
302 tsize = colon_pos - pos;
303 if ((pos >= fsize) || (pos + tsize >= fsize) ||
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
308 ("Syntax error in blacklist file at offset %llu, giving up!\n"),
309 (unsigned long long) colon_pos);
318 transport_name = GNUNET_malloc (tsize + 1);
319 memcpy (transport_name, &data[pos], tsize);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Read transport name `%s' in blacklist file.\n",
324 memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
327 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
331 ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
332 (unsigned long long) pos);
334 while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
336 GNUNET_free_non_null (transport_name);
339 enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
341 GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
345 ("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
346 (unsigned long long) pos, &enc);
351 memcmp (&pid, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
354 GST_blacklist_add_peer (&pid, transport_name);
358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
359 _("Found myself `%s' in blacklist (useless, ignored)\n"),
363 pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
364 GNUNET_free_non_null (transport_name);
365 while ((pos < fsize) && isspace ((unsigned char) data[pos]))
368 GNUNET_STATISTICS_update (GST_stats, "# Transport entries blacklisted",
369 entries_found, GNUNET_NO);
376 * Start blacklist subsystem.
378 * @param server server used to accept clients from
381 GST_blacklist_start (struct GNUNET_SERVER_Handle *server)
383 read_blacklist_file ();
384 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
390 * Free the given entry in the blacklist.
393 * @param key host identity (unused)
394 * @param value the blacklist entry
395 * @return GNUNET_OK (continue to iterate)
398 free_blacklist_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
408 * Stop blacklist subsystem.
411 GST_blacklist_stop ()
413 if (NULL != blacklist)
415 GNUNET_CONTAINER_multihashmap_iterate (blacklist, &free_blacklist_entry,
417 GNUNET_CONTAINER_multihashmap_destroy (blacklist);
424 * Transmit blacklist query to the client.
426 * @param cls the 'struct GST_BlacklistCheck'
427 * @param size number of bytes allowed
428 * @param buf where to copy the message
429 * @return number of bytes copied to buf
432 transmit_blacklist_message (void *cls, size_t size, void *buf)
434 struct GST_BlacklistCheck *bc = cls;
435 struct Blacklisters *bl;
436 struct BlacklistMessage bm;
441 GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
442 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
444 "Failed to send blacklist test for peer `%s' to client\n",
445 GNUNET_i2s (&bc->peer));
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Sending blacklist test for peer `%s' to client\n",
450 GNUNET_i2s (&bc->peer));
452 bm.header.size = htons (sizeof (struct BlacklistMessage));
453 bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
454 bm.is_allowed = htonl (0);
456 memcpy (buf, &bm, sizeof (bm));
457 GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
458 bl->waiting_for_reply = GNUNET_YES;
464 * Perform next action in the blacklist check.
466 * @param cls the 'struct GST_BlacklistCheck*'
470 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
472 struct GST_BlacklistCheck *bc = cls;
473 struct Blacklisters *bl;
475 bc->task = GNUNET_SCHEDULER_NO_TASK;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "No other blacklist clients active, will allow neighbour `%s'\n",
481 GNUNET_i2s (&bc->peer));
482 bc->cont (bc->cont_cls, &bc->peer, GNUNET_OK);
483 GNUNET_CONTAINER_DLL_remove(bc_head, bc_tail, bc);
487 if ((bl->bc != NULL) || (bl->waiting_for_reply != GNUNET_NO))
488 return; /* someone else busy with this client */
491 GNUNET_SERVER_notify_transmit_ready (bl->client,
492 sizeof (struct BlacklistMessage),
493 GNUNET_TIME_UNIT_FOREVER_REL,
494 &transmit_blacklist_message, bc);
499 * Got the result about an existing connection from a new blacklister.
500 * Shutdown the neighbour if necessary.
503 * @param peer the neighbour that was investigated
504 * @param allowed GNUNET_OK if we can keep it,
505 * GNUNET_NO if we must shutdown the connection
508 confirm_or_drop_neighbour (void *cls, const struct GNUNET_PeerIdentity *peer,
511 if (GNUNET_OK == allowed)
512 return; /* we're done */
513 GNUNET_STATISTICS_update (GST_stats,
514 gettext_noop ("# disconnects due to blacklist"), 1,
516 GST_neighbours_force_disconnect (peer);
521 * Closure for 'test_connection_ok'.
523 struct TestConnectionContext
526 * Is this the first neighbour we're checking?
531 * Handle to the blacklisting client we need to ask.
533 struct Blacklisters *bl;
538 * Test if an existing connection is still acceptable given a new
539 * blacklisting client.
541 * @param cls the 'struct TestConnectionContest'
542 * @param neighbour neighbour's identity
543 * @param address the address
544 * @param bandwidth_in inbound quota in NBO
545 * @param bandwidth_out outbound quota in NBO
548 test_connection_ok (void *cls, const struct GNUNET_PeerIdentity *neighbour,
549 const struct GNUNET_HELLO_Address *address,
550 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
551 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
553 struct TestConnectionContext *tcc = cls;
554 struct GST_BlacklistCheck *bc;
556 bc = GNUNET_malloc (sizeof (struct GST_BlacklistCheck));
557 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
558 bc->peer = *neighbour;
559 bc->cont = &confirm_or_drop_neighbour;
561 bc->bl_pos = tcc->bl;
562 if (GNUNET_YES == tcc->first)
564 /* all would wait for the same client, no need to
565 * create more than just the first task right now */
566 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
567 tcc->first = GNUNET_NO;
573 * Initialize a blacklisting client. We got a blacklist-init
574 * message from this client, add him to the list of clients
575 * to query for blacklisting.
578 * @param client the client
579 * @param message the blacklist-init message that was sent
582 GST_blacklist_handle_init (void *cls, struct GNUNET_SERVER_Client *client,
583 const struct GNUNET_MessageHeader *message)
585 struct Blacklisters *bl;
586 struct TestConnectionContext tcc;
591 if (bl->client == client)
594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
599 GNUNET_SERVER_client_mark_monitor (client);
600 bl = GNUNET_malloc (sizeof (struct Blacklisters));
602 GNUNET_SERVER_client_keep (client);
603 GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
605 /* confirm that all existing connections are OK! */
607 tcc.first = GNUNET_YES;
608 GST_neighbours_iterate (&test_connection_ok, &tcc);
613 * A blacklisting client has sent us reply. Process it.
616 * @param client the client
617 * @param message the blacklist-init message that was sent
620 GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
621 const struct GNUNET_MessageHeader *message)
623 const struct BlacklistMessage *msg =
624 (const struct BlacklistMessage *) message;
625 struct Blacklisters *bl;
626 struct GST_BlacklistCheck *bc;
629 while ((bl != NULL) && (bl->client != client))
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist client disconnected\n");
634 /* FIXME: other error handling here!? */
635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
640 bl->waiting_for_reply = GNUNET_NO;
643 /* only run this if the blacklist check has not been
644 * cancelled in the meantime... */
645 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "Blacklist check failed, peer not allowed\n");
649 bc->cont (bc->cont_cls, &bc->peer, GNUNET_NO);
650 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Blacklist check succeeded, continuing with checks\n");
657 bc->bl_pos = bc->bl_pos->next;
658 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
661 /* check if any other bc's are waiting for this blacklister */
663 for (bc = bc_head; bc != NULL; bc = bc->next)
664 if ((bc->bl_pos == bl) && (GNUNET_SCHEDULER_NO_TASK == bc->task))
666 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
673 * Add the given peer to the blacklist (for the given transport).
675 * @param peer peer to blacklist
676 * @param transport_name transport to blacklist for this peer, NULL for all
679 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
680 const char *transport_name)
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Adding peer `%s' with plugin `%s' to blacklist\n",
684 GNUNET_i2s (peer), transport_name);
685 if (blacklist == NULL)
687 GNUNET_CONTAINER_multihashmap_create (TRANSPORT_BLACKLIST_HT_SIZE,
689 GNUNET_CONTAINER_multihashmap_put (blacklist, &peer->hashPubKey,
690 GNUNET_strdup (transport_name),
691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
696 * Test if the given blacklist entry matches. If so,
697 * abort the iteration.
699 * @param cls the transport name to match (const char*)
700 * @param key the key (unused)
701 * @param value the 'char *' (name of a blacklisted transport)
702 * @return GNUNET_OK if the entry does not match, GNUNET_NO if it matches
705 test_blacklisted (void *cls, const struct GNUNET_HashCode * key, void *value)
707 const char *transport_name = cls;
710 /* blacklist check for specific no specific transport*/
711 if (transport_name == NULL)
714 /* blacklist check for specific transport */
715 if (0 == strcmp (transport_name, be))
716 return GNUNET_NO; /* abort iteration! */
722 * Test if a peer/transport combination is blacklisted.
724 * @param peer the identity of the peer to test
725 * @param transport_name name of the transport to test, never NULL
726 * @param cont function to call with result
727 * @param cont_cls closure for 'cont'
728 * @return handle to the blacklist check, NULL if the decision
729 * was made instantly and 'cont' was already called
731 struct GST_BlacklistCheck *
732 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
733 const char *transport_name,
734 GST_BlacklistTestContinuation cont, void *cont_cls)
736 struct GST_BlacklistCheck *bc;
738 GNUNET_assert (peer != NULL);
740 if ((blacklist != NULL) &&
742 GNUNET_CONTAINER_multihashmap_get_multiple (blacklist, &peer->hashPubKey,
744 (void *) transport_name)))
746 /* disallowed by config, disapprove instantly */
747 GNUNET_STATISTICS_update (GST_stats,
748 gettext_noop ("# disconnects due to blacklist"),
751 cont (cont_cls, peer, GNUNET_NO);
757 /* no blacklist clients, approve instantly */
759 cont (cont_cls, peer, GNUNET_OK);
763 /* need to query blacklist clients */
764 bc = GNUNET_malloc (sizeof (struct GST_BlacklistCheck));
765 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
768 bc->cont_cls = cont_cls;
769 bc->bl_pos = bl_head;
770 bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
776 * Cancel a blacklist check.
778 * @param bc check to cancel
781 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
783 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
784 if (bc->bl_pos != NULL)
786 if (bc->bl_pos->bc == bc)
788 /* we're at the head of the queue, remove us! */
789 bc->bl_pos->bc = NULL;
792 if (GNUNET_SCHEDULER_NO_TASK != bc->task)
794 GNUNET_SCHEDULER_cancel (bc->task);
795 bc->task = GNUNET_SCHEDULER_NO_TASK;
799 GNUNET_SERVER_notify_transmit_ready_cancel (bc->th);
806 /* end of file gnunet-service-transport_blacklist.c */