aboutsummaryrefslogtreecommitdiff
path: root/src/core/gnunet-service-core_neighbours.c
blob: c1c62cf1aede06edd4dbc62ad0e5bd249fa46b04 (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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
     This file is part of GNUnet.
     Copyright (C) 2009, 2010, 2011 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 core/gnunet-service-core_neighbours.c
 * @brief code for managing low-level 'plaintext' connections with transport (key exchange may or may not be done yet)
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_statistics_service.h"
#include "gnunet_transport_service.h"
#include "gnunet-service-core.h"
#include "gnunet-service-core_neighbours.h"
#include "gnunet-service-core_kx.h"
#include "gnunet-service-core_sessions.h"
#include "gnunet_constants.h"


/**
 * Message ready for transmission via transport service.  This struct
 * is followed by the actual content of the message.
 */
struct NeighbourMessageEntry
{

  /**
   * We keep messages in a doubly linked list.
   */
  struct NeighbourMessageEntry *next;

  /**
   * We keep messages in a doubly linked list.
   */
  struct NeighbourMessageEntry *prev;

  /**
   * By when are we supposed to transmit this message?
   */
  struct GNUNET_TIME_Absolute deadline;

  /**
   * What time did we submit the request?
   */
  struct GNUNET_TIME_Absolute submission_time;

  /**
   * How long is the message? (number of bytes following the `struct
   * MessageEntry`, but not including the size of `struct
   * MessageEntry` itself!)
   */
  size_t size;

};


/**
 * Data kept per transport-connected peer.
 */
struct Neighbour
{

  /**
   * Head of the batched message queue (already ordered, transmit
   * starting with the head).
   */
  struct NeighbourMessageEntry *message_head;

  /**
   * Tail of the batched message queue (already ordered, append new
   * messages to tail).
   */
  struct NeighbourMessageEntry *message_tail;

  /**
   * Handle for pending requests for transmission to this peer
   * with the transport service.  NULL if no request is pending.
   */
  struct GNUNET_TRANSPORT_TransmitHandle *th;

  /**
   * Information about the key exchange with the other peer.
   */
  struct GSC_KeyExchangeInfo *kxinfo;

  /**
   * Identity of the other peer.
   */
  struct GNUNET_PeerIdentity peer;

  /**
   * ID of task used for re-trying plaintext scheduling.
   */
  struct GNUNET_SCHEDULER_Task *retry_plaintext_task;

  /**
   * How many messages are in the queue for this neighbour?
   */
  unsigned int queue_size;

  /**
   * #GNUNET_YES if this peer currently has excess bandwidth.
   */
  int has_excess_bandwidth;

};


/**
 * Map of peer identities to `struct Neighbour`.
 */
static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;

/**
 * Transport service.
 */
static struct GNUNET_TRANSPORT_Handle *transport;


/**
 * Find the entry for the given neighbour.
 *
 * @param peer identity of the neighbour
 * @return NULL if we are not connected, otherwise the
 *         neighbour's entry.
 */
static struct Neighbour *
find_neighbour (const struct GNUNET_PeerIdentity *peer)
{
  if (NULL == neighbours)
    return NULL;
  return GNUNET_CONTAINER_multipeermap_get (neighbours,
                                            peer);
}


/**
 * Free the given entry for the neighbour.
 *
 * @param n neighbour to free
 */
static void
free_neighbour (struct Neighbour *n)
{
  struct NeighbourMessageEntry *m;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Destroying neighbour entry for peer `%s'\n",
              GNUNET_i2s (&n->peer));
  while (NULL != (m = n->message_head))
  {
    GNUNET_CONTAINER_DLL_remove (n->message_head,
                                 n->message_tail,
                                 m);
    n->queue_size--;
    GNUNET_free (m);
  }
  GNUNET_assert (0 == n->queue_size);
  if (NULL != n->th)
  {
    GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
    n->th = NULL;
  }
  GNUNET_STATISTICS_update (GSC_stats,
                            gettext_noop
                            ("# sessions terminated by transport disconnect"),
                            1, GNUNET_NO);
  if (NULL != n->kxinfo)
  {
    GSC_KX_stop (n->kxinfo);
    n->kxinfo = NULL;
  }
  if (NULL != n->retry_plaintext_task)
  {
    GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
    n->retry_plaintext_task = NULL;
  }
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multipeermap_remove (neighbours,
                                                       &n->peer, n));
  GNUNET_STATISTICS_set (GSC_stats,
                         gettext_noop ("# neighbour entries allocated"),
                         GNUNET_CONTAINER_multipeermap_size (neighbours),
                         GNUNET_NO);
  GNUNET_free (n);
}


/**
 * Check if we have encrypted messages for the specified neighbour
 * pending, and if so, check with the transport about sending them
 * out.
 *
 * @param n neighbour to check.
 */
static void
process_queue (struct Neighbour *n);


/**
 * Function called when the transport service is ready to receive a
 * message for the respective peer
 *
 * @param cls neighbour to use message from
 * @param size number of bytes we can transmit
 * @param buf where to copy the message
 * @return number of bytes transmitted
 */
static size_t
transmit_ready (void *cls,
                size_t size,
                void *buf)
{
  struct Neighbour *n = cls;
  struct NeighbourMessageEntry *m;
  size_t ret;
  char *cbuf;
  struct GNUNET_TIME_Relative delay;
  struct GNUNET_TIME_Relative overdue;

  n->th = NULL;
  m = n->message_head;
  if (NULL == m)
  {
    GNUNET_break (0);
    return 0;
  }
  GNUNET_CONTAINER_DLL_remove (n->message_head,
                               n->message_tail,
                               m);
  n->queue_size--;
  if (NULL == buf)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Transmission of message of type %u and size %u failed\n",
                (unsigned int)
                ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
                (unsigned int) m->size);
    GNUNET_free (m);
    process_queue (n);
    return 0;
  }
  delay = GNUNET_TIME_absolute_get_duration (m->submission_time);
  overdue = GNUNET_TIME_absolute_get_duration (m->deadline);
  cbuf = buf;
  GNUNET_assert (size >= m->size);
  memcpy (cbuf,
          &m[1],
          m->size);
  ret = m->size;
  if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Copied overdue message of type %u and size %u into transport buffer for `%s' with delay of %s\n",
                (unsigned int)
                ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
                (unsigned int) ret,
                GNUNET_i2s (&n->peer),
                GNUNET_STRINGS_relative_time_to_string (delay,
                                                        GNUNET_YES));
  else
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Copied message of type %u and size %u into transport buffer for `%s' with delay of %s\n",
                (unsigned int)
                ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
                (unsigned int) ret,
                GNUNET_i2s (&n->peer),
                GNUNET_STRINGS_relative_time_to_string (delay,
                                                        GNUNET_YES));
  GNUNET_free (m);
  n->has_excess_bandwidth = GNUNET_NO;
  process_queue (n);
  GNUNET_STATISTICS_update (GSC_stats,
                            gettext_noop
                            ("# encrypted bytes given to transport"), ret,
                            GNUNET_NO);
  return ret;
}


/**
 * Check if we have messages for the specified neighbour pending, and
 * if so, check with the transport about sending them out.
 *
 * @param n neighbour to check.
 */
static void
process_queue (struct Neighbour *n)
{
  struct NeighbourMessageEntry *m;

  if (NULL != n->th)
    return;                     /* request already pending */
  m = n->message_head;
  if (NULL == m)
  {
    /* notify sessions that the queue is empty and more messages
     * could thus be queued now */
    GSC_SESSIONS_solicit (&n->peer);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Asking transport for transmission of %u bytes to `%s' in next %s\n",
              (unsigned int) m->size,
              GNUNET_i2s (&n->peer),
              GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline),
                                                      GNUNET_NO));
  m->submission_time = GNUNET_TIME_absolute_get ();
  n->th
    = GNUNET_TRANSPORT_notify_transmit_ready (transport,
                                              &n->peer,
                                              m->size,
                                              GNUNET_TIME_absolute_get_remaining (m->deadline),
                                              &transmit_ready,
                                              n);
  if (NULL != n->th)
    return;
  /* message request too large or duplicate request */
  GNUNET_break (0);
  /* discard encrypted message */
  GNUNET_CONTAINER_DLL_remove (n->message_head,
                               n->message_tail,
                               m);
  n->queue_size--;
  GNUNET_free (m);
  process_queue (n);
}


