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 * Session handle for connections.
50 struct GNUNET_ATS_Session
53 * Pointer to the global plugin struct.
55 struct Plugin *plugin;
58 * Address we use for the other peer.
60 struct GNUNET_HELLO_Address *address;
63 * To whom are we talking to.
65 struct GNUNET_PeerIdentity sender;
68 * Number of bytes waiting for transmission to this peer.
71 unsigned long long bytes_in_queue;
74 * Number of messages waiting for transmission to this peer.
77 unsigned int msgs_in_queue;
80 * Current distance to the given peer.
85 * Current network the next hop peer is located in
87 enum GNUNET_ATS_Network_Type network;
90 * Does the transport service know about this session (and we thus
91 * need to call `session_end` when it is released?)
99 * Encapsulation of all of the state of the plugin.
106 struct GNUNET_TRANSPORT_PluginEnvironment *env;
109 * Hash map of sessions (active and inactive).
111 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
114 * Copy of the handler array where the closures are
115 * set to this struct's instance.
117 struct GNUNET_SERVER_MessageHandler *handlers;
120 * Handle to the DV service
122 struct GNUNET_DV_ServiceHandle *dvh;
125 * Tokenizer for boxed messages.
127 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
130 * Function to call about session status changes.
132 GNUNET_TRANSPORT_SessionInfoCallback sic;
135 * Closure for @e sic.
142 * If a session monitor is attached, notify it about the new
145 * @param plugin our plugin
146 * @param session session that changed state
147 * @param state new state of the session
150 notify_session_monitor (struct Plugin *plugin,
151 struct GNUNET_ATS_Session *session,
152 enum GNUNET_TRANSPORT_SessionState state)
154 struct GNUNET_TRANSPORT_SessionInfo info;
156 if (NULL == plugin->sic)
158 memset (&info, 0, sizeof (info));
160 info.is_inbound = GNUNET_SYSERR; /* hard to say */
161 info.num_msg_pending = session->msgs_in_queue;
162 info.num_bytes_pending = session->bytes_in_queue;
163 /* info.receive_delay remains zero as this is not supported by DV
164 (cannot selectively not receive from 'core') */
165 info.session_timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
166 info.address = session->address;
167 plugin->sic (plugin->sic_cls,
174 * Notify transport service about the change in distance.
176 * @param session session where the distance changed
179 notify_distance_change (struct GNUNET_ATS_Session *session)
181 struct Plugin *plugin = session->plugin;
183 if (GNUNET_YES != session->active)
185 plugin->env->update_address_distance (plugin->env->cls,
192 * Function called by MST on each message from the box.
194 * @param cls closure with the `struct Plugin *`
195 * @param client identification of the client (with the 'struct GNUNET_ATS_Session')
196 * @param message the actual message
197 * @return #GNUNET_OK on success
202 const struct GNUNET_MessageHeader *message)
204 struct Plugin *plugin = cls;
205 struct GNUNET_ATS_Session *session = client;
207 session->active = GNUNET_YES;
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "Delivering message of type %u with %u bytes from peer `%s'\n",
210 ntohs (message->type),
211 ntohs (message->size),
212 GNUNET_i2s (&session->sender));
213 plugin->env->receive (plugin->env->cls,
217 plugin->env->update_address_distance (plugin->env->cls,
225 * Handler for messages received from the DV service.
227 * @param cls closure with the plugin
228 * @param sender sender of the message
229 * @param distance how far did the message travel
230 * @param msg actual message payload
233 handle_dv_message_received (void *cls,
234 const struct GNUNET_PeerIdentity *sender,
236 const struct GNUNET_MessageHeader *msg)
238 struct Plugin *plugin = cls;
239 struct GNUNET_ATS_Session *session;
241 LOG (GNUNET_ERROR_TYPE_DEBUG,
242 "Received DV_MESSAGE_RECEIVED message for peer `%s': new distance %u\n",
245 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
252 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
254 /* need to unbox using MST */
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Unboxing DV message using MST\n");
257 GNUNET_SERVER_mst_receive (plugin->mst,
259 (const char *) &msg[1],
260 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
265 session->active = GNUNET_YES;
266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "Delivering message of type %u with %u bytes from peer `%s'\n",
270 GNUNET_i2s (sender));
271 plugin->env->receive (plugin->env->cls,
275 plugin->env->update_address_distance (plugin->env->cls,
282 * Function called if DV starts to be able to talk to a peer.
284 * @param cls closure with `struct Plugin *`
285 * @param peer newly connected peer
286 * @param distance distance to the peer
287 * @param network the network the next hop is located in
290 handle_dv_connect (void *cls,
291 const struct GNUNET_PeerIdentity *peer,
293 enum GNUNET_ATS_Network_Type network)
295 struct Plugin *plugin = cls;
296 struct GNUNET_ATS_Session *session;
298 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
300 * This requires transport plugin to be linked to libgnunetats.
301 * If you remove it, also remove libgnunetats linkage from Makefile.am
303 LOG (GNUNET_ERROR_TYPE_DEBUG,
304 "Received DV_CONNECT message for peer `%s' with next hop in network %s\n",
306 GNUNET_ATS_print_network_type (network));
308 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
313 session->distance = distance;
314 notify_distance_change (session);
315 return; /* nothing to do */
318 session = GNUNET_new (struct GNUNET_ATS_Session);
319 session->address = GNUNET_HELLO_address_allocate (peer, "dv",
321 GNUNET_HELLO_ADDRESS_INFO_NONE);
322 session->sender = *peer;
323 session->plugin = plugin;
324 session->distance = distance;
325 session->network = network;
326 GNUNET_assert(GNUNET_YES ==
327 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
328 &session->sender, session,
329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
331 LOG (GNUNET_ERROR_TYPE_DEBUG,
332 "Creating new DV session %p for peer `%s' at distance %u\n",
337 session->active = GNUNET_YES;
338 plugin->env->session_start (plugin->env->cls,
342 plugin->env->update_address_distance (plugin->env->cls,
346 notify_session_monitor (session->plugin,
348 GNUNET_TRANSPORT_SS_UP);
353 * Function called if DV distance to a peer is changed.
355 * @param cls closure with `struct Plugin *`
356 * @param peer connected peer
357 * @param distance new distance to the peer
358 * @param network network type used for the connection
361 handle_dv_distance_changed (void *cls,
362 const struct GNUNET_PeerIdentity *peer,
364 enum GNUNET_ATS_Network_Type network)
366 struct Plugin *plugin = cls;
367 struct GNUNET_ATS_Session *session;
369 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Received `%s' message for peer `%s': new distance %u\n",
372 "DV_DISTANCE_CHANGED",
375 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
380 handle_dv_connect (plugin, peer, distance, network);
383 session->distance = distance;
384 notify_distance_change (session);
389 * Release session object and clean up associated resources.
391 * @param session session to clean up
394 free_session (struct GNUNET_ATS_Session *session)
396 struct Plugin *plugin = session->plugin;
398 GNUNET_assert (GNUNET_YES ==
399 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
403 LOG (GNUNET_ERROR_TYPE_DEBUG,
404 "Freeing session %p for peer `%s'\n",
406 GNUNET_i2s (&session->sender));
407 if (GNUNET_YES == session->active)
409 notify_session_monitor (session->plugin,
411 GNUNET_TRANSPORT_SS_DONE);
412 plugin->env->session_end (plugin->env->cls,
415 session->active = GNUNET_NO;
417 GNUNET_HELLO_address_free (session->address);
418 GNUNET_free (session);
423 * Function called if DV is no longer able to talk to a peer.
425 * @param cls closure with `struct Plugin *`
426 * @param peer peer that disconnected
429 handle_dv_disconnect (void *cls,
430 const struct GNUNET_PeerIdentity *peer)
432 struct Plugin *plugin = cls;
433 struct GNUNET_ATS_Session *session;
435 LOG (GNUNET_ERROR_TYPE_DEBUG,
436 "Received `%s' message for peer `%s'\n",
439 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
442 return; /* nothing to do */
443 free_session (session);
448 * Function that can be used by the transport service to transmit
449 * a message using the plugin.
452 * @param session the session used
453 * @param priority how important is the message
454 * @param msgbuf the message to transmit
455 * @param msgbuf_size number of bytes in 'msgbuf'
456 * @param timeout when should we time out
457 * @param cont continuation to call once the message has
458 * been transmitted (or if the transport is ready
459 * for the next transmission call; or if the
460 * peer disconnected...)
461 * @param cont_cls closure for @a cont
462 * @return number of bytes used (on the physical network, with overheads);
463 * -1 on hard errors (i.e. address invalid); 0 is a legal value
464 * and does NOT mean that the message was not transmitted (DV)
467 dv_plugin_send (void *cls,
468 struct GNUNET_ATS_Session *session,
471 unsigned int priority,
472 struct GNUNET_TIME_Relative timeout,
473 GNUNET_TRANSPORT_TransmitContinuation cont,
476 struct Plugin *plugin = cls;
477 const struct GNUNET_MessageHeader *msg;
478 struct GNUNET_MessageHeader *box;
481 msg = (const struct GNUNET_MessageHeader *) msgbuf;
482 if (ntohs (msg->size) != msgbuf_size)
485 LOG (GNUNET_ERROR_TYPE_DEBUG,
486 "Boxing DV message\n");
487 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
488 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
489 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
490 memcpy (&box[1], msgbuf, msgbuf_size);
493 GNUNET_DV_send (plugin->dvh,
500 GNUNET_free_non_null (box);
506 * Function that can be used to force the plugin to disconnect
507 * from the given peer and cancel all previous transmissions
508 * (and their continuations).
510 * @param cls closure with the `struct Plugin *`
511 * @param target peer from which to disconnect
514 dv_plugin_disconnect_peer (void *cls,
515 const struct GNUNET_PeerIdentity *target)
517 struct Plugin *plugin = cls;
518 struct GNUNET_ATS_Session *session;
520 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
523 return; /* nothing to do */
524 session->active = GNUNET_NO;
529 * Function that can be used to force the plugin to disconnect
530 * from the given peer and cancel all previous transmissions
531 * (and their continuations).
533 * @param cls closure with the `struct Plugin *`
534 * @param session which session to disconnect
538 dv_plugin_disconnect_session (void *cls,
539 struct GNUNET_ATS_Session *session)
541 session->active = GNUNET_NO;
547 * Convert the transports address to a nice, human-readable
551 * @param type name of the transport that generated the address
552 * @param addr one of the addresses of the host, NULL for the last address
553 * the specific address format depends on the transport
554 * @param addrlen length of the address
555 * @param numeric should (IP) addresses be displayed in numeric form?
556 * @param timeout after how long should we give up?
557 * @param asc function to call on each string
558 * @param asc_cls closure for @a asc
561 dv_plugin_address_pretty_printer (void *cls,
566 struct GNUNET_TIME_Relative timeout,
567 GNUNET_TRANSPORT_AddressStringCallback asc,
570 if ( (0 == addrlen) &&
571 (0 == strcmp (type, "dv")) )
586 * Convert the DV address to a pretty string.
589 * @param addr the (hopefully) DV address
590 * @param addrlen the length of the @a addr
591 * @return string representing the DV address
594 dv_plugin_address_to_string (void *cls,
600 GNUNET_break (0); /* malformed */
608 * Another peer has suggested an address for this peer and transport
609 * plugin. Check that this could be a valid address. This function
610 * is not expected to 'validate' the address in the sense of trying to
611 * connect to it but simply to see if the binary format is technically
612 * legal for establishing a connection to this peer (and make sure that
613 * the address really corresponds to our network connection/settings
614 * and not some potential man-in-the-middle).
617 * @param addr pointer to the address
618 * @param addrlen length of @a addr
619 * @return #GNUNET_OK if this is a plausible address for this peer
620 * and transport, #GNUNET_SYSERR if not
624 dv_plugin_check_address (void *cls,
629 return GNUNET_SYSERR;
635 * Create a new session to transmit data to the target
636 * This session will used to send data to this peer and the plugin will
637 * notify us by calling the env->session_end function
639 * @param cls the plugin
640 * @param address the address
641 * @return the session if the address is valid, NULL otherwise
643 static struct GNUNET_ATS_Session *
644 dv_get_session (void *cls,
645 const struct GNUNET_HELLO_Address *address)
647 struct Plugin *plugin = cls;
648 struct GNUNET_ATS_Session *session;
650 if (0 != address->address_length)
652 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
655 return NULL; /* not valid right now */
656 session->active = GNUNET_YES;
662 * Function called to convert a string address to
665 * @param cls closure ('struct Plugin*')
666 * @param addr string address
667 * @param addrlen length of the @a addr including \0 termination
668 * @param buf location to store the buffer
669 * If the function returns #GNUNET_SYSERR, its contents are undefined.
670 * @param added length of created address
671 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
674 dv_plugin_string_to_address (void *cls,
680 if ( (addrlen == 3) &&
681 (0 == strcmp ("dv", addr)) )
686 return GNUNET_SYSERR;
691 * Function that will be called whenever the transport service wants to
692 * notify the plugin that a session is still active and in use and
693 * therefore the session timeout for this session has to be updated
695 * @param cls closure (`struct Plugin *`)
696 * @param peer which peer was the session for
697 * @param session which session is being updated
700 dv_plugin_update_session_timeout (void *cls,
701 const struct GNUNET_PeerIdentity *peer,
702 struct GNUNET_ATS_Session *session)
704 /* DV currently doesn't time out like "normal" plugins,
705 so it should be safe to do nothing, right?
706 (or should we add an internal timeout?) */
711 * Function to obtain the network type for a session
712 * FIXME: we should probably look at the network type
713 * used by the next hop here. Or find some other way
714 * to properly allow ATS-DV resource allocation.
716 * @param cls closure (`struct Plugin *`)
717 * @param session the session
718 * @return the network type
720 static enum GNUNET_ATS_Network_Type
721 dv_get_network (void *cls,
722 struct GNUNET_ATS_Session *session)
724 GNUNET_assert (NULL != session);
725 return session->network;
730 * Function obtain the network type for an address.
732 * @param cls closure (`struct Plugin *`)
733 * @param address the address
734 * @return the network type
736 static enum GNUNET_ATS_Network_Type
737 dv_plugin_get_network_for_address (void *cls,
738 const struct GNUNET_HELLO_Address *address)
740 return GNUNET_ATS_NET_WAN; /* FOR NOW */
745 * Function that is called to get the keepalive factor.
746 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
747 * calculate the interval between keepalive packets.
749 * @param cls closure with the `struct Plugin`
750 * @return keepalive factor
753 dv_plugin_query_keepalive_factor (void *cls)
760 * Return information about the given session to the
763 * @param cls the `struct Plugin` with the monitor callback (`sic`)
764 * @param peer peer we send information about
765 * @param value our `struct GNUNET_ATS_Session` to send information about
766 * @return #GNUNET_OK (continue to iterate)
769 send_session_info_iter (void *cls,
770 const struct GNUNET_PeerIdentity *peer,
773 struct Plugin *plugin = cls;
774 struct GNUNET_ATS_Session *session = value;
776 if (GNUNET_YES != session->active)
778 notify_session_monitor (plugin,
780 GNUNET_TRANSPORT_SS_UP);
786 * Begin monitoring sessions of a plugin. There can only
787 * be one active monitor per plugin (i.e. if there are
788 * multiple monitors, the transport service needs to
789 * multiplex the generated events over all of them).
791 * @param cls closure of the plugin
792 * @param sic callback to invoke, NULL to disable monitor;
793 * plugin will being by iterating over all active
794 * sessions immediately and then enter monitor mode
795 * @param sic_cls closure for @a sic
798 dv_plugin_setup_monitor (void *cls,
799 GNUNET_TRANSPORT_SessionInfoCallback sic,
802 struct Plugin *plugin = cls;
805 plugin->sic_cls = sic_cls;
808 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
809 &send_session_info_iter,
811 /* signal end of first iteration */
812 sic (sic_cls, NULL, NULL);
818 * Entry point for the plugin.
820 * @param cls closure with the plugin environment
824 libgnunet_plugin_transport_dv_init (void *cls)
826 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
827 struct GNUNET_TRANSPORT_PluginFunctions *api;
828 struct Plugin *plugin;
830 plugin = GNUNET_new (struct Plugin);
832 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
833 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
835 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
838 &handle_dv_distance_changed,
839 &handle_dv_disconnect,
840 &handle_dv_message_received);
841 if (NULL == plugin->dvh)
843 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
844 GNUNET_SERVER_mst_destroy (plugin->mst);
845 GNUNET_free (plugin);
848 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
850 api->send = &dv_plugin_send;
851 api->disconnect_peer = &dv_plugin_disconnect_peer;
852 api->disconnect_session = &dv_plugin_disconnect_session;
853 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
854 api->check_address = &dv_plugin_check_address;
855 api->address_to_string = &dv_plugin_address_to_string;
856 api->string_to_address = &dv_plugin_string_to_address;
857 api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
858 api->get_session = &dv_get_session;
859 api->get_network = &dv_get_network;
860 api->get_network_for_address = &dv_plugin_get_network_for_address;
861 api->update_session_timeout = &dv_plugin_update_session_timeout;
862 api->setup_monitor = &dv_plugin_setup_monitor;
868 * Function called to free a session.
872 * @param value session to free
873 * @return #GNUNET_OK (continue to iterate)
876 free_session_iterator (void *cls,
877 const struct GNUNET_PeerIdentity *key,
880 struct GNUNET_ATS_Session *session = value;
882 free_session (session);
888 * Exit point from the plugin.
890 * @param cls plugin API
894 libgnunet_plugin_transport_dv_done (void *cls)
896 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
897 struct Plugin *plugin = api->cls;
899 GNUNET_DV_service_disconnect (plugin->dvh);
900 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
901 &free_session_iterator,
903 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
904 GNUNET_SERVER_mst_destroy (plugin->mst);
905 GNUNET_free (plugin);
910 /* end of plugin_transport_dv.c */