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 2, 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 br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
182 sizeof (struct GNUNET_MessageHeader),
183 GNUNET_TIME_UNIT_FOREVER_REL,
185 &transmit_blacklist_init,
191 * Transmit the blacklist response to the service.
193 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
194 * @param size number of bytes available in buf
195 * @param buf where the callee should write the message
196 * @return number of bytes written to buf
199 transmit_blacklist_reply (void *cls,
200 size_t size, void *buf)
202 struct GNUNET_TRANSPORT_Blacklist *br = cls;
203 struct BlacklistMessage req;
210 req.header.size = htons (sizeof (req));
211 req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
212 req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
214 memcpy (buf, &req, sizeof (req));
221 * Send our reply to a blacklisting request.
223 * @param br our overall context
226 reply (struct GNUNET_TRANSPORT_Blacklist *br)
228 br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
229 sizeof (struct BlacklistMessage),
230 GNUNET_TIME_UNIT_FOREVER_REL,
232 &transmit_blacklist_reply,
243 * Install a blacklist callback. The service will be queried for all
244 * existing connections as well as any fresh connections to check if
245 * they are permitted. If the blacklisting callback is unregistered,
246 * all hosts that were denied in the past will automatically be
247 * whitelisted again. Cancelling the blacklist handle is also the
248 * only way to re-enable connections from peers that were previously
251 * @param sched scheduler to use
252 * @param cfg configuration to use
253 * @param cb callback to invoke to check if connections are allowed
254 * @param cb_cls closure for cb
255 * @return NULL on error, otherwise handle for cancellation
257 struct GNUNET_TRANSPORT_Blacklist *
258 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
259 const struct GNUNET_CONFIGURATION_Handle *cfg,
260 GNUNET_TRANSPORT_BlacklistCallback cb,
263 struct GNUNET_CLIENT_Connection * client;
264 struct GNUNET_TRANSPORT_Blacklist *ret;
266 client = GNUNET_CLIENT_connect (sched, "transport", cfg);
269 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
270 ret->client = client;
273 ret->th = GNUNET_CLIENT_notify_transmit_ready (client,
274 sizeof (struct GNUNET_MessageHeader),
275 GNUNET_TIME_UNIT_FOREVER_REL,
277 &transmit_blacklist_init,
284 * Abort the blacklist. Note that this function is the only way for
285 * removing a peer from the blacklist.
287 * @param br handle of the request that is to be cancelled
290 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
293 GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
294 GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
299 /* end of transport_api_blacklist.c */