diff options
-rw-r--r-- | src/transport/transport_api_blacklist.c | 215 |
1 files changed, 57 insertions, 158 deletions
diff --git a/src/transport/transport_api_blacklist.c b/src/transport/transport_api_blacklist.c index 2a401f593..2835e225f 100644 --- a/src/transport/transport_api_blacklist.c +++ b/src/transport/transport_api_blacklist.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2010-2014 GNUnet e.V. | 3 | Copyright (C) 2010-2014, 2016 GNUnet e.V. |
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 |
@@ -40,7 +40,7 @@ struct GNUNET_TRANSPORT_Blacklist | |||
40 | /** | 40 | /** |
41 | * Connection to transport service. | 41 | * Connection to transport service. |
42 | */ | 42 | */ |
43 | struct GNUNET_CLIENT_Connection *client; | 43 | struct GNUNET_MQ_Handle *mq; |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * Configuration to use. | 46 | * Configuration to use. |
@@ -48,11 +48,6 @@ struct GNUNET_TRANSPORT_Blacklist | |||
48 | const struct GNUNET_CONFIGURATION_Handle *cfg; | 48 | const struct GNUNET_CONFIGURATION_Handle *cfg; |
49 | 49 | ||
50 | /** | 50 | /** |
51 | * Pending handle for the current request. | ||
52 | */ | ||
53 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
54 | |||
55 | /** | ||
56 | * Function to call for determining if a peer is allowed | 51 | * Function to call for determining if a peer is allowed |
57 | * to communicate with us. | 52 | * to communicate with us. |
58 | */ | 53 | */ |
@@ -63,11 +58,6 @@ struct GNUNET_TRANSPORT_Blacklist | |||
63 | */ | 58 | */ |
64 | void *cb_cls; | 59 | void *cb_cls; |
65 | 60 | ||
66 | /** | ||
67 | * Peer currently under consideration. | ||
68 | */ | ||
69 | struct GNUNET_PeerIdentity peer; | ||
70 | |||
71 | }; | 61 | }; |
72 | 62 | ||
73 | 63 | ||
@@ -81,82 +71,44 @@ reconnect (struct GNUNET_TRANSPORT_Blacklist *br); | |||
81 | 71 | ||
82 | 72 | ||
83 | /** | 73 | /** |
84 | * Send our reply to a blacklisting request. | ||
85 | * | ||
86 | * @param br our overall context | ||
87 | */ | ||
88 | static void | ||
89 | reply (struct GNUNET_TRANSPORT_Blacklist *br); | ||
90 | |||
91 | |||
92 | /** | ||
93 | * Handle blacklist queries. | 74 | * Handle blacklist queries. |
94 | * | 75 | * |
95 | * @param cls our overall handle | 76 | * @param cls our overall handle |
96 | * @param msg query | 77 | * @param bm query |
97 | */ | 78 | */ |
98 | static void | 79 | static void |
99 | query_handler (void *cls, | 80 | handle_query (void *cls, |
100 | const struct GNUNET_MessageHeader *msg) | 81 | const struct BlacklistMessage *bm) |
101 | { | 82 | { |
102 | struct GNUNET_TRANSPORT_Blacklist *br = cls; | 83 | struct GNUNET_TRANSPORT_Blacklist *br = cls; |
103 | const struct BlacklistMessage *bm; | 84 | struct GNUNET_MQ_Envelope *env; |
85 | struct BlacklistMessage *res; | ||
104 | 86 | ||
105 | GNUNET_assert (NULL != br); | ||
106 | if ((NULL == msg) || | ||
107 | (ntohs (msg->size) != sizeof (struct BlacklistMessage)) || | ||
108 | (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY)) | ||
109 | { | ||
110 | reconnect (br); | ||
111 | return; | ||
112 | } | ||
113 | bm = (const struct BlacklistMessage *) msg; | ||
114 | GNUNET_break (0 == ntohl (bm->is_allowed)); | 87 | GNUNET_break (0 == ntohl (bm->is_allowed)); |
115 | br->peer = bm->peer; | 88 | env = GNUNET_MQ_msg (res, |
116 | reply (br); | 89 | GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY); |
90 | res->is_allowed = htonl (br->cb (br->cb_cls, | ||
91 | &bm->peer)); | ||
92 | res->peer = bm->peer; | ||
93 | GNUNET_MQ_send (br->mq, | ||
94 | env); | ||
117 | } | 95 | } |
118 | 96 | ||
119 | |||
120 | /** | 97 | /** |
121 | * Receive blacklist queries from transport service. | 98 | * Generic error handler, called with the appropriate error code and |
99 | * the same closure specified at the creation of the message queue. | ||
100 | * Not every message queue implementation supports an error handler. | ||
122 | * | 101 | * |
123 | * @param br overall handle | 102 | * @param cls closure with the `struct GNUNET_TRANSPORT_Blacklist *` |
103 | * @param error error code | ||
124 | */ | 104 | */ |
125 | static void | 105 | static void |
126 | receive (struct GNUNET_TRANSPORT_Blacklist *br) | 106 | mq_error_handler (void *cls, |
127 | { | 107 | enum GNUNET_MQ_Error error) |
128 | GNUNET_CLIENT_receive (br->client, &query_handler, br, | ||
129 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
130 | } | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Transmit the blacklist initialization request to the service. | ||
135 | * | ||
136 | * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *` | ||
137 | * @param size number of bytes available in @a buf | ||
138 | * @param buf where the callee should write the message | ||
139 | * @return number of bytes written to @a buf | ||
140 | */ | ||
141 | static size_t | ||
142 | transmit_blacklist_init (void *cls, | ||
143 | size_t size, | ||
144 | void *buf) | ||
145 | { | 108 | { |
146 | struct GNUNET_TRANSPORT_Blacklist *br = cls; | 109 | struct GNUNET_TRANSPORT_Blacklist *br = cls; |
147 | struct GNUNET_MessageHeader req; | ||
148 | 110 | ||
149 | br->th = NULL; | 111 | reconnect (br); |
150 | if (NULL == buf) | ||
151 | { | ||
152 | reconnect (br); | ||
153 | return 0; | ||
154 | } | ||
155 | req.size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
156 | req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT); | ||
157 | memcpy (buf, &req, sizeof (req)); | ||
158 | receive (br); | ||
159 | return sizeof (req); | ||
160 | } | 112 | } |
161 | 113 | ||
162 | 114 | ||
@@ -168,72 +120,29 @@ transmit_blacklist_init (void *cls, | |||
168 | static void | 120 | static void |
169 | reconnect (struct GNUNET_TRANSPORT_Blacklist *br) | 121 | reconnect (struct GNUNET_TRANSPORT_Blacklist *br) |
170 | { | 122 | { |
171 | if (NULL != br->client) | 123 | GNUNET_MQ_hd_fixed_size (query, |
172 | GNUNET_CLIENT_disconnect (br->client); | 124 | GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY, |
173 | br->client = GNUNET_CLIENT_connect ("transport", br->cfg); | 125 | struct BlacklistMessage); |
174 | GNUNET_assert (NULL != br->client); | 126 | struct GNUNET_MQ_MessageHandler handlers[] = { |
175 | br->th = | 127 | make_query_handler (br), |
176 | GNUNET_CLIENT_notify_transmit_ready (br->client, | 128 | GNUNET_MQ_handler_end () |
177 | sizeof (struct GNUNET_MessageHeader), | 129 | }; |
178 | GNUNET_TIME_UNIT_FOREVER_REL, | 130 | struct GNUNET_MQ_Envelope *env; |
179 | GNUNET_YES, &transmit_blacklist_init, | 131 | struct GNUNET_MessageHeader *req; |
180 | br); | 132 | |
181 | } | 133 | if (NULL != br->mq) |
182 | 134 | GNUNET_MQ_destroy (br->mq); | |
183 | 135 | br->mq = GNUNET_CLIENT_connecT (br->cfg, | |
184 | /** | 136 | "transport", |
185 | * Transmit the blacklist response to the service. | 137 | handlers, |
186 | * | 138 | &mq_error_handler, |
187 | * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *` | 139 | br); |
188 | * @param size number of bytes available in @a buf | 140 | if (NULL == br->mq) |
189 | * @param buf where the callee should write the message | ||
190 | * @return number of bytes written to @a buf | ||
191 | */ | ||
192 | static size_t | ||
193 | transmit_blacklist_reply (void *cls, | ||
194 | size_t size, | ||
195 | void *buf) | ||
196 | { | ||
197 | struct GNUNET_TRANSPORT_Blacklist *br = cls; | ||
198 | struct BlacklistMessage req; | ||
199 | |||
200 | br->th = NULL; | ||
201 | if (NULL == buf) | ||
202 | { | ||
203 | reconnect (br); | ||
204 | return 0; | ||
205 | } | ||
206 | req.header.size = htons (sizeof (req)); | ||
207 | req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY); | ||
208 | req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer)); | ||
209 | req.peer = br->peer; | ||
210 | memcpy (buf, &req, sizeof (req)); | ||
211 | br->th = NULL; | ||
212 | receive (br); | ||
213 | return sizeof (req); | ||
214 | } | ||
215 | |||
216 | |||
217 | /** | ||
218 | * Send our reply to a blacklisting request. | ||
219 | * | ||
220 | * @param br our overall context | ||
221 | */ | ||
222 | static void | ||
223 | reply (struct GNUNET_TRANSPORT_Blacklist *br) | ||
224 | { | ||
225 | GNUNET_assert (NULL == br->th); | ||
226 | br->th = | ||
227 | GNUNET_CLIENT_notify_transmit_ready (br->client, | ||
228 | sizeof (struct BlacklistMessage), | ||
229 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
230 | GNUNET_NO, &transmit_blacklist_reply, | ||
231 | br); | ||
232 | if (NULL == br->th) | ||
233 | { | ||
234 | reconnect (br); | ||
235 | return; | 141 | return; |
236 | } | 142 | env = GNUNET_MQ_msg (req, |
143 | GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT); | ||
144 | GNUNET_MQ_send (br->mq, | ||
145 | env); | ||
237 | } | 146 | } |
238 | 147 | ||
239 | 148 | ||
@@ -256,24 +165,19 @@ GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
256 | GNUNET_TRANSPORT_BlacklistCallback cb, | 165 | GNUNET_TRANSPORT_BlacklistCallback cb, |
257 | void *cb_cls) | 166 | void *cb_cls) |
258 | { | 167 | { |
259 | struct GNUNET_CLIENT_Connection *client; | 168 | struct GNUNET_TRANSPORT_Blacklist *br; |
260 | struct GNUNET_TRANSPORT_Blacklist *ret; | 169 | |
261 | 170 | br = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist); | |
262 | client = GNUNET_CLIENT_connect ("transport", cfg); | 171 | br->cfg = cfg; |
263 | if (NULL == client) | 172 | br->cb = cb; |
173 | br->cb_cls = cb_cls; | ||
174 | reconnect (br); | ||
175 | if (NULL == br->mq) | ||
176 | { | ||
177 | GNUNET_free (br); | ||
264 | return NULL; | 178 | return NULL; |
265 | ret = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist); | 179 | } |
266 | ret->client = client; | 180 | return br; |
267 | ret->cfg = cfg; | ||
268 | ret->cb = cb; | ||
269 | ret->cb_cls = cb_cls; | ||
270 | ret->th = | ||
271 | GNUNET_CLIENT_notify_transmit_ready (client, | ||
272 | sizeof (struct GNUNET_MessageHeader), | ||
273 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
274 | GNUNET_YES, &transmit_blacklist_init, | ||
275 | ret); | ||
276 | return ret; | ||
277 | } | 181 | } |
278 | 182 | ||
279 | 183 | ||
@@ -286,12 +190,7 @@ GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
286 | void | 190 | void |
287 | GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br) | 191 | GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br) |
288 | { | 192 | { |
289 | if (NULL != br->th) | 193 | GNUNET_MQ_destroy (br->mq); |
290 | { | ||
291 | GNUNET_CLIENT_notify_transmit_ready_cancel (br->th); | ||
292 | br->th = NULL; | ||
293 | } | ||
294 | GNUNET_CLIENT_disconnect (br->client); | ||
295 | GNUNET_free (br); | 194 | GNUNET_free (br); |
296 | } | 195 | } |
297 | 196 | ||