aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/gnunet-service-cadet-new_connection.c
blob: 440d64fb6bf6178adbc0412807768989ca2cfa35 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
     This file is part of GNUnet.
     Copyright (C) 2001-2017 GNUnet e.V.

     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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file cadet/gnunet-service-cadet-new_connection.c
 * @brief management of CORE-level end-to-end connections; establishes
 *        end-to-end routes and transmits messages along the route
 * @author Bartlomiej Polot
 * @author Christian Grothoff
 *
 * TODO:
 * - congestion control
 * - GCC_debug()
 * - keepalive messages
 * - performance metrics
 * - back-off reset
 */
#include "platform.h"
#include "gnunet-service-cadet-new_channel.h"
#include "gnunet-service-cadet-new_connection.h"
#include "gnunet-service-cadet-new_paths.h"
#include "gnunet-service-cadet-new_peer.h"
#include "gnunet-service-cadet-new_tunnels.h"
#include "gnunet_cadet_service.h"
#include "cadet_protocol.h"


/**
 * All the states a connection can be in.
 */
enum CadetConnectionState
{
  /**
   * Uninitialized status, we have not yet even gotten the message queue.
   */
  CADET_CONNECTION_NEW,

  /**
   * Connection create message in queue, awaiting transmission by CORE.
   */
  CADET_CONNECTION_SENDING_CREATE,

  /**
   * Connection create message sent, waiting for ACK.
   */
  CADET_CONNECTION_SENT,

  /**
   * Connection confirmed, ready to carry traffic.
   */
  CADET_CONNECTION_READY,

  /**
   * Connection to be destroyed, just waiting to empty queues.
   */
  CADET_CONNECTION_DESTROYED,

  /**
   * Connection to be destroyed because of a distant peer, same as DESTROYED.
   */
  CADET_CONNECTION_BROKEN
};


/**
 * Low-level connection to a destination.
 */
struct CadetConnection
{

  /**
   * ID of the connection.
   */
  struct GNUNET_CADET_ConnectionTunnelIdentifier cid;

  /**
   * To which peer does this connection go?
   */
  struct CadetPeer *destination;

  /**
   * Which tunnel is using this connection?
   */
  struct CadetTConnection *ct;

  /**
   * Path we are using to our destination.
   */
  struct CadetPeerPath *path;

  /**
   * Pending message, NULL if we are ready to transmit.
   */
  struct GNUNET_MQ_Envelope *env;

  /**
   * Message queue to the first hop, or NULL if we have no connection yet.
   */
  struct GNUNET_MQ_Handle *mq;

  /**
   * Handle for calling #GCP_request_mq_cancel() once we are finished.
   */
  struct GCP_MessageQueueManager *mq_man;

  /**
   * Task for connection maintenance.
   */
  struct GNUNET_SCHEDULER_Task *task;

  /**
   * Function to call once we are ready to transmit.
   */
  GNUNET_SCHEDULER_TaskCallback ready_cb;

  /**
   * Closure for @e ready_cb.
   */
  void *ready_cb_cls;

  /**
   * How long do we wait before we try again with a CREATE message?
   */
  struct GNUNET_TIME_Relative retry_delay;

  /**
   * State of the connection.
   */
  enum CadetConnectionState state;

  /**
   * Offset of our @e destination in @e path.
   */
  unsigned int off;

};


/**
 * Is the given connection currently ready for transmission?
 *
 * @param cc connection to transmit on
 * @return #GNUNET_YES if we could transmit
 */
int
GCC_is_ready (struct CadetConnection *cc)
{
  return ( (NULL != cc->mq) &&
           (CADET_CONNECTION_READY == cc->state) &&
           (NULL == cc->env) ) ? GNUNET_YES : GNUNET_NO;
}


/**
 * Destroy a connection.
 *
 * @param cc connection to destroy
 */
