Check that you are not present in trail twice
[oweals/gnunet.git] / src / transport / transport_api_blacklist.c
index 82deadb92db0eeb74d57a3624fc84c8e6c2a3440..2d6dc5f065b5169ca34ce1277a407b5015e27ea6 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2010 Christian Grothoff (and other contributing authors)
+     (C) 2010-2014 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  * @author Christian Grothoff
  */
 #include "platform.h"
-#include "gnunet_client_lib.h"
+#include "gnunet_util_lib.h"
 #include "gnunet_arm_service.h"
 #include "gnunet_hello_lib.h"
 #include "gnunet_protocols.h"
-#include "gnunet_server_lib.h"
-#include "gnunet_time_lib.h"
 #include "gnunet_transport_service.h"
 #include "transport.h"
 
@@ -42,12 +40,7 @@ struct GNUNET_TRANSPORT_Blacklist
   /**
    * Connection to transport service.
    */
-  struct GNUNET_CLIENT_Connection * client;
-
-  /**
-   * Scheduler to use.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
+  struct GNUNET_CLIENT_Connection *client;
 
   /**
    * Configuration to use.
@@ -56,7 +49,7 @@ struct GNUNET_TRANSPORT_Blacklist
 
   /**
    * Pending handle for the current request.
-   */ 
+   */
   struct GNUNET_CLIENT_TransmitHandle *th;
 
   /**
@@ -66,9 +59,9 @@ struct GNUNET_TRANSPORT_Blacklist
   GNUNET_TRANSPORT_BlacklistCallback cb;
 
   /**
-   * Closure for 'cb'.
+   * Closure for @e cb.
    */
-  void *cb_cls;  
+  void *cb_cls;
 
   /**
    * Peer currently under consideration.
@@ -104,21 +97,23 @@ reply (struct GNUNET_TRANSPORT_Blacklist *br);
  */
 static void
 query_handler (void *cls,
-              const struct GNUNET_MessageHeader *msg)
+               const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_TRANSPORT_Blacklist *br = cls;
   const struct BlacklistMessage *bm;
-  
-  if ( (ntohs(msg->size) != sizeof (struct BlacklistMessage)) ||
-       (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY) )
-    {
-      reconnect (br);
-      return;
-    }
-  bm = (const struct BlacklistMessage *)msg;
+
+  GNUNET_assert (NULL != br);
+  if ((NULL == msg) ||
+      (ntohs (msg->size) != sizeof (struct BlacklistMessage)) ||
+      (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY))
+  {
+    reconnect (br);
+    return;
+  }
+  bm = (const struct BlacklistMessage *) msg;
   GNUNET_break (0 == ntohl (bm->is_allowed));
   br->peer = bm->peer;
-  reply (br);  
+  reply (br);
 }
 
 
