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