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 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)));
112 struct GNUNET_ATS_Information atsi[2];
115 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
116 atsi[0].value = htonl (1);
117 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
118 atsi[1].value = mc->ats_address_network_type;
119 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
121 hello = (struct GNUNET_MessageHeader *) &msg[1];
122 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
123 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
124 (const char *) &mc->addr, sizeof (mc->addr));
126 GNUNET_STATISTICS_update (plugin->env->stats,
128 ("# IPv6 multicast HELLO beacons received via udp"),
134 broadcast_ipv4_mst_cb (void *cls, void *client,
135 const struct GNUNET_MessageHeader *message)
137 struct Plugin *plugin = cls;
138 struct Mstv4Context *mc = client;
139 const struct GNUNET_MessageHeader *hello;
140 struct UDP_Beacon_Message *msg;
142 msg = (struct UDP_Beacon_Message *) message;
144 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
145 ntohs (msg->header.type))
147 LOG (GNUNET_ERROR_TYPE_DEBUG,
148 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
149 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
150 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
152 struct GNUNET_ATS_Information atsi[2];
155 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
156 atsi[0].value = htonl (1);
157 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
158 atsi[1].value = 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 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
163 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
164 (const char *) &mc->addr, sizeof (mc->addr));
166 GNUNET_STATISTICS_update (plugin->env->stats,
168 ("# IPv4 broadcast HELLO beacons received via udp"),
174 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
176 struct GNUNET_ATS_Information ats;
178 if (addrlen == sizeof (struct sockaddr_in))
180 LOG (GNUNET_ERROR_TYPE_DEBUG,
181 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
182 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
183 struct Mstv4Context *mc;
185 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
186 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
188 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
189 mc->addr.u4_port = av4->sin_port;
190 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
191 mc->ats_address_network_type = ats.value;
193 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
194 GNUNET_NO, GNUNET_NO))
197 else if (addrlen == sizeof (struct sockaddr_in6))
199 LOG (GNUNET_ERROR_TYPE_DEBUG,
200 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
201 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
202 struct Mstv6Context *mc;
204 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
205 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
207 mc->addr.ipv6_addr = av6->sin6_addr;
208 mc->addr.u6_port = av6->sin6_port;
209 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
210 mc->ats_address_network_type = ats.value;
213 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
214 GNUNET_NO, GNUNET_NO))
220 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
225 const struct GNUNET_MessageHeader *hello;
226 hello = plugin->env->get_our_hello ();
227 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
228 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
230 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
231 (msg_size > (UDP_MTU)))
234 msg->sender = *(plugin->env->my_identity);
235 msg->header.size = htons (msg_size);
236 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
237 memcpy (&msg[1], hello, hello_size);
242 udp_ipv4_broadcast_send (void *cls,
243 const struct GNUNET_SCHEDULER_TaskContext *tc)
245 struct Plugin *plugin = cls;
248 char buf[65536] GNUNET_ALIGN;
249 struct BroadcastAddress *baddr;
251 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
253 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
255 baddr = plugin->ipv4_broadcast_head;
257 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
259 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
261 addr->sin_port = htons (plugin->port);
263 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
264 (const struct sockaddr *) addr,
266 if (sent == GNUNET_SYSERR)
267 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
272 GNUNET_a2s (baddr->addr, baddr->addrlen));
277 plugin->send_ipv4_broadcast_task =
278 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
279 &udp_ipv4_broadcast_send, plugin);
283 udp_ipv6_broadcast_send (void *cls,
284 const struct GNUNET_SCHEDULER_TaskContext *tc)
286 struct Plugin *plugin = cls;
289 char buf[65536] GNUNET_ALIGN;
291 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
293 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
295 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
296 (const struct sockaddr *)
297 &plugin->ipv6_multicast_address,
298 sizeof (struct sockaddr_in6));
299 if (sent == GNUNET_SYSERR)
300 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
303 LOG (GNUNET_ERROR_TYPE_DEBUG,
304 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
306 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
307 sizeof (struct sockaddr_in6)));
309 plugin->send_ipv6_broadcast_task =
310 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
311 &udp_ipv6_broadcast_send, plugin);
316 iface_proc (void *cls, const char *name, int isDefault,
317 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
318 const struct sockaddr *netmask, socklen_t addrlen)
320 struct Plugin *plugin = cls;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
325 GNUNET_a2s (addr, addrlen), name, addr);
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "broadcast address %s for interface %s %p\n ",
328 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
330 GNUNET_a2s (netmask, addrlen), name, netmask);
332 /* Collecting broadcast addresses */
333 if (broadcast_addr != NULL)
335 struct BroadcastAddress *ba =
336 GNUNET_malloc (sizeof (struct BroadcastAddress));
337 ba->addr = GNUNET_malloc (addrlen);
338 memcpy (ba->addr, broadcast_addr, addrlen);
339 ba->addrlen = addrlen;
340 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
341 plugin->ipv4_broadcast_tail, ba);
349 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
351 /* create IPv4 broadcast socket */
352 plugin->broadcast_ipv4 = GNUNET_NO;
353 if (plugin->sockv4 != NULL)
357 if (GNUNET_NETWORK_socket_setsockopt
358 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
359 sizeof (int)) != GNUNET_OK)
361 LOG (GNUNET_ERROR_TYPE_WARNING,
363 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
364 ntohs (serverAddrv4->sin_port));
368 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
369 plugin->send_ipv4_broadcast_task =
370 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
372 plugin->broadcast_ipv4_mst =
373 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
375 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
376 plugin->broadcast_ipv4 = GNUNET_YES;
380 plugin->broadcast_ipv6 = GNUNET_NO;
381 if (plugin->sockv6 != NULL)
383 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
385 inet_pton (AF_INET6, "FF05::13B",
386 &plugin->ipv6_multicast_address.sin6_addr));
388 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
389 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
391 plugin->broadcast_ipv6_mst =
392 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
394 /* Create IPv6 multicast request */
395 struct ipv6_mreq multicastRequest;
397 multicastRequest.ipv6mr_multiaddr =
398 plugin->ipv6_multicast_address.sin6_addr;
399 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
401 * http://tools.ietf.org/html/rfc2553#section-5.2:
405 * Join a multicast group on a specified local interface. If the
406 * interface index is specified as 0, the kernel chooses the local
407 * interface. For example, some kernels look up the multicast
408 * group in the normal IPv6 routing table and using the resulting
411 multicastRequest.ipv6mr_interface = 0;
413 /* Join the multicast group */
414 if (GNUNET_NETWORK_socket_setsockopt
415 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
416 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
418 LOG (GNUNET_ERROR_TYPE_WARNING,
419 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
423 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
424 plugin->send_ipv6_broadcast_task =
425 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
426 plugin->broadcast_ipv6 = GNUNET_YES;
432 stop_broadcast (struct Plugin *plugin)
434 if (plugin->broadcast_ipv4)
436 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
438 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
439 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
442 if (plugin->broadcast_ipv4_mst != NULL)
443 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
445 while (plugin->ipv4_broadcast_head != NULL)
447 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
449 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
450 plugin->ipv4_broadcast_tail, p);
451 GNUNET_free (p->addr);
456 if (plugin->broadcast_ipv6)
458 /* Create IPv6 multicast request */
459 struct ipv6_mreq multicastRequest;
461 multicastRequest.ipv6mr_multiaddr =
462 plugin->ipv6_multicast_address.sin6_addr;
463 multicastRequest.ipv6mr_interface = 0;
465 /* Join the multicast address */
466 if (GNUNET_NETWORK_socket_setsockopt
467 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
468 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
470 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
477 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
479 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
480 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
482 if (plugin->broadcast_ipv6_mst != NULL)
483 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
488 /* end of plugin_transport_udp_broadcasting.c */