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