-changing exit helper code to automatically do the network configuration for an exit...
[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   if ((ntohs (msg->size) != sizeof (struct BlacklistMessage)) ||
107       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY))
108   {
109     reconnect (br);
110     return;
111   }
112   bm = (const struct BlacklistMessage *) msg;
113   GNUNET_break (0 == ntohl (bm->is_allowed));
114   br->peer = bm->peer;
115   reply (br);
116 }
117
118
119 /**
120  * Receive blacklist queries from transport service.
121  *
122  * @param br overall handle
123  */
124 static void
125 receive (struct GNUNET_TRANSPORT_Blacklist *br)
126 {
127   GNUNET_CLIENT_receive (br->client, &query_handler, br,
128                          GNUNET_TIME_UNIT_FOREVER_REL);
129 }
130
131
132 /**
133  * Transmit the blacklist initialization request to the service.
134  *
135  * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
136  * @param size number of bytes available in buf
137  * @param buf where the callee should write the message
138  * @return number of bytes written to buf
139  */
140 static size_t
141 transmit_blacklist_init (void *cls, size_t size, void *buf)
142 {
143   struct GNUNET_TRANSPORT_Blacklist *br = cls;
144   struct GNUNET_MessageHeader req;
145
146   if (buf == NULL)
147   {
148     reconnect (br);
149     return 0;
150   }
151   req.size = htons (sizeof (struct GNUNET_MessageHeader));
152   req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
153   memcpy (buf, &req, sizeof (req));
154   receive (br);
155   return sizeof (req);
156 }
157
158
159 /**
160  * Establish blacklist connection to transport service.
161  *
162  * @param br overall handle
163  */
164 static void
165 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
166 {
167   if (br->client != NULL)
168     GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
169   br->client = GNUNET_CLIENT_connect ("transport", br->cfg);
170   GNUNET_assert (br->client != NULL);
171   br->th =
172       GNUNET_CLIENT_notify_transmit_ready (br->client,
173                                            sizeof (struct GNUNET_MessageHeader),
174                                            GNUNET_TIME_UNIT_FOREVER_REL,
175                                            GNUNET_YES, &transmit_blacklist_init,
176                                            br);
177 }
178
179
180 /**
181  * Transmit the blacklist response to the service.
182  *
183  * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
184  * @param size number of bytes available in buf
185  * @param buf where the callee should write the message
186  * @return number of bytes written to buf
187  */
188 static size_t
189 transmit_blacklist_reply (void *cls, size_t size, void *buf)
190 {
191   struct GNUNET_TRANSPORT_Blacklist *br = cls;
192   struct BlacklistMessage req;
193
194   if (buf == NULL)
195   {
196     reconnect (br);
197     return 0;
198   }
199   req.header.size = htons (sizeof (req));
200   req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
201   req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
202   req.peer = br->peer;
203   memcpy (buf, &req, sizeof (req));
204   receive (br);
205   return sizeof (req);
206 }
207
208
209 /**
210  * Send our reply to a blacklisting request.
211  *
212  * @param br our overall context
213  */
214 static void
215 reply (struct GNUNET_TRANSPORT_Blacklist *br)
216 {
217   br->th =
218       GNUNET_CLIENT_notify_transmit_ready (br->client,
219                                            sizeof (struct BlacklistMessage),
220                                            GNUNET_TIME_UNIT_FOREVER_REL,
221                                            GNUNET_NO, &transmit_blacklist_reply,
222                                            br);
223   if (br->th == NULL)
224   {
225     reconnect (br);
226     return;
227   }
228 }
229
230
231 /**
232  * Install a blacklist callback.  The service will be queried for all
233  * existing connections as well as any fresh connections to check if
234  * they are permitted.  If the blacklisting callback is unregistered,
235  * all hosts that were denied in the past will automatically be
236  * whitelisted again.  Cancelling the blacklist handle is also the
237  * only way to re-enable connections from peers that were previously
238  * blacklisted.
239  *
240  * @param cfg configuration to use
241  * @param cb callback to invoke to check if connections are allowed
242  * @param cb_cls closure for cb
243  * @return NULL on error, otherwise handle for cancellation
244  */
245 struct GNUNET_TRANSPORT_Blacklist *
246 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
247                             GNUNET_TRANSPORT_BlacklistCallback cb, void *cb_cls)
248 {
249   struct GNUNET_CLIENT_Connection *client;
250   struct GNUNET_TRANSPORT_Blacklist *ret;
251
252   client = GNUNET_CLIENT_connect ("transport", cfg);
253   if (NULL == client)
254     return NULL;
255   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
256   ret->client = client;
257   ret->cfg = cfg;
258   ret->th =
259       GNUNET_CLIENT_notify_transmit_ready (client,
260                                            sizeof (struct GNUNET_MessageHeader),
261                                            GNUNET_TIME_UNIT_FOREVER_REL,
262                                            GNUNET_YES, &transmit_blacklist_init,
263                                            ret);
264   return ret;
265 }
266
267
268 /**
269  * Abort the blacklist.  Note that this function is the only way for
270  * removing a peer from the blacklist.
271  *
272  * @param br handle of the request that is to be cancelled
273  */
274 void
275 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
276 {
277   if (br->th != NULL)
278     GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
279   GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
280   GNUNET_free (br);
281 }
282
283
284 /* end of transport_api_blacklist.c */