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__)
43 * Encapsulation of all of the state of the plugin.
49 * An active request for transmission via DV.
57 struct PendingRequest *next;
62 struct PendingRequest *prev;
65 * Continuation function to call once the transmission buffer
66 * has again space available. NULL if there is no
67 * continuation to call.
69 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
72 * Closure for transmit_cont.
74 void *transmit_cont_cls;
77 * Transmission handle from DV client library.
79 struct GNUNET_DV_TransmitHandle *th;
82 * Session of this request.
84 struct Session *session;
90 * Session handle for connections.
96 * Mandatory session header.
98 struct SessionHeader header;
101 * Pointer to the global plugin struct.
103 struct Plugin *plugin;
106 * Head of pending requests.
108 struct PendingRequest *pr_head;
111 * Tail of pending requests.
113 struct PendingRequest *pr_tail;
116 * To whom are we talking to.
118 struct GNUNET_PeerIdentity sender;
121 * Current distance to the given peer.
126 * Does the transport service know about this session (and we thus
127 * need to call 'session_end' when it is released?)
135 * Encapsulation of all of the state of the plugin.
142 struct GNUNET_TRANSPORT_PluginEnvironment *env;
145 * Hash map of sessions (active and inactive).
147 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
150 * Copy of the handler array where the closures are
151 * set to this struct's instance.
153 struct GNUNET_SERVER_MessageHandler *handlers;
156 * Handle to the DV service
158 struct GNUNET_DV_ServiceHandle *dvh;
161 * Tokenizer for boxed messages.
163 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
169 * Notify transport service about the change in distance.
171 * @param session session where the distance changed
174 notify_distance_change (struct Session *session)
176 struct Plugin *plugin = session->plugin;
177 struct GNUNET_ATS_Information ats;
179 ats.type = htonl ((uint32_t) GNUNET_ATS_QUALITY_NET_DISTANCE);
180 ats.value = htonl (session->distance);
181 plugin->env->update_address_metrics (plugin->env->cls,
190 * Function called by MST on each message from the box.
192 * @param cls closure with the 'struct Plugin'
193 * @param client identification of the client (with the 'struct Session')
194 * @param message the actual message
195 * @return GNUNET_OK on success
200 const struct GNUNET_MessageHeader *message)
202 struct Plugin *plugin = cls;
203 struct Session *session = client;
204 struct GNUNET_ATS_Information ats;
206 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
207 ats.value = htonl (session->distance);
208 session->active = GNUNET_YES;
209 plugin->env->receive (plugin->env->cls,
213 plugin->env->update_address_metrics (plugin->env->cls,
214 &session->sender, "", 0, session, &ats, 1);
220 * Handler for messages received from the DV service.
222 * @param cls closure with the plugin
223 * @param sender sender of the message
224 * @param distance how far did the message travel
225 * @param msg actual message payload
228 handle_dv_message_received (void *cls,
229 const struct GNUNET_PeerIdentity *sender,
231 const struct GNUNET_MessageHeader *msg)
233 struct Plugin *plugin = cls;
234 struct GNUNET_ATS_Information ats;
235 struct Session *session;
237 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s': new distance %u\n",
238 "DV_MESSAGE_RECEIVED",
239 GNUNET_i2s (sender), distance);
242 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
249 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
251 /* need to unbox using MST */
252 GNUNET_SERVER_mst_receive (plugin->mst,
254 (const char *) &msg[1],
255 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
260 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
261 ats.value = htonl (distance);
262 session->active = GNUNET_YES;
263 plugin->env->receive (plugin->env->cls, sender,
266 plugin->env->update_address_metrics (plugin->env->cls,
267 sender, "", 0, session, &ats, 1);
272 * Function called if DV starts to be able to talk to a peer.
274 * @param cls closure with 'struct Plugin'
275 * @param peer newly connected peer
276 * @param distance distance to the peer
279 handle_dv_connect (void *cls,
280 const struct GNUNET_PeerIdentity *peer,
283 struct Plugin *plugin = cls;
284 struct Session *session;
286 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s'\n",
290 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
295 session->distance = distance;
296 if (GNUNET_YES == session->active)
297 notify_distance_change (session);
298 return; /* nothing to do */
300 session = GNUNET_new (struct Session);
301 session->sender = *peer;
302 session->distance = distance;
303 GNUNET_assert (GNUNET_YES ==
304 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
312 * Function called if DV distance to a peer is changed.
314 * @param cls closure with 'struct Plugin'
315 * @param peer connected peer
316 * @param distance new distance to the peer
319 handle_dv_distance_changed (void *cls,
320 const struct GNUNET_PeerIdentity *peer,
323 struct Plugin *plugin = cls;
324 struct Session *session;
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s': new distance %u\n",
327 "DV_DISTANCE_CHANGED",
328 GNUNET_i2s (peer), distance);
330 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
335 handle_dv_connect (plugin, peer, distance);
338 session->distance = distance;
339 if (GNUNET_YES == session->active)
340 notify_distance_change (session);
345 * Release session object and clean up associated resources.
347 * @param session session to clean up
350 free_session (struct Session *session)
352 struct Plugin *plugin = session->plugin;
353 struct PendingRequest *pr;
355 GNUNET_assert (GNUNET_YES ==
356 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
359 if (GNUNET_YES == session->active)
360 plugin->env->session_end (plugin->env->cls,
363 while (NULL != (pr = session->pr_head))
365 GNUNET_CONTAINER_DLL_remove (session->pr_head,
368 GNUNET_DV_send_cancel (pr->th);
370 if (NULL != pr->transmit_cont)
371 pr->transmit_cont (pr->transmit_cont_cls,
373 GNUNET_SYSERR, 0, 0);
376 GNUNET_free (session);
381 * Function called if DV is no longer able to talk to a peer.
383 * @param cls closure with 'struct Plugin'
384 * @param peer peer that disconnected
387 handle_dv_disconnect (void *cls,
388 const struct GNUNET_PeerIdentity *peer)
390 struct Plugin *plugin = cls;
391 struct Session *session;
393 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s'\n",
397 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
400 return; /* nothing to do */
401 free_session (session);
406 * Function called once the delivery of a message has been successful.
407 * Clean up the pending request, and call continuations.
410 * @param ok GNUNET_OK on success, GNUNET_SYSERR on error
413 send_finished (void *cls,
416 struct PendingRequest *pr = cls;
417 struct Session *session = pr->session;
420 GNUNET_CONTAINER_DLL_remove (session->pr_head,
423 if (NULL != pr->transmit_cont)
424 pr->transmit_cont (pr->transmit_cont_cls,
432 * Function that can be used by the transport service to transmit
433 * a message using the plugin.
436 * @param session the session used
437 * @param priority how important is the message
438 * @param msgbuf the message to transmit
439 * @param msgbuf_size number of bytes in 'msgbuf'
440 * @param timeout when should we time out
441 * @param cont continuation to call once the message has
442 * been transmitted (or if the transport is ready
443 * for the next transmission call; or if the
444 * peer disconnected...)
445 * @param cont_cls closure for cont
446 * @return number of bytes used (on the physical network, with overheads);
447 * -1 on hard errors (i.e. address invalid); 0 is a legal value
448 * and does NOT mean that the message was not transmitted (DV)
451 dv_plugin_send (void *cls,
452 struct Session *session,
453 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
454 struct GNUNET_TIME_Relative timeout,
455 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
457 struct PendingRequest *pr;
458 const struct GNUNET_MessageHeader *msg;
459 struct GNUNET_MessageHeader *box;
462 msg = (const struct GNUNET_MessageHeader *) msgbuf;
463 if (ntohs (msg->size) != msgbuf_size)
466 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
467 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
468 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
469 memcpy (&box[1], msgbuf, msgbuf_size);
472 pr = GNUNET_new (struct PendingRequest);
473 pr->transmit_cont = cont;
474 pr->transmit_cont_cls = cont_cls;
475 pr->session = session;
476 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
479 pr->th = GNUNET_DV_send (session->plugin->dvh,
484 GNUNET_free_non_null (box);
490 * Function that can be used to force the plugin to disconnect
491 * from the given peer and cancel all previous transmissions
492 * (and their continuations).
495 * @param target peer from which to disconnect
498 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
500 struct Plugin *plugin = cls;
501 struct Session *session;
502 struct PendingRequest *pr;
504 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
507 return; /* nothing to do */
508 while (NULL != (pr = session->pr_head))
510 GNUNET_CONTAINER_DLL_remove (session->pr_head,
513 GNUNET_DV_send_cancel (pr->th);
515 if (NULL != pr->transmit_cont)
516 pr->transmit_cont (pr->transmit_cont_cls,
518 GNUNET_SYSERR, 0, 0);
521 session->active = GNUNET_NO;
526 * Convert the transports address to a nice, human-readable
530 * @param type name of the transport that generated the address
531 * @param addr one of the addresses of the host, NULL for the last address
532 * the specific address format depends on the transport
533 * @param addrlen length of the address
534 * @param numeric should (IP) addresses be displayed in numeric form?
535 * @param timeout after how long should we give up?
536 * @param asc function to call on each string
537 * @param asc_cls closure for asc
540 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
541 size_t addrlen, int numeric,
542 struct GNUNET_TIME_Relative timeout,
543 GNUNET_TRANSPORT_AddressStringCallback asc,
546 if ( (0 == addrlen) &&
547 (0 == strcmp (type, "dv")) )
554 * Convert the DV address to a pretty string.
557 * @param addr the (hopefully) DV address
558 * @param addrlen the length of the address
560 * @return string representing the DV address
563 dv_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
567 GNUNET_break (0); /* malformed */
575 * Another peer has suggested an address for this peer and transport
576 * plugin. Check that this could be a valid address. This function
577 * is not expected to 'validate' the address in the sense of trying to
578 * connect to it but simply to see if the binary format is technically
579 * legal for establishing a connection to this peer (and make sure that
580 * the address really corresponds to our network connection/settings
581 * and not some potential man-in-the-middle).
584 * @param addr pointer to the address
585 * @param addrlen length of addr
586 * @return GNUNET_OK if this is a plausible address for this peer
587 * and transport, GNUNET_SYSERR if not
591 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
594 return GNUNET_SYSERR;
600 * Create a new session to transmit data to the target
601 * This session will used to send data to this peer and the plugin will
602 * notify us by calling the env->session_end function
604 * @param cls the plugin
605 * @param address the address
606 * @return the session if the address is valid, NULL otherwise
608 static struct Session *
609 dv_get_session (void *cls,
610 const struct GNUNET_HELLO_Address *address)
612 struct Plugin *plugin = cls;
613 struct Session *session;
615 if (0 != address->address_length)
617 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
620 return NULL; /* not valid right now */
621 session->active = GNUNET_YES;
627 * Function called to convert a string address to
630 * @param cls closure ('struct Plugin*')
631 * @param addr string address
632 * @param addrlen length of the address including \0 termination
633 * @param buf location to store the buffer
634 * If the function returns GNUNET_SYSERR, its contents are undefined.
635 * @param added length of created address
636 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
639 dv_plugin_string_to_address (void *cls,
645 if ( (addrlen == 3) &&
646 (0 == strcmp ("dv", addr)) )
651 return GNUNET_SYSERR;
657 * Function to obtain the network type for a session
658 * FIXME: we should probably look at the network type
659 * used by the next hop here. Or find some other way
660 * to properly allow ATS-DV resource allocation.
662 * @param cls closure ('struct Plugin*')
663 * @param session the session
664 * @return the network type
666 static enum GNUNET_ATS_Network_Type
667 dv_get_network (void *cls,
668 struct Session *session)
670 GNUNET_assert (NULL != session);
671 return GNUNET_ATS_NET_UNSPECIFIED;
676 * Entry point for the plugin.
678 * @param cls closure with the plugin environment
682 libgnunet_plugin_transport_dv_init (void *cls)
684 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
685 struct GNUNET_TRANSPORT_PluginFunctions *api;
686 struct Plugin *plugin;
688 plugin = GNUNET_new (struct Plugin);
690 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
691 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
693 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
696 &handle_dv_distance_changed,
697 &handle_dv_disconnect,
698 &handle_dv_message_received);
699 if (NULL == plugin->dvh)
701 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
702 GNUNET_SERVER_mst_destroy (plugin->mst);
703 GNUNET_free (plugin);
706 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
708 api->send = &dv_plugin_send;
709 api->disconnect = &dv_plugin_disconnect;
710 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
711 api->check_address = &dv_plugin_check_address;
712 api->address_to_string = &dv_plugin_address_to_string;
713 api->string_to_address = &dv_plugin_string_to_address;
714 api->get_session = &dv_get_session;
715 api->get_network = &dv_get_network;
721 * Function called to free a session.
725 * @param value session to free
726 * @return GNUNET_OK (continue to iterate)
729 free_session_iterator (void *cls,
730 const struct GNUNET_PeerIdentity *key,
733 struct Session *session = value;
735 free_session (session);
741 * Exit point from the plugin.
743 * @param cls plugin API
747 libgnunet_plugin_transport_dv_done (void *cls)
749 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
750 struct Plugin *plugin = api->cls;
752 GNUNET_DV_service_disconnect (plugin->dvh);
753 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
754 &free_session_iterator,
756 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
757 GNUNET_SERVER_mst_destroy (plugin->mst);
758 GNUNET_free (plugin);
763 /* end of plugin_transport_dv.c */