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_nat_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
44 /* *********** Cryogenic ********** */
49 #include <sys/ioctl.h>
50 #include <sys/select.h>
54 #define PM_SET_DELAY_AND_TIMEOUT _IOW(PM_MAGIC, 1, struct pm_times)
57 unsigned long delay_msecs;
58 unsigned long timeout_msecs;
61 /************************************/
64 struct UDP_Beacon_Message
69 struct GNUNET_MessageHeader header;
72 * What is the identity of the sender
74 struct GNUNET_PeerIdentity sender;
78 struct BroadcastAddress
80 struct BroadcastAddress *next;
82 struct BroadcastAddress *prev;
85 * ID of select broadcast task
87 struct GNUNET_SCHEDULER_Task * broadcast_task;
89 struct Plugin *plugin;
91 struct sockaddr *addr;
99 struct GNUNET_DISK_FileHandle *cryogenic_fd;
102 * Time out for cryogenic.
104 struct pm_times cryogenic_times;
110 * Client-specific context for #broadcast_mst_cb().
114 struct Plugin *plugin;
116 const union UdpAddress *udp_addr;
123 enum GNUNET_ATS_Network_Type ats_address_network_type;
128 * Parse broadcast message received.
130 * @param cls the `struct Plugin`
131 * @param client the `struct MstContext` with sender address
132 * @param message the message we received
133 * @return #GNUNET_OK (always)
136 broadcast_mst_cb (void *cls,
138 const struct GNUNET_MessageHeader *message)
140 struct Plugin *plugin = cls;
141 struct MstContext *mc = client;
142 struct GNUNET_HELLO_Address *address;
143 const struct GNUNET_MessageHeader *hello;
144 const struct UDP_Beacon_Message *msg;
146 msg = (const struct UDP_Beacon_Message *) message;
148 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
149 ntohs (msg->header.type))
151 LOG (GNUNET_ERROR_TYPE_DEBUG,
152 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
153 ntohs (msg->header.size),
154 GNUNET_i2s (&msg->sender),
155 udp_address_to_string (NULL,
158 hello = (struct GNUNET_MessageHeader *) &msg[1];
159 address = GNUNET_HELLO_address_allocate (&msg->sender,
163 GNUNET_HELLO_ADDRESS_INFO_NONE);
164 plugin->env->receive (plugin->env->cls,
168 GNUNET_HELLO_address_free (address);
169 GNUNET_STATISTICS_update (plugin->env->stats,
170 _("# Multicast HELLO beacons received via UDP"),
177 * We received a broadcast message. Process it and all subsequent
178 * messages in the same packet.
180 * @param plugin the UDP plugin
181 * @param buf the buffer with the message(s)
182 * @param size number of bytes in @a buf
183 * @param udp_addr address of the sender
184 * @param udp_addr_len number of bytes in @a udp_addr
185 * @param network_type network type of the sender's address
188 udp_broadcast_receive (struct Plugin *plugin,
191 const union UdpAddress *udp_addr,
193 enum GNUNET_ATS_Network_Type network_type)
195 struct MstContext mc;
197 mc.udp_addr = udp_addr;
198 mc.udp_addr_len = udp_addr_len;
199 mc.ats_address_network_type = network_type;
200 GNUNET_SERVER_mst_receive (plugin->broadcast_mst,
209 prepare_beacon (struct Plugin *plugin,
210 struct UDP_Beacon_Message *msg)
215 const struct GNUNET_MessageHeader *hello;
216 hello = plugin->env->get_our_hello ();
219 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
220 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
222 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
223 (msg_size > (UDP_MTU)))
226 msg->sender = *(plugin->env->my_identity);
227 msg->header.size = htons (msg_size);
228 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
229 GNUNET_memcpy (&msg[1], hello, hello_size);
235 udp_ipv4_broadcast_send (void *cls)
237 struct BroadcastAddress *baddr = cls;
238 struct Plugin *plugin = baddr->plugin;
241 char buf[65536] GNUNET_ALIGN;
243 baddr->broadcast_task = NULL;
245 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
248 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
250 addr->sin_port = htons (plugin->port);
251 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
252 (const struct sockaddr *) addr,
254 if (sent == GNUNET_SYSERR)
256 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
258 /* "Network unreachable" or "Network down"
260 * This indicates that we just do not have network connectivity
262 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
263 "Network connectivity is down, cannot send beacon!\n");
266 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
272 GNUNET_a2s (baddr->addr, baddr->addrlen));
280 if (NULL != baddr->cryogenic_fd)
282 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
283 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
285 if (ioctl(baddr->cryogenic_fd->fd,
286 PM_SET_DELAY_AND_TIMEOUT,
287 &baddr->cryogenic_times) < 0)
289 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
290 baddr->broadcast_task =
291 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
292 &udp_ipv4_broadcast_send, baddr);
295 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
297 &udp_ipv4_broadcast_send,
303 baddr->broadcast_task =
304 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
305 &udp_ipv4_broadcast_send, baddr);
310 udp_ipv6_broadcast_send (void *cls)
312 struct BroadcastAddress *baddr = cls;
313 struct Plugin *plugin = baddr->plugin;
316 char buf[65536] GNUNET_ALIGN;
317 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
319 baddr->broadcast_task = NULL;
321 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
322 /* Note: unclear if this actually works to limit the multicast to
323 the specified interface as we're not (necessarily) using a
324 link-local multicast group and the kernel suggests that the
325 scope ID is only respected for link-local addresses; however,
326 if the scope ID is ignored, the kernel should just multicast
327 on ALL interfaces, which is merely slightly less efficient;
328 in that case, we might want to revert to only doing this
329 once, and not per interface (hard to test...) */
330 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
331 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
332 (const struct sockaddr *)
333 &plugin->ipv6_multicast_address,
334 sizeof (struct sockaddr_in6));
335 plugin->ipv6_multicast_address.sin6_scope_id = 0;
336 if (sent == GNUNET_SYSERR)
338 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
340 /* "Network unreachable" or "Network down"
342 * This indicates that this system is IPv6 enabled, but does not
343 * have a valid global IPv6 address assigned
345 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
346 "Network connectivity is down, cannot send beacon!\n");
349 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
353 LOG (GNUNET_ERROR_TYPE_DEBUG,
354 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
356 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
357 sizeof (struct sockaddr_in6)));
363 if (NULL != baddr->cryogenic_fd)
365 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
366 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
368 if (ioctl(baddr->cryogenic_fd->fd,
369 PM_SET_DELAY_AND_TIMEOUT,
370 &baddr->cryogenic_times) < 0)
372 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
373 baddr->broadcast_task =
374 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
375 &udp_ipv6_broadcast_send, baddr);
378 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
380 // Why does this say ipv4 instead of ipv6 here? Looks like a bug!
381 &udp_ipv4_broadcast_send,
386 baddr->broadcast_task =
387 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
388 &udp_ipv6_broadcast_send, baddr);
393 * Callback function invoked for each interface found.
395 * @param cls closure with the `struct Plugin`
396 * @param name name of the interface (can be NULL for unknown)
397 * @param isDefault is this presumably the default interface
398 * @param addr address of this interface (can be NULL for unknown or unassigned)
399 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
400 * @param netmask the network mask (can be NULL for unknown or unassigned)
401 * @param addrlen length of the address
402 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
405 iface_proc (void *cls,
408 const struct sockaddr *addr,
409 const struct sockaddr *broadcast_addr,
410 const struct sockaddr *netmask, socklen_t addrlen)
412 struct Plugin *plugin = cls;
413 struct BroadcastAddress *ba;
414 enum GNUNET_ATS_Network_Type network;
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "address %s for interface %s %p\n ",
420 GNUNET_a2s (addr, addrlen), name, addr);
421 if (NULL == broadcast_addr)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "broadcast address %s for interface %s %p\n ",
425 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
427 GNUNET_a2s (netmask, addrlen), name, netmask);
429 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
430 if (GNUNET_ATS_NET_LOOPBACK == network)
432 /* Broadcasting on loopback does not make sense */
436 ba = GNUNET_new (struct BroadcastAddress);
438 ba->addr = GNUNET_malloc (addrlen);
439 GNUNET_memcpy (ba->addr, broadcast_addr, addrlen);
440 ba->addrlen = addrlen;
442 if ( (GNUNET_YES == plugin->enable_ipv4) &&
443 (NULL != plugin->sockv4) &&
444 (addrlen == sizeof (struct sockaddr_in)) )
448 * setup Cryogenic FD for ipv4 broadcasting
452 GNUNET_asprintf (&filename,
455 if (0 == ACCESS (name, R_OK))
458 GNUNET_DISK_file_open (filename,
459 GNUNET_DISK_OPEN_WRITE,
460 GNUNET_DISK_PERM_NONE);
462 GNUNET_free (filename);
465 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
467 if ((GNUNET_YES == plugin->enable_ipv6) &&
468 (NULL != plugin->sockv6) &&
469 (addrlen == sizeof (struct sockaddr_in6)))
471 /* Create IPv6 multicast request */
472 struct ipv6_mreq multicastRequest;
473 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
475 multicastRequest.ipv6mr_multiaddr =
476 plugin->ipv6_multicast_address.sin6_addr;
477 /* http://tools.ietf.org/html/rfc2553#section-5.2:
481 * Join a multicast group on a specified local interface. If the
482 * interface index is specified as 0, the kernel chooses the local
483 * interface. For example, some kernels look up the multicast
484 * group in the normal IPv6 routing table and using the resulting
485 * interface; we do this for each interface, so no need to use
488 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
490 /* Join the multicast group */
492 GNUNET_NETWORK_socket_setsockopt
493 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
494 &multicastRequest, sizeof (multicastRequest)))
496 LOG (GNUNET_ERROR_TYPE_WARNING,
497 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
503 * setup Cryogenic FD for ipv6 broadcasting
507 GNUNET_asprintf (&filename,
510 if (0 == ACCESS (name, R_OK))
513 GNUNET_DISK_file_open (filename,
514 GNUNET_DISK_OPEN_WRITE,
515 GNUNET_DISK_PERM_NONE);
517 GNUNET_free (filename);
520 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
523 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
524 plugin->broadcast_tail, ba);
530 * Setup broadcasting subsystem.
533 * @param server_addrv6
534 * @param server_addrv4
537 setup_broadcast (struct Plugin *plugin,
538 struct sockaddr_in6 *server_addrv6,
539 struct sockaddr_in *server_addrv4)
542 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
546 LOG (GNUNET_ERROR_TYPE_WARNING,
547 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
551 /* always create tokenizers */
552 plugin->broadcast_mst =
553 GNUNET_SERVER_mst_create (&broadcast_mst_cb, plugin);
555 if (GNUNET_YES != plugin->enable_broadcasting)
556 return; /* We do not send, just receive */
558 /* create IPv4 broadcast socket */
559 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
563 if (GNUNET_NETWORK_socket_setsockopt
564 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
565 sizeof (int)) != GNUNET_OK)
567 LOG (GNUNET_ERROR_TYPE_WARNING,
568 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
569 ntohs (server_addrv4->sin_port));
572 /* create IPv6 multicast socket */
573 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
575 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
577 inet_pton (AF_INET6, "FF05::13B",
578 &plugin->ipv6_multicast_address.sin6_addr));
579 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
580 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
582 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
587 * Stop broadcasting subsystem.
592 stop_broadcast (struct Plugin *plugin)
594 if (GNUNET_YES == plugin->enable_broadcasting)
596 /* Disable broadcasting */
597 while (plugin->broadcast_head != NULL)
599 struct BroadcastAddress *p = plugin->broadcast_head;
601 if (p->broadcast_task != NULL)
603 GNUNET_SCHEDULER_cancel (p->broadcast_task);
604 p->broadcast_task = NULL;
606 if ((GNUNET_YES == plugin->enable_ipv6) &&
607 (NULL != plugin->sockv6) &&
608 (p->addrlen == sizeof (struct sockaddr_in6)))
610 /* Create IPv6 multicast request */
611 struct ipv6_mreq multicastRequest;
612 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
614 multicastRequest.ipv6mr_multiaddr =
615 plugin->ipv6_multicast_address.sin6_addr;
616 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
618 /* Leave the multicast group */
620 GNUNET_NETWORK_socket_setsockopt
621 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
622 &multicastRequest, sizeof (multicastRequest)))
624 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
628 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
633 GNUNET_DISK_file_close(p->cryogenic_fd);
635 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
636 plugin->broadcast_tail, p);
637 GNUNET_free (p->addr);
643 if (NULL != plugin->broadcast_mst)
645 GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
646 plugin->broadcast_mst = NULL;
650 /* end of plugin_transport_udp_broadcasting.c */