@@ -130,33 +125,33 @@ query_handler (void *cls,
 static void
 receive (struct GNUNET_TRANSPORT_Blacklist *br)
 {
-  GNUNET_CLIENT_receive (br->client,
-                        &query_handler,
-                        br,
-                        GNUNET_TIME_UNIT_FOREVER_REL);
+  GNUNET_CLIENT_receive (br->client, &query_handler, br,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 
 /**
  * Transmit the blacklist initialization request to the service.
  *
- * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
- * @param size number of bytes available in buf
+ * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *`
+ * @param size number of bytes available in @a buf
  * @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
  */
 static size_t
 transmit_blacklist_init (void *cls,
-                        size_t size, void *buf)
+                         size_t size,
+                         void *buf)
 {
   struct GNUNET_TRANSPORT_Blacklist *br = cls;
   struct GNUNET_MessageHeader req;
 
-  if (buf == NULL)
-    {
-      reconnect (br);
-      return 0;
-    }
+  br->th = NULL;
+  if (NULL == buf)
+  {
+    reconnect (br);
+    return 0;
+  }
   req.size = htons (sizeof (struct GNUNET_MessageHeader));
   req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
   memcpy (buf, &req, sizeof (req));
@@ -173,46 +168,47 @@ transmit_blacklist_init (void *cls,
 static void
 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
 {
-  if (br->client != NULL)
-    GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
-  br->client = GNUNET_CLIENT_connect (br->sched,
-                                     "transport",
-                                     br->cfg);
-  GNUNET_assert (br->client != NULL);
-  br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
-                                               sizeof (struct GNUNET_MessageHeader),
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               GNUNET_YES,
-                                               &transmit_blacklist_init,
-                                               br);
+  if (NULL != br->client)
+    GNUNET_CLIENT_disconnect (br->client);
+  br->client = GNUNET_CLIENT_connect ("transport", br->cfg);
+  GNUNET_assert (NULL != br->client);
+  br->th =
+      GNUNET_CLIENT_notify_transmit_ready (br->client,
+                                           sizeof (struct GNUNET_MessageHeader),
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_YES, &transmit_blacklist_init,
+                                           br);
 }
 
 
 /**
  * Transmit the blacklist response to the service.
  *
- * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
- * @param size number of bytes available in buf
+ * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *`
+ * @param size number of bytes available in @a buf
  * @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
  */
 static size_t
 transmit_blacklist_reply (void *cls,
-                         size_t size, void *buf)
+                          size_t size,
+                          void *buf)
 {
   struct GNUNET_TRANSPORT_Blacklist *br = cls;
   struct BlacklistMessage req;
 
-  if (buf == NULL)
-    {
-      reconnect (br);
-      return 0;
-    }
+  br->th = NULL;
+  if (NULL == buf)
+  {
+    reconnect (br);
+    return 0;
+  }
   req.header.size = htons (sizeof (req));
   req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
   req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
   req.peer = br->peer;
   memcpy (buf, &req, sizeof (req));
+  br->th = NULL;
   receive (br);
   return sizeof (req);
 }
@@ -226,17 +222,18 @@ transmit_blacklist_reply (void *cls,
 static void
 reply (struct GNUNET_TRANSPORT_Blacklist *br)
 {
-  br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
-                                               sizeof (struct BlacklistMessage),
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               GNUNET_NO,
-                                               &transmit_blacklist_reply,
-                                               br);
-  if (br->th == NULL)
-    {
-      reconnect (br);
-      return;
-    }
+  GNUNET_assert (NULL == br->th);
+  br->th =
+      GNUNET_CLIENT_notify_transmit_ready (br->client,
+                                           sizeof (struct BlacklistMessage),
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_NO, &transmit_blacklist_reply,
+                                           br);
+  if (NULL == br->th)
+  {
+    reconnect (br);
+    return;
+  }
 }
 
 
@@ -249,34 +246,33 @@ reply (struct GNUNET_TRANSPORT_Blacklist *br)
  * only way to re-enable connections from peers that were previously
  * blacklisted.
  *
- * @param sched scheduler to use
  * @param cfg configuration to use
  * @param cb callback to invoke to check if connections are allowed
- * @param cb_cls closure for cb
+ * @param cb_cls closure for @a cb
  * @return NULL on error, otherwise handle for cancellation
  */
 struct GNUNET_TRANSPORT_Blacklist *
-GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
-                           const struct GNUNET_CONFIGURATION_Handle *cfg,
-                           GNUNET_TRANSPORT_BlacklistCallback cb,
-                           void *cb_cls)
+GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                            GNUNET_TRANSPORT_BlacklistCallback cb,
+                            void *cb_cls)
 {
-  struct GNUNET_CLIENT_Connection * client;
+  struct GNUNET_CLIENT_Connection *client;
   struct GNUNET_TRANSPORT_Blacklist *ret;
 
-  client = GNUNET_CLIENT_connect (sched, "transport", cfg);
+  client = GNUNET_CLIENT_connect ("transport", cfg);
   if (NULL == client)
     return NULL;
-  ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
+  ret = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
   ret->client = client;
-  ret->sched = sched;
   ret->cfg = cfg;
-  ret->th = GNUNET_CLIENT_notify_transmit_ready (client,
-                                                sizeof (struct GNUNET_MessageHeader),
-                                                GNUNET_TIME_UNIT_FOREVER_REL,
-                                                GNUNET_YES,
-                                                &transmit_blacklist_init,
-                                                ret);  
+  ret->cb = cb;
+  ret->cb_cls = cb_cls;
+  ret->th =
+      GNUNET_CLIENT_notify_transmit_ready (client,
+                                           sizeof (struct GNUNET_MessageHeader),
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_YES, &transmit_blacklist_init,
+                                           ret);
   return ret;
 }
 
@@ -290,9 +286,12 @@ GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
 void
 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
 {
-  if (br->th != NULL)
+  if (NULL != br->th)
+  {
     GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
-  GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
+    br->th = NULL;
+  }
+  GNUNET_CLIENT_disconnect (br->client);
   GNUNET_free (br);
 }