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;
60 /************************************/
63 struct UDP_Beacon_Message
68 struct GNUNET_MessageHeader header;
71 * What is the identity of the sender
73 struct GNUNET_PeerIdentity sender;
77 struct BroadcastAddress
79 struct BroadcastAddress *next;
81 struct BroadcastAddress *prev;
84 * ID of select broadcast task
86 GNUNET_SCHEDULER_TaskIdentifier broadcast_task;
88 struct Plugin *plugin;
97 struct GNUNET_DISK_FileHandle *cryogenic_fd;
99 struct pm_times cryogenic_times;
105 struct Plugin *plugin;
107 struct IPv4UdpAddress addr;
109 * ATS network type in NBO
111 uint32_t ats_address_network_type;
116 struct Plugin *plugin;
118 struct IPv6UdpAddress addr;
120 * ATS network type in NBO
122 uint32_t ats_address_network_type;
127 broadcast_ipv6_mst_cb (void *cls, void *client,
128 const struct GNUNET_MessageHeader *message)
130 struct Plugin *plugin = cls;
131 struct Mstv6Context *mc = client;
132 const struct GNUNET_MessageHeader *hello;
133 const struct UDP_Beacon_Message *msg;
134 struct GNUNET_ATS_Information atsi;
136 msg = (const struct UDP_Beacon_Message *) message;
138 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
139 ntohs (msg->header.type))
141 LOG (GNUNET_ERROR_TYPE_DEBUG,
142 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
143 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
144 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
147 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
148 atsi.value = mc->ats_address_network_type;
149 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
151 hello = (struct GNUNET_MessageHeader *) &msg[1];
152 plugin->env->receive (plugin->env->cls,
156 (const char *) &mc->addr,
158 plugin->env->update_address_metrics (plugin->env->cls,
160 (const char *) &mc->addr,
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 const struct GNUNET_MessageHeader *hello;
181 const struct UDP_Beacon_Message *msg;
182 struct GNUNET_ATS_Information atsi;
184 msg = (const struct UDP_Beacon_Message *) message;
186 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
187 ntohs (msg->header.type))
189 LOG (GNUNET_ERROR_TYPE_DEBUG,
190 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
191 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
192 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
196 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
197 atsi.value = mc->ats_address_network_type;
198 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
200 hello = (struct GNUNET_MessageHeader *) &msg[1];
201 plugin->env->receive (plugin->env->cls,
205 (const char *) &mc->addr,
208 plugin->env->update_address_metrics (plugin->env->cls,
210 (const char *) &mc->addr,
215 GNUNET_STATISTICS_update (plugin->env->stats,
216 _("# IPv4 broadcast HELLO beacons received via udp"),
224 udp_broadcast_receive (struct Plugin *plugin,
227 const struct sockaddr *addr,
230 struct GNUNET_ATS_Information ats;
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_malloc (sizeof (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 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
245 mc->ats_address_network_type = ats.value;
247 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
249 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
250 GNUNET_NO, GNUNET_NO))
253 if (addrlen == sizeof (struct sockaddr_in6))
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
257 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
258 struct Mstv6Context *mc;
260 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
261 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
263 mc->addr.ipv6_addr = av6->sin6_addr;
264 mc->addr.u6_port = av6->sin6_port;
265 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
266 mc->ats_address_network_type = ats.value;
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 = GNUNET_SCHEDULER_NO_TASK;
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));
347 if (NULL != baddr->cryogenic_fd)
349 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
350 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
352 if (ioctl(baddr->cryogenic_fd->fd,
353 PM_SET_DELAY_AND_TIMEOUT,
354 &baddr->cryogenic_times) < 0)
356 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
357 baddr->broadcast_task =
358 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
359 &udp_ipv4_broadcast_send, baddr);
362 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
364 &udp_ipv4_broadcast_send,
369 baddr->broadcast_task =
370 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
371 &udp_ipv4_broadcast_send, baddr);
376 udp_ipv6_broadcast_send (void *cls,
377 const struct GNUNET_SCHEDULER_TaskContext *tc)
379 struct BroadcastAddress *baddr = cls;
380 struct Plugin *plugin = baddr->plugin;
383 char buf[65536] GNUNET_ALIGN;
384 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
386 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
388 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
389 /* Note: unclear if this actually works to limit the multicast to
390 the specified interface as we're not (necessarily) using a
391 link-local multicast group and the kernel suggests that the
392 scope ID is only respected for link-local addresses; however,
393 if the scope ID is ignored, the kernel should just multicast
394 on ALL interfaces, which is merely slightly less efficient;
395 in that case, we might want to revert to only doing this
396 once, and not per interface (hard to test...) */
397 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
398 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
399 (const struct sockaddr *)
400 &plugin->ipv6_multicast_address,
401 sizeof (struct sockaddr_in6));
402 plugin->ipv6_multicast_address.sin6_scope_id = 0;
403 if (sent == GNUNET_SYSERR)
405 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
407 /* "Network unreachable" or "Network down"
409 * This indicates that this system is IPv6 enabled, but does not
410 * have a valid global IPv6 address assigned
412 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
413 "Network connectivity is down, cannot send beacon!\n");
416 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
420 LOG (GNUNET_ERROR_TYPE_DEBUG,
421 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
423 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
424 sizeof (struct sockaddr_in6)));
429 if (NULL != baddr->cryogenic_fd)
431 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
432 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
434 if (ioctl(baddr->cryogenic_fd->fd,
435 PM_SET_DELAY_AND_TIMEOUT,
436 &baddr->cryogenic_times) < 0)
438 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
439 baddr->broadcast_task =
440 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
441 &udp_ipv6_broadcast_send, baddr);
444 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
446 &udp_ipv4_broadcast_send,
450 baddr->broadcast_task =
451 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
452 &udp_ipv6_broadcast_send, baddr);
457 * Callback function invoked for each interface found.
459 * @param cls closure with the `struct Plugin`
460 * @param name name of the interface (can be NULL for unknown)
461 * @param isDefault is this presumably the default interface
462 * @param addr address of this interface (can be NULL for unknown or unassigned)
463 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
464 * @param netmask the network mask (can be NULL for unknown or unassigned)
465 * @param addrlen length of the address
466 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
469 iface_proc (void *cls,
472 const struct sockaddr *addr,
473 const struct sockaddr *broadcast_addr,
474 const struct sockaddr *netmask, socklen_t addrlen)
476 struct Plugin *plugin = cls;
477 struct BroadcastAddress *ba;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "address %s for interface %s %p\n ",
483 GNUNET_a2s (addr, addrlen), name, addr);
484 if (NULL == broadcast_addr)
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "broadcast address %s for interface %s %p\n ",
488 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
490 GNUNET_a2s (netmask, addrlen), name, netmask);
492 ba = GNUNET_new (struct BroadcastAddress);
494 ba->addr = GNUNET_malloc (addrlen);
495 memcpy (ba->addr, broadcast_addr, addrlen);
496 ba->addrlen = addrlen;
497 if ( (GNUNET_YES == plugin->enable_ipv4) &&
498 (NULL != plugin->sockv4) &&
499 (addrlen == sizeof (struct sockaddr_in)) )
503 * setup Cryogenic FD for ipv4 broadcasting
506 GNUNET_asprintf (&filename,
510 GNUNET_DISK_file_open (filename,
511 GNUNET_DISK_OPEN_WRITE,
512 GNUNET_DISK_PERM_NONE);
513 GNUNET_free (filename);
516 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
518 if ((GNUNET_YES == plugin->enable_ipv6) &&
519 (NULL != plugin->sockv6) &&
520 (addrlen == sizeof (struct sockaddr_in6)))
522 /* Create IPv6 multicast request */
523 struct ipv6_mreq multicastRequest;
524 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
526 multicastRequest.ipv6mr_multiaddr =
527 plugin->ipv6_multicast_address.sin6_addr;
528 /* http://tools.ietf.org/html/rfc2553#section-5.2:
532 * Join a multicast group on a specified local interface. If the
533 * interface index is specified as 0, the kernel chooses the local
534 * interface. For example, some kernels look up the multicast
535 * group in the normal IPv6 routing table and using the resulting
536 * interface; we do this for each interface, so no need to use
539 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
541 /* Join the multicast group */
543 GNUNET_NETWORK_socket_setsockopt
544 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
545 &multicastRequest, sizeof (multicastRequest)))
547 LOG (GNUNET_ERROR_TYPE_WARNING,
548 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "IPv6 multicasting running\n");
556 * setup Cryogenic FD for ipv6 broadcasting
559 GNUNET_asprintf (&filename,
563 GNUNET_DISK_file_open (filename,
564 GNUNET_DISK_OPEN_WRITE,
565 GNUNET_DISK_PERM_NONE);
566 GNUNET_free (filename);
570 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
573 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
574 plugin->broadcast_tail, ba);
580 setup_broadcast (struct Plugin *plugin,
581 struct sockaddr_in6 *server_addrv6,
582 struct sockaddr_in *server_addrv4)
584 const struct GNUNET_MessageHeader *hello;
586 hello = plugin->env->get_our_hello ();
587 if (GNUNET_YES == GNUNET_HELLO_is_friend_only((const struct GNUNET_HELLO_Message *) hello))
589 LOG (GNUNET_ERROR_TYPE_WARNING,
590 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
594 /* create IPv4 broadcast socket */
595 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
599 if (GNUNET_NETWORK_socket_setsockopt
600 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
601 sizeof (int)) != GNUNET_OK)
603 LOG (GNUNET_ERROR_TYPE_WARNING,
604 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
605 ntohs (server_addrv4->sin_port));
609 plugin->broadcast_ipv4_mst =
610 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
611 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
614 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
616 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
618 inet_pton (AF_INET6, "FF05::13B",
619 &plugin->ipv6_multicast_address.sin6_addr));
620 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
621 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
622 plugin->broadcast_ipv6_mst =
623 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
625 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
630 stop_broadcast (struct Plugin *plugin)
632 while (plugin->broadcast_head != NULL)
634 struct BroadcastAddress *p = plugin->broadcast_head;
636 if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
638 GNUNET_SCHEDULER_cancel (p->broadcast_task);
639 p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
641 if ((GNUNET_YES == plugin->enable_ipv6) &&
642 (NULL != plugin->sockv6) &&
643 (p->addrlen == sizeof (struct sockaddr_in6)))
645 /* Create IPv6 multicast request */
646 struct ipv6_mreq multicastRequest;
647 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
649 multicastRequest.ipv6mr_multiaddr =
650 plugin->ipv6_multicast_address.sin6_addr;
651 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
653 /* Leave the multicast group */
655 GNUNET_NETWORK_socket_setsockopt
656 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
657 &multicastRequest, sizeof (multicastRequest)))
659 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
670 GNUNET_DISK_file_close(p->cryogenic_fd);
672 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
673 plugin->broadcast_tail, p);
674 GNUNET_free (p->addr);
677 if (plugin->broadcast_ipv4_mst != NULL)
678 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
679 if (plugin->broadcast_ipv6_mst != NULL)
680 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
683 /* end of plugin_transport_udp_broadcasting.c */