-fix (C) notices
[oweals/gnunet.git] / src / transport / transport_api_blacklist.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2014 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
33
34 /**
35  * Handle for blacklisting requests.
36  */
37 struct GNUNET_TRANSPORT_Blacklist
38 {
39
40   /**
41    * Connection to transport service.
42    */
43   struct GNUNET_CLIENT_Connection *client;
44
45   /**
46    * Configuration to use.
47    */
48   const struct GNUNET_CONFIGURATION_Handle *cfg;
49
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
57    * to communicate with us.
58    */
59   GNUNET_TRANSPORT_BlacklistCallback cb;
60
61   /**
62    * Closure for @e cb.
63    */
64   void *cb_cls;
65
66   /**
67    * Peer currently under consideration.
68    */
69   struct GNUNET_PeerIdentity peer;
70
71 };
72
73
74 /**
75  * Establish blacklist connection to transport service.
76  *
77  * @param br overall handle
78  */
79 static void
80 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
81
82
83 /**
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.
94  *
95  * @param cls our overall handle
96  * @param msg query
97  */
98 static void
99 query_handler (void *cls,
100                const struct GNUNET_MessageHeader *msg)
101 {
102   struct GNUNET_TRANSPORT_Blacklist *br = cls;
103   const struct BlacklistMessage *bm;
104
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));
115   br->peer = bm->peer;
116   reply (br);
117 }
118
119
120 /**
121  * Receive blacklist queries from transport service.
122  *
123  * @param br overall handle
124  */
125 static void
126 receive (struct GNUNET_TRANSPORT_Blacklist *br)
127 {
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 {
146   struct GNUNET_TRANSPORT_Blacklist *br = cls;
147   struct GNUNET_MessageHeader req;
148
149   br->th = NULL;
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 }
161
162
163 /**
164  * Establish blacklist connection to transport service.
165  *
166  * @param br overall handle
167  */
168 static void
169 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
170 {
171   if (NULL != br->client)
172     GNUNET_CLIENT_disconnect (br->client);
173   br->client = GNUNET_CLIENT_connect ("transport", br->cfg);
174   GNUNET_assert (NULL != br->client);
175   br->th =
176       GNUNET_CLIENT_notify_transmit_ready (br->client,
177                                            sizeof (struct GNUNET_MessageHeader),
178                                            GNUNET_TIME_UNIT_FOREVER_REL,
179                                            GNUNET_YES, &transmit_blacklist_init,
180                                            br);
181 }
182
183
184 /**
185  * Transmit the blacklist response to the service.
186  *
187  * @param cls closure with `struct GNUNET_TRANSPORT_Blacklist *`
188  * @param size number of bytes available in @a buf
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;
236   }
237 }
238
239
240 /**
241  * Install a blacklist callback.  The service will be queried for all
242  * existing connections as well as any fresh connections to check if
243  * they are permitted.  If the blacklisting callback is unregistered,
244  * all hosts that were denied in the past will automatically be
245  * whitelisted again.  Cancelling the blacklist handle is also the
246  * only way to re-enable connections from peers that were previously
247  * blacklisted.
248  *
249  * @param cfg configuration to use
250  * @param cb callback to invoke to check if connections are allowed
251  * @param cb_cls closure for @a cb
252  * @return NULL on error, otherwise handle for cancellation
253  */
254 struct GNUNET_TRANSPORT_Blacklist *
255 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
256                             GNUNET_TRANSPORT_BlacklistCallback cb,
257                             void *cb_cls)
258 {
259   struct GNUNET_CLIENT_Connection *client;
260   struct GNUNET_TRANSPORT_Blacklist *ret;
261
262   client = GNUNET_CLIENT_connect ("transport", cfg);
263   if (NULL == client)
264     return NULL;
265   ret = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
266   ret->client = client;
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 }
278
279
280 /**
281  * Abort the blacklist.  Note that this function is the only way for
282  * removing a peer from the blacklist.
283  *
284  * @param br handle of the request that is to be cancelled
285  */
286 void
287 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
288 {
289   if (NULL != br->th)
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);
296 }
297
298
299 /* end of transport_api_blacklist.c */