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_BlacklistRequest
43 * Connection to transport service.
45 struct GNUNET_CLIENT_Connection * client;
48 * Function to call when done.
50 GNUNET_SCHEDULER_Task cont;
60 struct GNUNET_SCHEDULER_Handle *sched;
63 * Pending handle for the blacklisting request.
65 struct GNUNET_CLIENT_TransmitHandle *th;
68 * How long should 'peer' be blacklisted?
70 struct GNUNET_TIME_Absolute duration;
73 * Which peer is being blacklisted?
75 struct GNUNET_PeerIdentity peer;
81 * Function called to notify a client about the socket
82 * begin ready to queue more data. "buf" will be
83 * NULL and "size" zero if the socket was closed for
84 * writing in the meantime.
87 * @param size number of bytes available in buf
88 * @param buf where the callee should write the message
89 * @return number of bytes written to buf
92 transmit_blacklist_request (void *cls,
93 size_t size, void *buf)
95 struct GNUNET_TRANSPORT_BlacklistRequest *br = cls;
96 struct BlacklistMessage req;
100 GNUNET_SCHEDULER_add_continuation (br->sched,
103 GNUNET_SCHEDULER_REASON_TIMEOUT);
107 req.header.size = htons (sizeof (req));
108 req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
109 req.reserved = htonl (0);
111 req.until = GNUNET_TIME_absolute_hton (br->duration);
112 memcpy (buf, &req, sizeof (req));
113 GNUNET_SCHEDULER_add_continuation (br->sched,
116 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
123 * Blacklist a peer for a given period of time. All connections
124 * (inbound and outbound) to a peer that is blacklisted will be
125 * dropped (as soon as we learn who the connection is for). A second
126 * call to this function for the same peer overrides previous
127 * blacklisting requests.
129 * @param sched scheduler to use
130 * @param cfg configuration to use
131 * @param peer identity of peer to blacklist
132 * @param duration how long to blacklist, use GNUNET_TIME_UNIT_ZERO to
133 * re-enable connections
134 * @param timeout when should this operation (trying to establish the
135 * blacklisting time out)
136 * @param cont continuation to call once the request has been processed
137 * @param cont_cls closure for cont
138 * @return NULL on error, otherwise handle for cancellation
140 struct GNUNET_TRANSPORT_BlacklistRequest *
141 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
142 const struct GNUNET_CONFIGURATION_Handle *cfg,
143 const struct GNUNET_PeerIdentity *peer,
144 struct GNUNET_TIME_Relative duration,
145 struct GNUNET_TIME_Relative timeout,
146 GNUNET_SCHEDULER_Task cont,
149 struct GNUNET_CLIENT_Connection * client;
150 struct GNUNET_TRANSPORT_BlacklistRequest *ret;
152 client = GNUNET_CLIENT_connect (sched, "transport", cfg);
155 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_BlacklistRequest));
156 ret->client = client;
158 ret->duration = GNUNET_TIME_relative_to_absolute (duration);
161 ret->cont_cls = cont_cls;
162 ret->th = GNUNET_CLIENT_notify_transmit_ready (client,
163 sizeof (struct BlacklistMessage),
166 &transmit_blacklist_request,
168 GNUNET_assert (NULL != ret->th);
174 * Abort transmitting the blacklist request. Note that this function
175 * is NOT for removing a peer from the blacklist (for that, call
176 * GNUNET_TRANSPORT_blacklist with a duration of zero). This function
177 * is only for aborting the transmission of a blacklist request
178 * (i.e. because of shutdown).
180 * @param br handle of the request that is to be cancelled
183 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_BlacklistRequest * br)
185 GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
191 * Handle for blacklist notifications.
193 struct GNUNET_TRANSPORT_BlacklistNotification
197 * Function to call whenever there is a change.
199 GNUNET_TRANSPORT_BlacklistCallback notify;
202 * Closure for notify.
209 struct GNUNET_SCHEDULER_Handle *sched;
212 * Configuration to use.
214 const struct GNUNET_CONFIGURATION_Handle *cfg;
217 * Connection to transport service.
219 struct GNUNET_CLIENT_Connection * client;
222 * Pending handle for the notification request.
224 struct GNUNET_CLIENT_TransmitHandle *th;
229 * Send a request to receive blacklisting notifications
231 * @param bn context to initialize
234 request_notifications (struct GNUNET_TRANSPORT_BlacklistNotification *bn);
238 * Destroy the existing connection to the transport service and
239 * setup a new one (the existing one had serious problems).
241 * @param bn context to re-initialize
244 retry_get_notifications (struct GNUNET_TRANSPORT_BlacklistNotification *bn)
246 GNUNET_CLIENT_disconnect (bn->client, GNUNET_NO);
247 bn->client = GNUNET_CLIENT_connect (bn->sched, "transport", bn->cfg);
248 request_notifications (bn);
253 * Function called whenever we get a blacklisting notification.
254 * Pass it on to the callback and wait for more.
256 * @param cls our 'struct GNUNET_TRANSPORT_BlacklistNotification *'
257 * @param msg the blacklisting notification, NULL on error
260 recv_blacklist_info (void *cls,
261 const struct GNUNET_MessageHeader *msg)
263 struct GNUNET_TRANSPORT_BlacklistNotification *bn = cls;
264 const struct BlacklistMessage *req;
266 if ( (msg == NULL) ||
267 (sizeof(struct BlacklistMessage) != ntohs(msg->size)) ||
268 (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST != ntohs(msg->type)) )
270 retry_get_notifications (bn);
273 req = (const struct BlacklistMessage*) msg;
274 bn->notify (bn->notify_cls,
276 GNUNET_TIME_absolute_ntoh (req->until));
277 GNUNET_CLIENT_receive (bn->client,
278 &recv_blacklist_info,
280 GNUNET_TIME_UNIT_FOREVER_REL);
285 * Function called to notify a client about the socket
286 * begin ready to queue more data. "buf" will be
287 * NULL and "size" zero if the socket was closed for
288 * writing in the meantime.
291 * @param size number of bytes available in buf
292 * @param buf where the callee should write the message
293 * @return number of bytes written to buf
296 transmit_notify_request (void *cls,
297 size_t size, void *buf)
299 struct GNUNET_TRANSPORT_BlacklistNotification *bn = cls;
300 struct GNUNET_MessageHeader hdr;
305 retry_get_notifications (bn);
308 GNUNET_assert (size >= sizeof(hdr));
309 hdr.size = htons (sizeof (hdr));
310 hdr.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_NOTIFY);
311 memcpy (buf, &hdr, sizeof(hdr));
317 * Send a request to receive blacklisting notifications
319 * @param bn context to initialize
322 request_notifications (struct GNUNET_TRANSPORT_BlacklistNotification *bn)
324 GNUNET_assert (bn->client != NULL);
325 bn->th = GNUNET_CLIENT_notify_transmit_ready (bn->client,
326 sizeof (struct GNUNET_MessageHeader),
327 GNUNET_TIME_UNIT_FOREVER_REL,
329 &transmit_notify_request,
331 GNUNET_assert (bn->th != NULL);
332 GNUNET_CLIENT_receive (bn->client,
333 &recv_blacklist_info,
335 GNUNET_TIME_UNIT_FOREVER_REL);
340 * Call a function whenever a peer's blacklisting status changes.
342 * @param sched scheduler to use
343 * @param cfg configuration to use
344 * @param bc function to call on status changes
345 * @param bc_cls closure for bc
346 * @return NULL on error, otherwise handle for cancellation
348 struct GNUNET_TRANSPORT_BlacklistNotification *
349 GNUNET_TRANSPORT_blacklist_notify (struct GNUNET_SCHEDULER_Handle *sched,
350 const struct GNUNET_CONFIGURATION_Handle *cfg,
351 GNUNET_TRANSPORT_BlacklistCallback bc,
354 struct GNUNET_TRANSPORT_BlacklistNotification *ret;
355 struct GNUNET_CLIENT_Connection * client;
357 client = GNUNET_CLIENT_connect (sched, "transport", cfg);
360 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_BlacklistNotification));
361 ret->client = client;
365 ret->notify_cls = bc_cls;
366 request_notifications (ret);
372 * Stop calling the notification callback associated with
373 * the given blacklist notification.
375 * @param bn handle of the request that is to be cancelled
378 GNUNET_TRANSPORT_blacklist_notify_cancel (struct GNUNET_TRANSPORT_BlacklistNotification * bn)
381 GNUNET_CLIENT_notify_transmit_ready_cancel (bn->th);
382 GNUNET_CLIENT_disconnect (bn->client, GNUNET_NO);
386 /* end of transport_api_blacklist.c */