2 This file is part of GNUnet
3 Copyright (C) 2002--2014 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file dv/plugin_transport_dv.c
23 * @brief DV transport service, takes incoming DV requests and deals with
25 * @author Nathan Evans
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_dv_service.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_transport_plugin.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-dv",__VA_ARGS__)
42 * Encapsulation of all of the state of the plugin.
48 * An active request for transmission via DV.
56 struct PendingRequest *next;
61 struct PendingRequest *prev;
64 * Continuation function to call once the transmission buffer
65 * has again space available. NULL if there is no
66 * continuation to call.
68 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
71 * Closure for @e transmit_cont.
73 void *transmit_cont_cls;
76 * Transmission handle from DV client library.
78 struct GNUNET_DV_TransmitHandle *th;
81 * Session of this request.
83 struct GNUNET_ATS_Session *session;
86 * Number of bytes to transmit.
93 * Session handle for connections.
95 struct GNUNET_ATS_Session
98 * Pointer to the global plugin struct.
100 struct Plugin *plugin;
103 * Head of pending requests.
105 struct PendingRequest *pr_head;
108 * Tail of pending requests.
110 struct PendingRequest *pr_tail;
113 * Address we use for the other peer.
115 struct GNUNET_HELLO_Address *address;
118 * To whom are we talking to.
120 struct GNUNET_PeerIdentity sender;
123 * Number of bytes waiting for transmission to this peer.
124 * FIXME: not set yet.
126 unsigned long long bytes_in_queue;
129 * Number of messages waiting for transmission to this peer.
130 * FIXME: not set yet.
132 unsigned int msgs_in_queue;
135 * Current distance to the given peer.
140 * Current network the next hop peer is located in
142 enum GNUNET_ATS_Network_Type network;
145 * Does the transport service know about this session (and we thus
146 * need to call `session_end` when it is released?)
154 * Encapsulation of all of the state of the plugin.
161 struct GNUNET_TRANSPORT_PluginEnvironment *env;
164 * Hash map of sessions (active and inactive).
166 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
169 * Copy of the handler array where the closures are
170 * set to this struct's instance.
172 struct GNUNET_SERVER_MessageHandler *handlers;
175 * Handle to the DV service
177 struct GNUNET_DV_ServiceHandle *dvh;
180 * Tokenizer for boxed messages.
182 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
185 * Function to call about session status changes.
187 GNUNET_TRANSPORT_SessionInfoCallback sic;
190 * Closure for @e sic.
197 * If a session monitor is attached, notify it about the new
200 * @param plugin our plugin
201 * @param session session that changed state
202 * @param state new state of the session
205 notify_session_monitor (struct Plugin *plugin,
206 struct GNUNET_ATS_Session *session,
207 enum GNUNET_TRANSPORT_SessionState state)
209 struct GNUNET_TRANSPORT_SessionInfo info;
211 if (NULL == plugin->sic)
213 memset (&info, 0, sizeof (info));
215 info.is_inbound = GNUNET_SYSERR; /* hard to say */
216 info.num_msg_pending = session->msgs_in_queue;
217 info.num_bytes_pending = session->bytes_in_queue;
218 /* info.receive_delay remains zero as this is not supported by DV
219 (cannot selectively not receive from 'core') */
220 info.session_timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
221 info.address = session->address;
222 plugin->sic (plugin->sic_cls,
229 * Notify transport service about the change in distance.
231 * @param session session where the distance changed
234 notify_distance_change (struct GNUNET_ATS_Session *session)
236 struct Plugin *plugin = session->plugin;
238 if (GNUNET_YES != session->active)
240 plugin->env->update_address_distance (plugin->env->cls,
247 * Function called by MST on each message from the box.
249 * @param cls closure with the `struct Plugin *`
250 * @param client identification of the client (with the 'struct GNUNET_ATS_Session')
251 * @param message the actual message
252 * @return #GNUNET_OK on success
257 const struct GNUNET_MessageHeader *message)
259 struct Plugin *plugin = cls;
260 struct GNUNET_ATS_Session *session = client;
262 session->active = GNUNET_YES;
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Delivering message of type %u with %u bytes from peer `%s'\n",
265 ntohs (message->type),
266 ntohs (message->size),
267 GNUNET_i2s (&session->sender));
268 plugin->env->receive (plugin->env->cls,
272 plugin->env->update_address_distance (plugin->env->cls,
280 * Handler for messages received from the DV service.
282 * @param cls closure with the plugin
283 * @param sender sender of the message
284 * @param distance how far did the message travel
285 * @param msg actual message payload
288 handle_dv_message_received (void *cls,
289 const struct GNUNET_PeerIdentity *sender,
291 const struct GNUNET_MessageHeader *msg)
293 struct Plugin *plugin = cls;
294 struct GNUNET_ATS_Session *session;
296 LOG (GNUNET_ERROR_TYPE_DEBUG,
297 "Received DV_MESSAGE_RECEIVED message for peer `%s': new distance %u\n",
300 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
307 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
309 /* need to unbox using MST */
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "Unboxing DV message using MST\n");
312 GNUNET_SERVER_mst_receive (plugin->mst,
314 (const char *) &msg[1],
315 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
320 session->active = GNUNET_YES;
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "Delivering message of type %u with %u bytes from peer `%s'\n",
325 GNUNET_i2s (sender));
326 plugin->env->receive (plugin->env->cls,
330 plugin->env->update_address_distance (plugin->env->cls,
337 * Function called if DV starts to be able to talk to a peer.
339 * @param cls closure with `struct Plugin *`
340 * @param peer newly connected peer
341 * @param distance distance to the peer
342 * @param network the network the next hop is located in
345 handle_dv_connect (void *cls,
346 const struct GNUNET_PeerIdentity *peer,
348 enum GNUNET_ATS_Network_Type network)
350 struct Plugin *plugin = cls;
351 struct GNUNET_ATS_Session *session;
353 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
355 * This requires transport plugin to be linked to libgnunetats.
356 * If you remove it, also remove libgnunetats linkage from Makefile.am
358 LOG (GNUNET_ERROR_TYPE_DEBUG,
359 "Received DV_CONNECT message for peer `%s' with next hop in network %s\n",
361 GNUNET_ATS_print_network_type (network));
363 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
368 session->distance = distance;
369 notify_distance_change (session);
370 return; /* nothing to do */
373 session = GNUNET_new (struct GNUNET_ATS_Session);
374 session->address = GNUNET_HELLO_address_allocate (peer, "dv",
376 GNUNET_HELLO_ADDRESS_INFO_NONE);
377 session->sender = *peer;
378 session->plugin = plugin;
379 session->distance = distance;
380 session->network = network;
381 GNUNET_assert(GNUNET_YES ==
382 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
383 &session->sender, session,
384 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "Creating new DV session %p for peer `%s' at distance %u\n",
392 session->active = GNUNET_YES;
393 plugin->env->session_start (plugin->env->cls,
397 plugin->env->update_address_distance (plugin->env->cls,
401 notify_session_monitor (session->plugin,
403 GNUNET_TRANSPORT_SS_UP);
408 * Function called if DV distance to a peer is changed.
410 * @param cls closure with `struct Plugin *`
411 * @param peer connected peer
412 * @param distance new distance to the peer
413 * @param network network type used for the connection
416 handle_dv_distance_changed (void *cls,
417 const struct GNUNET_PeerIdentity *peer,
419 enum GNUNET_ATS_Network_Type network)
421 struct Plugin *plugin = cls;
422 struct GNUNET_ATS_Session *session;
424 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
425 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Received `%s' message for peer `%s': new distance %u\n",
427 "DV_DISTANCE_CHANGED",
430 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
435 handle_dv_connect (plugin, peer, distance, network);
438 session->distance = distance;
439 notify_distance_change (session);
444 * Release session object and clean up associated resources.
446 * @param session session to clean up
449 free_session (struct GNUNET_ATS_Session *session)
451 struct Plugin *plugin = session->plugin;
452 struct PendingRequest *pr;
454 GNUNET_assert (GNUNET_YES ==
455 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Freeing session %p for peer `%s'\n",
462 GNUNET_i2s (&session->sender));
463 if (GNUNET_YES == session->active)
465 notify_session_monitor (session->plugin,
467 GNUNET_TRANSPORT_SS_DONE);
468 plugin->env->session_end (plugin->env->cls,
471 session->active = GNUNET_NO;
473 while (NULL != (pr = session->pr_head))
475 GNUNET_CONTAINER_DLL_remove (session->pr_head,
478 GNUNET_DV_send_cancel (pr->th);
480 if (NULL != pr->transmit_cont)
481 pr->transmit_cont (pr->transmit_cont_cls,
487 GNUNET_HELLO_address_free (session->address);
488 GNUNET_free (session);
493 * Function called if DV is no longer able to talk to a peer.
495 * @param cls closure with `struct Plugin *`
496 * @param peer peer that disconnected
499 handle_dv_disconnect (void *cls,
500 const struct GNUNET_PeerIdentity *peer)
502 struct Plugin *plugin = cls;
503 struct GNUNET_ATS_Session *session;
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Received `%s' message for peer `%s'\n",
509 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
512 return; /* nothing to do */
513 free_session (session);
518 * Function called once the delivery of a message has been successful.
519 * Clean up the pending request, and call continuations.
524 send_finished (void *cls)
526 struct PendingRequest *pr = cls;
527 struct GNUNET_ATS_Session *session = pr->session;
530 GNUNET_CONTAINER_DLL_remove (session->pr_head,
533 if (NULL != pr->transmit_cont)
534 pr->transmit_cont (pr->transmit_cont_cls,
543 * Function that can be used by the transport service to transmit
544 * a message using the plugin.
547 * @param session the session used
548 * @param priority how important is the message
549 * @param msgbuf the message to transmit
550 * @param msgbuf_size number of bytes in 'msgbuf'
551 * @param timeout when should we time out
552 * @param cont continuation to call once the message has
553 * been transmitted (or if the transport is ready
554 * for the next transmission call; or if the
555 * peer disconnected...)
556 * @param cont_cls closure for @a cont
557 * @return number of bytes used (on the physical network, with overheads);
558 * -1 on hard errors (i.e. address invalid); 0 is a legal value
559 * and does NOT mean that the message was not transmitted (DV)
562 dv_plugin_send (void *cls,
563 struct GNUNET_ATS_Session *session,
566 unsigned int priority,
567 struct GNUNET_TIME_Relative timeout,
568 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
570 struct Plugin *plugin = cls;
571 struct PendingRequest *pr;
572 const struct GNUNET_MessageHeader *msg;
573 struct GNUNET_MessageHeader *box;
576 msg = (const struct GNUNET_MessageHeader *) msgbuf;
577 if (ntohs (msg->size) != msgbuf_size)
580 LOG (GNUNET_ERROR_TYPE_DEBUG,
581 "Boxing DV message\n");
582 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
583 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
584 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
585 memcpy (&box[1], msgbuf, msgbuf_size);
588 pr = GNUNET_new (struct PendingRequest);
589 pr->transmit_cont = cont;
590 pr->transmit_cont_cls = cont_cls;
591 pr->session = session;
592 pr->size = msgbuf_size;
593 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
597 pr->th = GNUNET_DV_send (plugin->dvh,
602 GNUNET_free_non_null (box);
608 * Function that can be used to force the plugin to disconnect
609 * from the given peer and cancel all previous transmissions
610 * (and their continuations).
612 * @param cls closure with the `struct Plugin *`
613 * @param target peer from which to disconnect
616 dv_plugin_disconnect_peer (void *cls,
617 const struct GNUNET_PeerIdentity *target)
619 struct Plugin *plugin = cls;
620 struct GNUNET_ATS_Session *session;
621 struct PendingRequest *pr;
623 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
626 return; /* nothing to do */
627 while (NULL != (pr = session->pr_head))
629 GNUNET_CONTAINER_DLL_remove (session->pr_head,
632 GNUNET_DV_send_cancel (pr->th);
634 if (NULL != pr->transmit_cont)
635 pr->transmit_cont (pr->transmit_cont_cls,
641 session->active = GNUNET_NO;
646 * Function that can be used to force the plugin to disconnect
647 * from the given peer and cancel all previous transmissions
648 * (and their continuations).
650 * @param cls closure with the `struct Plugin *`
651 * @param session which session to disconnect
655 dv_plugin_disconnect_session (void *cls,
656 struct GNUNET_ATS_Session *session)
658 struct PendingRequest *pr;
660 while (NULL != (pr = session->pr_head))
662 GNUNET_CONTAINER_DLL_remove (session->pr_head,
665 GNUNET_DV_send_cancel (pr->th);
667 if (NULL != pr->transmit_cont)
668 pr->transmit_cont (pr->transmit_cont_cls,
674 session->active = GNUNET_NO;
680 * Convert the transports address to a nice, human-readable
684 * @param type name of the transport that generated the address
685 * @param addr one of the addresses of the host, NULL for the last address
686 * the specific address format depends on the transport
687 * @param addrlen length of the address
688 * @param numeric should (IP) addresses be displayed in numeric form?
689 * @param timeout after how long should we give up?
690 * @param asc function to call on each string
691 * @param asc_cls closure for @a asc
694 dv_plugin_address_pretty_printer (void *cls, const char *type,
696 size_t addrlen, int numeric,
697 struct GNUNET_TIME_Relative timeout,
698 GNUNET_TRANSPORT_AddressStringCallback asc,
701 if ( (0 == addrlen) &&
702 (0 == strcmp (type, "dv")) )
717 * Convert the DV address to a pretty string.
720 * @param addr the (hopefully) DV address
721 * @param addrlen the length of the @a addr
722 * @return string representing the DV address
725 dv_plugin_address_to_string (void *cls,
731 GNUNET_break (0); /* malformed */
739 * Another peer has suggested an address for this peer and transport
740 * plugin. Check that this could be a valid address. This function
741 * is not expected to 'validate' the address in the sense of trying to
742 * connect to it but simply to see if the binary format is technically
743 * legal for establishing a connection to this peer (and make sure that
744 * the address really corresponds to our network connection/settings
745 * and not some potential man-in-the-middle).
748 * @param addr pointer to the address
749 * @param addrlen length of @a addr
750 * @return #GNUNET_OK if this is a plausible address for this peer
751 * and transport, #GNUNET_SYSERR if not
755 dv_plugin_check_address (void *cls,
760 return GNUNET_SYSERR;
766 * Create a new session to transmit data to the target
767 * This session will used to send data to this peer and the plugin will
768 * notify us by calling the env->session_end function
770 * @param cls the plugin
771 * @param address the address
772 * @return the session if the address is valid, NULL otherwise
774 static struct GNUNET_ATS_Session *
775 dv_get_session (void *cls,
776 const struct GNUNET_HELLO_Address *address)
778 struct Plugin *plugin = cls;
779 struct GNUNET_ATS_Session *session;
781 if (0 != address->address_length)
783 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
786 return NULL; /* not valid right now */
787 session->active = GNUNET_YES;
793 * Function called to convert a string address to
796 * @param cls closure ('struct Plugin*')
797 * @param addr string address
798 * @param addrlen length of the @a addr including \0 termination
799 * @param buf location to store the buffer
800 * If the function returns #GNUNET_SYSERR, its contents are undefined.
801 * @param added length of created address
802 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
805 dv_plugin_string_to_address (void *cls,
811 if ( (addrlen == 3) &&
812 (0 == strcmp ("dv", addr)) )
817 return GNUNET_SYSERR;
822 * Function that will be called whenever the transport service wants to
823 * notify the plugin that a session is still active and in use and
824 * therefore the session timeout for this session has to be updated
826 * @param cls closure (`struct Plugin *`)
827 * @param peer which peer was the session for
828 * @param session which session is being updated
831 dv_plugin_update_session_timeout (void *cls,
832 const struct GNUNET_PeerIdentity *peer,
833 struct GNUNET_ATS_Session *session)
835 /* DV currently doesn't time out like "normal" plugins,
836 so it should be safe to do nothing, right?
837 (or should we add an internal timeout?) */
842 * Function to obtain the network type for a session
843 * FIXME: we should probably look at the network type
844 * used by the next hop here. Or find some other way
845 * to properly allow ATS-DV resource allocation.
847 * @param cls closure (`struct Plugin *`)
848 * @param session the session
849 * @return the network type
851 static enum GNUNET_ATS_Network_Type
852 dv_get_network (void *cls,
853 struct GNUNET_ATS_Session *session)
855 GNUNET_assert (NULL != session);
856 return session->network;
861 * Function obtain the network type for an address.
863 * @param cls closure (`struct Plugin *`)
864 * @param address the address
865 * @return the network type
867 static enum GNUNET_ATS_Network_Type
868 dv_plugin_get_network_for_address (void *cls,
869 const struct GNUNET_HELLO_Address *address)
871 return GNUNET_ATS_NET_WAN; /* FOR NOW */
876 * Function that is called to get the keepalive factor.
877 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
878 * calculate the interval between keepalive packets.
880 * @param cls closure with the `struct Plugin`
881 * @return keepalive factor
884 dv_plugin_query_keepalive_factor (void *cls)
891 * Return information about the given session to the
894 * @param cls the `struct Plugin` with the monitor callback (`sic`)
895 * @param peer peer we send information about
896 * @param value our `struct GNUNET_ATS_Session` to send information about
897 * @return #GNUNET_OK (continue to iterate)
900 send_session_info_iter (void *cls,
901 const struct GNUNET_PeerIdentity *peer,
904 struct Plugin *plugin = cls;
905 struct GNUNET_ATS_Session *session = value;
907 if (GNUNET_YES != session->active)
909 notify_session_monitor (plugin,
911 GNUNET_TRANSPORT_SS_UP);
917 * Begin monitoring sessions of a plugin. There can only
918 * be one active monitor per plugin (i.e. if there are
919 * multiple monitors, the transport service needs to
920 * multiplex the generated events over all of them).
922 * @param cls closure of the plugin
923 * @param sic callback to invoke, NULL to disable monitor;
924 * plugin will being by iterating over all active
925 * sessions immediately and then enter monitor mode
926 * @param sic_cls closure for @a sic
929 dv_plugin_setup_monitor (void *cls,
930 GNUNET_TRANSPORT_SessionInfoCallback sic,
933 struct Plugin *plugin = cls;
936 plugin->sic_cls = sic_cls;
939 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
940 &send_session_info_iter,
942 /* signal end of first iteration */
943 sic (sic_cls, NULL, NULL);
949 * Entry point for the plugin.
951 * @param cls closure with the plugin environment
955 libgnunet_plugin_transport_dv_init (void *cls)
957 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
958 struct GNUNET_TRANSPORT_PluginFunctions *api;
959 struct Plugin *plugin;
961 plugin = GNUNET_new (struct Plugin);
963 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
964 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
966 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
969 &handle_dv_distance_changed,
970 &handle_dv_disconnect,
971 &handle_dv_message_received);
972 if (NULL == plugin->dvh)
974 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
975 GNUNET_SERVER_mst_destroy (plugin->mst);
976 GNUNET_free (plugin);
979 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
981 api->send = &dv_plugin_send;
982 api->disconnect_peer = &dv_plugin_disconnect_peer;
983 api->disconnect_session = &dv_plugin_disconnect_session;
984 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
985 api->check_address = &dv_plugin_check_address;
986 api->address_to_string = &dv_plugin_address_to_string;
987 api->string_to_address = &dv_plugin_string_to_address;
988 api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
989 api->get_session = &dv_get_session;
990 api->get_network = &dv_get_network;
991 api->get_network_for_address = &dv_plugin_get_network_for_address;
992 api->update_session_timeout = &dv_plugin_update_session_timeout;
993 api->setup_monitor = &dv_plugin_setup_monitor;
999 * Function called to free a session.
1003 * @param value session to free
1004 * @return #GNUNET_OK (continue to iterate)
1007 free_session_iterator (void *cls,
1008 const struct GNUNET_PeerIdentity *key,
1011 struct GNUNET_ATS_Session *session = value;
1013 free_session (session);
1019 * Exit point from the plugin.
1021 * @param cls plugin API
1025 libgnunet_plugin_transport_dv_done (void *cls)
1027 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1028 struct Plugin *plugin = api->cls;
1030 GNUNET_DV_service_disconnect (plugin->dvh);
1031 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
1032 &free_session_iterator,
1034 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
1035 GNUNET_SERVER_mst_destroy (plugin->mst);
1036 GNUNET_free (plugin);
1041 /* end of plugin_transport_dv.c */