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_protocols.h"
31 #include "gnunet_connection_lib.h"
32 #include "gnunet_server_lib.h"
33 #include "gnunet_service_lib.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_dv_service.h"
36 #include "gnunet_transport_service.h"
37 #include "gnunet_transport_plugin.h"
41 #define LOG(kind,...) GNUNET_log_from (kind, "transport-dv",__VA_ARGS__)
45 * Encapsulation of all of the state of the plugin.
51 * An active request for transmission via DV.
59 struct PendingRequest *next;
64 struct PendingRequest *prev;
67 * Continuation function to call once the transmission buffer
68 * has again space available. NULL if there is no
69 * continuation to call.
71 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
74 * Closure for transmit_cont.
76 void *transmit_cont_cls;
79 * Transmission handle from DV client library.
81 struct GNUNET_DV_TransmitHandle *th;
84 * Session of this request.
86 struct Session *session;
92 * Session handle for connections.
98 * Mandatory session header.
100 struct SessionHeader header;
103 * Pointer to the global plugin struct.
105 struct Plugin *plugin;
108 * Head of pending requests.
110 struct PendingRequest *pr_head;
113 * Tail of pending requests.
115 struct PendingRequest *pr_tail;
118 * To whom are we talking to.
120 struct GNUNET_PeerIdentity sender;
123 * Current distance to the given peer.
128 * Does the transport service know about this session (and we thus
129 * need to call 'session_end' when it is released?)
137 * Encapsulation of all of the state of the plugin.
144 struct GNUNET_TRANSPORT_PluginEnvironment *env;
147 * Hash map of sessions (active and inactive).
149 struct GNUNET_CONTAINER_MultiHashMap *sessions;
152 * Copy of the handler array where the closures are
153 * set to this struct's instance.
155 struct GNUNET_SERVER_MessageHandler *handlers;
158 * Handle to the DV service
160 struct GNUNET_DV_ServiceHandle *dvh;
163 * Tokenizer for boxed messages.
165 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
171 * Notify transport service about the change in distance.
173 * @param session session where the distance changed
176 notify_distance_change (struct Session *session)
178 struct Plugin *plugin = session->plugin;
179 struct GNUNET_ATS_Information ats;
181 ats.type = htonl ((uint32_t) GNUNET_ATS_QUALITY_NET_DISTANCE);
182 ats.value = htonl (session->distance);
183 plugin->env->update_address_metrics (plugin->env->cls,
192 * Function called by MST on each message from the box.
194 * @param cls closure with the 'struct Plugin'
195 * @param client identification of the client (with the 'struct Session')
196 * @param message the actual message
197 * @return GNUNET_OK on success
202 const struct GNUNET_MessageHeader *message)
204 struct Plugin *plugin = cls;
205 struct Session *session = client;
206 struct GNUNET_ATS_Information ats;
208 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
209 ats.value = htonl (session->distance);
210 session->active = GNUNET_YES;
211 plugin->env->receive (plugin->env->cls,
215 plugin->env->update_address_metrics (plugin->env->cls,
216 &session->sender, "", 0, session, &ats, 1);
222 * Handler for messages received from the DV service.
224 * @param cls closure with the plugin
225 * @param sender sender of the message
226 * @param distance how far did the message travel
227 * @param msg actual message payload
230 handle_dv_message_received (void *cls,
231 const struct GNUNET_PeerIdentity *sender,
233 const struct GNUNET_MessageHeader *msg)
235 struct Plugin *plugin = cls;
236 struct GNUNET_ATS_Information ats;
237 struct Session *session;
239 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
240 &sender->hashPubKey);
246 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
248 /* need to unbox using MST */
249 GNUNET_SERVER_mst_receive (plugin->mst,
251 (const char *) &msg[1],
252 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
257 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
258 ats.value = htonl (distance);
259 session->active = GNUNET_YES;
260 plugin->env->receive (plugin->env->cls, sender,
263 plugin->env->update_address_metrics (plugin->env->cls,
264 sender, "", 0, session, &ats, 1);
269 * Function called if DV starts to be able to talk to a peer.
271 * @param cls closure with 'struct Plugin'
272 * @param peer newly connected peer
273 * @param distance distance to the peer
276 handle_dv_connect (void *cls,
277 const struct GNUNET_PeerIdentity *peer,
280 struct Plugin *plugin = cls;
281 struct Session *session;
283 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
288 session->distance = distance;
289 if (GNUNET_YES == session->active)
290 notify_distance_change (session);
291 return; /* nothing to do */
293 session = GNUNET_new (struct Session);
294 session->sender = *peer;
295 session->distance = distance;
296 GNUNET_assert (GNUNET_YES ==
297 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
298 &session->sender.hashPubKey,
300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
305 * Function called if DV distance to a peer is changed.
307 * @param cls closure with 'struct Plugin'
308 * @param peer connected peer
309 * @param distance new distance to the peer
312 handle_dv_distance_changed (void *cls,
313 const struct GNUNET_PeerIdentity *peer,
316 struct Plugin *plugin = cls;
317 struct Session *session;
319 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
324 handle_dv_connect (plugin, peer, distance);
327 session->distance = distance;
328 if (GNUNET_YES == session->active)
329 notify_distance_change (session);
334 * Release session object and clean up associated resources.
336 * @param session session to clean up
339 free_session (struct Session *session)
341 struct Plugin *plugin = session->plugin;
342 struct PendingRequest *pr;
344 GNUNET_assert (GNUNET_YES ==
345 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
346 &session->sender.hashPubKey,
348 if (GNUNET_YES == session->active)
349 plugin->env->session_end (plugin->env->cls,
352 while (NULL != (pr = session->pr_head))
354 GNUNET_CONTAINER_DLL_remove (session->pr_head,
357 GNUNET_DV_send_cancel (pr->th);
359 if (NULL != pr->transmit_cont)
360 pr->transmit_cont (pr->transmit_cont_cls,
362 GNUNET_SYSERR, 0, 0);
365 GNUNET_free (session);
370 * Function called if DV is no longer able to talk to a peer.
372 * @param cls closure with 'struct Plugin'
373 * @param peer peer that disconnected
376 handle_dv_disconnect (void *cls,
377 const struct GNUNET_PeerIdentity *peer)
379 struct Plugin *plugin = cls;
380 struct Session *session;
382 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
385 return; /* nothing to do */
386 free_session (session);
391 * Function called once the delivery of a message has been successful.
392 * Clean up the pending request, and call continuations.
395 * @param ok GNUNET_OK on success, GNUNET_SYSERR on error
398 send_finished (void *cls,
401 struct PendingRequest *pr = cls;
402 struct Session *session = pr->session;
405 GNUNET_CONTAINER_DLL_remove (session->pr_head,
408 if (NULL != pr->transmit_cont)
409 pr->transmit_cont (pr->transmit_cont_cls,
417 * Function that can be used by the transport service to transmit
418 * a message using the plugin.
421 * @param session the session used
422 * @param priority how important is the message
423 * @param msgbuf the message to transmit
424 * @param msgbuf_size number of bytes in 'msgbuf'
425 * @param timeout when should we time out
426 * @param cont continuation to call once the message has
427 * been transmitted (or if the transport is ready
428 * for the next transmission call; or if the
429 * peer disconnected...)
430 * @param cont_cls closure for cont
431 * @return number of bytes used (on the physical network, with overheads);
432 * -1 on hard errors (i.e. address invalid); 0 is a legal value
433 * and does NOT mean that the message was not transmitted (DV)
436 dv_plugin_send (void *cls,
437 struct Session *session,
438 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
439 struct GNUNET_TIME_Relative timeout,
440 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
442 struct PendingRequest *pr;
443 const struct GNUNET_MessageHeader *msg;
444 struct GNUNET_MessageHeader *box;
447 msg = (const struct GNUNET_MessageHeader *) msgbuf;
448 if (ntohs (msg->size) != msgbuf_size)
451 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
452 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
453 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
454 memcpy (&box[1], msgbuf, msgbuf_size);
457 pr = GNUNET_new (struct PendingRequest);
458 pr->transmit_cont = cont;
459 pr->transmit_cont_cls = cont_cls;
460 pr->session = session;
461 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
464 pr->th = GNUNET_DV_send (session->plugin->dvh,
469 GNUNET_free_non_null (box);
475 * Function that can be used to force the plugin to disconnect
476 * from the given peer and cancel all previous transmissions
477 * (and their continuations).
480 * @param target peer from which to disconnect
483 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
485 struct Plugin *plugin = cls;
486 struct Session *session;
487 struct PendingRequest *pr;
489 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
490 &target->hashPubKey);
492 return; /* nothing to do */
493 while (NULL != (pr = session->pr_head))
495 GNUNET_CONTAINER_DLL_remove (session->pr_head,
498 GNUNET_DV_send_cancel (pr->th);
500 if (NULL != pr->transmit_cont)
501 pr->transmit_cont (pr->transmit_cont_cls,
503 GNUNET_SYSERR, 0, 0);
506 session->active = GNUNET_NO;
511 * Convert the transports address to a nice, human-readable
515 * @param type name of the transport that generated the address
516 * @param addr one of the addresses of the host, NULL for the last address
517 * the specific address format depends on the transport
518 * @param addrlen length of the address
519 * @param numeric should (IP) addresses be displayed in numeric form?
520 * @param timeout after how long should we give up?
521 * @param asc function to call on each string
522 * @param asc_cls closure for asc
525 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
526 size_t addrlen, int numeric,
527 struct GNUNET_TIME_Relative timeout,
528 GNUNET_TRANSPORT_AddressStringCallback asc,
531 if ( (0 == addrlen) &&
532 (0 == strcmp (type, "dv")) )
539 * Convert the DV address to a pretty string.
542 * @param addr the (hopefully) DV address
543 * @param addrlen the length of the address
545 * @return string representing the DV address
548 dv_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
552 GNUNET_break (0); /* malformed */
560 * Another peer has suggested an address for this peer and transport
561 * plugin. Check that this could be a valid address. This function
562 * is not expected to 'validate' the address in the sense of trying to
563 * connect to it but simply to see if the binary format is technically
564 * legal for establishing a connection to this peer (and make sure that
565 * the address really corresponds to our network connection/settings
566 * and not some potential man-in-the-middle).
569 * @param addr pointer to the address
570 * @param addrlen length of addr
571 * @return GNUNET_OK if this is a plausible address for this peer
572 * and transport, GNUNET_SYSERR if not
576 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
579 return GNUNET_SYSERR;
585 * Create a new session to transmit data to the target
586 * This session will used to send data to this peer and the plugin will
587 * notify us by calling the env->session_end function
589 * @param cls the plugin
590 * @param address the address
591 * @return the session if the address is valid, NULL otherwise
593 static struct Session *
594 dv_get_session (void *cls,
595 const struct GNUNET_HELLO_Address *address)
597 struct Plugin *plugin = cls;
598 struct Session *session;
600 if (0 != address->address_length)
602 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
603 &address->peer.hashPubKey);
605 return NULL; /* not valid right now */
606 session->active = GNUNET_YES;
612 * Function called to convert a string address to
615 * @param cls closure ('struct Plugin*')
616 * @param addr string address
617 * @param addrlen length of the address including \0 termination
618 * @param buf location to store the buffer
619 * If the function returns GNUNET_SYSERR, its contents are undefined.
620 * @param added length of created address
621 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
624 dv_plugin_string_to_address (void *cls,
630 if ( (addrlen == 3) &&
631 (0 == strcmp ("dv", addr)) )
636 return GNUNET_SYSERR;
642 * Function to obtain the network type for a session
643 * FIXME: we should probably look at the network type
644 * used by the next hop here. Or find some other way
645 * to properly allow ATS-DV resource allocation.
647 * @param cls closure ('struct Plugin*')
648 * @param session the session
649 * @return the network type
651 static enum GNUNET_ATS_Network_Type
652 dv_get_network (void *cls,
653 struct Session *session)
655 GNUNET_assert (NULL != session);
656 return GNUNET_ATS_NET_UNSPECIFIED;
661 * Entry point for the plugin.
663 * @param cls closure with the plugin environment
667 libgnunet_plugin_transport_dv_init (void *cls)
669 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
670 struct GNUNET_TRANSPORT_PluginFunctions *api;
671 struct Plugin *plugin;
673 plugin = GNUNET_new (struct Plugin);
675 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (1024 * 8, GNUNET_YES);
676 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
678 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
681 &handle_dv_distance_changed,
682 &handle_dv_disconnect,
683 &handle_dv_message_received);
684 if (NULL == plugin->dvh)
686 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
687 GNUNET_SERVER_mst_destroy (plugin->mst);
688 GNUNET_free (plugin);
691 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
693 api->send = &dv_plugin_send;
694 api->disconnect = &dv_plugin_disconnect;
695 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
696 api->check_address = &dv_plugin_check_address;
697 api->address_to_string = &dv_plugin_address_to_string;
698 api->string_to_address = &dv_plugin_string_to_address;
699 api->get_session = &dv_get_session;
700 api->get_network = &dv_get_network;
706 * Function called to free a session.
710 * @param value session to free
711 * @return GNUNET_OK (continue to iterate)
714 free_session_iterator (void *cls,
715 const struct GNUNET_HashCode *key,
718 struct Session *session = value;
720 free_session (session);
726 * Exit point from the plugin.
728 * @param cls plugin API
732 libgnunet_plugin_transport_dv_done (void *cls)
734 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
735 struct Plugin *plugin = api->cls;
737 GNUNET_DV_service_disconnect (plugin->dvh);
738 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
739 &free_session_iterator,
741 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
742 GNUNET_SERVER_mst_destroy (plugin->mst);
743 GNUNET_free (plugin);
748 /* end of plugin_transport_dv.c */