glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / transport / transport_api_blacklist.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2014, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file transport/transport_api_blacklist.c
18  * @brief library to access the blacklisting functions of the transport service
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_arm_service.h"
24 #include "gnunet_hello_lib.h"
25 #include "gnunet_protocols.h"
26 #include "gnunet_transport_service.h"
27 #include "transport.h"
28
29 /**
30  * Handle for blacklisting requests.
31  */
32 struct GNUNET_TRANSPORT_Blacklist
33 {
34
35   /**
36    * Connection to transport service.
37    */
38   struct GNUNET_MQ_Handle *mq;
39
40   /**
41    * Configuration to use.
42    */
43   const struct GNUNET_CONFIGURATION_Handle *cfg;
44
45   /**
46    * Function to call for determining if a peer is allowed
47    * to communicate with us.
48    */
49   GNUNET_TRANSPORT_BlacklistCallback cb;
50
51   /**
52    * Closure for @e cb.
53    */
54   void *cb_cls;
55
56 };
57
58
59 /**
60  * Establish blacklist connection to transport service.
61  *
62  * @param br overall handle
63  */
64 static void
65 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
66
67
68 /**
69  * Handle blacklist queries.
70  *
71  * @param cls our overall handle
72  * @param bm query
73  */
74 static void
75 handle_query (void *cls,
76               const struct BlacklistMessage *bm)
77 {
78   struct GNUNET_TRANSPORT_Blacklist *br = cls;
79   struct GNUNET_MQ_Envelope *env;
80   struct BlacklistMessage *res;
81
82   GNUNET_break (0 == ntohl (bm->is_allowed));
83   env = GNUNET_MQ_msg (res,
84                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
85   res->is_allowed = htonl (br->cb (br->cb_cls,
86                                    &bm->peer));
87   res->peer = bm->peer;
88   GNUNET_MQ_send (br->mq,
89                   env);
90 }
91
92 /**
93  * Generic error handler, called with the appropriate error code and
94  * the same closure specified at the creation of the message queue.
95  * Not every message queue implementation supports an error handler.
96  *
97  * @param cls closure with the `struct GNUNET_TRANSPORT_Blacklist *`
98  * @param error error code
99  */
100 static void
101 mq_error_handler (void *cls,
102                   enum GNUNET_MQ_Error error)
103 {
104   struct GNUNET_TRANSPORT_Blacklist *br = cls;
105
106   reconnect (br);
107 }
108
109
110 /**
111  * Establish blacklist connection to transport service.
112  *
113  * @param br overall handle
114  */
115 static void
116 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
117 {
118   struct GNUNET_MQ_MessageHandler handlers[] = {
119     GNUNET_MQ_hd_fixed_size (query,
120                              GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY,
121                              struct BlacklistMessage,
122                              br),
123     GNUNET_MQ_handler_end ()
124   };
125   struct GNUNET_MQ_Envelope *env;
126   struct GNUNET_MessageHeader *req;
127
128   if (NULL != br->mq)
129     GNUNET_MQ_destroy (br->mq);
130   br->mq = GNUNET_CLIENT_connect (br->cfg,
131                                   "transport",
132                                   handlers,
133                                   &mq_error_handler,
134                                   br);
135   if (NULL == br->mq)
136     return;
137   env = GNUNET_MQ_msg (req,
138                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
139   GNUNET_MQ_send (br->mq,
140                   env);
141 }
142
143
144 /**
145  * Install a blacklist callback.  The service will be queried for all
146  * existing connections as well as any fresh connections to check if
147  * they are permitted.  If the blacklisting callback is unregistered,
148  * all hosts that were denied in the past will automatically be
149  * whitelisted again.  Cancelling the blacklist handle is also the
150  * only way to re-enable connections from peers that were previously
151  * blacklisted.
152  *
153  * @param cfg configuration to use
154  * @param cb callback to invoke to check if connections are allowed
155  * @param cb_cls closure for @a cb
156  * @return NULL on error, otherwise handle for cancellation
157  */
158 struct GNUNET_TRANSPORT_Blacklist *
159 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
160                             GNUNET_TRANSPORT_BlacklistCallback cb,
161                             void *cb_cls)
162 {
163   struct GNUNET_TRANSPORT_Blacklist *br;
164
165   br = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
166   br->cfg = cfg;
167   br->cb = cb;
168   br->cb_cls = cb_cls;
169   reconnect (br);
170   if (NULL == br->mq)
171   {
172     GNUNET_free (br);
173     return NULL;
174   }
175   return br;
176 }
177
178
179 /**
180  * Abort the blacklist.  Note that this function is the only way for
181  * removing a peer from the blacklist.
182  *
183  * @param br handle of the request that is to be cancelled
184  */
185 void
186 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
187 {
188   GNUNET_MQ_destroy (br->mq);
189   GNUNET_free (br);
190 }
191
192
193 /* end of transport_api_blacklist.c */