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;
62 struct BroadcastAddress *prev;
71 struct Plugin *plugin;
73 struct IPv4UdpAddress addr;
75 * ATS network type in NBO
77 uint32_t ats_address_network_type;
82 struct Plugin *plugin;
84 struct IPv6UdpAddress addr;
86 * ATS network type in NBO
88 uint32_t ats_address_network_type;
93 broadcast_ipv6_mst_cb (void *cls, void *client,
94 const struct GNUNET_MessageHeader *message)
97 struct Plugin *plugin = cls;
98 struct Mstv6Context *mc = client;
99 const struct GNUNET_MessageHeader *hello;
100 const struct UDP_Beacon_Message *msg;
101 struct GNUNET_ATS_Information atsi;
103 msg = (const struct UDP_Beacon_Message *) message;
105 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
106 ntohs (msg->header.type))
108 LOG (GNUNET_ERROR_TYPE_DEBUG,
109 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
110 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
111 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
114 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
115 atsi.value = mc->ats_address_network_type;
116 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
118 hello = (struct GNUNET_MessageHeader *) &msg[1];
119 plugin->env->receive (plugin->env->cls,
123 (const char *) &mc->addr,
125 plugin->env->update_address_metrics (plugin->env->cls,
127 (const char *) &mc->addr,
132 GNUNET_STATISTICS_update (plugin->env->stats,
134 ("# IPv6 multicast HELLO beacons received via udp"),
142 broadcast_ipv4_mst_cb (void *cls, void *client,
143 const struct GNUNET_MessageHeader *message)
145 struct Plugin *plugin = cls;
146 struct Mstv4Context *mc = client;
147 const struct GNUNET_MessageHeader *hello;
148 const struct UDP_Beacon_Message *msg;
149 struct GNUNET_ATS_Information atsi;
151 msg = (const struct UDP_Beacon_Message *) message;
153 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
154 ntohs (msg->header.type))
156 LOG (GNUNET_ERROR_TYPE_DEBUG,
157 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
158 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
159 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
163 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
164 atsi.value = mc->ats_address_network_type;
165 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
167 hello = (struct GNUNET_MessageHeader *) &msg[1];
168 plugin->env->receive (plugin->env->cls,
172 (const char *) &mc->addr,
175 plugin->env->update_address_metrics (plugin->env->cls,
177 (const char *) &mc->addr,
182 GNUNET_STATISTICS_update (plugin->env->stats,
184 ("# IPv4 broadcast HELLO beacons received via udp"),
191 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
193 struct GNUNET_ATS_Information ats;
195 if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in)))
197 LOG (GNUNET_ERROR_TYPE_DEBUG,
198 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
199 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
200 struct Mstv4Context *mc;
202 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
203 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
205 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
206 mc->addr.u4_port = av4->sin_port;
207 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
208 mc->ats_address_network_type = ats.value;
210 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
212 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
213 GNUNET_NO, GNUNET_NO))
216 else if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in6)))
218 LOG (GNUNET_ERROR_TYPE_DEBUG,
219 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
220 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
221 struct Mstv6Context *mc;
223 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
224 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
226 mc->addr.ipv6_addr = av6->sin6_addr;
227 mc->addr.u6_port = av6->sin6_port;
228 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
229 mc->ats_address_network_type = ats.value;
230 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
232 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
233 GNUNET_NO, GNUNET_NO))
239 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
244 const struct GNUNET_MessageHeader *hello;
245 hello = plugin->env->get_our_hello ();
248 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
249 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
251 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
252 (msg_size > (UDP_MTU)))
255 msg->sender = *(plugin->env->my_identity);
256 msg->header.size = htons (msg_size);
257 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
258 memcpy (&msg[1], hello, hello_size);
263 udp_ipv4_broadcast_send (void *cls,
264 const struct GNUNET_SCHEDULER_TaskContext *tc)
266 struct Plugin *plugin = cls;
269 char buf[65536] GNUNET_ALIGN;
270 struct BroadcastAddress *baddr;
272 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
274 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
276 baddr = plugin->ipv4_broadcast_head;
278 while ((msg_size > 0) && (baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
280 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
282 addr->sin_port = htons (plugin->port);
284 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
285 (const struct sockaddr *) addr,
287 if (sent == GNUNET_SYSERR)
289 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
291 /* "Network unreachable" or "Network down"
293 * This indicates that we just do not have network connectivity
295 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
296 "Network connectivity is down, cannot send beacon!\n");
299 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
303 LOG (GNUNET_ERROR_TYPE_DEBUG,
304 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
305 GNUNET_a2s (baddr->addr, baddr->addrlen));
310 plugin->send_ipv4_broadcast_task =
311 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
312 &udp_ipv4_broadcast_send, plugin);
316 udp_ipv6_broadcast_send (void *cls,
317 const struct GNUNET_SCHEDULER_TaskContext *tc)
319 struct Plugin *plugin = cls;
322 char buf[65536] GNUNET_ALIGN;
324 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
326 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
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 if (sent == GNUNET_SYSERR)
334 if ((ENETUNREACH == errno) || (ENETDOWN == errno))
336 /* "Network unreachable" or "Network down"
338 * This indicates that this system is IPv6 enabled, but does not
339 * have a valid global IPv6 address assigned
341 GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
342 "Network connectivity is down, cannot send beacon!\n");
345 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
349 LOG (GNUNET_ERROR_TYPE_DEBUG,
350 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
352 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
353 sizeof (struct sockaddr_in6)));
355 plugin->send_ipv6_broadcast_task =
356 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
357 &udp_ipv6_broadcast_send, plugin);
362 iface_proc (void *cls, const char *name, int isDefault,
363 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
364 const struct sockaddr *netmask, socklen_t addrlen)
366 struct Plugin *plugin = cls;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
371 GNUNET_a2s (addr, addrlen), name, addr);
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "broadcast address %s for interface %s %p\n ",
374 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
376 GNUNET_a2s (netmask, addrlen), name, netmask);
378 /* Collecting broadcast addresses */
379 if (broadcast_addr != NULL)
381 struct BroadcastAddress *ba =
382 GNUNET_malloc (sizeof (struct BroadcastAddress));
383 ba->addr = GNUNET_malloc (addrlen);
384 memcpy (ba->addr, broadcast_addr, addrlen);
385 ba->addrlen = addrlen;
386 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
387 plugin->ipv4_broadcast_tail, ba);
395 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
397 const struct GNUNET_MessageHeader *hello;
398 hello = plugin->env->get_our_hello ();
400 if (GNUNET_YES == GNUNET_HELLO_is_friend_only((const struct GNUNET_HELLO_Message *) hello))
402 LOG (GNUNET_ERROR_TYPE_WARNING,
403 _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
408 /* create IPv4 broadcast socket */
409 plugin->broadcast_ipv4 = GNUNET_NO;
410 if (plugin->sockv4 != NULL)
414 if (GNUNET_NETWORK_socket_setsockopt
415 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
416 sizeof (int)) != GNUNET_OK)
418 LOG (GNUNET_ERROR_TYPE_WARNING,
420 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
421 ntohs (serverAddrv4->sin_port));
425 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
426 plugin->send_ipv4_broadcast_task =
427 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
429 plugin->broadcast_ipv4_mst =
430 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
432 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
433 plugin->broadcast_ipv4 = GNUNET_YES;
437 plugin->broadcast_ipv6 = GNUNET_NO;
438 if (plugin->sockv6 != NULL)
440 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
442 inet_pton (AF_INET6, "FF05::13B",
443 &plugin->ipv6_multicast_address.sin6_addr));
445 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
446 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
448 plugin->broadcast_ipv6_mst =
449 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
451 /* Create IPv6 multicast request */
452 struct ipv6_mreq multicastRequest;
454 multicastRequest.ipv6mr_multiaddr =
455 plugin->ipv6_multicast_address.sin6_addr;
456 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
458 * http://tools.ietf.org/html/rfc2553#section-5.2:
462 * Join a multicast group on a specified local interface. If the
463 * interface index is specified as 0, the kernel chooses the local
464 * interface. For example, some kernels look up the multicast
465 * group in the normal IPv6 routing table and using the resulting
468 multicastRequest.ipv6mr_interface = 0;
470 /* Join the multicast group */
471 if (GNUNET_NETWORK_socket_setsockopt
472 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
473 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
475 LOG (GNUNET_ERROR_TYPE_WARNING,
476 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
481 plugin->send_ipv6_broadcast_task =
482 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
483 plugin->broadcast_ipv6 = GNUNET_YES;
489 stop_broadcast (struct Plugin *plugin)
491 if (plugin->broadcast_ipv4)
493 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
495 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
496 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
499 if (plugin->broadcast_ipv4_mst != NULL)
500 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
502 while (plugin->ipv4_broadcast_head != NULL)
504 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
506 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
507 plugin->ipv4_broadcast_tail, p);
508 GNUNET_free (p->addr);
513 if (plugin->broadcast_ipv6)
515 /* Create IPv6 multicast request */
516 struct ipv6_mreq multicastRequest;
518 multicastRequest.ipv6mr_multiaddr =
519 plugin->ipv6_multicast_address.sin6_addr;
520 multicastRequest.ipv6mr_interface = 0;
522 /* Join the multicast address */
523 if (GNUNET_NETWORK_socket_setsockopt
524 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
525 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
527 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
531 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
534 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
536 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
537 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
539 if (plugin->broadcast_ipv6_mst != NULL)
540 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
545 /* end of plugin_transport_udp_broadcasting.c */