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;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "address %s for interface %s %p\n ",
481 GNUNET_a2s (addr, addrlen), name, addr);
482 if (NULL == broadcast_addr)
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "broadcast address %s for interface %s %p\n ",
486 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
488 GNUNET_a2s (netmask, addrlen), name, netmask);
490 ba = GNUNET_new (struct BroadcastAddress);
492 ba->addr = GNUNET_malloc (addrlen);
493 memcpy (ba->addr, broadcast_addr, addrlen);
494 ba->addrlen = addrlen;
495 if ( (GNUNET_YES == plugin->enable_ipv4) &&
496 (NULL != plugin->sockv4) &&
497 (addrlen == sizeof (struct sockaddr_in)) )
501 * setup Cryogenic FD for ipv4 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_ipv4_broadcast_send, ba);
520 if ((GNUNET_YES == plugin->enable_ipv6) &&
521 (NULL != plugin->sockv6) &&
522 (addrlen == sizeof (struct sockaddr_in6)))
524 /* Create IPv6 multicast request */
525 struct ipv6_mreq multicastRequest;
526 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
528 multicastRequest.ipv6mr_multiaddr =
529 plugin->ipv6_multicast_address.sin6_addr;
530 /* http://tools.ietf.org/html/rfc2553#section-5.2:
534 * Join a multicast group on a specified local interface. If the
535 * interface index is specified as 0, the kernel chooses the local
536 * interface. For example, some kernels look up the multicast
537 * group in the normal IPv6 routing table and using the resulting
538 * interface; we do this for each interface, so no need to use
541 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
543 /* Join the multicast group */
545 GNUNET_NETWORK_socket_setsockopt
546 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
547 &multicastRequest, sizeof (multicastRequest)))
549 LOG (GNUNET_ERROR_TYPE_WARNING,
550 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
556 * setup Cryogenic FD for ipv6 broadcasting
560 GNUNET_asprintf (&filename,
563 if (0 == ACCESS (name, R_OK))
566 GNUNET_DISK_file_open (filename,
567 GNUNET_DISK_OPEN_WRITE,
568 GNUNET_DISK_PERM_NONE);
570 GNUNET_free (filename);
573 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
576 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
577 plugin->broadcast_tail, ba);
583 setup_broadcast (struct Plugin *plugin,
584 struct sockaddr_in6 *server_addrv6,
585 struct sockaddr_in *server_addrv4)
587 const struct GNUNET_MessageHeader *hello;
589 hello = plugin->env->get_our_hello ();
591 GNUNET_HELLO_is_friend_only ((const struct GNUNET_HELLO_Message *) hello))
593 LOG (GNUNET_ERROR_TYPE_WARNING,
594 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
598 /* always create tokenizers */
599 plugin->broadcast_ipv4_mst =
600 GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin);
601 plugin->broadcast_ipv6_mst =
602 GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
604 if (GNUNET_YES != plugin->enable_broadcasting)
605 return; /* We do not send, just receive */
607 /* create IPv4 broadcast socket */
608 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
612 if (GNUNET_NETWORK_socket_setsockopt
613 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
614 sizeof (int)) != GNUNET_OK)
616 LOG (GNUNET_ERROR_TYPE_WARNING,
617 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
618 ntohs (server_addrv4->sin_port));
621 /* create IPv6 multicast socket */
622 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
624 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
626 inet_pton (AF_INET6, "FF05::13B",
627 &plugin->ipv6_multicast_address.sin6_addr));
628 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
629 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
631 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
636 stop_broadcast (struct Plugin *plugin)
638 if (GNUNET_YES == plugin->enable_broadcasting)
640 /* Disable broadcasting */
641 while (plugin->broadcast_head != NULL)
643 struct BroadcastAddress *p = plugin->broadcast_head;
645 if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
647 GNUNET_SCHEDULER_cancel (p->broadcast_task);
648 p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
650 if ((GNUNET_YES == plugin->enable_ipv6) &&
651 (NULL != plugin->sockv6) &&
652 (p->addrlen == sizeof (struct sockaddr_in6)))
654 /* Create IPv6 multicast request */
655 struct ipv6_mreq multicastRequest;
656 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
658 multicastRequest.ipv6mr_multiaddr =
659 plugin->ipv6_multicast_address.sin6_addr;
660 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
662 /* Leave the multicast group */
664 GNUNET_NETWORK_socket_setsockopt
665 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
666 &multicastRequest, sizeof (multicastRequest)))
668 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
672 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
677 GNUNET_DISK_file_close(p->cryogenic_fd);
679 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
680 plugin->broadcast_tail, p);
681 GNUNET_free (p->addr);
687 if (NULL != plugin->broadcast_ipv4_mst)
689 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
690 plugin->broadcast_ipv4_mst = NULL;
692 if (NULL != plugin->broadcast_ipv6_mst)
694 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
695 plugin->broadcast_ipv6_mst = NULL;
699 /* end of plugin_transport_udp_broadcasting.c */