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 * 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 * Message-Packet header.
61 * size of the message, in bytes, including this header.
63 struct GNUNET_MessageHeader header;
66 * What is the identity of the sender (GNUNET_hash of public key)
68 struct GNUNET_PeerIdentity sender;
72 /* Forward definition */
75 struct PrettyPrinterContext
77 GNUNET_TRANSPORT_AddressStringCallback asc;
83 * Encapsulation of all of the state of the plugin.
90 struct GNUNET_TRANSPORT_PluginEnvironment *env;
93 * Handle for the statistics service.
95 struct GNUNET_STATISTICS_Handle *statistics;
98 * Handle to the network service.
100 struct GNUNET_SERVICE_Context *service;
103 * ID of task used to update our addresses when one expires.
105 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
110 GNUNET_SCHEDULER_TaskIdentifier select_task;
113 * Port that we are actually listening on.
118 * Port that the user said we would have visible to the
126 struct GNUNET_NETWORK_FDSet *rs;
130 /* *********** globals ************* */
133 * the socket that we transmit all data with
135 static struct GNUNET_NETWORK_Handle *udp_sock;
138 * Disconnect from a remote node.
140 * @param tsession the session that is closed
141 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
144 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
150 * Shutdown the server process (stop receiving inbound traffic). Maybe
154 udp_transport_server_stop (void *cls)
156 struct Plugin *plugin = cls;
157 GNUNET_assert (udp_sock != NULL);
158 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
160 GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
161 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
164 GNUNET_NETWORK_socket_close (udp_sock);
170 * Function that can be used by the transport service to transmit
171 * a message using the plugin.
174 * @param target who should receive this message (ignored by UDP)
175 * @param msg the message to transmit
176 * @param priority how important is the message (ignored by UDP)
177 * @param timeout when should we time out (give up) if we can not transmit?
178 * @param addr the addr to send the message to, needs to be a sockaddr for us
179 * @param addrlen the len of addr
180 * @param force_address not used, we had better have an address to send to
181 * because we are stateless!!
182 * @param cont continuation to call once the message has
183 * been transmitted (or if the transport is ready
184 * for the next transmission call; or if the
185 * peer disconnected...)
186 * @param cont_cls closure for cont
188 * @return the number of bytes written
192 udp_plugin_send (void *cls,
193 const struct GNUNET_PeerIdentity *target,
194 const struct GNUNET_MessageHeader *msg,
195 unsigned int priority,
196 struct GNUNET_TIME_Relative timeout,
200 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
202 struct Plugin *plugin = cls;
203 struct UDPMessage *message;
207 GNUNET_assert(udp_sock != NULL);
208 GNUNET_assert(addr != NULL);
210 /* Build the message to be sent */
211 message = GNUNET_malloc (sizeof (struct UDPMessage) + ntohs (msg->size));
212 ssize = sizeof (struct UDPMessage) + ntohs (msg->size);
215 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
216 ("In udp_send, ssize is %d\n"), ssize);
218 message->header.size = htons (ssize);
219 message->header.type = htons (0);
220 memcpy (&message->sender, plugin->env->my_identity,
221 sizeof (struct GNUNET_PeerIdentity));
222 memcpy (&message[1], msg, ntohs (msg->size));
224 /* Actually send the message */
226 GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
232 if (sent == GNUNET_SYSERR)
233 cont (cont_cls, target, GNUNET_SYSERR);
235 cont (cont_cls, target, GNUNET_OK);
237 GNUNET_free (message);
243 * @param cls the plugin handle
244 * @param tc the scheduling context (for rescheduling this function again)
246 * We have been notified that our writeset has something to read. Presumably
247 * select has been called already, so we can go ahead and start reading from
248 * the socket immediately. Then we check if there is more to be read by
249 * calling select ourselves while there is stuff on the wire. Then reschedule
250 * this function to be called again once more is available.
254 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
256 struct Plugin *plugin = cls;
257 struct GNUNET_TIME_Relative timeout =
258 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
260 struct UDPMessage *msg;
261 const struct GNUNET_MessageHeader *hdr;
262 struct GNUNET_PeerIdentity *sender;
265 struct sockaddr_storage addr;
270 buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
273 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
274 ("we expect to read %u bytes\n"), buflen);
277 if (buflen == GNUNET_NO)
280 buf = GNUNET_malloc (buflen);
281 fromlen = sizeof (addr);
283 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
284 ("src_addr_len is %u\n"), fromlen);
286 memset (&addr, 0, fromlen);
288 GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
289 (struct sockaddr *) &addr, &fromlen);
292 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
293 ("socket_recv returned %u, src_addr_len is %u\n"), ret,
302 msg = (struct UDPMessage *) buf;
305 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
306 ("header reports message size of %d\n"),
307 ntohs (msg->header.size));
309 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
310 ("header reports message type of %d\n"),
311 ntohs (msg->header.type));
313 if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
316 GNUNET_NETWORK_fdset_zero (plugin->rs);
317 GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
320 hdr = (const struct GNUNET_MessageHeader *) &msg[1];
321 sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
322 memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
325 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
326 ("msg reports message size of %d\n"),
329 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
330 ("msg reports message type of %d\n"),
333 plugin->env->receive (plugin->env->cls,
334 &msg->header, sender, UDP_DIRECT_DISTANCE, (char *)&addr, fromlen);
336 GNUNET_free (sender);
340 while (GNUNET_NETWORK_socket_select (plugin->rs,
344 && GNUNET_NETWORK_fdset_isset (plugin->rs, udp_sock));
346 plugin->select_task =
347 GNUNET_SCHEDULER_add_select (plugin->env->sched,
348 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
349 GNUNET_SCHEDULER_NO_TASK,
350 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
351 NULL, &udp_plugin_select, plugin);
356 * Create a UDP socket. If possible, use IPv6, otherwise
359 static struct GNUNET_NETWORK_Handle *
360 udp_transport_server_start (void *cls)
362 struct Plugin *plugin = cls;
363 struct GNUNET_NETWORK_Handle *desc;
364 struct sockaddr_in serverAddrv4;
365 struct sockaddr_in6 serverAddrv6;
366 struct sockaddr *serverAddr;
371 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
374 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
377 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
378 #if HAVE_SOCKADDR_IN_SIN_LEN
379 serverAddrv6.sin6_len = sizeof (serverAddrv6);
381 serverAddrv6.sin6_family = AF_INET6;
382 serverAddrv6.sin6_addr = in6addr_any;
383 serverAddrv6.sin6_port = htons (plugin->open_port);
384 addrlen = sizeof (serverAddrv6);
385 serverAddr = (struct sockaddr *) &serverAddrv6;
390 desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
393 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
398 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
399 #if HAVE_SOCKADDR_IN_SIN_LEN
400 serverAddrv4.sin_len = sizeof (serverAddrv4);
402 serverAddrv4.sin_family = AF_INET;
403 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
404 serverAddrv4.sin_port = htons (plugin->open_port);
405 addrlen = sizeof (serverAddrv4);
406 serverAddr = (struct sockaddr *) &serverAddrv4;
412 GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
416 plugin->rs = GNUNET_NETWORK_fdset_create ();
418 GNUNET_NETWORK_fdset_zero (plugin->rs);
419 GNUNET_NETWORK_fdset_set (plugin->rs, desc);
421 plugin->select_task =
422 GNUNET_SCHEDULER_add_select (plugin->env->sched,
423 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
424 GNUNET_SCHEDULER_NO_TASK,
425 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
426 NULL, &udp_plugin_select, plugin);
433 * Check if the given port is plausible (must be either
434 * our listen port or our advertised port). If it is
435 * neither, we return one of these two ports at random.
437 * @return either in_port or a more plausible port
440 check_port (struct Plugin *plugin, uint16_t in_port)
442 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
444 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
446 ? plugin->open_port : plugin->adv_port;
451 * Another peer has suggested an address for this peer and transport
452 * plugin. Check that this could be a valid address. This function
453 * is not expected to 'validate' the address in the sense of trying to
454 * connect to it but simply to see if the binary format is technically
455 * legal for establishing a connection.
457 * @param addr pointer to the address, may be modified (slightly)
458 * @param addrlen length of addr
459 * @return GNUNET_OK if this is a plausible address for this peer
460 * and transport, GNUNET_SYSERR if not
462 * TODO: perhaps make everything work with sockaddr_storage, it may
463 * be a cleaner way to handle addresses in UDP
466 udp_check_address (void *cls, void *addr, size_t addrlen)
468 struct Plugin *plugin = cls;
469 char buf[sizeof (struct sockaddr_in6)];
471 struct sockaddr_in *v4;
472 struct sockaddr_in6 *v6;
474 if ((addrlen != sizeof (struct sockaddr_in)) &&
475 (addrlen != sizeof (struct sockaddr_in6)))
478 return GNUNET_SYSERR;
480 memcpy (buf, addr, sizeof (struct sockaddr_in6));
481 if (addrlen == sizeof (struct sockaddr_in))
483 v4 = (struct sockaddr_in *) buf;
484 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
488 v6 = (struct sockaddr_in6 *) buf;
489 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
492 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
494 "Informing transport service about my address `%s'.\n",
495 GNUNET_a2s (addr, addrlen));
503 * Append our port and forward the result.
506 append_port (void *cls, const char *hostname)
508 struct PrettyPrinterContext *ppc = cls;
511 if (hostname == NULL)
513 ppc->asc (ppc->asc_cls, NULL);
517 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
518 ppc->asc (ppc->asc_cls, ret);
524 * Convert the transports address to a nice, human-readable
528 * @param type name of the transport that generated the address
529 * @param addr one of the addresses of the host, NULL for the last address
530 * the specific address format depends on the transport
531 * @param addrlen length of the address
532 * @param numeric should (IP) addresses be displayed in numeric form?
533 * @param timeout after how long should we give up?
534 * @param asc function to call on each string
535 * @param asc_cls closure for asc
538 udp_plugin_address_pretty_printer (void *cls,
543 struct GNUNET_TIME_Relative timeout,
544 GNUNET_TRANSPORT_AddressStringCallback asc,
547 struct Plugin *plugin = cls;
548 const struct sockaddr_in *v4;
549 const struct sockaddr_in6 *v6;
550 struct PrettyPrinterContext *ppc;
552 if ((addrlen != sizeof (struct sockaddr_in)) &&
553 (addrlen != sizeof (struct sockaddr_in6)))
555 /* invalid address */
560 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
562 ppc->asc_cls = asc_cls;
563 if (addrlen == sizeof (struct sockaddr_in))
565 v4 = (const struct sockaddr_in *) addr;
566 ppc->port = ntohs (v4->sin_port);
570 v6 = (const struct sockaddr_in6 *) addr;
571 ppc->port = ntohs (v6->sin6_port);
574 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
578 !numeric, timeout, &append_port, ppc);
582 * Set a quota for receiving data from the given peer; this is a
583 * per-transport limit. This call has no meaning for UDP, as if
584 * we don't receive data it still comes in. UDP has no friendliness
585 * guarantees, and our buffers will fill at some level.
588 * @param target the peer for whom the quota is given
589 * @param quota_in quota for receiving/sending data in bytes per ms
592 udp_plugin_set_receive_quota (void *cls,
593 const struct GNUNET_PeerIdentity *target,
596 return; /* Do nothing */
600 * The exported method. Makes the core api available via a global and
601 * returns the udp transport API.
604 libgnunet_plugin_transport_udp_init (void *cls)
606 unsigned long long mtu;
608 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
609 struct GNUNET_TRANSPORT_PluginFunctions *api;
610 struct Plugin *plugin;
611 struct GNUNET_SERVICE_Context *service;
612 unsigned long long aport;
613 unsigned long long bport;
615 service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
618 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
619 ("Failed to start service for `%s' transport plugin.\n"),
625 GNUNET_CONFIGURATION_get_value_number (env->cfg,
631 GNUNET_CONFIGURATION_get_value_number (env->cfg,
634 &aport)) && (aport > 65535)))
636 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
639 ("Require valid port number for service `%s' in configuration!\n"),
641 GNUNET_SERVICE_stop (service);
649 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
651 _("MTU %llu for `%s' is probably too low!\n"), mtu,
654 plugin = GNUNET_malloc (sizeof (struct Plugin));
655 plugin->open_port = bport;
656 plugin->adv_port = aport;
658 plugin->statistics = NULL;
659 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
662 api->send = &udp_plugin_send;
663 api->disconnect = &udp_disconnect;
664 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
665 api->set_receive_quota = &udp_plugin_set_receive_quota;
666 api->check_address = &udp_check_address;
668 plugin->service = service;
670 udp_sock = udp_transport_server_start (plugin);
672 GNUNET_assert (udp_sock != NULL);
678 libgnunet_plugin_transport_udp_done (void *cls)
680 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
681 struct Plugin *plugin = api->cls;
683 udp_transport_server_stop (plugin);
684 if (NULL != hostname_dns)
686 GNUNET_RESOLVER_request_cancel (hostname_dns);
689 GNUNET_SERVICE_stop (plugin->service);
691 GNUNET_NETWORK_fdset_destroy (plugin->rs);
692 GNUNET_free (plugin);
697 /* end of plugin_transport_udp.c */