void
GCC_destroy (struct CadetConnection *cc)
{
  if (NULL != cc->env)
  {
    if (NULL != cc->mq)
      GNUNET_MQ_send_cancel (cc->env);
    else
      GNUNET_MQ_discard (cc->env);
    cc->env = NULL;
  }
  if ( (NULL != cc->mq) &&
       (CADET_CONNECTION_SENDING_CREATE != cc->state) )
  {
    /* Need to notify next hop that we are down. */
    struct GNUNET_MQ_Envelope *env;
    struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;

    env = GNUNET_MQ_msg (destroy_msg,
                         GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
    destroy_msg->cid = cc->cid;
    GNUNET_MQ_send (cc->mq,
                    env);
  }
  cc->mq = NULL;
  GCP_request_mq_cancel (cc->mq_man);
  cc->mq_man = NULL;
  GCPP_del_connection (cc->path,
                       cc->off,
                       cc);
  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multishortmap_remove (connections,
                                                        &GCC_get_id (cc)->connection_of_tunnel,
                                                        cc));
  GNUNET_free (cc);
}


/**
 * Return the tunnel associated with this connection.
 *
 * @param cc connection to query
 * @return corresponding entry in the tunnel's connection list
 */
struct CadetTConnection *
GCC_get_ct (struct CadetConnection *cc)
{
  return cc->ct;
}


/**
 * A connection ACK was received for this connection, implying
 * that the end-to-end connection is up.  Process it.
 *
 * @param cc the connection that got the ACK.
 */
void
GCC_handle_connection_ack (struct CadetConnection *cc)
{
  GNUNET_SCHEDULER_cancel (cc->task);
#if FIXME
  cc->task = GNUNET_SCHEDULER_add_delayed (cc->keepalive_period,
                                           &send_keepalive,
                                           cc);
#endif
  cc->state = CADET_CONNECTION_READY;
  cc->ready_cb (cc->ready_cb_cls);
}


/**
 * Handle KX message.
 *
 * @param cc connection that received encrypted message
 * @param msg the key exchange message
 */
