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;
50 struct GNUNET_SCHEDULER_Handle *sched;
53 * Configuration to use.
55 const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * Pending handle for the current request.
60 struct GNUNET_CLIENT_TransmitHandle *th;
63 * Function to call for determining if a peer is allowed
64 * to communicate with us.
66 GNUNET_TRANSPORT_BlacklistCallback cb;
74 * Peer currently under consideration.
76 struct GNUNET_PeerIdentity peer;
82 * Establish blacklist connection to transport service.
84 * @param br overall handle
87 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
91 * Send our reply to a blacklisting request.
93 * @param br our overall context
96 reply (struct GNUNET_TRANSPORT_Blacklist *br);
100 * Handle blacklist queries.
102 * @param cls our overall handle
106 query_handler (void *cls,
107 const struct GNUNET_MessageHeader *msg)
109 struct GNUNET_TRANSPORT_Blacklist *br = cls;
110 const struct BlacklistMessage *bm;
112 if ( (ntohs(msg->size) != sizeof (struct BlacklistMessage)) ||
113 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY) )
118 bm = (const struct BlacklistMessage *)msg;
119 GNUNET_break (0 == ntohl (bm->is_allowed));
126 * Receive blacklist queries from transport service.
128 * @param br overall handle
131 receive (struct GNUNET_TRANSPORT_Blacklist *br)
133 GNUNET_CLIENT_receive (br->client,
136 GNUNET_TIME_UNIT_FOREVER_REL);
141 * Transmit the blacklist initialization request to the service.
143 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
144 * @param size number of bytes available in buf
145 * @param buf where the callee should write the message
146 * @return number of bytes written to buf
149 transmit_blacklist_init (void *cls,
150 size_t size, void *buf)
152 struct GNUNET_TRANSPORT_Blacklist *br = cls;
153 struct GNUNET_MessageHeader req;
160 req.size = htons (sizeof (struct GNUNET_MessageHeader));
161 req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
162 memcpy (buf, &req, sizeof (req));
169 * Establish blacklist connection to transport service.
171 * @param br overall handle
174 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
176 if (br->client != NULL)
177 GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
178 br->client = GNUNET_CLIENT_connect (br->sched,
181 GNUNET_assert (br->client != NULL);
182 br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
183 sizeof (struct GNUNET_MessageHeader),
184 GNUNET_TIME_UNIT_FOREVER_REL,
186 &transmit_blacklist_init,
192 * Transmit the blacklist response to the service.
194 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
195 * @param size number of bytes available in buf
196 * @param buf where the callee should write the message
197 * @return number of bytes written to buf
200 transmit_blacklist_reply (void *cls,
201 size_t size, void *buf)
203 struct GNUNET_TRANSPORT_Blacklist *br = cls;
204 struct BlacklistMessage req;
211 req.header.size = htons (sizeof (req));
212 req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
213 req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
215 memcpy (buf, &req, sizeof (req));
222 * Send our reply to a blacklisting request.
224 * @param br our overall context
227 reply (struct GNUNET_TRANSPORT_Blacklist *br)
229 br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
230 sizeof (struct BlacklistMessage),
231 GNUNET_TIME_UNIT_FOREVER_REL,
233 &transmit_blacklist_reply,
244 * Install a blacklist callback. The service will be queried for all
245 * existing connections as well as any fresh connections to check if
246 * they are permitted. If the blacklisting callback is unregistered,
247 * all hosts that were denied in the past will automatically be
248 * whitelisted again. Cancelling the blacklist handle is also the
249 * only way to re-enable connections from peers that were previously
252 * @param sched scheduler to use
253 * @param cfg configuration to use
254 * @param cb callback to invoke to check if connections are allowed
255 * @param cb_cls closure for cb
256 * @return NULL on error, otherwise handle for cancellation
258 struct GNUNET_TRANSPORT_Blacklist *
259 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
260 const struct GNUNET_CONFIGURATION_Handle *cfg,
261 GNUNET_TRANSPORT_BlacklistCallback cb,
264 struct GNUNET_CLIENT_Connection * client;
265 struct GNUNET_TRANSPORT_Blacklist *ret;
267 client = GNUNET_CLIENT_connect (sched, "transport", cfg);
270 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
271 ret->client = client;
274 ret->th = GNUNET_CLIENT_notify_transmit_ready (client,
275 sizeof (struct GNUNET_MessageHeader),
276 GNUNET_TIME_UNIT_FOREVER_REL,
278 &transmit_blacklist_init,
285 * Abort the blacklist. Note that this function is the only way for
286 * removing a peer from the blacklist.
288 * @param br handle of the request that is to be cancelled
291 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
294 GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
295 GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
300 /* end of transport_api_blacklist.c */