aboutsummaryrefslogtreecommitdiff
path: root/src/dhtu/plugin_dhtu_ip.c
blob: 19fe191924d06d9226edc4524e7d72f1e64bf2a6 (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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
     This file is part of GNUnet
     Copyright (C) 2021 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
 */

/**
 * @author Christian Grothoff
 *
 * @file plugin_dhtu_ip.c
 * @brief plain IP based DHT network underlay
 */
#include "platform.h"
#include "gnunet_dhtu_plugin.h"

#define SCAN_FREQ GNUNET_TIME_UNIT_MINUTES

/**
 * Opaque handle that the underlay offers for our address to be used when
 * sending messages to another peer.
 */
struct GNUNET_DHTU_Source
{

  /**
   * Kept in a DLL.
   */
  struct GNUNET_DHTU_Source *next;

  /**
   * Kept in a DLL.
   */
  struct GNUNET_DHTU_Source *prev;

  /**
   * Application context for this source.
   */
  void *app_ctx;

  /**
   * Address in URL form ("ip+udp://$IP:$PORT")
   */
  char *address;
  
  /**
   * Hash of the IP address.
   */
  struct GNUNET_DHTU_Hash id;
  
  /**
   * My actual address.
   */
  struct sockaddr_storage addr;

  /**
   * Number of bytes in @a addr.
   */
  socklen_t addrlen;
  
  /**
   * Last generation this address was observed.
   */
  unsigned int scan_generation;
  
};


/**
 * Opaque handle that the underlay offers for the target peer when sending
 * messages to another peer.
 */
struct GNUNET_DHTU_Target
{
  
  /**
   * Application context for this target.
   */
  void *app_ctx;

  /**
   * Head of preferences expressed for this target.
   */
  struct GNUNET_DHTU_PreferenceHandle *ph_head;

  /**
   * Tail of preferences expressed for this target.
   */
  struct GNUNET_DHTU_PreferenceHandle *ph_tail;
  
  /**
   * Target IP address.
   */
  struct sockaddr_storage addr;

  /**
   * Number of bytes in @a addr.
   */
  socklen_t addrlen;
  
  /**
   * Preference counter, length of the @a ph_head DLL.
   */
  unsigned int ph_count;

};

/**
 * Opaque handle expressing a preference of the DHT to
 * keep a particular target connected.
 */
struct GNUNET_DHTU_PreferenceHandle
{
  /**
   * Kept in a DLL.
   */
  struct GNUNET_DHTU_PreferenceHandle *next;

  /**
   * Kept in a DLL.
   */
  struct GNUNET_DHTU_PreferenceHandle *prev;

  /**
   * Target a preference was expressed for.
   */
  struct GNUNET_DHTU_Target *target;
};


/**
 * Closure for all plugin functions.
 */
struct Plugin
{
  /** 
   * Callbacks into the DHT.
   */
  struct GNUNET_DHTU_PluginEnvironment *env;

  /**
   * Head of sources where we receive traffic.
   */
  struct GNUNET_DHTU_Source *src_head;

  /**
   * Tail of sources where we receive traffic.
   */
  struct GNUNET_DHTU_Source *src_tail;

  /**
   * Task that scans for IP address changes.
   */
  struct GNUNET_SCHEDULER_Task *scan_task;

  /**
   * Task that reads incoming UDP packets.
   */
  struct GNUNET_SCHEDULER_Task *read_task;

  /**
   * Port we bind to. 
   */
  char *port;

  /**
   * How often have we scanned for IPs?
   */
  unsigned int scan_generation;

  /**
   * My UDP socket.
   */
  struct GNUNET_NETWORK_Handle *sock;
};


/**
 * Use our private key to sign a message.
 *
 * @param cls closure
 * @param pk our private key to sign with
 * @param purpose what to sign
 * @param[out] signature, allocated on heap and returned
 * @return -1 on error, otherwise number of bytes in @a sig
 */
static ssize_t
ip_sign (void *cls,
         const struct GNUNET_DHTU_PrivateKey *pk,
         const struct GNUNET_DHTU_SignaturePurpose *purpose,
         void **sig)
{
  return 0;
}


/**
 * Verify signature in @a sig over @a purpose.
 *
 * @param cls closure
 * @param pk public key to verify signature of
 * @param purpose what was being signed
 * @param sig signature data
 * @param sig_size number of bytes in @a sig
 * @return #GNUNET_OK if signature is valid
 *         #GNUNET_NO if signatures are not supported
 *         #GNUNET_SYSERR if signature is invalid
 */
static enum GNUNET_GenericReturnValue
ip_verify (void *cls,
           const struct GNUNET_DHTU_PublicKey *pk,
           const struct GNUNET_DHTU_SignaturePurpose *purpose,
           const void *sig,
           size_t sig_size)
{
  return GNUNET_NO;
}


/**
 * Request creation of a session with a peer at the given @a address.
 *
 * @param cls closure (internal context for the plugin)
 * @param address target address to connect to
 */
static void
ip_try_connect (void *cls,
                const char *address)
{
  struct Plugin *plugin = cls;
  char *colon;
  const char *port;
  char *addr;
  struct addrinfo hints = {
    .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
  };
  struct addrinfo *result = NULL;
  
  if (0 !=
      strncmp (address,
               "ip+",
               strlen ("ip+")))
  {
    GNUNET_break (0);
    return;
  }
  address += strlen ("ip+");
  if (0 !=
      strncmp (address,
               "udp://",
               strlen ("udp://")))
  {
    GNUNET_break (0);
    return;
  }
  address += strlen ("udp://");
  addr = GNUNET_strdup (address);
  colon = strchr (addr, ':');
  if (NULL == colon)
  {
    port = plugin->port;
  }
  else
  {
    *colon = '\0';
    port = colon + 1;
  }
  if (0 !=
      getaddrinfo (addr,
                   port,
                   &hints,
                   &result))
  {
    GNUNET_break (0);
    GNUNET_free (addr);
    return;
  }
  GNUNET_free (addr);
  (void) result->ai_addr; // FIXME: use!

  // FIXME: create target, etc.
  freeaddrinfo (result);
}


/**
 * Request underlay to keep the connection to @a target alive if possible.
 * Hold may be called multiple times to express a strong preference to
 * keep a connection, say because a @a target is in multiple tables.
 * 
 * @param cls closure
 * @param target connection to keep alive
 */
static struct GNUNET_DHTU_PreferenceHandle *
ip_hold (void *cls,
         struct GNUNET_DHTU_Target *target)
{
  struct GNUNET_DHTU_PreferenceHandle *ph;

  ph = GNUNET_new (struct GNUNET_DHTU_PreferenceHandle);
  ph->target = target;
  GNUNET_CONTAINER_DLL_insert (target->ph_head,
                               target->ph_tail,
                               ph);
  target->ph_count++;
  return ph;
}


/**
 * Do no long request underlay to keep the connection alive.
 * 
 * @param cls closure
 * @param target connection to keep alive
 */
static void
ip_drop (struct GNUNET_DHTU_PreferenceHandle *ph)
{
  struct GNUNET_DHTU_Target *target = ph->target;
  
  GNUNET_CONTAINER_DLL_remove (target->ph_head,
                               target->ph_tail,
                               ph);
  target->ph_count--;
  GNUNET_free (ph);
}


/**
 * Send message to some other participant over the network.  Note that
 * sending is not guaranteeing that the other peer actually received the
 * message.  For any given @a target, the DHT must wait for the @a
 * finished_cb to be called before calling send() again.
 *
 * @param cls closure (internal context for the plugin)
 * @param target receiver identification
 * @param msg message
 * @param msg_size number of bytes in @a msg
 * @param finished_cb function called once transmission is done
 *        (not called if @a target disconnects, then only the
 *         disconnect_cb is called). 
 * @param finished_cb_cls closure for @a finished_cb
 */
static void
ip_send (void *cls,
         struct GNUNET_DHTU_Target *target,
         const void *msg,
         size_t msg_size,
         GNUNET_SCHEDULER_TaskCallback finished_cb,
         void *finished_cb_cls)
{
  struct Plugin *plugin = cls;

  GNUNET_NETWORK_socket_sendto (plugin->sock,
                                msg,
                                msg_size,
                                (const struct sockaddr *) &target->addr,
                                target->addrlen);
  finished_cb (finished_cb_cls);
}


/**
 * Create a new source on which we may be receiving traffic.
 *
 * @param plugin our plugin
 * @param addr our address
 * @param addrlen number of bytes in @a addr
 * @return new source object
 */
static struct GNUNET_DHTU_Source *
create_source (struct Plugin *plugin,
               const struct sockaddr *addr,
               socklen_t addrlen)
{
  struct GNUNET_DHTU_Source *src;
  
  src = GNUNET_new (struct GNUNET_DHTU_Source);
  src->addrlen = addrlen;
  memcpy (&src->addr,
          addr,
          addrlen);
  src->scan_generation = plugin->scan_generation;
  switch (addr->sa_family)
  {
  case AF_INET:
    {
      const struct sockaddr_in *s4 = (const struct sockaddr_in *) addr;
      char buf[INET_ADDRSTRLEN];

      GNUNET_assert (sizeof (struct sockaddr_in) == addrlen);
      GNUNET_CRYPTO_hash (&s4->sin_addr,
                          sizeof (struct in_addr),
                          &src->id.hc);
      GNUNET_asprintf (&src->address,
                       "ip+udp://%s:%u",
                       inet_ntop (AF_INET,
                                  &s4->sin_addr,
                                  buf,
                                  sizeof (buf)),
                       ntohs (s4->sin_port));
    }
    break;
  case AF_INET6:
    {
      const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) addr;
      char buf[INET6_ADDRSTRLEN];

      GNUNET_assert (sizeof (struct sockaddr_in6) == addrlen);
      GNUNET_CRYPTO_hash (&s6->sin6_addr,
                          sizeof (struct in6_addr),
                          &src->id.hc);
      GNUNET_asprintf (&src->address,
                       "ip+udp://[%s]:%u",
                       inet_ntop (AF_INET6,
                                  &s6->sin6_addr,
                                  buf,
                                  sizeof (buf)),
                       ntohs (s6->sin6_port));
    }
    break;
  default:
    GNUNET_break (0);
    GNUNET_free (src);
    return NULL;
  }
  GNUNET_CONTAINER_DLL_insert (plugin->src_head,
                               plugin->src_tail,
                               src);
  plugin->env->address_add_cb (plugin->env->cls,
                               &src->id,
                               NULL, /* no key */
                               src->address,
                               src,
                               &src->app_ctx);
  return src;
}