void
GCC_handle_kx (struct CadetConnection *cc,
               const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
{
  GCT_handle_kx (cc->ct,
                 msg);
}


/**
 * Handle encrypted message.
 *
 * @param cc connection that received encrypted message
 * @param msg the encrypted message to decrypt
 */
void
GCC_handle_encrypted (struct CadetConnection *cc,
                      const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
{
  GCT_handle_encrypted (cc->ct,
                        msg);
}


/**
 * Send a CREATE message to the first hop.
 *
 * @param cls the `struct CadetConnection` to initiate
 */
static void
send_create (void *cls);


/**
 * We finished transmission of the create message, now wait for
 * ACK or retransmit.
 *
 * @param cls the `struct CadetConnection` that sent the create message
 */
static void
transmit_create_done_cb (void *cls)
{
  struct CadetConnection *cc = cls;

  cc->state = CADET_CONNECTION_SENT;
  cc->env = NULL;
  /* FIXME: at some point, we need to reset the delay back to 0! */
  cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
  cc->task = GNUNET_SCHEDULER_add_delayed (cc->retry_delay,
                                           &send_create,
                                           cc);
}


/**
 * Send a CREATE message to the first hop.
 *
 * @param cls the `struct CadetConnection` to initiate
 */
static void
send_create (void *cls)
{
  struct CadetConnection *cc = cls;
  struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
  struct GNUNET_PeerIdentity *pids;
  struct GNUNET_MQ_Envelope *env;
  unsigned int path_length;

  cc->task = NULL;
  GNUNET_assert (NULL != cc->mq);
  path_length = GCPP_get_length (cc->path);
  env = GNUNET_MQ_msg_extra (create_msg,
                             path_length * sizeof (struct GNUNET_PeerIdentity),
                             GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
  create_msg->cid = cc->cid;
  pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
  for (unsigned int i=0;i<path_length;i++)
    pids[i] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
                                                    i));
  cc->env = env;
  GNUNET_MQ_notify_sent (env,
                         &transmit_create_done_cb,
                         cc);
  GNUNET_MQ_send (cc->mq,
                  env);
}


/**
 * There has been a change in the message queue existence for our
 * peer at the first hop.  Adjust accordingly.
 *
 * @param cls the `struct CadetConnection`
 * @param mq NULL if the CORE connection was lost, non-NULL if
 *           it became available
 */
static void
manage_first_hop_mq (void *cls,
                     struct GNUNET_MQ_Handle *mq)
{
  struct CadetConnection *cc = cls;

  if (NULL == mq)
  {
    /* Connection is down, for now... */
    cc->mq = NULL;
    if (NULL != cc->task)
    {
      GNUNET_SCHEDULER_cancel (cc->task);
      cc->task = NULL;
    }
    return;
  }

  cc->mq = mq;
  cc->state = CADET_CONNECTION_SENDING_CREATE;

  /* Now repeat sending connection creation messages
     down the path, until we get an ACK! */
  cc->task = GNUNET_SCHEDULER_add_now (&send_create,
                                       cc);
}


/**
 * Create a connection to @a destination via @a path and
 * notify @a cb whenever we are ready for more data.
 *
 * @param destination where to go
 * @param path which path to take (may not be the full path)
 * @param ct tunnel that uses the connection
 * @param ready_cb function to call when ready to transmit
 * @param ready_cb_cls closure for @a cb
 * @return handle to the connection
 */
struct CadetConnection *
GCC_create (struct CadetPeer *destination,
            struct CadetPeerPath *path,
            struct CadetTConnection *ct,
            GNUNET_SCHEDULER_TaskCallback ready_cb,
            void *ready_cb_cls)
{
  struct CadetConnection *cc;
  struct CadetPeer *first_hop;
  unsigned int off;

  off = GCPP_find_peer (path,
                        destination);
  GNUNET_assert (UINT_MAX > off);
  cc = GNUNET_new (struct CadetConnection);
  cc->ct = ct;
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                              &cc->cid,
                              sizeof (cc->cid));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multishortmap_put (connections,
                                                     &GCC_get_id (cc)->connection_of_tunnel,
                                                     cc,
                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  cc->ready_cb = ready_cb;
  cc->ready_cb_cls = ready_cb_cls;
  cc->path = path;
  cc->off = off;
  GCPP_add_connection (path,
                       off,
                       cc);
  for (unsigned int i=0;i<off;i++)
    GCP_add_connection (GCPP_get_peer_at_offset (path,
                                                 off),
                        cc);

  first_hop = GCPP_get_peer_at_offset (path,
                                       0);
  cc->mq_man = GCP_request_mq (first_hop,
                               &manage_first_hop_mq,
                               cc);
  return cc;
}


/**
 * We finished transmission of a message, if we are still ready, tell
 * the tunnel!
 *
 * @param cls our `struct CadetConnection`
 */
static void
transmit_done_cb (void *cls)
{
  struct CadetConnection *cc = cls;

  cc->env = NULL;
  if ( (NULL != cc->mq) &&
       (CADET_CONNECTION_READY == cc->state) )
    cc->ready_cb (cc->ready_cb_cls);
}


/**
 * Transmit message @a msg via connection @a cc.  Must only be called
 * (once) after the connection has signalled that it is ready via the
 * `ready_cb`.  Clients can also use #GCC_is_ready() to check if the
 * connection is right now ready for transmission.
 *
 * @param cc connection identification
 * @param env envelope with message to transmit
 */
void
GCC_transmit (struct CadetConnection *cc,
              struct GNUNET_MQ_Envelope *env)
{
  GNUNET_assert (NULL == cc->env);
  cc->env = env;
  GNUNET_MQ_notify_sent (env,
                         &transmit_done_cb,
                         cc);
  if ( (NULL != cc->mq) &&
       (CADET_CONNECTION_READY == cc->state) )
    GNUNET_MQ_send (cc->mq,
                    env);
}


/**
 * Obtain the path used by this connection.
 *
 * @param cc connection
 * @return path to @a cc
 */
struct CadetPeerPath *
GCC_get_path (struct CadetConnection *cc)
{
  return cc->path;
}


/**
 * Obtain unique ID for the connection.
 *
 * @param cc connection.
 * @return unique number of the connection
 */
const struct GNUNET_CADET_ConnectionTunnelIdentifier *
GCC_get_id (struct CadetConnection *cc)
{
  return &cc->cid;
}


/**
 * Log connection info.
 *
 * @param cc connection
 * @param level Debug level to use.
 */
void
GCC_debug (struct CadetConnection *cc,
           enum GNUNET_ErrorType level)
{
  GNUNET_break (0); // FIXME: implement...
}

/* end of gnunet-service-cadet-new_connection.c */