2 This file is part of GNUnet.
3 Copyright (C) 2010-2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file transport/transport_api_blacklist.c
21 * @brief library to access the blacklisting functions of the transport service
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_transport_service.h"
30 #include "transport.h"
33 * Handle for blacklisting requests.
35 struct GNUNET_TRANSPORT_Blacklist
39 * Connection to transport service.
41 struct GNUNET_MQ_Handle *mq;
44 * Configuration to use.
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Function to call for determining if a peer is allowed
50 * to communicate with us.
52 GNUNET_TRANSPORT_BlacklistCallback cb;
63 * Establish blacklist connection to transport service.
65 * @param br overall handle
68 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
72 * Handle blacklist queries.
74 * @param cls our overall handle
78 handle_query (void *cls,
79 const struct BlacklistMessage *bm)
81 struct GNUNET_TRANSPORT_Blacklist *br = cls;
82 struct GNUNET_MQ_Envelope *env;
83 struct BlacklistMessage *res;
85 GNUNET_break (0 == ntohl (bm->is_allowed));
86 env = GNUNET_MQ_msg (res,
87 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
88 res->is_allowed = htonl (br->cb (br->cb_cls,
91 GNUNET_MQ_send (br->mq,
96 * Generic error handler, called with the appropriate error code and
97 * the same closure specified at the creation of the message queue.
98 * Not every message queue implementation supports an error handler.
100 * @param cls closure with the `struct GNUNET_TRANSPORT_Blacklist *`
101 * @param error error code
104 mq_error_handler (void *cls,
105 enum GNUNET_MQ_Error error)
107 struct GNUNET_TRANSPORT_Blacklist *br = cls;
114 * Establish blacklist connection to transport service.
116 * @param br overall handle
119 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
121 struct GNUNET_MQ_MessageHandler handlers[] = {
122 GNUNET_MQ_hd_fixed_size (query,
123 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY,
124 struct BlacklistMessage,
126 GNUNET_MQ_handler_end ()
128 struct GNUNET_MQ_Envelope *env;
129 struct GNUNET_MessageHeader *req;
132 GNUNET_MQ_destroy (br->mq);
133 br->mq = GNUNET_CLIENT_connect (br->cfg,
140 env = GNUNET_MQ_msg (req,
141 GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
142 GNUNET_MQ_send (br->mq,
148 * Install a blacklist callback. The service will be queried for all
149 * existing connections as well as any fresh connections to check if
150 * they are permitted. If the blacklisting callback is unregistered,
151 * all hosts that were denied in the past will automatically be
152 * whitelisted again. Cancelling the blacklist handle is also the
153 * only way to re-enable connections from peers that were previously
156 * @param cfg configuration to use
157 * @param cb callback to invoke to check if connections are allowed
158 * @param cb_cls closure for @a cb
159 * @return NULL on error, otherwise handle for cancellation
161 struct GNUNET_TRANSPORT_Blacklist *
162 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
163 GNUNET_TRANSPORT_BlacklistCallback cb,
166 struct GNUNET_TRANSPORT_Blacklist *br;
168 br = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
183 * Abort the blacklist. Note that this function is the only way for
184 * removing a peer from the blacklist.
186 * @param br handle of the request that is to be cancelled
189 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
191 GNUNET_MQ_destroy (br->mq);
196 /* end of transport_api_blacklist.c */