[avahi] Re: [avahi-commits] r646 - in /trunk: avahi-core/core.h avahi-core/server.c avahi-core/socket.c avahi-core/socket.h avahi-daemon/avahi-daemon.conf avahi-daemon/main.c docs/TODO

Marc Krochmal marc at apple.com
Wed Sep 28 18:00:53 PDT 2005


Hey Lennart,

I can't say that this check-in makes me happy.  :-(

You should always be setting SO_REUSEADDR.  In fact, this is a MUST  
in the spec.

    Continuing the previous point, since using an unprivileged port
    allows normal user-level code to bind, a given machine may have more
    than one such user-level application running at a time. Because of
    this, any code binding to UDP port 5353 MUST use the SO_REUSEPORT
    option, so as to be a good citizen and not block other clients on  
the
    machine from also binding to that port.

I even responded to someone on the Bonjour list who was asking  
questions about the comments on the Avahi web site, where it says you  
must not run multiple responders on the same machine.  I told him  
that running Avahi, Howl, and Bonjour on the same machine will work  
fine in reality, which is true.

Could you at least set SO_REUSEADDR by default?

-Marc




On Sep 28, 2005, at 4:49 PM, svnmailer-noreply at 0pointer.de wrote:

> Author: lennart
> Date: Thu Sep 29 01:49:09 2005
> New Revision: 646
>
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi? 
> rev=646&root=avahi&view=rev
> Log:
> * add option to not enable SO_REUSEADDR for multicast sockets,  
> effectively disallow multiple mDNS stacks running simultaneously
> * add a config file option for this and enable it by default
> * remove some bad \n in log messages
>
> Modified:
>     trunk/avahi-core/core.h
>     trunk/avahi-core/server.c
>     trunk/avahi-core/socket.c
>     trunk/avahi-core/socket.h
>     trunk/avahi-daemon/avahi-daemon.conf
>     trunk/avahi-daemon/main.c
>     trunk/docs/TODO
>
> Modified: trunk/avahi-core/core.h
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/avahi-core/core.h? 
> rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/avahi-core/core.h (original)
> +++ trunk/avahi-core/core.h Thu Sep 29 01:49:09 2005
> @@ -80,6 +80,7 @@
>      int enable_wide_area;             /**< Enable wide area  
> support */
>      AvahiAddress wide_area_servers[AVAHI_MAX_WIDE_AREA_SERVERS]; / 
> ** Unicast DNS server to use for wide area lookup */
>      unsigned n_wide_area_servers;     /**< Number of servers in  
> wide_area_servers[] */
> +    int disallow_other_stacks;        /**< Make sure that only one  
> mDNS responder is run at the same time on the local machine. If  
> this is enable Avahi will not set SO_REUSADDR on its sockets,  
> effectively preventing other stacks from running on the local  
> machine */
>  } AvahiServerConfig;
>
>  /** Allocate a new mDNS responder object. */
>
> Modified: trunk/avahi-core/server.c
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/avahi-core/ 
> server.c?rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/avahi-core/server.c (original)
> +++ trunk/avahi-core/server.c Thu Sep 29 01:49:09 2005
> @@ -1324,8 +1324,8 @@
>  static int setup_sockets(AvahiServer *s) {
>      assert(s);
>
> -    s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1;
> -    s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1;
> +    s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4(s- 
> >config.disallow_other_stacks) : -1;
> +    s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6(s- 
> >config.disallow_other_stacks) : -1;
>
>      if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0)
>          return AVAHI_ERR_NO_NETWORK;
> @@ -2446,6 +2446,7 @@
>      c->add_service_cookie = 1;
>      c->enable_wide_area = 0;
>      c->n_wide_area_servers = 0;
> +    c->disallow_other_stacks = 0;
>
>      return c;
>  }
>
> Modified: trunk/avahi-core/socket.c
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/avahi-core/ 
> socket.c?rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/avahi-core/socket.c (original)
> +++ trunk/avahi-core/socket.c Thu Sep 29 01:49:09 2005
> @@ -93,7 +93,7 @@
>      mreq.imr_ifindex = idx;
>
>      if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof 
> (mreq)) < 0) {
> -        avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s", strerror 
> (errno));
>          return -1;
>      }
>
> @@ -111,7 +111,7 @@
>      mreq6.ipv6mr_interface = idx;
>
>      if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6,  
> sizeof(mreq6)) < 0) {
> -        avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s", strerror 
> (errno));
>          return -1;
>      }
>
> @@ -129,7 +129,7 @@
>      mreq.imr_ifindex = idx;
>
>      if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof 
> (mreq)) < 0) {
> -        avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s", strerror 
> (errno));
>          return -1;
>      }
>
> @@ -147,7 +147,7 @@
>      mreq6.ipv6mr_interface = idx;
>
>      if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6,  
> sizeof(mreq6)) < 0) {
> -        avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s\n",  
> strerror(errno));
> +        avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s", strerror 
> (errno));
>          return -1;
>      }
>
> @@ -164,7 +164,7 @@
>      if (bind(fd, sa, l) < 0) {
>
>          if (errno != EADDRINUSE) {
> -            avahi_log_warn("bind() failed: %s\n", strerror(errno));
> +            avahi_log_warn("bind() failed: %s", strerror(errno));
>              return -1;
>          }
>
> @@ -174,12 +174,12 @@
>          /* Try again, this time with SO_REUSEADDR set */
>          yes = 1;
>          if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof 
> (yes)) < 0) {
> -            avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror 
> (errno));
> +            avahi_log_warn("SO_REUSEADDR failed: %s", strerror 
> (errno));
>              return -1;
>          }
>
>          if (bind(fd, sa, l) < 0) {
> -            avahi_log_warn("bind() failed: %s\n", strerror(errno));
> +            avahi_log_warn("bind() failed: %s", strerror(errno));
>              return -1;
>          }
>      } else {
> @@ -190,7 +190,7 @@
>
>          yes = 1;
>          if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof 
> (yes)) < 0) {
> -            avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror 
> (errno));
> +            avahi_log_warn("SO_REUSEADDR failed: %s", strerror 
> (errno));
>              return -1;
>          }
>      }
> @@ -198,30 +198,30 @@
>      return 0;
>  }
>
> -int avahi_open_socket_ipv4(void) {
> +int avahi_open_socket_ipv4(int no_reuse) {
>      struct sockaddr_in local;
> -    int fd = -1, ttl, yes;
> +    int fd = -1, ttl, yes, r;
>
>      if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
> -        avahi_log_warn("socket() failed: %s\n", strerror(errno));
> +        avahi_log_warn("socket() failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      ttl = 255;
>      if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof 
> (ttl)) < 0) {
> -        avahi_log_warn("IP_MULTICAST_TTL failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror 
> (errno));
>          goto fail;
>      }
>
>      ttl = 255;
>      if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
> -        avahi_log_warn("IP_TTL failed: %s\n", strerror(errno));
> +        avahi_log_warn("IP_TTL failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof 
> (yes)) < 0) {
> -        avahi_log_warn("IP_MULTICAST_LOOP failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror 
> (errno));
>          goto fail;
>      }
>
> @@ -229,28 +229,33 @@
>      local.sin_family = AF_INET;
>      local.sin_port = htons(AVAHI_MDNS_PORT);
>
> -    if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof 
> (local)) < 0)
> +    if (no_reuse)
> +        r = bind(fd, (struct sockaddr*) &local, sizeof(local));
> +    else
> +        r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof 
> (local));
> +
> +    if (r < 0)
>          goto fail;
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
> -        avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno));
> +        avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
> -        avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno));
> +        avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_cloexec(fd) < 0) {
> -        avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
> +        avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_nonblock(fd) < 0) {
> -        avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
> +        avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
>          goto fail;
>      }
>
> @@ -263,38 +268,38 @@
>      return -1;
>  }
>
> -int avahi_open_socket_ipv6(void) {
> +int avahi_open_socket_ipv6(int no_reuse) {
>      struct sockaddr_in6 sa, local;
> -    int fd = -1, ttl, yes;
> +    int fd = -1, ttl, yes, r;
>
>      mdns_mcast_group_ipv6(&sa);
>
>      if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
> -        avahi_log_warn("socket() failed: %s\n", strerror(errno));
> +        avahi_log_warn("socket() failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      ttl = 255;
>      if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof 
> (ttl)) < 0) {
> -        avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror 
> (errno));
>          goto fail;
>      }
>
>      ttl = 255;
>      if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof 
> (ttl)) < 0) {
> -        avahi_log_warn("IPV6_UNICAST_HOPS failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror 
> (errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) <  
> 0) {
> -        avahi_log_warn("IPV6_V6ONLY failed: %s\n", strerror(errno));
> +        avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof 
> (yes)) < 0) {
> -        avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror 
> (errno));
>          goto fail;
>      }
>
> @@ -302,28 +307,33 @@
>      local.sin6_family = AF_INET6;
>      local.sin6_port = htons(AVAHI_MDNS_PORT);
>
> -    if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof 
> (local)) < 0)
> +    if (no_reuse)
> +        r = bind(fd, (struct sockaddr*) &local, sizeof(local));
> +    else
> +        r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof 
> (local));
> +
> +    if (r < 0)
>          goto fail;
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes))  
> < 0) {
> -        avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes))  
> < 0) {
> -        avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno));
> +        avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_cloexec(fd) < 0) {
> -        avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
> +        avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_nonblock(fd) < 0) {
> -        avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
> +        avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
>          goto fail;
>      }
>
> @@ -346,7 +356,7 @@
>              break;
>
>          if (errno != EAGAIN) {
> -            avahi_log_debug("sendmsg() failed: %s\n", strerror 
> (errno));
> +            avahi_log_debug("sendmsg() failed: %s", strerror(errno));
>              return -1;
>          }
>
> @@ -631,7 +641,7 @@
>      int fd = -1, yes;
>
>      if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
> -        avahi_log_warn("socket() failed: %s\n", strerror(errno));
> +        avahi_log_warn("socket() failed: %s", strerror(errno));
>          goto fail;
>      }
>
> @@ -639,29 +649,29 @@
>      local.sin_family = AF_INET;
>
>      if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
> -        avahi_log_warn("bind() failed: %s\n", strerror(errno));
> +        avahi_log_warn("bind() failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
> -        avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno));
> +        avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
> -        avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno));
> +        avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_cloexec(fd) < 0) {
> -        avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
> +        avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_nonblock(fd) < 0) {
> -        avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
> +        avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
>          goto fail;
>      }
>
> @@ -679,7 +689,7 @@
>      int fd = -1, yes;
>
>      if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
> -        avahi_log_warn("socket() failed: %s\n", strerror(errno));
> +        avahi_log_warn("socket() failed: %s", strerror(errno));
>          goto fail;
>      }
>
> @@ -687,29 +697,29 @@
>      local.sin6_family = AF_INET6;
>
>      if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
> -        avahi_log_warn("bind() failed: %s\n", strerror(errno));
> +        avahi_log_warn("bind() failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes))  
> < 0) {
> -        avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror 
> (errno));
> +        avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      yes = 1;
>      if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes))  
> < 0) {
> -        avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno));
> +        avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_cloexec(fd) < 0) {
> -        avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno));
> +        avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno));
>          goto fail;
>      }
>
>      if (avahi_set_nonblock(fd) < 0) {
> -        avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno));
> +        avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno));
>          goto fail;
>      }
>
>
> Modified: trunk/avahi-core/socket.h
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/avahi-core/ 
> socket.h?rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/avahi-core/socket.h (original)
> +++ trunk/avahi-core/socket.h Thu Sep 29 01:49:09 2005
> @@ -32,8 +32,8 @@
>  #define AVAHI_IPV4_MCAST_GROUP "224.0.0.251"
>  #define AVAHI_IPV6_MCAST_GROUP "ff02::fb"
>
> -int avahi_open_socket_ipv4(void);
> -int avahi_open_socket_ipv6(void);
> +int avahi_open_socket_ipv4(int no_reuse);
> +int avahi_open_socket_ipv6(int no_reuse);
>
>  int avahi_open_unicast_socket_ipv4(void);
>  int avahi_open_unicast_socket_ipv6(void);
>
> Modified: trunk/avahi-daemon/avahi-daemon.conf
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/avahi-daemon/ 
> avahi-daemon.conf?rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/avahi-daemon/avahi-daemon.conf (original)
> +++ trunk/avahi-daemon/avahi-daemon.conf Thu Sep 29 01:49:09 2005
> @@ -7,6 +7,7 @@
>  use-iff-running=no
>  enable-dbus=yes
>  add-service-cookie=yes
> +disallow-other-stacks=yes
>
>  [wide-area]
>  enable-wide-area=yes
>
> Modified: trunk/avahi-daemon/main.c
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/avahi-daemon/ 
> main.c?rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/avahi-daemon/main.c (original)
> +++ trunk/avahi-daemon/main.c Thu Sep 29 01:49:09 2005
> @@ -393,6 +393,8 @@
>                      c->server_config.check_response_ttl = is_yes(p- 
> >value);
>                  else if (strcasecmp(p->key, "use-iff-running") == 0)
>                      c->server_config.use_iff_running = is_yes(p- 
> >value);
> +                else if (strcasecmp(p->key, "disallow-other- 
> stacks") == 0)
> +                    c->server_config.disallow_other_stacks = is_yes 
> (p->value);
>                  else if (strcasecmp(p->key, "enable-dbus") == 0) {
>
>                      if (*(p->value) == 'w' || *(p->value) == 'W') {
> @@ -871,6 +873,7 @@
>      init_rand_seed();
>
>      avahi_server_config_init(&config.server_config);
> +    config.server_config.disallow_other_stacks = 1;
>      config.command = DAEMON_RUN;
>      config.daemonize = 0;
>      config.config_file = NULL;
>
> Modified: trunk/docs/TODO
> URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/docs/TODO? 
> rev=646&root=avahi&r1=645&r2=646&view=diff
> ====================================================================== 
> ========
> --- trunk/docs/TODO (original)
> +++ trunk/docs/TODO Thu Sep 29 01:49:09 2005
> @@ -10,8 +10,6 @@
>
>  * Add static host configuration like static services [lathiat]
>  * wrap avahi_server_add_record() via DBUS and in avahi-client   
> [lathiat]
> -
> -* add option to disable SO_REUSEADDR to disallow binding of  
> multiple processes to port 5353
>
>  * add flags argument to disable cookies-setting for local services
>
> @@ -87,3 +85,4 @@
>  * allow resolving of services without name
>  * add sever version check to avahi-client
>  * Passive observation of failures
> +* add option to disable SO_REUSEADDR to disallow binding of  
> multiple processes to port 5353
>
> _______________________________________________
> avahi-commits mailing list
> avahi-commits at mail.0pointer.de
> https://tango.0pointer.de/mailman/listinfo/avahi-commits


More information about the avahi mailing list