aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api2_application.c
blob: 00f5f62eb39baa4af15faf8bd0a781fe5b44f285 (plain) (blame)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
     This file is part of GNUnet.
     Copyright (C) 2010--2019 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     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
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @file transport/transport_api2_application.c
 * @brief enable clients to ask TRANSPORT about establishing connections to peers
 * @author Christian Grothoff
 * @author Matthias Wachs
 */
#include "platform.h"
#include "gnunet_transport_application_service.h"
#include "gnunet_transport_core_service.h"
#include "transport.h"


#define LOG(kind, ...) \
  GNUNET_log_from (kind, "transport-application-api", __VA_ARGS__)


/**
 * Handle for TRANSPORT address suggestion requests.
 */
struct GNUNET_TRANSPORT_ApplicationSuggestHandle
{
  /**
   * ID of the peer for which address suggestion was requested.
   */
  struct GNUNET_PeerIdentity id;

  /**
   * Connecitivity handle this suggestion handle belongs to.
   */
  struct GNUNET_TRANSPORT_ApplicationHandle *ch;

  /**
   * What preference is being expressed?
   */
  enum GNUNET_MQ_PriorityPreferences pk;

  /**
   * How much bandwidth does the client expect?
   */
  struct GNUNET_BANDWIDTH_Value32NBO bw;
};


/**
 * Handle to the TRANSPORT subsystem for application management.
 */
struct GNUNET_TRANSPORT_ApplicationHandle
{
  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Map with the identities of all the peers for which we would
   * like to have address suggestions.  The key is the PID, the
   * value is currently the `struct GNUNET_TRANSPORT_ApplicationSuggestHandle`
   */
  struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;

  /**
   * Message queue for sending requests to the TRANSPORT service.
   */
  struct GNUNET_MQ_Handle *mq;

  /**
   * Task to trigger reconnect.
   */
  struct GNUNET_SCHEDULER_Task *task;

  /**
   * Reconnect backoff delay.
   */
  struct GNUNET_TIME_Relative backoff;
};


/**
 * Re-establish the connection to the TRANSPORT service.
 *
 * @param ch handle to use to re-connect.
 */
static void
reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch);


/**
 * Re-establish the connection to the TRANSPORT service.
 *
 * @param cls handle to use to re-connect.
 */
static void
reconnect_task (void *cls)
{
  struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;

  ch->task = NULL;
  reconnect (ch);
}


/**
 * Disconnect from TRANSPORT and then reconnect.
 *
 * @param ch our handle
 */
static void
force_reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch)
{
  if (NULL != ch->mq)
  {
    GNUNET_MQ_destroy (ch->mq);
    ch->mq = NULL;
  }
  ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
  ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff, &reconnect_task, ch);
}


/**
 * We encountered an error handling the MQ to the
 * TRANSPORT service.  Reconnect.
 *
 * @param cls the `struct GNUNET_TRANSPORT_ApplicationHandle`
 * @param error details about the error
 */
static void
error_handler (void *cls, enum GNUNET_MQ_Error error)
{
  struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "TRANSPORT connection died (code %d), reconnecting\n",
       (int) error);
  force_reconnect (ch);
}


/**
 * Transmit request for an address suggestion.
 *
 * @param cls the `struct GNUNET_TRANSPORT_ApplicationHandle`
 * @param peer peer to ask for an address suggestion for
 * @param value the `struct GNUNET_TRANSPORT_SuggestHandle`
 * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
 *         failure (message queue no longer exists)
 */
static int
transmit_suggestion (void *cls,
                     const struct GNUNET_PeerIdentity *peer,
                     void *value)
{
  struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
  struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh = value;
  struct GNUNET_MQ_Envelope *ev;
  struct ExpressPreferenceMessage *m;

  if (NULL == ch->mq)
    return GNUNET_SYSERR;
  ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST);
  m->pk = htonl ((uint32_t) sh->pk);
  m->bw = sh->bw;
  m->peer = *peer;
  GNUNET_MQ_send (ch->mq, ev);
  return GNUNET_OK;
}


/**
 * Re-establish the connection to the TRANSPORT service.
 *
 * @param ch handle to use to re-connect.
 */
static void
reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch)
{
  static const struct GNUNET_MQ_MessageHandler handlers[] = { { NULL, 0, 0 } };

  GNUNET_assert (NULL == ch->mq);
  ch->mq =
    GNUNET_CLIENT_connect (ch->cfg, "transport", handlers, &error_handler, ch);
  if (NULL == ch->mq)
  {
    force_reconnect (ch);
    return;
  }
  GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
                                         &transmit_suggestion,
                                         ch);
}


/**
 * Initialize the TRANSPORT application suggestion client handle.
 *
 * @param cfg configuration to use
 * @return transport application handle, NULL on error
 */
struct GNUNET_TRANSPORT_ApplicationHandle *
GNUNET_TRANSPORT_application_init (
  const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_TRANSPORT_ApplicationHandle *ch;

  ch = GNUNET_new (struct GNUNET_TRANSPORT_ApplicationHandle);
  ch->cfg = cfg;
  ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
  reconnect (ch);
  return ch;
}


