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