2 This file is part of GNUnet.
3 (C) 2010 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_client_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_time_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "transport.h"
37 * Handle for blacklisting requests.
39 struct GNUNET_TRANSPORT_Blacklist
43 * Connection to transport service.
45 struct GNUNET_CLIENT_Connection * client;
48 * Configuration to use.
50 const struct GNUNET_CONFIGURATION_Handle *cfg;
53 * Pending handle for the current request.
55 struct GNUNET_CLIENT_TransmitHandle *th;
58 * Function to call for determining if a peer is allowed
59 * to communicate with us.
61 GNUNET_TRANSPORT_BlacklistCallback cb;
69 * Peer currently under consideration.
71 struct GNUNET_PeerIdentity peer;
77 * Establish blacklist connection to transport service.
79 * @param br overall handle
82 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
86 * Send our reply to a blacklisting request.
88 * @param br our overall context
91 reply (struct GNUNET_TRANSPORT_Blacklist *br);
95 * Handle blacklist queries.
97 * @param cls our overall handle
101 query_handler (void *cls,
102 const struct GNUNET_MessageHeader *msg)
104 struct GNUNET_TRANSPORT_Blacklist *br = cls;
105 const struct BlacklistMessage *bm;
107 if ( (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,
131 GNUNET_TIME_UNIT_FOREVER_REL);
136 * Transmit the blacklist initialization request to the service.
138 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
139 * @param size number of bytes available in buf
140 * @param buf where the callee should write the message
141 * @return number of bytes written to buf
144 transmit_blacklist_init (void *cls,
145 size_t size, void *buf)
147 struct GNUNET_TRANSPORT_Blacklist *br = cls;
148 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 (br->client != NULL)
172 GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
173 br->client = GNUNET_CLIENT_connect ("transport",
175 GNUNET_assert (br->client != NULL);
176 br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
177 sizeof (struct GNUNET_MessageHeader),
178 GNUNET_TIME_UNIT_FOREVER_REL,
180 &transmit_blacklist_init,
186 * Transmit the blacklist response to the service.
188 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
189 * @param size number of bytes available in buf
190 * @param buf where the callee should write the message
191 * @return number of bytes written to buf
194 transmit_blacklist_reply (void *cls,
195 size_t size, void *buf)
197 struct GNUNET_TRANSPORT_Blacklist *br = cls;
198 struct BlacklistMessage req;
205 req.header.size = htons (sizeof (req));
206 req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
207 req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
209 memcpy (buf, &req, sizeof (req));
216 * Send our reply to a blacklisting request.
218 * @param br our overall context
221 reply (struct GNUNET_TRANSPORT_Blacklist *br)
223 br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
224 sizeof (struct BlacklistMessage),
225 GNUNET_TIME_UNIT_FOREVER_REL,
227 &transmit_blacklist_reply,
238 * Install a blacklist callback. The service will be queried for all
239 * existing connections as well as any fresh connections to check if
240 * they are permitted. If the blacklisting callback is unregistered,
241 * all hosts that were denied in the past will automatically be
242 * whitelisted again. Cancelling the blacklist handle is also the
243 * only way to re-enable connections from peers that were previously
246 * @param cfg configuration to use
247 * @param cb callback to invoke to check if connections are allowed
248 * @param cb_cls closure for cb
249 * @return NULL on error, otherwise handle for cancellation
251 struct GNUNET_TRANSPORT_Blacklist *
252 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
253 GNUNET_TRANSPORT_BlacklistCallback cb,
256 struct GNUNET_CLIENT_Connection * client;
257 struct GNUNET_TRANSPORT_Blacklist *ret;
259 client = GNUNET_CLIENT_connect ("transport", cfg);
262 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
263 ret->client = client;
265 ret->th = GNUNET_CLIENT_notify_transmit_ready (client,
266 sizeof (struct GNUNET_MessageHeader),
267 GNUNET_TIME_UNIT_FOREVER_REL,
269 &transmit_blacklist_init,
276 * Abort the blacklist. Note that this function is the only way for
277 * removing a peer from the blacklist.
279 * @param br handle of the request that is to be cancelled
282 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
285 GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
286 GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
291 /* end of transport_api_blacklist.c */