-doxygen
[oweals/gnunet.git] / src / transport / gnunet-service-transport_blacklist.c
index b1a0047f726db5d3ce4e7febf0fe86162893214a..87d7539295ba91874ad8f56eeee1bea177811820 100644 (file)
 /**
  * @file transport/gnunet-service-transport_blacklist.c
  * @brief blacklisting implementation
- * @author Christian Grothoff
+ * @author Christian Grothoff, Matthias Wachs
+ * @details This is the blacklisting component of transport service. With
+ * blacklisting it is possible to deny connections to specific peers of
+ * to use a specific plugin to a specific peer. Peers can be blacklisted using
+ * the configuration or a blacklist client can be asked.
+ *
+ * To blacklist peers using the configuration you have to add a section to your
+ * configuration containing the peer id of the peer to blacklist and the plugin
+ * if required.
+ *
+ * Example:
+ * To blacklist connections to P565... on peer AG2P... using tcp add:
+ * [transport-blacklist-AG2PHES1BARB9IJCPAMJTFPVJ5V3A72S3F2A8SBUB8DAQ2V0O3V8G6G2JU56FHGFOHMQVKBSQFV98TCGTC3RJ1NINP82G0RC00N1520]
+ * P565723JO1C2HSN6J29TAQ22MN6CI8HTMUU55T0FUQG4CMDGGEQ8UCNBKUMB94GC8R9G4FB2SF9LDOBAJ6AMINBP4JHHDD6L7VD801G = tcp
+ *
+ * To blacklist connections to P565... on peer AG2P... using all plugins add:
+ * [transport-blacklist-AG2PHES1BARB9IJCPAMJTFPVJ5V3A72S3F2A8SBUB8DAQ2V0O3V8G6G2JU56FHGFOHMQVKBSQFV98TCGTC3RJ1NINP82G0RC00N1520]
+ * P565723JO1C2HSN6J29TAQ22MN6CI8HTMUU55T0FUQG4CMDGGEQ8UCNBKUMB94GC8R9G4FB2SF9LDOBAJ6AMINBP4JHHDD6L7VD801G =
+ *
+ * You can also add a blacklist client usign the blacklist api. On a blacklist
+ * check, blacklisting first checks internally if the peer is blacklisted and
+ * if not, it asks the blacklisting clients. Clients are asked if it is OK to
+ * connect to a peer ID, the plugin is omitted.
+ *
+ * On blacklist check for (peer, plugin)
+ * - Do we have a local blacklist entry for this peer and this plugin?
+ *   - YES: disallow connection
+ * - Do we have a local blacklist entry for this peer and all plugins?
+ *   - YES: disallow connection
+ * - Does one of the clients disallow?
+ *   - YES: disallow connection
+ *
  */
 #include "platform.h"
 #include "gnunet-service-transport.h"
@@ -29,7 +60,6 @@
 #include "gnunet-service-transport_neighbours.h"
 #include "transport.h"
 
-
 /**
  * Size of the blacklist hash map.
  */
@@ -152,7 +182,7 @@ static struct Blacklisters *bl_tail;
  * Hashmap of blacklisted peers.  Values are of type 'char *' (transport names),
  * can be NULL if we have no static blacklist.
  */
-static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
+static struct GNUNET_CONTAINER_MultiPeerMap *blacklist;
 
 
 /**
@@ -214,7 +244,7 @@ client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
  * @param option name of the option
  * @param value value of the option
  */
-static void 
+static void
 blacklist_cfg_iter (void *cls, const char *section,
                    const char *option,
                    const char *value)
@@ -224,11 +254,11 @@ blacklist_cfg_iter (void *cls, const char *section,
   char *plugs;
   char *pos;
 
-  if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string2 (option,
-                                                   strlen (option), 
-                                                   &peer.hashPubKey))
+  if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (option,
+                                                                 strlen (option),
+                                                                 &peer.public_key))
     return;
