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;
163 * Notify transport service about the change in distance.
165 * @param session session where the distance changed
168 notify_distance_change (struct Session *session)
170 GNUNET_break (0); // FIXME: need extended plugin API!
175 * Handler for messages received from the DV service.
177 * @param cls closure with the plugin
178 * @param sender sender of the message
179 * @param distance how far did the message travel
180 * @param msg actual message payload
183 handle_dv_message_received (void *cls,
184 const struct GNUNET_PeerIdentity *sender,
186 const struct GNUNET_MessageHeader *msg)
188 struct Plugin *plugin = cls;
189 struct GNUNET_ATS_Information ats;
190 struct Session *session;
192 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
193 &sender->hashPubKey);
199 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
200 ats.value = htonl (distance);
201 session->active = GNUNET_YES;
202 plugin->env->receive (plugin->env->cls, sender,
210 * Function called if DV starts to be able to talk to a peer.
212 * @param cls closure with 'struct Plugin'
213 * @param peer newly connected peer
214 * @param distance distance to the peer
217 handle_dv_connect (void *cls,
218 const struct GNUNET_PeerIdentity *peer,
221 struct Plugin *plugin = cls;
222 struct Session *session;
224 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
229 session->distance = distance;
230 if (GNUNET_YES == session->active)
231 notify_distance_change (session);
232 return; /* nothing to do */
234 session = GNUNET_malloc (sizeof (struct Session));
235 session->sender = *peer;
236 session->distance = distance;
237 GNUNET_assert (GNUNET_YES ==
238 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
239 &session->sender.hashPubKey,
241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
246 * Function called if DV distance to a peer is changed.
248 * @param cls closure with 'struct Plugin'
249 * @param peer connected peer
250 * @param distance new distance to the peer
253 handle_dv_distance_changed (void *cls,
254 const struct GNUNET_PeerIdentity *peer,
257 struct Plugin *plugin = cls;
258 struct Session *session;
260 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
265 handle_dv_connect (plugin, peer, distance);
268 session->distance = distance;
269 if (GNUNET_YES == session->active)
270 notify_distance_change (session);
275 * Release session object and clean up associated resources.
277 * @param session session to clean up
280 free_session (struct Session *session)
282 struct Plugin *plugin = session->plugin;
283 struct PendingRequest *pr;
285 GNUNET_assert (GNUNET_YES ==
286 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
287 &session->sender.hashPubKey,
289 if (GNUNET_YES == session->active)
290 plugin->env->session_end (plugin->env->cls,
293 while (NULL != (pr = session->pr_head))
295 GNUNET_CONTAINER_DLL_remove (session->pr_head,
298 GNUNET_DV_send_cancel (pr->th);
300 if (NULL != pr->transmit_cont)
301 pr->transmit_cont (pr->transmit_cont_cls,
303 GNUNET_SYSERR, 0, 0);
306 GNUNET_free (session);
311 * Function called if DV is no longer able to talk to a peer.
313 * @param cls closure with 'struct Plugin'
314 * @param peer peer that disconnected
317 handle_dv_disconnect (void *cls,
318 const struct GNUNET_PeerIdentity *peer)
320 struct Plugin *plugin = cls;
321 struct Session *session;
323 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
326 return; /* nothing to do */
327 free_session (session);
332 * Function called once the delivery of a message has been successful.
333 * Clean up the pending request, and call continuations.
336 * @param ok GNUNET_OK on success, GNUNET_SYSERR on error
339 send_finished (void *cls,
342 struct PendingRequest *pr = cls;
343 struct Session *session = pr->session;
346 GNUNET_CONTAINER_DLL_remove (session->pr_head,
349 if (NULL != pr->transmit_cont)
350 pr->transmit_cont (pr->transmit_cont_cls,
358 * Function that can be used by the transport service to transmit
359 * a message using the plugin.
362 * @param session the session used
363 * @param priority how important is the message
364 * @param msgbuf the message to transmit
365 * @param msgbuf_size number of bytes in 'msgbuf'
366 * @param timeout when should we time out
367 * @param cont continuation to call once the message has
368 * been transmitted (or if the transport is ready
369 * for the next transmission call; or if the
370 * peer disconnected...)
371 * @param cont_cls closure for cont
372 * @return number of bytes used (on the physical network, with overheads);
373 * -1 on hard errors (i.e. address invalid); 0 is a legal value
374 * and does NOT mean that the message was not transmitted (DV)
377 dv_plugin_send (void *cls,
378 struct Session *session,
379 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
380 struct GNUNET_TIME_Relative timeout,
381 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
383 struct PendingRequest *pr;
384 const struct GNUNET_MessageHeader *msg;
386 msg = (const struct GNUNET_MessageHeader *) msgbuf;
387 GNUNET_assert (ntohs (msg->size) == msgbuf_size); // API will change...
388 pr = GNUNET_malloc (sizeof (struct PendingRequest));
389 pr->transmit_cont = cont;
390 pr->transmit_cont_cls = cont_cls;
391 pr->session = session;
392 GNUNET_CONTAINER_DLL_insert_tail (session->pr_head,
395 pr->th = GNUNET_DV_send (session->plugin->dvh,
405 * Function that can be used to force the plugin to disconnect
406 * from the given peer and cancel all previous transmissions
407 * (and their continuations).
410 * @param target peer from which to disconnect
413 dv_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
415 struct Plugin *plugin = cls;
416 struct Session *session;
417 struct PendingRequest *pr;
419 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
420 &target->hashPubKey);
422 return; /* nothing to do */
423 while (NULL != (pr = session->pr_head))
425 GNUNET_CONTAINER_DLL_remove (session->pr_head,
428 GNUNET_DV_send_cancel (pr->th);
430 if (NULL != pr->transmit_cont)
431 pr->transmit_cont (pr->transmit_cont_cls,
433 GNUNET_SYSERR, 0, 0);
436 session->active = GNUNET_NO;
441 * Convert the transports address to a nice, human-readable
445 * @param type name of the transport that generated the address
446 * @param addr one of the addresses of the host, NULL for the last address
447 * the specific address format depends on the transport
448 * @param addrlen length of the address
449 * @param numeric should (IP) addresses be displayed in numeric form?
450 * @param timeout after how long should we give up?
451 * @param asc function to call on each string
452 * @param asc_cls closure for asc
455 dv_plugin_address_pretty_printer (void *cls, const char *type, const void *addr,
456 size_t addrlen, int numeric,
457 struct GNUNET_TIME_Relative timeout,
458 GNUNET_TRANSPORT_AddressStringCallback asc,
461 if ( (0 == addrlen) &&
462 (0 == strcmp (type, "dv")) )
469 * Convert the DV address to a pretty string.
472 * @param addr the (hopefully) DV address
473 * @param addrlen the length of the address
475 * @return string representing the DV address
478 dv_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
482 GNUNET_break (0); /* malformed */
490 * Another peer has suggested an address for this peer and transport
491 * plugin. Check that this could be a valid address. This function
492 * is not expected to 'validate' the address in the sense of trying to
493 * connect to it but simply to see if the binary format is technically
494 * legal for establishing a connection to this peer (and make sure that
495 * the address really corresponds to our network connection/settings
496 * and not some potential man-in-the-middle).
499 * @param addr pointer to the address
500 * @param addrlen length of addr
501 * @return GNUNET_OK if this is a plausible address for this peer
502 * and transport, GNUNET_SYSERR if not
506 dv_plugin_check_address (void *cls, const void *addr, size_t addrlen)
509 return GNUNET_SYSERR;
515 * Create a new session to transmit data to the target
516 * This session will used to send data to this peer and the plugin will
517 * notify us by calling the env->session_end function
519 * @param cls the plugin
520 * @param address the address
521 * @return the session if the address is valid, NULL otherwise
523 static struct Session *
524 dv_get_session (void *cls,
525 const struct GNUNET_HELLO_Address *address)
527 struct Plugin *plugin = cls;
528 struct Session *session;
530 if (0 != address->address_length)
532 session = GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
533 &address->peer.hashPubKey);
535 return NULL; /* not valid right now */
536 session->active = GNUNET_YES;
542 * Function called to convert a string address to
545 * @param cls closure ('struct Plugin*')
546 * @param addr string address
547 * @param addrlen length of the address including \0 termination
548 * @param buf location to store the buffer
549 * If the function returns GNUNET_SYSERR, its contents are undefined.
550 * @param added length of created address
551 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
554 dv_plugin_string_to_address (void *cls,
560 if ( (addrlen == 3) &&
561 (0 == strcmp ("dv", addr)) )
566 return GNUNET_SYSERR;
571 * Entry point for the plugin.
574 libgnunet_plugin_transport_dv_init (void *cls)
576 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
577 struct GNUNET_TRANSPORT_PluginFunctions *api;
578 struct Plugin *plugin;
580 plugin = GNUNET_malloc (sizeof (struct Plugin));
582 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (1024 * 8, GNUNET_YES);
583 plugin->dvh = GNUNET_DV_service_connect (env->cfg,
586 &handle_dv_distance_changed,
587 &handle_dv_disconnect,
588 &handle_dv_message_received);
589 if (NULL == plugin->dvh)
591 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
592 GNUNET_free (plugin);
595 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
597 api->send = &dv_plugin_send;
598 api->disconnect = &dv_plugin_disconnect;
599 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
600 api->check_address = &dv_plugin_check_address;
601 api->address_to_string = &dv_plugin_address_to_string;
602 api->string_to_address = &dv_plugin_string_to_address;
603 api->get_session = dv_get_session;
609 * Function called to free a session.
613 * @param value session to free
614 * @return GNUNET_OK (continue to iterate)
617 free_session_iterator (void *cls,
618 const struct GNUNET_HashCode *key,
621 struct Session *session = value;
623 free_session (session);
629 * Exit point from the plugin.
632 libgnunet_plugin_transport_dv_done (void *cls)
634 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
635 struct Plugin *plugin = api->cls;
637 GNUNET_DV_service_disconnect (plugin->dvh);
638 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
639 &free_session_iterator,
641 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
642 GNUNET_free (plugin);
647 /* end of plugin_transport_dv.c */