/**
 * Function called by transport to notify us that
 * a peer connected to us (on the network level).
 *
 * @param cls closure
 * @param peer the peer that connected
 */
static void
handle_transport_notify_connect (void *cls,
                                 const struct GNUNET_PeerIdentity *peer)
{
  struct Neighbour *n;

  if (0 == memcmp (peer,
                   &GSC_my_identity,
                   sizeof (struct GNUNET_PeerIdentity)))
  {
    GNUNET_break (0);
    return;
  }
  n = find_neighbour (peer);
  if (NULL != n)
  {
    /* duplicate connect notification!? */
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s exists already\n",
                GNUNET_i2s (peer));
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received connection from `%s'.\n",
              GNUNET_i2s (peer));
  n = GNUNET_new (struct Neighbour);
  n->peer = *peer;
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multipeermap_put (neighbours,
                                                    &n->peer,
                                                    n,
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  GNUNET_STATISTICS_set (GSC_stats,
                         gettext_noop ("# neighbour entries allocated"),
                         GNUNET_CONTAINER_multipeermap_size (neighbours),
                         GNUNET_NO);
  n->kxinfo = GSC_KX_start (peer);
}


/**
 * Function called by transport telling us that a peer
 * disconnected.
 *
 * @param cls closure
 * @param peer the peer that disconnected
 */
static void
handle_transport_notify_disconnect (void *cls,
                                    const struct GNUNET_PeerIdentity *peer)
{
  struct Neighbour *n;

  if (0 == memcmp (peer,
                   &GSC_my_identity,
                   sizeof (struct GNUNET_PeerIdentity)))
  {
    GNUNET_break (0);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Peer `%s' disconnected from us; received notification from transport.\n",
              GNUNET_i2s (peer));
  n = find_neighbour (peer);
  if (NULL == n)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s not found\n",
                GNUNET_i2s (peer));
    return;
  }
  free_neighbour (n);
}


