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"
42 * Encapsulation of all of the state of the plugin.
48 * An active request for transmission via DV.
56 struct PendingRequest *next;
61 struct PendingRequest *prev;
64 * Continuation function to call once the transmission buffer
65 * has again space available. NULL if there is no
66 * continuation to call.
68 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
71 * Closure for transmit_cont.
73 void *transmit_cont_cls;
76 * Transmission handle from DV client library.
78 struct GNUNET_DV_TransmitHandle *th;
81 * Session of this request.
83 struct Session *session;
89 * Session handle for connections.
95 * Mandatory session header.
97 struct SessionHeader header;
100 * Pointer to the global plugin struct.
102 struct Plugin *plugin;
105 * Head of pending requests.
107 struct PendingRequest *pr_head;
110 * Tail of pending requests.
112 struct PendingRequest *pr_tail;
115 * To whom are we talking to.
117 struct GNUNET_PeerIdentity sender;
120 * Current distance to the given peer.
125 * Does the transport service know about this session (and we thus
126 * need to call 'session_end' when it is released?)
134 * Encapsulation of all of the state of the plugin.
141 struct GNUNET_TRANSPORT_PluginEnvironment *env;
144 * Hash map of sessions (active and inactive).
146 struct GNUNET_CONTAINER_MultiHashMap *sessions;
149 * Copy of the handler array where the closures are
150 * set to this struct's instance.
152 struct GNUNET_SERVER_MessageHandler *handlers;
155 * Handle to the DV service
157 struct GNUNET_DV_ServiceHandle *dvh;
160 * Tokenizer for boxed messages.
162 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
168 * Notify transport service about the change in distance.
170 * @param session session where the distance changed
173 notify_distance_change (struct Session *session)
175 GNUNET_break (0); // FIXME: need extended plugin API!
180 * Function called by MST on each message from the box.
182 * @param cls closure with the 'struct Plugin'
183 * @param client identification of the client (with the 'struct Session')
184 * @param message the actual message
185 * @return GNUNET_OK on success
190 const struct GNUNET_MessageHeader *message)
192 struct Plugin *plugin = cls;
193 struct Session *session = client;
194 struct GNUNET_ATS_Information ats;
196 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
197 ats.value = htonl (session->distance);
198 session->active = GNUNET_YES;
199 plugin->env->receive (plugin->env->cls,
209 * Handler for messages received from the DV service.
211 * @param cls closure with the plugin
212 * @param sender sender of the message
213 * @param distance how far did the message travel
214 * @param msg actual message payload
217 handle_dv_message_received (void *cls,
218 const struct GNUNET_PeerIdentity *sender,
220 const struct GNUNET_MessageHeader *msg)
222 struct Plugin *plugin = cls;
223 struct GNUNET_ATS_Information ats;
224 struct Session *session;
226 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
227 &sender->hashPubKey);
233 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
235 /* need to unbox using MST */
236 GNUNET_SERVER_mst_receive (plugin->mst,
238 (const char *) &msg[1],
239 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
244 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
245 ats.value = htonl (distance);
246 session->active = GNUNET_YES;
247 plugin->env->receive (plugin->env->cls, sender,
255 * Function called if DV starts to be able to talk to a peer.
257 * @param cls closure with 'struct Plugin'
258 * @param peer newly connected peer
259 * @param distance distance to the peer
262 handle_dv_connect (void *cls,
263 const struct GNUNET_PeerIdentity *peer,
266 struct Plugin *plugin = cls;
267 struct Session *session;
269 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
274 session->distance = distance;
275 if (GNUNET_YES == session->active)
276 notify_distance_change (session);
277 return; /* nothing to do */
279 session = GNUNET_malloc (sizeof (struct Session));
280 session->sender = *peer;
281 session->distance = distance;
282 GNUNET_assert (GNUNET_YES ==
283 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
284 &session->sender.hashPubKey,
286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
291 * Function called if DV distance to a peer is changed.
293 * @param cls closure with 'struct Plugin'
294 * @param peer connected peer
295 * @param distance new distance to the peer
298 handle_dv_distance_changed (void *cls,
299 const struct GNUNET_PeerIdentity *peer,
302 struct Plugin *plugin = cls;
303 struct Session *session;
305 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
310 handle_dv_connect (plugin, peer, distance);
313 session->distance = distance;
314 if (GNUNET_YES == session->active)
315 notify_distance_change (session);
320 * Release session object and clean up associated resources.
322 * @param session session to clean up
325 free_session (struct Session *session)
327 struct Plugin *plugin = session->plugin;
328 struct PendingRequest *pr;
330 GNUNET_assert (GNUNET_YES ==
331 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
332 &session->sender.hashPubKey,
334 if (GNUNET_YES == session->active)
335 plugin->env->session_end (plugin->env->cls,
338 while (NULL != (pr = session->pr_head))
340 GNUNET_CONTAINER_DLL_remove (session->pr_head,
343 GNUNET_DV_send_cancel (pr->th);
345 if (NULL != pr->transmit_cont)
346 pr->transmit_cont (pr->transmit_cont_cls,
348 GNUNET_SYSERR, 0, 0);
351 GNUNET_free (session);
356 * Function called if DV is no longer able to talk to a peer.
358 * @param cls closure with 'struct Plugin'
359 * @param peer peer that disconnected
362 handle_dv_disconnect (void *cls,
363 const struct GNUNET_PeerIdentity *peer)
365 struct Plugin *plugin = cls;
366 struct Session *session;
368 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
371 return; /* nothing to do */
372 free_session (session);
377 * Function called once the delivery of a message has been successful.
378 * Clean up the pending request, and call continuations.
381 * @param ok GNUNET_OK on success, GNUNET_SYSERR on error
384 send_finished (void *cls,
387 struct PendingRequest *pr = cls;
388 struct Session *session = pr->session;
391 GNUNET_CONTAINER_DLL_remove (session->pr_head,
394 if (NULL != pr->transmit_cont)
395 pr->transmit_cont (pr->transmit_cont_cls,
403 * Function that can be used by the transport service to transmit
404 * a message using the plugin.
407 * @param session the session used
408 * @param priority how important is the message
409 * @param msgbuf the message to transmit
410 * @param msgbuf_size number of bytes in 'msgbuf'
411 * @param timeout when should we time out
412 * @param cont continuation to call once the message has
413 * been transmitted (or if the transport is ready
414 * for the next transmission call; or if the
415 * peer disconnected...)
416 * @param cont_cls closure for cont
417 * @return number of bytes used (on the physical network, with overheads);
418 * -1 on hard errors (i.e. address invalid); 0 is a legal value
419 * and does NOT mean that the message was not transmitted (DV)
422 dv_plugin_send (void *cls,
423 struct Session *session,
424 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
425 struct GNUNET_TIME_Relative timeout,
426 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
428 struct PendingRequest *pr;
429 const struct GNUNET_MessageHeader *msg;
430 struct GNUNET_MessageHeader *box;
433 msg = (const struct GNUNET_MessageHeader *) msgbuf;
434 if (ntohs (msg->size) != msgbuf_size)
437 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
438 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
439 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
440 memcpy (&box[1], msgbuf, msgbuf_size);
443 pr = GNUNET_malloc (sizeof (struct PendingRequest));
444 pr->transmit_cont = cont;
445 pr->transmit_cont_cls = cont_cls;
446 pr->session = session;
447 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
450 pr->th = GNUNET_DV_send (session->plugin->dvh,
455 GNUNET_free_non_null (box);
461 * Function that can be used to force the plugin to disconnect
462 * from the given peer and cancel all previous transmissions
463 * (and their continuations).
466 * @param target peer from which to disconnect
469 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
471 struct Plugin *plugin = cls;
472 struct Session *session;
473 struct PendingRequest *pr;
475 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
476 &target->hashPubKey);
478 return; /* nothing to do */
479 while (NULL != (pr = session->pr_head))
481 GNUNET_CONTAINER_DLL_remove (session->pr_head,
484 GNUNET_DV_send_cancel (pr->th);
486 if (NULL != pr->transmit_cont)
487 pr->transmit_cont (pr->transmit_cont_cls,
489 GNUNET_SYSERR, 0, 0);
492 session->active = GNUNET_NO;
497 * Convert the transports address to a nice, human-readable
501 * @param type name of the transport that generated the address
502 * @param addr one of the addresses of the host, NULL for the last address
503 * the specific address format depends on the transport
504 * @param addrlen length of the address
505 * @param numeric should (IP) addresses be displayed in numeric form?
506 * @param timeout after how long should we give up?
507 * @param asc function to call on each string
508 * @param asc_cls closure for asc
511 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
512 size_t addrlen, int numeric,
513 struct GNUNET_TIME_Relative timeout,
514 GNUNET_TRANSPORT_AddressStringCallback asc,
517 if ( (0 == addrlen) &&
518 (0 == strcmp (type, "dv")) )
525 * Convert the DV address to a pretty string.
528 * @param addr the (hopefully) DV address
529 * @param addrlen the length of the address
531 * @return string representing the DV address
534 dv_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
538 GNUNET_break (0); /* malformed */
546 * Another peer has suggested an address for this peer and transport
547 * plugin. Check that this could be a valid address. This function
548 * is not expected to 'validate' the address in the sense of trying to
549 * connect to it but simply to see if the binary format is technically
550 * legal for establishing a connection to this peer (and make sure that
551 * the address really corresponds to our network connection/settings
552 * and not some potential man-in-the-middle).
555 * @param addr pointer to the address
556 * @param addrlen length of addr
557 * @return GNUNET_OK if this is a plausible address for this peer
558 * and transport, GNUNET_SYSERR if not
562 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
565 return GNUNET_SYSERR;
571 * Create a new session to transmit data to the target
572 * This session will used to send data to this peer and the plugin will
573 * notify us by calling the env->session_end function
575 * @param cls the plugin
576 * @param address the address
577 * @return the session if the address is valid, NULL otherwise
579 static struct Session *
580 dv_get_session (void *cls,
581 const struct GNUNET_HELLO_Address *address)
583 struct Plugin *plugin = cls;
584 struct Session *session;
586 if (0 != address->address_length)
588 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
589 &address->peer.hashPubKey);
591 return NULL; /* not valid right now */
592 session->active = GNUNET_YES;
598 * Function called to convert a string address to
601 * @param cls closure ('struct Plugin*')
602 * @param addr string address
603 * @param addrlen length of the address including \0 termination
604 * @param buf location to store the buffer
605 * If the function returns GNUNET_SYSERR, its contents are undefined.
606 * @param added length of created address
607 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
610 dv_plugin_string_to_address (void *cls,
616 if ( (addrlen == 3) &&
617 (0 == strcmp ("dv", addr)) )
622 return GNUNET_SYSERR;
627 * Entry point for the plugin.
630 libgnunet_plugin_transport_dv_init (void *cls)
632 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
633 struct GNUNET_TRANSPORT_PluginFunctions *api;
634 struct Plugin *plugin;
636 plugin = GNUNET_malloc (sizeof (struct Plugin));
638 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (1024 * 8, GNUNET_YES);
639 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
641 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
644 &handle_dv_distance_changed,
645 &handle_dv_disconnect,
646 &handle_dv_message_received);
647 if (NULL == plugin->dvh)
649 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
650 GNUNET_SERVER_mst_destroy (plugin->mst);
651 GNUNET_free (plugin);
654 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
656 api->send = &dv_plugin_send;
657 api->disconnect = &dv_plugin_disconnect;
658 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
659 api->check_address = &dv_plugin_check_address;
660 api->address_to_string = &dv_plugin_address_to_string;
661 api->string_to_address = &dv_plugin_string_to_address;
662 api->get_session = dv_get_session;
668 * Function called to free a session.
672 * @param value session to free
673 * @return GNUNET_OK (continue to iterate)
676 free_session_iterator (void *cls,
677 const struct GNUNET_HashCode *key,
680 struct Session *session = value;
682 free_session (session);
688 * Exit point from the plugin.
691 libgnunet_plugin_transport_dv_done (void *cls)
693 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
694 struct Plugin *plugin = api->cls;
696 GNUNET_DV_service_disconnect (plugin->dvh);
697 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
698 &free_session_iterator,
700 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
701 GNUNET_SERVER_mst_destroy (plugin->mst);
702 GNUNET_free (plugin);
707 /* end of plugin_transport_dv.c */