2 This file is part of GNUnet
3 Copyright (C) 2010, 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/plugin_transport_udp_broadcasting.c
23 * @brief Neighbour discovery with UDP
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "plugin_transport_udp.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fragmentation_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
43 /* *********** Cryogenic ********** */
48 #include <sys/ioctl.h>
49 #include <sys/select.h>
53 #define PM_SET_DELAY_AND_TIMEOUT _IOW(PM_MAGIC, 1, struct pm_times)
56 unsigned long delay_msecs;
57 unsigned long timeout_msecs;
60 /************************************/
63 struct UDP_Beacon_Message
68 struct GNUNET_MessageHeader header;
71 * What is the identity of the sender
73 struct GNUNET_PeerIdentity sender;
77 struct BroadcastAddress
79 struct BroadcastAddress *next;
81 struct BroadcastAddress *prev;
84 * ID of select broadcast task
86 struct GNUNET_SCHEDULER_Task * broadcast_task;
88 struct Plugin *plugin;
90 struct sockaddr *addr;
98 struct GNUNET_DISK_FileHandle *cryogenic_fd;
101 * Time out for cryogenic.
103 struct pm_times cryogenic_times;
109 * Client-specific context for #broadcast_mst_cb().
113 struct Plugin *plugin;
115 const union UdpAddress *udp_addr;
122 enum GNUNET_NetworkType ats_address_network_type;
127 * Parse broadcast message received.
129 * @param cls the `struct Plugin`
130 * @param client the `struct MstContext` with sender address
131 * @param message the message we received
132 * @return #GNUNET_OK (always)
135 broadcast_mst_cb (void *cls,
136 const struct GNUNET_MessageHeader *message)
138 struct MstContext *mc = cls;
139 struct Plugin *plugin = mc->plugin;
140 struct GNUNET_HELLO_Address *address;
141 const struct GNUNET_MessageHeader *hello;
142 const struct UDP_Beacon_Message *msg;
144 msg = (const struct UDP_Beacon_Message *) message;
146 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
147 ntohs (msg->header.type))
149 LOG (GNUNET_ERROR_TYPE_DEBUG,
150 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
151 ntohs (msg->header.size),
152 GNUNET_i2s (&msg->sender),
153 udp_address_to_string (NULL,
156 hello = (struct GNUNET_MessageHeader *) &msg[1];
157 address = GNUNET_HELLO_address_allocate (&msg->sender,
161 GNUNET_HELLO_ADDRESS_INFO_NONE);
162 plugin->env->receive (plugin->env->cls,
166 GNUNET_HELLO_address_free (address);
167 GNUNET_STATISTICS_update (plugin->env->stats,
168 _("# Multicast HELLO beacons received via UDP"),
175 * We received a broadcast message. Process it and all subsequent
176 * messages in the same packet.
178 * @param plugin the UDP plugin
179 * @param buf the buffer with the message(s)
180 * @param size number of bytes in @a buf
181 * @param udp_addr address of the sender
182 * @param udp_addr_len number of bytes in @a udp_addr
183 * @param network_type network type of the sender's address
186 udp_broadcast_receive (struct Plugin *plugin,
189 const union UdpAddress *udp_addr,
191 enum GNUNET_NetworkType network_type)
193 struct GNUNET_MessageStreamTokenizer *broadcast_mst;
194 struct MstContext mc;
196 broadcast_mst = GNUNET_MST_create (&broadcast_mst_cb,
199 mc.udp_addr = udp_addr;
200 mc.udp_addr_len = udp_addr_len;
201 mc.ats_address_network_type = network_type;
202 GNUNET_MST_from_buffer (broadcast_mst,
206 GNUNET_MST_destroy (broadcast_mst);
211 prepare_beacon (struct Plugin *plugin,
212 struct UDP_Beacon_Message *msg)
217 const struct GNUNET_MessageHeader *hello;
218 hello = plugin->env->get_our_hello ();
221 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
222 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
224 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
225 (msg_size > (UDP_MTU)))
228 msg->sender = *(plugin->env->my_identity);
229 msg->header.size = htons (msg_size);
230 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
231 GNUNET_memcpy (&msg[1], hello, hello_size);
237 udp_ipv4_broadcast_send (void *cls)
239 struct BroadcastAddress *baddr = cls;
240 struct Plugin *plugin = baddr->plugin;
243 char buf[65536] GNUNET_ALIGN;
245 baddr->broadcast_task = NULL;
247 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
250 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
252 addr->sin_port = htons (plugin->port);
253 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
254 (const struct sockaddr *) addr,
256 if (sent == GNUNET_SYSERR)
258 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
260 /* "Network unreachable" or "Network down"
262 * This indicates that we just do not have network connectivity
264 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
265 "Network connectivity is down, cannot send beacon!\n");
268 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
272 LOG (GNUNET_ERROR_TYPE_DEBUG,
273 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
274 GNUNET_a2s (baddr->addr, baddr->addrlen));
282 if (NULL != baddr->cryogenic_fd)
284 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
285 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
287 if (ioctl(baddr->cryogenic_fd->fd,
288 PM_SET_DELAY_AND_TIMEOUT,
289 &baddr->cryogenic_times) < 0)
291 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
292 baddr->broadcast_task =
293 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
294 &udp_ipv4_broadcast_send, baddr);
297 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
299 &udp_ipv4_broadcast_send,
305 baddr->broadcast_task =
306 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
307 &udp_ipv4_broadcast_send, baddr);
312 udp_ipv6_broadcast_send (void *cls)
314 struct BroadcastAddress *baddr = cls;
315 struct Plugin *plugin = baddr->plugin;
318 char buf[65536] GNUNET_ALIGN;
319 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
321 baddr->broadcast_task = NULL;
323 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
324 /* Note: unclear if this actually works to limit the multicast to
325 the specified interface as we're not (necessarily) using a
326 link-local multicast group and the kernel suggests that the
327 scope ID is only respected for link-local addresses; however,
328 if the scope ID is ignored, the kernel should just multicast
329 on ALL interfaces, which is merely slightly less efficient;
330 in that case, we might want to revert to only doing this
331 once, and not per interface (hard to test...) */
332 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
333 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
334 (const struct sockaddr *)
335 &plugin->ipv6_multicast_address,
336 sizeof (struct sockaddr_in6));
337 plugin->ipv6_multicast_address.sin6_scope_id = 0;
338 if (sent == GNUNET_SYSERR)
340 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
342 /* "Network unreachable" or "Network down"
344 * This indicates that this system is IPv6 enabled, but does not
345 * have a valid global IPv6 address assigned
347 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
348 "Network connectivity is down, cannot send beacon!\n");
351 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
358 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
359 sizeof (struct sockaddr_in6)));
365 if (NULL != baddr->cryogenic_fd)
367 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
368 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
370 if (ioctl(baddr->cryogenic_fd->fd,
371 PM_SET_DELAY_AND_TIMEOUT,
372 &baddr->cryogenic_times) < 0)
374 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
375 baddr->broadcast_task =
376 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
377 &udp_ipv6_broadcast_send, baddr);
380 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
382 &udp_ipv6_broadcast_send,
387 baddr->broadcast_task =
388 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
389 &udp_ipv6_broadcast_send, baddr);
394 * Callback function invoked for each interface found.
396 * @param cls closure with the `struct Plugin`
397 * @param name name of the interface (can be NULL for unknown)
398 * @param isDefault is this presumably the default interface
399 * @param addr address of this interface (can be NULL for unknown or unassigned)
400 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
401 * @param netmask the network mask (can be NULL for unknown or unassigned)
402 * @param addrlen length of the address
403 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
406 iface_proc (void *cls,
409 const struct sockaddr *addr,
410 const struct sockaddr *broadcast_addr,
411 const struct sockaddr *netmask, socklen_t addrlen)
413 struct Plugin *plugin = cls;
414 struct BroadcastAddress *ba;
415 enum GNUNET_NetworkType network;
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "address %s for interface %s %p\n ",
421 GNUNET_a2s (addr, addrlen), name, addr);
422 if (NULL == broadcast_addr)
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "broadcast address %s for interface %s %p\n ",
426 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
428 GNUNET_a2s (netmask, addrlen), name, netmask);
430 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
431 if (GNUNET_NT_LOOPBACK == network)
433 /* Broadcasting on loopback does not make sense */
437 ba = GNUNET_new (struct BroadcastAddress);
439 ba->addr = GNUNET_malloc (addrlen);
440 GNUNET_memcpy (ba->addr, broadcast_addr, addrlen);
441 ba->addrlen = addrlen;
443 if ( (GNUNET_YES == plugin->enable_ipv4) &&
444 (NULL != plugin->sockv4) &&
445 (addrlen == sizeof (struct sockaddr_in)) )
449 * setup Cryogenic FD for ipv4 broadcasting
453 GNUNET_asprintf (&filename,
456 if (0 == ACCESS (name, R_OK))
459 GNUNET_DISK_file_open (filename,
460 GNUNET_DISK_OPEN_WRITE,
461 GNUNET_DISK_PERM_NONE);
463 GNUNET_free (filename);
466 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
468 if ((GNUNET_YES == plugin->enable_ipv6) &&
469 (NULL != plugin->sockv6) &&
470 (addrlen == sizeof (struct sockaddr_in6)))
472 /* Create IPv6 multicast request */
473 struct ipv6_mreq multicastRequest;
474 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
476 multicastRequest.ipv6mr_multiaddr =
477 plugin->ipv6_multicast_address.sin6_addr;
478 /* http://tools.ietf.org/html/rfc2553#section-5.2:
482 * Join a multicast group on a specified local interface. If the
483 * interface index is specified as 0, the kernel chooses the local
484 * interface. For example, some kernels look up the multicast
485 * group in the normal IPv6 routing table and using the resulting
486 * interface; we do this for each interface, so no need to use
489 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
491 /* Join the multicast group */
493 GNUNET_NETWORK_socket_setsockopt
494 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
495 &multicastRequest, sizeof (multicastRequest)))
497 LOG (GNUNET_ERROR_TYPE_WARNING,
498 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
504 * setup Cryogenic FD for ipv6 broadcasting
508 GNUNET_asprintf (&filename,
511 if (0 == ACCESS (name, R_OK))
514 GNUNET_DISK_file_open (filename,
515 GNUNET_DISK_OPEN_WRITE,
516 GNUNET_DISK_PERM_NONE);
518 GNUNET_free (filename);
521 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
524 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
525 plugin->broadcast_tail, ba);
531 * Setup broadcasting subsystem.
534 * @param server_addrv6
535 * @param server_addrv4
538 setup_broadcast (struct Plugin *plugin,
539 struct sockaddr_in6 *server_addrv6,
540 struct sockaddr_in *server_addrv4)
543 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
547 LOG (GNUNET_ERROR_TYPE_WARNING,
548 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
552 if (GNUNET_YES != plugin->enable_broadcasting)
553 return; /* We do not send, just receive */
555 /* create IPv4 broadcast socket */
556 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
560 if (GNUNET_NETWORK_socket_setsockopt
561 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
562 sizeof (int)) != GNUNET_OK)
564 LOG (GNUNET_ERROR_TYPE_WARNING,
565 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
566 ntohs (server_addrv4->sin_port));
569 /* create IPv6 multicast socket */
570 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
572 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
574 inet_pton (AF_INET6, "FF05::13B",
575 &plugin->ipv6_multicast_address.sin6_addr));
576 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
577 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
579 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
584 * Stop broadcasting subsystem.
589 stop_broadcast (struct Plugin *plugin)
591 if (GNUNET_YES == plugin->enable_broadcasting)
593 /* Disable broadcasting */
594 while (plugin->broadcast_head != NULL)
596 struct BroadcastAddress *p = plugin->broadcast_head;
598 if (p->broadcast_task != NULL)
600 GNUNET_SCHEDULER_cancel (p->broadcast_task);
601 p->broadcast_task = NULL;
603 if ((GNUNET_YES == plugin->enable_ipv6) &&
604 (NULL != plugin->sockv6) &&
605 (p->addrlen == sizeof (struct sockaddr_in6)))
607 /* Create IPv6 multicast request */
608 struct ipv6_mreq multicastRequest;
609 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
611 multicastRequest.ipv6mr_multiaddr =
612 plugin->ipv6_multicast_address.sin6_addr;
613 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
615 /* Leave the multicast group */
617 GNUNET_NETWORK_socket_setsockopt
618 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
619 &multicastRequest, sizeof (multicastRequest)))
621 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
625 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
630 GNUNET_DISK_file_close(p->cryogenic_fd);
632 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
633 plugin->broadcast_tail, p);
634 GNUNET_free (p->addr);
640 /* end of plugin_transport_udp_broadcasting.c */