xvine:fixes
[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 @e 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    * Number of bytes to transmit.
89    */
90   size_t size;
91 };
92
93
94 /**
95  * Session handle for connections.
96  */
97 struct Session
98 {
99   /**
100    * Pointer to the global plugin struct.
101    */
102   struct Plugin *plugin;
103
104   /**
105    * Head of pending requests.
106    */
107   struct PendingRequest *pr_head;
108
109   /**
110    * Tail of pending requests.
111    */
112   struct PendingRequest *pr_tail;
113
114   struct GNUNET_HELLO_Address *address;
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->address, session, &ats, 1);
191 }
192
193
194 /**
195  * Function called by MST on each message from the box.
196  *
197  * @param cls closure with the `struct Plugin *`
198  * @param client identification of the client (with the 'struct Session')
199  * @param message the actual message
200  * @return #GNUNET_OK on success
201  */
202 static int
203 unbox_cb (void *cls,
204           void *client,
205           const struct GNUNET_MessageHeader *message)
206 {
207   struct Plugin *plugin = cls;
208   struct Session *session = client;
209   struct GNUNET_ATS_Information ats;
210
211   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
212   ats.value = htonl (session->distance);
213   session->active = GNUNET_YES;
214   LOG (GNUNET_ERROR_TYPE_DEBUG,
215        "Delivering message of type %u with %u bytes from peer `%s'\n",
216        ntohs (message->type),
217        ntohs (message->size),
218        GNUNET_i2s (&session->sender));
219
220   plugin->env->receive (plugin->env->cls, session->address, session,
221                         message);
222   plugin->env->update_address_metrics (plugin->env->cls,
223       session->address, session, &ats, 1);
224   return GNUNET_OK;
225 }
226
227
228 /**
229  * Handler for messages received from the DV service.
230  *
231  * @param cls closure with the plugin
232  * @param sender sender of the message
233  * @param distance how far did the message travel
234  * @param msg actual message payload
235  */
236 static void
237 handle_dv_message_received (void *cls,
238                             const struct GNUNET_PeerIdentity *sender,
239                             uint32_t distance,
240                             const struct GNUNET_MessageHeader *msg)
241 {
242   struct Plugin *plugin = cls;
243   struct GNUNET_ATS_Information ats;
244   struct Session *session;
245
246   LOG (GNUNET_ERROR_TYPE_DEBUG,
247        "Received DV_MESSAGE_RECEIVED message for peer `%s': new distance %u\n",
248        GNUNET_i2s (sender), distance);
249   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
250                                                sender);
251   if (NULL == session)
252   {
253     GNUNET_break (0);
254     return;
255   }
256   if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
257   {
258     /* need to unbox using MST */
259     LOG (GNUNET_ERROR_TYPE_DEBUG,
260          "Unboxing DV message using MST\n");
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   LOG (GNUNET_ERROR_TYPE_DEBUG,
273        "Delivering message of type %u with %u bytes from peer `%s'\n",
274        ntohs (msg->type),
275        ntohs (msg->size),
276        GNUNET_i2s (sender));
277   plugin->env->receive (plugin->env->cls, session->address, session, msg);
278   plugin->env->update_address_metrics (plugin->env->cls,
279       session->address, session, &ats, 1);
280 }
281
282
283 /**
284  * Function called if DV starts to be able to talk to a peer.
285  *
286  * @param cls closure with `struct Plugin *`
287  * @param peer newly connected peer
288  * @param distance distance to the peer
289  * @param network the network the next hop is located in
290  */
291 static void
292 handle_dv_connect (void *cls,
293                    const struct GNUNET_PeerIdentity *peer,
294                    uint32_t distance,
295                    enum GNUNET_ATS_Network_Type network)
296 {
297   struct Plugin *plugin = cls;
298   struct Session *session;
299   struct GNUNET_ATS_Information ats[2];
300
301   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
302   /**
303    * This requires transport plugin to be linked to libgnunetats.
304    * If you remove it, also remove libgnunetats linkage from Makefile.am
305    */
306   LOG (GNUNET_ERROR_TYPE_DEBUG,
307        "Received `%s' message for peer `%s' with next hop in network %s\n",
308        "DV_CONNECT",
309        GNUNET_i2s (peer),
310        GNUNET_ATS_print_network_type (network));
311
312   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
313                                                peer);
314   if (NULL != session)
315   {
316     GNUNET_break (0);
317     session->distance = distance;
318     notify_distance_change (session);
319     return; /* nothing to do */
320   }
321
322   session = GNUNET_new (struct Session);
323   session->address = GNUNET_HELLO_address_allocate (peer, PLUGIN_NAME,
324       NULL, 0, GNUNET_HELLO_ADDRESS_INFO_NONE);
325   session->sender = *peer;
326   session->plugin = plugin;
327   session->distance = distance;
328   session->network = network;
329   GNUNET_assert(GNUNET_YES ==
330                 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
331                                                    &session->sender, session,
332                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
333
334   LOG (GNUNET_ERROR_TYPE_DEBUG,
335        "Creating new DV session %p for peer `%s' at distance %u\n",
336        session,
337        GNUNET_i2s (peer),
338        distance);
339
340   /* Notify transport and ats about new connection */
341   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
342   ats[0].value = htonl (distance);
343   ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
344   ats[1].value = htonl ((uint32_t) network);
345   session->active = GNUNET_YES;
346   plugin->env->session_start (plugin->env->cls, session->address,
347                               session, ats, 2);
348 }
349
350
351 /**
352  * Function called if DV distance to a peer is changed.
353  *
354  * @param cls closure with `struct Plugin *`
355  * @param peer connected peer
356  * @param distance new distance to the peer
357  * @param network network type used for the connection
358  */
359 static void
360 handle_dv_distance_changed (void *cls,
361                             const struct GNUNET_PeerIdentity *peer,
362                             uint32_t distance,
363                             enum GNUNET_ATS_Network_Type network)
364 {
365   struct Plugin *plugin = cls;
366   struct Session *session;
367
368   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
369   LOG (GNUNET_ERROR_TYPE_DEBUG,
370        "Received `%s' message for peer `%s': new distance %u\n",
371        "DV_DISTANCE_CHANGED",
372        GNUNET_i2s (peer),
373        distance);
374   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
375                                                peer);
376   if (NULL == session)
377   {
378     GNUNET_break (0);
379     handle_dv_connect (plugin, peer, distance, network);
380     return;
381   }
382   session->distance = distance;
383   notify_distance_change (session);
384 }
385
386
387 /**
388  * Release session object and clean up associated resources.
389  *
390  * @param session session to clean up
391  */
392 static void
393 free_session (struct Session *session)
394 {
395   struct Plugin *plugin = session->plugin;
396   struct PendingRequest *pr;
397
398   GNUNET_assert (GNUNET_YES ==
399                  GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
400                                                        &session->sender,
401                                                        session));
402
403   LOG (GNUNET_ERROR_TYPE_DEBUG,
404        "Freeing session %p for peer `%s'\n",
405        session,
406        GNUNET_i2s (&session->sender));
407   if (GNUNET_YES == session->active)
408   {
409     plugin->env->session_end (plugin->env->cls,
410                               session->address,
411                               session);
412     session->active = GNUNET_NO;
413   }
414   while (NULL != (pr = session->pr_head))
415   {
416     GNUNET_CONTAINER_DLL_remove (session->pr_head,
417                                  session->pr_tail,
418                                  pr);
419     GNUNET_DV_send_cancel (pr->th);
420     pr->th = NULL;
421     if (NULL != pr->transmit_cont)
422       pr->transmit_cont (pr->transmit_cont_cls,
423                          &session->sender,
424                          GNUNET_SYSERR,
425                          pr->size, 0);
426     GNUNET_free (pr);
427   }
428   GNUNET_HELLO_address_free (session->address);
429   GNUNET_free (session);
430 }
431
432
433 /**
434  * Function called if DV is no longer able to talk to a peer.
435  *
436  * @param cls closure with `struct Plugin *`
437  * @param peer peer that disconnected
438  */
439 static void
440 handle_dv_disconnect (void *cls,
441                       const struct GNUNET_PeerIdentity *peer)
442 {
443   struct Plugin *plugin = cls;
444   struct Session *session;
445
446   LOG (GNUNET_ERROR_TYPE_DEBUG,
447        "Received `%s' message for peer `%s'\n",
448        "DV_DISCONNECT",
449        GNUNET_i2s (peer));
450   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
451                                                peer);
452   if (NULL == session)
453     return; /* nothing to do */
454   free_session (session);
455 }
456
457
458 /**
459  * Function called once the delivery of a message has been successful.
460  * Clean up the pending request, and call continuations.
461  *
462  * @param cls closure
463  * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
464  */
465 static void
466 send_finished (void *cls,
467                int ok)
468 {
469   struct PendingRequest *pr = cls;
470   struct Session *session = pr->session;
471
472   pr->th = NULL;
473   GNUNET_CONTAINER_DLL_remove (session->pr_head,
474                                session->pr_tail,
475                                pr);
476   if (NULL != pr->transmit_cont)
477     pr->transmit_cont (pr->transmit_cont_cls,
478                        &session->sender,
479                        ok,
480                        pr->size, 0);
481   GNUNET_free (pr);
482 }
483
484
485 /**
486  * Function that can be used by the transport service to transmit
487  * a message using the plugin.
488  *
489  * @param cls closure
490  * @param session the session used
491  * @param priority how important is the message
492  * @param msgbuf the message to transmit
493  * @param msgbuf_size number of bytes in 'msgbuf'
494  * @param timeout when should we time out
495  * @param cont continuation to call once the message has
496  *        been transmitted (or if the transport is ready
497  *        for the next transmission call; or if the
498  *        peer disconnected...)
499  * @param cont_cls closure for @a cont
500  * @return number of bytes used (on the physical network, with overheads);
501  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
502  *         and does NOT mean that the message was not transmitted (DV)
503  */
504 static ssize_t
505 dv_plugin_send (void *cls,
506                 struct Session *session,
507                 const char *msgbuf,
508                 size_t msgbuf_size,
509                 unsigned int priority,
510                 struct GNUNET_TIME_Relative timeout,
511                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
512 {
513   struct Plugin *plugin = cls;
514   struct PendingRequest *pr;
515   const struct GNUNET_MessageHeader *msg;
516   struct GNUNET_MessageHeader *box;
517
518   box = NULL;
519   msg = (const struct GNUNET_MessageHeader *) msgbuf;
520   if (ntohs (msg->size) != msgbuf_size)
521   {
522     /* need to box */
523     LOG (GNUNET_ERROR_TYPE_DEBUG,
524          "Boxing DV message\n");
525     box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
526     box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
527     box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
528     memcpy (&box[1], msgbuf, msgbuf_size);
529     msg = box;
530   }
531   pr = GNUNET_new (struct PendingRequest);
532   pr->transmit_cont = cont;
533   pr->transmit_cont_cls = cont_cls;
534   pr->session = session;
535   pr->size = msgbuf_size;
536   GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
537                                     session->pr_tail,
538                                     pr);
539
540   pr->th = GNUNET_DV_send (plugin->dvh,
541                            &session->sender,
542                            msg,
543                            &send_finished,
544                            pr);
545   GNUNET_free_non_null (box);
546   return 0; /* DV */
547 }
548
549
550 /**
551  * Function that can be used to force the plugin to disconnect
552  * from the given peer and cancel all previous transmissions
553  * (and their continuations).
554  *
555  * @param cls closure with the `struct Plugin *`
556  * @param target peer from which to disconnect
557  */
558 static void
559 dv_plugin_disconnect_peer (void *cls,
560                            const struct GNUNET_PeerIdentity *target)
561 {
562   struct Plugin *plugin = cls;
563   struct Session *session;
564   struct PendingRequest *pr;
565
566   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
567                                                target);
568   if (NULL == session)
569     return; /* nothing to do */
570   while (NULL != (pr = session->pr_head))
571   {
572     GNUNET_CONTAINER_DLL_remove (session->pr_head,
573                                  session->pr_tail,
574                                  pr);
575     GNUNET_DV_send_cancel (pr->th);
576     pr->th = NULL;
577     if (NULL != pr->transmit_cont)
578       pr->transmit_cont (pr->transmit_cont_cls,
579                          &session->sender,
580                          GNUNET_SYSERR,
581                          pr->size, 0);
582     GNUNET_free (pr);
583   }
584   session->active = GNUNET_NO;
585 }
586
587
588 /**
589  * Function that can be used to force the plugin to disconnect
590  * from the given peer and cancel all previous transmissions
591  * (and their continuations).
592  *
593  * @param cls closure with the `struct Plugin *`
594  * @param session which session to disconnect
595  * @return #GNUNET_OK
596  */
597 static int
598 dv_plugin_disconnect_session (void *cls,
599                               struct Session *session)
600 {
601   struct PendingRequest *pr;
602
603   while (NULL != (pr = session->pr_head))
604   {
605     GNUNET_CONTAINER_DLL_remove (session->pr_head,
606                                  session->pr_tail,
607                                  pr);
608     GNUNET_DV_send_cancel (pr->th);
609     pr->th = NULL;
610     if (NULL != pr->transmit_cont)
611       pr->transmit_cont (pr->transmit_cont_cls,
612                          &session->sender,
613                          GNUNET_SYSERR,
614                          pr->size, 0);
615     GNUNET_free (pr);
616   }
617   session->active = GNUNET_NO;
618   return GNUNET_OK;
619 }
620
621
622 /**
623  * Convert the transports address to a nice, human-readable
624  * format.
625  *
626  * @param cls closure
627  * @param type name of the transport that generated the address
628  * @param addr one of the addresses of the host, NULL for the last address
629  *        the specific address format depends on the transport
630  * @param addrlen length of the address
631  * @param numeric should (IP) addresses be displayed in numeric form?
632  * @param timeout after how long should we give up?
633  * @param asc function to call on each string
634  * @param asc_cls closure for @a asc
635  */
636 static void
637 dv_plugin_address_pretty_printer (void *cls, const char *type,
638                                   const void *addr,
639                                   size_t addrlen, int numeric,
640                                   struct GNUNET_TIME_Relative timeout,
641                                   GNUNET_TRANSPORT_AddressStringCallback asc,
642                                   void *asc_cls)
643 {
644   if ( (0 == addrlen) &&
645        (0 == strcmp (type, "dv")) )
646     asc (asc_cls,
647          "dv",
648          GNUNET_OK);
649   else
650     asc (asc_cls,
651          NULL,
652          GNUNET_SYSERR);
653   asc (asc_cls,
654        NULL,
655        GNUNET_OK);
656 }
657
658
659 /**
660  * Convert the DV address to a pretty string.
661  *
662  * @param cls closure
663  * @param addr the (hopefully) DV address
664  * @param addrlen the length of the @a addr
665  * @return string representing the DV address
666  */
667 static const char *
668 dv_plugin_address_to_string (void *cls,
669                              const void *addr,
670                              size_t addrlen)
671 {
672   if (0 != addrlen)
673   {
674     GNUNET_break (0); /* malformed */
675     return NULL;
676   }
677   return "dv";
678 }
679
680
681 /**
682  * Another peer has suggested an address for this peer and transport
683  * plugin.  Check that this could be a valid address.  This function
684  * is not expected to 'validate' the address in the sense of trying to
685  * connect to it but simply to see if the binary format is technically
686  * legal for establishing a connection to this peer (and make sure that
687  * the address really corresponds to our network connection/settings
688  * and not some potential man-in-the-middle).
689  *
690  * @param cls closure
691  * @param addr pointer to the address
692  * @param addrlen length of @a addr
693  * @return #GNUNET_OK if this is a plausible address for this peer
694  *         and transport, #GNUNET_SYSERR if not
695  *
696  */
697 static int
698 dv_plugin_check_address (void *cls,
699                          const void *addr,
700                          size_t addrlen)
701 {
702   if (0 != addrlen)
703     return GNUNET_SYSERR;
704   return GNUNET_OK;
705 }
706
707
708 /**
709  * Create a new session to transmit data to the target
710  * This session will used to send data to this peer and the plugin will
711  * notify us by calling the env->session_end function
712  *
713  * @param cls the plugin
714  * @param address the address
715  * @return the session if the address is valid, NULL otherwise
716  */
717 static struct Session *
718 dv_get_session (void *cls,
719                 const struct GNUNET_HELLO_Address *address)
720 {
721   struct Plugin *plugin = cls;
722   struct Session *session;
723
724   if (0 != address->address_length)
725     return NULL;
726   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
727                                                &address->peer);
728   if (NULL == session)
729     return NULL; /* not valid right now */
730   session->active = GNUNET_YES;
731   return session;
732 }
733
734
735 /**
736  * Function called to convert a string address to
737  * a binary address.
738  *
739  * @param cls closure ('struct Plugin*')
740  * @param addr string address
741  * @param addrlen length of the @a addr including \0 termination
742  * @param buf location to store the buffer
743  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
744  * @param added length of created address
745  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
746  */
747 static int
748 dv_plugin_string_to_address (void *cls,
749                              const char *addr,
750                              uint16_t addrlen,
751                              void **buf,
752                              size_t *added)
753 {
754   if ( (addrlen == 3) &&
755        (0 == strcmp ("dv", addr)) )
756   {
757     *added = 0;
758     return GNUNET_OK;
759   }
760   return GNUNET_SYSERR;
761 }
762
763
764 /**
765  * Function that will be called whenever the transport service wants to
766  * notify the plugin that a session is still active and in use and
767  * therefore the session timeout for this session has to be updated
768  *
769  * @param cls closure (`struct Plugin *`)
770  * @param peer which peer was the session for
771  * @param session which session is being updated
772  */
773 static void
774 dv_plugin_update_session_timeout (void *cls,
775                                   const struct GNUNET_PeerIdentity *peer,
776                                   struct Session *session)
777 {
778   /* DV currently doesn't time out like "normal" plugins,
779      so it should be safe to do nothing, right?
780      (or should we add an internal timeout?) */
781 }
782
783
784 /**
785  * Function to obtain the network type for a session
786  * FIXME: we should probably look at the network type
787  * used by the next hop here.  Or find some other way
788  * to properly allow ATS-DV resource allocation.
789  *
790  * @param cls closure (`struct Plugin *`)
791  * @param session the session
792  * @return the network type
793  */
794 static enum GNUNET_ATS_Network_Type
795 dv_get_network (void *cls,
796                 struct Session *session)
797 {
798   GNUNET_assert (NULL != session);
799   return session->network;
800 }
801
802
803 /**
804  * Function that is called to get the keepalive factor.
805  * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
806  * calculate the interval between keepalive packets.
807  *
808  * @param cls closure with the `struct Plugin`
809  * @return keepalive factor
810  */
811 static unsigned int
812 dv_plugin_query_keepalive_factor (void *cls)
813 {
814   return 3;
815 }
816
817
818 /**
819  * Entry point for the plugin.
820  *
821  * @param cls closure with the plugin environment
822  * @return plugin API
823  */
824 void *
825 libgnunet_plugin_transport_dv_init (void *cls)
826 {
827   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
828   struct GNUNET_TRANSPORT_PluginFunctions *api;
829   struct Plugin *plugin;
830
831   plugin = GNUNET_new (struct Plugin);
832   plugin->env = env;
833   plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
834   plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
835                                           plugin);
836   plugin->dvh = GNUNET_DV_service_connect (env->cfg,
837                                            plugin,
838                                            &handle_dv_connect,
839                                            &handle_dv_distance_changed,
840                                            &handle_dv_disconnect,
841                                            &handle_dv_message_received);
842   if (NULL == plugin->dvh)
843   {
844     GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
845     GNUNET_SERVER_mst_destroy (plugin->mst);
846     GNUNET_free (plugin);
847     return NULL;
848   }
849   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
850   api->cls = plugin;
851   api->send = &dv_plugin_send;
852   api->disconnect_peer = &dv_plugin_disconnect_peer;
853   api->disconnect_session = &dv_plugin_disconnect_session;
854   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
855   api->check_address = &dv_plugin_check_address;
856   api->address_to_string = &dv_plugin_address_to_string;
857   api->string_to_address = &dv_plugin_string_to_address;
858   api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
859   api->get_session = &dv_get_session;
860   api->get_network = &dv_get_network;
861   api->update_session_timeout = &dv_plugin_update_session_timeout;
862   return api;
863 }
864
865
866 /**
867  * Function called to free a session.
868  *
869  * @param cls NULL
870  * @param key unused
871  * @param value session to free
872  * @return #GNUNET_OK (continue to iterate)
873  */
874 static int
875 free_session_iterator (void *cls,
876                        const struct GNUNET_PeerIdentity *key,
877                        void *value)
878 {
879   struct Session *session = value;
880
881   free_session (session);
882   return GNUNET_OK;
883 }
884
885
886 /**
887  * Exit point from the plugin.
888  *
889  * @param cls plugin API
890  * @return NULL
891  */
892 void *
893 libgnunet_plugin_transport_dv_done (void *cls)
894 {
895   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
896   struct Plugin *plugin = api->cls;
897
898   GNUNET_DV_service_disconnect (plugin->dvh);
899   GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
900                                          &free_session_iterator,
901                                          NULL);
902   GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
903   GNUNET_SERVER_mst_destroy (plugin->mst);
904   GNUNET_free (plugin);
905   GNUNET_free (api);
906   return NULL;
907 }
908
909 /* end of plugin_transport_dv.c */