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 | |
parent | bfd71264204549e472ea6a3bebd710a8a21a9f1c (diff) | |
download | gnunet-3926d80e05c1145d84f807224e6cb30eee4c1b40.tar.gz gnunet-3926d80e05c1145d84f807224e6cb30eee4c1b40.zip |
blacklisting API implementation
-rw-r--r-- | src/include/gnunet_protocols.h | 28 | ||||
-rw-r--r-- | src/include/gnunet_transport_service.h | 30 | ||||
-rw-r--r-- | src/transport/Makefile.am | 3 | ||||
-rw-r--r-- | src/transport/transport.h | 41 | ||||
-rw-r--r-- | src/transport/transport_api_blacklist.c | 386 |
5 files changed, 475 insertions, 13 deletions
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index 0925a1909..5451f53d9 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors) | 3 | (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -185,43 +185,55 @@ extern "C" | |||
185 | */ | 185 | */ |
186 | #define GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY 30 | 186 | #define GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY 30 |
187 | 187 | ||
188 | /** | ||
189 | * Change in blacklisting status of a peer. | ||
190 | */ | ||
191 | #define GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST 31 | ||
192 | |||
193 | /** | ||
194 | * Request to transport to notify us about any blacklisting status | ||
195 | * changes on this connection (and to immediately send all | ||
196 | * active blacklist entries). | ||
197 | */ | ||
198 | #define GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_NOTIFY 32 | ||
199 | |||
188 | 200 | ||
189 | /** | 201 | /** |
190 | * Request addition of a HELLO | 202 | * Request addition of a HELLO |
191 | */ | 203 | */ |
192 | #define GNUNET_MESSAGE_TYPE_PEERINFO_ADD 32 | 204 | #define GNUNET_MESSAGE_TYPE_PEERINFO_ADD 36 |
193 | 205 | ||
194 | /** | 206 | /** |
195 | * Request update and listing of a peer. | 207 | * Request update and listing of a peer. |
196 | */ | 208 | */ |
197 | #define GNUNET_MESSAGE_TYPE_PEERINFO_GET 33 | 209 | #define GNUNET_MESSAGE_TYPE_PEERINFO_GET 37 |
198 | 210 | ||
199 | /** | 211 | /** |
200 | * Request update and listing of all peers. | 212 | * Request update and listing of all peers. |
201 | */ | 213 | */ |
202 | #define GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL 34 | 214 | #define GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL 38 |
203 | 215 | ||
204 | /** | 216 | /** |
205 | * Information about one of the peers. | 217 | * Information about one of the peers. |
206 | */ | 218 | */ |
207 | #define GNUNET_MESSAGE_TYPE_PEERINFO_INFO 35 | 219 | #define GNUNET_MESSAGE_TYPE_PEERINFO_INFO 39 |
208 | 220 | ||
209 | /** | 221 | /** |
210 | * End of information about other peers. | 222 | * End of information about other peers. |
211 | */ | 223 | */ |
212 | #define GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END 36 | 224 | #define GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END 40 |
213 | 225 | ||
214 | /** | 226 | /** |
215 | * Start notifying this client about all changes to | 227 | * Start notifying this client about all changes to |
216 | * the known peers until it disconnects. | 228 | * the known peers until it disconnects. |
217 | */ | 229 | */ |
218 | #define GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY 37 | 230 | #define GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY 41 |
219 | 231 | ||
220 | 232 | ||
221 | /** | 233 | /** |
222 | * Welcome message between TCP transports. | 234 | * Welcome message between TCP transports. |
223 | */ | 235 | */ |
224 | #define GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME 40 | 236 | #define GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME 60 |
225 | 237 | ||
226 | /** | 238 | /** |
227 | * Welcome message between TCP transports. | 239 | * Welcome message between TCP transports. |
diff --git a/src/include/gnunet_transport_service.h b/src/include/gnunet_transport_service.h index 10b7e8f89..0d378cbd3 100644 --- a/src/include/gnunet_transport_service.h +++ b/src/include/gnunet_transport_service.h | |||
@@ -294,6 +294,11 @@ GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched, | |||
294 | 294 | ||
295 | 295 | ||
296 | 296 | ||
297 | /** | ||
298 | * Handle for blacklisting requests. | ||
299 | */ | ||
300 | struct GNUNET_TRANSPORT_BlacklistRequest; | ||
301 | |||
297 | 302 | ||
298 | /** | 303 | /** |
299 | * Blacklist a peer for a given period of time. All connections | 304 | * Blacklist a peer for a given period of time. All connections |
@@ -307,12 +312,33 @@ GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched, | |||
307 | * @param peer identity of peer to blacklist | 312 | * @param peer identity of peer to blacklist |
308 | * @param duration how long to blacklist, use GNUNET_TIME_UNIT_ZERO to | 313 | * @param duration how long to blacklist, use GNUNET_TIME_UNIT_ZERO to |
309 | * re-enable connections | 314 | * re-enable connections |
315 | * @param timeout when should this operation (trying to establish the | ||
316 | * blacklisting time out) | ||
317 | * @param cont continuation to call once the request has been processed | ||
318 | * @param cont_cls closure for cont | ||
319 | * @return NULL on error, otherwise handle for cancellation | ||
310 | */ | 320 | */ |
311 | void | 321 | struct GNUNET_TRANSPORT_BlacklistRequest * |
312 | GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched, | 322 | GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched, |
313 | const struct GNUNET_CONFIGURATION_Handle *cfg, | 323 | const struct GNUNET_CONFIGURATION_Handle *cfg, |
314 | const struct GNUNET_PeerIdentity *peer, | 324 | const struct GNUNET_PeerIdentity *peer, |
315 | struct GNUNET_TIME_Relative duration); | 325 | struct GNUNET_TIME_Relative duration, |
326 | struct GNUNET_TIME_Relative timeout, | ||
327 | GNUNET_SCHEDULER_Task cont, | ||
328 | void *cont_cls); | ||
329 | |||
330 | |||
331 | /** | ||
332 | * Abort transmitting the blacklist request. Note that this function | ||
333 | * is NOT for removing a peer from the blacklist (for that, call | ||
334 | * GNUNET_TRANSPORT_blacklist with a duration of zero). This function | ||
335 | * is only for aborting the transmission of a blacklist request | ||
336 | * (i.e. because of shutdown). | ||
337 | * | ||
338 | * @param br handle of the request that is to be cancelled | ||
339 | */ | ||
340 | void | ||
341 | GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_BlacklistRequest * br); | ||
316 | 342 | ||
317 | 343 | ||
318 | /** | 344 | /** |
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 8d5ea59dd..87860c202 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am | |||
@@ -15,7 +15,8 @@ lib_LTLIBRARIES = \ | |||
15 | libgnunettransport.la | 15 | libgnunettransport.la |
16 | 16 | ||
17 | libgnunettransport_la_SOURCES = \ | 17 | libgnunettransport_la_SOURCES = \ |
18 | transport_api.c transport.h | 18 | transport_api.c transport.h \ |
19 | transport_api_blacklist.c | ||
19 | libgnunettransport_la_LIBADD = \ | 20 | libgnunettransport_la_LIBADD = \ |
20 | $(top_builddir)/src/arm/libgnunetarm.la \ | 21 | $(top_builddir)/src/arm/libgnunetarm.la \ |
21 | $(top_builddir)/src/hello/libgnunethello.la \ | 22 | $(top_builddir)/src/hello/libgnunethello.la \ |
diff --git a/src/transport/transport.h b/src/transport/transport.h index fad430009..edbf16685 100644 --- a/src/transport/transport.h +++ b/src/transport/transport.h | |||
@@ -231,8 +231,6 @@ struct OutboundMessage | |||
231 | }; | 231 | }; |
232 | 232 | ||
233 | 233 | ||
234 | |||
235 | |||
236 | /** | 234 | /** |
237 | * Message from the library to the transport service | 235 | * Message from the library to the transport service |
238 | * asking for converting a transport address to a | 236 | * asking for converting a transport address to a |
@@ -247,6 +245,12 @@ struct AddressLookupMessage | |||
247 | struct GNUNET_MessageHeader header; | 245 | struct GNUNET_MessageHeader header; |
248 | 246 | ||
249 | /** | 247 | /** |
248 | * Should the conversion use numeric IP addresses (otherwise | ||
249 | * a reverse DNS lookup is OK -- if applicable). | ||
250 | */ | ||
251 | int32_t numeric_only GNUNET_PACKED; | ||
252 | |||
253 | /** | ||
250 | * timeout to give up. | 254 | * timeout to give up. |
251 | */ | 255 | */ |
252 | struct GNUNET_TIME_AbsoluteNBO timeout; | 256 | struct GNUNET_TIME_AbsoluteNBO timeout; |
@@ -261,4 +265,37 @@ struct AddressLookupMessage | |||
261 | }; | 265 | }; |
262 | 266 | ||
263 | 267 | ||
268 | |||
269 | /** | ||
270 | * Change in blacklisting (either request or notification, | ||
271 | * depending on which direction it is going). | ||
272 | */ | ||
273 | struct BlacklistMessage | ||
274 | { | ||
275 | |||
276 | /** | ||
277 | * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST | ||
278 | */ | ||
279 | struct GNUNET_MessageHeader header; | ||
280 | |||
281 | /** | ||
282 | * Reserved (for alignment). | ||
283 | */ | ||
284 | uint32_t reserved GNUNET_PACKED; | ||
285 | |||
286 | /** | ||
287 | * Which peer is being blacklisted (or has seen its | ||
288 | * blacklisting expire)? | ||
289 | */ | ||
290 | struct GNUNET_PeerIdentity peer; | ||
291 | |||
292 | /** | ||
293 | * Until what time is this peer blacklisted (zero for | ||
294 | * no longer blacklisted). | ||
295 | */ | ||
296 | struct GNUNET_TIME_AbsoluteNBO until; | ||
297 | |||
298 | }; | ||
299 | |||
300 | |||
264 | /* end of transport.h */ | 301 | /* end of transport.h */ |
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 */ | ||