transport service hello reduction, klocwork fixes
[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    * 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   GNUNET_assert (br->client != NULL);
182   br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
183                                                 sizeof (struct GNUNET_MessageHeader),
184                                                 GNUNET_TIME_UNIT_FOREVER_REL,
185                                                 GNUNET_YES,
186                                                 &transmit_blacklist_init,
187                                                 br);
188 }
189
190
191 /**
192  * Transmit the blacklist response to the service.
193  *
194  * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
195  * @param size number of bytes available in buf
196  * @param buf where the callee should write the message
197  * @return number of bytes written to buf
198  */
199 static size_t
200 transmit_blacklist_reply (void *cls,
201                           size_t size, void *buf)
202 {
203   struct GNUNET_TRANSPORT_Blacklist *br = cls;
204   struct BlacklistMessage req;
205
206   if (buf == NULL)
207     {
208       reconnect (br);
209       return 0;
210     }
211   req.header.size = htons (sizeof (req));
212   req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
213   req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
214   req.peer = br->peer;
215   memcpy (buf, &req, sizeof (req));
216   receive (br);
217   return sizeof (req);
218 }
219
220
221 /**
222  * Send our reply to a blacklisting request.
223  *
224  * @param br our overall context
225  */
226 static void
227 reply (struct GNUNET_TRANSPORT_Blacklist *br)
228 {
229   br->th = GNUNET_CLIENT_notify_transmit_ready (br->client,
230                                                 sizeof (struct BlacklistMessage),
231                                                 GNUNET_TIME_UNIT_FOREVER_REL,
232                                                 GNUNET_NO,
233                                                 &transmit_blacklist_reply,
234                                                 br);
235   if (br->th == NULL)
236     {
237       reconnect (br);
238       return;
239     }
240 }
241
242
243 /**
244  * Install a blacklist callback.  The service will be queried for all
245  * existing connections as well as any fresh connections to check if
246  * they are permitted.  If the blacklisting callback is unregistered,
247  * all hosts that were denied in the past will automatically be
248  * whitelisted again.  Cancelling the blacklist handle is also the
249  * only way to re-enable connections from peers that were previously
250  * blacklisted.
251  *
252  * @param sched scheduler to use
253  * @param cfg configuration to use
254  * @param cb callback to invoke to check if connections are allowed
255  * @param cb_cls closure for cb
256  * @return NULL on error, otherwise handle for cancellation
257  */
258 struct GNUNET_TRANSPORT_Blacklist *
259 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
260                             const struct GNUNET_CONFIGURATION_Handle *cfg,
261                             GNUNET_TRANSPORT_BlacklistCallback cb,
262                             void *cb_cls)
263 {
264   struct GNUNET_CLIENT_Connection * client;
265   struct GNUNET_TRANSPORT_Blacklist *ret;
266
267   client = GNUNET_CLIENT_connect (sched, "transport", cfg);
268   if (NULL == client)
269     return NULL;
270   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
271   ret->client = client;
272   ret->sched = sched;
273   ret->cfg = cfg;
274   ret->th = GNUNET_CLIENT_notify_transmit_ready (client,
275                                                  sizeof (struct GNUNET_MessageHeader),
276                                                  GNUNET_TIME_UNIT_FOREVER_REL,
277                                                  GNUNET_YES,
278                                                  &transmit_blacklist_init,
279                                                  ret);
280   return ret;
281 }
282
283
284 /**
285  * Abort the blacklist.  Note that this function is the only way for
286  * removing a peer from the blacklist.
287  *
288  * @param br handle of the request that is to be cancelled
289  */
290 void
291 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
292 {
293   if (br->th != NULL)
294     GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
295   GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
296   GNUNET_free (br);
297 }
298
299
300 /* end of transport_api_blacklist.c */