-more debugging, fixing targets
[oweals/gnunet.git] / src / dv / plugin_transport_dv.c
1 /*
2      This file is part of GNUnet
3      (C) 2002--2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file dv/plugin_transport_dv.c
23  * @brief DV transport service, takes incoming DV requests and deals with
24  * the DV service
25  * @author Nathan Evans
26  * @author Christian Grothoff
27  */
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_dv_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "dv.h"
37
38
39 #define LOG(kind,...) GNUNET_log_from (kind, "transport-dv",__VA_ARGS__)
40
41 #define PLUGIN_NAME "dv"
42
43 /**
44  * Encapsulation of all of the state of the plugin.
45  */
46 struct Plugin;
47
48
49 /**
50  * An active request for transmission via DV.
51  */
52 struct PendingRequest
53 {
54
55   /**
56    * This is a DLL.
57    */
58   struct PendingRequest *next;
59
60   /**
61    * This is a DLL.
62    */
63   struct PendingRequest *prev;
64
65   /**
66    * Continuation function to call once the transmission buffer
67    * has again space available.  NULL if there is no
68    * continuation to call.
69    */
70   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
71
72   /**
73    * Closure for transmit_cont.
74    */
75   void *transmit_cont_cls;
76
77   /**
78    * Transmission handle from DV client library.
79    */
80   struct GNUNET_DV_TransmitHandle *th;
81
82   /**
83    * Session of this request.
84    */
85   struct Session *session;
86
87 };
88
89
90 /**
91  * Session handle for connections.
92  */
93 struct Session
94 {
95
96   /**
97    * Mandatory session header.
98    */
99   struct SessionHeader header;
100
101   /**
102    * Pointer to the global plugin struct.
103    */
104   struct Plugin *plugin;
105
106   /**
107    * Head of pending requests.
108    */
109   struct PendingRequest *pr_head;
110
111   /**
112    * Tail of pending requests.
113    */
114   struct PendingRequest *pr_tail;
115
116   /**
117    * To whom are we talking to.
118    */
119   struct GNUNET_PeerIdentity sender;
120
121   /**
122    * Current distance to the given peer.
123    */
124   uint32_t distance;
125
126   /**
127    * Current network the next hop peer is located in
128    */
129   enum GNUNET_ATS_Network_Type network;
130
131   /**
132    * Does the transport service know about this session (and we thus
133    * need to call `session_end` when it is released?)
134    */
135   int active;
136
137 };
138
139
140 /**
141  * Encapsulation of all of the state of the plugin.
142  */
143 struct Plugin
144 {
145   /**
146    * Our environment.
147    */
148   struct GNUNET_TRANSPORT_PluginEnvironment *env;
149
150   /**
151    * Hash map of sessions (active and inactive).
152    */
153   struct GNUNET_CONTAINER_MultiPeerMap *sessions;
154
155   /**
156    * Copy of the handler array where the closures are
157    * set to this struct's instance.
158    */
159   struct GNUNET_SERVER_MessageHandler *handlers;
160
161   /**
162    * Handle to the DV service
163    */
164   struct GNUNET_DV_ServiceHandle *dvh;
165
166   /**
167    * Tokenizer for boxed messages.
168    */
169   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
170
171 };
172
173
174 /**
175  * Notify transport service about the change in distance.
176  *
177  * @param session session where the distance changed
178  */
179 static void
180 notify_distance_change (struct Session *session)
181 {
182   struct Plugin *plugin = session->plugin;
183   struct GNUNET_ATS_Information ats;
184
185   if (GNUNET_YES != session->active)
186     return;
187   ats.type = htonl ((uint32_t) GNUNET_ATS_QUALITY_NET_DISTANCE);
188   ats.value = htonl (session->distance);
189   plugin->env->update_address_metrics (plugin->env->cls,
190                                        &session->sender,
191                                        NULL, 0,
192                                        session,
193                                        &ats, 1);
194 }
195
196
197 /**
198  * Function called by MST on each message from the box.
199  *
200  * @param cls closure with the `struct Plugin *`
201  * @param client identification of the client (with the 'struct Session')
202  * @param message the actual message
203  * @return #GNUNET_OK on success
204  */
205 static int
206 unbox_cb (void *cls,
207           void *client,
208           const struct GNUNET_MessageHeader *message)
209 {
210   struct Plugin *plugin = cls;
211   struct Session *session = client;
212   struct GNUNET_ATS_Information ats;
213
214   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
215   ats.value = htonl (session->distance);
216   session->active = GNUNET_YES;
217   plugin->env->receive (plugin->env->cls,
218                         &session->sender,
219                         message,
220                         session, "", 0);
221   plugin->env->update_address_metrics (plugin->env->cls,
222                                        &session->sender, NULL,
223                                        0, session,
224                                        &ats, 1);
225   return GNUNET_OK;
226 }
227
228
229 /**
230  * Handler for messages received from the DV service.
231  *
232  * @param cls closure with the plugin
233  * @param sender sender of the message
234  * @param distance how far did the message travel
235  * @param msg actual message payload
236  */
237 static void
238 handle_dv_message_received (void *cls,
239                             const struct GNUNET_PeerIdentity *sender,
240                             uint32_t distance,
241                             const struct GNUNET_MessageHeader *msg)
242 {
243   struct Plugin *plugin = cls;
244   struct GNUNET_ATS_Information ats;
245   struct Session *session;
246
247   LOG (GNUNET_ERROR_TYPE_DEBUG,
248        "Received `%s' message for peer `%s': new distance %u\n",
249        "DV_MESSAGE_RECEIVED",
250        GNUNET_i2s (sender), distance);
251   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
252                                                sender);
253   if (NULL == session)
254   {
255     GNUNET_break (0);
256     return;
257   }
258   if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
259   {
260     /* need to unbox using MST */
261     GNUNET_SERVER_mst_receive (plugin->mst,
262                                session,
263                                (const char *) &msg[1],
264                                ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
265                                GNUNET_YES,
266                                GNUNET_NO);
267     return;
268   }
269   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
270   ats.value = htonl (distance);
271   session->active = GNUNET_YES;
272   plugin->env->receive (plugin->env->cls, sender,
273                         msg,
274                         session, "", 0);
275   plugin->env->update_address_metrics (plugin->env->cls,
276                                        sender, "",
277                                        0, session,
278                                        &ats, 1);
279 }
280
281
282 /**
283  * Function called if DV starts to be able to talk to a peer.
284  *
285  * @param cls closure with `struct Plugin *`
286  * @param peer newly connected peer
287  * @param distance distance to the peer
288  * @param network the network the next hop is located in
289  */
290 static void
291 handle_dv_connect (void *cls,
292                    const struct GNUNET_PeerIdentity *peer,
293                    uint32_t distance,
294                    enum GNUNET_ATS_Network_Type network)
295 {
296   struct Plugin *plugin = cls;
297   struct Session *session;
298   struct GNUNET_ATS_Information ats[2];
299
300   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
301   /**
302    * This requires transport plugin to be linked to libgnunetats.
303    * If you remove it, also remove libgnunetats linkage from Makefile.am
304    */
305   LOG (GNUNET_ERROR_TYPE_DEBUG,
306        "Received `%s' message for peer `%s' with next hop in network %s\n",
307        "DV_CONNECT",
308        GNUNET_i2s (peer),
309        GNUNET_ATS_print_network_type (network));
310
311   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
312                                                peer);
313   if (NULL != session)
314   {
315     GNUNET_break (0);
316     session->distance = distance;
317     notify_distance_change (session);
318     return; /* nothing to do */
319   }
320
321   session = GNUNET_new (struct Session);
322   session->sender = *peer;
323   session->plugin = plugin;
324   session->distance = distance;
325   session->network = network;
326   GNUNET_assert(GNUNET_YES ==
327                 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
328                                                    &session->sender, session,
329                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
330
331   LOG (GNUNET_ERROR_TYPE_DEBUG,
332        "Creating new DV session %p for peer `%s' at distance %u\n",
333        session,
334        GNUNET_i2s (peer),
335        distance);
336
337   /* Notify transport and ats about new connection */
338   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
339   ats[0].value = htonl (distance);
340   ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
341   ats[1].value = htonl ((uint32_t) network);
342   session->active = GNUNET_YES;
343   plugin->env->session_start (plugin->env->cls, peer,
344                               PLUGIN_NAME,
345                               NULL, 0,
346                               session, ats, 2);
347 }
348
349
350 /**
351  * Function called if DV distance to a peer is changed.
352  *
353  * @param cls closure with `struct Plugin *`
354  * @param peer connected peer
355  * @param distance new distance to the peer
356  * @param network network type used for the connection
357  */
358 static void
359 handle_dv_distance_changed (void *cls,
360                             const struct GNUNET_PeerIdentity *peer,
361                             uint32_t distance,
362                             enum GNUNET_ATS_Network_Type network)
363 {
364   struct Plugin *plugin = cls;
365   struct Session *session;
366
367   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
368   LOG (GNUNET_ERROR_TYPE_DEBUG,
369        "Received `%s' message for peer `%s': new distance %u\n",
370        "DV_DISTANCE_CHANGED",
371        GNUNET_i2s (peer),
372        distance);
373   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
374                                                peer);
375   if (NULL == session)
376   {
377     GNUNET_break (0);
378     handle_dv_connect (plugin, peer, distance, network);
379     return;
380   }
381   session->distance = distance;
382   notify_distance_change (session);
383 }
384
385
386 /**
387  * Release session object and clean up associated resources.
388  *
389  * @param session session to clean up
390  */
391 static void
392 free_session (struct Session *session)
393 {
394   struct Plugin *plugin = session->plugin;
395   struct PendingRequest *pr;
396
397   GNUNET_assert (GNUNET_YES ==
398                  GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
399                                                        &session->sender,
400                                                        session));
401
402   LOG (GNUNET_ERROR_TYPE_DEBUG,
403        "Freeing session %p for peer `%s'\n",
404        session,
405        GNUNET_i2s (&session->sender));
406   if (GNUNET_YES == session->active)
407   {
408     plugin->env->session_end (plugin->env->cls,
409                               &session->sender,
410                               session);
411     session->active = GNUNET_NO;
412   }
413   while (NULL != (pr = session->pr_head))
414   {
415     GNUNET_CONTAINER_DLL_remove (session->pr_head,
416                                  session->pr_tail,
417                                  pr);
418     GNUNET_DV_send_cancel (pr->th);
419     pr->th = NULL;
420     if (NULL != pr->transmit_cont)
421       pr->transmit_cont (pr->transmit_cont_cls,
422                          &session->sender,
423                          GNUNET_SYSERR, 0, 0);
424     GNUNET_free (pr);
425   }
426   GNUNET_free (session);
427 }
428
429
430 /**
431  * Function called if DV is no longer able to talk to a peer.
432  *
433  * @param cls closure with `struct Plugin *`
434  * @param peer peer that disconnected
435  */
436 static void
437 handle_dv_disconnect (void *cls,
438                       const struct GNUNET_PeerIdentity *peer)
439 {
440   struct Plugin *plugin = cls;
441   struct Session *session;
442
443   LOG (GNUNET_ERROR_TYPE_DEBUG,
444        "Received `%s' message for peer `%s'\n",
445        "DV_DISCONNECT",
446        GNUNET_i2s (peer));
447   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
448                                                peer);
449   if (NULL == session)
450     return; /* nothing to do */
451   free_session (session);
452 }
453
454
455 /**
456  * Function called once the delivery of a message has been successful.
457  * Clean up the pending request, and call continuations.
458  *
459  * @param cls closure
460  * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
461  */
462 static void
463 send_finished (void *cls,
464                int ok)
465 {
466   struct PendingRequest *pr = cls;
467   struct Session *session = pr->session;
468
469   pr->th = NULL;
470   GNUNET_CONTAINER_DLL_remove (session->pr_head,
471                                session->pr_tail,
472                                pr);
473   if (NULL != pr->transmit_cont)
474     pr->transmit_cont (pr->transmit_cont_cls,
475                        &session->sender,
476                        ok, 0, 0);
477   GNUNET_free (pr);
478 }
479
480
481 /**
482  * Function that can be used by the transport service to transmit
483  * a message using the plugin.
484  *
485  * @param cls closure
486  * @param session the session used
487  * @param priority how important is the message
488  * @param msgbuf the message to transmit
489  * @param msgbuf_size number of bytes in 'msgbuf'
490  * @param timeout when should we time out
491  * @param cont continuation to call once the message has
492  *        been transmitted (or if the transport is ready
493  *        for the next transmission call; or if the
494  *        peer disconnected...)
495  * @param cont_cls closure for @a cont
496  * @return number of bytes used (on the physical network, with overheads);
497  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
498  *         and does NOT mean that the message was not transmitted (DV)
499  */
500 static ssize_t
501 dv_plugin_send (void *cls,
502                 struct Session *session,
503                 const char *msgbuf,
504                 size_t msgbuf_size,
505                 unsigned int priority,
506                 struct GNUNET_TIME_Relative timeout,
507                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
508 {
509   struct Plugin *plugin = cls;
510   struct PendingRequest *pr;
511   const struct GNUNET_MessageHeader *msg;
512   struct GNUNET_MessageHeader *box;
513
514   box = NULL;
515   msg = (const struct GNUNET_MessageHeader *) msgbuf;
516   if (ntohs (msg->size) != msgbuf_size)
517   {
518     /* need to box */
519     box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
520     box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
521     box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
522     memcpy (&box[1], msgbuf, msgbuf_size);
523     msg = box;
524   }
525   pr = GNUNET_new (struct PendingRequest);
526   pr->transmit_cont = cont;
527   pr->transmit_cont_cls = cont_cls;
528   pr->session = session;
529   GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
530                                     session->pr_tail,
531                                     pr);
532
533   pr->th = GNUNET_DV_send (plugin->dvh,
534                            &session->sender,
535                            msg ,
536                            &send_finished,
537                            pr);
538   GNUNET_free_non_null (box);
539   return 0; /* DV */
540 }
541
542
543 /**
544  * Function that can be used to force the plugin to disconnect
545  * from the given peer and cancel all previous transmissions
546  * (and their continuations).
547  *
548  * @param cls closure with the `struct Plugin *`
549  * @param target peer from which to disconnect
550  */
551 static void
552 dv_plugin_disconnect_peer (void *cls,
553                            const struct GNUNET_PeerIdentity *target)
554 {
555   struct Plugin *plugin = cls;
556   struct Session *session;
557   struct PendingRequest *pr;
558
559   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
560                                                target);
561   if (NULL == session)
562     return; /* nothing to do */
563   while (NULL != (pr = session->pr_head))
564   {
565     GNUNET_CONTAINER_DLL_remove (session->pr_head,
566                                  session->pr_tail,
567                                  pr);
568     GNUNET_DV_send_cancel (pr->th);
569     pr->th = NULL;
570     if (NULL != pr->transmit_cont)
571       pr->transmit_cont (pr->transmit_cont_cls,
572                          &session->sender,
573                          GNUNET_SYSERR, 0, 0);
574     GNUNET_free (pr);
575   }
576   session->active = GNUNET_NO;
577 }
578
579
580 /**
581  * Function that can be used to force the plugin to disconnect
582  * from the given peer and cancel all previous transmissions
583  * (and their continuations).
584  *
585  * @param cls closure with the `struct Plugin *`
586  * @param session which session to disconnect
587  * @return #GNUNET_OK
588  */
589 static int
590 dv_plugin_disconnect_session (void *cls,
591                               struct Session *session)
592 {
593   struct PendingRequest *pr;
594
595   while (NULL != (pr = session->pr_head))
596   {
597     GNUNET_CONTAINER_DLL_remove (session->pr_head,
598                                  session->pr_tail,
599                                  pr);
600     GNUNET_DV_send_cancel (pr->th);
601     pr->th = NULL;
602     if (NULL != pr->transmit_cont)
603       pr->transmit_cont (pr->transmit_cont_cls,
604                          &session->sender,
605                          GNUNET_SYSERR, 0, 0);
606     GNUNET_free (pr);
607   }
608   session->active = GNUNET_NO;
609   return GNUNET_OK;
610 }
611
612
613 /**
614  * Convert the transports address to a nice, human-readable
615  * format.
616  *
617  * @param cls closure
618  * @param type name of the transport that generated the address
619  * @param addr one of the addresses of the host, NULL for the last address
620  *        the specific address format depends on the transport
621  * @param addrlen length of the address
622  * @param numeric should (IP) addresses be displayed in numeric form?
623  * @param timeout after how long should we give up?
624  * @param asc function to call on each string
625  * @param asc_cls closure for @a asc
626  */
627 static void
628 dv_plugin_address_pretty_printer (void *cls, const char *type,
629                                   const void *addr,
630                                   size_t addrlen, int numeric,
631                                   struct GNUNET_TIME_Relative timeout,
632                                   GNUNET_TRANSPORT_AddressStringCallback asc,
633                                   void *asc_cls)
634 {
635   if ( (0 == addrlen) &&
636        (0 == strcmp (type, "dv")) )
637     asc (asc_cls, "dv");
638   asc (asc_cls, NULL);
639 }
640
641
642 /**
643  * Convert the DV address to a pretty string.
644  *
645  * @param cls closure
646  * @param addr the (hopefully) DV address
647  * @param addrlen the length of the @a addr
648  * @return string representing the DV address
649  */
650 static const char *
651 dv_plugin_address_to_string (void *cls,
652                              const void *addr,
653                              size_t addrlen)
654 {
655   if (0 != addrlen)
656   {
657     GNUNET_break (0); /* malformed */
658     return NULL;
659   }
660   return "dv";
661 }
662
663
664 /**
665  * Another peer has suggested an address for this peer and transport
666  * plugin.  Check that this could be a valid address.  This function
667  * is not expected to 'validate' the address in the sense of trying to
668  * connect to it but simply to see if the binary format is technically
669  * legal for establishing a connection to this peer (and make sure that
670  * the address really corresponds to our network connection/settings
671  * and not some potential man-in-the-middle).
672  *
673  * @param cls closure
674  * @param addr pointer to the address
675  * @param addrlen length of addr
676  * @return #GNUNET_OK if this is a plausible address for this peer
677  *         and transport, #GNUNET_SYSERR if not
678  *
679  */
680 static int
681 dv_plugin_check_address (void *cls,
682                          const void *addr,
683                          size_t addrlen)
684 {
685   if (0 != addrlen)
686     return GNUNET_SYSERR;
687   return GNUNET_OK;
688 }
689
690
691 /**
692  * Create a new session to transmit data to the target
693  * This session will used to send data to this peer and the plugin will
694  * notify us by calling the env->session_end function
695  *
696  * @param cls the plugin
697  * @param address the address
698  * @return the session if the address is valid, NULL otherwise
699  */
700 static struct Session *
701 dv_get_session (void *cls,
702                 const struct GNUNET_HELLO_Address *address)
703 {
704   struct Plugin *plugin = cls;
705   struct Session *session;
706
707   if (0 != address->address_length)
708     return NULL;
709   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
710                                                &address->peer);
711   if (NULL == session)
712     return NULL; /* not valid right now */
713   session->active = GNUNET_YES;
714   return session;
715 }
716
717
718 /**
719  * Function called to convert a string address to
720  * a binary address.
721  *
722  * @param cls closure ('struct Plugin*')
723  * @param addr string address
724  * @param addrlen length of the @a addr including \0 termination
725  * @param buf location to store the buffer
726  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
727  * @param added length of created address
728  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
729  */
730 static int
731 dv_plugin_string_to_address (void *cls,
732                              const char *addr,
733                              uint16_t addrlen,
734                              void **buf,
735                              size_t *added)
736 {
737   if ( (addrlen == 3) &&
738        (0 == strcmp ("dv", addr)) )
739   {
740     *added = 0;
741     return GNUNET_OK;
742   }
743   return GNUNET_SYSERR;
744 }
745
746 static void
747 dv_plugin_update_session_timeout (void *cls,
748                                   const struct GNUNET_PeerIdentity *peer,
749                                   struct Session *session)
750 {
751
752 }
753
754 /**
755  * Function to obtain the network type for a session
756  * FIXME: we should probably look at the network type
757  * used by the next hop here.  Or find some other way
758  * to properly allow ATS-DV resource allocation.
759  *
760  * @param cls closure (`struct Plugin *`)
761  * @param session the session
762  * @return the network type
763  */
764 static enum GNUNET_ATS_Network_Type
765 dv_get_network (void *cls,
766                 struct Session *session)
767 {
768   GNUNET_assert (NULL != session);
769   return session->network;
770 }
771
772
773 /**
774  * Function that is called to get the keepalive factor.
775  * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
776  * calculate the interval between keepalive packets.
777  *
778  * @param cls closure with the `struct Plugin`
779  * @return keepalive factor
780  */
781 static unsigned int
782 dv_plugin_query_keepalive_factor (void *cls)
783 {
784   return 3;
785 }
786
787
788 /**
789  * Entry point for the plugin.
790  *
791  * @param cls closure with the plugin environment
792  * @return plugin API
793  */
794 void *
795 libgnunet_plugin_transport_dv_init (void *cls)
796 {
797   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
798   struct GNUNET_TRANSPORT_PluginFunctions *api;
799   struct Plugin *plugin;
800
801   plugin = GNUNET_new (struct Plugin);
802   plugin->env = env;
803   plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
804   plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
805                                           plugin);
806   plugin->dvh = GNUNET_DV_service_connect (env->cfg,
807                                            plugin,
808                                            &handle_dv_connect,
809                                            &handle_dv_distance_changed,
810                                            &handle_dv_disconnect,
811                                            &handle_dv_message_received);
812   if (NULL == plugin->dvh)
813   {
814     GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
815     GNUNET_SERVER_mst_destroy (plugin->mst);
816     GNUNET_free (plugin);
817     return NULL;
818   }
819   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
820   api->cls = plugin;
821   api->send = &dv_plugin_send;
822   api->disconnect_peer = &dv_plugin_disconnect_peer;
823   api->disconnect_session = &dv_plugin_disconnect_session;
824   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
825   api->check_address = &dv_plugin_check_address;
826   api->address_to_string = &dv_plugin_address_to_string;
827   api->string_to_address = &dv_plugin_string_to_address;
828   api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
829   api->get_session = &dv_get_session;
830   api->get_network = &dv_get_network;
831   api->update_session_timeout = &dv_plugin_update_session_timeout;
832   return api;
833 }
834
835
836 /**
837  * Function called to free a session.
838  *
839  * @param cls NULL
840  * @param key unused
841  * @param value session to free
842  * @return #GNUNET_OK (continue to iterate)
843  */
844 static int
845 free_session_iterator (void *cls,
846                        const struct GNUNET_PeerIdentity *key,
847                        void *value)
848 {
849   struct Session *session = value;
850
851   free_session (session);
852   return GNUNET_OK;
853 }
854
855
856 /**
857  * Exit point from the plugin.
858  *
859  * @param cls plugin API
860  * @return NULL
861  */
862 void *
863 libgnunet_plugin_transport_dv_done (void *cls)
864 {
865   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
866   struct Plugin *plugin = api->cls;
867
868   GNUNET_DV_service_disconnect (plugin->dvh);
869   GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
870                                          &free_session_iterator,
871                                          NULL);
872   GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
873   GNUNET_SERVER_mst_destroy (plugin->mst);
874   GNUNET_free (plugin);
875   GNUNET_free (api);
876   return NULL;
877 }
878
879 /* end of plugin_transport_dv.c */