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