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,
203 plugin->env->update_address_metrics (plugin->env->cls,
204 &session->sender, "", 0, session, &ats, 1);
210 * Handler for messages received from the DV service.
212 * @param cls closure with the plugin
213 * @param sender sender of the message
214 * @param distance how far did the message travel
215 * @param msg actual message payload
218 handle_dv_message_received (void *cls,
219 const struct GNUNET_PeerIdentity *sender,
221 const struct GNUNET_MessageHeader *msg)
223 struct Plugin *plugin = cls;
224 struct GNUNET_ATS_Information ats;
225 struct Session *session;
227 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
228 &sender->hashPubKey);
234 if (GNUNET_MESSAGE_TYPE_DV_BOX == ntohs (msg->type))
236 /* need to unbox using MST */
237 GNUNET_SERVER_mst_receive (plugin->mst,
239 (const char *) &msg[1],
240 ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader),
245 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
246 ats.value = htonl (distance);
247 session->active = GNUNET_YES;
248 plugin->env->receive (plugin->env->cls, sender,
251 plugin->env->update_address_metrics (plugin->env->cls,
252 sender, "", 0, session, &ats, 1);
257 * Function called if DV starts to be able to talk to a peer.
259 * @param cls closure with 'struct Plugin'
260 * @param peer newly connected peer
261 * @param distance distance to the peer
264 handle_dv_connect (void *cls,
265 const struct GNUNET_PeerIdentity *peer,
268 struct Plugin *plugin = cls;
269 struct Session *session;
271 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
276 session->distance = distance;
277 if (GNUNET_YES == session->active)
278 notify_distance_change (session);
279 return; /* nothing to do */
281 session = GNUNET_malloc (sizeof (struct Session));
282 session->sender = *peer;
283 session->distance = distance;
284 GNUNET_assert (GNUNET_YES ==
285 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
286 &session->sender.hashPubKey,
288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
293 * Function called if DV distance to a peer is changed.
295 * @param cls closure with 'struct Plugin'
296 * @param peer connected peer
297 * @param distance new distance to the peer
300 handle_dv_distance_changed (void *cls,
301 const struct GNUNET_PeerIdentity *peer,
304 struct Plugin *plugin = cls;
305 struct Session *session;
307 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
312 handle_dv_connect (plugin, peer, distance);
315 session->distance = distance;
316 if (GNUNET_YES == session->active)
317 notify_distance_change (session);
322 * Release session object and clean up associated resources.
324 * @param session session to clean up
327 free_session (struct Session *session)
329 struct Plugin *plugin = session->plugin;
330 struct PendingRequest *pr;
332 GNUNET_assert (GNUNET_YES ==
333 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
334 &session->sender.hashPubKey,
336 if (GNUNET_YES == session->active)
337 plugin->env->session_end (plugin->env->cls,
340 while (NULL != (pr = session->pr_head))
342 GNUNET_CONTAINER_DLL_remove (session->pr_head,
345 GNUNET_DV_send_cancel (pr->th);
347 if (NULL != pr->transmit_cont)
348 pr->transmit_cont (pr->transmit_cont_cls,
350 GNUNET_SYSERR, 0, 0);
353 GNUNET_free (session);
358 * Function called if DV is no longer able to talk to a peer.
360 * @param cls closure with 'struct Plugin'
361 * @param peer peer that disconnected
364 handle_dv_disconnect (void *cls,
365 const struct GNUNET_PeerIdentity *peer)
367 struct Plugin *plugin = cls;
368 struct Session *session;
370 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
373 return; /* nothing to do */
374 free_session (session);
379 * Function called once the delivery of a message has been successful.
380 * Clean up the pending request, and call continuations.
383 * @param ok GNUNET_OK on success, GNUNET_SYSERR on error
386 send_finished (void *cls,
389 struct PendingRequest *pr = cls;
390 struct Session *session = pr->session;
393 GNUNET_CONTAINER_DLL_remove (session->pr_head,
396 if (NULL != pr->transmit_cont)
397 pr->transmit_cont (pr->transmit_cont_cls,
405 * Function that can be used by the transport service to transmit
406 * a message using the plugin.
409 * @param session the session used
410 * @param priority how important is the message
411 * @param msgbuf the message to transmit
412 * @param msgbuf_size number of bytes in 'msgbuf'
413 * @param timeout when should we time out
414 * @param cont continuation to call once the message has
415 * been transmitted (or if the transport is ready
416 * for the next transmission call; or if the
417 * peer disconnected...)
418 * @param cont_cls closure for cont
419 * @return number of bytes used (on the physical network, with overheads);
420 * -1 on hard errors (i.e. address invalid); 0 is a legal value
421 * and does NOT mean that the message was not transmitted (DV)
424 dv_plugin_send (void *cls,
425 struct Session *session,
426 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
427 struct GNUNET_TIME_Relative timeout,
428 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
430 struct PendingRequest *pr;
431 const struct GNUNET_MessageHeader *msg;
432 struct GNUNET_MessageHeader *box;
435 msg = (const struct GNUNET_MessageHeader *) msgbuf;
436 if (ntohs (msg->size) != msgbuf_size)
439 box = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
440 box->type = htons (GNUNET_MESSAGE_TYPE_DV_BOX);
441 box->size = htons (sizeof (struct GNUNET_MessageHeader) + msgbuf_size);
442 memcpy (&box[1], msgbuf, msgbuf_size);
445 pr = GNUNET_malloc (sizeof (struct PendingRequest));
446 pr->transmit_cont = cont;
447 pr->transmit_cont_cls = cont_cls;
448 pr->session = session;
449 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
452 pr->th = GNUNET_DV_send (session->plugin->dvh,
457 GNUNET_free_non_null (box);
463 * Function that can be used to force the plugin to disconnect
464 * from the given peer and cancel all previous transmissions
465 * (and their continuations).
468 * @param target peer from which to disconnect
471 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
473 struct Plugin *plugin = cls;
474 struct Session *session;
475 struct PendingRequest *pr;
477 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
478 &target->hashPubKey);
480 return; /* nothing to do */
481 while (NULL != (pr = session->pr_head))
483 GNUNET_CONTAINER_DLL_remove (session->pr_head,
486 GNUNET_DV_send_cancel (pr->th);
488 if (NULL != pr->transmit_cont)
489 pr->transmit_cont (pr->transmit_cont_cls,
491 GNUNET_SYSERR, 0, 0);
494 session->active = GNUNET_NO;
499 * Convert the transports address to a nice, human-readable
503 * @param type name of the transport that generated the address
504 * @param addr one of the addresses of the host, NULL for the last address
505 * the specific address format depends on the transport
506 * @param addrlen length of the address
507 * @param numeric should (IP) addresses be displayed in numeric form?
508 * @param timeout after how long should we give up?
509 * @param asc function to call on each string
510 * @param asc_cls closure for asc
513 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
514 size_t addrlen, int numeric,
515 struct GNUNET_TIME_Relative timeout,
516 GNUNET_TRANSPORT_AddressStringCallback asc,
519 if ( (0 == addrlen) &&
520 (0 == strcmp (type, "dv")) )
527 * Convert the DV address to a pretty string.
530 * @param addr the (hopefully) DV address
531 * @param addrlen the length of the address
533 * @return string representing the DV address
536 dv_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
540 GNUNET_break (0); /* malformed */
548 * Another peer has suggested an address for this peer and transport
549 * plugin. Check that this could be a valid address. This function
550 * is not expected to 'validate' the address in the sense of trying to
551 * connect to it but simply to see if the binary format is technically
552 * legal for establishing a connection to this peer (and make sure that
553 * the address really corresponds to our network connection/settings
554 * and not some potential man-in-the-middle).
557 * @param addr pointer to the address
558 * @param addrlen length of addr
559 * @return GNUNET_OK if this is a plausible address for this peer
560 * and transport, GNUNET_SYSERR if not
564 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
567 return GNUNET_SYSERR;
573 * Create a new session to transmit data to the target
574 * This session will used to send data to this peer and the plugin will
575 * notify us by calling the env->session_end function
577 * @param cls the plugin
578 * @param address the address
579 * @return the session if the address is valid, NULL otherwise
581 static struct Session *
582 dv_get_session (void *cls,
583 const struct GNUNET_HELLO_Address *address)
585 struct Plugin *plugin = cls;
586 struct Session *session;
588 if (0 != address->address_length)
590 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
591 &address->peer.hashPubKey);
593 return NULL; /* not valid right now */
594 session->active = GNUNET_YES;
600 * Function called to convert a string address to
603 * @param cls closure ('struct Plugin*')
604 * @param addr string address
605 * @param addrlen length of the address including \0 termination
606 * @param buf location to store the buffer
607 * If the function returns GNUNET_SYSERR, its contents are undefined.
608 * @param added length of created address
609 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
612 dv_plugin_string_to_address (void *cls,
618 if ( (addrlen == 3) &&
619 (0 == strcmp ("dv", addr)) )
624 return GNUNET_SYSERR;
629 * Entry point for the plugin.
632 libgnunet_plugin_transport_dv_init (void *cls)
634 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
635 struct GNUNET_TRANSPORT_PluginFunctions *api;
636 struct Plugin *plugin;
638 plugin = GNUNET_malloc (sizeof (struct Plugin));
640 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (1024 * 8, GNUNET_YES);
641 plugin->mst = GNUNET_SERVER_mst_create (&unbox_cb,
643 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
646 &handle_dv_distance_changed,
647 &handle_dv_disconnect,
648 &handle_dv_message_received);
649 if (NULL == plugin->dvh)
651 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
652 GNUNET_SERVER_mst_destroy (plugin->mst);
653 GNUNET_free (plugin);
656 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
658 api->send = &dv_plugin_send;
659 api->disconnect = &dv_plugin_disconnect;
660 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
661 api->check_address = &dv_plugin_check_address;
662 api->address_to_string = &dv_plugin_address_to_string;
663 api->string_to_address = &dv_plugin_string_to_address;
664 api->get_session = dv_get_session;
670 * Function called to free a session.
674 * @param value session to free
675 * @return GNUNET_OK (continue to iterate)
678 free_session_iterator (void *cls,
679 const struct GNUNET_HashCode *key,
682 struct Session *session = value;
684 free_session (session);
690 * Exit point from the plugin.
693 libgnunet_plugin_transport_dv_done (void *cls)
695 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
696 struct Plugin *plugin = api->cls;
698 GNUNET_DV_service_disconnect (plugin->dvh);
699 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
700 &free_session_iterator,
702 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
703 GNUNET_SERVER_mst_destroy (plugin->mst);
704 GNUNET_free (plugin);
709 /* end of plugin_transport_dv.c */