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
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 ats.type = htonl ((uint32_t) GNUNET_ATS_QUALITY_NET_DISTANCE);
186 ats.value = htonl (session->distance);
187 plugin->env->update_address_metrics (plugin->env->cls,
196 * Function called by MST on each message from the box.
198 * @param cls closure with the 'struct Plugin'
199 * @param client identification of the client (with the 'struct Session')
200 * @param message the actual message
201 * @return GNUNET_OK on success
206 const struct GNUNET_MessageHeader *message)
208 struct Plugin *plugin = cls;
209 struct Session *session = client;
210 struct GNUNET_ATS_Information ats;
212 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
213 ats.value = htonl (session->distance);
214 session->active = GNUNET_YES;
215 plugin->env->receive (plugin->env->cls,
219 plugin->env->update_address_metrics (plugin->env->cls,
220 &session->sender, NULL, 0, session, &ats, 1);
226 * Handler for messages received from the DV service.
228 * @param cls closure with the plugin
229 * @param sender sender of the message
230 * @param distance how far did the message travel
231 * @param msg actual message payload
234 handle_dv_message_received (void *cls,
235 const struct GNUNET_PeerIdentity *sender,
237 const struct GNUNET_MessageHeader *msg)
239 struct Plugin *plugin = cls;
240 struct GNUNET_ATS_Information ats;
241 struct Session *session;
243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s': new distance %u\n",
244 "DV_MESSAGE_RECEIVED",
245 GNUNET_i2s (sender), distance);
248 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
255 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
257 /* need to unbox using MST */
258 GNUNET_SERVER_mst_receive (plugin->mst,
260 (const char *) &msg[1],
261 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
266 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
267 ats.value = htonl (distance);
268 session->active = GNUNET_YES;
269 plugin->env->receive (plugin->env->cls, sender,
272 plugin->env->update_address_metrics (plugin->env->cls,
273 sender, "", 0, session, &ats, 1);
278 * Function called if DV starts to be able to talk to a peer.
280 * @param cls closure with 'struct Plugin'
281 * @param peer newly connected peer
282 * @param distance distance to the peer
285 handle_dv_connect (void *cls,
286 const struct GNUNET_PeerIdentity *peer,
287 uint32_t distance, uint32_t network)
289 struct Plugin *plugin = cls;
290 struct Session *session;
291 struct GNUNET_ATS_Information ats[2];
293 LOG (GNUNET_ERROR_TYPE_DEBUG,
294 "Received `%s' message for peer `%s' with next hop in network %s \n",
297 GNUNET_ATS_print_network_type (network));
299 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
304 session->distance = distance;
305 if (GNUNET_YES == session->active)
306 notify_distance_change (session);
307 return; /* nothing to do */
309 session = GNUNET_new (struct Session);
310 session->sender = *peer;
311 session->distance = distance;
312 session->network = network;
313 GNUNET_assert (GNUNET_YES ==
314 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
319 /* Notify transport and ats about new connection */
320 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
321 ats[0].value = htonl (distance);
322 ats[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
323 ats[0].value = htonl (network);
324 plugin->env->session_start (plugin->env->cls, peer, PLUGIN_NAME, NULL, 0,
330 * Function called if DV distance to a peer is changed.
332 * @param cls closure with 'struct Plugin'
333 * @param peer connected peer
334 * @param distance new distance to the peer
337 handle_dv_distance_changed (void *cls,
338 const struct GNUNET_PeerIdentity *peer,
341 struct Plugin *plugin = cls;
342 struct Session *session;
344 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s': new distance %u\n",
345 "DV_DISTANCE_CHANGED",
346 GNUNET_i2s (peer), distance);
348 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
354 handle_dv_connect (plugin, peer, distance, 0);
357 session->distance = distance;
358 if (GNUNET_YES == session->active)
359 notify_distance_change (session);
364 * Release session object and clean up associated resources.
366 * @param session session to clean up
369 free_session (struct Session *session)
371 struct Plugin *plugin = session->plugin;
372 struct PendingRequest *pr;
374 GNUNET_assert (GNUNET_YES ==
375 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
378 if (GNUNET_YES == session->active)
379 plugin->env->session_end (plugin->env->cls,
382 while (NULL != (pr = session->pr_head))
384 GNUNET_CONTAINER_DLL_remove (session->pr_head,
387 GNUNET_DV_send_cancel (pr->th);
389 if (NULL != pr->transmit_cont)
390 pr->transmit_cont (pr->transmit_cont_cls,
392 GNUNET_SYSERR, 0, 0);
395 GNUNET_free (session);
400 * Function called if DV is no longer able to talk to a peer.
402 * @param cls closure with 'struct Plugin'
403 * @param peer peer that disconnected
406 handle_dv_disconnect (void *cls,
407 const struct GNUNET_PeerIdentity *peer)
409 struct Plugin *plugin = cls;
410 struct Session *session;
412 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for peer `%s'\n",
416 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
419 return; /* nothing to do */
421 free_session (session);
426 * Function called once the delivery of a message has been successful.
427 * Clean up the pending request, and call continuations.
430 * @param ok GNUNET_OK on success, GNUNET_SYSERR on error
433 send_finished (void *cls,
436 struct PendingRequest *pr = cls;
437 struct Session *session = pr->session;
440 GNUNET_CONTAINER_DLL_remove (session->pr_head,
443 if (NULL != pr->transmit_cont)
444 pr->transmit_cont (pr->transmit_cont_cls,
452 * Function that can be used by the transport service to transmit
453 * a message using the plugin.
456 * @param session the session used
457 * @param priority how important is the message
458 * @param msgbuf the message to transmit
459 * @param msgbuf_size number of bytes in 'msgbuf'
460 * @param timeout when should we time out
461 * @param cont continuation to call once the message has
462 * been transmitted (or if the transport is ready
463 * for the next transmission call; or if the
464 * peer disconnected...)
465 * @param cont_cls closure for cont
466 * @return number of bytes used (on the physical network, with overheads);
467 * -1 on hard errors (i.e. address invalid); 0 is a legal value
468 * and does NOT mean that the message was not transmitted (DV)
471 dv_plugin_send (void *cls,
472 struct Session *session,
473 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
474 struct GNUNET_TIME_Relative timeout,
475 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
477 struct PendingRequest *pr;
478 const struct GNUNET_MessageHeader *msg;
479 struct GNUNET_MessageHeader *box;
482 msg = (const struct GNUNET_MessageHeader *) msgbuf;
483 if (ntohs (msg->size) != msgbuf_size)
486 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
487 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
488 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
489 memcpy (&box[1], msgbuf, msgbuf_size);
492 pr = GNUNET_new (struct PendingRequest);
493 pr->transmit_cont = cont;
494 pr->transmit_cont_cls = cont_cls;
495 pr->session = session;
496 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
499 pr->th = GNUNET_DV_send (session->plugin->dvh,
504 GNUNET_free_non_null (box);
510 * Function that can be used to force the plugin to disconnect
511 * from the given peer and cancel all previous transmissions
512 * (and their continuations).
515 * @param target peer from which to disconnect
518 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
520 struct Plugin *plugin = cls;
521 struct Session *session;
522 struct PendingRequest *pr;
524 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
527 return; /* nothing to do */
528 while (NULL != (pr = session->pr_head))
530 GNUNET_CONTAINER_DLL_remove (session->pr_head,
533 GNUNET_DV_send_cancel (pr->th);
535 if (NULL != pr->transmit_cont)
536 pr->transmit_cont (pr->transmit_cont_cls,
538 GNUNET_SYSERR, 0, 0);
541 session->active = GNUNET_NO;
546 * Convert the transports address to a nice, human-readable
550 * @param type name of the transport that generated the address
551 * @param addr one of the addresses of the host, NULL for the last address
552 * the specific address format depends on the transport
553 * @param addrlen length of the address
554 * @param numeric should (IP) addresses be displayed in numeric form?
555 * @param timeout after how long should we give up?
556 * @param asc function to call on each string
557 * @param asc_cls closure for asc
560 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
561 size_t addrlen, int numeric,
562 struct GNUNET_TIME_Relative timeout,
563 GNUNET_TRANSPORT_AddressStringCallback asc,
566 if ( (0 == addrlen) &&
567 (0 == strcmp (type, "dv")) )
574 * Convert the DV address to a pretty string.
577 * @param addr the (hopefully) DV address
578 * @param addrlen the length of the address
580 * @return string representing the DV address
583 dv_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
587 GNUNET_break (0); /* malformed */
595 * Another peer has suggested an address for this peer and transport
596 * plugin. Check that this could be a valid address. This function
597 * is not expected to 'validate' the address in the sense of trying to
598 * connect to it but simply to see if the binary format is technically
599 * legal for establishing a connection to this peer (and make sure that
600 * the address really corresponds to our network connection/settings
601 * and not some potential man-in-the-middle).
604 * @param addr pointer to the address
605 * @param addrlen length of addr
606 * @return GNUNET_OK if this is a plausible address for this peer
607 * and transport, GNUNET_SYSERR if not
611 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
614 return GNUNET_SYSERR;
620 * Create a new session to transmit data to the target
621 * This session will used to send data to this peer and the plugin will
622 * notify us by calling the env->session_end function
624 * @param cls the plugin
625 * @param address the address
626 * @return the session if the address is valid, NULL otherwise
628 static struct Session *
629 dv_get_session (void *cls,
630 const struct GNUNET_HELLO_Address *address)
632 struct Plugin *plugin = cls;
633 struct Session *session;
635 if (0 != address->address_length)
637 session = GNUNET_CONTAINER_multipeermap_get (plugin->sessions,
640 return NULL; /* not valid right now */
641 session->active = GNUNET_YES;
647 * Function called to convert a string address to
650 * @param cls closure ('struct Plugin*')
651 * @param addr string address
652 * @param addrlen length of the address including \0 termination
653 * @param buf location to store the buffer
654 * If the function returns GNUNET_SYSERR, its contents are undefined.
655 * @param added length of created address
656 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
659 dv_plugin_string_to_address (void *cls,
665 if ( (addrlen == 3) &&
666 (0 == strcmp ("dv", addr)) )
671 return GNUNET_SYSERR;
677 * Function to obtain the network type for a session
678 * FIXME: we should probably look at the network type
679 * used by the next hop here. Or find some other way
680 * to properly allow ATS-DV resource allocation.
682 * @param cls closure ('struct Plugin*')
683 * @param session the session
684 * @return the network type
686 static enum GNUNET_ATS_Network_Type
687 dv_get_network (void *cls,
688 struct Session *session)
690 GNUNET_assert (NULL != session);
691 return session->network;
696 * Entry point for the plugin.
698 * @param cls closure with the plugin environment
702 libgnunet_plugin_transport_dv_init (void *cls)
704 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
705 struct GNUNET_TRANSPORT_PluginFunctions *api;
706 struct Plugin *plugin;
708 plugin = GNUNET_new (struct Plugin);
710 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (1024 * 8, GNUNET_YES);
711 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
713 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
716 &handle_dv_distance_changed,
717 &handle_dv_disconnect,
718 &handle_dv_message_received);
719 if (NULL == plugin->dvh)
721 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
722 GNUNET_SERVER_mst_destroy (plugin->mst);
723 GNUNET_free (plugin);
726 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
728 api->send = &dv_plugin_send;
729 api->disconnect = &dv_plugin_disconnect;
730 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
731 api->check_address = &dv_plugin_check_address;
732 api->address_to_string = &dv_plugin_address_to_string;
733 api->string_to_address = &dv_plugin_string_to_address;
734 api->get_session = &dv_get_session;
735 api->get_network = &dv_get_network;
741 * Function called to free a session.
745 * @param value session to free
746 * @return GNUNET_OK (continue to iterate)
749 free_session_iterator (void *cls,
750 const struct GNUNET_PeerIdentity *key,
753 struct Session *session = value;
755 free_session (session);
761 * Exit point from the plugin.
763 * @param cls plugin API
767 libgnunet_plugin_transport_dv_done (void *cls)
769 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
770 struct Plugin *plugin = api->cls;
772 GNUNET_DV_service_disconnect (plugin->dvh);
773 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
774 &free_session_iterator,
776 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
777 GNUNET_SERVER_mst_destroy (plugin->mst);
778 GNUNET_free (plugin);
783 /* end of plugin_transport_dv.c */