error msg
[oweals/gnunet.git] / src / transport / transport_api_blacklist.c
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 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., 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_Blacklist
40 {
41
42   /**
43    * Connection to transport service.
44    */
45   struct GNUNET_CLIENT_Connection *client;
46
47   /**
48    * Configuration to use.
49    */
50   const struct GNUNET_CONFIGURATION_Handle *cfg;
51
52   /**
53    * Pending handle for the current request.
54    */
55   struct GNUNET_CLIENT_TransmitHandle *th;
56
57   /**
58    * Function to call for determining if a peer is allowed
59    * to communicate with us.
60    */
61   GNUNET_TRANSPORT_BlacklistCallback cb;
62
63   /**
64    * Closure for 'cb'.
65    */
66   void *cb_cls;
67
68   /**
69    * Peer currently under consideration.
70    */
71   struct GNUNET_PeerIdentity peer;
72
73 };
74
75
76 /**
77  * Establish blacklist connection to transport service.
78  *
79  * @param br overall handle
80  */
81 static void
82 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
83
84
85 /**
86  * Send our reply to a blacklisting request.
87  *
88  * @param br our overall context
89  */
90 static void
91 reply (struct GNUNET_TRANSPORT_Blacklist *br);
92
93
94 /**
95  * Handle blacklist queries.
96  *
97  * @param cls our overall handle
98  * @param msg query
99  */
100 static void
101 query_handler (void *cls, const struct GNUNET_MessageHeader *msg)
102 {
103   struct GNUNET_TRANSPORT_Blacklist *br = cls;
104   const struct BlacklistMessage *bm;
105
106   GNUNET_assert (br != NULL);
107   if ((NULL == msg) ||
108       (ntohs (msg->size) != sizeof (struct BlacklistMessage)) ||
109       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY))
110   {
111     reconnect (br);
112     return;
113   }
114   bm = (const struct BlacklistMessage *) msg;
115   GNUNET_break (0 == ntohl (bm->is_allowed));
116   br->peer = bm->peer;
117   reply (br);
118 }
119
120
121 /**
122  * Receive blacklist queries from transport service.
123  *
124  * @param br overall handle
125  */
126 static void
127 receive (struct GNUNET_TRANSPORT_Blacklist *br)
128 {
129   GNUNET_CLIENT_receive (br->client, &query_handler, br,
130                          GNUNET_TIME_UNIT_FOREVER_REL);
131 }
132
133
134 /**
135  * Transmit the blacklist initialization request to the service.
136  *
137  * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
138  * @param size number of bytes available in buf
139  * @param buf where the callee should write the message
140  * @return number of bytes written to buf
141  */
142 static size_t
143 transmit_blacklist_init (void *cls, size_t size, void *buf)
144 {
145   struct GNUNET_TRANSPORT_Blacklist *br = cls;
146   struct GNUNET_MessageHeader req;
147
148   br->th = NULL;
149   if (buf == NULL)
150   {
151     reconnect (br);
152     return 0;
153   }
154   req.size = htons (sizeof (struct GNUNET_MessageHeader));
155   req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
156   memcpy (buf, &req, sizeof (req));
157   receive (br);
158   return sizeof (req);
159 }
160
161
162 /**
163  * Establish blacklist connection to transport service.
164  *
165  * @param br overall handle
166  */
167 static void
168 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
169 {
170   if (br->client != NULL)
171     GNUNET_CLIENT_disconnect (br->client);
172   br->client = GNUNET_CLIENT_connect ("transport", br->cfg);
173   GNUNET_assert (br->client != NULL);
174   br->th =
175       GNUNET_CLIENT_notify_transmit_ready (br->client,
176                                            sizeof (struct GNUNET_MessageHeader),
177                                            GNUNET_TIME_UNIT_FOREVER_REL,
178                                            GNUNET_YES, &transmit_blacklist_init,
179                                            br);
180 }
181
182
183 /**
184  * Transmit the blacklist response to the service.
185  *
186  * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
187  * @param size number of bytes available in buf
188  * @param buf where the callee should write the message
189  * @return number of bytes written to buf
190  */
191 static size_t
192 transmit_blacklist_reply (void *cls, size_t size, void *buf)
193 {
194   struct GNUNET_TRANSPORT_Blacklist *br = cls;
195   struct BlacklistMessage req;
196
197   br->th = NULL;
198   if (buf == NULL)
199   {
200     reconnect (br);
201     return 0;
202   }
203   req.header.size = htons (sizeof (req));
204   req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
205   req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
206   req.peer = br->peer;
207   memcpy (buf, &req, sizeof (req));
208   br->th = NULL;
209   receive (br);
210   return sizeof (req);
211 }
212
213
214 /**
215  * Send our reply to a blacklisting request.
216  *
217  * @param br our overall context
218  */
219 static void
220 reply (struct GNUNET_TRANSPORT_Blacklist *br)
221 {
222   GNUNET_assert (br->th == NULL);
223   br->th =
224       GNUNET_CLIENT_notify_transmit_ready (br->client,
225                                            sizeof (struct BlacklistMessage),
226                                            GNUNET_TIME_UNIT_FOREVER_REL,
227                                            GNUNET_NO, &transmit_blacklist_reply,
228                                            br);
229   if (br->th == NULL)
230   {
231     reconnect (br);
232     return;
233   }
234 }
235
236
237 /**
238  * Install a blacklist callback.  The service will be queried for all
239  * existing connections as well as any fresh connections to check if
240  * they are permitted.  If the blacklisting callback is unregistered,
241  * all hosts that were denied in the past will automatically be
242  * whitelisted again.  Cancelling the blacklist handle is also the
243  * only way to re-enable connections from peers that were previously
244  * blacklisted.
245  *
246  * @param cfg configuration to use
247  * @param cb callback to invoke to check if connections are allowed
248  * @param cb_cls closure for cb
249  * @return NULL on error, otherwise handle for cancellation
250  */
251 struct GNUNET_TRANSPORT_Blacklist *
252 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
253                             GNUNET_TRANSPORT_BlacklistCallback cb, void *cb_cls)
254 {
255   struct GNUNET_CLIENT_Connection *client;
256   struct GNUNET_TRANSPORT_Blacklist *ret;
257
258   client = GNUNET_CLIENT_connect ("transport", cfg);
259   if (NULL == client)
260     return NULL;
261   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
262   ret->client = client;
263   ret->cfg = cfg;
264   ret->cb = cb;
265   ret->cb_cls = cb_cls;
266   GNUNET_assert (ret->th == NULL);
267   ret->th =
268       GNUNET_CLIENT_notify_transmit_ready (client,
269                                            sizeof (struct GNUNET_MessageHeader),
270                                            GNUNET_TIME_UNIT_FOREVER_REL,
271                                            GNUNET_YES, &transmit_blacklist_init,
272                                            ret);
273   return ret;
274 }
275
276
277 /**
278  * Abort the blacklist.  Note that this function is the only way for
279  * removing a peer from the blacklist.
280  *
281  * @param br handle of the request that is to be cancelled
282  */
283 void
284 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
285 {
286   if (br->th != NULL)
287   {
288     GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
289     br->th = NULL;
290   }
291   GNUNET_CLIENT_disconnect (br->client);
292   GNUNET_free (br);
293 }
294
295
296 /* end of transport_api_blacklist.c */