allow distance change to communicate changes in network of next hop as well, related...
[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   uint32_t 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, uint32_t network)
294 {
295   struct Plugin *plugin = cls;
296   struct Session *session;
297   struct GNUNET_ATS_Information ats[2];
298
299   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
300   /**
301    * This requires transport plugin to be linked to libgnunetats.
302    * If you remove it, also remove libgnunetats linkage from Makefile.am
303    */
304   LOG (GNUNET_ERROR_TYPE_DEBUG,
305        "Received `%s' message for peer `%s' with next hop in network %s\n",
306        "DV_CONNECT",
307        GNUNET_i2s (peer),
308        GNUNET_ATS_print_network_type (network));
309
310   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
311                                                peer);
312   if (NULL != session)
313   {
314     GNUNET_break (0);
315     session->distance = distance;
316     notify_distance_change (session);
317     return; /* nothing to do */
318   }
319
320   session = GNUNET_new (struct Session);
321   session->sender = *peer;
322   session->plugin = plugin;
323   session->distance = distance;
324   session->network = network;
325   GNUNET_assert(GNUNET_YES ==
326                 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
327                                                    &session->sender, session,
328                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
329
330   LOG (GNUNET_ERROR_TYPE_DEBUG,
331        "Creating new session %p for peer `%s'\n",
332        session,
333        GNUNET_i2s (peer));
334
335   /* Notify transport and ats about new connection */
336   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
337   ats[0].value = htonl (distance);
338   ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
339   ats[1].value = htonl (network);
340   session->active = GNUNET_YES;
341   plugin->env->session_start (plugin->env->cls, peer,
342                               PLUGIN_NAME,
343                               NULL, 0,
344                               session, ats, 2);
345 }
346
347
348 /**
349  * Function called if DV distance to a peer is changed.
350  *
351  * @param cls closure with `struct Plugin *`
352  * @param peer connected peer
353  * @param distance new distance to the peer
354  * @param network network type used for the connection
355  */
356 static void
357 handle_dv_distance_changed (void *cls,
358                             const struct GNUNET_PeerIdentity *peer,
359                             uint32_t distance,
360                             uint32_t network)
361 {
362   struct Plugin *plugin = cls;
363   struct Session *session;
364
365   LOG (GNUNET_ERROR_TYPE_DEBUG,
366        "Received `%s' message for peer `%s': new distance %u\n",
367        "DV_DISTANCE_CHANGED",
368        GNUNET_i2s (peer),
369        distance);
370   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
371                                                peer);
372   if (NULL == session)
373   {
374     GNUNET_break (0);
375     handle_dv_connect (plugin, peer, distance, network);
376     return;
377   }
378   session->distance = distance;
379   notify_distance_change (session);
380 }
381
382
383 /**
384  * Release session object and clean up associated resources.
385  *
386  * @param session session to clean up
387  */
388 static void
389 free_session (struct Session *session)
390 {
391   struct Plugin *plugin = session->plugin;
392   struct PendingRequest *pr;
393
394   GNUNET_assert (GNUNET_YES ==
395                  GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
396                                                        &session->sender,
397                                                        session));
398
399   LOG (GNUNET_ERROR_TYPE_DEBUG,
400        "Freeing session %p for peer `%s'\n",
401        session,
402        GNUNET_i2s (&session->sender));
403   if (GNUNET_YES == session->active)
404   {
405     plugin->env->session_end (plugin->env->cls,
406                               &session->sender,
407                               session);
408     session->active = GNUNET_NO;
409   }
410   while (NULL != (pr = session->pr_head))
411   {
412     GNUNET_CONTAINER_DLL_remove (session->pr_head,
413                                  session->pr_tail,
414                                  pr);
415     GNUNET_DV_send_cancel (pr->th);
416     pr->th = NULL;
417     if (NULL != pr->transmit_cont)
418       pr->transmit_cont (pr->transmit_cont_cls,
419                          &session->sender,
420                          GNUNET_SYSERR, 0, 0);
421     GNUNET_free (pr);
422   }
423   GNUNET_free (session);
424 }
425
426
427 /**
428  * Function called if DV is no longer able to talk to a peer.
429  *
430  * @param cls closure with `struct Plugin *`
431  * @param peer peer that disconnected
432  */
433 static void
434 handle_dv_disconnect (void *cls,
435                       const struct GNUNET_PeerIdentity *peer)
436 {
437   struct Plugin *plugin = cls;
438   struct Session *session;
439
440   LOG (GNUNET_ERROR_TYPE_DEBUG,
441        "Received `%s' message for peer `%s'\n",
442        "DV_DISCONNECT",
443        GNUNET_i2s (peer));
444   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
445                                                peer);
446   if (NULL == session)
447     return; /* nothing to do */
448   free_session (session);
449 }
450
451
452 /**
453  * Function called once the delivery of a message has been successful.
454  * Clean up the pending request, and call continuations.
455  *
456  * @param cls closure
457  * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
458  */
459 static void
460 send_finished (void *cls,
461                int ok)
462 {
463   struct PendingRequest *pr = cls;
464   struct Session *session = pr->session;
465
466   pr->th = NULL;
467   GNUNET_CONTAINER_DLL_remove (session->pr_head,
468                                session->pr_tail,
469                                pr);
470   if (NULL != pr->transmit_cont)
471     pr->transmit_cont (pr->transmit_cont_cls,
472                        &session->sender,
473                        ok, 0, 0);
474   GNUNET_free (pr);
475 }
476
477
478 /**
479  * Function that can be used by the transport service to transmit
480  * a message using the plugin.
481  *
482  * @param cls closure
483  * @param session the session used
484  * @param priority how important is the message
485  * @param msgbuf the message to transmit
486  * @param msgbuf_size number of bytes in 'msgbuf'
487  * @param timeout when should we time out
488  * @param cont continuation to call once the message has
489  *        been transmitted (or if the transport is ready
490  *        for the next transmission call; or if the
491  *        peer disconnected...)
492  * @param cont_cls closure for @a cont
493  * @return number of bytes used (on the physical network, with overheads);
494  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
495  *         and does NOT mean that the message was not transmitted (DV)
496  */
497 static ssize_t
498 dv_plugin_send (void *cls,
499                 struct Session *session,
500                 const char *msgbuf,
501                 size_t msgbuf_size,
502                 unsigned int priority,
503                 struct GNUNET_TIME_Relative timeout,
504                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
505 {
506   struct Plugin *plugin = cls;
507   struct PendingRequest *pr;
508   const struct GNUNET_MessageHeader *msg;
509   struct GNUNET_MessageHeader *box;
510
511   box = NULL;
512   msg = (const struct GNUNET_MessageHeader *) msgbuf;
513   if (ntohs (msg->size) != msgbuf_size)
514   {
515     /* need to box */
516     box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
517     box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
518     box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
519     memcpy (&box[1], msgbuf, msgbuf_size);
520     msg = box;
521   }
522   pr = GNUNET_new (struct PendingRequest);
523   pr->transmit_cont = cont;
524   pr->transmit_cont_cls = cont_cls;
525   pr->session = session;
526   GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
527                                     session->pr_tail,
528                                     pr);
529
530   pr->th = GNUNET_DV_send (plugin->dvh,
531                            &session->sender,
532                            msg ,
533                            &send_finished,
534                            pr);
535   GNUNET_free_non_null (box);
536   return 0; /* DV */
537 }
538
539
540 /**
541  * Function that can be used to force the plugin to disconnect
542  * from the given peer and cancel all previous transmissions
543  * (and their continuations).
544  *
545  * @param cls closure with the `struct Plugin *`
546  * @param target peer from which to disconnect
547  */
548 static void
549 dv_plugin_disconnect_peer (void *cls,
550                            const struct GNUNET_PeerIdentity *target)
551 {
552   struct Plugin *plugin = cls;
553   struct Session *session;
554   struct PendingRequest *pr;
555
556   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
557                                                target);
558   if (NULL == session)
559     return; /* nothing to do */
560   while (NULL != (pr = session->pr_head))
561   {
562     GNUNET_CONTAINER_DLL_remove (session->pr_head,
563                                  session->pr_tail,
564                                  pr);
565     GNUNET_DV_send_cancel (pr->th);
566     pr->th = NULL;
567     if (NULL != pr->transmit_cont)
568       pr->transmit_cont (pr->transmit_cont_cls,
569                          &session->sender,
570                          GNUNET_SYSERR, 0, 0);
571     GNUNET_free (pr);
572   }
573   session->active = GNUNET_NO;
574 }
575
576
577 /**
578  * Function that can be used to force the plugin to disconnect
579  * from the given peer and cancel all previous transmissions
580  * (and their continuations).
581  *
582  * @param cls closure with the `struct Plugin *`
583  * @param session which session to disconnect
584  * @return #GNUNET_OK
585  */
586 static int
587 dv_plugin_disconnect_session (void *cls,
588                               struct Session *session)
589 {
590   struct PendingRequest *pr;
591
592   while (NULL != (pr = session->pr_head))
593   {
594     GNUNET_CONTAINER_DLL_remove (session->pr_head,
595                                  session->pr_tail,
596                                  pr);
597     GNUNET_DV_send_cancel (pr->th);
598     pr->th = NULL;
599     if (NULL != pr->transmit_cont)
600       pr->transmit_cont (pr->transmit_cont_cls,
601                          &session->sender,
602                          GNUNET_SYSERR, 0, 0);
603     GNUNET_free (pr);
604   }
605   session->active = GNUNET_NO;
606   return GNUNET_OK;
607 }
608
609
610 /**
611  * Convert the transports address to a nice, human-readable
612  * format.
613  *
614  * @param cls closure
615  * @param type name of the transport that generated the address
616  * @param addr one of the addresses of the host, NULL for the last address
617  *        the specific address format depends on the transport
618  * @param addrlen length of the address
619  * @param numeric should (IP) addresses be displayed in numeric form?
620  * @param timeout after how long should we give up?
621  * @param asc function to call on each string
622  * @param asc_cls closure for @a asc
623  */
624 static void
625 dv_plugin_address_pretty_printer (void *cls, const char *type,
626                                   const void *addr,
627                                   size_t addrlen, int numeric,
628                                   struct GNUNET_TIME_Relative timeout,
629                                   GNUNET_TRANSPORT_AddressStringCallback asc,
630                                   void *asc_cls)
631 {
632   if ( (0 == addrlen) &&
633        (0 == strcmp (type, "dv")) )
634     asc (asc_cls, "dv");
635   asc (asc_cls, NULL);
636 }
637
638
639 /**
640  * Convert the DV address to a pretty string.
641  *
642  * @param cls closure
643  * @param addr the (hopefully) DV address
644  * @param addrlen the length of the @a addr
645  * @return string representing the DV address
646  */
647 static const char *
648 dv_plugin_address_to_string (void *cls,
649                              const void *addr,
650                              size_t addrlen)
651 {
652   if (0 != addrlen)
653   {
654     GNUNET_break (0); /* malformed */
655     return NULL;
656   }
657   return "dv";
658 }
659
660
661 /**
662  * Another peer has suggested an address for this peer and transport
663  * plugin.  Check that this could be a valid address.  This function
664  * is not expected to 'validate' the address in the sense of trying to
665  * connect to it but simply to see if the binary format is technically
666  * legal for establishing a connection to this peer (and make sure that
667  * the address really corresponds to our network connection/settings
668  * and not some potential man-in-the-middle).
669  *
670  * @param cls closure
671  * @param addr pointer to the address
672  * @param addrlen length of addr
673  * @return #GNUNET_OK if this is a plausible address for this peer
674  *         and transport, #GNUNET_SYSERR if not
675  *
676  */
677 static int
678 dv_plugin_check_address (void *cls,
679                          const void *addr,
680                          size_t addrlen)
681 {
682   if (0 != addrlen)
683     return GNUNET_SYSERR;
684   return GNUNET_OK;
685 }
686
687
688 /**
689  * Create a new session to transmit data to the target
690  * This session will used to send data to this peer and the plugin will
691  * notify us by calling the env->session_end function
692  *
693  * @param cls the plugin
694  * @param address the address
695  * @return the session if the address is valid, NULL otherwise
696  */
697 static struct Session *
698 dv_get_session (void *cls,
699                 const struct GNUNET_HELLO_Address *address)
700 {
701   struct Plugin *plugin = cls;
702   struct Session *session;
703
704   if (0 != address->address_length)
705     return NULL;
706   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
707                                                &address->peer);
708   if (NULL == session)
709     return NULL; /* not valid right now */
710   session->active = GNUNET_YES;
711   return session;
712 }
713
714
715 /**
716  * Function called to convert a string address to
717  * a binary address.
718  *
719  * @param cls closure ('struct Plugin*')
720  * @param addr string address
721  * @param addrlen length of the @a addr including \0 termination
722  * @param buf location to store the buffer
723  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
724  * @param added length of created address
725  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
726  */
727 static int
728 dv_plugin_string_to_address (void *cls,
729                              const char *addr,
730                              uint16_t addrlen,
731                              void **buf,
732                              size_t *added)
733 {
734   if ( (addrlen == 3) &&
735        (0 == strcmp ("dv", addr)) )
736   {
737     *added = 0;
738     return GNUNET_OK;
739   }
740   return GNUNET_SYSERR;
741 }
742
743
744
745 /**
746  * Function to obtain the network type for a session
747  * FIXME: we should probably look at the network type
748  * used by the next hop here.  Or find some other way
749  * to properly allow ATS-DV resource allocation.
750  *
751  * @param cls closure (`struct Plugin *`)
752  * @param session the session
753  * @return the network type
754  */
755 static enum GNUNET_ATS_Network_Type
756 dv_get_network (void *cls,
757                 struct Session *session)
758 {
759   GNUNET_assert (NULL != session);
760   return session->network;
761 }
762
763
764 /**
765  * Entry point for the plugin.
766  *
767  * @param cls closure with the plugin environment
768  * @return plugin API
769  */
770 void *
771 libgnunet_plugin_transport_dv_init (void *cls)
772 {
773   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
774   struct GNUNET_TRANSPORT_PluginFunctions *api;
775   struct Plugin *plugin;
776
777   plugin = GNUNET_new (struct Plugin);
778   plugin->env = env;
779   plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
780   plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
781                                           plugin);
782   plugin->dvh = GNUNET_DV_service_connect (env->cfg,
783                                            plugin,
784                                            &handle_dv_connect,
785                                            &handle_dv_distance_changed,
786                                            &handle_dv_disconnect,
787                                            &handle_dv_message_received);
788   if (NULL == plugin->dvh)
789   {
790     GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
791     GNUNET_SERVER_mst_destroy (plugin->mst);
792     GNUNET_free (plugin);
793     return NULL;
794   }
795   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
796   api->cls = plugin;
797   api->send = &dv_plugin_send;
798   api->disconnect_peer = &dv_plugin_disconnect_peer;
799   api->disconnect_session = &dv_plugin_disconnect_session;
800   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
801   api->check_address = &dv_plugin_check_address;
802   api->address_to_string = &dv_plugin_address_to_string;
803   api->string_to_address = &dv_plugin_string_to_address;
804   api->get_session = &dv_get_session;
805   api->get_network = &dv_get_network;
806   return api;
807 }
808
809
810 /**
811  * Function called to free a session.
812  *
813  * @param cls NULL
814  * @param key unused
815  * @param value session to free
816  * @return #GNUNET_OK (continue to iterate)
817  */
818 static int
819 free_session_iterator (void *cls,
820                        const struct GNUNET_PeerIdentity *key,
821                        void *value)
822 {
823   struct Session *session = value;
824
825   free_session (session);
826   return GNUNET_OK;
827 }
828
829
830 /**
831  * Exit point from the plugin.
832  *
833  * @param cls plugin API
834  * @return NULL
835  */
836 void *
837 libgnunet_plugin_transport_dv_done (void *cls)
838 {
839   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
840   struct Plugin *plugin = api->cls;
841
842   GNUNET_DV_service_disconnect (plugin->dvh);
843   GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
844                                          &free_session_iterator,
845                                          NULL);
846   GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
847   GNUNET_SERVER_mst_destroy (plugin->mst);
848   GNUNET_free (plugin);
849   GNUNET_free (api);
850   return NULL;
851 }
852
853 /* end of plugin_transport_dv.c */