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