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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_ATS_Network_Type 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,
137 const struct GNUNET_MessageHeader *message)
139 struct Plugin *plugin = cls;
140 struct MstContext *mc = client;
141 struct GNUNET_HELLO_Address *address;
142 const struct GNUNET_MessageHeader *hello;
143 const struct UDP_Beacon_Message *msg;
145 msg = (const struct UDP_Beacon_Message *) message;
147 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
148 ntohs (msg->header.type))
150 LOG (GNUNET_ERROR_TYPE_DEBUG,
151 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
152 ntohs (msg->header.size),
153 GNUNET_i2s (&msg->sender),
154 udp_address_to_string (NULL,
157 hello = (struct GNUNET_MessageHeader *) &msg[1];
158 address = GNUNET_HELLO_address_allocate (&msg->sender,
162 GNUNET_HELLO_ADDRESS_INFO_NONE);
163 plugin->env->receive (plugin->env->cls,
167 GNUNET_HELLO_address_free (address);
168 GNUNET_STATISTICS_update (plugin->env->stats,
169 _("# Multicast HELLO beacons received via UDP"),
176 * We received a broadcast message. Process it and all subsequent
177 * messages in the same packet.
179 * @param plugin the UDP plugin
180 * @param buf the buffer with the message(s)
181 * @param size number of bytes in @a buf
182 * @param udp_addr address of the sender
183 * @param udp_addr_len number of bytes in @a udp_addr
184 * @param network_type network type of the sender's address
187 udp_broadcast_receive (struct Plugin *plugin,
190 const union UdpAddress *udp_addr,
192 enum GNUNET_ATS_Network_Type network_type)
194 struct MstContext mc;
196 mc.udp_addr = udp_addr;
197 mc.udp_addr_len = udp_addr_len;
198 mc.ats_address_network_type = network_type;
199 GNUNET_SERVER_mst_receive (plugin->broadcast_mst,
208 prepare_beacon (struct Plugin *plugin,
209 struct UDP_Beacon_Message *msg)
214 const struct GNUNET_MessageHeader *hello;
215 hello = plugin->env->get_our_hello ();
218 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
219 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
221 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
222 (msg_size > (UDP_MTU)))
225 msg->sender = *(plugin->env->my_identity);
226 msg->header.size = htons (msg_size);
227 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
228 GNUNET_memcpy (&msg[1], hello, hello_size);
234 udp_ipv4_broadcast_send (void *cls)
236 struct BroadcastAddress *baddr = cls;
237 struct Plugin *plugin = baddr->plugin;
240 char buf[65536] GNUNET_ALIGN;
242 baddr->broadcast_task = NULL;
244 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
247 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
249 addr->sin_port = htons (plugin->port);
250 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
251 (const struct sockaddr *) addr,
253 if (sent == GNUNET_SYSERR)
255 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
257 /* "Network unreachable" or "Network down"
259 * This indicates that we just do not have network connectivity
261 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
262 "Network connectivity is down, cannot send beacon!\n");
265 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
269 LOG (GNUNET_ERROR_TYPE_DEBUG,
270 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
271 GNUNET_a2s (baddr->addr, baddr->addrlen));
279 if (NULL != baddr->cryogenic_fd)
281 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
282 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
284 if (ioctl(baddr->cryogenic_fd->fd,
285 PM_SET_DELAY_AND_TIMEOUT,
286 &baddr->cryogenic_times) < 0)
288 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
289 baddr->broadcast_task =
290 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
291 &udp_ipv4_broadcast_send, baddr);
294 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
296 &udp_ipv4_broadcast_send,
302 baddr->broadcast_task =
303 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
304 &udp_ipv4_broadcast_send, baddr);
309 udp_ipv6_broadcast_send (void *cls)
311 struct BroadcastAddress *baddr = cls;
312 struct Plugin *plugin = baddr->plugin;
315 char buf[65536] GNUNET_ALIGN;
316 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
318 baddr->broadcast_task = NULL;
320 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
321 /* Note: unclear if this actually works to limit the multicast to
322 the specified interface as we're not (necessarily) using a
323 link-local multicast group and the kernel suggests that the
324 scope ID is only respected for link-local addresses; however,
325 if the scope ID is ignored, the kernel should just multicast
326 on ALL interfaces, which is merely slightly less efficient;
327 in that case, we might want to revert to only doing this
328 once, and not per interface (hard to test...) */
329 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
330 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
331 (const struct sockaddr *)
332 &plugin->ipv6_multicast_address,
333 sizeof (struct sockaddr_in6));
334 plugin->ipv6_multicast_address.sin6_scope_id = 0;
335 if (sent == GNUNET_SYSERR)
337 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
339 /* "Network unreachable" or "Network down"
341 * This indicates that this system is IPv6 enabled, but does not
342 * have a valid global IPv6 address assigned
344 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
345 "Network connectivity is down, cannot send beacon!\n");
348 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
352 LOG (GNUNET_ERROR_TYPE_DEBUG,
353 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
355 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
356 sizeof (struct sockaddr_in6)));
362 if (NULL != baddr->cryogenic_fd)
364 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
365 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
367 if (ioctl(baddr->cryogenic_fd->fd,
368 PM_SET_DELAY_AND_TIMEOUT,
369 &baddr->cryogenic_times) < 0)
371 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
372 baddr->broadcast_task =
373 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
374 &udp_ipv6_broadcast_send, baddr);
377 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
379 &udp_ipv6_broadcast_send,
384 baddr->broadcast_task =
385 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
386 &udp_ipv6_broadcast_send, baddr);
391 * Callback function invoked for each interface found.
393 * @param cls closure with the `struct Plugin`
394 * @param name name of the interface (can be NULL for unknown)
395 * @param isDefault is this presumably the default interface
396 * @param addr address of this interface (can be NULL for unknown or unassigned)
397 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
398 * @param netmask the network mask (can be NULL for unknown or unassigned)
399 * @param addrlen length of the address
400 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
403 iface_proc (void *cls,
406 const struct sockaddr *addr,
407 const struct sockaddr *broadcast_addr,
408 const struct sockaddr *netmask, socklen_t addrlen)
410 struct Plugin *plugin = cls;
411 struct BroadcastAddress *ba;
412 enum GNUNET_ATS_Network_Type network;
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "address %s for interface %s %p\n ",
418 GNUNET_a2s (addr, addrlen), name, addr);
419 if (NULL == broadcast_addr)
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "broadcast address %s for interface %s %p\n ",
423 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
425 GNUNET_a2s (netmask, addrlen), name, netmask);
427 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
428 if (GNUNET_ATS_NET_LOOPBACK == network)
430 /* Broadcasting on loopback does not make sense */
434 ba = GNUNET_new (struct BroadcastAddress);
436 ba->addr = GNUNET_malloc (addrlen);
437 GNUNET_memcpy (ba->addr, broadcast_addr, addrlen);
438 ba->addrlen = addrlen;
440 if ( (GNUNET_YES == plugin->enable_ipv4) &&
441 (NULL != plugin->sockv4) &&
442 (addrlen == sizeof (struct sockaddr_in)) )
446 * setup Cryogenic FD for ipv4 broadcasting
450 GNUNET_asprintf (&filename,
453 if (0 == ACCESS (name, R_OK))
456 GNUNET_DISK_file_open (filename,
457 GNUNET_DISK_OPEN_WRITE,
458 GNUNET_DISK_PERM_NONE);
460 GNUNET_free (filename);
463 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
465 if ((GNUNET_YES == plugin->enable_ipv6) &&
466 (NULL != plugin->sockv6) &&
467 (addrlen == sizeof (struct sockaddr_in6)))
469 /* Create IPv6 multicast request */
470 struct ipv6_mreq multicastRequest;
471 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
473 multicastRequest.ipv6mr_multiaddr =
474 plugin->ipv6_multicast_address.sin6_addr;
475 /* http://tools.ietf.org/html/rfc2553#section-5.2:
479 * Join a multicast group on a specified local interface. If the
480 * interface index is specified as 0, the kernel chooses the local
481 * interface. For example, some kernels look up the multicast
482 * group in the normal IPv6 routing table and using the resulting
483 * interface; we do this for each interface, so no need to use
486 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
488 /* Join the multicast group */
490 GNUNET_NETWORK_socket_setsockopt
491 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
492 &multicastRequest, sizeof (multicastRequest)))
494 LOG (GNUNET_ERROR_TYPE_WARNING,
495 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
501 * setup Cryogenic FD for ipv6 broadcasting
505 GNUNET_asprintf (&filename,
508 if (0 == ACCESS (name, R_OK))
511 GNUNET_DISK_file_open (filename,
512 GNUNET_DISK_OPEN_WRITE,
513 GNUNET_DISK_PERM_NONE);
515 GNUNET_free (filename);
518 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
521 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
522 plugin->broadcast_tail, ba);
528 * Setup broadcasting subsystem.
531 * @param server_addrv6
532 * @param server_addrv4
535 setup_broadcast (struct Plugin *plugin,
536 struct sockaddr_in6 *server_addrv6,
537 struct sockaddr_in *server_addrv4)
540 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
544 LOG (GNUNET_ERROR_TYPE_WARNING,
545 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
549 /* always create tokenizers */
550 plugin->broadcast_mst =
551 GNUNET_SERVER_mst_create (&broadcast_mst_cb, plugin);
553 if (GNUNET_YES != plugin->enable_broadcasting)
554 return; /* We do not send, just receive */
556 /* create IPv4 broadcast socket */
557 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
561 if (GNUNET_NETWORK_socket_setsockopt
562 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
563 sizeof (int)) != GNUNET_OK)
565 LOG (GNUNET_ERROR_TYPE_WARNING,
566 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
567 ntohs (server_addrv4->sin_port));
570 /* create IPv6 multicast socket */
571 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
573 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
575 inet_pton (AF_INET6, "FF05::13B",
576 &plugin->ipv6_multicast_address.sin6_addr));
577 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
578 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
580 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
585 * Stop broadcasting subsystem.
590 stop_broadcast (struct Plugin *plugin)
592 if (GNUNET_YES == plugin->enable_broadcasting)
594 /* Disable broadcasting */
595 while (plugin->broadcast_head != NULL)
597 struct BroadcastAddress *p = plugin->broadcast_head;
599 if (p->broadcast_task != NULL)
601 GNUNET_SCHEDULER_cancel (p->broadcast_task);
602 p->broadcast_task = NULL;
604 if ((GNUNET_YES == plugin->enable_ipv6) &&
605 (NULL != plugin->sockv6) &&
606 (p->addrlen == sizeof (struct sockaddr_in6)))
608 /* Create IPv6 multicast request */
609 struct ipv6_mreq multicastRequest;
610 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
612 multicastRequest.ipv6mr_multiaddr =
613 plugin->ipv6_multicast_address.sin6_addr;
614 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
616 /* Leave the multicast group */
618 GNUNET_NETWORK_socket_setsockopt
619 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
620 &multicastRequest, sizeof (multicastRequest)))
622 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
626 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
631 GNUNET_DISK_file_close(p->cryogenic_fd);
633 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
634 plugin->broadcast_tail, p);
635 GNUNET_free (p->addr);
641 if (NULL != plugin->broadcast_mst)
643 GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
644 plugin->broadcast_mst = NULL;
648 /* end of plugin_transport_udp_broadcasting.c */