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;
94 broadcast_ipv6_mst_cb (void *cls, void *client,
95 const struct GNUNET_MessageHeader *message)
98 struct Plugin *plugin = cls;
99 struct Mstv6Context *mc = client;
100 const struct GNUNET_MessageHeader *hello;
101 struct UDP_Beacon_Message *msg;
103 msg = (struct UDP_Beacon_Message *) message;
105 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
106 ntohs (msg->header.type))
108 #if DEBUG_UDP_BROADCASTING
109 LOG (GNUNET_ERROR_TYPE_DEBUG,
110 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
111 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
112 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
114 struct GNUNET_ATS_Information atsi[2];
117 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
118 atsi[0].value = htonl (1);
119 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
120 atsi[1].value = mc->ats_address_network_type;
121 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
123 hello = (struct GNUNET_MessageHeader *) &msg[1];
124 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
125 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
126 (const char *) &mc->addr, sizeof (mc->addr));
128 GNUNET_STATISTICS_update (plugin->env->stats,
130 ("# IPv6 multicast HELLO beacons received via udp"),
136 broadcast_ipv4_mst_cb (void *cls, void *client,
137 const struct GNUNET_MessageHeader *message)
139 struct Plugin *plugin = cls;
140 struct Mstv4Context *mc = client;
141 const struct GNUNET_MessageHeader *hello;
142 struct UDP_Beacon_Message *msg;
144 msg = (struct UDP_Beacon_Message *) message;
146 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
147 ntohs (msg->header.type))
149 #if DEBUG_UDP_BROADCASTING
150 LOG (GNUNET_ERROR_TYPE_DEBUG,
151 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
152 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
153 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
156 struct GNUNET_ATS_Information atsi[2];
159 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
160 atsi[0].value = htonl (1);
161 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
162 atsi[1].value = mc->ats_address_network_type;
163 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
165 hello = (struct GNUNET_MessageHeader *) &msg[1];
166 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
167 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
168 (const char *) &mc->addr, sizeof (mc->addr));
170 GNUNET_STATISTICS_update (plugin->env->stats,
172 ("# IPv4 broadcast HELLO beacons received via udp"),
178 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
180 struct GNUNET_ATS_Information ats;
182 if (addrlen == sizeof (struct sockaddr_in))
184 #if DEBUG_UDP_BROADCASTING
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
187 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
189 struct Mstv4Context *mc;
191 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
192 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
194 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
195 mc->addr.u4_port = av4->sin_port;
196 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
197 mc->ats_address_network_type = ats.value;
199 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
200 GNUNET_NO, GNUNET_NO))
203 else if (addrlen == sizeof (struct sockaddr_in6))
205 #if DEBUG_UDP_BROADCASTING
206 LOG (GNUNET_ERROR_TYPE_DEBUG,
207 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
208 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
210 struct Mstv6Context *mc;
212 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
213 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
215 mc->addr.ipv6_addr = av6->sin6_addr;
216 mc->addr.u6_port = av6->sin6_port;
217 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
218 mc->ats_address_network_type = ats.value;
221 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
222 GNUNET_NO, GNUNET_NO))
228 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
233 const struct GNUNET_MessageHeader *hello;
234 hello = plugin->env->get_our_hello ();
235 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
236 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
238 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
239 (msg_size > (UDP_MTU)))
242 msg->sender = *(plugin->env->my_identity);
243 msg->header.size = htons (msg_size);
244 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
245 memcpy (&msg[1], hello, hello_size);
250 udp_ipv4_broadcast_send (void *cls,
251 const struct GNUNET_SCHEDULER_TaskContext *tc)
253 struct Plugin *plugin = cls;
256 char buf[65536] GNUNET_ALIGN;
257 struct BroadcastAddress *baddr;
259 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
261 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
263 baddr = plugin->ipv4_broadcast_head;
265 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
267 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
269 addr->sin_port = htons (plugin->port);
271 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
272 (const struct sockaddr *) addr,
274 if (sent == GNUNET_SYSERR)
275 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
280 GNUNET_a2s (baddr->addr, baddr->addrlen));
285 plugin->send_ipv4_broadcast_task =
286 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
287 &udp_ipv4_broadcast_send, plugin);
291 udp_ipv6_broadcast_send (void *cls,
292 const struct GNUNET_SCHEDULER_TaskContext *tc)
294 struct Plugin *plugin = cls;
297 char buf[65536] GNUNET_ALIGN;
299 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
301 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
303 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
304 (const struct sockaddr *)
305 &plugin->ipv6_multicast_address,
306 sizeof (struct sockaddr_in6));
307 if (sent == GNUNET_SYSERR)
308 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
311 LOG (GNUNET_ERROR_TYPE_DEBUG,
312 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
314 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
315 sizeof (struct sockaddr_in6)));
317 plugin->send_ipv6_broadcast_task =
318 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
319 &udp_ipv6_broadcast_send, plugin);
324 iface_proc (void *cls, const char *name, int isDefault,
325 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
326 const struct sockaddr *netmask, socklen_t addrlen)
328 struct Plugin *plugin = cls;
332 #if DEBUG_UDP_BROADCASTING
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
334 GNUNET_a2s (addr, addrlen), name, addr);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "broadcast address %s for interface %s %p\n ",
337 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
339 GNUNET_a2s (netmask, addrlen), name, netmask);
342 /* Collecting broadcast addresses */
343 if (broadcast_addr != NULL)
345 struct BroadcastAddress *ba =
346 GNUNET_malloc (sizeof (struct BroadcastAddress));
347 ba->addr = GNUNET_malloc (addrlen);
348 memcpy (ba->addr, broadcast_addr, addrlen);
349 ba->addrlen = addrlen;
350 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
351 plugin->ipv4_broadcast_tail, ba);
359 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
361 /* create IPv4 broadcast socket */
362 plugin->broadcast_ipv4 = GNUNET_NO;
363 if (plugin->sockv4 != NULL)
367 if (GNUNET_NETWORK_socket_setsockopt
368 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
369 sizeof (int)) != GNUNET_OK)
371 LOG (GNUNET_ERROR_TYPE_WARNING,
373 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
374 ntohs (serverAddrv4->sin_port));
378 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
379 plugin->send_ipv4_broadcast_task =
380 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
382 plugin->broadcast_ipv4_mst =
383 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
386 plugin->broadcast_ipv4 = GNUNET_YES;
390 plugin->broadcast_ipv6 = GNUNET_NO;
391 if (plugin->sockv6 != NULL)
393 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
395 inet_pton (AF_INET6, "FF05::13B",
396 &plugin->ipv6_multicast_address.sin6_addr));
398 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
399 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
401 plugin->broadcast_ipv6_mst =
402 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
404 /* Create IPv6 multicast request */
405 struct ipv6_mreq multicastRequest;
407 multicastRequest.ipv6mr_multiaddr =
408 plugin->ipv6_multicast_address.sin6_addr;
409 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
411 * http://tools.ietf.org/html/rfc2553#section-5.2:
415 * Join a multicast group on a specified local interface. If the
416 * interface index is specified as 0, the kernel chooses the local
417 * interface. For example, some kernels look up the multicast
418 * group in the normal IPv6 routing table and using the resulting
421 multicastRequest.ipv6mr_interface = 0;
423 /* Join the multicast group */
424 if (GNUNET_NETWORK_socket_setsockopt
425 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
426 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
428 LOG (GNUNET_ERROR_TYPE_WARNING,
429 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
434 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
436 plugin->send_ipv6_broadcast_task =
437 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
438 plugin->broadcast_ipv6 = GNUNET_YES;
444 stop_broadcast (struct Plugin *plugin)
446 if (plugin->broadcast_ipv4)
448 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
450 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
451 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
454 if (plugin->broadcast_ipv4_mst != NULL)
455 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
457 while (plugin->ipv4_broadcast_head != NULL)
459 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
461 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
462 plugin->ipv4_broadcast_tail, p);
463 GNUNET_free (p->addr);
468 if (plugin->broadcast_ipv6)
470 /* Create IPv6 multicast request */
471 struct ipv6_mreq multicastRequest;
473 multicastRequest.ipv6mr_multiaddr =
474 plugin->ipv6_multicast_address.sin6_addr;
475 multicastRequest.ipv6mr_interface = 0;
477 /* Join the multicast address */
478 if (GNUNET_NETWORK_socket_setsockopt
479 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
480 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
482 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
487 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
491 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
493 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
494 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
496 if (plugin->broadcast_ipv6_mst != NULL)
497 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
502 /* end of plugin_transport_udp_broadcasting.c */