diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-01-21 12:27:29 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-01-21 12:27:29 +0000 |
commit | 3926d80e05c1145d84f807224e6cb30eee4c1b40 (patch) | |
tree | 9fc6544cfef2f157d2b7badaa9ebb105037e2bbb /src/transport/transport_api_blacklist.c | |
parent | bfd71264204549e472ea6a3bebd710a8a21a9f1c (diff) | |
download | gnunet-3926d80e05c1145d84f807224e6cb30eee4c1b40.tar.gz gnunet-3926d80e05c1145d84f807224e6cb30eee4c1b40.zip |
blacklisting API implementation
Diffstat (limited to 'src/transport/transport_api_blacklist.c')
-rw-r--r-- | src/transport/transport_api_blacklist.c | 386 |
1 files changed, 386 insertions, 0 deletions
diff --git a/src/transport/transport_api_blacklist.c b/src/transport/transport_api_blacklist.c new file mode 100644 index 000000000..d33c4123c --- /dev/null +++ b/src/transport/transport_api_blacklist.c | |||
@@ -0,0 +1,386 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/transport_api_blacklist.c | ||
23 | * @brief library to access the blacklisting functions of the transport service | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
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" | ||
35 | |||
36 | /** | ||
37 | * Handle for blacklisting requests. | ||
38 | */ | ||
39 | struct GNUNET_TRANSPORT_BlacklistRequest | ||
40 | { | ||
41 | |||
42 | /** | ||
43 | * Connection to transport service. | ||
44 | */ | ||
45 | struct GNUNET_CLIENT_Connection * client; | ||
46 | |||
47 | /** | ||
48 | * Function to call when done. | ||
49 | */ | ||
50 | GNUNET_SCHEDULER_Task cont; | ||
51 | |||
52 | /** | ||
53 | * Clsoure for 'cont'. | ||
54 | */ | ||
55 | void *cont_cls; | ||
56 | |||
57 | /** | ||
58 | * Scheduler to use. | ||
59 | */ | ||
60 | struct GNUNET_SCHEDULER_Handle *sched; | ||
61 | |||
62 | /** | ||
63 | * Pending handle for the blacklisting request. | ||
64 | */ | ||
65 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
66 | |||
67 | /** | ||
68 | * How long should 'peer' be blacklisted? | ||
69 | */ | ||
70 | struct GNUNET_TIME_Absolute duration; | ||
71 | |||
72 | /** | ||
73 | * Which peer is being blacklisted? | ||
74 | */ | ||
75 | struct GNUNET_PeerIdentity peer; | ||
76 | |||
77 | }; | ||
78 | |||
79 | |||
80 | /** | ||
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. | ||
85 | * | ||
86 | * @param cls closure | ||
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 | ||
90 | */ | ||
91 | static size_t | ||
92 | transmit_blacklist_request (void *cls, | ||
93 | size_t size, void *buf) | ||
94 | { | ||
95 | struct GNUNET_TRANSPORT_BlacklistRequest *br = cls; | ||
96 | struct BlacklistMessage req; | ||
97 | |||
98 | if (buf == NULL) | ||
99 | { | ||
100 | GNUNET_SCHEDULER_add_continuation (br->sched, | ||
101 | br->cont, | ||
102 | br->cont_cls, | ||
103 | GNUNET_SCHEDULER_REASON_TIMEOUT); | ||
104 | GNUNET_free (br); | ||
105 | return 0; | ||
106 | } | ||
107 | req.header.size = htons (sizeof (req)); | ||
108 | req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST); | ||
109 | req.reserved = htonl (0); | ||
110 | req.peer = br->peer; | ||
111 | req.until = GNUNET_TIME_absolute_hton (br->duration); | ||
112 | memcpy (buf, &req, sizeof (req)); | ||
113 | GNUNET_SCHEDULER_add_continuation (br->sched, | ||
114 | br->cont, | ||
115 | br->cont_cls, | ||
116 | GNUNET_SCHEDULER_REASON_PREREQ_DONE); | ||
117 | GNUNET_free (br); | ||
118 | return sizeof (req); | ||
119 | } | ||
120 | |||
121 | |||
122 | /** | ||
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. | ||
128 | * | ||
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 | ||
139 | */ | ||
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, | ||
147 | void *cont_cls) | ||
148 | { | ||
149 | struct GNUNET_CLIENT_Connection * client; | ||
150 | struct GNUNET_TRANSPORT_BlacklistRequest *ret; | ||
151 | |||
152 | client = GNUNET_CLIENT_connect (sched, "transport", cfg); | ||
153 | if (NULL == client) | ||
154 | return NULL; | ||
155 | ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_BlacklistRequest)); | ||
156 | ret->client = client; | ||
157 | ret->peer = *peer; | ||
158 | ret->duration = GNUNET_TIME_relative_to_absolute (duration); | ||
159 | ret->sched = sched; | ||
160 | ret->cont = cont; | ||
161 | ret->cont_cls = cont_cls; | ||
162 | ret->th = GNUNET_CLIENT_notify_transmit_ready (client, | ||
163 | sizeof (struct BlacklistMessage), | ||
164 | timeout, | ||
165 | GNUNET_YES, | ||
166 | &transmit_blacklist_request, | ||
167 | ret); | ||
168 | GNUNET_assert (NULL != ret->th); | ||
169 | return ret; | ||
170 | } | ||
171 | |||
172 | |||
173 | /** | ||
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). | ||
179 | * | ||
180 | * @param br handle of the request that is to be cancelled | ||
181 | */ | ||
182 | void | ||
183 | GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_BlacklistRequest * br) | ||
184 | { | ||
185 | GNUNET_CLIENT_notify_transmit_ready_cancel (br->th); | ||
186 | GNUNET_free (br); | ||
187 | } | ||
188 | |||
189 | |||
190 | /** | ||
191 | * Handle for blacklist notifications. | ||
192 | */ | ||
193 | struct GNUNET_TRANSPORT_BlacklistNotification | ||
194 | { | ||
195 | |||
196 | /** | ||
197 | * Function to call whenever there is a change. | ||
198 | */ | ||
199 | GNUNET_TRANSPORT_BlacklistCallback notify; | ||
200 | |||
201 | /** | ||
202 | * Closure for notify. | ||
203 | */ | ||
204 | void *notify_cls; | ||
205 | |||
206 | /** | ||
207 | * Scheduler to use. | ||
208 | */ | ||
209 | struct GNUNET_SCHEDULER_Handle *sched; | ||
210 | |||
211 | /** | ||
212 | * Configuration to use. | ||
213 | */ | ||
214 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
215 | |||
216 | /** | ||
217 | * Connection to transport service. | ||
218 | */ | ||
219 | struct GNUNET_CLIENT_Connection * client; | ||
220 | |||
221 | /** | ||
222 | * Pending handle for the notification request. | ||
223 | */ | ||
224 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
225 | }; | ||
226 | |||
227 | |||
228 | /** | ||
229 | * Send a request to receive blacklisting notifications | ||
230 | * | ||
231 | * @param bn context to initialize | ||
232 | */ | ||
233 | static void | ||
234 | request_notifications (struct GNUNET_TRANSPORT_BlacklistNotification *bn); | ||
235 | |||
236 | |||
237 | /** | ||
238 | * Destroy the existing connection to the transport service and | ||
239 | * setup a new one (the existing one had serious problems). | ||
240 | * | ||
241 | * @param bn context to re-initialize | ||
242 | */ | ||
243 | static void | ||
244 | retry_get_notifications (struct GNUNET_TRANSPORT_BlacklistNotification *bn) | ||
245 | { | ||
246 | GNUNET_CLIENT_disconnect (bn->client); | ||
247 | bn->client = GNUNET_CLIENT_connect (bn->sched, "transport", bn->cfg); | ||
248 | request_notifications (bn); | ||
249 | } | ||
250 | |||
251 | |||
252 | /** | ||
253 | * Function called whenever we get a blacklisting notification. | ||
254 | * Pass it on to the callback and wait for more. | ||
255 | * | ||
256 | * @param cls our 'struct GNUNET_TRANSPORT_BlacklistNotification *' | ||
257 | * @param msg the blacklisting notification, NULL on error | ||
258 | */ | ||
259 | static void | ||
260 | recv_blacklist_info (void *cls, | ||
261 | const struct GNUNET_MessageHeader *msg) | ||
262 | { | ||
263 | struct GNUNET_TRANSPORT_BlacklistNotification *bn = cls; | ||
264 | const struct BlacklistMessage *req; | ||
265 | |||
266 | if ( (msg == NULL) || | ||
267 | (sizeof(struct BlacklistMessage) != ntohs(msg->size)) || | ||
268 | (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST != ntohs(msg->type)) ) | ||
269 | { | ||
270 | retry_get_notifications (bn); | ||
271 | return; | ||
272 | } | ||
273 | req = (const struct BlacklistMessage*) msg; | ||
274 | bn->notify (bn->notify_cls, | ||
275 | &req->peer, | ||
276 | GNUNET_TIME_absolute_ntoh (req->until)); | ||
277 | GNUNET_CLIENT_receive (bn->client, | ||
278 | &recv_blacklist_info, | ||
279 | bn, | ||
280 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
281 | } | ||
282 | |||
283 | |||
284 | /** | ||
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. | ||
289 | * | ||
290 | * @param cls closure | ||
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 | ||
294 | */ | ||
295 | static size_t | ||
296 | transmit_notify_request (void *cls, | ||
297 | size_t size, void *buf) | ||
298 | { | ||
299 | struct GNUNET_TRANSPORT_BlacklistNotification *bn = cls; | ||
300 | struct GNUNET_MessageHeader hdr; | ||
301 | |||
302 | bn->th = NULL; | ||
303 | if (buf == NULL) | ||
304 | { | ||
305 | retry_get_notifications (bn); | ||
306 | return 0; | ||
307 | } | ||
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)); | ||
312 | return sizeof(hdr); | ||
313 | } | ||
314 | |||
315 | |||
316 | /** | ||
317 | * Send a request to receive blacklisting notifications | ||
318 | * | ||
319 | * @param bn context to initialize | ||
320 | */ | ||
321 | static void | ||
322 | request_notifications (struct GNUNET_TRANSPORT_BlacklistNotification *bn) | ||
323 | { | ||
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, | ||
328 | GNUNET_YES, | ||
329 | &transmit_notify_request, | ||
330 | bn); | ||
331 | GNUNET_assert (bn->th != NULL); | ||
332 | GNUNET_CLIENT_receive (bn->client, | ||
333 | &recv_blacklist_info, | ||
334 | bn, | ||
335 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
336 | } | ||
337 | |||
338 | |||
339 | /** | ||
340 | * Call a function whenever a peer's blacklisting status changes. | ||
341 | * | ||
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 | ||
347 | */ | ||
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, | ||
352 | void *bc_cls) | ||
353 | { | ||
354 | struct GNUNET_TRANSPORT_BlacklistNotification *ret; | ||
355 | struct GNUNET_CLIENT_Connection * client; | ||
356 | |||
357 | client = GNUNET_CLIENT_connect (sched, "transport", cfg); | ||
358 | if (NULL == client) | ||
359 | return NULL; | ||
360 | ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_BlacklistNotification)); | ||
361 | ret->client = client; | ||
362 | ret->sched = sched; | ||
363 | ret->cfg = cfg; | ||
364 | ret->notify = bc; | ||
365 | ret->notify_cls = bc_cls; | ||
366 | request_notifications (ret); | ||
367 | return ret; | ||
368 | } | ||
369 | |||
370 | |||
371 | /** | ||
372 | * Stop calling the notification callback associated with | ||
373 | * the given blacklist notification. | ||
374 | * | ||
375 | * @param bn handle of the request that is to be cancelled | ||
376 | */ | ||
377 | void | ||
378 | GNUNET_TRANSPORT_blacklist_notify_cancel (struct GNUNET_TRANSPORT_BlacklistNotification * bn) | ||
379 | { | ||
380 | if (bn->th != NULL) | ||
381 | GNUNET_CLIENT_notify_transmit_ready_cancel (bn->th); | ||
382 | GNUNET_CLIENT_disconnect (bn->client); | ||
383 | GNUNET_free (bn); | ||
384 | } | ||
385 | |||
386 | /* end of transport_api_blacklist.c */ | ||