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;
170 GNUNET_assert (udp_sock != NULL);
171 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
173 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
174 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
177 ret = GNUNET_NETWORK_socket_close (udp_sock);
178 if (ret != GNUNET_SYSERR)
184 * Function that can be used by the transport service to transmit
185 * a message using the plugin.
188 * @param target who should receive this message (ignored by UDP)
189 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
190 * @param msgbufsize the size of the msgbuf to send
191 * @param priority how important is the message (ignored by UDP)
192 * @param timeout when should we time out (give up) if we can not transmit?
193 * @param addr the addr to send the message to, needs to be a sockaddr for us
194 * @param addrlen the len of addr
195 * @param force_address not used, we had better have an address to send to
196 * because we are stateless!!
197 * @param cont continuation to call once the message has
198 * been transmitted (or if the transport is ready
199 * for the next transmission call; or if the
200 * peer disconnected...)
201 * @param cont_cls closure for cont
203 * @return the number of bytes written
207 udp_plugin_send (void *cls,
208 const struct GNUNET_PeerIdentity *target,
211 unsigned int priority,
212 struct GNUNET_TIME_Relative timeout,
216 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
218 struct Plugin *plugin = cls;
219 struct UDPMessage *message;
223 GNUNET_assert(udp_sock != NULL);
225 if ((addr == NULL) || (addrlen == 0))
228 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
229 ("udp_plugin_send called without address, returning!\n"));
231 cont (cont_cls, target, GNUNET_OK);
232 return 0; /* Can never send if we don't have an address!! */
235 /* Build the message to be sent */
236 message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
237 ssize = sizeof (struct UDPMessage) + msgbuf_size;
240 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
241 ("In udp_send, ssize is %d, sending message to %s\n"), ssize, GNUNET_a2s((const struct sockaddr *)addr, addrlen));
243 message->header.size = htons (ssize);
244 message->header.type = htons (0);
245 memcpy (&message->sender, plugin->env->my_identity,
246 sizeof (struct GNUNET_PeerIdentity));
247 memcpy (&message[1], msgbuf, msgbuf_size);
249 /* Actually send the message */
251 GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
257 if (sent == GNUNET_SYSERR)
258 cont (cont_cls, target, GNUNET_SYSERR);
262 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
263 ("Sucessfully sent message, calling transmit continuation!\n"));
265 cont (cont_cls, target, GNUNET_OK);
268 GNUNET_free (message);
274 * Add the IP of our network interface to the list of
275 * our external IP addresses.
278 process_interfaces (void *cls,
281 const struct sockaddr *addr, socklen_t addrlen)
283 struct Plugin *plugin = cls;
285 struct sockaddr_in *v4;
286 struct sockaddr_in6 *v6;
288 af = addr->sa_family;
291 v4 = (struct sockaddr_in *) addr;
292 v4->sin_port = htons (plugin->adv_port);
296 GNUNET_assert (af == AF_INET6);
297 v6 = (struct sockaddr_in6 *) addr;
298 v6->sin6_port = htons (plugin->adv_port);
300 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
301 GNUNET_ERROR_TYPE_BULK,
302 "udp", _("Found address `%s' (%s)\n"),
303 GNUNET_a2s (addr, addrlen), name);
304 plugin->env->notify_address (plugin->env->cls,
306 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
313 * Function called by the resolver for each address obtained from DNS
314 * for our own hostname. Add the addresses to the list of our
315 * external IP addresses.
318 * @param addr one of the addresses of the host, NULL for the last address
319 * @param addrlen length of the address
322 process_hostname_ips (void *cls,
323 const struct sockaddr *addr, socklen_t addrlen)
325 struct Plugin *plugin = cls;
329 plugin->hostname_dns = NULL;
332 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
337 * @param cls the plugin handle
338 * @param tc the scheduling context (for rescheduling this function again)
340 * We have been notified that our writeset has something to read. Presumably
341 * select has been called already, so we can go ahead and start reading from
342 * the socket immediately. Then we check if there is more to be read by
343 * calling select ourselves while there is stuff on the wire. Then reschedule
344 * this function to be called again once more is available.
348 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
350 struct Plugin *plugin = cls;
352 struct UDPMessage *msg;
353 const struct GNUNET_MessageHeader *hdr;
354 struct GNUNET_PeerIdentity *sender;
357 struct sockaddr_storage addr;
363 const struct GNUNET_MessageHeader *currhdr;
366 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
367 ("entered select...\n"));
370 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
373 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
374 ("we expect to read %u bytes\n"), buflen);
377 if (buflen == GNUNET_NO)
380 buf = GNUNET_malloc (buflen);
381 fromlen = sizeof (addr);
383 memset (&addr, 0, fromlen);
385 GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
386 (struct sockaddr *) &addr, &fromlen);
389 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
390 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
399 msg = (struct UDPMessage *) buf;
402 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
403 ("header reports message size of %d\n"),
404 ntohs (msg->header.size));
406 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
407 ("header reports message type of %d\n"),
408 ntohs (msg->header.type));
410 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
413 GNUNET_NETWORK_fdset_zero (plugin->rs);
414 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
417 hdr = (const struct GNUNET_MessageHeader *) &msg[1];
418 msgbuf = (char *)&msg[1];
419 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
420 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
424 tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UDP", _
427 ("offset is %d, tsize is %d (UDPMessage size is %d)\n"),
428 offset, tsize, sizeof(struct UDPMessage));
430 while (offset < tsize)
432 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
434 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
435 ("processing msg %d: type %d, size %d at offset %d\n"),
436 count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
438 plugin->env->receive (plugin->env->cls,
439 sender, currhdr, UDP_DIRECT_DISTANCE, (char *)&addr, fromlen);
440 offset += ntohs(currhdr->size);
442 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
443 ("offset now %d, tsize %d\n"),
449 GNUNET_free (sender);
452 plugin->select_task =
453 GNUNET_SCHEDULER_add_select (plugin->env->sched,
454 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
455 GNUNET_SCHEDULER_NO_TASK,
456 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
457 NULL, &udp_plugin_select, plugin);
462 * Create a UDP socket. If possible, use IPv6, otherwise
465 static struct GNUNET_NETWORK_Handle *
466 udp_transport_server_start (void *cls)
468 struct Plugin *plugin = cls;
469 struct GNUNET_NETWORK_Handle *desc;
470 struct sockaddr_in serverAddrv4;
471 struct sockaddr_in6 serverAddrv6;
472 struct sockaddr *serverAddr;
477 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
480 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
483 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
484 #if HAVE_SOCKADDR_IN_SIN_LEN
485 serverAddrv6.sin6_len = sizeof (serverAddrv6);
487 serverAddrv6.sin6_family = AF_INET6;
488 serverAddrv6.sin6_addr = in6addr_any;
489 serverAddrv6.sin6_port = htons (plugin->open_port);
490 addrlen = sizeof (serverAddrv6);
491 serverAddr = (struct sockaddr *) &serverAddrv6;
496 desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
499 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
504 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
505 #if HAVE_SOCKADDR_IN_SIN_LEN
506 serverAddrv4.sin_len = sizeof (serverAddrv4);
508 serverAddrv4.sin_family = AF_INET;
509 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
510 serverAddrv4.sin_port = htons (plugin->open_port);
511 addrlen = sizeof (serverAddrv4);
512 serverAddr = (struct sockaddr *) &serverAddrv4;
518 GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
522 plugin->rs = GNUNET_NETWORK_fdset_create ();
524 GNUNET_NETWORK_fdset_zero (plugin->rs);
525 GNUNET_NETWORK_fdset_set (plugin->rs, desc);
527 plugin->select_task =
528 GNUNET_SCHEDULER_add_select (plugin->env->sched,
529 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
530 GNUNET_SCHEDULER_NO_TASK,
531 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
532 NULL, &udp_plugin_select, plugin);
539 * Check if the given port is plausible (must be either
540 * our listen port or our advertised port). If it is
541 * neither, we return one of these two ports at random.
543 * @return either in_port or a more plausible port
546 check_port (struct Plugin *plugin, uint16_t in_port)
548 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
550 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
552 ? plugin->open_port : plugin->adv_port;
557 * Another peer has suggested an address for this peer and transport
558 * plugin. Check that this could be a valid address. This function
559 * is not expected to 'validate' the address in the sense of trying to
560 * connect to it but simply to see if the binary format is technically
561 * legal for establishing a connection.
563 * @param addr pointer to the address, may be modified (slightly)
564 * @param addrlen length of addr
565 * @return GNUNET_OK if this is a plausible address for this peer
566 * and transport, GNUNET_SYSERR if not
568 * TODO: perhaps make everything work with sockaddr_storage, it may
569 * be a cleaner way to handle addresses in UDP
572 udp_check_address (void *cls, void *addr, size_t addrlen)
574 struct Plugin *plugin = cls;
575 char buf[sizeof (struct sockaddr_in6)];
577 struct sockaddr_in *v4;
578 struct sockaddr_in6 *v6;
580 if ((addrlen != sizeof (struct sockaddr_in)) &&
581 (addrlen != sizeof (struct sockaddr_in6)))
584 return GNUNET_SYSERR;
586 memcpy (buf, addr, sizeof (struct sockaddr_in6));
587 if (addrlen == sizeof (struct sockaddr_in))
589 v4 = (struct sockaddr_in *) buf;
590 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
594 v6 = (struct sockaddr_in6 *) buf;
595 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
598 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
600 "Informing transport service about my address `%s'.\n",
601 GNUNET_a2s (addr, addrlen));
609 * Append our port and forward the result.
612 append_port (void *cls, const char *hostname)
614 struct PrettyPrinterContext *ppc = cls;
617 if (hostname == NULL)
619 ppc->asc (ppc->asc_cls, NULL);
623 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
624 ppc->asc (ppc->asc_cls, ret);
630 * Convert the transports address to a nice, human-readable
634 * @param type name of the transport that generated the address
635 * @param addr one of the addresses of the host, NULL for the last address
636 * the specific address format depends on the transport
637 * @param addrlen length of the address
638 * @param numeric should (IP) addresses be displayed in numeric form?
639 * @param timeout after how long should we give up?
640 * @param asc function to call on each string
641 * @param asc_cls closure for asc
644 udp_plugin_address_pretty_printer (void *cls,
649 struct GNUNET_TIME_Relative timeout,
650 GNUNET_TRANSPORT_AddressStringCallback asc,
653 struct Plugin *plugin = cls;
654 const struct sockaddr_in *v4;
655 const struct sockaddr_in6 *v6;
656 struct PrettyPrinterContext *ppc;
658 if ((addrlen != sizeof (struct sockaddr_in)) &&
659 (addrlen != sizeof (struct sockaddr_in6)))
661 /* invalid address */
666 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
668 ppc->asc_cls = asc_cls;
669 if (addrlen == sizeof (struct sockaddr_in))
671 v4 = (const struct sockaddr_in *) addr;
672 ppc->port = ntohs (v4->sin_port);
676 v6 = (const struct sockaddr_in6 *) addr;
677 ppc->port = ntohs (v6->sin6_port);
680 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
684 !numeric, timeout, &append_port, ppc);
688 * Set a quota for receiving data from the given peer; this is a
689 * per-transport limit. This call has no meaning for UDP, as if
690 * we don't receive data it still comes in. UDP has no friendliness
691 * guarantees, and our buffers will fill at some level.
694 * @param target the peer for whom the quota is given
695 * @param quota_in quota for receiving/sending data in bytes per ms
698 udp_plugin_set_receive_quota (void *cls,
699 const struct GNUNET_PeerIdentity *target,
702 return; /* Do nothing */
706 * The exported method. Makes the core api available via a global and
707 * returns the udp transport API.
710 libgnunet_plugin_transport_udp_init (void *cls)
712 unsigned long long mtu;
714 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
715 struct GNUNET_TRANSPORT_PluginFunctions *api;
716 struct Plugin *plugin;
717 struct GNUNET_SERVICE_Context *service;
718 unsigned long long aport;
719 unsigned long long bport;
721 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
724 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
725 ("Failed to start service for `%s' transport plugin.\n"),
731 GNUNET_CONFIGURATION_get_value_number (env->cfg,
737 GNUNET_CONFIGURATION_get_value_number (env->cfg,
740 &aport)) && (aport > 65535)))
742 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
745 ("Require valid port number for service `%s' in configuration!\n"),
747 GNUNET_SERVICE_stop (service);
755 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
757 _("MTU %llu for `%s' is probably too low!\n"), mtu,
760 plugin = GNUNET_malloc (sizeof (struct Plugin));
761 plugin->open_port = bport;
762 plugin->adv_port = aport;
764 plugin->statistics = NULL;
765 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
768 api->send = &udp_plugin_send;
769 api->disconnect = &udp_disconnect;
770 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
771 api->set_receive_quota = &udp_plugin_set_receive_quota;
772 api->check_address = &udp_check_address;
774 plugin->service = service;
776 /* FIXME: do the two calls below periodically again and
777 not just once (since the info we get might change...) */
778 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
779 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
782 HOSTNAME_RESOLVE_TIMEOUT,
783 &process_hostname_ips,
786 udp_sock = udp_transport_server_start (plugin);
788 GNUNET_assert (udp_sock != NULL);
794 libgnunet_plugin_transport_udp_done (void *cls)
796 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
797 struct Plugin *plugin = api->cls;
799 udp_transport_server_stop (plugin);
800 if (NULL != hostname_dns)
802 GNUNET_RESOLVER_request_cancel (hostname_dns);
805 GNUNET_SERVICE_stop (plugin->service);
807 GNUNET_NETWORK_fdset_destroy (plugin->rs);
808 GNUNET_free (plugin);
813 /* end of plugin_transport_udp.c */