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, sizeof (mc->addr), GNUNET_HELLO_ADDRESS_INFO_INBOUND);
164 plugin->env->receive (plugin->env->cls, address, NULL, hello);
165 plugin->env->update_address_metrics (plugin->env->cls, address,
167 GNUNET_HELLO_address_free (address);
168 GNUNET_STATISTICS_update (plugin->env->stats,
170 ("# IPv6 multicast HELLO beacons received via udp"),
178 broadcast_ipv4_mst_cb (void *cls, void *client,
179 const struct GNUNET_MessageHeader *message)
181 struct Plugin *plugin = cls;
182 struct Mstv4Context *mc = client;
183 struct GNUNET_HELLO_Address *address;
184 const struct GNUNET_MessageHeader *hello;
185 const struct UDP_Beacon_Message *msg;
186 struct GNUNET_ATS_Information atsi;
188 msg = (const struct UDP_Beacon_Message *) message;
190 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
191 ntohs (msg->header.type))
193 LOG (GNUNET_ERROR_TYPE_DEBUG,
194 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
195 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
196 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
200 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
201 atsi.value = htonl (mc->ats_address_network_type);
202 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
204 hello = (struct GNUNET_MessageHeader *) &msg[1];
205 address = GNUNET_HELLO_address_allocate (&msg->sender, PLUGIN_NAME,
206 (const char *) &mc->addr, sizeof (mc->addr), GNUNET_HELLO_ADDRESS_INFO_INBOUND);
207 plugin->env->receive (plugin->env->cls, address, NULL, hello);
208 plugin->env->update_address_metrics (plugin->env->cls, address,
210 GNUNET_HELLO_address_free (address);
212 GNUNET_STATISTICS_update (plugin->env->stats,
213 _("# IPv4 broadcast HELLO beacons received via udp"),
221 udp_broadcast_receive (struct Plugin *plugin,
224 const struct sockaddr *addr,
227 if (addrlen == sizeof (struct sockaddr_in))
229 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
231 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
232 struct Mstv4Context *mc;
234 mc = GNUNET_new (struct Mstv4Context);
235 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
237 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
238 mc->addr.u4_port = av4->sin_port;
239 mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls,
240 (const struct sockaddr *) addr,
243 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
245 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
246 GNUNET_NO, GNUNET_NO))
249 if (addrlen == sizeof (struct sockaddr_in6))
251 LOG (GNUNET_ERROR_TYPE_DEBUG,
252 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
253 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
254 struct Mstv6Context *mc;
256 mc = GNUNET_new (struct Mstv6Context);
257 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
259 mc->addr.ipv6_addr = av6->sin6_addr;
260 mc->addr.u6_port = av6->sin6_port;
261 mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
262 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
264 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
265 GNUNET_NO, GNUNET_NO))
272 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
277 const struct GNUNET_MessageHeader *hello;
278 hello = plugin->env->get_our_hello ();
281 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
282 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
284 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
285 (msg_size > (UDP_MTU)))
288 msg->sender = *(plugin->env->my_identity);
289 msg->header.size = htons (msg_size);
290 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
291 memcpy (&msg[1], hello, hello_size);
297 udp_ipv4_broadcast_send (void *cls,
298 const struct GNUNET_SCHEDULER_TaskContext *tc)
300 struct BroadcastAddress *baddr = cls;
301 struct Plugin *plugin = baddr->plugin;
304 char buf[65536] GNUNET_ALIGN;
306 baddr->broadcast_task = NULL;
308 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
311 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
313 addr->sin_port = htons (plugin->port);
314 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
315 (const struct sockaddr *) addr,
317 if (sent == GNUNET_SYSERR)
319 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
321 /* "Network unreachable" or "Network down"
323 * This indicates that we just do not have network connectivity
325 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
326 "Network connectivity is down, cannot send beacon!\n");
329 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
335 GNUNET_a2s (baddr->addr, baddr->addrlen));
343 if (NULL != baddr->cryogenic_fd)
345 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
346 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
348 if (ioctl(baddr->cryogenic_fd->fd,
349 PM_SET_DELAY_AND_TIMEOUT,
350 &baddr->cryogenic_times) < 0)
352 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
353 baddr->broadcast_task =
354 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
355 &udp_ipv4_broadcast_send, baddr);
358 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
360 &udp_ipv4_broadcast_send,
366 baddr->broadcast_task =
367 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
368 &udp_ipv4_broadcast_send, baddr);
373 udp_ipv6_broadcast_send (void *cls,
374 const struct GNUNET_SCHEDULER_TaskContext *tc)
376 struct BroadcastAddress *baddr = cls;
377 struct Plugin *plugin = baddr->plugin;
380 char buf[65536] GNUNET_ALIGN;
381 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
383 baddr->broadcast_task = NULL;
385 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
386 /* Note: unclear if this actually works to limit the multicast to
387 the specified interface as we're not (necessarily) using a
388 link-local multicast group and the kernel suggests that the
389 scope ID is only respected for link-local addresses; however,
390 if the scope ID is ignored, the kernel should just multicast
391 on ALL interfaces, which is merely slightly less efficient;
392 in that case, we might want to revert to only doing this
393 once, and not per interface (hard to test...) */
394 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
395 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
396 (const struct sockaddr *)
397 &plugin->ipv6_multicast_address,
398 sizeof (struct sockaddr_in6));
399 plugin->ipv6_multicast_address.sin6_scope_id = 0;
400 if (sent == GNUNET_SYSERR)
402 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
404 /* "Network unreachable" or "Network down"
406 * This indicates that this system is IPv6 enabled, but does not
407 * have a valid global IPv6 address assigned
409 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
410 "Network connectivity is down, cannot send beacon!\n");
413 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
417 LOG (GNUNET_ERROR_TYPE_DEBUG,
418 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
420 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
421 sizeof (struct sockaddr_in6)));
427 if (NULL != baddr->cryogenic_fd)
429 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
430 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
432 if (ioctl(baddr->cryogenic_fd->fd,
433 PM_SET_DELAY_AND_TIMEOUT,
434 &baddr->cryogenic_times) < 0)
436 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
437 baddr->broadcast_task =
438 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
439 &udp_ipv6_broadcast_send, baddr);
442 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
444 &udp_ipv4_broadcast_send,
449 baddr->broadcast_task =
450 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
451 &udp_ipv6_broadcast_send, baddr);
456 * Callback function invoked for each interface found.
458 * @param cls closure with the `struct Plugin`
459 * @param name name of the interface (can be NULL for unknown)
460 * @param isDefault is this presumably the default interface
461 * @param addr address of this interface (can be NULL for unknown or unassigned)
462 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
463 * @param netmask the network mask (can be NULL for unknown or unassigned)
464 * @param addrlen length of the address
465 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
468 iface_proc (void *cls,
471 const struct sockaddr *addr,
472 const struct sockaddr *broadcast_addr,
473 const struct sockaddr *netmask, socklen_t addrlen)
475 struct Plugin *plugin = cls;
476 struct BroadcastAddress *ba;
477 enum GNUNET_ATS_Network_Type network;
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 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
493 if (GNUNET_ATS_NET_LOOPBACK == network)
495 /* Broadcasting on loopback does not make sense */
499 ba = GNUNET_new (struct BroadcastAddress);
501 ba->addr = GNUNET_malloc (addrlen);
502 memcpy (ba->addr, broadcast_addr, addrlen);
503 ba->addrlen = addrlen;
505 if ( (GNUNET_YES == plugin->enable_ipv4) &&
506 (NULL != plugin->sockv4) &&
507 (addrlen == sizeof (struct sockaddr_in)) )
511 * setup Cryogenic FD for ipv4 broadcasting
515 GNUNET_asprintf (&filename,
518 if (0 == ACCESS (name, R_OK))
521 GNUNET_DISK_file_open (filename,
522 GNUNET_DISK_OPEN_WRITE,
523 GNUNET_DISK_PERM_NONE);
525 GNUNET_free (filename);
528 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
530 if ((GNUNET_YES == plugin->enable_ipv6) &&
531 (NULL != plugin->sockv6) &&
532 (addrlen == sizeof (struct sockaddr_in6)))
534 /* Create IPv6 multicast request */
535 struct ipv6_mreq multicastRequest;
536 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
538 multicastRequest.ipv6mr_multiaddr =
539 plugin->ipv6_multicast_address.sin6_addr;
540 /* http://tools.ietf.org/html/rfc2553#section-5.2:
544 * Join a multicast group on a specified local interface. If the
545 * interface index is specified as 0, the kernel chooses the local
546 * interface. For example, some kernels look up the multicast
547 * group in the normal IPv6 routing table and using the resulting
548 * interface; we do this for each interface, so no need to use
551 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
553 /* Join the multicast group */
555 GNUNET_NETWORK_socket_setsockopt
556 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
557 &multicastRequest, sizeof (multicastRequest)))
559 LOG (GNUNET_ERROR_TYPE_WARNING,
560 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
566 * setup Cryogenic FD for ipv6 broadcasting
570 GNUNET_asprintf (&filename,
573 if (0 == ACCESS (name, R_OK))
576 GNUNET_DISK_file_open (filename,
577 GNUNET_DISK_OPEN_WRITE,
578 GNUNET_DISK_PERM_NONE);
580 GNUNET_free (filename);
583 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
586 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
587 plugin->broadcast_tail, ba);
593 setup_broadcast (struct Plugin *plugin,
594 struct sockaddr_in6 *server_addrv6,
595 struct sockaddr_in *server_addrv4)
597 const struct GNUNET_MessageHeader *hello;
599 hello = plugin->env->get_our_hello ();
601 GNUNET_HELLO_is_friend_only ((const struct GNUNET_HELLO_Message *) hello))
603 LOG (GNUNET_ERROR_TYPE_WARNING,
604 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
608 /* always create tokenizers */
609 plugin->broadcast_ipv4_mst =
610 GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin);
611 plugin->broadcast_ipv6_mst =
612 GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
614 if (GNUNET_YES != plugin->enable_broadcasting)
615 return; /* We do not send, just receive */
617 /* create IPv4 broadcast socket */
618 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
622 if (GNUNET_NETWORK_socket_setsockopt
623 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
624 sizeof (int)) != GNUNET_OK)
626 LOG (GNUNET_ERROR_TYPE_WARNING,
627 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
628 ntohs (server_addrv4->sin_port));
631 /* create IPv6 multicast socket */
632 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
634 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
636 inet_pton (AF_INET6, "FF05::13B",
637 &plugin->ipv6_multicast_address.sin6_addr));
638 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
639 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
641 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
646 stop_broadcast (struct Plugin *plugin)
648 if (GNUNET_YES == plugin->enable_broadcasting)
650 /* Disable broadcasting */
651 while (plugin->broadcast_head != NULL)
653 struct BroadcastAddress *p = plugin->broadcast_head;
655 if (p->broadcast_task != NULL)
657 GNUNET_SCHEDULER_cancel (p->broadcast_task);
658 p->broadcast_task = NULL;
660 if ((GNUNET_YES == plugin->enable_ipv6) &&
661 (NULL != plugin->sockv6) &&
662 (p->addrlen == sizeof (struct sockaddr_in6)))
664 /* Create IPv6 multicast request */
665 struct ipv6_mreq multicastRequest;
666 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
668 multicastRequest.ipv6mr_multiaddr =
669 plugin->ipv6_multicast_address.sin6_addr;
670 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
672 /* Leave the multicast group */
674 GNUNET_NETWORK_socket_setsockopt
675 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
676 &multicastRequest, sizeof (multicastRequest)))
678 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
682 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
687 GNUNET_DISK_file_close(p->cryogenic_fd);
689 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
690 plugin->broadcast_tail, p);
691 GNUNET_free (p->addr);
697 if (NULL != plugin->broadcast_ipv4_mst)
699 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
700 plugin->broadcast_ipv4_mst = NULL;
702 if (NULL != plugin->broadcast_ipv6_mst)
704 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
705 plugin->broadcast_ipv6_mst = NULL;
709 /* end of plugin_transport_udp_broadcasting.c */