From ad7b145ebe82c3a53632eaa6ec75b014d91a7bea Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 19 Jun 2005 13:44:50 +0000 Subject: * detect other running mDNS stacks * replace more g_message()/g_warning() calls with avahi_log_xxx() * fix configuration file paths * only load configuration file when running daemon * require uid == 0 * fix static service reloading * add new command line option for reloading -r git-svn-id: file:///home/lennart/svn/public/avahi/trunk@129 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/server.c | 6 ++--- avahi-core/socket.c | 67 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 23 deletions(-) (limited to 'avahi-core') diff --git a/avahi-core/server.c b/avahi-core/server.c index d03a284..86f29fb 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -1268,14 +1268,14 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah } if (s->fd_ipv4 < 0 && s->config.use_ipv4) - avahi_log_debug("Failed to create IPv4 socket, proceeding in IPv6 only mode"); + avahi_log_notice("Failed to create IPv4 socket, proceeding in IPv6 only mode"); else if (s->fd_ipv6 < 0 && s->config.use_ipv6) - avahi_log_debug("Failed to create IPv6 socket, proceeding in IPv4 only mode"); + avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode"); s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1; s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1; - g_main_context_ref(c->context = c ? c : g_main_context_default()); + g_main_context_ref(s->context = (c ? c : g_main_context_default())); /* Prepare IO source registration */ s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*)); diff --git a/avahi-core/socket.c b/avahi-core/socket.c index 55db704..8f3d896 100644 --- a/avahi-core/socket.c +++ b/avahi-core/socket.c @@ -156,6 +156,50 @@ int avahi_mdns_mcast_leave_ipv6 (int index, int fd) { return 0; } +static gint bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { + gint yes; + + g_assert(fd >= 0); + g_assert(sa); + g_assert(l > 0); + + if (bind(fd, sa, l) < 0) { + + if (errno != EADDRINUSE) { + avahi_log_warn("bind() failed: %s\n", strerror(errno)); + return -1; + } + + avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***", + sa->sa_family == AF_INET ? "IPv4" : "IPv6"); + + /* 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)); + return -1; + } + + if (bind(fd, sa, l) < 0) { + avahi_log_warn("bind() failed: %s\n", strerror(errno)); + return -1; + } + } else { + + /* We enable SO_REUSEADDR afterwards, to make sure that the + * user may run other mDNS implementations if he really + * wants. */ + + yes = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { + avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno)); + return -1; + } + } + + return 0; +} + gint avahi_open_socket_ipv4(void) { struct sockaddr_in local; int fd = -1, ttl, yes; @@ -177,27 +221,18 @@ gint avahi_open_socket_ipv4(void) { goto fail; } - yes = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - avahi_log_warn("SO_REUSEADDR failed: %s\n", 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)); goto fail; } - memset(&local, 0, sizeof(local)); local.sin_family = AF_INET; local.sin_port = htons(AVAHI_MDNS_PORT); - - if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + + if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0) goto fail; - } yes = 1; if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) { @@ -253,12 +288,6 @@ gint avahi_open_socket_ipv6(void) { goto fail; } - yes = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - avahi_log_warn("SO_REUSEADDR failed: %s\n", 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)); @@ -275,10 +304,8 @@ gint avahi_open_socket_ipv6(void) { local.sin6_family = AF_INET6; local.sin6_port = htons(AVAHI_MDNS_PORT); - if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0) goto fail; - } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) { -- cgit