2935d8da390adc74a8227564254f2a2128391dca
[oweals/gnunet.git] / src / transport / plugin_transport_udp_broadcasting.c
1 /*
2      This file is part of GNUnet
3      (C) 2010, 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/plugin_transport_udp_broadcasting.c
23  * @brief Neighbour discovery with UDP
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
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"
41
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
43
44
45 struct UDP_Beacon_Message
46 {
47  /**
48   * Message header.
49   */
50   struct GNUNET_MessageHeader header;
51
52  /**
53   * What is the identity of the sender
54   */
55   struct GNUNET_PeerIdentity sender;
56 };
57
58
59 struct BroadcastAddress
60 {
61   struct BroadcastAddress *next;
62   struct BroadcastAddress *prev;
63
64   void *addr;
65   socklen_t addrlen;
66 };
67
68
69 struct Mstv4Context
70 {
71   struct Plugin *plugin;
72
73   struct IPv4UdpAddress addr;
74   /**
75    * ATS network type in NBO
76    */
77   uint32_t ats_address_network_type;
78 };
79
80 struct Mstv6Context
81 {
82   struct Plugin *plugin;
83
84   struct IPv6UdpAddress addr;
85   /**
86    * ATS network type in NBO
87    */
88   uint32_t ats_address_network_type;
89 };
90
91
92
93 void
94 broadcast_ipv6_mst_cb (void *cls, void *client,
95                        const struct GNUNET_MessageHeader *message)
96 {
97
98   struct Plugin *plugin = cls;
99   struct Mstv6Context *mc = client;
100   const struct GNUNET_MessageHeader *hello;
101   struct UDP_Beacon_Message *msg;
102
103   msg = (struct UDP_Beacon_Message *) message;
104
105   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
106       ntohs (msg->header.type))
107     return;
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];
113
114   /* setup ATS */
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);
120
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));
125
126   GNUNET_STATISTICS_update (plugin->env->stats,
127                             _
128                             ("# IPv6 multicast HELLO beacons received via udp"),
129                             1, GNUNET_NO);
130   GNUNET_free (mc);
131 }
132
133 void
134 broadcast_ipv4_mst_cb (void *cls, void *client,
135                        const struct GNUNET_MessageHeader *message)
136 {
137   struct Plugin *plugin = cls;
138   struct Mstv4Context *mc = client;
139   const struct GNUNET_MessageHeader *hello;
140   struct UDP_Beacon_Message *msg;
141
142   msg = (struct UDP_Beacon_Message *) message;
143
144   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
145       ntohs (msg->header.type))
146     return;
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)));
151
152   struct GNUNET_ATS_Information atsi[2];
153
154   /* setup ATS */
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);
160
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));
165
166   GNUNET_STATISTICS_update (plugin->env->stats,
167                             _
168                             ("# IPv4 broadcast HELLO beacons received via udp"),
169                             1, GNUNET_NO);
170   GNUNET_free (mc);
171 }
172
173 void
174 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
175 {
176   struct GNUNET_ATS_Information ats;
177
178   if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in)))
179   {
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;
184
185     mc = GNUNET_malloc (sizeof (struct Mstv4Context));
186     struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
187
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;
192
193     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
194     if (GNUNET_OK !=
195         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
196                                    GNUNET_NO, GNUNET_NO))
197       GNUNET_free (mc);
198   }
199   else if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in6)))
200   {
201     LOG (GNUNET_ERROR_TYPE_DEBUG,
202          "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
203          size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
204     struct Mstv6Context *mc;
205
206     mc = GNUNET_malloc (sizeof (struct Mstv6Context));
207     struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
208
209     mc->addr.ipv6_addr = av6->sin6_addr;
210     mc->addr.u6_port = av6->sin6_port;
211     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
212     mc->ats_address_network_type = ats.value;
213     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
214     if (GNUNET_OK !=
215         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
216                                    GNUNET_NO, GNUNET_NO))
217       GNUNET_free (mc);
218   }
219 }
220
221 static unsigned int
222 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
223 {
224   uint16_t hello_size;
225   uint16_t msg_size;
226
227   const struct GNUNET_MessageHeader *hello;
228   hello = plugin->env->get_our_hello ();
229   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
230   msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
231
232   if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
233       (msg_size > (UDP_MTU)))
234     return 0;
235
236   msg->sender = *(plugin->env->my_identity);
237   msg->header.size = htons (msg_size);
238   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
239   memcpy (&msg[1], hello, hello_size);
240   return msg_size;
241 }
242
243 static void
244 udp_ipv4_broadcast_send (void *cls,
245                          const struct GNUNET_SCHEDULER_TaskContext *tc)
246 {
247   struct Plugin *plugin = cls;
248   int sent;
249   uint16_t msg_size;
250   char buf[65536] GNUNET_ALIGN;
251   struct BroadcastAddress *baddr;
252
253   plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
254
255   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
256   sent = 0;
257   baddr = plugin->ipv4_broadcast_head;
258   /* just IPv4 */
259   while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
260   {
261     struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
262
263     addr->sin_port = htons (plugin->port);
264
265     sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
266                                       (const struct sockaddr *) addr,
267                                       baddr->addrlen);
268     if (sent == GNUNET_SYSERR)
269       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
270     else
271     {
272       LOG (GNUNET_ERROR_TYPE_DEBUG,
273            "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
274            GNUNET_a2s (baddr->addr, baddr->addrlen));
275     }
276     baddr = baddr->next;
277   }
278
279   plugin->send_ipv4_broadcast_task =
280       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
281                                     &udp_ipv4_broadcast_send, plugin);
282 }
283
284 static void
285 udp_ipv6_broadcast_send (void *cls,
286                          const struct GNUNET_SCHEDULER_TaskContext *tc)
287 {
288   struct Plugin *plugin = cls;
289   int sent;
290   uint16_t msg_size;
291   char buf[65536] GNUNET_ALIGN;
292
293   plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
294
295   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
296   sent = 0;
297   sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
298                                     (const struct sockaddr *)
299                                     &plugin->ipv6_multicast_address,
300                                     sizeof (struct sockaddr_in6));
301   if (sent == GNUNET_SYSERR)
302     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
303   else
304   {
305     LOG (GNUNET_ERROR_TYPE_DEBUG,
306          "Sending IPv6 HELLO beacon broadcast with  %i bytes to address %s\n",
307          sent,
308          GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
309                      sizeof (struct sockaddr_in6)));
310   }
311   plugin->send_ipv6_broadcast_task =
312       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
313                                     &udp_ipv6_broadcast_send, plugin);
314 }
315
316
317 static int
318 iface_proc (void *cls, const char *name, int isDefault,
319             const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
320             const struct sockaddr *netmask, socklen_t addrlen)
321 {
322   struct Plugin *plugin = cls;
323
324   if (addr != NULL)
325   {
326     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
327                 GNUNET_a2s (addr, addrlen), name, addr);
328     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329                 "broadcast address %s for interface %s %p\n ",
330                 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
331     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
332                 GNUNET_a2s (netmask, addrlen), name, netmask);
333
334     /* Collecting broadcast addresses */
335     if (broadcast_addr != NULL)
336     {
337       struct BroadcastAddress *ba =
338           GNUNET_malloc (sizeof (struct BroadcastAddress));
339       ba->addr = GNUNET_malloc (addrlen);
340       memcpy (ba->addr, broadcast_addr, addrlen);
341       ba->addrlen = addrlen;
342       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
343                                    plugin->ipv4_broadcast_tail, ba);
344     }
345   }
346   return GNUNET_OK;
347 }
348
349
350 void
351 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
352 {
353   /* create IPv4 broadcast socket */
354   plugin->broadcast_ipv4 = GNUNET_NO;
355   if (plugin->sockv4 != NULL)
356   {
357     int yes = 1;
358
359     if (GNUNET_NETWORK_socket_setsockopt
360         (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
361          sizeof (int)) != GNUNET_OK)
362     {
363       LOG (GNUNET_ERROR_TYPE_WARNING,
364            _
365            ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
366            ntohs (serverAddrv4->sin_port));
367     }
368     else
369     {
370       GNUNET_OS_network_interfaces_list (iface_proc, plugin);
371       plugin->send_ipv4_broadcast_task =
372           GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
373
374       plugin->broadcast_ipv4_mst =
375           GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
376
377       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
378       plugin->broadcast_ipv4 = GNUNET_YES;
379     }
380   }
381
382   plugin->broadcast_ipv6 = GNUNET_NO;
383   if (plugin->sockv6 != NULL)
384   {
385     memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
386     GNUNET_assert (1 ==
387                    inet_pton (AF_INET6, "FF05::13B",
388                               &plugin->ipv6_multicast_address.sin6_addr));
389
390     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
391     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
392
393     plugin->broadcast_ipv6_mst =
394         GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
395
396     /* Create IPv6 multicast request */
397     struct ipv6_mreq multicastRequest;
398
399     multicastRequest.ipv6mr_multiaddr =
400         plugin->ipv6_multicast_address.sin6_addr;
401     /* TODO: 0 selects the "best" interface, tweak to use all interfaces
402      *
403      * http://tools.ietf.org/html/rfc2553#section-5.2:
404      *
405      * IPV6_JOIN_GROUP
406      *
407      * Join a multicast group on a specified local interface.  If the
408      * interface index is specified as 0, the kernel chooses the local
409      * interface.  For example, some kernels look up the multicast
410      * group in the normal IPv6 routing table and using the resulting
411      * interface.
412      * */
413     multicastRequest.ipv6mr_interface = 0;
414
415     /* Join the multicast group */
416     if (GNUNET_NETWORK_socket_setsockopt
417         (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
418          (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
419     {
420       LOG (GNUNET_ERROR_TYPE_WARNING,
421       "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
422     }
423     else
424     {
425       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
426       plugin->send_ipv6_broadcast_task =
427           GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
428       plugin->broadcast_ipv6 = GNUNET_YES;
429     }
430   }
431 }
432
433 void
434 stop_broadcast (struct Plugin *plugin)
435 {
436   if (plugin->broadcast_ipv4)
437   {
438     if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
439     {
440       GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
441       plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
442     }
443
444     if (plugin->broadcast_ipv4_mst != NULL)
445       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
446
447     while (plugin->ipv4_broadcast_head != NULL)
448     {
449       struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
450
451       GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
452                                    plugin->ipv4_broadcast_tail, p);
453       GNUNET_free (p->addr);
454       GNUNET_free (p);
455     }
456   }
457
458   if (plugin->broadcast_ipv6)
459   {
460     /* Create IPv6 multicast request */
461     struct ipv6_mreq multicastRequest;
462
463     multicastRequest.ipv6mr_multiaddr =
464         plugin->ipv6_multicast_address.sin6_addr;
465     multicastRequest.ipv6mr_interface = 0;
466
467     /* Join the multicast address */
468     if (GNUNET_NETWORK_socket_setsockopt
469         (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
470         (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
471     {
472        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
473     }
474     else
475     {
476       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
477     }
478
479     if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
480     {
481       GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
482       plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
483     }
484     if (plugin->broadcast_ipv6_mst != NULL)
485       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
486   }
487
488 }
489
490 /* end of plugin_transport_udp_broadcasting.c */