fix warnings
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
33
34 /**
35  * Handle for blacklisting requests.
36  */
37 struct GNUNET_TRANSPORT_Blacklist
38 {
39   /**
40    * Connection to transport service.
41    */
42   struct GNUNET_MQ_Handle *mq;
43
44   /**
45    * Configuration to use.
46    */
47   const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49   /**
50    * Function to call for determining if a peer is allowed
51    * to communicate with us.
52    */
53   GNUNET_TRANSPORT_BlacklistCallback cb;
54
55   /**
56    * Closure for @e cb.
57    */
58   void *cb_cls;
59 };
60
61
62 /**
63  * Establish blacklist connection to transport service.
64  *
65  * @param br overall handle
66  */
67 static void
68 reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
69
70
71 /**
72  * Handle blacklist queries.
73  *
74  * @param cls our overall handle
75  * @param bm query
76  */
77 static void
78 handle_query (void *cls,
79               const struct BlacklistMessage *bm)
80 {
81   struct GNUNET_TRANSPORT_Blacklist *br = cls;
82   struct GNUNET_MQ_Envelope *env;
83   struct BlacklistMessage *res;
84
85   GNUNET_break (0 == ntohl (bm->is_allowed));
86   env = GNUNET_MQ_msg (res,
87                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
88   res->is_allowed = htonl (br->cb (br->cb_cls,
89                                    &bm->peer));
90   res->peer = bm->peer;
91   GNUNET_MQ_send (br->mq,
92                   env);
93 }
94
95
96 /**
97  * Generic error handler, called with the appropriate error code and
98  * the same closure specified at the creation of the message queue.
99  * Not every message queue implementation supports an error handler.
100  *
101  * @param cls closure with the `struct GNUNET_TRANSPORT_Blacklist *`
102  * @param error error code
103  */
104 static void
105 mq_error_handler (void *cls,
106                   enum GNUNET_MQ_Error error)
107 {
108   struct GNUNET_TRANSPORT_Blacklist *br = cls;
109
110   reconnect (br);
111 }
112
113
114 /**
115  * Establish blacklist connection to transport service.
116  *
117  * @param br overall handle
118  */
119 static void
120 reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
121 {
122   struct GNUNET_MQ_MessageHandler handlers[] = {
123     GNUNET_MQ_hd_fixed_size (query,
124                              GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY,
125                              struct BlacklistMessage,
126                              br),
127     GNUNET_MQ_handler_end ()
128   };
129   struct GNUNET_MQ_Envelope *env;
130   struct GNUNET_MessageHeader *req;
131
132   if (NULL != br->mq)
133     GNUNET_MQ_destroy (br->mq);
134   br->mq = GNUNET_CLIENT_connect (br->cfg,
135                                   "transport",
136                                   handlers,
137                                   &mq_error_handler,
138                                   br);
139   if (NULL == br->mq)
140     return;
141   env = GNUNET_MQ_msg (req,
142                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
143   GNUNET_MQ_send (br->mq,
144                   env);
145 }
146
147
148 /**
149  * Install a blacklist callback.  The service will be queried for all
150  * existing connections as well as any fresh connections to check if
151  * they are permitted.  If the blacklisting callback is unregistered,
152  * all hosts that were denied in the past will automatically be
153  * whitelisted again.  Cancelling the blacklist handle is also the
154  * only way to re-enable connections from peers that were previously
155  * blacklisted.
156  *
157  * @param cfg configuration to use
158  * @param cb callback to invoke to check if connections are allowed
159  * @param cb_cls closure for @a cb
160  * @return NULL on error, otherwise handle for cancellation
161  */
162 struct GNUNET_TRANSPORT_Blacklist *
163 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
164                             GNUNET_TRANSPORT_BlacklistCallback cb,
165                             void *cb_cls)
166 {
167   struct GNUNET_TRANSPORT_Blacklist *br;
168
169   br = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
170   br->cfg = cfg;
171   br->cb = cb;
172   br->cb_cls = cb_cls;
173   reconnect (br);
174   if (NULL == br->mq)
175   {
176     GNUNET_free (br);
177     return NULL;
178   }
179   return br;
180 }
181
182
183 /**
184  * Abort the blacklist.  Note that this function is the only way for
185  * removing a peer from the blacklist.
186  *
187  * @param br handle of the request that is to be cancelled
188  */
189 void
190 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
191 {
192   GNUNET_MQ_destroy (br->mq);
193   GNUNET_free (br);
194 }
195
196
197 /* end of transport_api_blacklist.c */