/**
 * Function called by the transport for each received message.
 *
 * @param cls closure
 * @param peer (claimed) identity of the other peer
 * @param message the message
 */
static void
handle_transport_receive (void *cls,
                          const struct GNUNET_PeerIdentity *peer,
                          const struct GNUNET_MessageHeader *message)
{
  struct Neighbour *n;
  uint16_t type;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received message of type %u from `%s', demultiplexing.\n",
              (unsigned int) ntohs (message->type),
              GNUNET_i2s (peer));
  if (0 == memcmp (peer,
                   &GSC_my_identity,
                   sizeof (struct GNUNET_PeerIdentity)))
  {
    GNUNET_break (0);
    return;
  }
  n = find_neighbour (peer);
  if (NULL == n)
  {
    /* received message from peer that is not connected!? */
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s not found\n",
                GNUNET_i2s (peer));
    return;
  }
  type = ntohs (message->type);
  switch (type)
  {
  case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
    GSC_KX_handle_ephemeral_key (n->kxinfo, message);
    break;
  case GNUNET_MESSAGE_TYPE_CORE_PING:
    GSC_KX_handle_ping (n->kxinfo, message);
    break;
  case GNUNET_MESSAGE_TYPE_CORE_PONG:
    GSC_KX_handle_pong (n->kxinfo, message);
    break;
  case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
    GSC_KX_handle_encrypted_message (n->kxinfo, message);
    break;
  case GNUNET_MESSAGE_TYPE_DUMMY:
    /*  Dummy messages for testing / benchmarking, just discard */
    break;
  default:
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
                (unsigned int) type,
                (unsigned int) ntohs (message->size),
                GNUNET_i2s (peer));
    return;
  }
}


/**
 * Transmit the given message to the given target.
 *
 * @param target peer that should receive the message (must be connected)
 * @param msg message to transmit
 * @param timeout by when should the transmission be done?
 */
void
GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
                         const struct GNUNET_MessageHeader *msg,
                         struct GNUNET_TIME_Relative timeout)
{
  struct NeighbourMessageEntry *me;
  struct Neighbour *n;
  size_t msize;

  n = find_neighbour (target);
  if (NULL == n)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s not found\n",
                GNUNET_i2s (target));
    return;
  }
  msize = ntohs (msg->size);
  me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
  me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
  me->size = msize;
  memcpy (&me[1],
          msg,
          msize);
  GNUNET_CONTAINER_DLL_insert_tail (n->message_head,
                                    n->message_tail,
                                    me);
  n->queue_size++;
  process_queue (n);
}


/**
 * One of our neighbours has excess bandwidth, remember this.
 *
 * @param cls NULL
 * @param pid identity of the peer with excess bandwidth
 */
static void
handle_transport_notify_excess_bw (void *cls,
                                   const struct GNUNET_PeerIdentity *pid)
{
  struct Neighbour *n;

  n = find_neighbour (pid);
  if (NULL == n)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s not found\n",
                GNUNET_i2s (pid));
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Peer %s has excess bandwidth available\n",
              GNUNET_i2s (pid));
  n->has_excess_bandwidth = GNUNET_YES;
  GSC_SESSIONS_solicit (pid);
}


/**
 * Check how many messages are queued for the given neighbour.
 *
 * @param target neighbour to check
 * @return number of items in the message queue
 */
unsigned int
GSC_NEIGHBOURS_get_queue_size (const struct GNUNET_PeerIdentity *target)
{
  struct Neighbour *n;

  n = find_neighbour (target);
  if (NULL == n)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s not found\n",
                GNUNET_i2s (target));
    return UINT_MAX;
  }
  return n->queue_size;
}


/**
 * Check if the given neighbour has excess bandwidth available.
 *
 * @param target neighbour to check
 * @return #GNUNET_YES if excess bandwidth is available, #GNUNET_NO if not
 */
int
GSC_NEIGHBOURS_check_excess_bandwidth (const struct GNUNET_PeerIdentity *target)
{
  struct Neighbour *n;

  n = find_neighbour (target);
  if (NULL == n)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Peer %s not found\n",
                GNUNET_i2s (target));
    return GNUNET_SYSERR;
  }
  return n->has_excess_bandwidth;
}


/**
 * Initialize neighbours subsystem.
 */
int
GSC_NEIGHBOURS_init ()
{
  neighbours = GNUNET_CONTAINER_multipeermap_create (128,
                                                     GNUNET_YES);
  transport =
      GNUNET_TRANSPORT_connect2 (GSC_cfg,
                                 &GSC_my_identity,
                                 NULL,
                                 &handle_transport_receive,
                                 &handle_transport_notify_connect,
                                 &handle_transport_notify_disconnect,
                                 &handle_transport_notify_excess_bw);
  if (NULL == transport)
  {
    GNUNET_CONTAINER_multipeermap_destroy (neighbours);
    neighbours = NULL;
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}


/**
 * Wrapper around #free_neighbour().
 *
 * @param cls unused
 * @param key peer identity
 * @param value the `struct Neighbour` to free
 * @return #GNUNET_OK (continue to iterate)
 */
static int
free_neighbour_helper (void *cls,
		       const struct GNUNET_PeerIdentity * key,
		       void *value)
{
  struct Neighbour *n = value;

  /* transport should have 'disconnected' all neighbours... */
  GNUNET_break (0);
  free_neighbour (n);
  return GNUNET_OK;
}


/**
 * Shutdown neighbours subsystem.
 */
void
GSC_NEIGHBOURS_done ()
{
  if (NULL != transport)
  {
    GNUNET_TRANSPORT_disconnect (transport);
    transport = NULL;
  }
  if (NULL != neighbours)
  {
    GNUNET_CONTAINER_multipeermap_iterate (neighbours,
                                           &free_neighbour_helper,
					   NULL);
    GNUNET_CONTAINER_multipeermap_destroy (neighbours);
    neighbours = NULL;
  }
}

/* end of gnunet-service-core_neighbours.c */