2 This file is part of GNUnet
3 (C) 2010, 2011 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 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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;
99 struct GNUNET_DISK_FileHandle *cryogenic_fd;
102 * Time out for cryogenic.
104 struct pm_times cryogenic_times;
111 struct Plugin *plugin;
113 struct IPv4UdpAddress addr;
118 enum GNUNET_ATS_Network_Type ats_address_network_type;
124 struct Plugin *plugin;
126 struct IPv6UdpAddress addr;
131 enum GNUNET_ATS_Network_Type ats_address_network_type;
136 broadcast_ipv6_mst_cb (void *cls, void *client,
137 const struct GNUNET_MessageHeader *message)
139 struct Plugin *plugin = cls;
140 struct Mstv6Context *mc = client;
141 struct GNUNET_HELLO_Address *address;
142 const struct GNUNET_MessageHeader *hello;
143 const struct UDP_Beacon_Message *msg;
144 struct GNUNET_ATS_Information atsi;
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), GNUNET_i2s (&msg->sender),
154 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
157 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
158 atsi.value = htonl (mc->ats_address_network_type);
159 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
161 hello = (struct GNUNET_MessageHeader *) &msg[1];
162 address = GNUNET_HELLO_address_allocate (&msg->sender, PLUGIN_NAME,
163 (const char *) &mc->addr,
165 GNUNET_HELLO_ADDRESS_INFO_NONE);
166 plugin->env->receive (plugin->env->cls, address, NULL, hello);
167 plugin->env->update_address_metrics (plugin->env->cls, address,
169 GNUNET_HELLO_address_free (address);
170 GNUNET_STATISTICS_update (plugin->env->stats,
172 ("# IPv6 multicast HELLO beacons received via udp"),
180 broadcast_ipv4_mst_cb (void *cls, void *client,
181 const struct GNUNET_MessageHeader *message)
183 struct Plugin *plugin = cls;
184 struct Mstv4Context *mc = client;
185 struct GNUNET_HELLO_Address *address;
186 const struct GNUNET_MessageHeader *hello;
187 const struct UDP_Beacon_Message *msg;
188 struct GNUNET_ATS_Information atsi;
190 msg = (const struct UDP_Beacon_Message *) message;
192 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
193 ntohs (msg->header.type))
195 LOG (GNUNET_ERROR_TYPE_DEBUG,
196 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
197 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
198 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
202 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
203 atsi.value = htonl (mc->ats_address_network_type);
204 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
206 hello = (struct GNUNET_MessageHeader *) &msg[1];
207 address = GNUNET_HELLO_address_allocate (&msg->sender,
209 (const char *) &mc->addr,
211 GNUNET_HELLO_ADDRESS_INFO_NONE);
212 plugin->env->receive (plugin->env->cls, address, NULL, hello);
213 plugin->env->update_address_metrics (plugin->env->cls, address,
215 GNUNET_HELLO_address_free (address);
217 GNUNET_STATISTICS_update (plugin->env->stats,
218 _("# IPv4 broadcast HELLO beacons received via udp"),
226 udp_broadcast_receive (struct Plugin *plugin,
229 const struct sockaddr *addr,
232 if (addrlen == sizeof (struct sockaddr_in))
234 LOG (GNUNET_ERROR_TYPE_DEBUG,
235 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
236 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
237 struct Mstv4Context *mc;
239 mc = GNUNET_new (struct Mstv4Context);
240 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
242 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
243 mc->addr.u4_port = av4->sin_port;
244 mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls,
245 (const struct sockaddr *) addr,
248 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
250 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
251 GNUNET_NO, GNUNET_NO))
254 if (addrlen == sizeof (struct sockaddr_in6))
256 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
258 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
259 struct Mstv6Context *mc;
261 mc = GNUNET_new (struct Mstv6Context);
262 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
264 mc->addr.ipv6_addr = av6->sin6_addr;
265 mc->addr.u6_port = av6->sin6_port;
266 mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
267 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
269 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
270 GNUNET_NO, GNUNET_NO))
277 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
282 const struct GNUNET_MessageHeader *hello;
283 hello = plugin->env->get_our_hello ();
286 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
287 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
289 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
290 (msg_size > (UDP_MTU)))
293 msg->sender = *(plugin->env->my_identity);
294 msg->header.size = htons (msg_size);
295 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
296 memcpy (&msg[1], hello, hello_size);
302 udp_ipv4_broadcast_send (void *cls,
303 const struct GNUNET_SCHEDULER_TaskContext *tc)
305 struct BroadcastAddress *baddr = cls;
306 struct Plugin *plugin = baddr->plugin;
309 char buf[65536] GNUNET_ALIGN;
311 baddr->broadcast_task = NULL;
313 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
316 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
318 addr->sin_port = htons (plugin->port);
319 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
320 (const struct sockaddr *) addr,
322 if (sent == GNUNET_SYSERR)
324 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
326 /* "Network unreachable" or "Network down"
328 * This indicates that we just do not have network connectivity
330 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
331 "Network connectivity is down, cannot send beacon!\n");
334 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
338 LOG (GNUNET_ERROR_TYPE_DEBUG,
339 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
340 GNUNET_a2s (baddr->addr, baddr->addrlen));
348 if (NULL != baddr->cryogenic_fd)
350 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
351 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
353 if (ioctl(baddr->cryogenic_fd->fd,
354 PM_SET_DELAY_AND_TIMEOUT,
355 &baddr->cryogenic_times) < 0)
357 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
358 baddr->broadcast_task =
359 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
360 &udp_ipv4_broadcast_send, baddr);
363 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
365 &udp_ipv4_broadcast_send,
371 baddr->broadcast_task =
372 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
373 &udp_ipv4_broadcast_send, baddr);
378 udp_ipv6_broadcast_send (void *cls,
379 const struct GNUNET_SCHEDULER_TaskContext *tc)
381 struct BroadcastAddress *baddr = cls;
382 struct Plugin *plugin = baddr->plugin;
385 char buf[65536] GNUNET_ALIGN;
386 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
388 baddr->broadcast_task = NULL;
390 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
391 /* Note: unclear if this actually works to limit the multicast to
392 the specified interface as we're not (necessarily) using a
393 link-local multicast group and the kernel suggests that the
394 scope ID is only respected for link-local addresses; however,
395 if the scope ID is ignored, the kernel should just multicast
396 on ALL interfaces, which is merely slightly less efficient;
397 in that case, we might want to revert to only doing this
398 once, and not per interface (hard to test...) */
399 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
400 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
401 (const struct sockaddr *)
402 &plugin->ipv6_multicast_address,
403 sizeof (struct sockaddr_in6));
404 plugin->ipv6_multicast_address.sin6_scope_id = 0;
405 if (sent == GNUNET_SYSERR)
407 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
409 /* "Network unreachable" or "Network down"
411 * This indicates that this system is IPv6 enabled, but does not
412 * have a valid global IPv6 address assigned
414 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
415 "Network connectivity is down, cannot send beacon!\n");
418 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
422 LOG (GNUNET_ERROR_TYPE_DEBUG,
423 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
425 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
426 sizeof (struct sockaddr_in6)));
432 if (NULL != baddr->cryogenic_fd)
434 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
435 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
437 if (ioctl(baddr->cryogenic_fd->fd,
438 PM_SET_DELAY_AND_TIMEOUT,
439 &baddr->cryogenic_times) < 0)
441 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
442 baddr->broadcast_task =
443 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
444 &udp_ipv6_broadcast_send, baddr);
447 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
449 &udp_ipv4_broadcast_send,
454 baddr->broadcast_task =
455 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
456 &udp_ipv6_broadcast_send, baddr);
461 * Callback function invoked for each interface found.
463 * @param cls closure with the `struct Plugin`
464 * @param name name of the interface (can be NULL for unknown)
465 * @param isDefault is this presumably the default interface
466 * @param addr address of this interface (can be NULL for unknown or unassigned)
467 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
468 * @param netmask the network mask (can be NULL for unknown or unassigned)
469 * @param addrlen length of the address
470 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
473 iface_proc (void *cls,
476 const struct sockaddr *addr,
477 const struct sockaddr *broadcast_addr,
478 const struct sockaddr *netmask, socklen_t addrlen)
480 struct Plugin *plugin = cls;
481 struct BroadcastAddress *ba;
482 enum GNUNET_ATS_Network_Type network;
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "address %s for interface %s %p\n ",
488 GNUNET_a2s (addr, addrlen), name, addr);
489 if (NULL == broadcast_addr)
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "broadcast address %s for interface %s %p\n ",
493 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
495 GNUNET_a2s (netmask, addrlen), name, netmask);
497 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
498 if (GNUNET_ATS_NET_LOOPBACK == network)
500 /* Broadcasting on loopback does not make sense */
504 ba = GNUNET_new (struct BroadcastAddress);
506 ba->addr = GNUNET_malloc (addrlen);
507 memcpy (ba->addr, broadcast_addr, addrlen);
508 ba->addrlen = addrlen;
510 if ( (GNUNET_YES == plugin->enable_ipv4) &&
511 (NULL != plugin->sockv4) &&
512 (addrlen == sizeof (struct sockaddr_in)) )
516 * setup Cryogenic FD for ipv4 broadcasting
520 GNUNET_asprintf (&filename,
523 if (0 == ACCESS (name, R_OK))
526 GNUNET_DISK_file_open (filename,
527 GNUNET_DISK_OPEN_WRITE,
528 GNUNET_DISK_PERM_NONE);
530 GNUNET_free (filename);
533 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
535 if ((GNUNET_YES == plugin->enable_ipv6) &&
536 (NULL != plugin->sockv6) &&
537 (addrlen == sizeof (struct sockaddr_in6)))
539 /* Create IPv6 multicast request */
540 struct ipv6_mreq multicastRequest;
541 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
543 multicastRequest.ipv6mr_multiaddr =
544 plugin->ipv6_multicast_address.sin6_addr;
545 /* http://tools.ietf.org/html/rfc2553#section-5.2:
549 * Join a multicast group on a specified local interface. If the
550 * interface index is specified as 0, the kernel chooses the local
551 * interface. For example, some kernels look up the multicast
552 * group in the normal IPv6 routing table and using the resulting
553 * interface; we do this for each interface, so no need to use
556 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
558 /* Join the multicast group */
560 GNUNET_NETWORK_socket_setsockopt
561 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
562 &multicastRequest, sizeof (multicastRequest)))
564 LOG (GNUNET_ERROR_TYPE_WARNING,
565 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
571 * setup Cryogenic FD for ipv6 broadcasting
575 GNUNET_asprintf (&filename,
578 if (0 == ACCESS (name, R_OK))
581 GNUNET_DISK_file_open (filename,
582 GNUNET_DISK_OPEN_WRITE,
583 GNUNET_DISK_PERM_NONE);
585 GNUNET_free (filename);
588 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
591 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
592 plugin->broadcast_tail, ba);
598 setup_broadcast (struct Plugin *plugin,
599 struct sockaddr_in6 *server_addrv6,
600 struct sockaddr_in *server_addrv4)
602 const struct GNUNET_MessageHeader *hello;
604 hello = plugin->env->get_our_hello ();
606 GNUNET_HELLO_is_friend_only ((const struct GNUNET_HELLO_Message *) hello))
608 LOG (GNUNET_ERROR_TYPE_WARNING,
609 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
613 /* always create tokenizers */
614 plugin->broadcast_ipv4_mst =
615 GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin);
616 plugin->broadcast_ipv6_mst =
617 GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
619 if (GNUNET_YES != plugin->enable_broadcasting)
620 return; /* We do not send, just receive */
622 /* create IPv4 broadcast socket */
623 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
627 if (GNUNET_NETWORK_socket_setsockopt
628 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
629 sizeof (int)) != GNUNET_OK)
631 LOG (GNUNET_ERROR_TYPE_WARNING,
632 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
633 ntohs (server_addrv4->sin_port));
636 /* create IPv6 multicast socket */
637 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
639 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
641 inet_pton (AF_INET6, "FF05::13B",
642 &plugin->ipv6_multicast_address.sin6_addr));
643 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
644 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
646 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
651 stop_broadcast (struct Plugin *plugin)
653 if (GNUNET_YES == plugin->enable_broadcasting)
655 /* Disable broadcasting */
656 while (plugin->broadcast_head != NULL)
658 struct BroadcastAddress *p = plugin->broadcast_head;
660 if (p->broadcast_task != NULL)
662 GNUNET_SCHEDULER_cancel (p->broadcast_task);
663 p->broadcast_task = NULL;
665 if ((GNUNET_YES == plugin->enable_ipv6) &&
666 (NULL != plugin->sockv6) &&
667 (p->addrlen == sizeof (struct sockaddr_in6)))
669 /* Create IPv6 multicast request */
670 struct ipv6_mreq multicastRequest;
671 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
673 multicastRequest.ipv6mr_multiaddr =
674 plugin->ipv6_multicast_address.sin6_addr;
675 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
677 /* Leave the multicast group */
679 GNUNET_NETWORK_socket_setsockopt
680 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
681 &multicastRequest, sizeof (multicastRequest)))
683 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
687 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
692 GNUNET_DISK_file_close(p->cryogenic_fd);
694 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
695 plugin->broadcast_tail, p);
696 GNUNET_free (p->addr);
702 if (NULL != plugin->broadcast_ipv4_mst)
704 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
705 plugin->broadcast_ipv4_mst = NULL;
707 if (NULL != plugin->broadcast_ipv6_mst)
709 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
710 plugin->broadcast_ipv6_mst = NULL;
714 /* end of plugin_transport_udp_broadcasting.c */