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 * Transport cost to peer, always 1 for UDP (direct connection)
48 #define UDP_DIRECT_DISTANCE 1
51 * Handle for request of hostname resolution, non-NULL if pending.
53 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
56 * How long until we give up on transmitting the welcome message?
58 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 * Message-Packet header.
67 * size of the message, in bytes, including this header.
69 struct GNUNET_MessageHeader header;
72 * What is the identity of the sender (GNUNET_hash of public key)
74 struct GNUNET_PeerIdentity sender;
78 /* Forward definition */
81 struct PrettyPrinterContext
83 GNUNET_TRANSPORT_AddressStringCallback asc;
89 * Encapsulation of all of the state of the plugin.
96 struct GNUNET_TRANSPORT_PluginEnvironment *env;
99 * Handle for the statistics service.
101 struct GNUNET_STATISTICS_Handle *statistics;
104 * Handle to the network service.
106 struct GNUNET_SERVICE_Context *service;
109 * Handle for request of hostname resolution, non-NULL if pending.
111 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
114 * ID of task used to update our addresses when one expires.
116 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
121 GNUNET_SCHEDULER_TaskIdentifier select_task;
124 * Port that we are actually listening on.
129 * Port that the user said we would have visible to the
137 struct GNUNET_NETWORK_FDSet *rs;
141 /* *********** globals ************* */
144 * the socket that we transmit all data with
146 static struct GNUNET_NETWORK_Handle *udp_sock;
149 * Disconnect from a remote node.
151 * @param tsession the session that is closed
152 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
155 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
161 * Shutdown the server process (stop receiving inbound traffic). Maybe
165 udp_transport_server_stop (void *cls)
167 struct Plugin *plugin = cls;
168 GNUNET_assert (udp_sock != NULL);
169 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
171 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
172 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
175 GNUNET_NETWORK_socket_close (udp_sock);
181 * Function that can be used by the transport service to transmit
182 * a message using the plugin.
185 * @param target who should receive this message (ignored by UDP)
186 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
187 * @param msgbufsize the size of the msgbuf to send
188 * @param priority how important is the message (ignored by UDP)
189 * @param timeout when should we time out (give up) if we can not transmit?
190 * @param addr the addr to send the message to, needs to be a sockaddr for us
191 * @param addrlen the len of addr
192 * @param force_address not used, we had better have an address to send to
193 * because we are stateless!!
194 * @param cont continuation to call once the message has
195 * been transmitted (or if the transport is ready
196 * for the next transmission call; or if the
197 * peer disconnected...)
198 * @param cont_cls closure for cont
200 * @return the number of bytes written
204 udp_plugin_send (void *cls,
205 const struct GNUNET_PeerIdentity *target,
208 unsigned int priority,
209 struct GNUNET_TIME_Relative timeout,
213 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
215 struct Plugin *plugin = cls;
216 struct UDPMessage *message;
220 GNUNET_assert(udp_sock != NULL);
222 if ((addr == NULL) || (addrlen == 0))
225 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
226 ("udp_plugin_send called without address, returning!\n"));
228 cont (cont_cls, target, GNUNET_OK);
229 return 0; /* Can never send if we don't have an address!! */
232 /* Build the message to be sent */
233 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
234 ssize = sizeof (struct UDPMessage) + msgbuf_size;
237 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
238 ("In udp_send, ssize is %d, sending message to %s\n"), ssize, GNUNET_a2s((const struct sockaddr *)addr, addrlen));
240 message->header.size = htons (ssize);
241 message->header.type = htons (0);
242 memcpy (&message->sender, plugin->env->my_identity,
243 sizeof (struct GNUNET_PeerIdentity));
244 memcpy (&message[1], msgbuf, msgbuf_size);
246 /* Actually send the message */
248 GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
254 if (sent == GNUNET_SYSERR)
255 cont (cont_cls, target, GNUNET_SYSERR);
259 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
260 ("Sucessfully sent message, calling transmit continuation!\n"));
262 cont (cont_cls, target, GNUNET_OK);
265 GNUNET_free (message);
271 * Add the IP of our network interface to the list of
272 * our external IP addresses.
275 process_interfaces (void *cls,
278 const struct sockaddr *addr, socklen_t addrlen)
280 struct Plugin *plugin = cls;
282 struct sockaddr_in *v4;
283 struct sockaddr_in6 *v6;
285 af = addr->sa_family;
288 v4 = (struct sockaddr_in *) addr;
289 v4->sin_port = htons (plugin->adv_port);
293 GNUNET_assert (af == AF_INET6);
294 v6 = (struct sockaddr_in6 *) addr;
295 v6->sin6_port = htons (plugin->adv_port);
297 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
298 GNUNET_ERROR_TYPE_BULK,
299 "udp", _("Found address `%s' (%s)\n"),
300 GNUNET_a2s (addr, addrlen), name);
301 plugin->env->notify_address (plugin->env->cls,
303 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
310 * Function called by the resolver for each address obtained from DNS
311 * for our own hostname. Add the addresses to the list of our
312 * external IP addresses.
315 * @param addr one of the addresses of the host, NULL for the last address
316 * @param addrlen length of the address
319 process_hostname_ips (void *cls,
320 const struct sockaddr *addr, socklen_t addrlen)
322 struct Plugin *plugin = cls;
326 plugin->hostname_dns = NULL;
329 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
334 * @param cls the plugin handle
335 * @param tc the scheduling context (for rescheduling this function again)
337 * We have been notified that our writeset has something to read. Presumably
338 * select has been called already, so we can go ahead and start reading from
339 * the socket immediately. Then we check if there is more to be read by
340 * calling select ourselves while there is stuff on the wire. Then reschedule
341 * this function to be called again once more is available.
345 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
347 struct Plugin *plugin = cls;
349 struct UDPMessage *msg;
350 const struct GNUNET_MessageHeader *hdr;
351 struct GNUNET_PeerIdentity *sender;
354 struct sockaddr_storage addr;
360 const struct GNUNET_MessageHeader *currhdr;
363 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
364 ("entered select...\n"));
367 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
370 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
371 ("we expect to read %u bytes\n"), buflen);
374 if (buflen == GNUNET_NO)
377 buf = GNUNET_malloc (buflen);
378 fromlen = sizeof (addr);
380 memset (&addr, 0, fromlen);
382 GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
383 (struct sockaddr *) &addr, &fromlen);
386 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
387 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
396 msg = (struct UDPMessage *) buf;
399 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
400 ("header reports message size of %d\n"),
401 ntohs (msg->header.size));
403 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
404 ("header reports message type of %d\n"),
405 ntohs (msg->header.type));
407 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
410 GNUNET_NETWORK_fdset_zero (plugin->rs);
411 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
414 hdr = (const struct GNUNET_MessageHeader *) &msg[1];
415 msgbuf = (char *)&msg[1];
416 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
417 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
421 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UDP", _
424 ("offset is %d, tsize is %d (UDPMessage size is %d)\n"),
425 offset, tsize, sizeof(struct UDPMessage));
427 while (offset < tsize)
429 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
431 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
432 ("processing msg %d: type %d, size %d at offset %d\n"),
433 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
435 plugin->env->receive (plugin->env->cls,
436 sender, currhdr, UDP_DIRECT_DISTANCE, (char *)&addr, fromlen);
437 offset += ntohs(currhdr->size);
439 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
440 ("offset now %d, tsize %d\n"),
446 GNUNET_free (sender);
449 plugin->select_task =
450 GNUNET_SCHEDULER_add_select (plugin->env->sched,
451 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
452 GNUNET_SCHEDULER_NO_TASK,
453 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
454 NULL, &udp_plugin_select, plugin);
459 * Create a UDP socket. If possible, use IPv6, otherwise
462 static struct GNUNET_NETWORK_Handle *
463 udp_transport_server_start (void *cls)
465 struct Plugin *plugin = cls;
466 struct GNUNET_NETWORK_Handle *desc;
467 struct sockaddr_in serverAddrv4;
468 struct sockaddr_in6 serverAddrv6;
469 struct sockaddr *serverAddr;
474 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
477 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
480 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
481 #if HAVE_SOCKADDR_IN_SIN_LEN
482 serverAddrv6.sin6_len = sizeof (serverAddrv6);
484 serverAddrv6.sin6_family = AF_INET6;
485 serverAddrv6.sin6_addr = in6addr_any;
486 serverAddrv6.sin6_port = htons (plugin->open_port);
487 addrlen = sizeof (serverAddrv6);
488 serverAddr = (struct sockaddr *) &serverAddrv6;
493 desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
496 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
501 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
502 #if HAVE_SOCKADDR_IN_SIN_LEN
503 serverAddrv4.sin_len = sizeof (serverAddrv4);
505 serverAddrv4.sin_family = AF_INET;
506 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
507 serverAddrv4.sin_port = htons (plugin->open_port);
508 addrlen = sizeof (serverAddrv4);
509 serverAddr = (struct sockaddr *) &serverAddrv4;
515 GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
519 plugin->rs = GNUNET_NETWORK_fdset_create ();
521 GNUNET_NETWORK_fdset_zero (plugin->rs);
522 GNUNET_NETWORK_fdset_set (plugin->rs, desc);
524 plugin->select_task =
525 GNUNET_SCHEDULER_add_select (plugin->env->sched,
526 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
527 GNUNET_SCHEDULER_NO_TASK,
528 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
529 NULL, &udp_plugin_select, plugin);
536 * Check if the given port is plausible (must be either
537 * our listen port or our advertised port). If it is
538 * neither, we return one of these two ports at random.
540 * @return either in_port or a more plausible port
543 check_port (struct Plugin *plugin, uint16_t in_port)
545 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
547 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
549 ? plugin->open_port : plugin->adv_port;
554 * Another peer has suggested an address for this peer and transport
555 * plugin. Check that this could be a valid address. This function
556 * is not expected to 'validate' the address in the sense of trying to
557 * connect to it but simply to see if the binary format is technically
558 * legal for establishing a connection.
560 * @param addr pointer to the address, may be modified (slightly)
561 * @param addrlen length of addr
562 * @return GNUNET_OK if this is a plausible address for this peer
563 * and transport, GNUNET_SYSERR if not
565 * TODO: perhaps make everything work with sockaddr_storage, it may
566 * be a cleaner way to handle addresses in UDP
569 udp_check_address (void *cls, void *addr, size_t addrlen)
571 struct Plugin *plugin = cls;
572 char buf[sizeof (struct sockaddr_in6)];
574 struct sockaddr_in *v4;
575 struct sockaddr_in6 *v6;
577 if ((addrlen != sizeof (struct sockaddr_in)) &&
578 (addrlen != sizeof (struct sockaddr_in6)))
581 return GNUNET_SYSERR;
583 memcpy (buf, addr, sizeof (struct sockaddr_in6));
584 if (addrlen == sizeof (struct sockaddr_in))
586 v4 = (struct sockaddr_in *) buf;
587 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
591 v6 = (struct sockaddr_in6 *) buf;
592 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
597 "Informing transport service about my address `%s'.\n",
598 GNUNET_a2s (addr, addrlen));
606 * Append our port and forward the result.
609 append_port (void *cls, const char *hostname)
611 struct PrettyPrinterContext *ppc = cls;
614 if (hostname == NULL)
616 ppc->asc (ppc->asc_cls, NULL);
620 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
621 ppc->asc (ppc->asc_cls, ret);
627 * Convert the transports address to a nice, human-readable
631 * @param type name of the transport that generated the address
632 * @param addr one of the addresses of the host, NULL for the last address
633 * the specific address format depends on the transport
634 * @param addrlen length of the address
635 * @param numeric should (IP) addresses be displayed in numeric form?
636 * @param timeout after how long should we give up?
637 * @param asc function to call on each string
638 * @param asc_cls closure for asc
641 udp_plugin_address_pretty_printer (void *cls,
646 struct GNUNET_TIME_Relative timeout,
647 GNUNET_TRANSPORT_AddressStringCallback asc,
650 struct Plugin *plugin = cls;
651 const struct sockaddr_in *v4;
652 const struct sockaddr_in6 *v6;
653 struct PrettyPrinterContext *ppc;
655 if ((addrlen != sizeof (struct sockaddr_in)) &&
656 (addrlen != sizeof (struct sockaddr_in6)))
658 /* invalid address */
663 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
665 ppc->asc_cls = asc_cls;
666 if (addrlen == sizeof (struct sockaddr_in))
668 v4 = (const struct sockaddr_in *) addr;
669 ppc->port = ntohs (v4->sin_port);
673 v6 = (const struct sockaddr_in6 *) addr;
674 ppc->port = ntohs (v6->sin6_port);
677 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
681 !numeric, timeout, &append_port, ppc);
685 * Set a quota for receiving data from the given peer; this is a
686 * per-transport limit. This call has no meaning for UDP, as if
687 * we don't receive data it still comes in. UDP has no friendliness
688 * guarantees, and our buffers will fill at some level.
691 * @param target the peer for whom the quota is given
692 * @param quota_in quota for receiving/sending data in bytes per ms
695 udp_plugin_set_receive_quota (void *cls,
696 const struct GNUNET_PeerIdentity *target,
699 return; /* Do nothing */
703 * The exported method. Makes the core api available via a global and
704 * returns the udp transport API.
707 libgnunet_plugin_transport_udp_init (void *cls)
709 unsigned long long mtu;
711 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
712 struct GNUNET_TRANSPORT_PluginFunctions *api;
713 struct Plugin *plugin;
714 struct GNUNET_SERVICE_Context *service;
715 unsigned long long aport;
716 unsigned long long bport;
718 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
721 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
722 ("Failed to start service for `%s' transport plugin.\n"),
728 GNUNET_CONFIGURATION_get_value_number (env->cfg,
734 GNUNET_CONFIGURATION_get_value_number (env->cfg,
737 &aport)) && (aport > 65535)))
739 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
742 ("Require valid port number for service `%s' in configuration!\n"),
744 GNUNET_SERVICE_stop (service);
752 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
754 _("MTU %llu for `%s' is probably too low!\n"), mtu,
757 plugin = GNUNET_malloc (sizeof (struct Plugin));
758 plugin->open_port = bport;
759 plugin->adv_port = aport;
761 plugin->statistics = NULL;
762 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
765 api->send = &udp_plugin_send;
766 api->disconnect = &udp_disconnect;
767 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
768 api->set_receive_quota = &udp_plugin_set_receive_quota;
769 api->check_address = &udp_check_address;
771 plugin->service = service;
773 /* FIXME: do the two calls below periodically again and
774 not just once (since the info we get might change...) */
775 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
776 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
779 HOSTNAME_RESOLVE_TIMEOUT,
780 &process_hostname_ips,
783 udp_sock = udp_transport_server_start (plugin);
785 GNUNET_assert (udp_sock != NULL);
791 libgnunet_plugin_transport_udp_done (void *cls)
793 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
794 struct Plugin *plugin = api->cls;
796 udp_transport_server_stop (plugin);
797 if (NULL != hostname_dns)
799 GNUNET_RESOLVER_request_cancel (hostname_dns);
802 GNUNET_SERVICE_stop (plugin->service);
804 GNUNET_NETWORK_fdset_destroy (plugin->rs);
805 GNUNET_free (plugin);
810 /* end of plugin_transport_udp.c */