2 This file is part of GNUnet.
3 (C) 2010-2014 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/transport_api_blacklist.c
23 * @brief library to access the blacklisting functions of the transport service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
35 * Handle for blacklisting requests.
37 struct GNUNET_TRANSPORT_Blacklist
41 * Connection to transport service.
43 struct GNUNET_CLIENT_Connection *client;
46 * Configuration to use.
48 const struct GNUNET_CONFIGURATION_Handle *cfg;
51 * Pending handle for the current request.
53 struct GNUNET_CLIENT_TransmitHandle *th;
56 * Function to call for determining if a peer is allowed
57 * to communicate with us.
59 GNUNET_TRANSPORT_BlacklistCallback cb;
67 * Peer currently under consideration.
69 struct GNUNET_PeerIdentity peer;
75 * Establish blacklist connection to transport service.
77 * @param br overall handle
80 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
84 * Send our reply to a blacklisting request.
86 * @param br our overall context
89 reply (struct GNUNET_TRANSPORT_Blacklist *br);
93 * Handle blacklist queries.
95 * @param cls our overall handle
99 query_handler (void *cls,
100 const struct GNUNET_MessageHeader *msg)
102 struct GNUNET_TRANSPORT_Blacklist *br = cls;
103 const struct BlacklistMessage *bm;
105 GNUNET_assert (NULL != br);
107 (ntohs (msg->size) != sizeof (struct BlacklistMessage)) ||
108 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY))
113 bm = (const struct BlacklistMessage *) msg;
114 GNUNET_break (0 == ntohl (bm->is_allowed));
121 * Receive blacklist queries from transport service.
123 * @param br overall handle
126 receive (struct GNUNET_TRANSPORT_Blacklist *br)
128 GNUNET_CLIENT_receive (br->client, &query_handler, br,
129 GNUNET_TIME_UNIT_FOREVER_REL);
134 * Transmit the blacklist initialization request to the service.
136 * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *`
137 * @param size number of bytes available in @a buf
138 * @param buf where the callee should write the message
139 * @return number of bytes written to @a buf
142 transmit_blacklist_init (void *cls,
146 struct GNUNET_TRANSPORT_Blacklist *br = cls;
147 struct GNUNET_MessageHeader req;
155 req.size = htons (sizeof (struct GNUNET_MessageHeader));
156 req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
157 memcpy (buf, &req, sizeof (req));
164 * Establish blacklist connection to transport service.
166 * @param br overall handle
169 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
171 if (NULL != br->client)
172 GNUNET_CLIENT_disconnect (br->client);
173 br->client = GNUNET_CLIENT_connect ("transport", br->cfg);
174 GNUNET_assert (NULL != br->client);
176 GNUNET_CLIENT_notify_transmit_ready (br->client,
177 sizeof (struct GNUNET_MessageHeader),
178 GNUNET_TIME_UNIT_FOREVER_REL,
179 GNUNET_YES, &transmit_blacklist_init,
185 * Transmit the blacklist response to the service.
187 * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *`
188 * @param size number of bytes available in @a buf
189 * @param buf where the callee should write the message
190 * @return number of bytes written to @a buf
193 transmit_blacklist_reply (void *cls,
197 struct GNUNET_TRANSPORT_Blacklist *br = cls;
198 struct BlacklistMessage req;
206 req.header.size = htons (sizeof (req));
207 req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
208 req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
210 memcpy (buf, &req, sizeof (req));
218 * Send our reply to a blacklisting request.
220 * @param br our overall context
223 reply (struct GNUNET_TRANSPORT_Blacklist *br)
225 GNUNET_assert (NULL == br->th);
227 GNUNET_CLIENT_notify_transmit_ready (br->client,
228 sizeof (struct BlacklistMessage),
229 GNUNET_TIME_UNIT_FOREVER_REL,
230 GNUNET_NO, &transmit_blacklist_reply,
241 * Install a blacklist callback. The service will be queried for all
242 * existing connections as well as any fresh connections to check if
243 * they are permitted. If the blacklisting callback is unregistered,
244 * all hosts that were denied in the past will automatically be
245 * whitelisted again. Cancelling the blacklist handle is also the
246 * only way to re-enable connections from peers that were previously
249 * @param cfg configuration to use
250 * @param cb callback to invoke to check if connections are allowed
251 * @param cb_cls closure for @a cb
252 * @return NULL on error, otherwise handle for cancellation
254 struct GNUNET_TRANSPORT_Blacklist *
255 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
256 GNUNET_TRANSPORT_BlacklistCallback cb,
259 struct GNUNET_CLIENT_Connection *client;
260 struct GNUNET_TRANSPORT_Blacklist *ret;
262 client = GNUNET_CLIENT_connect ("transport", cfg);
265 ret = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
266 ret->client = client;
269 ret->cb_cls = cb_cls;
271 GNUNET_CLIENT_notify_transmit_ready (client,
272 sizeof (struct GNUNET_MessageHeader),
273 GNUNET_TIME_UNIT_FOREVER_REL,
274 GNUNET_YES, &transmit_blacklist_init,
281 * Abort the blacklist. Note that this function is the only way for
282 * removing a peer from the blacklist.
284 * @param br handle of the request that is to be cancelled
287 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
291 GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
294 GNUNET_CLIENT_disconnect (br->client);
299 /* end of transport_api_blacklist.c */