-  
+
   if ((NULL == value) || (0 == strcmp(value, "")))
   {
     /* Blacklist whole peer */
@@ -265,9 +295,9 @@ read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
   char cfg_sect[512];
   unsigned int res = 0;
 
-  GNUNET_snprintf (cfg_sect, 
+  GNUNET_snprintf (cfg_sect,
                   sizeof (cfg_sect),
-                  "transport-blacklist-%s", 
+                  "transport-blacklist-%s",
                   GNUNET_i2s_full (my_id));
   GNUNET_CONFIGURATION_iterate_section_values (cfg, cfg_sect, &blacklist_cfg_iter, &res);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -304,7 +334,9 @@ GST_blacklist_start (struct GNUNET_SERVER_Handle *server,
  * @return GNUNET_OK (continue to iterate)
  */
 static int
-free_blacklist_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
+free_blacklist_entry (void *cls,
+                     const struct GNUNET_PeerIdentity *key,
+                     void *value)
 {
   char *be = value;
 
@@ -321,9 +353,9 @@ GST_blacklist_stop ()
 {
   if (NULL != blacklist)
   {
-    GNUNET_CONTAINER_multihashmap_iterate (blacklist, &free_blacklist_entry,
+    GNUNET_CONTAINER_multipeermap_iterate (blacklist, &free_blacklist_entry,
                                            NULL);
-    GNUNET_CONTAINER_multihashmap_destroy (blacklist);
+    GNUNET_CONTAINER_multipeermap_destroy (blacklist);
     blacklist = NULL;
   }
 }
@@ -568,7 +600,6 @@ GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
     }
   }
   /* check if any other bc's are waiting for this blacklister */
-  bc = bc_head;
   for (bc = bc_head; bc != NULL; bc = bc->next)
     if ((bc->bl_pos == bl) && (GNUNET_SCHEDULER_NO_TASK == bc->task))
     {
@@ -588,24 +619,25 @@ void
 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
                         const char *transport_name)
 {
-       char * transport = NULL;
-       if (NULL != transport_name)
-       {
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Adding peer `%s' with plugin `%s' to blacklist\n",
-              GNUNET_i2s (peer), transport_name);
-                       transport = GNUNET_strdup (transport_name);
-       }
-       else
-               GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-            "Adding peer `%s' with all plugins to blacklist\n",
-            GNUNET_i2s (peer));
+  char * transport = NULL;
+
+  if (NULL != transport_name)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               "Adding peer `%s' with plugin `%s' to blacklist\n",
+               GNUNET_i2s (peer), transport_name);
+    transport = GNUNET_strdup (transport_name);
+  }
+  else
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               "Adding peer `%s' with all plugins to blacklist\n",
+               GNUNET_i2s (peer));
   if (blacklist == NULL)
     blacklist =
-      GNUNET_CONTAINER_multihashmap_create (TRANSPORT_BLACKLIST_HT_SIZE,
+      GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
                                            GNUNET_NO);
 
-  GNUNET_CONTAINER_multihashmap_put (blacklist, &peer->hashPubKey,
+  GNUNET_CONTAINER_multipeermap_put (blacklist, peer,
                                      transport,
                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
 }
@@ -618,10 +650,12 @@ GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
  * @param cls the transport name to match (const char*)
  * @param key the key (unused)
  * @param value the 'char *' (name of a blacklisted transport)
- * @return GNUNET_OK if the entry does not match, GNUNET_NO if it matches
+ * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
  */
 static int
-test_blacklisted (void *cls, const struct GNUNET_HashCode * key, void *value)
+test_blacklisted (void *cls,
+                 const struct GNUNET_PeerIdentity *key,
+                 void *value)
 {
   const char *transport_name = cls;
   char *be = value;
@@ -636,10 +670,10 @@ test_blacklisted (void *cls, const struct GNUNET_HashCode * key, void *value)
    */
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                                               "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
-               GNUNET_h2s (key),
-               (NULL == transport_name) ? "unspecified" : transport_name,
-               (NULL == be) ? "all plugins" : be);
+             "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
+             GNUNET_i2s (key),
+             (NULL == transport_name) ? "unspecified" : transport_name,
+             (NULL == be) ? "all plugins" : be);
   /* all plugins for this peer were blacklisted: disallow */
   if (NULL == value)
                return GNUNET_NO;
@@ -679,7 +713,7 @@ GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
    * If iteration is aborted, we found a matching blacklist entry */
   if ((blacklist != NULL) &&
       (GNUNET_SYSERR ==
-       GNUNET_CONTAINER_multihashmap_get_multiple (blacklist, &peer->hashPubKey,
+       GNUNET_CONTAINER_multipeermap_get_multiple (blacklist, peer,
                                                    &test_blacklisted,
                                                    (void *) transport_name)))
   {