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