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;
120 struct GNUNET_HELLO_Address *address;
123 * To whom are we talking to.
125 struct GNUNET_PeerIdentity sender;
128 * Current distance to the given peer.
133 * Current network the next hop peer is located in
135 enum GNUNET_ATS_Network_Type network;
138 * Does the transport service know about this session (and we thus
139 * need to call `session_end` when it is released?)
147 * Encapsulation of all of the state of the plugin.
154 struct GNUNET_TRANSPORT_PluginEnvironment *env;
157 * Hash map of sessions (active and inactive).
159 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
162 * Copy of the handler array where the closures are
163 * set to this struct's instance.
165 struct GNUNET_SERVER_MessageHandler *handlers;
168 * Handle to the DV service
170 struct GNUNET_DV_ServiceHandle *dvh;
173 * Tokenizer for boxed messages.
175 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
181 * Notify transport service about the change in distance.
183 * @param session session where the distance changed
186 notify_distance_change (struct Session *session)
188 struct Plugin *plugin = session->plugin;
189 struct GNUNET_ATS_Information ats;
191 if (GNUNET_YES != session->active)
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);
201 * Function called by MST on each message from the box.
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
211 const struct GNUNET_MessageHeader *message)
213 struct Plugin *plugin = cls;
214 struct Session *session = client;
215 struct GNUNET_ATS_Information ats;
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));
226 plugin->env->receive (plugin->env->cls, session->address, session,
228 plugin->env->update_address_metrics (plugin->env->cls,
229 session->address, session, &ats, 1);
235 * Handler for messages received from the DV service.
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
243 handle_dv_message_received (void *cls,
244 const struct GNUNET_PeerIdentity *sender,
246 const struct GNUNET_MessageHeader *msg)
248 struct Plugin *plugin = cls;
249 struct GNUNET_ATS_Information ats;
250 struct Session *session;
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,
262 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
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,
269 (const char *) &msg[1],
270 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
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",
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);
290 * Function called if DV starts to be able to talk to a peer.
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
298 handle_dv_connect (void *cls,
299 const struct GNUNET_PeerIdentity *peer,
301 enum GNUNET_ATS_Network_Type network)
303 struct Plugin *plugin = cls;
304 struct Session *session;
305 struct GNUNET_ATS_Information ats[2];
307 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
309 * This requires transport plugin to be linked to libgnunetats.
310 * If you remove it, also remove libgnunetats linkage from Makefile.am
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Received `%s' message for peer `%s' with next hop in network %s\n",
316 GNUNET_ATS_print_network_type (network));
318 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
323 session->distance = distance;
324 notify_distance_change (session);
325 return; /* nothing to do */
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));
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "Creating new DV session %p for peer `%s' at distance %u\n",
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,
358 * Function called if DV distance to a peer is changed.
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
366 handle_dv_distance_changed (void *cls,
367 const struct GNUNET_PeerIdentity *peer,
369 enum GNUNET_ATS_Network_Type network)
371 struct Plugin *plugin = cls;
372 struct Session *session;
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",
380 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
385 handle_dv_connect (plugin, peer, distance, network);
388 session->distance = distance;
389 notify_distance_change (session);
394 * Release session object and clean up associated resources.
396 * @param session session to clean up
399 free_session (struct Session *session)
401 struct Plugin *plugin = session->plugin;
402 struct PendingRequest *pr;
404 GNUNET_assert (GNUNET_YES ==
405 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
409 LOG (GNUNET_ERROR_TYPE_DEBUG,
410 "Freeing session %p for peer `%s'\n",
412 GNUNET_i2s (&session->sender));
413 if (GNUNET_YES == session->active)
415 plugin->env->session_end (plugin->env->cls,
418 session->active = GNUNET_NO;
420 while (NULL != (pr = session->pr_head))
422 GNUNET_CONTAINER_DLL_remove (session->pr_head,
425 GNUNET_DV_send_cancel (pr->th);
427 if (NULL != pr->transmit_cont)
428 pr->transmit_cont (pr->transmit_cont_cls,
434 GNUNET_HELLO_address_free (session->address);
435 GNUNET_free (session);
440 * Function called if DV is no longer able to talk to a peer.
442 * @param cls closure with `struct Plugin *`
443 * @param peer peer that disconnected
446 handle_dv_disconnect (void *cls,
447 const struct GNUNET_PeerIdentity *peer)
449 struct Plugin *plugin = cls;
450 struct Session *session;
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 "Received `%s' message for peer `%s'\n",
456 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
459 return; /* nothing to do */
460 free_session (session);
465 * Function called once the delivery of a message has been successful.
466 * Clean up the pending request, and call continuations.
469 * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
472 send_finished (void *cls,
475 struct PendingRequest *pr = cls;
476 struct Session *session = pr->session;
479 GNUNET_CONTAINER_DLL_remove (session->pr_head,
482 if (NULL != pr->transmit_cont)
483 pr->transmit_cont (pr->transmit_cont_cls,
492 * Function that can be used by the transport service to transmit
493 * a message using the plugin.
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)
511 dv_plugin_send (void *cls,
512 struct Session *session,
515 unsigned int priority,
516 struct GNUNET_TIME_Relative timeout,
517 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
519 struct Plugin *plugin = cls;
520 struct PendingRequest *pr;
521 const struct GNUNET_MessageHeader *msg;
522 struct GNUNET_MessageHeader *box;
525 msg = (const struct GNUNET_MessageHeader *) msgbuf;
526 if (ntohs (msg->size) != msgbuf_size)
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);
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,
546 pr->th = GNUNET_DV_send (plugin->dvh,
551 GNUNET_free_non_null (box);
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).
561 * @param cls closure with the `struct Plugin *`
562 * @param target peer from which to disconnect
565 dv_plugin_disconnect_peer (void *cls,
566 const struct GNUNET_PeerIdentity *target)
568 struct Plugin *plugin = cls;
569 struct Session *session;
570 struct PendingRequest *pr;
572 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
575 return; /* nothing to do */
576 while (NULL != (pr = session->pr_head))
578 GNUNET_CONTAINER_DLL_remove (session->pr_head,
581 GNUNET_DV_send_cancel (pr->th);
583 if (NULL != pr->transmit_cont)
584 pr->transmit_cont (pr->transmit_cont_cls,
590 session->active = GNUNET_NO;
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).
599 * @param cls closure with the `struct Plugin *`
600 * @param session which session to disconnect
604 dv_plugin_disconnect_session (void *cls,
605 struct Session *session)
607 struct PendingRequest *pr;
609 while (NULL != (pr = session->pr_head))
611 GNUNET_CONTAINER_DLL_remove (session->pr_head,
614 GNUNET_DV_send_cancel (pr->th);
616 if (NULL != pr->transmit_cont)
617 pr->transmit_cont (pr->transmit_cont_cls,
623 session->active = GNUNET_NO;
629 * Convert the transports address to a nice, human-readable
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
643 dv_plugin_address_pretty_printer (void *cls, const char *type,
645 size_t addrlen, int numeric,
646 struct GNUNET_TIME_Relative timeout,
647 GNUNET_TRANSPORT_AddressStringCallback asc,
650 if ( (0 == addrlen) &&
651 (0 == strcmp (type, "dv")) )
652 asc (asc_cls, "dv", GNUNET_OK);
654 asc (asc_cls, NULL, GNUNET_SYSERR);
656 asc (asc_cls, NULL, GNUNET_OK);
661 * Convert the DV address to a pretty string.
664 * @param addr the (hopefully) DV address
665 * @param addrlen the length of the @a addr
666 * @return string representing the DV address
669 dv_plugin_address_to_string (void *cls,
675 GNUNET_break (0); /* malformed */
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).
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
699 dv_plugin_check_address (void *cls,
704 return GNUNET_SYSERR;
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
714 * @param cls the plugin
715 * @param address the address
716 * @return the session if the address is valid, NULL otherwise
718 static struct Session *
719 dv_get_session (void *cls,
720 const struct GNUNET_HELLO_Address *address)
722 struct Plugin *plugin = cls;
723 struct Session *session;
725 if (0 != address->address_length)
727 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
730 return NULL; /* not valid right now */
731 session->active = GNUNET_YES;
737 * Function called to convert a string address to
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
749 dv_plugin_string_to_address (void *cls,
755 if ( (addrlen == 3) &&
756 (0 == strcmp ("dv", addr)) )
761 return GNUNET_SYSERR;
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
770 * @param cls closure (`struct Plugin *`)
771 * @param peer which peer was the session for
772 * @param session which session is being updated
775 dv_plugin_update_session_timeout (void *cls,
776 const struct GNUNET_PeerIdentity *peer,
777 struct Session *session)
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?) */
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.
791 * @param cls closure (`struct Plugin *`)
792 * @param session the session
793 * @return the network type
795 static enum GNUNET_ATS_Network_Type
796 dv_get_network (void *cls,
797 struct Session *session)
799 GNUNET_assert (NULL != session);
800 return session->network;
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.
809 * @param cls closure with the `struct Plugin`
810 * @return keepalive factor
813 dv_plugin_query_keepalive_factor (void *cls)
820 * Entry point for the plugin.
822 * @param cls closure with the plugin environment
826 libgnunet_plugin_transport_dv_init (void *cls)
828 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
829 struct GNUNET_TRANSPORT_PluginFunctions *api;
830 struct Plugin *plugin;
832 plugin = GNUNET_new (struct Plugin);
834 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
835 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
837 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
840 &handle_dv_distance_changed,
841 &handle_dv_disconnect,
842 &handle_dv_message_received);
843 if (NULL == plugin->dvh)
845 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
846 GNUNET_SERVER_mst_destroy (plugin->mst);
847 GNUNET_free (plugin);
850 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
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;
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 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 */