/**
 * Callback function invoked for each interface found.
 *
 * @param cls closure
 * @param name name of the interface (can be NULL for unknown)
 * @param isDefault is this presumably the default interface
 * @param addr address of this interface (can be NULL for unknown or unassigned)
 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
 * @param netmask the network mask (can be NULL for unknown or unassigned)
 * @param addrlen length of the address
 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
 */
static int
process_ifcs (void *cls,
              const char *name,
              int isDefault,
              const struct sockaddr *addr,
              const struct sockaddr *broadcast_addr,
              const struct sockaddr *netmask,
              socklen_t addrlen)
{
  struct Plugin *plugin = cls;
  struct GNUNET_DHTU_Source *src;

  for (src = plugin->src_head;
       NULL != src;
       src = src->next)
  {
    if ( (addrlen == src->addrlen) &&
         (0 == memcmp (addr,
                       &src->addr,
                       addrlen)) )
    {
      src->scan_generation = plugin->scan_generation;
      return GNUNET_OK;
    }
  }
  (void) create_source (plugin,
                        addr,
                        addrlen);
  return GNUNET_OK;
}


/**
 * Scan network interfaces for IP address changes.
 *
 * @param cls a `struct Plugin`
 */
static void
scan (void *cls)
{
  struct Plugin *plugin = cls;
  struct GNUNET_DHTU_Source *next;

  plugin->scan_generation++;
  GNUNET_OS_network_interfaces_list (&process_ifcs,
                                     plugin);
  for (struct GNUNET_DHTU_Source *src = plugin->src_head;
       NULL != src;
       src = next)
  {
    next = src->next;
    if (src->scan_generation == plugin->scan_generation)
      continue;
    GNUNET_CONTAINER_DLL_remove (plugin->src_head,
                                 plugin->src_tail,
                                 src);
    plugin->env->address_del_cb (src->app_ctx);
    GNUNET_free (src->address);
    GNUNET_free (src);
  }
  plugin->scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ,
                                                    &scan,
                                                    plugin);
}


