diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-18 23:03:19 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-18 23:03:19 +0200 |
commit | 73c76767565f1648991f2ee979d248402da52273 (patch) | |
tree | c0f210a45188f9ab797c4935a9bd92875698aa6c /src/resolve | |
parent | resolved-llmnr: use TAKE_FD() (diff) | |
download | systemd-73c76767565f1648991f2ee979d248402da52273.tar.xz systemd-73c76767565f1648991f2ee979d248402da52273.zip |
resolved-mdns: use TAKE_FD()
Diffstat (limited to 'src/resolve')
-rw-r--r-- | src/resolve/resolved-mdns.c | 190 |
1 files changed, 72 insertions, 118 deletions
diff --git a/src/resolve/resolved-mdns.c b/src/resolve/resolved-mdns.c index f91c0ff7f0..89c2497d32 100644 --- a/src/resolve/resolved-mdns.c +++ b/src/resolve/resolved-mdns.c @@ -339,6 +339,7 @@ int manager_mdns_ipv4_fd(Manager *m) { .in.sin_family = AF_INET, .in.sin_port = htobe16(MDNS_PORT), }; + _cleanup_close_ int s = -1; int r; assert(m); @@ -346,88 +347,64 @@ int manager_mdns_ipv4_fd(Manager *m) { if (m->mdns_ipv4_fd >= 0) return m->mdns_ipv4_fd; - m->mdns_ipv4_fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (m->mdns_ipv4_fd < 0) + s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (s < 0) return log_error_errno(errno, "mDNS-IPv4: Failed to create socket: %m"); - r = setsockopt_int(m->mdns_ipv4_fd, IPPROTO_IP, IP_TTL, 255); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set IP_TTL: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_TTL, 255); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set IP_TTL: %m"); - r = setsockopt_int(m->mdns_ipv4_fd, IPPROTO_IP, IP_MULTICAST_TTL, 255); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set IP_MULTICAST_TTL: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_MULTICAST_TTL, 255); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set IP_MULTICAST_TTL: %m"); - r = setsockopt_int(m->mdns_ipv4_fd, IPPROTO_IP, IP_MULTICAST_LOOP, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set IP_MULTICAST_LOOP: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_MULTICAST_LOOP, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set IP_MULTICAST_LOOP: %m"); - r = setsockopt_int(m->mdns_ipv4_fd, IPPROTO_IP, IP_PKTINFO, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set IP_PKTINFO: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_PKTINFO, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set IP_PKTINFO: %m"); - r = setsockopt_int(m->mdns_ipv4_fd, IPPROTO_IP, IP_RECVTTL, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set IP_RECVTTL: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_RECVTTL, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set IP_RECVTTL: %m"); /* Disable Don't-Fragment bit in the IP header */ - r = setsockopt_int(m->mdns_ipv4_fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set IP_MTU_DISCOVER: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set IP_MTU_DISCOVER: %m"); /* See the section 15.1 of RFC6762 */ /* first try to bind without SO_REUSEADDR to detect another mDNS responder */ - r = bind(m->mdns_ipv4_fd, &sa.sa, sizeof(sa.in)); + r = bind(s, &sa.sa, sizeof(sa.in)); if (r < 0) { - if (errno != EADDRINUSE) { - r = log_error_errno(errno, "mDNS-IPv4: Failed to bind socket: %m"); - goto fail; - } + if (errno != EADDRINUSE) + return log_error_errno(errno, "mDNS-IPv4: Failed to bind socket: %m"); log_warning("mDNS-IPv4: There appears to be another mDNS responder running, or previously systemd-resolved crashed with some outstanding transfers."); /* try again with SO_REUSEADDR */ - r = setsockopt_int(m->mdns_ipv4_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set SO_REUSEADDR: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set SO_REUSEADDR: %m"); - r = bind(m->mdns_ipv4_fd, &sa.sa, sizeof(sa.in)); - if (r < 0) { - r = log_error_errno(errno, "mDNS-IPv4: Failed to bind socket: %m"); - goto fail; - } + r = bind(s, &sa.sa, sizeof(sa.in)); + if (r < 0) + return log_error_errno(errno, "mDNS-IPv4: Failed to bind socket: %m"); } else { /* enable SO_REUSEADDR for the case that the user really wants multiple mDNS responders */ - r = setsockopt_int(m->mdns_ipv4_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv4: Failed to set SO_REUSEADDR: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv4: Failed to set SO_REUSEADDR: %m"); } - r = sd_event_add_io(m->event, &m->mdns_ipv4_event_source, m->mdns_ipv4_fd, EPOLLIN, on_mdns_packet, m); + r = sd_event_add_io(m->event, &m->mdns_ipv4_event_source, s, EPOLLIN, on_mdns_packet, m); if (r < 0) - goto fail; - - return m->mdns_ipv4_fd; + return log_error_errno(r, "mDNS-IPv4: Failed to create event source: %m"); -fail: - m->mdns_ipv4_fd = safe_close(m->mdns_ipv4_fd); - return r; + return m->mdns_ipv4_fd = TAKE_FD(s); } int manager_mdns_ipv6_fd(Manager *m) { @@ -435,6 +412,7 @@ int manager_mdns_ipv6_fd(Manager *m) { .in6.sin6_family = AF_INET6, .in6.sin6_port = htobe16(MDNS_PORT), }; + _cleanup_close_ int s = -1; int r; assert(m); @@ -442,86 +420,62 @@ int manager_mdns_ipv6_fd(Manager *m) { if (m->mdns_ipv6_fd >= 0) return m->mdns_ipv6_fd; - m->mdns_ipv6_fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (m->mdns_ipv6_fd < 0) + s = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (s < 0) return log_error_errno(errno, "mDNS-IPv6: Failed to create socket: %m"); - r = setsockopt_int(m->mdns_ipv6_fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_UNICAST_HOPS: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_UNICAST_HOPS: %m"); /* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */ - r = setsockopt_int(m->mdns_ipv6_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_MULTICAST_HOPS: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_MULTICAST_HOPS: %m"); - r = setsockopt_int(m->mdns_ipv6_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_MULTICAST_LOOP: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_MULTICAST_LOOP: %m"); - r = setsockopt_int(m->mdns_ipv6_fd, IPPROTO_IPV6, IPV6_V6ONLY, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_V6ONLY: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_V6ONLY, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_V6ONLY: %m"); - r = setsockopt_int(m->mdns_ipv6_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_RECVPKTINFO: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_RECVPKTINFO: %m"); - r = setsockopt_int(m->mdns_ipv6_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_RECVHOPLIMIT: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set IPV6_RECVHOPLIMIT: %m"); /* See the section 15.1 of RFC6762 */ /* first try to bind without SO_REUSEADDR to detect another mDNS responder */ - r = bind(m->mdns_ipv6_fd, &sa.sa, sizeof(sa.in6)); + r = bind(s, &sa.sa, sizeof(sa.in6)); if (r < 0) { - if (errno != EADDRINUSE) { - r = log_error_errno(errno, "mDNS-IPv6: Failed to bind socket: %m"); - goto fail; - } + if (errno != EADDRINUSE) + return log_error_errno(errno, "mDNS-IPv6: Failed to bind socket: %m"); log_warning("mDNS-IPv6: There appears to be another mDNS responder running, or previously systemd-resolved crashed with some outstanding transfers."); /* try again with SO_REUSEADDR */ - r = setsockopt_int(m->mdns_ipv6_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set SO_REUSEADDR: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set SO_REUSEADDR: %m"); - r = bind(m->mdns_ipv6_fd, &sa.sa, sizeof(sa.in6)); - if (r < 0) { - r = log_error_errno(errno, "mDNS-IPv6: Failed to bind socket: %m"); - goto fail; - } + r = bind(s, &sa.sa, sizeof(sa.in6)); + if (r < 0) + return log_error_errno(errno, "mDNS-IPv6: Failed to bind socket: %m"); } else { /* enable SO_REUSEADDR for the case that the user really wants multiple mDNS responders */ - r = setsockopt_int(m->mdns_ipv6_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "mDNS-IPv6: Failed to set SO_REUSEADDR: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "mDNS-IPv6: Failed to set SO_REUSEADDR: %m"); } - r = sd_event_add_io(m->event, &m->mdns_ipv6_event_source, m->mdns_ipv6_fd, EPOLLIN, on_mdns_packet, m); + r = sd_event_add_io(m->event, &m->mdns_ipv6_event_source, s, EPOLLIN, on_mdns_packet, m); if (r < 0) - goto fail; - - return m->mdns_ipv6_fd; + return log_error_errno(r, "mDNS-IPv6: Failed to create event source: %m"); -fail: - m->mdns_ipv6_fd = safe_close(m->mdns_ipv6_fd); - return r; + return m->mdns_ipv6_fd = TAKE_FD(s); } |