resolved-dns-synthesize.c revision 839a4a20d8b4f6ebf3a342fd05c821358be5a313
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
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 "alloc-util.h"
#include "hostname-util.h"
#include "local-addresses.h"
#include "resolved-dns-synthesize.h"
static int SYNTHESIZE_IFINDEX(int ifindex) {
/* When the caller asked for resolving on a specific
* interface, we synthesize the answer for that
* interface. However, if nothing specific was claimed and we
* only return localhost RRs, we synthesize the answer for
* localhost. */
if (ifindex > 0)
return ifindex;
return LOOPBACK_IFINDEX;
}
/* Picks an address family depending on set flags. This is
* purely for synthesized answers, where the family we return
* for the reply should match what was requested in the
* question, even though we are synthesizing the answer
* here. */
if (!(flags & SD_RESOLVED_DNS)) {
return AF_INET;
return AF_INET6;
}
return AF_UNSPEC;
}
/* Similar as SYNTHESIZE_FAMILY() but does this for the
* protocol. If resolving via DNS was requested, we claim it
* was DNS. Similar, if nothing specific was
* requested. However, if only resolving via LLMNR was
* requested we return that. */
if (flags & SD_RESOLVED_DNS)
return DNS_PROTOCOL_DNS;
if (flags & SD_RESOLVED_LLMNR)
return DNS_PROTOCOL_LLMNR;
if (flags & SD_RESOLVED_MDNS)
return DNS_PROTOCOL_MDNS;
return DNS_PROTOCOL_DNS;
}
static int synthesize_localhost_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
int r;
assert(m);
if (r < 0)
return r;
if (!rr)
return -ENOMEM;
if (r < 0)
return r;
}
if (!rr)
return -ENOMEM;
if (r < 0)
return r;
}
return 0;
}
static int answer_add_ptr(DnsAnswer **answer, const char *from, const char *to, int ifindex, DnsAnswerFlags flags) {
if (!rr)
return -ENOMEM;
return -ENOMEM;
}
static int synthesize_localhost_ptr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
int r;
assert(m);
if (r < 0)
return r;
r = answer_add_ptr(answer, DNS_RESOURCE_KEY_NAME(key), "localhost", SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
if (r < 0)
return r;
}
return 0;
}
static int answer_add_addresses_rr(
const char *name,
struct local_address *addresses,
unsigned n_addresses) {
unsigned j;
int r;
if (r < 0)
return r;
for (j = 0; j < n_addresses; j++) {
if (r < 0)
return r;
if (r < 0)
return r;
}
return 0;
}
static int answer_add_addresses_ptr(
const char *name,
struct local_address *addresses,
unsigned n_addresses,
unsigned j;
int r;
for (j = 0; j < n_addresses; j++) {
continue;
continue;
}
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
}
return 0;
}
static int synthesize_system_hostname_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
int n = 0, af;
assert(m);
if (af >= 0) {
if (n < 0)
return n;
if (n == 0) {
/* If we have no local addresses then use ::1
* and 127.0.0.2 as local ones. */
buffer[n++] = (struct local_address) {
};
buffer[n++] = (struct local_address) {
};
}
}
}
static int synthesize_system_hostname_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) {
int n, r;
assert(m);
/* Always map the IPv4 address 127.0.0.2 to the local
* hostname, in addition to "localhost": */
if (r < 0)
return r;
r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->llmnr_hostname, SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
if (r < 0)
return r;
r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", m->mdns_hostname, SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
if (r < 0)
return r;
r = answer_add_ptr(answer, "2.0.0.127.in-addr.arpa", "localhost", SYNTHESIZE_IFINDEX(ifindex), DNS_ANSWER_AUTHENTICATED);
if (r < 0)
return r;
return 0;
}
if (n < 0)
return n;
if (r < 0)
return r;
}
static int synthesize_gateway_rr(Manager *m, const DnsResourceKey *key, int ifindex, DnsAnswer **answer) {
int n = 0, af;
assert(m);
if (af >= 0) {
if (n < 0)
return n;
}
}
static int synthesize_gateway_ptr(Manager *m, int af, const union in_addr_union *address, int ifindex, DnsAnswer **answer) {
int n;
assert(m);
if (n < 0)
return n;
}
Manager *m,
DnsQuestion *q,
int ifindex,
int *ret_family) {
int r;
assert(m);
assert(q);
DNS_QUESTION_FOREACH(key, q) {
union in_addr_union address;
const char *name;
int af;
continue;
if (is_localhost(name)) {
if (r < 0)
return log_error_errno(r, "Failed to synthesize localhost RRs: %m");
} else if (manager_is_own_hostname(m, name)) {
if (r < 0)
return log_error_errno(r, "Failed to synthesize system hostname RRs: %m");
} else if (is_gateway_hostname(name)) {
if (r < 0)
return log_error_errno(r, "Failed to synthesize gateway RRs: %m");
} else if ((dns_name_endswith(name, "127.in-addr.arpa") > 0 && dns_name_equal(name, "2.0.0.127.in-addr.arpa") == 0) ||
dns_name_equal(name, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0) {
if (r < 0)
return log_error_errno(r, "Failed to synthesize localhost PTR RRs: %m");
if (r < 0)
return log_error_errno(r, "Failed to synthesize system hostname PTR RR: %m");
if (r < 0)
return log_error_errno(r, "Failed to synthesize gateway hostname PTR RR: %m");
}
}
r = dns_answer_size(answer) > 0;
if (ret) {
}
if (ret_protocol)
if (ret_family)
return r;
}