diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-18 22:58:03 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-10-18 22:58:03 +0200 |
commit | 3f548ffff43b78661217422f7fccb561c9f5ba6e (patch) | |
tree | 8ed509f4c10c51825714d88dbae435ad384a552a /src/resolve | |
parent | tree-wide: use setsockopt_int() more (diff) | |
download | systemd-3f548ffff43b78661217422f7fccb561c9f5ba6e.tar.xz systemd-3f548ffff43b78661217422f7fccb561c9f5ba6e.zip |
resolved-llmnr: use TAKE_FD()
Diffstat (limited to 'src/resolve')
-rw-r--r-- | src/resolve/resolved-llmnr.c | 372 |
1 files changed, 142 insertions, 230 deletions
diff --git a/src/resolve/resolved-llmnr.c b/src/resolve/resolved-llmnr.c index 32130b9924..65f5ceecd0 100644 --- a/src/resolve/resolved-llmnr.c +++ b/src/resolve/resolved-llmnr.c @@ -115,6 +115,7 @@ int manager_llmnr_ipv4_udp_fd(Manager *m) { .in.sin_family = AF_INET, .in.sin_port = htobe16(LLMNR_PORT), }; + _cleanup_close_ int s = -1; int r; assert(m); @@ -122,90 +123,66 @@ int manager_llmnr_ipv4_udp_fd(Manager *m) { if (m->llmnr_ipv4_udp_fd >= 0) return m->llmnr_ipv4_udp_fd; - m->llmnr_ipv4_udp_fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (m->llmnr_ipv4_udp_fd < 0) + s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (s < 0) return log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to create socket: %m"); /* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */ - r = setsockopt_int(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_TTL, 255); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set IP_TTL: %m"); - r = setsockopt_int(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, 255); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set IP_MULTICAST_TTL: %m"); - r = setsockopt_int(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_MULTICAST_LOOP, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set IP_MULTICAST_LOOP: %m"); - r = setsockopt_int(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_PKTINFO, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set IP_PKTINFO: %m"); - r = setsockopt_int(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_RECVTTL, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set IP_RECVTTL: %m"); /* Disable Don't-Fragment bit in the IP header */ - r = setsockopt_int(m->llmnr_ipv4_udp_fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set IP_MTU_DISCOVER: %m"); /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */ - r = bind(m->llmnr_ipv4_udp_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, "LLMNR-IPv4(UDP): Failed to bind socket: %m"); - goto fail; - } + if (errno != EADDRINUSE) + return log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to bind socket: %m"); log_warning("LLMNR-IPv4(UDP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers."); /* try again with SO_REUSEADDR */ - r = setsockopt_int(m->llmnr_ipv4_udp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set SO_REUSEADDR: %m"); - goto fail; - } - - r = bind(m->llmnr_ipv4_udp_fd, &sa.sa, sizeof(sa.in)); - if (r < 0) { - r = log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to bind socket: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set SO_REUSEADDR: %m"); + + r = bind(s, &sa.sa, sizeof(sa.in)); + if (r < 0) + return log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to bind socket: %m"); } else { /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */ - r = setsockopt_int(m->llmnr_ipv4_udp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(UDP): 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, "LLMNR-IPv4(UDP): Failed to set SO_REUSEADDR: %m"); } - r = sd_event_add_io(m->event, &m->llmnr_ipv4_udp_event_source, m->llmnr_ipv4_udp_fd, EPOLLIN, on_llmnr_packet, m); + r = sd_event_add_io(m->event, &m->llmnr_ipv4_udp_event_source, s, EPOLLIN, on_llmnr_packet, m); if (r < 0) - goto fail; + return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to create event source: %m"); (void) sd_event_source_set_description(m->llmnr_ipv4_udp_event_source, "llmnr-ipv4-udp"); - return m->llmnr_ipv4_udp_fd; - -fail: - m->llmnr_ipv4_udp_fd = safe_close(m->llmnr_ipv4_udp_fd); - return r; + return m->llmnr_ipv4_udp_fd = TAKE_FD(s); } int manager_llmnr_ipv6_udp_fd(Manager *m) { @@ -213,6 +190,7 @@ int manager_llmnr_ipv6_udp_fd(Manager *m) { .in6.sin6_family = AF_INET6, .in6.sin6_port = htobe16(LLMNR_PORT), }; + _cleanup_close_ int s = -1; int r; assert(m); @@ -220,89 +198,65 @@ int manager_llmnr_ipv6_udp_fd(Manager *m) { if (m->llmnr_ipv6_udp_fd >= 0) return m->llmnr_ipv6_udp_fd; - m->llmnr_ipv6_udp_fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (m->llmnr_ipv6_udp_fd < 0) + s = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (s < 0) return log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to create socket: %m"); - r = setsockopt_int(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 255); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): 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->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): Failed to set IPV6_MULTICAST_HOPS: %m"); - r = setsockopt_int(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): Failed to set IPV6_MULTICAST_LOOP: %m"); - r = setsockopt_int(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_V6ONLY, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): Failed to set IPV6_V6ONLY: %m"); - r = setsockopt_int(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): Failed to set IPV6_RECVPKTINFO: %m"); - r = setsockopt_int(m->llmnr_ipv6_udp_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): Failed to set IPV6_RECVHOPLIMIT: %m"); /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */ - r = bind(m->llmnr_ipv6_udp_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, "LLMNR-IPv6(UDP): Failed to bind socket: %m"); - goto fail; - } + if (errno != EADDRINUSE) + return log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to bind socket: %m"); log_warning("LLMNR-IPv6(UDP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers."); /* try again with SO_REUSEADDR */ - r = setsockopt_int(m->llmnr_ipv6_udp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set SO_REUSEADDR: %m"); - goto fail; - } - - r = bind(m->llmnr_ipv6_udp_fd, &sa.sa, sizeof(sa.in6)); - if (r < 0) { - r = log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to bind socket: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set SO_REUSEADDR: %m"); + + r = bind(s, &sa.sa, sizeof(sa.in6)); + if (r < 0) + return log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to bind socket: %m"); } else { /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */ - r = setsockopt_int(m->llmnr_ipv6_udp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(UDP): 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, "LLMNR-IPv6(UDP): Failed to set SO_REUSEADDR: %m"); } - r = sd_event_add_io(m->event, &m->llmnr_ipv6_udp_event_source, m->llmnr_ipv6_udp_fd, EPOLLIN, on_llmnr_packet, m); + r = sd_event_add_io(m->event, &m->llmnr_ipv6_udp_event_source, s, EPOLLIN, on_llmnr_packet, m); if (r < 0) - goto fail; + return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to create event source: %m"); (void) sd_event_source_set_description(m->llmnr_ipv6_udp_event_source, "llmnr-ipv6-udp"); - return m->llmnr_ipv6_udp_fd; - -fail: - m->llmnr_ipv6_udp_fd = safe_close(m->llmnr_ipv6_udp_fd); - return r; + return m->llmnr_ipv6_udp_fd = TAKE_FD(s); } static int on_llmnr_stream_packet(DnsStream *s) { @@ -353,6 +307,7 @@ int manager_llmnr_ipv4_tcp_fd(Manager *m) { .in.sin_family = AF_INET, .in.sin_port = htobe16(LLMNR_PORT), }; + _cleanup_close_ int s = -1; int r; assert(m); @@ -360,84 +315,62 @@ int manager_llmnr_ipv4_tcp_fd(Manager *m) { if (m->llmnr_ipv4_tcp_fd >= 0) return m->llmnr_ipv4_tcp_fd; - m->llmnr_ipv4_tcp_fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (m->llmnr_ipv4_tcp_fd < 0) + s = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (s < 0) return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to create socket: %m"); /* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */ - r = setsockopt_int(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_TTL, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set IP_TTL: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IP, IP_TTL, true); + if (r < 0) + return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set IP_TTL: %m"); - r = setsockopt_int(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_PKTINFO, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(TCP): 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, "LLMNR-IPv4(TCP): Failed to set IP_PKTINFO: %m"); - r = setsockopt_int(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_RECVTTL, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(TCP): 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, "LLMNR-IPv4(TCP): Failed to set IP_RECVTTL: %m"); /* Disable Don't-Fragment bit in the IP header */ - r = setsockopt_int(m->llmnr_ipv4_tcp_fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(TCP): 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, "LLMNR-IPv4(TCP): Failed to set IP_MTU_DISCOVER: %m"); /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */ - r = bind(m->llmnr_ipv4_tcp_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, "LLMNR-IPv4(TCP): Failed to bind socket: %m"); - goto fail; - } + if (errno != EADDRINUSE) + return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to bind socket: %m"); log_warning("LLMNR-IPv4(TCP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers."); /* try again with SO_REUSEADDR */ - r = setsockopt_int(m->llmnr_ipv4_tcp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set SO_REUSEADDR: %m"); - goto fail; - } - - r = bind(m->llmnr_ipv4_tcp_fd, &sa.sa, sizeof(sa.in)); - if (r < 0) { - r = log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to bind socket: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set SO_REUSEADDR: %m"); + + r = bind(s, &sa.sa, sizeof(sa.in)); + if (r < 0) + return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to bind socket: %m"); } else { /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */ - r = setsockopt_int(m->llmnr_ipv4_tcp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv4(TCP): 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, "LLMNR-IPv4(TCP): Failed to set SO_REUSEADDR: %m"); } - r = listen(m->llmnr_ipv4_tcp_fd, SOMAXCONN); - if (r < 0) { - r = log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to listen the stream: %m"); - goto fail; - } + r = listen(s, SOMAXCONN); + if (r < 0) + return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to listen the stream: %m"); - r = sd_event_add_io(m->event, &m->llmnr_ipv4_tcp_event_source, m->llmnr_ipv4_tcp_fd, EPOLLIN, on_llmnr_stream, m); + r = sd_event_add_io(m->event, &m->llmnr_ipv4_tcp_event_source, s, EPOLLIN, on_llmnr_stream, m); if (r < 0) - goto fail; + return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to create event source: %m"); (void) sd_event_source_set_description(m->llmnr_ipv4_tcp_event_source, "llmnr-ipv4-tcp"); - return m->llmnr_ipv4_tcp_fd; - -fail: - m->llmnr_ipv4_tcp_fd = safe_close(m->llmnr_ipv4_tcp_fd); - return r; + return m->llmnr_ipv4_tcp_fd = TAKE_FD(s); } int manager_llmnr_ipv6_tcp_fd(Manager *m) { @@ -445,6 +378,7 @@ int manager_llmnr_ipv6_tcp_fd(Manager *m) { .in6.sin6_family = AF_INET6, .in6.sin6_port = htobe16(LLMNR_PORT), }; + _cleanup_close_ int s = -1; int r; assert(m); @@ -452,81 +386,59 @@ int manager_llmnr_ipv6_tcp_fd(Manager *m) { if (m->llmnr_ipv6_tcp_fd >= 0) return m->llmnr_ipv6_tcp_fd; - m->llmnr_ipv6_tcp_fd = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); - if (m->llmnr_ipv6_tcp_fd < 0) + s = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (s < 0) return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to create socket: %m"); /* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */ - r = setsockopt_int(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set IPV6_UNICAST_HOPS: %m"); - goto fail; - } + r = setsockopt_int(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, true); + if (r < 0) + return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set IPV6_UNICAST_HOPS: %m"); - r = setsockopt_int(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_V6ONLY, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(TCP): 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, "LLMNR-IPv6(TCP): Failed to set IPV6_V6ONLY: %m"); - r = setsockopt_int(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(TCP): 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, "LLMNR-IPv6(TCP): Failed to set IPV6_RECVPKTINFO: %m"); - r = setsockopt_int(m->llmnr_ipv6_tcp_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(TCP): 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, "LLMNR-IPv6(TCP): Failed to set IPV6_RECVHOPLIMIT: %m"); /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */ - r = bind(m->llmnr_ipv6_tcp_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, "LLMNR-IPv6(TCP): Failed to bind socket: %m"); - goto fail; - } + if (errno != EADDRINUSE) + return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to bind socket: %m"); log_warning("LLMNR-IPv6(TCP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers."); /* try again with SO_REUSEADDR */ - r = setsockopt_int(m->llmnr_ipv6_tcp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set SO_REUSEADDR: %m"); - goto fail; - } - - r = bind(m->llmnr_ipv6_tcp_fd, &sa.sa, sizeof(sa.in6)); - if (r < 0) { - r = log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to bind socket: %m"); - goto fail; - } + r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true); + if (r < 0) + return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set SO_REUSEADDR: %m"); + + r = bind(s, &sa.sa, sizeof(sa.in6)); + if (r < 0) + return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to bind socket: %m"); } else { /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */ - r = setsockopt_int(m->llmnr_ipv6_tcp_fd, SOL_SOCKET, SO_REUSEADDR, true); - if (r < 0) { - log_error_errno(r, "LLMNR-IPv6(TCP): 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, "LLMNR-IPv6(TCP): Failed to set SO_REUSEADDR: %m"); } - r = listen(m->llmnr_ipv6_tcp_fd, SOMAXCONN); - if (r < 0) { - r = log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to listen the stream: %m"); - goto fail; - } + r = listen(s, SOMAXCONN); + if (r < 0) + return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to listen the stream: %m"); - r = sd_event_add_io(m->event, &m->llmnr_ipv6_tcp_event_source, m->llmnr_ipv6_tcp_fd, EPOLLIN, on_llmnr_stream, m); + r = sd_event_add_io(m->event, &m->llmnr_ipv6_tcp_event_source, s, EPOLLIN, on_llmnr_stream, m); if (r < 0) - goto fail; + return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to create event source: %m"); (void) sd_event_source_set_description(m->llmnr_ipv6_tcp_event_source, "llmnr-ipv6-tcp"); - return m->llmnr_ipv6_tcp_fd; - -fail: - m->llmnr_ipv6_tcp_fd = safe_close(m->llmnr_ipv6_tcp_fd); - return r; + return m->llmnr_ipv6_tcp_fd = TAKE_FD(s); } |