-indent, doxygen
[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->address,
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", GNUNET_OK);
653   else
654     asc (asc_cls, NULL, GNUNET_SYSERR);
655
656   asc (asc_cls, NULL, GNUNET_OK);
657 }
658
659
660 /**
661  * Convert the DV address to a pretty string.
662  *
663  * @param cls closure
664  * @param addr the (hopefully) DV address
665  * @param addrlen the length of the @a addr
666  * @return string representing the DV address
667  */
668 static const char *
669 dv_plugin_address_to_string (void *cls,
670                              const void *addr,
671                              size_t addrlen)
672 {
673   if (0 != addrlen)
674   {
675     GNUNET_break (0); /* malformed */
676     return NULL;
677   }
678   return "dv";
679 }
680
681
682 /**
683  * Another peer has suggested an address for this peer and transport
684  * plugin.  Check that this could be a valid address.  This function
685  * is not expected to 'validate' the address in the sense of trying to
686  * connect to it but simply to see if the binary format is technically
687  * legal for establishing a connection to this peer (and make sure that
688  * the address really corresponds to our network connection/settings
689  * and not some potential man-in-the-middle).
690  *
691  * @param cls closure
692  * @param addr pointer to the address
693  * @param addrlen length of @a addr
694  * @return #GNUNET_OK if this is a plausible address for this peer
695  *         and transport, #GNUNET_SYSERR if not
696  *
697  */
698 static int
699 dv_plugin_check_address (void *cls,
700                          const void *addr,
701                          size_t addrlen)
702 {
703   if (0 != addrlen)
704     return GNUNET_SYSERR;
705   return GNUNET_OK;
706 }
707
708
709 /**
710  * Create a new session to transmit data to the target
711  * This session will used to send data to this peer and the plugin will
712  * notify us by calling the env->session_end function
713  *
714  * @param cls the plugin
715  * @param address the address
716  * @return the session if the address is valid, NULL otherwise
717  */
718 static struct Session *
719 dv_get_session (void *cls,
720                 const struct GNUNET_HELLO_Address *address)
721 {
722   struct Plugin *plugin = cls;
723   struct Session *session;
724
725   if (0 != address->address_length)
726     return NULL;
727   session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
728                                                &address->peer);
729   if (NULL == session)
730     return NULL; /* not valid right now */
731   session->active = GNUNET_YES;
732   return session;
733 }
734
735
736 /**
737  * Function called to convert a string address to
738  * a binary address.
739  *
740  * @param cls closure ('struct Plugin*')
741  * @param addr string address
742  * @param addrlen length of the @a addr including \0 termination
743  * @param buf location to store the buffer
744  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
745  * @param added length of created address
746  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
747  */
748 static int
749 dv_plugin_string_to_address (void *cls,
750                              const char *addr,
751                              uint16_t addrlen,
752                              void **buf,
753                              size_t *added)
754 {
755   if ( (addrlen == 3) &&
756        (0 == strcmp ("dv", addr)) )
757   {
758     *added = 0;
759     return GNUNET_OK;
760   }
761   return GNUNET_SYSERR;
762 }
763
764
765 /**
766  * Function that will be called whenever the transport service wants to
767  * notify the plugin that a session is still active and in use and
768  * therefore the session timeout for this session has to be updated
769  *
770  * @param cls closure (`struct Plugin *`)
771  * @param peer which peer was the session for
772  * @param session which session is being updated
773  */
774 static void
775 dv_plugin_update_session_timeout (void *cls,
776                                   const struct GNUNET_PeerIdentity *peer,
777                                   struct Session *session)
778 {
779   /* DV currently doesn't time out like "normal" plugins,
780      so it should be safe to do nothing, right?
781      (or should we add an internal timeout?) */
782 }
783
784
785 /**
786  * Function to obtain the network type for a session
787  * FIXME: we should probably look at the network type
788  * used by the next hop here.  Or find some other way
789  * to properly allow ATS-DV resource allocation.
790  *
791  * @param cls closure (`struct Plugin *`)
792  * @param session the session
793  * @return the network type
794  */
795 static enum GNUNET_ATS_Network_Type
796 dv_get_network (void *cls,
797                 struct Session *session)
798 {
799   GNUNET_assert (NULL != session);
800   return session->network;
801 }
802
803
804 /**
805  * Function that is called to get the keepalive factor.
806  * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
807  * calculate the interval between keepalive packets.
808  *
809  * @param cls closure with the `struct Plugin`
810  * @return keepalive factor
811  */
812 static unsigned int
813 dv_plugin_query_keepalive_factor (void *cls)
814 {
815   return 3;
816 }
817
818
819 /**
820  * Entry point for the plugin.
821  *
822  * @param cls closure with the plugin environment
823  * @return plugin API
824  */
825 void *
826 libgnunet_plugin_transport_dv_init (void *cls)
827 {
828   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
829   struct GNUNET_TRANSPORT_PluginFunctions *api;
830   struct Plugin *plugin;
831
832   plugin = GNUNET_new (struct Plugin);
833   plugin->env = env;
834   plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
835   plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
836                                           plugin);
837   plugin->dvh = GNUNET_DV_service_connect (env->cfg,
838                                            plugin,
839                                            &handle_dv_connect,
840                                            &handle_dv_distance_changed,
841                                            &handle_dv_disconnect,
842                                            &handle_dv_message_received);
843   if (NULL == plugin->dvh)
844   {
845     GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
846     GNUNET_SERVER_mst_destroy (plugin->mst);
847     GNUNET_free (plugin);
848     return NULL;
849   }
850   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
851   api->cls = plugin;
852   api->send = &dv_plugin_send;
853   api->disconnect_peer = &dv_plugin_disconnect_peer;
854   api->disconnect_session = &dv_plugin_disconnect_session;
855   api->address_pretty_printer = &dv_plugin_address_pretty_printer;
856   api->check_address = &dv_plugin_check_address;
857   api->address_to_string = &dv_plugin_address_to_string;
858   api->string_to_address = &dv_plugin_string_to_address;
859   api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
860   api->get_session = &dv_get_session;
861   api->get_network = &dv_get_network;
862   api->update_session_timeout = &dv_plugin_update_session_timeout;
863   return api;
864 }
865
866
867 /**
868  * Function called to free a session.
869  *
870  * @param cls NULL
871  * @param key unused
872  * @param value session to free
873  * @return #GNUNET_OK (continue to iterate)
874  */
875 static int
876 free_session_iterator (void *cls,
877                        const struct GNUNET_PeerIdentity *key,
878                        void *value)
879 {
880   struct Session *session = value;
881
882   free_session (session);
883   return GNUNET_OK;
884 }
885
886
887 /**
888  * Exit point from the plugin.
889  *
890  * @param cls plugin API
891  * @return NULL
892  */
893 void *
894 libgnunet_plugin_transport_dv_done (void *cls)
895 {
896   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
897   struct Plugin *plugin = api->cls;
898
899   GNUNET_DV_service_disconnect (plugin->dvh);
900   GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
901                                          &free_session_iterator,
902                                          NULL);
903   GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
904   GNUNET_SERVER_mst_destroy (plugin->mst);
905   GNUNET_free (plugin);
906   GNUNET_free (api);
907   return NULL;
908 }
909
910 /* end of plugin_transport_dv.c */