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 const struct GNUNET_MessageHeader *hello;
139 const struct UDP_Beacon_Message *msg;
140 struct GNUNET_ATS_Information atsi;
142 msg = (const struct UDP_Beacon_Message *) message;
144 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
145 ntohs (msg->header.type))
147 LOG (GNUNET_ERROR_TYPE_DEBUG,
148 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
149 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
150 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
153 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
154 atsi.value = mc->ats_address_network_type;
155 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
157 hello = (struct GNUNET_MessageHeader *) &msg[1];
158 plugin->env->receive (plugin->env->cls,
162 (const char *) &mc->addr,
164 plugin->env->update_address_metrics (plugin->env->cls,
166 (const char *) &mc->addr,
171 GNUNET_STATISTICS_update (plugin->env->stats,
173 ("# IPv6 multicast HELLO beacons received via udp"),
181 broadcast_ipv4_mst_cb (void *cls, void *client,
182 const struct GNUNET_MessageHeader *message)
184 struct Plugin *plugin = cls;
185 struct Mstv4Context *mc = client;
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 = 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 plugin->env->receive (plugin->env->cls,
211 (const char *) &mc->addr,
214 plugin->env->update_address_metrics (plugin->env->cls,
216 (const char *) &mc->addr,
221 GNUNET_STATISTICS_update (plugin->env->stats,
222 _("# IPv4 broadcast HELLO beacons received via udp"),
230 udp_broadcast_receive (struct Plugin *plugin,
233 const struct sockaddr *addr,
236 struct GNUNET_ATS_Information ats;
238 if (addrlen == sizeof (struct sockaddr_in))
240 LOG (GNUNET_ERROR_TYPE_DEBUG,
241 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
242 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
243 struct Mstv4Context *mc;
245 mc = GNUNET_new (struct Mstv4Context);
246 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
248 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
249 mc->addr.u4_port = av4->sin_port;
250 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
251 mc->ats_address_network_type = ats.value;
253 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
255 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
256 GNUNET_NO, GNUNET_NO))
259 if (addrlen == sizeof (struct sockaddr_in6))
261 LOG (GNUNET_ERROR_TYPE_DEBUG,
262 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
263 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
264 struct Mstv6Context *mc;
266 mc = GNUNET_new (struct Mstv6Context);
267 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
269 mc->addr.ipv6_addr = av6->sin6_addr;
270 mc->addr.u6_port = av6->sin6_port;
271 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
272 mc->ats_address_network_type = ats.value;
273 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
275 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
276 GNUNET_NO, GNUNET_NO))
283 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
288 const struct GNUNET_MessageHeader *hello;
289 hello = plugin->env->get_our_hello ();
292 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
293 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
295 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
296 (msg_size > (UDP_MTU)))
299 msg->sender = *(plugin->env->my_identity);
300 msg->header.size = htons (msg_size);
301 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
302 memcpy (&msg[1], hello, hello_size);
308 udp_ipv4_broadcast_send (void *cls,
309 const struct GNUNET_SCHEDULER_TaskContext *tc)
311 struct BroadcastAddress *baddr = cls;
312 struct Plugin *plugin = baddr->plugin;
315 char buf[65536] GNUNET_ALIGN;
317 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
319 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
322 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
324 addr->sin_port = htons (plugin->port);
325 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
326 (const struct sockaddr *) addr,
328 if (sent == GNUNET_SYSERR)
330 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
332 /* "Network unreachable" or "Network down"
334 * This indicates that we just do not have network connectivity
336 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
337 "Network connectivity is down, cannot send beacon!\n");
340 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
346 GNUNET_a2s (baddr->addr, baddr->addrlen));
354 if (NULL != baddr->cryogenic_fd)
356 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
357 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
359 if (ioctl(baddr->cryogenic_fd->fd,
360 PM_SET_DELAY_AND_TIMEOUT,
361 &baddr->cryogenic_times) < 0)
363 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
364 baddr->broadcast_task =
365 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
366 &udp_ipv4_broadcast_send, baddr);
369 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
371 &udp_ipv4_broadcast_send,
377 baddr->broadcast_task =
378 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
379 &udp_ipv4_broadcast_send, baddr);
384 udp_ipv6_broadcast_send (void *cls,
385 const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct BroadcastAddress *baddr = cls;
388 struct Plugin *plugin = baddr->plugin;
391 char buf[65536] GNUNET_ALIGN;
392 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
394 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
396 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
397 /* Note: unclear if this actually works to limit the multicast to
398 the specified interface as we're not (necessarily) using a
399 link-local multicast group and the kernel suggests that the
400 scope ID is only respected for link-local addresses; however,
401 if the scope ID is ignored, the kernel should just multicast
402 on ALL interfaces, which is merely slightly less efficient;
403 in that case, we might want to revert to only doing this
404 once, and not per interface (hard to test...) */
405 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
406 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
407 (const struct sockaddr *)
408 &plugin->ipv6_multicast_address,
409 sizeof (struct sockaddr_in6));
410 plugin->ipv6_multicast_address.sin6_scope_id = 0;
411 if (sent == GNUNET_SYSERR)
413 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
415 /* "Network unreachable" or "Network down"
417 * This indicates that this system is IPv6 enabled, but does not
418 * have a valid global IPv6 address assigned
420 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
421 "Network connectivity is down, cannot send beacon!\n");
424 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
428 LOG (GNUNET_ERROR_TYPE_DEBUG,
429 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
431 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
432 sizeof (struct sockaddr_in6)));
438 if (NULL != baddr->cryogenic_fd)
440 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
441 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
443 if (ioctl(baddr->cryogenic_fd->fd,
444 PM_SET_DELAY_AND_TIMEOUT,
445 &baddr->cryogenic_times) < 0)
447 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
448 baddr->broadcast_task =
449 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
450 &udp_ipv6_broadcast_send, baddr);
453 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
455 &udp_ipv4_broadcast_send,
460 baddr->broadcast_task =
461 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
462 &udp_ipv6_broadcast_send, baddr);
467 * Callback function invoked for each interface found.
469 * @param cls closure with the `struct Plugin`
470 * @param name name of the interface (can be NULL for unknown)
471 * @param isDefault is this presumably the default interface
472 * @param addr address of this interface (can be NULL for unknown or unassigned)
473 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
474 * @param netmask the network mask (can be NULL for unknown or unassigned)
475 * @param addrlen length of the address
476 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
479 iface_proc (void *cls,
482 const struct sockaddr *addr,
483 const struct sockaddr *broadcast_addr,
484 const struct sockaddr *netmask, socklen_t addrlen)
486 struct Plugin *plugin = cls;
487 struct BroadcastAddress *ba;
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "address %s for interface %s %p\n ",
493 GNUNET_a2s (addr, addrlen), name, addr);
494 if (NULL == broadcast_addr)
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "broadcast address %s for interface %s %p\n ",
498 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
500 GNUNET_a2s (netmask, addrlen), name, netmask);
502 ba = GNUNET_new (struct BroadcastAddress);
504 ba->addr = GNUNET_malloc (addrlen);
505 memcpy (ba->addr, broadcast_addr, addrlen);
506 ba->addrlen = addrlen;
507 if ( (GNUNET_YES == plugin->enable_ipv4) &&
508 (NULL != plugin->sockv4) &&
509 (addrlen == sizeof (struct sockaddr_in)) )
513 * setup Cryogenic FD for ipv4 broadcasting
517 GNUNET_asprintf (&filename,
520 if (0 == ACCESS (name, R_OK))
523 GNUNET_DISK_file_open (filename,
524 GNUNET_DISK_OPEN_WRITE,
525 GNUNET_DISK_PERM_NONE);
527 GNUNET_free (filename);
530 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
532 if ((GNUNET_YES == plugin->enable_ipv6) &&
533 (NULL != plugin->sockv6) &&
534 (addrlen == sizeof (struct sockaddr_in6)))
536 /* Create IPv6 multicast request */
537 struct ipv6_mreq multicastRequest;
538 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
540 multicastRequest.ipv6mr_multiaddr =
541 plugin->ipv6_multicast_address.sin6_addr;
542 /* http://tools.ietf.org/html/rfc2553#section-5.2:
546 * Join a multicast group on a specified local interface. If the
547 * interface index is specified as 0, the kernel chooses the local
548 * interface. For example, some kernels look up the multicast
549 * group in the normal IPv6 routing table and using the resulting
550 * interface; we do this for each interface, so no need to use
553 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
555 /* Join the multicast group */
557 GNUNET_NETWORK_socket_setsockopt
558 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
559 &multicastRequest, sizeof (multicastRequest)))
561 LOG (GNUNET_ERROR_TYPE_WARNING,
562 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
568 * setup Cryogenic FD for ipv6 broadcasting
572 GNUNET_asprintf (&filename,
575 if (0 == ACCESS (name, R_OK))
578 GNUNET_DISK_file_open (filename,
579 GNUNET_DISK_OPEN_WRITE,
580 GNUNET_DISK_PERM_NONE);
582 GNUNET_free (filename);
585 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
588 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
589 plugin->broadcast_tail, ba);
595 setup_broadcast (struct Plugin *plugin,
596 struct sockaddr_in6 *server_addrv6,
597 struct sockaddr_in *server_addrv4)
599 const struct GNUNET_MessageHeader *hello;
601 hello = plugin->env->get_our_hello ();
603 GNUNET_HELLO_is_friend_only ((const struct GNUNET_HELLO_Message *) hello))
605 LOG (GNUNET_ERROR_TYPE_WARNING,
606 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
610 /* always create tokenizers */
611 plugin->broadcast_ipv4_mst =
612 GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin);
613 plugin->broadcast_ipv6_mst =
614 GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
616 if (GNUNET_YES != plugin->enable_broadcasting)
617 return; /* We do not send, just receive */
619 /* create IPv4 broadcast socket */
620 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
624 if (GNUNET_NETWORK_socket_setsockopt
625 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
626 sizeof (int)) != GNUNET_OK)
628 LOG (GNUNET_ERROR_TYPE_WARNING,
629 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
630 ntohs (server_addrv4->sin_port));
633 /* create IPv6 multicast socket */
634 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
636 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
638 inet_pton (AF_INET6, "FF05::13B",
639 &plugin->ipv6_multicast_address.sin6_addr));
640 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
641 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
643 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
648 stop_broadcast (struct Plugin *plugin)
650 if (GNUNET_YES == plugin->enable_broadcasting)
652 /* Disable broadcasting */
653 while (plugin->broadcast_head != NULL)
655 struct BroadcastAddress *p = plugin->broadcast_head;
657 if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
659 GNUNET_SCHEDULER_cancel (p->broadcast_task);
660 p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
662 if ((GNUNET_YES == plugin->enable_ipv6) &&
663 (NULL != plugin->sockv6) &&
664 (p->addrlen == sizeof (struct sockaddr_in6)))
666 /* Create IPv6 multicast request */
667 struct ipv6_mreq multicastRequest;
668 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
670 multicastRequest.ipv6mr_multiaddr =
671 plugin->ipv6_multicast_address.sin6_addr;
672 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
674 /* Leave the multicast group */
676 GNUNET_NETWORK_socket_setsockopt
677 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
678 &multicastRequest, sizeof (multicastRequest)))
680 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
684 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
689 GNUNET_DISK_file_close(p->cryogenic_fd);
691 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
692 plugin->broadcast_tail, p);
693 GNUNET_free (p->addr);
699 if (NULL != plugin->broadcast_ipv4_mst)
701 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
702 plugin->broadcast_ipv4_mst = NULL;
704 if (NULL != plugin->broadcast_ipv6_mst)
706 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
707 plugin->broadcast_ipv6_mst = NULL;
711 /* end of plugin_transport_udp_broadcasting.c */