/**
 * Function called to free all `struct GNUNET_TRANSPORT_ApplicationSuggestHandle`s
 * in the map.
 *
 * @param cls NULL
 * @param key the key
 * @param value the value to free
 * @return #GNUNET_OK (continue to iterate)
 */
static int
free_sug_handle (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
{
  struct GNUNET_TRANSPORT_ApplicationSuggestHandle *cur = value;

  GNUNET_free (cur);
  return GNUNET_OK;
}


/**
 * Client is done with TRANSPORT application management, release resources.
 *
 * @param ch handle to release
 */
void
GNUNET_TRANSPORT_application_done (
  struct GNUNET_TRANSPORT_ApplicationHandle *ch)
{
  if (NULL != ch->mq)
  {
    GNUNET_MQ_destroy (ch->mq);
    ch->mq = NULL;
  }
  if (NULL != ch->task)
  {
    GNUNET_SCHEDULER_cancel (ch->task);
    ch->task = NULL;
  }
  GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
                                         &free_sug_handle,
                                         NULL);
  GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
  GNUNET_free (ch);
}


/**
 * An application would like TRANSPORT to connect to a peer.
 *
 * @param ch handle
 * @param peer identity of the peer we need an address for
 * @param pk what kind of application will the application require (can be
 *         #GNUNET_MQ_PRIO_BACKGROUND, we will still try to connect)
 * @param bw desired bandwidth, can be zero (we will still try to connect)
 * @return suggest handle, NULL if a request is already pending
 */
struct GNUNET_TRANSPORT_ApplicationSuggestHandle *
GNUNET_TRANSPORT_application_suggest (
  struct GNUNET_TRANSPORT_ApplicationHandle *ch,
  const struct GNUNET_PeerIdentity *peer,
  enum GNUNET_MQ_PriorityPreferences pk,
  struct GNUNET_BANDWIDTH_Value32NBO bw)
{
  struct GNUNET_TRANSPORT_ApplicationSuggestHandle *s;

  s = GNUNET_new (struct GNUNET_TRANSPORT_ApplicationSuggestHandle);
  s->ch = ch;
  s->id = *peer;
  s->pk = pk;
  s->bw = bw;
  (void) GNUNET_CONTAINER_multipeermap_put (
    ch->sug_requests,
    &s->id,
    s,
    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Requesting TRANSPORT to suggest address for `%s'\n",
       GNUNET_i2s (peer));
  if (NULL == ch->mq)
    return s;
  GNUNET_assert (GNUNET_OK == transmit_suggestion (ch, &s->id, s));
  return s;
}


/**
 * We no longer care about being connected to a peer.
 *
 * @param sh handle to stop
 */
void
GNUNET_TRANSPORT_application_suggest_cancel (
  struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh)
{
  struct GNUNET_TRANSPORT_ApplicationHandle *ch = sh->ch;
  struct GNUNET_MQ_Envelope *ev;
  struct ExpressPreferenceMessage *m;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Telling TRANSPORT we no longer care for an address for `%s'\n",
       GNUNET_i2s (&sh->id));
  GNUNET_assert (
    GNUNET_OK ==
    GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests, &sh->id, sh));
  if (NULL == ch->mq)
  {
    GNUNET_free (sh);
    return;
  }
  ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL);
  m->pk = htonl ((uint32_t) sh->pk);
  m->bw = sh->bw;
  m->peer = sh->id;
  GNUNET_MQ_send (ch->mq, ev);
  GNUNET_free (sh);
}


/**
 * An application (or a communicator) has received a HELLO (or other address
 * data of another peer) and wants TRANSPORT to validate that the address is
 * correct.  The result is NOT returned, in fact TRANSPORT may do nothing
 * (i.e. if it has too many active validations or recently tried this one
 * already).  If the @a addr validates, TRANSPORT will persist the address
 * with PEERSTORE.
 *
 * @param ch handle
 * @param peer identity of the peer we have an address for
 * @param nt network type of @a addr (as claimed by the other peer);
 *        used by TRANSPORT to avoid trying @a addr's that really cannot work
 *        due to network type mismatches
 * @param addr address to validate
 */
void
GNUNET_TRANSPORT_application_validate (
  struct GNUNET_TRANSPORT_ApplicationHandle *ch,
  const struct GNUNET_PeerIdentity *peer,
  enum GNUNET_NetworkType nt,
  const char *addr)
{
  struct GNUNET_MQ_Envelope *ev;
  struct RequestHelloValidationMessage *m;
  size_t alen;

  if (NULL == ch->mq)
  {
    GNUNET_log (
      GNUNET_ERROR_TYPE_WARNING,
      "Address validation for %s:%s skipped as transport is not connected\n",
      GNUNET_i2s (peer),
      addr);
    return;
  }
  alen = strlen (addr) + 1;
  ev =
    GNUNET_MQ_msg_extra (m,
                         alen,
                         GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION);
  m->peer = *peer;
  m->nt = htonl ((uint32_t) nt);
  memcpy (&m[1], addr, alen);
  GNUNET_MQ_send (ch->mq, ev);
}


/* end of transport_api2_application.c */