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