2 This file is part of GNUnet
3 Copyright (C) 2002--2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 Session *session;
86 * Number of bytes to transmit.
93 * Session handle for connections.
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 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 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 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 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 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 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 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 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 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 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.
522 * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
525 send_finished (void *cls,
528 struct PendingRequest *pr = cls;
529 struct Session *session = pr->session;
532 GNUNET_CONTAINER_DLL_remove (session->pr_head,
535 if (NULL != pr->transmit_cont)
536 pr->transmit_cont (pr->transmit_cont_cls,
545 * Function that can be used by the transport service to transmit
546 * a message using the plugin.
549 * @param session the session used
550 * @param priority how important is the message
551 * @param msgbuf the message to transmit
552 * @param msgbuf_size number of bytes in 'msgbuf'
553 * @param timeout when should we time out
554 * @param cont continuation to call once the message has
555 * been transmitted (or if the transport is ready
556 * for the next transmission call; or if the
557 * peer disconnected...)
558 * @param cont_cls closure for @a cont
559 * @return number of bytes used (on the physical network, with overheads);
560 * -1 on hard errors (i.e. address invalid); 0 is a legal value
561 * and does NOT mean that the message was not transmitted (DV)
564 dv_plugin_send (void *cls,
565 struct Session *session,
568 unsigned int priority,
569 struct GNUNET_TIME_Relative timeout,
570 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
572 struct Plugin *plugin = cls;
573 struct PendingRequest *pr;
574 const struct GNUNET_MessageHeader *msg;
575 struct GNUNET_MessageHeader *box;
578 msg = (const struct GNUNET_MessageHeader *) msgbuf;
579 if (ntohs (msg->size) != msgbuf_size)
582 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 "Boxing DV message\n");
584 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
585 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
586 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
587 memcpy (&box[1], msgbuf, msgbuf_size);
590 pr = GNUNET_new (struct PendingRequest);
591 pr->transmit_cont = cont;
592 pr->transmit_cont_cls = cont_cls;
593 pr->session = session;
594 pr->size = msgbuf_size;
595 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
599 pr->th = GNUNET_DV_send (plugin->dvh,
604 GNUNET_free_non_null (box);
610 * Function that can be used to force the plugin to disconnect
611 * from the given peer and cancel all previous transmissions
612 * (and their continuations).
614 * @param cls closure with the `struct Plugin *`
615 * @param target peer from which to disconnect
618 dv_plugin_disconnect_peer (void *cls,
619 const struct GNUNET_PeerIdentity *target)
621 struct Plugin *plugin = cls;
622 struct Session *session;
623 struct PendingRequest *pr;
625 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
628 return; /* nothing to do */
629 while (NULL != (pr = session->pr_head))
631 GNUNET_CONTAINER_DLL_remove (session->pr_head,
634 GNUNET_DV_send_cancel (pr->th);
636 if (NULL != pr->transmit_cont)
637 pr->transmit_cont (pr->transmit_cont_cls,
643 session->active = GNUNET_NO;
648 * Function that can be used to force the plugin to disconnect
649 * from the given peer and cancel all previous transmissions
650 * (and their continuations).
652 * @param cls closure with the `struct Plugin *`
653 * @param session which session to disconnect
657 dv_plugin_disconnect_session (void *cls,
658 struct Session *session)
660 struct PendingRequest *pr;
662 while (NULL != (pr = session->pr_head))
664 GNUNET_CONTAINER_DLL_remove (session->pr_head,
667 GNUNET_DV_send_cancel (pr->th);
669 if (NULL != pr->transmit_cont)
670 pr->transmit_cont (pr->transmit_cont_cls,
676 session->active = GNUNET_NO;
682 * Convert the transports address to a nice, human-readable
686 * @param type name of the transport that generated the address
687 * @param addr one of the addresses of the host, NULL for the last address
688 * the specific address format depends on the transport
689 * @param addrlen length of the address
690 * @param numeric should (IP) addresses be displayed in numeric form?
691 * @param timeout after how long should we give up?
692 * @param asc function to call on each string
693 * @param asc_cls closure for @a asc
696 dv_plugin_address_pretty_printer (void *cls, const char *type,
698 size_t addrlen, int numeric,
699 struct GNUNET_TIME_Relative timeout,
700 GNUNET_TRANSPORT_AddressStringCallback asc,
703 if ( (0 == addrlen) &&
704 (0 == strcmp (type, "dv")) )
719 * Convert the DV address to a pretty string.
722 * @param addr the (hopefully) DV address
723 * @param addrlen the length of the @a addr
724 * @return string representing the DV address
727 dv_plugin_address_to_string (void *cls,
733 GNUNET_break (0); /* malformed */
741 * Another peer has suggested an address for this peer and transport
742 * plugin. Check that this could be a valid address. This function
743 * is not expected to 'validate' the address in the sense of trying to
744 * connect to it but simply to see if the binary format is technically
745 * legal for establishing a connection to this peer (and make sure that
746 * the address really corresponds to our network connection/settings
747 * and not some potential man-in-the-middle).
750 * @param addr pointer to the address
751 * @param addrlen length of @a addr
752 * @return #GNUNET_OK if this is a plausible address for this peer
753 * and transport, #GNUNET_SYSERR if not
757 dv_plugin_check_address (void *cls,
762 return GNUNET_SYSERR;
768 * Create a new session to transmit data to the target
769 * This session will used to send data to this peer and the plugin will
770 * notify us by calling the env->session_end function
772 * @param cls the plugin
773 * @param address the address
774 * @return the session if the address is valid, NULL otherwise
776 static struct Session *
777 dv_get_session (void *cls,
778 const struct GNUNET_HELLO_Address *address)
780 struct Plugin *plugin = cls;
781 struct Session *session;
783 if (0 != address->address_length)
785 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
788 return NULL; /* not valid right now */
789 session->active = GNUNET_YES;
795 * Function called to convert a string address to
798 * @param cls closure ('struct Plugin*')
799 * @param addr string address
800 * @param addrlen length of the @a addr including \0 termination
801 * @param buf location to store the buffer
802 * If the function returns #GNUNET_SYSERR, its contents are undefined.
803 * @param added length of created address
804 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
807 dv_plugin_string_to_address (void *cls,
813 if ( (addrlen == 3) &&
814 (0 == strcmp ("dv", addr)) )
819 return GNUNET_SYSERR;
824 * Function that will be called whenever the transport service wants to
825 * notify the plugin that a session is still active and in use and
826 * therefore the session timeout for this session has to be updated
828 * @param cls closure (`struct Plugin *`)
829 * @param peer which peer was the session for
830 * @param session which session is being updated
833 dv_plugin_update_session_timeout (void *cls,
834 const struct GNUNET_PeerIdentity *peer,
835 struct Session *session)
837 /* DV currently doesn't time out like "normal" plugins,
838 so it should be safe to do nothing, right?
839 (or should we add an internal timeout?) */
844 * Function to obtain the network type for a session
845 * FIXME: we should probably look at the network type
846 * used by the next hop here. Or find some other way
847 * to properly allow ATS-DV resource allocation.
849 * @param cls closure (`struct Plugin *`)
850 * @param session the session
851 * @return the network type
853 static enum GNUNET_ATS_Network_Type
854 dv_get_network (void *cls,
855 struct Session *session)
857 GNUNET_assert (NULL != session);
858 return session->network;
863 * Function that is called to get the keepalive factor.
864 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
865 * calculate the interval between keepalive packets.
867 * @param cls closure with the `struct Plugin`
868 * @return keepalive factor
871 dv_plugin_query_keepalive_factor (void *cls)
878 * Return information about the given session to the
881 * @param cls the `struct Plugin` with the monitor callback (`sic`)
882 * @param peer peer we send information about
883 * @param value our `struct Session` to send information about
884 * @return #GNUNET_OK (continue to iterate)
887 send_session_info_iter (void *cls,
888 const struct GNUNET_PeerIdentity *peer,
891 struct Plugin *plugin = cls;
892 struct Session *session = value;
894 if (GNUNET_YES != session->active)
896 notify_session_monitor (plugin,
898 GNUNET_TRANSPORT_SS_UP);
904 * Begin monitoring sessions of a plugin. There can only
905 * be one active monitor per plugin (i.e. if there are
906 * multiple monitors, the transport service needs to
907 * multiplex the generated events over all of them).
909 * @param cls closure of the plugin
910 * @param sic callback to invoke, NULL to disable monitor;
911 * plugin will being by iterating over all active
912 * sessions immediately and then enter monitor mode
913 * @param sic_cls closure for @a sic
916 dv_plugin_setup_monitor (void *cls,
917 GNUNET_TRANSPORT_SessionInfoCallback sic,
920 struct Plugin *plugin = cls;
923 plugin->sic_cls = sic_cls;
926 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
927 &send_session_info_iter,
929 /* signal end of first iteration */
930 sic (sic_cls, NULL, NULL);
936 * Entry point for the plugin.
938 * @param cls closure with the plugin environment
942 libgnunet_plugin_transport_dv_init (void *cls)
944 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
945 struct GNUNET_TRANSPORT_PluginFunctions *api;
946 struct Plugin *plugin;
948 plugin = GNUNET_new (struct Plugin);
950 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
951 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
953 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
956 &handle_dv_distance_changed,
957 &handle_dv_disconnect,
958 &handle_dv_message_received);
959 if (NULL == plugin->dvh)
961 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
962 GNUNET_SERVER_mst_destroy (plugin->mst);
963 GNUNET_free (plugin);
966 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
968 api->send = &dv_plugin_send;
969 api->disconnect_peer = &dv_plugin_disconnect_peer;
970 api->disconnect_session = &dv_plugin_disconnect_session;
971 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
972 api->check_address = &dv_plugin_check_address;
973 api->address_to_string = &dv_plugin_address_to_string;
974 api->string_to_address = &dv_plugin_string_to_address;
975 api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
976 api->get_session = &dv_get_session;
977 api->get_network = &dv_get_network;
978 api->update_session_timeout = &dv_plugin_update_session_timeout;
979 api->setup_monitor = &dv_plugin_setup_monitor;
985 * Function called to free a session.
989 * @param value session to free
990 * @return #GNUNET_OK (continue to iterate)
993 free_session_iterator (void *cls,
994 const struct GNUNET_PeerIdentity *key,
997 struct Session *session = value;
999 free_session (session);
1005 * Exit point from the plugin.
1007 * @param cls plugin API
1011 libgnunet_plugin_transport_dv_done (void *cls)
1013 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1014 struct Plugin *plugin = api->cls;
1016 GNUNET_DV_service_disconnect (plugin->dvh);
1017 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
1018 &free_session_iterator,
1020 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
1021 GNUNET_SERVER_mst_destroy (plugin->mst);
1022 GNUNET_free (plugin);
1027 /* end of plugin_transport_dv.c */