2 This file is part of GNUnet
3 Copyright (C) 2010, 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file transport/plugin_transport_udp_broadcasting.c
18 * @brief Neighbour discovery with UDP
19 * @author Christian Grothoff
20 * @author Matthias Wachs
23 #include "plugin_transport_udp.h"
24 #include "gnunet_hello_lib.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_fragmentation_lib.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_resolver_service.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_transport_plugin.h"
34 #include "transport.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
38 /* *********** Cryogenic ********** */
43 #include <sys/ioctl.h>
44 #include <sys/select.h>
48 #define PM_SET_DELAY_AND_TIMEOUT _IOW(PM_MAGIC, 1, struct pm_times)
51 unsigned long delay_msecs;
52 unsigned long timeout_msecs;
55 /************************************/
58 struct UDP_Beacon_Message
63 struct GNUNET_MessageHeader header;
66 * What is the identity of the sender
68 struct GNUNET_PeerIdentity sender;
72 struct BroadcastAddress
74 struct BroadcastAddress *next;
76 struct BroadcastAddress *prev;
79 * ID of select broadcast task
81 struct GNUNET_SCHEDULER_Task * broadcast_task;
83 struct Plugin *plugin;
85 struct sockaddr *addr;
93 struct GNUNET_DISK_FileHandle *cryogenic_fd;
96 * Time out for cryogenic.
98 struct pm_times cryogenic_times;
104 * Client-specific context for #broadcast_mst_cb().
108 struct Plugin *plugin;
110 const union UdpAddress *udp_addr;
117 enum GNUNET_ATS_Network_Type ats_address_network_type;
122 * Parse broadcast message received.
124 * @param cls the `struct Plugin`
125 * @param client the `struct MstContext` with sender address
126 * @param message the message we received
127 * @return #GNUNET_OK (always)
130 broadcast_mst_cb (void *cls,
131 const struct GNUNET_MessageHeader *message)
133 struct MstContext *mc = cls;
134 struct Plugin *plugin = mc->plugin;
135 struct GNUNET_HELLO_Address *address;
136 const struct GNUNET_MessageHeader *hello;
137 const struct UDP_Beacon_Message *msg;
139 msg = (const struct UDP_Beacon_Message *) message;
141 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
142 ntohs (msg->header.type))
144 LOG (GNUNET_ERROR_TYPE_DEBUG,
145 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
146 ntohs (msg->header.size),
147 GNUNET_i2s (&msg->sender),
148 udp_address_to_string (NULL,
151 hello = (struct GNUNET_MessageHeader *) &msg[1];
152 address = GNUNET_HELLO_address_allocate (&msg->sender,
156 GNUNET_HELLO_ADDRESS_INFO_NONE);
157 plugin->env->receive (plugin->env->cls,
161 GNUNET_HELLO_address_free (address);
162 GNUNET_STATISTICS_update (plugin->env->stats,
163 _("# Multicast HELLO beacons received via UDP"),
170 * We received a broadcast message. Process it and all subsequent
171 * messages in the same packet.
173 * @param plugin the UDP plugin
174 * @param buf the buffer with the message(s)
175 * @param size number of bytes in @a buf
176 * @param udp_addr address of the sender
177 * @param udp_addr_len number of bytes in @a udp_addr
178 * @param network_type network type of the sender's address
181 udp_broadcast_receive (struct Plugin *plugin,
184 const union UdpAddress *udp_addr,
186 enum GNUNET_ATS_Network_Type network_type)
188 struct GNUNET_MessageStreamTokenizer *broadcast_mst;
189 struct MstContext mc;
191 broadcast_mst = GNUNET_MST_create (&broadcast_mst_cb,
194 mc.udp_addr = udp_addr;
195 mc.udp_addr_len = udp_addr_len;
196 mc.ats_address_network_type = network_type;
197 GNUNET_MST_from_buffer (broadcast_mst,
201 GNUNET_MST_destroy (broadcast_mst);
206 prepare_beacon (struct Plugin *plugin,
207 struct UDP_Beacon_Message *msg)
212 const struct GNUNET_MessageHeader *hello;
213 hello = plugin->env->get_our_hello ();
216 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
217 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
219 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
220 (msg_size > (UDP_MTU)))
223 msg->sender = *(plugin->env->my_identity);
224 msg->header.size = htons (msg_size);
225 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
226 GNUNET_memcpy (&msg[1], hello, hello_size);
232 udp_ipv4_broadcast_send (void *cls)
234 struct BroadcastAddress *baddr = cls;
235 struct Plugin *plugin = baddr->plugin;
238 char buf[65536] GNUNET_ALIGN;
240 baddr->broadcast_task = NULL;
242 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
245 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
247 addr->sin_port = htons (plugin->port);
248 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
249 (const struct sockaddr *) addr,
251 if (sent == GNUNET_SYSERR)
253 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
255 /* "Network unreachable" or "Network down"
257 * This indicates that we just do not have network connectivity
259 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
260 "Network connectivity is down, cannot send beacon!\n");
263 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
267 LOG (GNUNET_ERROR_TYPE_DEBUG,
268 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
269 GNUNET_a2s (baddr->addr, baddr->addrlen));
277 if (NULL != baddr->cryogenic_fd)
279 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
280 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
282 if (ioctl(baddr->cryogenic_fd->fd,
283 PM_SET_DELAY_AND_TIMEOUT,
284 &baddr->cryogenic_times) < 0)
286 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
287 baddr->broadcast_task =
288 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
289 &udp_ipv4_broadcast_send, baddr);
292 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
294 &udp_ipv4_broadcast_send,
300 baddr->broadcast_task =
301 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
302 &udp_ipv4_broadcast_send, baddr);
307 udp_ipv6_broadcast_send (void *cls)
309 struct BroadcastAddress *baddr = cls;
310 struct Plugin *plugin = baddr->plugin;
313 char buf[65536] GNUNET_ALIGN;
314 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
316 baddr->broadcast_task = NULL;
318 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
319 /* Note: unclear if this actually works to limit the multicast to
320 the specified interface as we're not (necessarily) using a
321 link-local multicast group and the kernel suggests that the
322 scope ID is only respected for link-local addresses; however,
323 if the scope ID is ignored, the kernel should just multicast
324 on ALL interfaces, which is merely slightly less efficient;
325 in that case, we might want to revert to only doing this
326 once, and not per interface (hard to test...) */
327 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
328 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
329 (const struct sockaddr *)
330 &plugin->ipv6_multicast_address,
331 sizeof (struct sockaddr_in6));
332 plugin->ipv6_multicast_address.sin6_scope_id = 0;
333 if (sent == GNUNET_SYSERR)
335 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
337 /* "Network unreachable" or "Network down"
339 * This indicates that this system is IPv6 enabled, but does not
340 * have a valid global IPv6 address assigned
342 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
343 "Network connectivity is down, cannot send beacon!\n");
346 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
350 LOG (GNUNET_ERROR_TYPE_DEBUG,
351 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
353 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
354 sizeof (struct sockaddr_in6)));
360 if (NULL != baddr->cryogenic_fd)
362 baddr->cryogenic_times.delay_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*0.5;
363 baddr->cryogenic_times.timeout_msecs = (plugin->broadcast_interval.rel_value_us/1000.0)*1.5;
365 if (ioctl(baddr->cryogenic_fd->fd,
366 PM_SET_DELAY_AND_TIMEOUT,
367 &baddr->cryogenic_times) < 0)
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
370 baddr->broadcast_task =
371 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
372 &udp_ipv6_broadcast_send, baddr);
375 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
377 &udp_ipv6_broadcast_send,
382 baddr->broadcast_task =
383 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
384 &udp_ipv6_broadcast_send, baddr);
389 * Callback function invoked for each interface found.
391 * @param cls closure with the `struct Plugin`
392 * @param name name of the interface (can be NULL for unknown)
393 * @param isDefault is this presumably the default interface
394 * @param addr address of this interface (can be NULL for unknown or unassigned)
395 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
396 * @param netmask the network mask (can be NULL for unknown or unassigned)
397 * @param addrlen length of the address
398 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
401 iface_proc (void *cls,
404 const struct sockaddr *addr,
405 const struct sockaddr *broadcast_addr,
406 const struct sockaddr *netmask, socklen_t addrlen)
408 struct Plugin *plugin = cls;
409 struct BroadcastAddress *ba;
410 enum GNUNET_ATS_Network_Type network;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "address %s for interface %s %p\n ",
416 GNUNET_a2s (addr, addrlen), name, addr);
417 if (NULL == broadcast_addr)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "broadcast address %s for interface %s %p\n ",
421 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
423 GNUNET_a2s (netmask, addrlen), name, netmask);
425 network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
426 if (GNUNET_ATS_NET_LOOPBACK == network)
428 /* Broadcasting on loopback does not make sense */
432 ba = GNUNET_new (struct BroadcastAddress);
434 ba->addr = GNUNET_malloc (addrlen);
435 GNUNET_memcpy (ba->addr, broadcast_addr, addrlen);
436 ba->addrlen = addrlen;
438 if ( (GNUNET_YES == plugin->enable_ipv4) &&
439 (NULL != plugin->sockv4) &&
440 (addrlen == sizeof (struct sockaddr_in)) )
444 * setup Cryogenic FD for ipv4 broadcasting
448 GNUNET_asprintf (&filename,
451 if (0 == ACCESS (name, R_OK))
454 GNUNET_DISK_file_open (filename,
455 GNUNET_DISK_OPEN_WRITE,
456 GNUNET_DISK_PERM_NONE);
458 GNUNET_free (filename);
461 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
463 if ((GNUNET_YES == plugin->enable_ipv6) &&
464 (NULL != plugin->sockv6) &&
465 (addrlen == sizeof (struct sockaddr_in6)))
467 /* Create IPv6 multicast request */
468 struct ipv6_mreq multicastRequest;
469 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
471 multicastRequest.ipv6mr_multiaddr =
472 plugin->ipv6_multicast_address.sin6_addr;
473 /* http://tools.ietf.org/html/rfc2553#section-5.2:
477 * Join a multicast group on a specified local interface. If the
478 * interface index is specified as 0, the kernel chooses the local
479 * interface. For example, some kernels look up the multicast
480 * group in the normal IPv6 routing table and using the resulting
481 * interface; we do this for each interface, so no need to use
484 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
486 /* Join the multicast group */
488 GNUNET_NETWORK_socket_setsockopt
489 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
490 &multicastRequest, sizeof (multicastRequest)))
492 LOG (GNUNET_ERROR_TYPE_WARNING,
493 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
499 * setup Cryogenic FD for ipv6 broadcasting
503 GNUNET_asprintf (&filename,
506 if (0 == ACCESS (name, R_OK))
509 GNUNET_DISK_file_open (filename,
510 GNUNET_DISK_OPEN_WRITE,
511 GNUNET_DISK_PERM_NONE);
513 GNUNET_free (filename);
516 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
519 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
520 plugin->broadcast_tail, ba);
526 * Setup broadcasting subsystem.
529 * @param server_addrv6
530 * @param server_addrv4
533 setup_broadcast (struct Plugin *plugin,
534 struct sockaddr_in6 *server_addrv6,
535 struct sockaddr_in *server_addrv4)
538 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
542 LOG (GNUNET_ERROR_TYPE_WARNING,
543 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
547 if (GNUNET_YES != plugin->enable_broadcasting)
548 return; /* We do not send, just receive */
550 /* create IPv4 broadcast socket */
551 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
555 if (GNUNET_NETWORK_socket_setsockopt
556 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
557 sizeof (int)) != GNUNET_OK)
559 LOG (GNUNET_ERROR_TYPE_WARNING,
560 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
561 ntohs (server_addrv4->sin_port));
564 /* create IPv6 multicast socket */
565 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
567 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
569 inet_pton (AF_INET6, "FF05::13B",
570 &plugin->ipv6_multicast_address.sin6_addr));
571 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
572 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
574 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
579 * Stop broadcasting subsystem.
584 stop_broadcast (struct Plugin *plugin)
586 if (GNUNET_YES == plugin->enable_broadcasting)
588 /* Disable broadcasting */
589 while (plugin->broadcast_head != NULL)
591 struct BroadcastAddress *p = plugin->broadcast_head;
593 if (p->broadcast_task != NULL)
595 GNUNET_SCHEDULER_cancel (p->broadcast_task);
596 p->broadcast_task = NULL;
598 if ((GNUNET_YES == plugin->enable_ipv6) &&
599 (NULL != plugin->sockv6) &&
600 (p->addrlen == sizeof (struct sockaddr_in6)))
602 /* Create IPv6 multicast request */
603 struct ipv6_mreq multicastRequest;
604 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
606 multicastRequest.ipv6mr_multiaddr =
607 plugin->ipv6_multicast_address.sin6_addr;
608 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
610 /* Leave the multicast group */
612 GNUNET_NETWORK_socket_setsockopt
613 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
614 &multicastRequest, sizeof (multicastRequest)))
616 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
625 GNUNET_DISK_file_close(p->cryogenic_fd);
627 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
628 plugin->broadcast_tail, p);
629 GNUNET_free (p->addr);
635 /* end of plugin_transport_udp_broadcasting.c */