aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_blacklist.c
blob: be52623bb6e42501e0b56fbaf799250e8558786d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
     This file is part of GNUnet.
     (C) 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file transport/transport_api_blacklist.c
 * @brief library to access the blacklisting functions of the transport service
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_client_lib.h"
#include "gnunet_arm_service.h"
#include "gnunet_hello_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_server_lib.h"
#include "gnunet_time_lib.h"
#include "gnunet_transport_service.h"
#include "transport.h"

/**
 * Handle for blacklisting requests.
 */
struct GNUNET_TRANSPORT_Blacklist
{

  /**
   * Connection to transport service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Configuration to use.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Pending handle for the current request.
   */
  struct GNUNET_CLIENT_TransmitHandle *th;

  /**
   * Function to call for determining if a peer is allowed
   * to communicate with us.
   */
  GNUNET_TRANSPORT_BlacklistCallback cb;

  /**
   * Closure for 'cb'.
   */
  void *cb_cls;

  /**
   * Peer currently under consideration.
   */
  struct GNUNET_PeerIdentity peer;

};


/**
 * Establish blacklist connection to transport service.
 *
 * @param br overall handle
 */
static void
reconnect (struct GNUNET_TRANSPORT_Blacklist *br);


/**
 * Send our reply to a blacklisting request.
 *
 * @param br our overall context
 */
static void
reply (struct GNUNET_TRANSPORT_Blacklist *br);


/**
 * Handle blacklist queries.
 *
 * @param cls our overall handle
 * @param msg query
 */
static void
query_handler (void *cls, const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_TRANSPORT_Blacklist *br = cls;
  const struct BlacklistMessage *bm;

  GNUNET_assert (br != NULL);
  if ((NULL == msg) ||
      (ntohs (msg->size) != sizeof (struct BlacklistMessage)) ||
      (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY))
  {
    reconnect (br);
    return;
  }
  bm = (const struct BlacklistMessage *) msg;
  GNUNET_break (0 == ntohl (bm->is_allowed));
  br->peer = bm->peer;
  reply (br);
}


/**
 * Receive blacklist queries from transport service.
 *
 * @param br overall handle
 */
static void
receive (struct GNUNET_TRANSPORT_Blacklist *br)
{
  GNUNET_CLIENT_receive (br->client, &query_handler, br,
                         GNUNET_TIME_UNIT_FOREVER_REL);
}


/**
 * Transmit the blacklist initialization request to the service.
 *
 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
 * @param size number of bytes available in buf
 * @param buf where the callee should write the message
 * @return number of bytes written to buf
 */
static size_t
transmit_blacklist_init (void *cls, size_t size, void *buf)
{
  struct GNUNET_TRANSPORT_Blacklist *br = cls;
  struct GNUNET_MessageHeader req;

  br->th = NULL;
  if (buf == NULL)
  {
    reconnect (br);
    return 0;
  }
  req.size = htons (sizeof (struct GNUNET_MessageHeader));
  req.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
  memcpy (buf, &req, sizeof (req));
  receive (br);
  return sizeof (req);
}


/**
 * Establish blacklist connection to transport service.
 *
 * @param br overall handle
 */
static void
reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
{
  if (br->client != NULL)
    GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
  br->client = GNUNET_CLIENT_connect ("transport", br->cfg);
  GNUNET_assert (br->client != NULL);
  br->th =
      GNUNET_CLIENT_notify_transmit_ready (br->client,
                                           sizeof (struct GNUNET_MessageHeader),
                                           GNUNET_TIME_UNIT_FOREVER_REL,
                                           GNUNET_YES, &transmit_blacklist_init,
                                           br);
}


/**
 * Transmit the blacklist response to the service.
 *
 * @param cls closure (struct GNUNET_TRANSPORT_Blacklist*)
 * @param size number of bytes available in buf
 * @param buf where the callee should write the message
 * @return number of bytes written to buf
 */
static size_t
transmit_blacklist_reply (void *cls, size_t size, void *buf)
{
  struct GNUNET_TRANSPORT_Blacklist *br = cls;
  struct BlacklistMessage req;

  br->th = NULL;
  if (buf == NULL)
  {
    reconnect (br);
    return 0;
  }
  req.header.size = htons (sizeof (req));
  req.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
  req.is_allowed = htonl (br->cb (br->cb_cls, &br->peer));
  req.peer = br->peer;
  memcpy (buf, &req, sizeof (req));
  br->th = NULL;
  receive (br);
  return sizeof (req);
}


/**
 * Send our reply to a blacklisting request.
 *
 * @param br our overall context
 */
static void
reply (struct GNUNET_TRANSPORT_Blacklist *br)
{
  GNUNET_assert (br->th == NULL);
  br->th =
      GNUNET_CLIENT_notify_transmit_ready (br->client,
                                           sizeof (struct BlacklistMessage),
                                           GNUNET_TIME_UNIT_FOREVER_REL,
                                           GNUNET_NO, &transmit_blacklist_reply,
                                           br);
  if (br->th == NULL)
  {
    reconnect (br);
    return;
  }
}


/**
 * Install a blacklist callback.  The service will be queried for all
 * existing connections as well as any fresh connections to check if
 * they are permitted.  If the blacklisting callback is unregistered,
 * all hosts that were denied in the past will automatically be
 * whitelisted again.  Cancelling the blacklist handle is also the
 * only way to re-enable connections from peers that were previously
 * blacklisted.
 *
 * @param cfg configuration to use
 * @param cb callback to invoke to check if connections are allowed
 * @param cb_cls closure for cb
 * @return NULL on error, otherwise handle for cancellation
 */
struct GNUNET_TRANSPORT_Blacklist *
GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
                            GNUNET_TRANSPORT_BlacklistCallback cb, void *cb_cls)
{
  struct GNUNET_CLIENT_Connection *client;
  struct GNUNET_TRANSPORT_Blacklist *ret;

  client = GNUNET_CLIENT_connect ("transport", cfg);
  if (NULL == client)
    return NULL;
  ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Blacklist));
  ret->client = client;
  ret->cfg = cfg;
  ret->cb = cb;
  ret->cb_cls = cb_cls;
  GNUNET_assert (ret->th == NULL);
  ret->th =
      GNUNET_CLIENT_notify_transmit_ready (client,
                                           sizeof (struct GNUNET_MessageHeader),
                                           GNUNET_TIME_UNIT_FOREVER_REL,
                                           GNUNET_YES, &transmit_blacklist_init,
                                           ret);
  return ret;
}


/**
 * Abort the blacklist.  Note that this function is the only way for
 * removing a peer from the blacklist.
 *
 * @param br handle of the request that is to be cancelled
 */
void
GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
{
  if (br->th != NULL)
  {
    GNUNET_CLIENT_notify_transmit_ready_cancel (br->th);
    br->th = NULL;
  }
  GNUNET_CLIENT_disconnect (br->client, GNUNET_NO);
  GNUNET_free (br);
}


/* end of transport_api_blacklist.c */