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_YES
46 * The default maximum size of each outbound UDP message,
47 * optimal value for Ethernet (10 or 100 MBit).
49 #define MESSAGE_SIZE 1472
52 * Handle for request of hostname resolution, non-NULL if pending.
54 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
57 * Message-Packet header.
62 * size of the message, in bytes, including this header.
64 struct GNUNET_MessageHeader header;
67 * What is the identity of the sender (GNUNET_hash of public key)
69 struct GNUNET_PeerIdentity sender;
73 /* Forward definition */
77 * Session handle for UDP connections.
83 * Stored in a linked list.
88 * Pointer to the global plugin struct.
90 struct Plugin *plugin;
93 * To whom are we talking to (set to our identity
95 struct GNUNET_PeerIdentity target;
98 * Address of the other peer if WE initiated the connection
99 * (and hence can be sure what it is), otherwise NULL.
104 * Length of connect_addr, can be 0.
109 * Random challenge number for validation
114 * Have we received validation (performed ping/pong) from this peer?
116 unsigned int validated;
121 * Encapsulation of all of the state of the plugin.
128 struct GNUNET_TRANSPORT_PluginEnvironment *env;
131 * List of open TCP sessions.
133 struct Session *sessions;
136 * Handle for the statistics service.
138 struct GNUNET_STATISTICS_Handle *statistics;
141 * Handle to the network service.
143 struct GNUNET_SERVICE_Context *service;
146 * ID of task used to update our addresses when one expires.
148 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
153 GNUNET_SCHEDULER_TaskIdentifier select_task;
156 * Port that we are actually listening on.
161 * Port that the user said we would have visible to the
169 struct GNUNET_NETWORK_FDSet *rs;
174 * Message used to ask a peer to validate receipt (to check an address
175 * from a HELLO). Followed by the address used. Note that the
176 * recipients response does not affirm that he has this address,
177 * only that he got the challenge message.
179 struct UDPPingMessage
183 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING
185 struct GNUNET_MessageHeader header;
188 * Random challenge number (in network byte order).
190 uint32_t challenge GNUNET_PACKED;
198 * Message used to validate a HELLO. The challenge is included in the
199 * confirmation to make matching of replies to requests possible. The
200 * signature signs the original challenge number, our public key, the
201 * sender's address (so that the sender can check that the address we
202 * saw is plausible for him and possibly detect a MiM attack) and a
203 * timestamp (to limit replay).<p>
205 * This message is followed by the address of the
206 * client that we are observing (which is part of what
209 struct UDPPongMessage
212 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG
214 struct GNUNET_MessageHeader header;
217 * Random challenge number (in network byte order).
219 uint32_t challenge GNUNET_PACKED;
221 /* Length of addr, appended to end of message */
222 unsigned int addrlen;
225 /* *********** globals ************* */
228 * the socket that we transmit all data with
230 static struct GNUNET_NETWORK_Handle *udp_sock;
234 * A (core) Session is to be associated with a transport session. The
235 * transport service may want to know in order to call back on the
236 * core if the connection is being closed.
238 * @param session the session handle passed along
239 * from the call to receive that was made by the transport
241 * @return GNUNET_OK if the session could be associated,
242 * GNUNET_SYSERR if not.
245 udp_associate (struct Session *session)
247 return GNUNET_SYSERR; /* UDP connections can never be associated */
251 * Disconnect from a remote node.
253 * @param tsession the session that is closed
254 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
257 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
263 * Shutdown the server process (stop receiving inbound traffic). Maybe
267 udp_transport_server_stop (void *cls)
269 struct Plugin *plugin = cls;
270 GNUNET_assert (udp_sock != NULL);
271 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
273 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
274 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
277 GNUNET_NETWORK_socket_close (udp_sock);
282 static struct Session *
283 find_session (void *cls, struct Session *session_list,
284 const struct GNUNET_PeerIdentity *peer)
286 struct Plugin *plugin = cls;
292 if (memcmp (peer, &pos->target, sizeof (struct GNUNET_PeerIdentity)) ==
302 * Function that can be used by the transport service to transmit
303 * a message using the plugin.
306 * @param service_context value passed to the transport-service
307 * to identify the neighbour
308 * @param target who should receive this message
309 * @param priority how important is the message
310 * @param msg the message to transmit
311 * @param timeout when should we time out (give up) if we can not transmit?
312 * @param cont continuation to call once the message has
313 * been transmitted (or if the transport is ready
314 * for the next transmission call; or if the
315 * peer disconnected...)
316 * @param cont_cls closure for cont
319 udp_plugin_send (void *cls,
320 const struct GNUNET_PeerIdentity *target,
321 unsigned int priority,
322 const struct GNUNET_MessageHeader *msg,
323 struct GNUNET_TIME_Relative timeout,
324 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
326 struct Plugin *plugin = cls;
327 struct Session *session;
328 struct UDPMessage *message;
332 session = find_session (plugin, plugin->sessions, target);
334 if ((session == NULL) || (udp_sock == NULL))
337 /* Build the message to be sent */
338 message = GNUNET_malloc (sizeof (struct UDPMessage) + ntohs (msg->size));
339 ssize = sizeof (struct UDPMessage) + ntohs (msg->size);
342 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
343 ("In udp_send, ssize is %d\n"), ssize);
345 message->header.size = htons (ssize);
346 message->header.type = htons (0);
347 memcpy (&message->sender, plugin->env->my_identity,
348 sizeof (struct GNUNET_PeerIdentity));
349 memcpy (&message[1], msg, ntohs (msg->size));
351 /* Actually send the message */
353 GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
354 session->connect_addr,
355 session->connect_alen);
359 if (sent == GNUNET_SYSERR)
360 cont (cont_cls, target, GNUNET_SYSERR);
362 cont (cont_cls, target, GNUNET_OK);
369 * We've received a PING from this peer via UDP.
370 * Send back our PONG.
373 * @param sender the Identity of the sender
374 * @param message the actual message
377 handle_udp_ping (void *cls,
378 struct GNUNET_PeerIdentity *sender,
379 struct sockaddr_storage *addr, size_t addrlen,
380 const struct GNUNET_MessageHeader *message)
382 struct Plugin *plugin = cls;
383 struct Session *head = plugin->sessions;
384 const struct UDPPingMessage *ping = (const struct UDPPingMessage *) message;
385 struct UDPPongMessage *pong;
386 struct Session *found;
389 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
390 ("handling ping, challenge is %d\n"),
391 ntohs (ping->challenge));
393 found = find_session (plugin, head, sender);
396 pong = GNUNET_malloc (sizeof (struct UDPPongMessage) + addrlen);
397 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG);
398 pong->header.size = htons (sizeof (struct UDPPongMessage) + addrlen);
399 pong->challenge = ping->challenge;
400 memcpy (&pong[1], addr, addrlen);
401 pong->addrlen = htons (addrlen);
403 udp_plugin_send (plugin, sender, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
405 GNUNET_TIME_relative_multiply
406 (GNUNET_TIME_UNIT_SECONDS, 30), NULL, NULL);
414 * We've received a PONG from this peer via UDP.
415 * Great. Call validate func if we haven't already
419 * @param client identification of the client
420 * @param message the actual message
423 handle_udp_pong (void *cls,
424 struct GNUNET_PeerIdentity *sender,
425 const struct GNUNET_MessageHeader *message)
427 struct Plugin *plugin = cls;
428 const struct UDPPongMessage *pong = (struct UDPPongMessage *) message;
429 struct Session *found;
430 unsigned int addr_len;
431 struct sockaddr_storage addr;
434 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _("handling pong\n"));
436 found = find_session (plugin, plugin->sessions, sender);
438 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
439 ("found->challenge %d, pong->challenge %d\n"),
440 found->challenge, ntohs (pong->challenge));
442 if ((found != NULL) && (found->challenge == ntohs (pong->challenge)))
444 found->validated = GNUNET_YES;
445 addr_len = ntohs (pong->addrlen);
447 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
448 ("found associated ping, addr is %u bytes\n"),
451 memcpy (&addr, &pong[1], addr_len);
452 plugin->env->notify_validation (plugin->env->cls, "udp", sender,
453 ntohs (pong->challenge),
460 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
461 ("Session not found!\n"));
468 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
470 struct Plugin *plugin = cls;
471 struct GNUNET_TIME_Relative timeout =
472 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
474 struct UDPMessage *msg;
475 const struct GNUNET_MessageHeader *hdr;
476 struct GNUNET_PeerIdentity *sender;
479 struct sockaddr_storage addr;
484 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
487 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
488 ("we expect to read %u bytes\n"), buflen);
491 if (buflen == GNUNET_NO)
494 buf = GNUNET_malloc (buflen);
495 fromlen = sizeof (addr);
498 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
499 ("src_addr_len is %u\n"), fromlen);
502 memset (&addr, 0, fromlen);
504 GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
505 (struct sockaddr *) &addr, &fromlen);
508 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
509 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
519 msg = (struct UDPMessage *) buf;
522 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
523 ("header reports message size of %d\n"),
524 ntohs (msg->header.size));
526 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
527 ("header reports message type of %d\n"),
528 ntohs (msg->header.type));
530 /*if (ntohs(hdr->size) < sizeof(struct UDPMessage))
533 GNUNET_NETWORK_fdset_zero(plugin->rs);
534 GNUNET_NETWORK_fdset_set(plugin->rs, udp_sock);
537 hdr = (const struct GNUNET_MessageHeader *) &msg[1];
538 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
539 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
542 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
543 ("msg reports message size of %d\n"),
546 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
547 ("msg reports message type of %d\n"),
551 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING)
553 handle_udp_ping (plugin, sender, &addr, fromlen, hdr);
556 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG)
558 handle_udp_pong (plugin, sender, hdr);
564 while (GNUNET_NETWORK_socket_select (plugin->rs,
568 && GNUNET_NETWORK_fdset_isset (plugin->rs, udp_sock));
570 plugin->select_task =
571 GNUNET_SCHEDULER_add_select (plugin->env->sched,
572 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
573 GNUNET_SCHEDULER_NO_TASK,
574 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
575 NULL, &udp_plugin_select, plugin);
580 * Create a UDP socket. If possible, use IPv6, otherwise
583 static struct GNUNET_NETWORK_Handle *
584 udp_transport_server_start (void *cls)
586 struct Plugin *plugin = cls;
587 struct GNUNET_NETWORK_Handle *desc;
588 struct sockaddr_in serverAddrv4;
589 struct sockaddr_in6 serverAddrv6;
590 struct sockaddr *serverAddr;
595 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
598 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
601 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
602 #if HAVE_SOCKADDR_IN_SIN_LEN
603 serverAddrv6.sin6_len = sizeof (serverAddrv6);
605 serverAddrv6.sin6_family = AF_INET6;
606 serverAddrv6.sin6_addr = in6addr_any;
607 serverAddrv6.sin6_port = htons (plugin->open_port);
608 addrlen = sizeof (serverAddrv6);
609 serverAddr = (struct sockaddr *) &serverAddrv6;
614 desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
617 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
622 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
623 #if HAVE_SOCKADDR_IN_SIN_LEN
624 serverAddrv4.sin_len = sizeof (serverAddrv4);
626 serverAddrv4.sin_family = AF_INET;
627 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
628 serverAddrv4.sin_port = htons (plugin->open_port);
629 addrlen = sizeof (serverAddrv4);
630 serverAddr = (struct sockaddr *) &serverAddrv4;
636 GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
640 plugin->rs = GNUNET_NETWORK_fdset_create ();
642 GNUNET_NETWORK_fdset_zero (plugin->rs);
643 GNUNET_NETWORK_fdset_set (plugin->rs, desc);
645 plugin->select_task =
646 GNUNET_SCHEDULER_add_select (plugin->env->sched,
647 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
648 GNUNET_SCHEDULER_NO_TASK,
649 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
650 NULL, &udp_plugin_select, plugin);
656 * Function that can be used by the transport service to validate that
657 * another peer is reachable at a particular address (even if we
658 * already have a connection to this peer, this function is required
659 * to establish a new one).
662 * @param target who should receive this message
663 * @param challenge challenge code to use
664 * @param addrlen length of the address
665 * @param addr the address
666 * @param timeout how long should we try to transmit these?
667 * @return GNUNET_OK if the transmission has been scheduled
670 udp_plugin_validate (void *cls,
671 const struct GNUNET_PeerIdentity *target,
673 struct GNUNET_TIME_Relative timeout,
674 const void *addr, size_t addrlen)
676 struct Plugin *plugin = cls;
677 struct Session *new_session;
678 struct UDPPongMessage *msg;
681 return GNUNET_SYSERR;
683 new_session = GNUNET_malloc (sizeof (struct Session));
684 new_session->connect_addr = GNUNET_malloc (addrlen);
685 memcpy (new_session->connect_addr, addr, addrlen);
686 new_session->connect_alen = addrlen;
689 (target, plugin->env->my_identity,
690 sizeof (struct GNUNET_PeerIdentity)) == 0)
692 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
693 ("definitely adding self to session list... hmmm\n"));
696 memcpy (&new_session->target, target, sizeof (struct GNUNET_PeerIdentity));
697 new_session->challenge = challenge;
698 new_session->validated = GNUNET_NO;
699 new_session->next = plugin->sessions;
700 plugin->sessions = new_session;
702 msg = GNUNET_malloc (sizeof (struct UDPPongMessage));
703 msg->header.size = htons (sizeof (struct UDPPongMessage));
704 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING);
705 msg->challenge = htons (challenge);
707 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
708 ("In validate, header size is %d, type %d, challenge %u\n"),
709 ntohs (msg->header.size), ntohs (msg->header.type),
710 ntohl (msg->challenge));
712 udp_plugin_send (plugin, target, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
713 &msg->header, timeout, NULL, NULL);
719 * Convert the transports address to a nice, human-readable
723 * @param type name of the transport that generated the address
724 * @param addr one of the addresses of the host, NULL for the last address
725 * the specific address format depends on the transport
726 * @param addrlen length of the address
727 * @param numeric should (IP) addresses be displayed in numeric form?
728 * @param timeout after how long should we give up?
729 * @param asc function to call on each string
730 * @param asc_cls closure for asc
733 udp_plugin_address_pretty_printer (void *cls,
738 struct GNUNET_TIME_Relative timeout,
739 GNUNET_TRANSPORT_AddressStringCallback asc,
746 * Set a quota for receiving data from the given peer; this is a
747 * per-transport limit. The transport should limit its read/select
748 * calls to stay below the quota (in terms of incoming data).
751 * @param target the peer for whom the quota is given
752 * @param quota_in quota for receiving/sending data in bytes per ms
755 udp_plugin_set_receive_quota (void *cls,
756 const struct GNUNET_PeerIdentity *target,
763 * Another peer has suggested an address for this
764 * peer and transport plugin. Check that this could be a valid
765 * address. If so, consider adding it to the list
769 * @param addr pointer to the address
770 * @param addrlen length of addr
771 * @return GNUNET_OK if this is a plausible address for this peer
775 udp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
778 return GNUNET_SYSERR;
783 * The exported method. Makes the core api available via a global and
784 * returns the udp transport API.
787 libgnunet_plugin_transport_udp_init (void *cls)
789 unsigned long long mtu;
791 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
792 struct GNUNET_TRANSPORT_PluginFunctions *api;
793 struct Plugin *plugin;
794 struct GNUNET_SERVICE_Context *service;
795 unsigned long long aport;
796 unsigned long long bport;
798 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
801 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
802 ("Failed to start service for `%s' transport plugin.\n"),
808 GNUNET_CONFIGURATION_get_value_number (env->cfg,
814 GNUNET_CONFIGURATION_get_value_number (env->cfg,
817 &aport)) && (aport > 65535)))
819 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
822 ("Require valid port number for service `%s' in configuration!\n"),
824 GNUNET_SERVICE_stop (service);
832 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
834 _("MTU %llu for `%s' is probably too low!\n"), mtu,
837 plugin = GNUNET_malloc (sizeof (struct Plugin));
838 plugin->open_port = bport;
839 plugin->adv_port = aport;
841 plugin->statistics = NULL;
842 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
843 plugin->sessions = NULL;
846 api->validate = &udp_plugin_validate;
847 api->send = &udp_plugin_send;
848 api->disconnect = &udp_disconnect;
849 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
850 api->set_receive_quota = &udp_plugin_set_receive_quota;
851 api->address_suggested = &udp_plugin_address_suggested;
852 api->cost_estimate = 17; /* TODO: ATS */
853 plugin->service = service;
855 udp_sock = udp_transport_server_start (plugin);
857 GNUNET_assert (udp_sock != NULL);
863 libgnunet_plugin_transport_udp_done (void *cls)
865 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
866 struct Plugin *plugin = api->cls;
868 udp_transport_server_stop (plugin);
869 if (NULL != hostname_dns)
871 GNUNET_RESOLVER_request_cancel (hostname_dns);
874 GNUNET_SERVICE_stop (plugin->service);
875 GNUNET_free (plugin);
880 /* end of plugin_transport_udp.c */