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 GNUNET_SCHEDULER_TaskIdentifier 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;
115 * ATS network type in NBO
117 uint32_t ats_address_network_type;
122 struct Plugin *plugin;
124 struct IPv6UdpAddress addr;
126 * ATS network type in NBO
128 uint32_t ats_address_network_type;
133 broadcast_ipv6_mst_cb (void *cls, void *client,
134 const struct GNUNET_MessageHeader *message)
136 struct Plugin *plugin = cls;
137 struct Mstv6Context *mc = client;
138 struct GNUNET_HELLO_Address *address;
139 const struct GNUNET_MessageHeader *hello;
140 const struct UDP_Beacon_Message *msg;
141 struct GNUNET_ATS_Information atsi;
143 msg = (const struct UDP_Beacon_Message *) message;
145 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
146 ntohs (msg->header.type))
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
150 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
151 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
154 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
155 atsi.value = mc->ats_address_network_type;
156 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
158 hello = (struct GNUNET_MessageHeader *) &msg[1];
159 address = GNUNET_HELLO_address_allocate (&msg->sender, PLUGIN_NAME,
160 (const char *) &mc->addr, sizeof (mc->addr), GNUNET_HELLO_ADDRESS_INFO_INBOUND);
161 plugin->env->receive (plugin->env->cls, address, NULL, hello);
162 plugin->env->update_address_metrics (plugin->env->cls, address,
164 GNUNET_HELLO_address_free (address);
165 GNUNET_STATISTICS_update (plugin->env->stats,
167 ("# IPv6 multicast HELLO beacons received via udp"),
175 broadcast_ipv4_mst_cb (void *cls, void *client,
176 const struct GNUNET_MessageHeader *message)
178 struct Plugin *plugin = cls;
179 struct Mstv4Context *mc = client;
180 struct GNUNET_HELLO_Address *address;
181 const struct GNUNET_MessageHeader *hello;
182 const struct UDP_Beacon_Message *msg;
183 struct GNUNET_ATS_Information atsi;
185 msg = (const struct UDP_Beacon_Message *) message;
187 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
188 ntohs (msg->header.type))
190 LOG (GNUNET_ERROR_TYPE_DEBUG,
191 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
192 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
193 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
197 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
198 atsi.value = mc->ats_address_network_type;
199 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
201 hello = (struct GNUNET_MessageHeader *) &msg[1];
202 address = GNUNET_HELLO_address_allocate (&msg->sender, PLUGIN_NAME,
203 (const char *) &mc->addr, sizeof (mc->addr), GNUNET_HELLO_ADDRESS_INFO_INBOUND);
204 plugin->env->receive (plugin->env->cls, address, NULL, hello);
205 plugin->env->update_address_metrics (plugin->env->cls, address,
207 GNUNET_HELLO_address_free (address);
209 GNUNET_STATISTICS_update (plugin->env->stats,
210 _("# IPv4 broadcast HELLO beacons received via udp"),
218 udp_broadcast_receive (struct Plugin *plugin,
221 const struct sockaddr *addr,
224 struct GNUNET_ATS_Information ats;
226 if (addrlen == sizeof (struct sockaddr_in))
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
230 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
231 struct Mstv4Context *mc;
233 mc = GNUNET_new (struct Mstv4Context);
234 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
236 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
237 mc->addr.u4_port = av4->sin_port;
238 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
239 mc->ats_address_network_type = ats.value;
241 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
243 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
244 GNUNET_NO, GNUNET_NO))
247 if (addrlen == sizeof (struct sockaddr_in6))
249 LOG (GNUNET_ERROR_TYPE_DEBUG,
250 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
251 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
252 struct Mstv6Context *mc;
254 mc = GNUNET_new (struct Mstv6Context);
255 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
257 mc->addr.ipv6_addr = av6->sin6_addr;
258 mc->addr.u6_port = av6->sin6_port;
259 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
260 mc->ats_address_network_type = ats.value;
261 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
263 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
264 GNUNET_NO, GNUNET_NO))
271 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
276 const struct GNUNET_MessageHeader *hello;
277 hello = plugin->env->get_our_hello ();
280 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
281 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
283 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
284 (msg_size > (UDP_MTU)))
287 msg->sender = *(plugin->env->my_identity);
288 msg->header.size = htons (msg_size);
289 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
290 memcpy (&msg[1], hello, hello_size);
296 udp_ipv4_broadcast_send (void *cls,
297 const struct GNUNET_SCHEDULER_TaskContext *tc)
299 struct BroadcastAddress *baddr = cls;
300 struct Plugin *plugin = baddr->plugin;
303 char buf[65536] GNUNET_ALIGN;
305 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
307 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
310 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
312 addr->sin_port = htons (plugin->port);
313 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
314 (const struct sockaddr *) addr,
316 if (sent == GNUNET_SYSERR)
318 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
320 /* "Network unreachable" or "Network down"
322 * This indicates that we just do not have network connectivity
324 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
325 "Network connectivity is down, cannot send beacon!\n");
328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
334 GNUNET_a2s (baddr->addr, baddr->addrlen));
342 if (NULL != baddr->cryogenic_fd)
344 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
345 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
347 if (ioctl(baddr->cryogenic_fd->fd,
348 PM_SET_DELAY_AND_TIMEOUT,
349 &baddr->cryogenic_times) < 0)
351 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
352 baddr->broadcast_task =
353 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
354 &udp_ipv4_broadcast_send, baddr);
357 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
359 &udp_ipv4_broadcast_send,
365 baddr->broadcast_task =
366 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
367 &udp_ipv4_broadcast_send, baddr);
372 udp_ipv6_broadcast_send (void *cls,
373 const struct GNUNET_SCHEDULER_TaskContext *tc)
375 struct BroadcastAddress *baddr = cls;
376 struct Plugin *plugin = baddr->plugin;
379 char buf[65536] GNUNET_ALIGN;
380 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
382 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
384 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
385 /* Note: unclear if this actually works to limit the multicast to
386 the specified interface as we're not (necessarily) using a
387 link-local multicast group and the kernel suggests that the
388 scope ID is only respected for link-local addresses; however,
389 if the scope ID is ignored, the kernel should just multicast
390 on ALL interfaces, which is merely slightly less efficient;
391 in that case, we might want to revert to only doing this
392 once, and not per interface (hard to test...) */
393 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
394 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
395 (const struct sockaddr *)
396 &plugin->ipv6_multicast_address,
397 sizeof (struct sockaddr_in6));
398 plugin->ipv6_multicast_address.sin6_scope_id = 0;
399 if (sent == GNUNET_SYSERR)
401 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
403 /* "Network unreachable" or "Network down"
405 * This indicates that this system is IPv6 enabled, but does not
406 * have a valid global IPv6 address assigned
408 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
409 "Network connectivity is down, cannot send beacon!\n");
412 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
416 LOG (GNUNET_ERROR_TYPE_DEBUG,
417 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
419 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
420 sizeof (struct sockaddr_in6)));
426 if (NULL != baddr->cryogenic_fd)
428 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
429 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
431 if (ioctl(baddr->cryogenic_fd->fd,
432 PM_SET_DELAY_AND_TIMEOUT,
433 &baddr->cryogenic_times) < 0)
435 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
436 baddr->broadcast_task =
437 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
438 &udp_ipv6_broadcast_send, baddr);
441 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
443 &udp_ipv4_broadcast_send,
448 baddr->broadcast_task =
449 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
450 &udp_ipv6_broadcast_send, baddr);
455 * Callback function invoked for each interface found.
457 * @param cls closure with the `struct Plugin`
458 * @param name name of the interface (can be NULL for unknown)
459 * @param isDefault is this presumably the default interface
460 * @param addr address of this interface (can be NULL for unknown or unassigned)
461 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
462 * @param netmask the network mask (can be NULL for unknown or unassigned)
463 * @param addrlen length of the address
464 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
467 iface_proc (void *cls,
470 const struct sockaddr *addr,
471 const struct sockaddr *broadcast_addr,
472 const struct sockaddr *netmask, socklen_t addrlen)
474 struct Plugin *plugin = cls;
475 struct BroadcastAddress *ba;
476 struct GNUNET_ATS_Information network;
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "address %s for interface %s %p\n ",
482 GNUNET_a2s (addr, addrlen), name, addr);
483 if (NULL == broadcast_addr)
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "broadcast address %s for interface %s %p\n ",
487 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
489 GNUNET_a2s (netmask, addrlen), name, netmask);
491 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
492 if (GNUNET_ATS_NET_LOOPBACK == ntohl(network.value))
494 /* Broadcasting on loopback does not make sense */
498 ba = GNUNET_new (struct BroadcastAddress);
500 ba->addr = GNUNET_malloc (addrlen);
501 memcpy (ba->addr, broadcast_addr, addrlen);
502 ba->addrlen = addrlen;
504 if ( (GNUNET_YES == plugin->enable_ipv4) &&
505 (NULL != plugin->sockv4) &&
506 (addrlen == sizeof (struct sockaddr_in)) )
510 * setup Cryogenic FD for ipv4 broadcasting
514 GNUNET_asprintf (&filename,
517 if (0 == ACCESS (name, R_OK))
520 GNUNET_DISK_file_open (filename,
521 GNUNET_DISK_OPEN_WRITE,
522 GNUNET_DISK_PERM_NONE);
524 GNUNET_free (filename);
527 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
529 if ((GNUNET_YES == plugin->enable_ipv6) &&
530 (NULL != plugin->sockv6) &&
531 (addrlen == sizeof (struct sockaddr_in6)))
533 /* Create IPv6 multicast request */
534 struct ipv6_mreq multicastRequest;
535 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
537 multicastRequest.ipv6mr_multiaddr =
538 plugin->ipv6_multicast_address.sin6_addr;
539 /* http://tools.ietf.org/html/rfc2553#section-5.2:
543 * Join a multicast group on a specified local interface. If the
544 * interface index is specified as 0, the kernel chooses the local
545 * interface. For example, some kernels look up the multicast
546 * group in the normal IPv6 routing table and using the resulting
547 * interface; we do this for each interface, so no need to use
550 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
552 /* Join the multicast group */
554 GNUNET_NETWORK_socket_setsockopt
555 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
556 &multicastRequest, sizeof (multicastRequest)))
558 LOG (GNUNET_ERROR_TYPE_WARNING,
559 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
565 * setup Cryogenic FD for ipv6 broadcasting
569 GNUNET_asprintf (&filename,
572 if (0 == ACCESS (name, R_OK))
575 GNUNET_DISK_file_open (filename,
576 GNUNET_DISK_OPEN_WRITE,
577 GNUNET_DISK_PERM_NONE);
579 GNUNET_free (filename);
582 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
585 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
586 plugin->broadcast_tail, ba);
592 setup_broadcast (struct Plugin *plugin,
593 struct sockaddr_in6 *server_addrv6,
594 struct sockaddr_in *server_addrv4)
596 const struct GNUNET_MessageHeader *hello;
598 hello = plugin->env->get_our_hello ();
600 GNUNET_HELLO_is_friend_only ((const struct GNUNET_HELLO_Message *) hello))
602 LOG (GNUNET_ERROR_TYPE_WARNING,
603 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
607 /* always create tokenizers */
608 plugin->broadcast_ipv4_mst =
609 GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin);
610 plugin->broadcast_ipv6_mst =
611 GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
613 if (GNUNET_YES != plugin->enable_broadcasting)
614 return; /* We do not send, just receive */
616 /* create IPv4 broadcast socket */
617 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
621 if (GNUNET_NETWORK_socket_setsockopt
622 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
623 sizeof (int)) != GNUNET_OK)
625 LOG (GNUNET_ERROR_TYPE_WARNING,
626 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
627 ntohs (server_addrv4->sin_port));
630 /* create IPv6 multicast socket */
631 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
633 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
635 inet_pton (AF_INET6, "FF05::13B",
636 &plugin->ipv6_multicast_address.sin6_addr));
637 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
638 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
640 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
645 stop_broadcast (struct Plugin *plugin)
647 if (GNUNET_YES == plugin->enable_broadcasting)
649 /* Disable broadcasting */
650 while (plugin->broadcast_head != NULL)
652 struct BroadcastAddress *p = plugin->broadcast_head;
654 if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
656 GNUNET_SCHEDULER_cancel (p->broadcast_task);
657 p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
659 if ((GNUNET_YES == plugin->enable_ipv6) &&
660 (NULL != plugin->sockv6) &&
661 (p->addrlen == sizeof (struct sockaddr_in6)))
663 /* Create IPv6 multicast request */
664 struct ipv6_mreq multicastRequest;
665 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
667 multicastRequest.ipv6mr_multiaddr =
668 plugin->ipv6_multicast_address.sin6_addr;
669 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
671 /* Leave the multicast group */
673 GNUNET_NETWORK_socket_setsockopt
674 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
675 &multicastRequest, sizeof (multicastRequest)))
677 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
681 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
686 GNUNET_DISK_file_close(p->cryogenic_fd);
688 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
689 plugin->broadcast_tail, p);
690 GNUNET_free (p->addr);
696 if (NULL != plugin->broadcast_ipv4_mst)
698 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
699 plugin->broadcast_ipv4_mst = NULL;
701 if (NULL != plugin->broadcast_ipv6_mst)
703 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
704 plugin->broadcast_ipv6_mst = NULL;
708 /* end of plugin_transport_udp_broadcasting.c */