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 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;
91 * Session handle for connections.
97 * Mandatory session header.
99 struct SessionHeader header;
102 * Pointer to the global plugin struct.
104 struct Plugin *plugin;
107 * Head of pending requests.
109 struct PendingRequest *pr_head;
112 * Tail of pending requests.
114 struct PendingRequest *pr_tail;
117 * To whom are we talking to.
119 struct GNUNET_PeerIdentity sender;
122 * Current distance to the given peer.
127 * Current network the next hop peer is located in
129 enum GNUNET_ATS_Network_Type network;
132 * Does the transport service know about this session (and we thus
133 * need to call `session_end` when it is released?)
141 * Encapsulation of all of the state of the plugin.
148 struct GNUNET_TRANSPORT_PluginEnvironment *env;
151 * Hash map of sessions (active and inactive).
153 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
156 * Copy of the handler array where the closures are
157 * set to this struct's instance.
159 struct GNUNET_SERVER_MessageHandler *handlers;
162 * Handle to the DV service
164 struct GNUNET_DV_ServiceHandle *dvh;
167 * Tokenizer for boxed messages.
169 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
175 * Notify transport service about the change in distance.
177 * @param session session where the distance changed
180 notify_distance_change (struct Session *session)
182 struct Plugin *plugin = session->plugin;
183 struct GNUNET_ATS_Information ats;
185 if (GNUNET_YES != session->active)
187 ats.type = htonl ((uint32_t) GNUNET_ATS_QUALITY_NET_DISTANCE);
188 ats.value = htonl (session->distance);
189 plugin->env->update_address_metrics (plugin->env->cls,
198 * Function called by MST on each message from the box.
200 * @param cls closure with the `struct Plugin *`
201 * @param client identification of the client (with the 'struct Session')
202 * @param message the actual message
203 * @return #GNUNET_OK on success
208 const struct GNUNET_MessageHeader *message)
210 struct Plugin *plugin = cls;
211 struct Session *session = client;
212 struct GNUNET_ATS_Information ats;
214 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
215 ats.value = htonl (session->distance);
216 session->active = GNUNET_YES;
217 plugin->env->receive (plugin->env->cls,
221 plugin->env->update_address_metrics (plugin->env->cls,
222 &session->sender, NULL,
230 * Handler for messages received from the DV service.
232 * @param cls closure with the plugin
233 * @param sender sender of the message
234 * @param distance how far did the message travel
235 * @param msg actual message payload
238 handle_dv_message_received (void *cls,
239 const struct GNUNET_PeerIdentity *sender,
241 const struct GNUNET_MessageHeader *msg)
243 struct Plugin *plugin = cls;
244 struct GNUNET_ATS_Information ats;
245 struct Session *session;
247 LOG (GNUNET_ERROR_TYPE_DEBUG,
248 "Received `%s' message for peer `%s': new distance %u\n",
249 "DV_MESSAGE_RECEIVED",
250 GNUNET_i2s (sender), distance);
251 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
258 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
260 /* need to unbox using MST */
261 GNUNET_SERVER_mst_receive (plugin->mst,
263 (const char *) &msg[1],
264 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
269 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
270 ats.value = htonl (distance);
271 session->active = GNUNET_YES;
272 plugin->env->receive (plugin->env->cls, sender,
275 plugin->env->update_address_metrics (plugin->env->cls,
283 * Function called if DV starts to be able to talk to a peer.
285 * @param cls closure with `struct Plugin *`
286 * @param peer newly connected peer
287 * @param distance distance to the peer
288 * @param network the network the next hop is located in
291 handle_dv_connect (void *cls,
292 const struct GNUNET_PeerIdentity *peer,
294 enum GNUNET_ATS_Network_Type network)
296 struct Plugin *plugin = cls;
297 struct Session *session;
298 struct GNUNET_ATS_Information ats[2];
300 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
302 * This requires transport plugin to be linked to libgnunetats.
303 * If you remove it, also remove libgnunetats linkage from Makefile.am
305 LOG (GNUNET_ERROR_TYPE_DEBUG,
306 "Received `%s' message for peer `%s' with next hop in network %s\n",
309 GNUNET_ATS_print_network_type (network));
311 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
316 session->distance = distance;
317 notify_distance_change (session);
318 return; /* nothing to do */
321 session = GNUNET_new (struct Session);
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 session %p for peer `%s'\n",
336 /* Notify transport and ats about new connection */
337 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
338 ats[0].value = htonl (distance);
339 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
340 ats[1].value = htonl ((uint32_t) network);
341 session->active = GNUNET_YES;
342 plugin->env->session_start (plugin->env->cls, peer,
350 * Function called if DV distance to a peer is changed.
352 * @param cls closure with `struct Plugin *`
353 * @param peer connected peer
354 * @param distance new distance to the peer
355 * @param network network type used for the connection
358 handle_dv_distance_changed (void *cls,
359 const struct GNUNET_PeerIdentity *peer,
361 enum GNUNET_ATS_Network_Type network)
363 struct Plugin *plugin = cls;
364 struct Session *session;
366 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network);
367 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 "Received `%s' message for peer `%s': new distance %u\n",
369 "DV_DISTANCE_CHANGED",
372 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
377 handle_dv_connect (plugin, peer, distance, network);
380 session->distance = distance;
381 notify_distance_change (session);
386 * Release session object and clean up associated resources.
388 * @param session session to clean up
391 free_session (struct Session *session)
393 struct Plugin *plugin = session->plugin;
394 struct PendingRequest *pr;
396 GNUNET_assert (GNUNET_YES ==
397 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "Freeing session %p for peer `%s'\n",
404 GNUNET_i2s (&session->sender));
405 if (GNUNET_YES == session->active)
407 plugin->env->session_end (plugin->env->cls,
410 session->active = GNUNET_NO;
412 while (NULL != (pr = session->pr_head))
414 GNUNET_CONTAINER_DLL_remove (session->pr_head,
417 GNUNET_DV_send_cancel (pr->th);
419 if (NULL != pr->transmit_cont)
420 pr->transmit_cont (pr->transmit_cont_cls,
422 GNUNET_SYSERR, 0, 0);
425 GNUNET_free (session);
430 * Function called if DV is no longer able to talk to a peer.
432 * @param cls closure with `struct Plugin *`
433 * @param peer peer that disconnected
436 handle_dv_disconnect (void *cls,
437 const struct GNUNET_PeerIdentity *peer)
439 struct Plugin *plugin = cls;
440 struct Session *session;
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Received `%s' message for peer `%s'\n",
446 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
449 return; /* nothing to do */
450 free_session (session);
455 * Function called once the delivery of a message has been successful.
456 * Clean up the pending request, and call continuations.
459 * @param ok #GNUNET_OK on success, #GNUNET_SYSERR on error
462 send_finished (void *cls,
465 struct PendingRequest *pr = cls;
466 struct Session *session = pr->session;
469 GNUNET_CONTAINER_DLL_remove (session->pr_head,
472 if (NULL != pr->transmit_cont)
473 pr->transmit_cont (pr->transmit_cont_cls,
481 * Function that can be used by the transport service to transmit
482 * a message using the plugin.
485 * @param session the session used
486 * @param priority how important is the message
487 * @param msgbuf the message to transmit
488 * @param msgbuf_size number of bytes in 'msgbuf'
489 * @param timeout when should we time out
490 * @param cont continuation to call once the message has
491 * been transmitted (or if the transport is ready
492 * for the next transmission call; or if the
493 * peer disconnected...)
494 * @param cont_cls closure for @a cont
495 * @return number of bytes used (on the physical network, with overheads);
496 * -1 on hard errors (i.e. address invalid); 0 is a legal value
497 * and does NOT mean that the message was not transmitted (DV)
500 dv_plugin_send (void *cls,
501 struct Session *session,
504 unsigned int priority,
505 struct GNUNET_TIME_Relative timeout,
506 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
508 struct Plugin *plugin = cls;
509 struct PendingRequest *pr;
510 const struct GNUNET_MessageHeader *msg;
511 struct GNUNET_MessageHeader *box;
514 msg = (const struct GNUNET_MessageHeader *) msgbuf;
515 if (ntohs (msg->size) != msgbuf_size)
518 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
519 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
520 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
521 memcpy (&box[1], msgbuf, msgbuf_size);
524 pr = GNUNET_new (struct PendingRequest);
525 pr->transmit_cont = cont;
526 pr->transmit_cont_cls = cont_cls;
527 pr->session = session;
528 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
532 pr->th = GNUNET_DV_send (plugin->dvh,
537 GNUNET_free_non_null (box);
543 * Function that can be used to force the plugin to disconnect
544 * from the given peer and cancel all previous transmissions
545 * (and their continuations).
547 * @param cls closure with the `struct Plugin *`
548 * @param target peer from which to disconnect
551 dv_plugin_disconnect_peer (void *cls,
552 const struct GNUNET_PeerIdentity *target)
554 struct Plugin *plugin = cls;
555 struct Session *session;
556 struct PendingRequest *pr;
558 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
561 return; /* nothing to do */
562 while (NULL != (pr = session->pr_head))
564 GNUNET_CONTAINER_DLL_remove (session->pr_head,
567 GNUNET_DV_send_cancel (pr->th);
569 if (NULL != pr->transmit_cont)
570 pr->transmit_cont (pr->transmit_cont_cls,
572 GNUNET_SYSERR, 0, 0);
575 session->active = GNUNET_NO;
580 * Function that can be used to force the plugin to disconnect
581 * from the given peer and cancel all previous transmissions
582 * (and their continuations).
584 * @param cls closure with the `struct Plugin *`
585 * @param session which session to disconnect
589 dv_plugin_disconnect_session (void *cls,
590 struct Session *session)
592 struct PendingRequest *pr;
594 while (NULL != (pr = session->pr_head))
596 GNUNET_CONTAINER_DLL_remove (session->pr_head,
599 GNUNET_DV_send_cancel (pr->th);
601 if (NULL != pr->transmit_cont)
602 pr->transmit_cont (pr->transmit_cont_cls,
604 GNUNET_SYSERR, 0, 0);
607 session->active = GNUNET_NO;
613 * Convert the transports address to a nice, human-readable
617 * @param type name of the transport that generated the address
618 * @param addr one of the addresses of the host, NULL for the last address
619 * the specific address format depends on the transport
620 * @param addrlen length of the address
621 * @param numeric should (IP) addresses be displayed in numeric form?
622 * @param timeout after how long should we give up?
623 * @param asc function to call on each string
624 * @param asc_cls closure for @a asc
627 dv_plugin_address_pretty_printer (void *cls, const char *type,
629 size_t addrlen, int numeric,
630 struct GNUNET_TIME_Relative timeout,
631 GNUNET_TRANSPORT_AddressStringCallback asc,
634 if ( (0 == addrlen) &&
635 (0 == strcmp (type, "dv")) )
642 * Convert the DV address to a pretty string.
645 * @param addr the (hopefully) DV address
646 * @param addrlen the length of the @a addr
647 * @return string representing the DV address
650 dv_plugin_address_to_string (void *cls,
656 GNUNET_break (0); /* malformed */
664 * Another peer has suggested an address for this peer and transport
665 * plugin. Check that this could be a valid address. This function
666 * is not expected to 'validate' the address in the sense of trying to
667 * connect to it but simply to see if the binary format is technically
668 * legal for establishing a connection to this peer (and make sure that
669 * the address really corresponds to our network connection/settings
670 * and not some potential man-in-the-middle).
673 * @param addr pointer to the address
674 * @param addrlen length of addr
675 * @return #GNUNET_OK if this is a plausible address for this peer
676 * and transport, #GNUNET_SYSERR if not
680 dv_plugin_check_address (void *cls,
685 return GNUNET_SYSERR;
691 * Create a new session to transmit data to the target
692 * This session will used to send data to this peer and the plugin will
693 * notify us by calling the env->session_end function
695 * @param cls the plugin
696 * @param address the address
697 * @return the session if the address is valid, NULL otherwise
699 static struct Session *
700 dv_get_session (void *cls,
701 const struct GNUNET_HELLO_Address *address)
703 struct Plugin *plugin = cls;
704 struct Session *session;
706 if (0 != address->address_length)
708 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
711 return NULL; /* not valid right now */
712 session->active = GNUNET_YES;
718 * Function called to convert a string address to
721 * @param cls closure ('struct Plugin*')
722 * @param addr string address
723 * @param addrlen length of the @a addr including \0 termination
724 * @param buf location to store the buffer
725 * If the function returns #GNUNET_SYSERR, its contents are undefined.
726 * @param added length of created address
727 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
730 dv_plugin_string_to_address (void *cls,
736 if ( (addrlen == 3) &&
737 (0 == strcmp ("dv", addr)) )
742 return GNUNET_SYSERR;
748 * Function to obtain the network type for a session
749 * FIXME: we should probably look at the network type
750 * used by the next hop here. Or find some other way
751 * to properly allow ATS-DV resource allocation.
753 * @param cls closure (`struct Plugin *`)
754 * @param session the session
755 * @return the network type
757 static enum GNUNET_ATS_Network_Type
758 dv_get_network (void *cls,
759 struct Session *session)
761 GNUNET_assert (NULL != session);
762 return session->network;
767 * Entry point for the plugin.
769 * @param cls closure with the plugin environment
773 libgnunet_plugin_transport_dv_init (void *cls)
775 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
776 struct GNUNET_TRANSPORT_PluginFunctions *api;
777 struct Plugin *plugin;
779 plugin = GNUNET_new (struct Plugin);
781 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
782 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
784 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
787 &handle_dv_distance_changed,
788 &handle_dv_disconnect,
789 &handle_dv_message_received);
790 if (NULL == plugin->dvh)
792 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
793 GNUNET_SERVER_mst_destroy (plugin->mst);
794 GNUNET_free (plugin);
797 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
799 api->send = &dv_plugin_send;
800 api->disconnect_peer = &dv_plugin_disconnect_peer;
801 api->disconnect_session = &dv_plugin_disconnect_session;
802 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
803 api->check_address = &dv_plugin_check_address;
804 api->address_to_string = &dv_plugin_address_to_string;
805 api->string_to_address = &dv_plugin_string_to_address;
806 api->get_session = &dv_get_session;
807 api->get_network = &dv_get_network;
813 * Function called to free a session.
817 * @param value session to free
818 * @return #GNUNET_OK (continue to iterate)
821 free_session_iterator (void *cls,
822 const struct GNUNET_PeerIdentity *key,
825 struct Session *session = value;
827 free_session (session);
833 * Exit point from the plugin.
835 * @param cls plugin API
839 libgnunet_plugin_transport_dv_done (void *cls)
841 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
842 struct Plugin *plugin = api->cls;
844 GNUNET_DV_service_disconnect (plugin->dvh);
845 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
846 &free_session_iterator,
848 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
849 GNUNET_SERVER_mst_destroy (plugin->mst);
850 GNUNET_free (plugin);
855 /* end of plugin_transport_dv.c */