/**
 * Find our source matching @a addr. If none exists,
 * create one!
 *
 * @param plugin the plugin handle
 * @param addr IP address to find
 * @param addrlen number of bytes in @a addr
 * @return matching source object
 */
static struct GNUNET_DHTU_Source *
find_source (struct Plugin *plugin,
             const void *addr,
             size_t addrlen)
{
  for (struct GNUNET_DHTU_Source *src = plugin->src_head;
       NULL != src;
       src = src->next)
  {
    if ( (addrlen == sizeof (struct in_addr)) &&
         (src->addrlen == sizeof (struct sockaddr_in)) )
    {
      const struct sockaddr_in *sa =
        (const struct sockaddr_in *) &src->addr;
      if (0 == memcmp (addr,
                       &sa->sin_addr,
                       addrlen))
        return src;
    }
    if ( (addrlen == sizeof (struct in6_addr)) &&
         (src->addrlen == sizeof (struct sockaddr_in6)) )
    {
      const struct sockaddr_in6 *sa =
        (const struct sockaddr_in6 *) &src->addr;
      if (0 == memcmp (addr,
                       &sa->sin6_addr,
                       addrlen))
        return src;
    }
  }

  {
    struct sockaddr_storage sa;
    socklen_t salen;

    memset (&sa,
            0,
            sizeof (sa));
    if (addrlen == sizeof (struct in_addr)) 
    {
      struct sockaddr_in *s4 =
        (struct sockaddr_in *) &sa;
      
      s4->sin_family = AF_INET;
      memcpy (&s4->sin_addr,
              addr,
              addrlen);
      salen = sizeof (*s4);
    }
    if (addrlen == sizeof (struct in6_addr)) 
    {
      struct sockaddr_in6 *s6 =
        (struct sockaddr_in6 *) &sa;
      
      s6->sin6_family = AF_INET6;
      memcpy (&s6->sin6_addr,
              addr,
              addrlen);
      salen = sizeof (*s6);
    }    
    return create_source (plugin,
                          (const struct sockaddr *) &sa,
                          salen);
  }
}


