resolved-mdns.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
This file is part of systemd.
Copyright 2015 Daniel Mack
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <resolv.h>
#include "fd-util.h"
#include "resolved-manager.h"
#include "resolved-mdns.h"
void manager_mdns_stop(Manager *m) {
assert(m);
}
int manager_mdns_start(Manager *m) {
int r;
assert(m);
if (m->mdns_support == RESOLVE_SUPPORT_NO)
return 0;
r = manager_mdns_ipv4_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
if (socket_ipv6_is_supported()) {
r = manager_mdns_ipv6_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
}
return 0;
log_warning("There appears to be another mDNS responder running. Turning off mDNS support.");
return 0;
}
int r;
if (r <= 0)
return r;
scope = manager_find_scope(m, p);
if (!scope) {
log_warning("Got mDNS UDP packet on unknown scope. Ignoring.");
return 0;
}
if (dns_packet_validate_reply(p) > 0) {
log_debug("Got mDNS reply packet");
/*
* mDNS is different from regular DNS and LLMNR with regard to handling responses.
* While on other protocols, we can ignore every answer that doesn't match a question
* we broadcast earlier, RFC6762, section 18.1 recommends looking at and caching all
* incoming information, regardless of the DNS packet ID.
*
* Hence, extract the packet here, and try to find a transaction for answer the we got
* and complete it. Also store the new information in scope's cache.
*/
r = dns_packet_extract(p);
if (r < 0) {
log_debug("mDNS packet extraction failed.");
return 0;
}
DnsTransaction *t;
/* If the received reply packet contains ANY record that is not .local or .in-addr.arpa,
* we assume someone's playing tricks on us and discard the packet completely. */
return 0;
if (t)
}
dns_cache_put(&scope->cache, NULL, DNS_PACKET_RCODE(p), p->answer, false, (uint32_t) -1, 0, p->family, &p->sender);
} else if (dns_packet_validate_query(p) > 0) {
} else
log_debug("Invalid mDNS UDP packet.");
return 0;
}
int manager_mdns_ipv4_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->mdns_ipv4_fd >= 0)
return m->mdns_ipv4_fd;
if (m->mdns_ipv4_fd < 0)
return -errno;
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->mdns_ipv4_event_source, m->mdns_ipv4_fd, EPOLLIN, on_mdns_packet, m);
if (r < 0)
goto fail;
return m->mdns_ipv4_fd;
fail:
return r;
}
int manager_mdns_ipv6_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->mdns_ipv6_fd >= 0)
return m->mdns_ipv6_fd;
if (m->mdns_ipv6_fd < 0)
return -errno;
if (r < 0) {
r = -errno;
goto fail;
}
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->mdns_ipv6_event_source, m->mdns_ipv6_fd, EPOLLIN, on_mdns_packet, m);
if (r < 0)
goto fail;
return m->mdns_ipv6_fd;
fail:
return r;
}