-doxygen fixes, fix starting gnunet-service-resolver as it is no longer in /opt/jdk1...
[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
63   struct BroadcastAddress *prev;
64
65   /**
66    * ID of select broadcast task
67    */
68   GNUNET_SCHEDULER_TaskIdentifier broadcast_task;
69
70   struct Plugin *plugin;
71
72   void *addr;
73
74   socklen_t addrlen;
75 };
76
77
78 struct Mstv4Context
79 {
80   struct Plugin *plugin;
81
82   struct IPv4UdpAddress addr;
83   /**
84    * ATS network type in NBO
85    */
86   uint32_t ats_address_network_type;
87 };
88
89 struct Mstv6Context
90 {
91   struct Plugin *plugin;
92
93   struct IPv6UdpAddress addr;
94   /**
95    * ATS network type in NBO
96    */
97   uint32_t ats_address_network_type;
98 };
99
100
101 static int
102 broadcast_ipv6_mst_cb (void *cls, void *client,
103                        const struct GNUNET_MessageHeader *message)
104 {
105   struct Plugin *plugin = cls;
106   struct Mstv6Context *mc = client;
107   const struct GNUNET_MessageHeader *hello;
108   const struct UDP_Beacon_Message *msg;
109   struct GNUNET_ATS_Information atsi;
110
111   msg = (const struct UDP_Beacon_Message *) message;
112
113   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
114       ntohs (msg->header.type))
115     return GNUNET_OK;
116   LOG (GNUNET_ERROR_TYPE_DEBUG,
117        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
118        ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
119        udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
120
121   /* setup ATS */
122   atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
123   atsi.value = mc->ats_address_network_type;
124   GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
125
126   hello = (struct GNUNET_MessageHeader *) &msg[1];
127   plugin->env->receive (plugin->env->cls,
128                         &msg->sender,
129                         hello,
130                         NULL,
131                         (const char *) &mc->addr,
132                         sizeof (mc->addr));
133   plugin->env->update_address_metrics (plugin->env->cls,
134                                        &msg->sender,
135                                        (const char *) &mc->addr,
136                                        sizeof (mc->addr),
137                                        NULL,
138                                        &atsi, 1);
139
140   GNUNET_STATISTICS_update (plugin->env->stats,
141                             _
142                             ("# IPv6 multicast HELLO beacons received via udp"),
143                             1, GNUNET_NO);
144   GNUNET_free (mc);
145   return GNUNET_OK;
146 }
147
148
149 static int
150 broadcast_ipv4_mst_cb (void *cls, void *client,
151                        const struct GNUNET_MessageHeader *message)
152 {
153   struct Plugin *plugin = cls;
154   struct Mstv4Context *mc = client;
155   const struct GNUNET_MessageHeader *hello;
156   const struct UDP_Beacon_Message *msg;
157   struct GNUNET_ATS_Information atsi;
158
159   msg = (const struct UDP_Beacon_Message *) message;
160
161   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
162       ntohs (msg->header.type))
163     return GNUNET_OK;
164   LOG (GNUNET_ERROR_TYPE_DEBUG,
165        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
166        ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
167        udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
168
169
170   /* setup ATS */
171   atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
172   atsi.value = mc->ats_address_network_type;
173   GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
174
175   hello = (struct GNUNET_MessageHeader *) &msg[1];
176   plugin->env->receive (plugin->env->cls,
177                         &msg->sender,
178                         hello,
179                         NULL,
180                         (const char *) &mc->addr,
181                         sizeof (mc->addr));
182
183   plugin->env->update_address_metrics (plugin->env->cls,
184                                        &msg->sender,
185                                        (const char *) &mc->addr,
186                                        sizeof (mc->addr),
187                                        NULL,
188                                        &atsi, 1);
189
190   GNUNET_STATISTICS_update (plugin->env->stats,
191                             _("# IPv4 broadcast HELLO beacons received via udp"),
192                             1, GNUNET_NO);
193   GNUNET_free (mc);
194   return GNUNET_OK;
195 }
196
197
198 void
199 udp_broadcast_receive (struct Plugin *plugin,
200                        const char * buf,
201                        ssize_t size,
202                        const struct sockaddr *addr,
203                        size_t addrlen)
204 {
205   struct GNUNET_ATS_Information ats;
206
207   if (addrlen == sizeof (struct sockaddr_in))
208   {
209     LOG (GNUNET_ERROR_TYPE_DEBUG,
210          "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
211          size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
212     struct Mstv4Context *mc;
213
214     mc = GNUNET_malloc (sizeof (struct Mstv4Context));
215     struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
216
217     mc->addr.ipv4_addr = av4->sin_addr.s_addr;
218     mc->addr.u4_port = av4->sin_port;
219     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
220     mc->ats_address_network_type = ats.value;
221
222     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
223     if (GNUNET_OK !=
224         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
225                                    GNUNET_NO, GNUNET_NO))
226       GNUNET_free (mc);
227   }
228   if (addrlen == sizeof (struct sockaddr_in6))
229   {
230     LOG (GNUNET_ERROR_TYPE_DEBUG,
231          "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
232          size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
233     struct Mstv6Context *mc;
234
235     mc = GNUNET_malloc (sizeof (struct Mstv6Context));
236     struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
237
238     mc->addr.ipv6_addr = av6->sin6_addr;
239     mc->addr.u6_port = av6->sin6_port;
240     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
241     mc->ats_address_network_type = ats.value;
242     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
243     if (GNUNET_OK !=
244         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
245                                    GNUNET_NO, GNUNET_NO))
246       GNUNET_free (mc);
247   }
248 }
249
250
251 static unsigned int
252 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
253 {
254   uint16_t hello_size;
255   uint16_t msg_size;
256
257   const struct GNUNET_MessageHeader *hello;
258   hello = plugin->env->get_our_hello ();
259   if (NULL == hello)
260     return 0;
261   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
262   msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
263
264   if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
265       (msg_size > (UDP_MTU)))
266     return 0;
267
268   msg->sender = *(plugin->env->my_identity);
269   msg->header.size = htons (msg_size);
270   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
271   memcpy (&msg[1], hello, hello_size);
272   return msg_size;
273 }
274
275
276 static void
277 udp_ipv4_broadcast_send (void *cls,
278                          const struct GNUNET_SCHEDULER_TaskContext *tc)
279 {
280   struct BroadcastAddress *baddr = cls;
281   struct Plugin *plugin = baddr->plugin;
282   int sent;
283   uint16_t msg_size;
284   char buf[65536] GNUNET_ALIGN;
285
286   baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
287
288   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
289   if (0 != msg_size)
290   {
291     struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
292
293     addr->sin_port = htons (plugin->port);
294     sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
295                                       (const struct sockaddr *) addr,
296                                       baddr->addrlen);
297     if (sent == GNUNET_SYSERR)
298     {
299       if ((ENETUNREACH == errno) || (ENETDOWN == errno))
300       {
301         /* "Network unreachable" or "Network down"
302          *
303          * This indicates that we just do not have network connectivity
304          */
305         GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
306             "Network connectivity is down, cannot send beacon!\n");
307       }
308       else
309         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
310     }
311     else
312     {
313       LOG (GNUNET_ERROR_TYPE_DEBUG,
314            "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
315            GNUNET_a2s (baddr->addr, baddr->addrlen));
316     }
317   }
318   // if (-1 != baddr->cryogenic_fd) {
319   //   ioctl (baddr->cryogenic_fd, broadcast_interval +/- X)
320   //   GNUNET_SCHEDULER_add_write_file (baddr->cryogenic_fd, &udp_ipv4_broadcast_send, baddr);
321   // } else
322   baddr->broadcast_task =
323     GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
324                                   &udp_ipv4_broadcast_send, baddr);
325 }
326
327
328 static void
329 udp_ipv6_broadcast_send (void *cls,
330                          const struct GNUNET_SCHEDULER_TaskContext *tc)
331 {
332   struct BroadcastAddress *baddr = cls;
333   struct Plugin *plugin = baddr->plugin;
334   ssize_t sent;
335   uint16_t msg_size;
336   char buf[65536] GNUNET_ALIGN;
337   const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) baddr->addr;
338
339   baddr->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
340
341   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
342   /* Note: unclear if this actually works to limit the multicast to
343      the specified interface as we're not (necessarily) using a
344      link-local multicast group and the kernel suggests that the
345      scope ID is only respected for link-local addresses; however,
346      if the scope ID is ignored, the kernel should just multicast
347      on ALL interfaces, which is merely slightly less efficient;
348      in that case, we might want to revert to only doing this
349      once, and not per interface (hard to test...) */
350   plugin->ipv6_multicast_address.sin6_scope_id = s6->sin6_scope_id;
351   sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
352                                     (const struct sockaddr *)
353                                     &plugin->ipv6_multicast_address,
354                                     sizeof (struct sockaddr_in6));
355   plugin->ipv6_multicast_address.sin6_scope_id = 0;
356   if (sent == GNUNET_SYSERR)
357   {
358     if ((ENETUNREACH == errno) || (ENETDOWN == errno))
359     {
360       /* "Network unreachable" or "Network down"
361        *
362        * This indicates that this system is IPv6 enabled, but does not
363        * have a valid global IPv6 address assigned
364        */
365       GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
366           "Network connectivity is down, cannot send beacon!\n");
367     }
368     else
369       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
370   }
371   else
372   {
373     LOG (GNUNET_ERROR_TYPE_DEBUG,
374          "Sending IPv6 HELLO beacon broadcast with %d bytes to address %s\n",
375          (int) sent,
376          GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
377                      sizeof (struct sockaddr_in6)));
378   }
379   // cryogenic...
380   baddr->broadcast_task =
381     GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
382                                   &udp_ipv6_broadcast_send, baddr);
383 }
384
385
386 /**
387  * Callback function invoked for each interface found.
388  *
389  * @param cls closure with the `struct Plugin`
390  * @param name name of the interface (can be NULL for unknown)
391  * @param isDefault is this presumably the default interface
392  * @param addr address of this interface (can be NULL for unknown or unassigned)
393  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
394  * @param netmask the network mask (can be NULL for unknown or unassigned)
395  * @param addrlen length of the address
396  * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
397  */
398 static int
399 iface_proc (void *cls,
400             const char *name,
401             int isDefault,
402             const struct sockaddr *addr,
403             const struct sockaddr *broadcast_addr,
404             const struct sockaddr *netmask, socklen_t addrlen)
405 {
406   struct Plugin *plugin = cls;
407   struct BroadcastAddress *ba;
408
409   if (NULL == addr)
410     return GNUNET_OK;
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412               "address %s for interface %s %p\n ",
413               GNUNET_a2s (addr, addrlen), name, addr);
414   if (NULL == broadcast_addr)
415     return GNUNET_OK;
416   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417               "broadcast address %s for interface %s %p\n ",
418               GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
420               GNUNET_a2s (netmask, addrlen), name, netmask);
421
422   ba = GNUNET_new (struct BroadcastAddress);
423   ba->plugin = plugin;
424   ba->addr = GNUNET_malloc (addrlen);
425   memcpy (ba->addr, broadcast_addr, addrlen);
426   ba->addrlen = addrlen;
427   if ( (GNUNET_YES == plugin->enable_ipv4) &&
428        (NULL != plugin->sockv4) &&
429        (addrlen == sizeof (struct sockaddr_in)) )
430   {
431     char *filename;
432
433     GNUNET_asprintf (&filename,
434                      "/dev/power/net-%s",
435                      name);
436     // ba->cryogenic_fd = GNUNET_DISK_file_open (filename, ...);
437     GNUNET_free (filename);
438     ba->broadcast_task =
439       GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
440   }
441   if ((GNUNET_YES == plugin->enable_ipv6) &&
442       (NULL != plugin->sockv6) &&
443       (addrlen == sizeof (struct sockaddr_in6)))
444   {
445     /* Create IPv6 multicast request */
446     struct ipv6_mreq multicastRequest;
447     const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
448
449     multicastRequest.ipv6mr_multiaddr =
450         plugin->ipv6_multicast_address.sin6_addr;
451     /* http://tools.ietf.org/html/rfc2553#section-5.2:
452      *
453      * IPV6_JOIN_GROUP
454      *
455      * Join a multicast group on a specified local interface.  If the
456      * interface index is specified as 0, the kernel chooses the local
457      * interface.  For example, some kernels look up the multicast
458      * group in the normal IPv6 routing table and using the resulting
459      * interface; we do this for each interface, so no need to use
460      * zero (anymore...).
461      */
462     multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
463
464     /* Join the multicast group */
465     if (GNUNET_OK !=
466         GNUNET_NETWORK_socket_setsockopt
467         (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
468          &multicastRequest, sizeof (multicastRequest)))
469     {
470       LOG (GNUNET_ERROR_TYPE_WARNING,
471       "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
472     }
473     else
474     {
475       LOG (GNUNET_ERROR_TYPE_DEBUG,
476            "IPv6 multicasting running\n");
477       // setup cryogenic FD...
478       ba->broadcast_task =
479           GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
480     }
481   }
482   GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
483                                plugin->broadcast_tail, ba);
484   return GNUNET_OK;
485 }
486
487
488 void
489 setup_broadcast (struct Plugin *plugin,
490                  struct sockaddr_in6 *server_addrv6,
491                  struct sockaddr_in *server_addrv4)
492 {
493   const struct GNUNET_MessageHeader *hello;
494
495   hello = plugin->env->get_our_hello ();
496   if (GNUNET_YES == GNUNET_HELLO_is_friend_only((const struct GNUNET_HELLO_Message *) hello))
497   {
498     LOG (GNUNET_ERROR_TYPE_WARNING,
499          _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
500     return;
501   }
502
503   /* create IPv4 broadcast socket */
504   if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
505   {
506     static int yes = 1;
507
508     if (GNUNET_NETWORK_socket_setsockopt
509         (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
510          sizeof (int)) != GNUNET_OK)
511     {
512       LOG (GNUNET_ERROR_TYPE_WARNING,
513            _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
514            ntohs (server_addrv4->sin_port));
515     }
516     else
517     {
518       plugin->broadcast_ipv4_mst =
519           GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
520       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
521     }
522   }
523   if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
524   {
525     memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
526     GNUNET_assert (1 ==
527                    inet_pton (AF_INET6, "FF05::13B",
528                               &plugin->ipv6_multicast_address.sin6_addr));
529     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
530     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
531     plugin->broadcast_ipv6_mst =
532       GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
533   }
534   GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
535 }
536
537
538 void
539 stop_broadcast (struct Plugin *plugin)
540 {
541   while (plugin->broadcast_head != NULL)
542   {
543     struct BroadcastAddress *p = plugin->broadcast_head;
544
545     if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
546     {
547       GNUNET_SCHEDULER_cancel (p->broadcast_task);
548       p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
549     }
550     if ((GNUNET_YES == plugin->enable_ipv6) &&
551         (NULL != plugin->sockv6) &&
552         (p->addrlen == sizeof (struct sockaddr_in6)))
553     {
554       /* Create IPv6 multicast request */
555       struct ipv6_mreq multicastRequest;
556       const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
557
558       multicastRequest.ipv6mr_multiaddr =
559         plugin->ipv6_multicast_address.sin6_addr;
560       multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
561
562       /* Leave the multicast group */
563       if (GNUNET_OK ==
564           GNUNET_NETWORK_socket_setsockopt
565           (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
566            &multicastRequest, sizeof (multicastRequest)))
567       {
568         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
569       }
570       else
571       {
572         LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
573       }
574     }
575     // close cryogenic FD...
576     GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
577                                  plugin->broadcast_tail, p);
578     GNUNET_free (p->addr);
579     GNUNET_free (p);
580   }
581   if (plugin->broadcast_ipv4_mst != NULL)
582     GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
583   if (plugin->broadcast_ipv6_mst != NULL)
584     GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
585 }
586
587 /* end of plugin_transport_udp_broadcasting.c */