2 This file is part of GNUnet
3 (C) 2002--2013 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
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"
39 #define LOG(kind,...) GNUNET_log_from (kind, "transport-dv",__VA_ARGS__)
41 #define PLUGIN_NAME "dv"
44 * Encapsulation of all of the state of the plugin.
50 * An active request for transmission via DV.
58 struct PendingRequest *next;
63 struct PendingRequest *prev;
66 * Continuation function to call once the transmission buffer
67 * has again space available. NULL if there is no
68 * continuation to call.
70 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
73 * Closure for @e transmit_cont.
75 void *transmit_cont_cls;
78 * Transmission handle from DV client library.
80 struct GNUNET_DV_TransmitHandle *th;
83 * Session of this request.
85 struct Session *session;
88 * Number of bytes to transmit.
95 * Session handle for connections.
101 * Mandatory session header.
103 struct SessionHeader header;
106 * Pointer to the global plugin struct.
108 struct Plugin *plugin;
111 * Head of pending requests.
113 struct PendingRequest *pr_head;
116 * Tail of pending requests.
118 struct PendingRequest *pr_tail;
121 * To whom are we talking to.
123 struct GNUNET_PeerIdentity sender;
126 * Current distance to the given peer.
131 * Current network the next hop peer is located in
133 enum GNUNET_ATS_Network_Type network;
136 * Does the transport service know about this session (and we thus
137 * need to call `session_end` when it is released?)
145 * Encapsulation of all of the state of the plugin.
152 struct GNUNET_TRANSPORT_PluginEnvironment *env;
155 * Hash map of sessions (active and inactive).
157 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
160 * Copy of the handler array where the closures are
161 * set to this struct's instance.
163 struct GNUNET_SERVER_MessageHandler *handlers;
166 * Handle to the DV service
168 struct GNUNET_DV_ServiceHandle *dvh;
171 * Tokenizer for boxed messages.
173 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
179 * Notify transport service about the change in distance.
181 * @param session session where the distance changed
184 notify_distance_change (struct Session *session)
186 struct Plugin *plugin = session->plugin;
187 struct GNUNET_ATS_Information ats;
189 if (GNUNET_YES != session->active)
191 ats.type = htonl ((uint32_t) GNUNET_ATS_QUALITY_NET_DISTANCE);
192 ats.value = htonl (session->distance);
193 plugin->env->update_address_metrics (plugin->env->cls,
202 * Function called by MST on each message from the box.
204 * @param cls closure with the `struct Plugin *`
205 * @param client identification of the client (with the 'struct Session')
206 * @param message the actual message
207 * @return #GNUNET_OK on success
212 const struct GNUNET_MessageHeader *message)
214 struct Plugin *plugin = cls;
215 struct Session *session = client;
216 struct GNUNET_ATS_Information ats;
218 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
219 ats.value = htonl (session->distance);
220 session->active = GNUNET_YES;
221 LOG (GNUNET_ERROR_TYPE_DEBUG,
222 "Delivering message of type %u with %u bytes from peer `%s'\n",
223 ntohs (message->type),
224 ntohs (message->size),
225 GNUNET_i2s (&session->sender));
227 plugin->env->receive (plugin->env->cls,
231 plugin->env->update_address_metrics (plugin->env->cls,
232 &session->sender, NULL,
240 * Handler for messages received from the DV service.
242 * @param cls closure with the plugin
243 * @param sender sender of the message
244 * @param distance how far did the message travel
245 * @param msg actual message payload
248 handle_dv_message_received (void *cls,
249 const struct GNUNET_PeerIdentity *sender,
251 const struct GNUNET_MessageHeader *msg)
253 struct Plugin *plugin = cls;
254 struct GNUNET_ATS_Information ats;
255 struct Session *session;
257 LOG (GNUNET_ERROR_TYPE_DEBUG,
258 "Received DV_MESSAGE_RECEIVED message for peer `%s': new distance %u\n",
259 GNUNET_i2s (sender), distance);
260 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
267 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
269 /* need to unbox using MST */
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "Unboxing DV message using MST\n");
272 GNUNET_SERVER_mst_receive (plugin->mst,
274 (const char *) &msg[1],
275 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
280 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
281 ats.value = htonl (distance);
282 session->active = GNUNET_YES;
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Delivering message of type %u with %u bytes from peer `%s'\n",
287 GNUNET_i2s (sender));
288 plugin->env->receive (plugin->env->cls, sender,
291 plugin->env->update_address_metrics (plugin->env->cls,
299 * Function called if DV starts to be able to talk to a peer.
301 * @param cls closure with `struct Plugin *`
302 * @param peer newly connected peer
303 * @param distance distance to the peer
304 * @param network the network the next hop is located in
307 handle_dv_connect (void *cls,
308 const struct GNUNET_PeerIdentity *peer,
310 enum GNUNET_ATS_Network_Type network)
312 struct Plugin *plugin = cls;
313 struct Session *session;
314 struct GNUNET_ATS_Information ats[2];
316 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
318 * This requires transport plugin to be linked to libgnunetats.
319 * If you remove it, also remove libgnunetats linkage from Makefile.am
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "Received `%s' message for peer `%s' with next hop in network %s\n",
325 GNUNET_ATS_print_network_type (network));
327 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
332 session->distance = distance;
333 notify_distance_change (session);
334 return; /* nothing to do */
337 session = GNUNET_new (struct Session);
338 session->sender = *peer;
339 session->plugin = plugin;
340 session->distance = distance;
341 session->network = network;
342 GNUNET_assert(GNUNET_YES ==
343 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
344 &session->sender, session,
345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Creating new DV session %p for peer `%s' at distance %u\n",
353 /* Notify transport and ats about new connection */
354 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
355 ats[0].value = htonl (distance);
356 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
357 ats[1].value = htonl ((uint32_t) network);
358 session->active = GNUNET_YES;
359 plugin->env->session_start (plugin->env->cls, peer,
367 * Function called if DV distance to a peer is changed.
369 * @param cls closure with `struct Plugin *`
370 * @param peer connected peer
371 * @param distance new distance to the peer
372 * @param network network type used for the connection
375 handle_dv_distance_changed (void *cls,
376 const struct GNUNET_PeerIdentity *peer,
378 enum GNUNET_ATS_Network_Type network)
380 struct Plugin *plugin = cls;
381 struct Session *session;
383 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "Received `%s' message for peer `%s': new distance %u\n",
386 "DV_DISTANCE_CHANGED",
389 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
394 handle_dv_connect (plugin, peer, distance, network);
397 session->distance = distance;
398 notify_distance_change (session);
403 * Release session object and clean up associated resources.
405 * @param session session to clean up
408 free_session (struct Session *session)
410 struct Plugin *plugin = session->plugin;
411 struct PendingRequest *pr;
413 GNUNET_assert (GNUNET_YES ==
414 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
418 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Freeing session %p for peer `%s'\n",
421 GNUNET_i2s (&session->sender));
422 if (GNUNET_YES == session->active)
424 plugin->env->session_end (plugin->env->cls,
427 session->active = GNUNET_NO;
429 while (NULL != (pr = session->pr_head))
431 GNUNET_CONTAINER_DLL_remove (session->pr_head,
434 GNUNET_DV_send_cancel (pr->th);
436 if (NULL != pr->transmit_cont)
437 pr->transmit_cont (pr->transmit_cont_cls,
443 GNUNET_free (session);
448 * Function called if DV is no longer able to talk to a peer.
450 * @param cls closure with `struct Plugin *`
451 * @param peer peer that disconnected
454 handle_dv_disconnect (void *cls,
455 const struct GNUNET_PeerIdentity *peer)
457 struct Plugin *plugin = cls;
458 struct Session *session;
460 LOG (GNUNET_ERROR_TYPE_DEBUG,
461 "Received `%s' message for peer `%s'\n",
464 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
467 return; /* nothing to do */
468 free_session (session);
473 * Function called once the delivery of a message has been successful.
474 * Clean up the pending request, and call continuations.
477 * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
480 send_finished (void *cls,
483 struct PendingRequest *pr = cls;
484 struct Session *session = pr->session;
487 GNUNET_CONTAINER_DLL_remove (session->pr_head,
490 if (NULL != pr->transmit_cont)
491 pr->transmit_cont (pr->transmit_cont_cls,
500 * Function that can be used by the transport service to transmit
501 * a message using the plugin.
504 * @param session the session used
505 * @param priority how important is the message
506 * @param msgbuf the message to transmit
507 * @param msgbuf_size number of bytes in 'msgbuf'
508 * @param timeout when should we time out
509 * @param cont continuation to call once the message has
510 * been transmitted (or if the transport is ready
511 * for the next transmission call; or if the
512 * peer disconnected...)
513 * @param cont_cls closure for @a cont
514 * @return number of bytes used (on the physical network, with overheads);
515 * -1 on hard errors (i.e. address invalid); 0 is a legal value
516 * and does NOT mean that the message was not transmitted (DV)
519 dv_plugin_send (void *cls,
520 struct Session *session,
523 unsigned int priority,
524 struct GNUNET_TIME_Relative timeout,
525 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
527 struct Plugin *plugin = cls;
528 struct PendingRequest *pr;
529 const struct GNUNET_MessageHeader *msg;
530 struct GNUNET_MessageHeader *box;
533 msg = (const struct GNUNET_MessageHeader *) msgbuf;
534 if (ntohs (msg->size) != msgbuf_size)
537 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Boxing DV message\n");
539 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
540 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
541 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
542 memcpy (&box[1], msgbuf, msgbuf_size);
545 pr = GNUNET_new (struct PendingRequest);
546 pr->transmit_cont = cont;
547 pr->transmit_cont_cls = cont_cls;
548 pr->session = session;
549 pr->size = msgbuf_size;
550 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
554 pr->th = GNUNET_DV_send (plugin->dvh,
559 GNUNET_free_non_null (box);
565 * Function that can be used to force the plugin to disconnect
566 * from the given peer and cancel all previous transmissions
567 * (and their continuations).
569 * @param cls closure with the `struct Plugin *`
570 * @param target peer from which to disconnect
573 dv_plugin_disconnect_peer (void *cls,
574 const struct GNUNET_PeerIdentity *target)
576 struct Plugin *plugin = cls;
577 struct Session *session;
578 struct PendingRequest *pr;
580 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
583 return; /* nothing to do */
584 while (NULL != (pr = session->pr_head))
586 GNUNET_CONTAINER_DLL_remove (session->pr_head,
589 GNUNET_DV_send_cancel (pr->th);
591 if (NULL != pr->transmit_cont)
592 pr->transmit_cont (pr->transmit_cont_cls,
598 session->active = GNUNET_NO;
603 * Function that can be used to force the plugin to disconnect
604 * from the given peer and cancel all previous transmissions
605 * (and their continuations).
607 * @param cls closure with the `struct Plugin *`
608 * @param session which session to disconnect
612 dv_plugin_disconnect_session (void *cls,
613 struct Session *session)
615 struct PendingRequest *pr;
617 while (NULL != (pr = session->pr_head))
619 GNUNET_CONTAINER_DLL_remove (session->pr_head,
622 GNUNET_DV_send_cancel (pr->th);
624 if (NULL != pr->transmit_cont)
625 pr->transmit_cont (pr->transmit_cont_cls,
631 session->active = GNUNET_NO;
637 * Convert the transports address to a nice, human-readable
641 * @param type name of the transport that generated the address
642 * @param addr one of the addresses of the host, NULL for the last address
643 * the specific address format depends on the transport
644 * @param addrlen length of the address
645 * @param numeric should (IP) addresses be displayed in numeric form?
646 * @param timeout after how long should we give up?
647 * @param asc function to call on each string
648 * @param asc_cls closure for @a asc
651 dv_plugin_address_pretty_printer (void *cls, const char *type,
653 size_t addrlen, int numeric,
654 struct GNUNET_TIME_Relative timeout,
655 GNUNET_TRANSPORT_AddressStringCallback asc,
658 if ( (0 == addrlen) &&
659 (0 == strcmp (type, "dv")) )
666 * Convert the DV address to a pretty string.
669 * @param addr the (hopefully) DV address
670 * @param addrlen the length of the @a addr
671 * @return string representing the DV address
674 dv_plugin_address_to_string (void *cls,
680 GNUNET_break (0); /* malformed */
688 * Another peer has suggested an address for this peer and transport
689 * plugin. Check that this could be a valid address. This function
690 * is not expected to 'validate' the address in the sense of trying to
691 * connect to it but simply to see if the binary format is technically
692 * legal for establishing a connection to this peer (and make sure that
693 * the address really corresponds to our network connection/settings
694 * and not some potential man-in-the-middle).
697 * @param addr pointer to the address
698 * @param addrlen length of @a addr
699 * @return #GNUNET_OK if this is a plausible address for this peer
700 * and transport, #GNUNET_SYSERR if not
704 dv_plugin_check_address (void *cls,
709 return GNUNET_SYSERR;
715 * Create a new session to transmit data to the target
716 * This session will used to send data to this peer and the plugin will
717 * notify us by calling the env->session_end function
719 * @param cls the plugin
720 * @param address the address
721 * @return the session if the address is valid, NULL otherwise
723 static struct Session *
724 dv_get_session (void *cls,
725 const struct GNUNET_HELLO_Address *address)
727 struct Plugin *plugin = cls;
728 struct Session *session;
730 if (0 != address->address_length)
732 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
735 return NULL; /* not valid right now */
736 session->active = GNUNET_YES;
742 * Function called to convert a string address to
745 * @param cls closure ('struct Plugin*')
746 * @param addr string address
747 * @param addrlen length of the @a addr including \0 termination
748 * @param buf location to store the buffer
749 * If the function returns #GNUNET_SYSERR, its contents are undefined.
750 * @param added length of created address
751 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
754 dv_plugin_string_to_address (void *cls,
760 if ( (addrlen == 3) &&
761 (0 == strcmp ("dv", addr)) )
766 return GNUNET_SYSERR;
771 * Function that will be called whenever the transport service wants to
772 * notify the plugin that a session is still active and in use and
773 * therefore the session timeout for this session has to be updated
775 * @param cls closure (`struct Plugin *`)
776 * @param peer which peer was the session for
777 * @param session which session is being updated
780 dv_plugin_update_session_timeout (void *cls,
781 const struct GNUNET_PeerIdentity *peer,
782 struct Session *session)
784 /* DV currently doesn't time out like "normal" plugins,
785 so it should be safe to do nothing, right?
786 (or should we add an internal timeout?) */
791 * Function to obtain the network type for a session
792 * FIXME: we should probably look at the network type
793 * used by the next hop here. Or find some other way
794 * to properly allow ATS-DV resource allocation.
796 * @param cls closure (`struct Plugin *`)
797 * @param session the session
798 * @return the network type
800 static enum GNUNET_ATS_Network_Type
801 dv_get_network (void *cls,
802 struct Session *session)
804 GNUNET_assert (NULL != session);
805 return session->network;
810 * Function that is called to get the keepalive factor.
811 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
812 * calculate the interval between keepalive packets.
814 * @param cls closure with the `struct Plugin`
815 * @return keepalive factor
818 dv_plugin_query_keepalive_factor (void *cls)
825 * Entry point for the plugin.
827 * @param cls closure with the plugin environment
831 libgnunet_plugin_transport_dv_init (void *cls)
833 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
834 struct GNUNET_TRANSPORT_PluginFunctions *api;
835 struct Plugin *plugin;
837 plugin = GNUNET_new (struct Plugin);
839 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
840 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
842 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
845 &handle_dv_distance_changed,
846 &handle_dv_disconnect,
847 &handle_dv_message_received);
848 if (NULL == plugin->dvh)
850 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
851 GNUNET_SERVER_mst_destroy (plugin->mst);
852 GNUNET_free (plugin);
855 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
857 api->send = &dv_plugin_send;
858 api->disconnect_peer = &dv_plugin_disconnect_peer;
859 api->disconnect_session = &dv_plugin_disconnect_session;
860 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
861 api->check_address = &dv_plugin_check_address;
862 api->address_to_string = &dv_plugin_address_to_string;
863 api->string_to_address = &dv_plugin_string_to_address;
864 api->query_keepalive_factor = &dv_plugin_query_keepalive_factor;
865 api->get_session = &dv_get_session;
866 api->get_network = &dv_get_network;
867 api->update_session_timeout = &dv_plugin_update_session_timeout;
873 * Function called to free a session.
877 * @param value session to free
878 * @return #GNUNET_OK (continue to iterate)
881 free_session_iterator (void *cls,
882 const struct GNUNET_PeerIdentity *key,
885 struct Session *session = value;
887 free_session (session);
893 * Exit point from the plugin.
895 * @param cls plugin API
899 libgnunet_plugin_transport_dv_done (void *cls)
901 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
902 struct Plugin *plugin = api->cls;
904 GNUNET_DV_service_disconnect (plugin->dvh);
905 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
906 &free_session_iterator,
908 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
909 GNUNET_SERVER_mst_destroy (plugin->mst);
910 GNUNET_free (plugin);
915 /* end of plugin_transport_dv.c */