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__)
45 struct UDP_Beacon_Message
50 struct GNUNET_MessageHeader header;
53 * What is the identity of the sender
55 struct GNUNET_PeerIdentity sender;
59 struct BroadcastAddress
61 struct BroadcastAddress *next;
63 struct BroadcastAddress *prev;
66 * ID of select broadcast task
68 GNUNET_SCHEDULER_TaskIdentifier broadcast_task;
70 struct Plugin *plugin;
80 struct Plugin *plugin;
82 struct IPv4UdpAddress addr;
84 * ATS network type in NBO
86 uint32_t ats_address_network_type;
91 struct Plugin *plugin;
93 struct IPv6UdpAddress addr;
95 * ATS network type in NBO
97 uint32_t ats_address_network_type;
102 broadcast_ipv6_mst_cb (void *cls, void *client,
103 const struct GNUNET_MessageHeader *message)
105 struct Plugin *plugin = cls;
106 struct Mstv6Context *mc = client;
107 const struct GNUNET_MessageHeader *hello;
108 const struct UDP_Beacon_Message *msg;
109 struct GNUNET_ATS_Information atsi;
111 msg = (const struct UDP_Beacon_Message *) message;
113 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
114 ntohs (msg->header.type))
116 LOG (GNUNET_ERROR_TYPE_DEBUG,
117 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
118 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
119 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
122 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
123 atsi.value = mc->ats_address_network_type;
124 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
126 hello = (struct GNUNET_MessageHeader *) &msg[1];
127 plugin->env->receive (plugin->env->cls,
131 (const char *) &mc->addr,
133 plugin->env->update_address_metrics (plugin->env->cls,
135 (const char *) &mc->addr,
140 GNUNET_STATISTICS_update (plugin->env->stats,
142 ("# IPv6 multicast HELLO beacons received via udp"),
150 broadcast_ipv4_mst_cb (void *cls, void *client,
151 const struct GNUNET_MessageHeader *message)
153 struct Plugin *plugin = cls;
154 struct Mstv4Context *mc = client;
155 const struct GNUNET_MessageHeader *hello;
156 const struct UDP_Beacon_Message *msg;
157 struct GNUNET_ATS_Information atsi;
159 msg = (const struct UDP_Beacon_Message *) message;
161 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
162 ntohs (msg->header.type))
164 LOG (GNUNET_ERROR_TYPE_DEBUG,
165 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
166 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
167 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
171 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
172 atsi.value = mc->ats_address_network_type;
173 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
175 hello = (struct GNUNET_MessageHeader *) &msg[1];
176 plugin->env->receive (plugin->env->cls,
180 (const char *) &mc->addr,
183 plugin->env->update_address_metrics (plugin->env->cls,
185 (const char *) &mc->addr,
190 GNUNET_STATISTICS_update (plugin->env->stats,
191 _("# IPv4 broadcast HELLO beacons received via udp"),
199 udp_broadcast_receive (struct Plugin *plugin,
202 const struct sockaddr *addr,
205 struct GNUNET_ATS_Information ats;
207 if (addrlen == sizeof (struct sockaddr_in))
209 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
211 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
212 struct Mstv4Context *mc;
214 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
215 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
217 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
218 mc->addr.u4_port = av4->sin_port;
219 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
220 mc->ats_address_network_type = ats.value;
222 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
224 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
225 GNUNET_NO, GNUNET_NO))
228 if (addrlen == sizeof (struct sockaddr_in6))
230 LOG (GNUNET_ERROR_TYPE_DEBUG,
231 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
232 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
233 struct Mstv6Context *mc;
235 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
236 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
238 mc->addr.ipv6_addr = av6->sin6_addr;
239 mc->addr.u6_port = av6->sin6_port;
240 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
241 mc->ats_address_network_type = ats.value;
242 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
244 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
245 GNUNET_NO, GNUNET_NO))
252 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
257 const struct GNUNET_MessageHeader *hello;
258 hello = plugin->env->get_our_hello ();
261 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
262 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
264 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
265 (msg_size > (UDP_MTU)))
268 msg->sender = *(plugin->env->my_identity);
269 msg->header.size = htons (msg_size);
270 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
271 memcpy (&msg[1], hello, hello_size);
277 udp_ipv4_broadcast_send (void *cls,
278 const struct GNUNET_SCHEDULER_TaskContext *tc)
280 struct BroadcastAddress *baddr = cls;
281 struct Plugin *plugin = baddr->plugin;
284 char buf[65536] GNUNET_ALIGN;
286 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
288 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
291 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
293 addr->sin_port = htons (plugin->port);
294 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
295 (const struct sockaddr *) addr,
297 if (sent == GNUNET_SYSERR)
299 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
301 /* "Network unreachable" or "Network down"
303 * This indicates that we just do not have network connectivity
305 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
306 "Network connectivity is down, cannot send beacon!\n");
309 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
315 GNUNET_a2s (baddr->addr, baddr->addrlen));
318 // if (-1 != baddr->cryogenic_fd) {
319 // ioctl (baddr->cryogenic_fd, broadcast_interval +/- X)
320 // GNUNET_SCHEDULER_add_write_file (baddr->cryogenic_fd, &udp_ipv4_broadcast_send, baddr);
322 baddr->broadcast_task =
323 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
324 &udp_ipv4_broadcast_send, baddr);
329 udp_ipv6_broadcast_send (void *cls,
330 const struct GNUNET_SCHEDULER_TaskContext *tc)
332 struct BroadcastAddress *baddr = cls;
333 struct Plugin *plugin = baddr->plugin;
336 char buf[65536] GNUNET_ALIGN;
337 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
339 baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
341 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
342 /* Note: unclear if this actually works to limit the multicast to
343 the specified interface as we're not (necessarily) using a
344 link-local multicast group and the kernel suggests that the
345 scope ID is only respected for link-local addresses; however,
346 if the scope ID is ignored, the kernel should just multicast
347 on ALL interfaces, which is merely slightly less efficient;
348 in that case, we might want to revert to only doing this
349 once, and not per interface (hard to test...) */
350 plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
351 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
352 (const struct sockaddr *)
353 &plugin->ipv6_multicast_address,
354 sizeof (struct sockaddr_in6));
355 plugin->ipv6_multicast_address.sin6_scope_id = 0;
356 if (sent == GNUNET_SYSERR)
358 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
360 /* "Network unreachable" or "Network down"
362 * This indicates that this system is IPv6 enabled, but does not
363 * have a valid global IPv6 address assigned
365 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
366 "Network connectivity is down, cannot send beacon!\n");
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
376 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
377 sizeof (struct sockaddr_in6)));
380 baddr->broadcast_task =
381 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
382 &udp_ipv6_broadcast_send, baddr);
387 * Callback function invoked for each interface found.
389 * @param cls closure with the `struct Plugin`
390 * @param name name of the interface (can be NULL for unknown)
391 * @param isDefault is this presumably the default interface
392 * @param addr address of this interface (can be NULL for unknown or unassigned)
393 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
394 * @param netmask the network mask (can be NULL for unknown or unassigned)
395 * @param addrlen length of the address
396 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
399 iface_proc (void *cls,
402 const struct sockaddr *addr,
403 const struct sockaddr *broadcast_addr,
404 const struct sockaddr *netmask, socklen_t addrlen)
406 struct Plugin *plugin = cls;
407 struct BroadcastAddress *ba;
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "address %s for interface %s %p\n ",
413 GNUNET_a2s (addr, addrlen), name, addr);
414 if (NULL == broadcast_addr)
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "broadcast address %s for interface %s %p\n ",
418 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
420 GNUNET_a2s (netmask, addrlen), name, netmask);
422 ba = GNUNET_new (struct BroadcastAddress);
424 ba->addr = GNUNET_malloc (addrlen);
425 memcpy (ba->addr, broadcast_addr, addrlen);
426 ba->addrlen = addrlen;
427 if ( (GNUNET_YES == plugin->enable_ipv4) &&
428 (NULL != plugin->sockv4) &&
429 (addrlen == sizeof (struct sockaddr_in)) )
433 GNUNET_asprintf (&filename,
436 // ba->cryogenic_fd = GNUNET_DISK_file_open (filename, ...);
437 GNUNET_free (filename);
439 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
441 if ((GNUNET_YES == plugin->enable_ipv6) &&
442 (NULL != plugin->sockv6) &&
443 (addrlen == sizeof (struct sockaddr_in6)))
445 /* Create IPv6 multicast request */
446 struct ipv6_mreq multicastRequest;
447 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
449 multicastRequest.ipv6mr_multiaddr =
450 plugin->ipv6_multicast_address.sin6_addr;
451 /* http://tools.ietf.org/html/rfc2553#section-5.2:
455 * Join a multicast group on a specified local interface. If the
456 * interface index is specified as 0, the kernel chooses the local
457 * interface. For example, some kernels look up the multicast
458 * group in the normal IPv6 routing table and using the resulting
459 * interface; we do this for each interface, so no need to use
462 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
464 /* Join the multicast group */
466 GNUNET_NETWORK_socket_setsockopt
467 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
468 &multicastRequest, sizeof (multicastRequest)))
470 LOG (GNUNET_ERROR_TYPE_WARNING,
471 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "IPv6 multicasting running\n");
477 // setup cryogenic FD...
479 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
482 GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
483 plugin->broadcast_tail, ba);
489 setup_broadcast (struct Plugin *plugin,
490 struct sockaddr_in6 *server_addrv6,
491 struct sockaddr_in *server_addrv4)
493 const struct GNUNET_MessageHeader *hello;
495 hello = plugin->env->get_our_hello ();
496 if (GNUNET_YES == GNUNET_HELLO_is_friend_only((const struct GNUNET_HELLO_Message *) hello))
498 LOG (GNUNET_ERROR_TYPE_WARNING,
499 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
503 /* create IPv4 broadcast socket */
504 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
508 if (GNUNET_NETWORK_socket_setsockopt
509 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
510 sizeof (int)) != GNUNET_OK)
512 LOG (GNUNET_ERROR_TYPE_WARNING,
513 _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
514 ntohs (server_addrv4->sin_port));
518 plugin->broadcast_ipv4_mst =
519 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
520 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
523 if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
525 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
527 inet_pton (AF_INET6, "FF05::13B",
528 &plugin->ipv6_multicast_address.sin6_addr));
529 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
530 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
531 plugin->broadcast_ipv6_mst =
532 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
534 GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
539 stop_broadcast (struct Plugin *plugin)
541 while (plugin->broadcast_head != NULL)
543 struct BroadcastAddress *p = plugin->broadcast_head;
545 if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
547 GNUNET_SCHEDULER_cancel (p->broadcast_task);
548 p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
550 if ((GNUNET_YES == plugin->enable_ipv6) &&
551 (NULL != plugin->sockv6) &&
552 (p->addrlen == sizeof (struct sockaddr_in6)))
554 /* Create IPv6 multicast request */
555 struct ipv6_mreq multicastRequest;
556 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
558 multicastRequest.ipv6mr_multiaddr =
559 plugin->ipv6_multicast_address.sin6_addr;
560 multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
562 /* Leave the multicast group */
564 GNUNET_NETWORK_socket_setsockopt
565 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
566 &multicastRequest, sizeof (multicastRequest)))
568 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
572 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
575 // close cryogenic FD...
576 GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
577 plugin->broadcast_tail, p);
578 GNUNET_free (p->addr);
581 if (plugin->broadcast_ipv4_mst != NULL)
582 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
583 if (plugin->broadcast_ipv6_mst != NULL)
584 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
587 /* end of plugin_transport_udp_broadcasting.c */