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