/**
 * UDP socket is ready to receive. Read.
 *
 * @param cls our `struct Plugin *`
 */
static void
read_cb (void *cls)
{
  struct Plugin *plugin = cls;
  ssize_t ret;
  char buf[65536];
  struct sockaddr_storage sa;
  struct iovec iov = {
    .iov_base = buf,
    .iov_len = sizeof (buf)
  };
  char ctl[1024];
  struct msghdr mh = {
    .msg_name = &sa,
    .msg_namelen = sizeof (sa),
    .msg_iov = &iov,
    .msg_iovlen = 1,
    .msg_control = ctl,
    .msg_controllen = sizeof (ctl)
  };
  
  ret = recvmsg  (GNUNET_NETWORK_get_fd (plugin->sock),
                  &mh,
                  MSG_DONTWAIT);
  if (ret >= 0)
  {
    struct GNUNET_DHTU_Target *dst = NULL;
    struct GNUNET_DHTU_Source *src = NULL;
    struct cmsghdr *cmsg;
    struct msghdr *msh = mh.msg_control;

    /* find IP where we received message */
    for (cmsg = CMSG_FIRSTHDR (msh);
         NULL != cmsg;
         cmsg = CMSG_NXTHDR (msh,
                             cmsg))
    {
      if ( (cmsg->cmsg_level == IPPROTO_IP) &&
           (cmsg->cmsg_type == IP_ORIGDSTADDR) )
      {
        if (CMSG_LEN (sizeof (struct in_addr)) ==
            cmsg->cmsg_len)
        {
          struct in6_addr ia;
          
          memcpy (&ia,
                  CMSG_DATA (cmsg),
                  sizeof (ia));
          src = find_source (plugin,
                             &ia,
                             sizeof (ia));
          break;
        }
        else
          GNUNET_break (0);
      }
      if ( (cmsg->cmsg_level == IPPROTO_IPV6) &&
           (cmsg->cmsg_type == IP_ORIGDSTADDR) )
      {
        if (CMSG_LEN (sizeof (struct in6_addr)) ==
                 cmsg->cmsg_len)
        {
          struct in6_addr ia;
          
          memcpy (&ia,
                  CMSG_DATA (cmsg),
                  sizeof (ia));
          src = find_source (plugin,
                             &ia,
                             sizeof (ia));
          break;
        }
        else
          GNUNET_break (0);
      }   
    }
    // FIXME: find or create 'dst'!
    if ( (NULL == src) ||
         (NULL == dst) )
    {
      GNUNET_break (0);
    }
    else
    {
      plugin->env->receive_cb (plugin->env->cls,
                               dst->app_ctx,
                               src->app_ctx,
                               buf,
                               ret);
    }
  }
  plugin->read_task = GNUNET_SCHEDULER_add_read_net (
    GNUNET_TIME_UNIT_FOREVER_REL,
    plugin->sock,
    &read_cb,
    plugin);
}


