2 This file is part of GNUnet
3 (C) 2010 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 2, 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 transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP transport service
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_service_lib.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "plugin_transport.h"
41 #include "transport.h"
43 #define DEBUG_UDP GNUNET_NO
46 * Handle for request of hostname resolution, non-NULL if pending.
48 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
51 * Message-Packet header.
56 * size of the message, in bytes, including this header.
58 struct GNUNET_MessageHeader header;
61 * What is the identity of the sender (GNUNET_hash of public key)
63 struct GNUNET_PeerIdentity sender;
67 /* Forward definition */
71 * Session handle for UDP connections.
77 * Stored in a linked list.
82 * Pointer to the global plugin struct.
84 struct Plugin *plugin;
87 * To whom are we talking to (set to our identity
89 struct GNUNET_PeerIdentity target;
92 * Address of the other peer if WE initiated the connection
93 * (and hence can be sure what it is), otherwise NULL.
98 * Length of connect_addr, can be 0.
103 * Random challenge number for validation
108 * Have we received validation (performed ping/pong) from this peer?
110 unsigned int validated;
113 * What is the latency of this connection? (Time between ping and pong)
115 struct GNUNET_TIME_Relative latency;
118 * At what GNUNET_TIME did we send a ping?
120 struct GNUNET_TIME_Absolute ping_sent;
124 * Encapsulation of all of the state of the plugin.
131 struct GNUNET_TRANSPORT_PluginEnvironment *env;
134 * List of open TCP sessions.
136 struct Session *sessions;
139 * Handle for the statistics service.
141 struct GNUNET_STATISTICS_Handle *statistics;
144 * Handle to the network service.
146 struct GNUNET_SERVICE_Context *service;
149 * ID of task used to update our addresses when one expires.
151 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
156 GNUNET_SCHEDULER_TaskIdentifier select_task;
159 * Port that we are actually listening on.
164 * Port that the user said we would have visible to the
172 struct GNUNET_NETWORK_FDSet *rs;
177 * Message used to ask a peer to validate receipt (to check an address
180 struct UDPPingMessage
184 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING
186 struct GNUNET_MessageHeader header;
189 * Random challenge number (in network byte order).
191 uint32_t challenge GNUNET_PACKED;
197 * Message used to validate a HELLO. The challenge number
198 * is sent along with whatever address the peer received
199 * the ping from. Used to validate our address (or at
200 * least give us a better idea where we look like we're
204 struct UDPPongMessage
207 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG
209 struct GNUNET_MessageHeader header;
212 * Random challenge number (in network byte order).
214 uint32_t challenge GNUNET_PACKED;
216 /* Length of addr, appended to end of message */
217 unsigned int addrlen;
220 /* *********** globals ************* */
223 * the socket that we transmit all data with
225 static struct GNUNET_NETWORK_Handle *udp_sock;
229 * A (core) Session is to be associated with a transport session. The
230 * transport service may want to know in order to call back on the
231 * core if the connection is being closed.
233 * @param session the session handle passed along
234 * from the call to receive that was made by the transport
236 * @return GNUNET_OK if the session could be associated,
237 * GNUNET_SYSERR if not.
240 udp_associate (struct Session *session)
242 return GNUNET_SYSERR; /* UDP connections can never be associated */
246 * Disconnect from a remote node.
248 * @param tsession the session that is closed
249 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
252 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
258 * Shutdown the server process (stop receiving inbound traffic). Maybe
262 udp_transport_server_stop (void *cls)
264 struct Plugin *plugin = cls;
265 GNUNET_assert (udp_sock != NULL);
266 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
268 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
269 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
272 GNUNET_NETWORK_socket_close (udp_sock);
277 static struct Session *
278 find_session (void *cls, const struct GNUNET_PeerIdentity *peer)
280 struct Plugin *plugin = cls;
282 pos = plugin->sessions;
286 if (memcmp (peer, &pos->target, sizeof (struct GNUNET_PeerIdentity)) ==
296 * Function that can be used by the transport service to transmit
297 * a message using the plugin.
300 * @param service_context value passed to the transport-service
301 * to identify the neighbour
302 * @param target who should receive this message
303 * @param priority how important is the message
304 * @param msg the message to transmit
305 * @param timeout when should we time out (give up) if we can not transmit?
306 * @param cont continuation to call once the message has
307 * been transmitted (or if the transport is ready
308 * for the next transmission call; or if the
309 * peer disconnected...)
310 * @param cont_cls closure for cont
313 udp_plugin_send (void *cls,
314 const struct GNUNET_PeerIdentity *target,
315 unsigned int priority,
316 const struct GNUNET_MessageHeader *msg,
317 struct GNUNET_TIME_Relative timeout,
318 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
320 struct Plugin *plugin = cls;
321 struct Session *session;
322 struct UDPMessage *message;
326 session = find_session (plugin, target);
328 if ((session == NULL) || (udp_sock == NULL))
331 /* Build the message to be sent */
332 message = GNUNET_malloc (sizeof (struct UDPMessage) + ntohs (msg->size));
333 ssize = sizeof (struct UDPMessage) + ntohs (msg->size);
336 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
337 ("In udp_send, ssize is %d\n"), ssize);
339 message->header.size = htons (ssize);
340 message->header.type = htons (0);
341 memcpy (&message->sender, plugin->env->my_identity,
342 sizeof (struct GNUNET_PeerIdentity));
343 memcpy (&message[1], msg, ntohs (msg->size));
345 /* Actually send the message */
347 GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
348 session->connect_addr,
349 session->connect_alen);
353 if (sent == GNUNET_SYSERR)
354 cont (cont_cls, target, GNUNET_SYSERR);
356 cont (cont_cls, target, GNUNET_OK);
358 GNUNET_free (message);
363 * We've received a PING from this peer via UDP.
364 * Send back our PONG.
367 * @param sender the Identity of the sender
368 * @param message the actual message
371 handle_udp_ping (void *cls,
372 struct GNUNET_PeerIdentity *sender,
373 struct sockaddr_storage *addr, size_t addrlen,
374 const struct GNUNET_MessageHeader *message)
376 struct Plugin *plugin = cls;
377 const struct UDPPingMessage *ping = (const struct UDPPingMessage *) message;
378 struct UDPPongMessage *pong;
379 struct Session *found;
382 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
383 ("handling ping, challenge is %d\n"),
384 ntohs (ping->challenge));
386 found = find_session (plugin, sender);
389 pong = GNUNET_malloc (sizeof (struct UDPPongMessage) + addrlen);
390 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG);
391 pong->header.size = htons (sizeof (struct UDPPongMessage) + addrlen);
392 pong->challenge = ping->challenge;
393 memcpy (&pong[1], addr, addrlen);
394 pong->addrlen = htons (addrlen);
396 udp_plugin_send (plugin, sender, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
398 GNUNET_TIME_relative_multiply
399 (GNUNET_TIME_UNIT_SECONDS, 30), NULL, NULL);
408 * We've received a PONG from this peer via UDP.
409 * Great. Call validate func if we haven't already
413 * @param client identification of the client
414 * @param message the actual message
417 handle_udp_pong (void *cls,
418 struct GNUNET_PeerIdentity *sender,
419 const struct GNUNET_MessageHeader *message)
421 struct Plugin *plugin = cls;
422 const struct UDPPongMessage *pong = (struct UDPPongMessage *) message;
423 struct Session *found;
424 unsigned int addr_len;
425 struct sockaddr_storage addr;
428 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _("handling pong\n"));
430 found = find_session (plugin, sender);
432 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
433 ("found->challenge %d, pong->challenge %d\n"),
434 found->challenge, ntohs (pong->challenge));
436 if ((found != NULL) && (found->challenge == ntohs (pong->challenge)))
438 found->validated = GNUNET_YES;
440 GNUNET_TIME_absolute_get_difference (found->ping_sent,
441 GNUNET_TIME_absolute_get ());
442 addr_len = ntohs (pong->addrlen);
444 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
445 ("found associated ping, addr is %u bytes\n"),
448 memcpy (&addr, &pong[1], addr_len);
449 plugin->env->notify_validation (plugin->env->cls, "udp", sender,
450 ntohs (pong->challenge),
457 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
458 ("Session not found!\n"));
465 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
467 struct Plugin *plugin = cls;
468 struct GNUNET_TIME_Relative timeout =
469 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
471 struct UDPMessage *msg;
472 const struct GNUNET_MessageHeader *hdr;
473 struct GNUNET_PeerIdentity *sender;
476 struct sockaddr_storage addr;
478 struct Session *found;
482 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
485 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
486 ("we expect to read %u bytes\n"), buflen);
489 if (buflen == GNUNET_NO)
492 buf = GNUNET_malloc (buflen);
493 fromlen = sizeof (addr);
495 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
496 ("src_addr_len is %u\n"), fromlen);
498 memset (&addr, 0, fromlen);
500 GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
501 (struct sockaddr *) &addr, &fromlen);
504 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
505 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
514 msg = (struct UDPMessage *) buf;
517 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
518 ("header reports message size of %d\n"),
519 ntohs (msg->header.size));
521 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
522 ("header reports message type of %d\n"),
523 ntohs (msg->header.type));
525 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
528 GNUNET_NETWORK_fdset_zero (plugin->rs);
529 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
532 hdr = (const struct GNUNET_MessageHeader *) &msg[1];
533 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
534 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
537 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
538 ("msg reports message size of %d\n"),
541 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
542 ("msg reports message type of %d\n"),
546 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING)
548 handle_udp_ping (plugin, sender, &addr, fromlen, hdr);
550 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG)
552 handle_udp_pong (plugin, sender, hdr);
556 found = find_session (plugin, sender);
558 plugin->env->receive (plugin->env->cls, found->latency, sender,
561 plugin->env->receive (plugin->env->cls,
562 GNUNET_TIME_relative_get_forever (), sender,
565 GNUNET_free (sender);
569 while (GNUNET_NETWORK_socket_select (plugin->rs,
573 && GNUNET_NETWORK_fdset_isset (plugin->rs, udp_sock));
575 plugin->select_task =
576 GNUNET_SCHEDULER_add_select (plugin->env->sched,
577 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
578 GNUNET_SCHEDULER_NO_TASK,
579 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
580 NULL, &udp_plugin_select, plugin);
585 * Create a UDP socket. If possible, use IPv6, otherwise
588 static struct GNUNET_NETWORK_Handle *
589 udp_transport_server_start (void *cls)
591 struct Plugin *plugin = cls;
592 struct GNUNET_NETWORK_Handle *desc;
593 struct sockaddr_in serverAddrv4;
594 struct sockaddr_in6 serverAddrv6;
595 struct sockaddr *serverAddr;
600 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
603 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
606 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
607 #if HAVE_SOCKADDR_IN_SIN_LEN
608 serverAddrv6.sin6_len = sizeof (serverAddrv6);
610 serverAddrv6.sin6_family = AF_INET6;
611 serverAddrv6.sin6_addr = in6addr_any;
612 serverAddrv6.sin6_port = htons (plugin->open_port);
613 addrlen = sizeof (serverAddrv6);
614 serverAddr = (struct sockaddr *) &serverAddrv6;
619 desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
622 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
627 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
628 #if HAVE_SOCKADDR_IN_SIN_LEN
629 serverAddrv4.sin_len = sizeof (serverAddrv4);
631 serverAddrv4.sin_family = AF_INET;
632 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
633 serverAddrv4.sin_port = htons (plugin->open_port);
634 addrlen = sizeof (serverAddrv4);
635 serverAddr = (struct sockaddr *) &serverAddrv4;
641 GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
645 plugin->rs = GNUNET_NETWORK_fdset_create ();
647 GNUNET_NETWORK_fdset_zero (plugin->rs);
648 GNUNET_NETWORK_fdset_set (plugin->rs, desc);
650 plugin->select_task =
651 GNUNET_SCHEDULER_add_select (plugin->env->sched,
652 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
653 GNUNET_SCHEDULER_NO_TASK,
654 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
655 NULL, &udp_plugin_select, plugin);
661 * Function that can be used by the transport service to validate that
662 * another peer is reachable at a particular address (even if we
663 * already have a connection to this peer, this function is required
664 * to establish a new one).
667 * @param target who should receive this message
668 * @param challenge challenge code to use
669 * @param addrlen length of the address
670 * @param addr the address
671 * @param timeout how long should we try to transmit these?
672 * @return GNUNET_OK if the transmission has been scheduled
675 udp_plugin_validate (void *cls,
676 const struct GNUNET_PeerIdentity *target,
678 struct GNUNET_TIME_Relative timeout,
679 const void *addr, size_t addrlen)
681 struct Plugin *plugin = cls;
682 struct Session *new_session;
683 struct UDPPingMessage *msg;
686 return GNUNET_SYSERR;
688 new_session = GNUNET_malloc (sizeof (struct Session));
689 new_session->connect_addr = GNUNET_malloc (addrlen);
690 memcpy (new_session->connect_addr, addr, addrlen);
691 new_session->connect_alen = addrlen;
694 (target, plugin->env->my_identity,
695 sizeof (struct GNUNET_PeerIdentity)) == 0)
697 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
698 ("definitely adding self to session list... hmmm\n"));
701 memcpy (&new_session->target, target, sizeof (struct GNUNET_PeerIdentity));
702 new_session->challenge = challenge;
703 new_session->validated = GNUNET_NO;
704 new_session->latency = GNUNET_TIME_relative_get_zero ();
705 new_session->ping_sent = GNUNET_TIME_absolute_get ();
706 new_session->next = plugin->sessions;
707 plugin->sessions = new_session;
709 msg = GNUNET_malloc (sizeof (struct UDPPingMessage));
710 msg->header.size = htons (sizeof (struct UDPPingMessage));
711 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING);
712 msg->challenge = htons (challenge);
714 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
715 ("In validate, header size is %d, type %d, challenge %u\n"),
716 ntohs (msg->header.size), ntohs (msg->header.type),
717 ntohl (msg->challenge));
719 udp_plugin_send (plugin, target, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
720 &msg->header, timeout, NULL, NULL);
727 * Convert the transports address to a nice, human-readable
731 * @param type name of the transport that generated the address
732 * @param addr one of the addresses of the host, NULL for the last address
733 * the specific address format depends on the transport
734 * @param addrlen length of the address
735 * @param numeric should (IP) addresses be displayed in numeric form?
736 * @param timeout after how long should we give up?
737 * @param asc function to call on each string
738 * @param asc_cls closure for asc
741 udp_plugin_address_pretty_printer (void *cls,
746 struct GNUNET_TIME_Relative timeout,
747 GNUNET_TRANSPORT_AddressStringCallback asc,
754 * Set a quota for receiving data from the given peer; this is a
755 * per-transport limit. The transport should limit its read/select
756 * calls to stay below the quota (in terms of incoming data).
759 * @param target the peer for whom the quota is given
760 * @param quota_in quota for receiving/sending data in bytes per ms
763 udp_plugin_set_receive_quota (void *cls,
764 const struct GNUNET_PeerIdentity *target,
771 * Another peer has suggested an address for this
772 * peer and transport plugin. Check that this could be a valid
773 * address. If so, consider adding it to the list
777 * @param addr pointer to the address
778 * @param addrlen length of addr
779 * @return GNUNET_OK if this is a plausible address for this peer
783 udp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
786 return GNUNET_SYSERR;
791 * The exported method. Makes the core api available via a global and
792 * returns the udp transport API.
795 libgnunet_plugin_transport_udp_init (void *cls)
797 unsigned long long mtu;
799 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
800 struct GNUNET_TRANSPORT_PluginFunctions *api;
801 struct Plugin *plugin;
802 struct GNUNET_SERVICE_Context *service;
803 unsigned long long aport;
804 unsigned long long bport;
806 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
809 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
810 ("Failed to start service for `%s' transport plugin.\n"),
816 GNUNET_CONFIGURATION_get_value_number (env->cfg,
822 GNUNET_CONFIGURATION_get_value_number (env->cfg,
825 &aport)) && (aport > 65535)))
827 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
830 ("Require valid port number for service `%s' in configuration!\n"),
832 GNUNET_SERVICE_stop (service);
840 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
842 _("MTU %llu for `%s' is probably too low!\n"), mtu,
845 plugin = GNUNET_malloc (sizeof (struct Plugin));
846 plugin->open_port = bport;
847 plugin->adv_port = aport;
849 plugin->statistics = NULL;
850 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
851 plugin->sessions = NULL;
854 api->validate = &udp_plugin_validate;
855 api->send = &udp_plugin_send;
856 api->disconnect = &udp_disconnect;
857 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
858 api->set_receive_quota = &udp_plugin_set_receive_quota;
859 api->address_suggested = &udp_plugin_address_suggested;
860 api->cost_estimate = 17; /* TODO: ATS */
861 plugin->service = service;
863 udp_sock = udp_transport_server_start (plugin);
865 GNUNET_assert (udp_sock != NULL);
871 libgnunet_plugin_transport_udp_done (void *cls)
873 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
874 struct Plugin *plugin = api->cls;
876 struct Session *oldpos;
878 udp_transport_server_stop (plugin);
879 if (NULL != hostname_dns)
881 GNUNET_RESOLVER_request_cancel (hostname_dns);
884 GNUNET_SERVICE_stop (plugin->service);
886 pos = plugin->sessions;
889 GNUNET_free (pos->connect_addr);
892 GNUNET_free (oldpos);
895 GNUNET_NETWORK_fdset_destroy (plugin->rs);
896 GNUNET_free (plugin);
901 /* end of plugin_transport_udp.c */