-fix time assertion introduce in last patch
[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   struct GNUNET_ATS_Information network;
477
478   if (NULL == addr)
479     return GNUNET_OK;
480   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481               "address %s for interface %s %p\n ",
482               GNUNET_a2s (addr, addrlen), name, addr);
483   if (NULL == broadcast_addr)
484     return GNUNET_OK;
485   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486               "broadcast address %s for interface %s %p\n ",
487               GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
488   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
489               GNUNET_a2s (netmask, addrlen), name, netmask);
490
491   network = plugin->env->get_address_type (plugin->env->cls, broadcast_addr, addrlen);
492   if (GNUNET_ATS_NET_LOOPBACK == ntohl(network.value))
493   {
494     /* Broadcasting on loopback does not make sense */
495     return GNUNET_YES;
496   }
497
498   ba = GNUNET_new (struct BroadcastAddress);
499   ba->plugin = plugin;
500   ba->addr = GNUNET_malloc (addrlen);
501   memcpy (ba->addr, broadcast_addr, addrlen);
502   ba->addrlen = addrlen;
503
504   if ( (GNUNET_YES == plugin->enable_ipv4) &&
505        (NULL != plugin->sockv4) &&
506        (addrlen == sizeof (struct sockaddr_in)) )
507   {
508 #if LINUX
509     /*
510      * setup Cryogenic FD for ipv4 broadcasting
511      */
512     char *filename;
513
514     GNUNET_asprintf (&filename,
515                      "/dev/cryogenic/%s",
516                      name);
517     if (0 == ACCESS (name, R_OK))
518     {
519       ba->cryogenic_fd =
520         GNUNET_DISK_file_open (filename,
521                                GNUNET_DISK_OPEN_WRITE,
522                                GNUNET_DISK_PERM_NONE);
523     }
524     GNUNET_free (filename);
525 #endif
526     ba->broadcast_task =
527         GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, ba);
528   }
529   if ((GNUNET_YES == plugin->enable_ipv6) &&
530       (NULL != plugin->sockv6) &&
531       (addrlen == sizeof (struct sockaddr_in6)))
532   {
533     /* Create IPv6 multicast request */
534     struct ipv6_mreq multicastRequest;
535     const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) broadcast_addr;
536
537     multicastRequest.ipv6mr_multiaddr =
538         plugin->ipv6_multicast_address.sin6_addr;
539     /* http://tools.ietf.org/html/rfc2553#section-5.2:
540      *
541      * IPV6_JOIN_GROUP
542      *
543      * Join a multicast group on a specified local interface.  If the
544      * interface index is specified as 0, the kernel chooses the local
545      * interface.  For example, some kernels look up the multicast
546      * group in the normal IPv6 routing table and using the resulting
547      * interface; we do this for each interface, so no need to use
548      * zero (anymore...).
549      */
550     multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
551
552     /* Join the multicast group */
553     if (GNUNET_OK !=
554         GNUNET_NETWORK_socket_setsockopt
555         (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
556          &multicastRequest, sizeof (multicastRequest)))
557     {
558       LOG (GNUNET_ERROR_TYPE_WARNING,
559       "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
560     }
561     else
562     {
563 #if LINUX
564       /*
565        * setup Cryogenic FD for ipv6 broadcasting
566        */
567       char *filename;
568
569       GNUNET_asprintf (&filename,
570                        "/dev/cryogenic/%s",
571                        name);
572       if (0 == ACCESS (name, R_OK))
573       {
574         ba->cryogenic_fd =
575           GNUNET_DISK_file_open (filename,
576                                  GNUNET_DISK_OPEN_WRITE,
577                                  GNUNET_DISK_PERM_NONE);
578       }
579       GNUNET_free (filename);
580 #endif
581       ba->broadcast_task =
582           GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, ba);
583     }
584   }
585   GNUNET_CONTAINER_DLL_insert (plugin->broadcast_head,
586                                plugin->broadcast_tail, ba);
587   return GNUNET_OK;
588 }
589
590
591 void
592 setup_broadcast (struct Plugin *plugin,
593                  struct sockaddr_in6 *server_addrv6,
594                  struct sockaddr_in *server_addrv4)
595 {
596   const struct GNUNET_MessageHeader *hello;
597
598   hello = plugin->env->get_our_hello ();
599   if (GNUNET_YES ==
600       GNUNET_HELLO_is_friend_only ((const struct GNUNET_HELLO_Message *) hello))
601   {
602     LOG (GNUNET_ERROR_TYPE_WARNING,
603          _("Disabling HELLO broadcasting due to friend-to-friend only configuration!\n"));
604     return;
605   }
606
607   /* always create tokenizers */
608   plugin->broadcast_ipv4_mst =
609     GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin);
610   plugin->broadcast_ipv6_mst =
611     GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
612
613   if (GNUNET_YES != plugin->enable_broadcasting)
614     return; /* We do not send, just receive */
615
616   /* create IPv4 broadcast socket */
617   if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
618   {
619     static int yes = 1;
620
621     if (GNUNET_NETWORK_socket_setsockopt
622         (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
623          sizeof (int)) != GNUNET_OK)
624     {
625       LOG (GNUNET_ERROR_TYPE_WARNING,
626            _("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
627            ntohs (server_addrv4->sin_port));
628     }
629   }
630   /* create IPv6 multicast socket */
631   if ((GNUNET_YES == plugin->enable_ipv6) && (plugin->sockv6 != NULL))
632   {
633     memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
634     GNUNET_assert (1 ==
635                    inet_pton (AF_INET6, "FF05::13B",
636                               &plugin->ipv6_multicast_address.sin6_addr));
637     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
638     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
639   }
640   GNUNET_OS_network_interfaces_list (&iface_proc, plugin);
641 }
642
643
644 void
645 stop_broadcast (struct Plugin *plugin)
646 {
647   if (GNUNET_YES == plugin->enable_broadcasting)
648   {
649     /* Disable broadcasting */
650     while (plugin->broadcast_head != NULL)
651     {
652       struct BroadcastAddress *p = plugin->broadcast_head;
653
654       if (p->broadcast_task != GNUNET_SCHEDULER_NO_TASK)
655       {
656         GNUNET_SCHEDULER_cancel (p->broadcast_task);
657         p->broadcast_task = GNUNET_SCHEDULER_NO_TASK;
658       }
659       if ((GNUNET_YES == plugin->enable_ipv6) &&
660           (NULL != plugin->sockv6) &&
661           (p->addrlen == sizeof (struct sockaddr_in6)))
662       {
663         /* Create IPv6 multicast request */
664         struct ipv6_mreq multicastRequest;
665         const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) p->addr;
666
667         multicastRequest.ipv6mr_multiaddr =
668           plugin->ipv6_multicast_address.sin6_addr;
669         multicastRequest.ipv6mr_interface = s6->sin6_scope_id;
670
671         /* Leave the multicast group */
672         if (GNUNET_OK ==
673             GNUNET_NETWORK_socket_setsockopt
674             (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
675              &multicastRequest, sizeof (multicastRequest)))
676         {
677           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
678         }
679         else
680         {
681           LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 multicasting stopped\n");
682         }
683       }
684
685 #if LINUX
686     GNUNET_DISK_file_close(p->cryogenic_fd);
687 #endif
688       GNUNET_CONTAINER_DLL_remove (plugin->broadcast_head,
689                                    plugin->broadcast_tail, p);
690       GNUNET_free (p->addr);
691       GNUNET_free (p);
692     }
693   }
694
695   /* Destroy MSTs */
696   if (NULL != plugin->broadcast_ipv4_mst)
697   {
698     GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
699     plugin->broadcast_ipv4_mst = NULL;
700   }
701   if (NULL != plugin->broadcast_ipv6_mst)
702   {
703     GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
704     plugin->broadcast_ipv6_mst = NULL;
705   }
706 }
707
708 /* end of plugin_transport_udp_broadcasting.c */