/**
 * Entry point for the plugin.
 *
 * @param cls closure (the `struct GNUNET_DHTU_PluginEnvironment`)
 * @return the plugin's API
 */
void *
libgnunet_plugin_dhtu_ip_init (void *cls)
{
  struct GNUNET_DHTU_PluginEnvironment *env = cls;
  struct GNUNET_DHTU_PluginFunctions *api;
  struct Plugin *plugin;
  char *port;
  unsigned int nport;
  int sock;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (env->cfg,
                                             "DHTU-IP",
                                             "UDP_PORT",
                                             &port))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "DHTU-IP",
                               "UDP_PORT");
    return NULL;
  }
  {
    char dummy;

    if ( (1 != sscanf (port,
                       "%u%c",
                       &nport,
                       &dummy)) ||
         (nport > UINT16_MAX) )
    {
      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
                                 "DHTU-IP",
                                 "UDP_PORT",
                                 "must be number below 65536");
      GNUNET_free (port);
      return NULL;
    }
  }
  plugin = GNUNET_new (struct Plugin);
  plugin->env = env;
  plugin->port = port;
  sock = socket (AF_INET6,
                 SOCK_DGRAM,
                 IPPROTO_UDP);
  if (-1 == sock)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                         "socket");
    GNUNET_free (plugin->port);
    GNUNET_free (plugin);
    return NULL;
  }
  {
    struct sockaddr_in6 sa = {
      .sin6_family = AF_INET6,
      .sin6_port = htons ((uint16_t) nport)
    };

    if (0 !=
        bind (sock,
              &sa,
              sizeof (sa)))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                           "socket");
      GNUNET_break (0 ==
                    close (sock));
      GNUNET_free (plugin->port);
      GNUNET_free (plugin);
      return NULL;
    }
  }
  plugin->sock = GNUNET_NETWORK_socket_box_native (sock);
  plugin->read_task = GNUNET_SCHEDULER_add_read_net (
    GNUNET_TIME_UNIT_FOREVER_REL,
    plugin->sock,
    &read_cb,
    plugin);
  // FIXME: deal with NSE callback...
  plugin->scan_task = GNUNET_SCHEDULER_add_now (&scan,
                                                plugin);
  api = GNUNET_new (struct GNUNET_DHTU_PluginFunctions);
  api->cls = plugin;
  api->sign = &ip_sign;
  api->verify = &ip_verify;
  api->try_connect = &ip_try_connect;
  api->hold = &ip_hold;
  api->drop = &ip_drop;
  api->send = &ip_send;
  return api;
}


/**
 * Exit point from the plugin.
 *
 * @param cls closure (our `struct Plugin`)
 * @return NULL
 */
void *
libgnunet_plugin_dhtu_ip_done (void *cls)
{
  struct GNUNET_DHTU_PluginFunctions *api = cls;
  struct Plugin *plugin = api->cls;

  GNUNET_SCHEDULER_cancel (plugin->scan_task);
  GNUNET_break (0 ==
                GNUNET_NETWORK_socket_close (plugin->sock));
  GNUNET_free (plugin->port);
  GNUNET_free (plugin);
  GNUNET_free (api);
  return NULL;
}