.in.sin_family = AF_INET,
.in.sin_port = htobe16(MDNS_PORT),
};
+ _cleanup_close_ int s = -1;
int r;
assert(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) {
.in6.sin6_family = AF_INET6,
.in6.sin6_port = htobe16(MDNS_PORT),
};
+ _cleanup_close_ int s = -1;
int r;
assert(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);
}