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