hunting bugs
[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 int
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 GNUNET_OK;
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   return GNUNET_OK;
132 }
133
134 int
135 broadcast_ipv4_mst_cb (void *cls, void *client,
136                        const struct GNUNET_MessageHeader *message)
137 {
138   struct Plugin *plugin = cls;
139   struct Mstv4Context *mc = client;
140   const struct GNUNET_MessageHeader *hello;
141   struct UDP_Beacon_Message *msg;
142
143   msg = (struct UDP_Beacon_Message *) message;
144
145   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
146       ntohs (msg->header.type))
147     return GNUNET_OK;
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)));
152
153   struct GNUNET_ATS_Information atsi[2];
154
155   /* setup ATS */
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);
161
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));
166
167   GNUNET_STATISTICS_update (plugin->env->stats,
168                             _
169                             ("# IPv4 broadcast HELLO beacons received via udp"),
170                             1, GNUNET_NO);
171   GNUNET_free (mc);
172   return GNUNET_OK;
173 }
174
175 void
176 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
177 {
178   struct GNUNET_ATS_Information ats;
179
180   if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in)))
181   {
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;
186
187     mc = GNUNET_malloc (sizeof (struct Mstv4Context));
188     struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
189
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;
194
195     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
196     if (GNUNET_OK !=
197         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
198                                    GNUNET_NO, GNUNET_NO))
199       GNUNET_free (mc);
200   }
201   else if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in6)))
202   {
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;
207
208     mc = GNUNET_malloc (sizeof (struct Mstv6Context));
209     struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
210
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);
216     if (GNUNET_OK !=
217         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
218                                    GNUNET_NO, GNUNET_NO))
219       GNUNET_free (mc);
220   }
221 }
222
223 static unsigned int
224 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
225 {
226   uint16_t hello_size;
227   uint16_t msg_size;
228
229   const struct GNUNET_MessageHeader *hello;
230   hello = plugin->env->get_our_hello ();
231   if (NULL == hello)
232     return 0;
233   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
234   msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
235
236   if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
237       (msg_size > (UDP_MTU)))
238     return 0;
239
240   msg->sender = *(plugin->env->my_identity);
241   msg->header.size = htons (msg_size);
242   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
243   memcpy (&msg[1], hello, hello_size);
244   return msg_size;
245 }
246
247 static void
248 udp_ipv4_broadcast_send (void *cls,
249                          const struct GNUNET_SCHEDULER_TaskContext *tc)
250 {
251   struct Plugin *plugin = cls;
252   int sent;
253   uint16_t msg_size;
254   char buf[65536] GNUNET_ALIGN;
255   struct BroadcastAddress *baddr;
256
257   plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
258
259   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
260   sent = 0;
261   baddr = plugin->ipv4_broadcast_head;
262   /* just IPv4 */
263   while ((msg_size > 0) && (baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
264   {
265     struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
266
267     addr->sin_port = htons (plugin->port);
268
269     sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
270                                       (const struct sockaddr *) addr,
271                                       baddr->addrlen);
272     if (sent == GNUNET_SYSERR)
273     {
274       if ((ENETUNREACH == errno) || (ENETDOWN == errno))
275       {
276         /* "Network unreachable" or "Network down"
277          *
278          * This indicates that we just do not have network connectivity
279          */
280         GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
281             "Network connectivity is down, cannot send beacon!\n");
282       }
283       else
284         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
285     }
286     else
287     {
288       LOG (GNUNET_ERROR_TYPE_DEBUG,
289            "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
290            GNUNET_a2s (baddr->addr, baddr->addrlen));
291     }
292     baddr = baddr->next;
293   }
294
295   plugin->send_ipv4_broadcast_task =
296       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
297                                     &udp_ipv4_broadcast_send, plugin);
298 }
299
300 static void
301 udp_ipv6_broadcast_send (void *cls,
302                          const struct GNUNET_SCHEDULER_TaskContext *tc)
303 {
304   struct Plugin *plugin = cls;
305   int sent;
306   uint16_t msg_size;
307   char buf[65536] GNUNET_ALIGN;
308
309   plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
310
311   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
312   sent = 0;
313   sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
314                                     (const struct sockaddr *)
315                                     &plugin->ipv6_multicast_address,
316                                     sizeof (struct sockaddr_in6));
317   if (sent == GNUNET_SYSERR)
318   {
319     if ((ENETUNREACH == errno) || (ENETDOWN == errno))
320     {
321       /* "Network unreachable" or "Network down"
322        *
323        * This indicates that this system is IPv6 enabled, but does not
324        * have a valid global IPv6 address assigned
325        */
326       GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
327           "Network connectivity is down, cannot send beacon!\n");
328     }
329     else
330       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
331   }
332   else
333   {
334     LOG (GNUNET_ERROR_TYPE_DEBUG,
335          "Sending IPv6 HELLO beacon broadcast with  %i bytes to address %s\n",
336          sent,
337          GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
338                      sizeof (struct sockaddr_in6)));
339   }
340   plugin->send_ipv6_broadcast_task =
341       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
342                                     &udp_ipv6_broadcast_send, plugin);
343 }
344
345
346 static int
347 iface_proc (void *cls, const char *name, int isDefault,
348             const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
349             const struct sockaddr *netmask, socklen_t addrlen)
350 {
351   struct Plugin *plugin = cls;
352
353   if (addr != NULL)
354   {
355     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
356                 GNUNET_a2s (addr, addrlen), name, addr);
357     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358                 "broadcast address %s for interface %s %p\n ",
359                 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
361                 GNUNET_a2s (netmask, addrlen), name, netmask);
362
363     /* Collecting broadcast addresses */
364     if (broadcast_addr != NULL)
365     {
366       struct BroadcastAddress *ba =
367           GNUNET_malloc (sizeof (struct BroadcastAddress));
368       ba->addr = GNUNET_malloc (addrlen);
369       memcpy (ba->addr, broadcast_addr, addrlen);
370       ba->addrlen = addrlen;
371       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
372                                    plugin->ipv4_broadcast_tail, ba);
373     }
374   }
375   return GNUNET_OK;
376 }
377
378
379 void
380 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
381 {
382   /* create IPv4 broadcast socket */
383   plugin->broadcast_ipv4 = GNUNET_NO;
384   if (plugin->sockv4 != NULL)
385   {
386     int yes = 1;
387
388     if (GNUNET_NETWORK_socket_setsockopt
389         (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
390          sizeof (int)) != GNUNET_OK)
391     {
392       LOG (GNUNET_ERROR_TYPE_WARNING,
393            _
394            ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
395            ntohs (serverAddrv4->sin_port));
396     }
397     else
398     {
399       GNUNET_OS_network_interfaces_list (iface_proc, plugin);
400       plugin->send_ipv4_broadcast_task =
401           GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
402
403       plugin->broadcast_ipv4_mst =
404           GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
405
406       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
407       plugin->broadcast_ipv4 = GNUNET_YES;
408     }
409   }
410
411   plugin->broadcast_ipv6 = GNUNET_NO;
412   if (plugin->sockv6 != NULL)
413   {
414     memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
415     GNUNET_assert (1 ==
416                    inet_pton (AF_INET6, "FF05::13B",
417                               &plugin->ipv6_multicast_address.sin6_addr));
418
419     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
420     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
421
422     plugin->broadcast_ipv6_mst =
423         GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
424
425     /* Create IPv6 multicast request */
426     struct ipv6_mreq multicastRequest;
427
428     multicastRequest.ipv6mr_multiaddr =
429         plugin->ipv6_multicast_address.sin6_addr;
430     /* TODO: 0 selects the "best" interface, tweak to use all interfaces
431      *
432      * http://tools.ietf.org/html/rfc2553#section-5.2:
433      *
434      * IPV6_JOIN_GROUP
435      *
436      * Join a multicast group on a specified local interface.  If the
437      * interface index is specified as 0, the kernel chooses the local
438      * interface.  For example, some kernels look up the multicast
439      * group in the normal IPv6 routing table and using the resulting
440      * interface.
441      * */
442     multicastRequest.ipv6mr_interface = 0;
443
444     /* Join the multicast group */
445     if (GNUNET_NETWORK_socket_setsockopt
446         (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
447          (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
448     {
449       LOG (GNUNET_ERROR_TYPE_WARNING,
450       "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
451     }
452     else
453     {
454       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
455       plugin->send_ipv6_broadcast_task =
456           GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
457       plugin->broadcast_ipv6 = GNUNET_YES;
458     }
459   }
460 }
461
462 void
463 stop_broadcast (struct Plugin *plugin)
464 {
465   if (plugin->broadcast_ipv4)
466   {
467     if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
468     {
469       GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
470       plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
471     }
472
473     if (plugin->broadcast_ipv4_mst != NULL)
474       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
475
476     while (plugin->ipv4_broadcast_head != NULL)
477     {
478       struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
479
480       GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
481                                    plugin->ipv4_broadcast_tail, p);
482       GNUNET_free (p->addr);
483       GNUNET_free (p);
484     }
485   }
486
487   if (plugin->broadcast_ipv6)
488   {
489     /* Create IPv6 multicast request */
490     struct ipv6_mreq multicastRequest;
491
492     multicastRequest.ipv6mr_multiaddr =
493         plugin->ipv6_multicast_address.sin6_addr;
494     multicastRequest.ipv6mr_interface = 0;
495
496     /* Join the multicast address */
497     if (GNUNET_NETWORK_socket_setsockopt
498         (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
499         (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
500     {
501        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
502     }
503     else
504     {
505       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
506     }
507
508     if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
509     {
510       GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
511       plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
512     }
513     if (plugin->broadcast_ipv6_mst != NULL)
514       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
515   }
516
517 }
518
519 /* end of plugin_transport_udp_broadcasting.c */