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"),
135 broadcast_ipv4_mst_cb (void *cls, void *client,
136 const struct GNUNET_MessageHeader *message)
138 struct Plugin *plugin = cls;
139 struct Mstv4Context *mc = client;
140 const struct GNUNET_MessageHeader *hello;
141 struct UDP_Beacon_Message *msg;
143 msg = (struct UDP_Beacon_Message *) message;
145 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
146 ntohs (msg->header.type))
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
150 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
151 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
153 struct GNUNET_ATS_Information atsi[2];
156 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
157 atsi[0].value = htonl (1);
158 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
159 atsi[1].value = mc->ats_address_network_type;
160 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
162 hello = (struct GNUNET_MessageHeader *) &msg[1];
163 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
164 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
165 (const char *) &mc->addr, sizeof (mc->addr));
167 GNUNET_STATISTICS_update (plugin->env->stats,
169 ("# IPv4 broadcast HELLO beacons received via udp"),
176 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
178 struct GNUNET_ATS_Information ats;
180 if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in)))
182 LOG (GNUNET_ERROR_TYPE_DEBUG,
183 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
184 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
185 struct Mstv4Context *mc;
187 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
188 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
190 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
191 mc->addr.u4_port = av4->sin_port;
192 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
193 mc->ats_address_network_type = ats.value;
195 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
197 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
198 GNUNET_NO, GNUNET_NO))
201 else if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in6)))
203 LOG (GNUNET_ERROR_TYPE_DEBUG,
204 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
205 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
206 struct Mstv6Context *mc;
208 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
209 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
211 mc->addr.ipv6_addr = av6->sin6_addr;
212 mc->addr.u6_port = av6->sin6_port;
213 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
214 mc->ats_address_network_type = ats.value;
215 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
217 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
218 GNUNET_NO, GNUNET_NO))
224 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
229 const struct GNUNET_MessageHeader *hello;
230 hello = plugin->env->get_our_hello ();
231 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
232 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
234 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
235 (msg_size > (UDP_MTU)))
238 msg->sender = *(plugin->env->my_identity);
239 msg->header.size = htons (msg_size);
240 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
241 memcpy (&msg[1], hello, hello_size);
246 udp_ipv4_broadcast_send (void *cls,
247 const struct GNUNET_SCHEDULER_TaskContext *tc)
249 struct Plugin *plugin = cls;
252 char buf[65536] GNUNET_ALIGN;
253 struct BroadcastAddress *baddr;
255 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
257 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
259 baddr = plugin->ipv4_broadcast_head;
261 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
263 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
265 addr->sin_port = htons (plugin->port);
267 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
268 (const struct sockaddr *) addr,
270 if (sent == GNUNET_SYSERR)
271 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
276 GNUNET_a2s (baddr->addr, baddr->addrlen));
281 plugin->send_ipv4_broadcast_task =
282 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
283 &udp_ipv4_broadcast_send, plugin);
287 udp_ipv6_broadcast_send (void *cls,
288 const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct Plugin *plugin = cls;
293 char buf[65536] GNUNET_ALIGN;
295 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
297 msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
299 sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
300 (const struct sockaddr *)
301 &plugin->ipv6_multicast_address,
302 sizeof (struct sockaddr_in6));
303 if (sent == GNUNET_SYSERR)
304 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
307 LOG (GNUNET_ERROR_TYPE_DEBUG,
308 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
310 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
311 sizeof (struct sockaddr_in6)));
313 plugin->send_ipv6_broadcast_task =
314 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
315 &udp_ipv6_broadcast_send, plugin);
320 iface_proc (void *cls, const char *name, int isDefault,
321 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
322 const struct sockaddr *netmask, socklen_t addrlen)
324 struct Plugin *plugin = cls;
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
329 GNUNET_a2s (addr, addrlen), name, addr);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "broadcast address %s for interface %s %p\n ",
332 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
334 GNUNET_a2s (netmask, addrlen), name, netmask);
336 /* Collecting broadcast addresses */
337 if (broadcast_addr != NULL)
339 struct BroadcastAddress *ba =
340 GNUNET_malloc (sizeof (struct BroadcastAddress));
341 ba->addr = GNUNET_malloc (addrlen);
342 memcpy (ba->addr, broadcast_addr, addrlen);
343 ba->addrlen = addrlen;
344 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
345 plugin->ipv4_broadcast_tail, ba);
353 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
355 /* create IPv4 broadcast socket */
356 plugin->broadcast_ipv4 = GNUNET_NO;
357 if (plugin->sockv4 != NULL)
361 if (GNUNET_NETWORK_socket_setsockopt
362 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
363 sizeof (int)) != GNUNET_OK)
365 LOG (GNUNET_ERROR_TYPE_WARNING,
367 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
368 ntohs (serverAddrv4->sin_port));
372 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
373 plugin->send_ipv4_broadcast_task =
374 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
376 plugin->broadcast_ipv4_mst =
377 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
379 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
380 plugin->broadcast_ipv4 = GNUNET_YES;
384 plugin->broadcast_ipv6 = GNUNET_NO;
385 if (plugin->sockv6 != NULL)
387 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
389 inet_pton (AF_INET6, "FF05::13B",
390 &plugin->ipv6_multicast_address.sin6_addr));
392 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
393 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
395 plugin->broadcast_ipv6_mst =
396 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
398 /* Create IPv6 multicast request */
399 struct ipv6_mreq multicastRequest;
401 multicastRequest.ipv6mr_multiaddr =
402 plugin->ipv6_multicast_address.sin6_addr;
403 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
405 * http://tools.ietf.org/html/rfc2553#section-5.2:
409 * Join a multicast group on a specified local interface. If the
410 * interface index is specified as 0, the kernel chooses the local
411 * interface. For example, some kernels look up the multicast
412 * group in the normal IPv6 routing table and using the resulting
415 multicastRequest.ipv6mr_interface = 0;
417 /* Join the multicast group */
418 if (GNUNET_NETWORK_socket_setsockopt
419 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
420 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
422 LOG (GNUNET_ERROR_TYPE_WARNING,
423 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
427 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
428 plugin->send_ipv6_broadcast_task =
429 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
430 plugin->broadcast_ipv6 = GNUNET_YES;
436 stop_broadcast (struct Plugin *plugin)
438 if (plugin->broadcast_ipv4)
440 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
442 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
443 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
446 if (plugin->broadcast_ipv4_mst != NULL)
447 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
449 while (plugin->ipv4_broadcast_head != NULL)
451 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
453 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
454 plugin->ipv4_broadcast_tail, p);
455 GNUNET_free (p->addr);
460 if (plugin->broadcast_ipv6)
462 /* Create IPv6 multicast request */
463 struct ipv6_mreq multicastRequest;
465 multicastRequest.ipv6mr_multiaddr =
466 plugin->ipv6_multicast_address.sin6_addr;
467 multicastRequest.ipv6mr_interface = 0;
469 /* Join the multicast address */
470 if (GNUNET_NETWORK_socket_setsockopt
471 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
472 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
474 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
478 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
481 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
483 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
484 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
486 if (plugin->broadcast_ipv6_mst != NULL)
487 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
492 /* end of plugin_transport_udp_broadcasting.c */