Browse Source

resolved: add a DNS client stub resolver

Let's turn resolved into a something truly useful: a fully asynchronous
DNS stub resolver that subscribes to network changes.

(More to come: caching, LLMNR, mDNS/DNS-SD, DNSSEC, IDN, NSS module)
keep-around/964a6d9fb555cc86528eb1cc1f6d044f85584842
Lennart Poettering 9 years ago
parent
commit
74b2466e14
  1. 1
      .gitignore
  2. 36
      Makefile.am
  3. 468
      src/resolve/resolved-bus.c
  4. 381
      src/resolve/resolved-dns-domain.c
  5. 43
      src/resolve/resolved-dns-domain.h
  6. 734
      src/resolve/resolved-dns-packet.c
  7. 132
      src/resolve/resolved-dns-packet.h
  8. 408
      src/resolve/resolved-dns-query.c
  9. 97
      src/resolve/resolved-dns-query.h
  10. 76
      src/resolve/resolved-dns-rr.c
  11. 114
      src/resolve/resolved-dns-rr.h
  12. 147
      src/resolve/resolved-dns-scope.c
  13. 69
      src/resolve/resolved-dns-scope.h
  14. 98
      src/resolve/resolved-dns-server.c
  15. 61
      src/resolve/resolved-dns-server.h
  16. 2
      src/resolve/resolved-gperf.gperf
  17. 378
      src/resolve/resolved-link.c
  18. 84
      src/resolve/resolved-link.h
  19. 817
      src/resolve/resolved-manager.c
  20. 35
      src/resolve/resolved.c
  21. 73
      src/resolve/resolved.h
  22. 173
      src/resolve/test-dns-domain.c
  23. 4
      src/shared/bus-errors.h
  24. 22
      src/shared/in-addr-util.c
  25. 1
      src/shared/in-addr-util.h
  26. 3
      src/shared/macro.h
  27. 2
      src/systemd/sd-dhcp-lease.h

1
.gitignore

@ -157,6 +157,7 @@
/test-dhcp-option
/test-dhcp-server
/test-dhcp6-client
/test-dns-domain
/test-icmp6-rs
/test-ellipsize
/test-engine

36
Makefile.am

@ -4536,11 +4536,22 @@ if ENABLE_RESOLVED
systemd_resolved_SOURCES = \
src/resolve/resolved.h \
src/resolve/resolved.c \
src/resolve/resolved-manager.c
systemd_resolved_CFLAGS = \
$(AM_CFLAGS) \
$(KMOD_CFLAGS)
src/resolve/resolved-manager.c \
src/resolve/resolved-bus.c \
src/resolve/resolved-link.h \
src/resolve/resolved-link.c \
src/resolve/resolved-dns-domain.h \
src/resolve/resolved-dns-domain.c \
src/resolve/resolved-dns-packet.h \
src/resolve/resolved-dns-packet.c \
src/resolve/resolved-dns-query.h \
src/resolve/resolved-dns-query.c \
src/resolve/resolved-dns-scope.h \
src/resolve/resolved-dns-scope.c \
src/resolve/resolved-dns-server.h \
src/resolve/resolved-dns-server.c \
src/resolve/resolved-dns-rr.h \
src/resolve/resolved-dns-rr.c
nodist_systemd_resolved_SOURCES = \
src/resolve/resolved-gperf.c
@ -4579,6 +4590,21 @@ EXTRA_DIST += \
CLEANFILES += \
src/resolve/resolved.conf
tests += \
test-dns-domain
test_dns_domain_SOURCES = \
src/resolve/resolved-dns-domain.h \
src/resolve/resolved-dns-domain.c \
src/resolve/test-dns-domain.c
test_dns_domain_LDADD = \
libsystemd-capability.la \
libsystemd-network.la \
libsystemd-label.la \
libsystemd-internal.la \
libsystemd-shared.la
endif
# ------------------------------------------------------------------------------

468
src/resolve/resolved-bus.c

@ -0,0 +1,468 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 "bus-errors.h"
#include "bus-util.h"
#include "resolved.h"
#include "resolved-dns-domain.h"
static void bus_method_resolve_hostname_complete(DnsQuery *q) {
int r;
assert(q);
switch(q->state) {
case DNS_QUERY_SKIPPED:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
break;
case DNS_QUERY_TIMEOUT:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
break;
case DNS_QUERY_ATTEMPTS_MAX:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
break;
case DNS_QUERY_FAILURE: {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
if (q->rcode == DNS_RCODE_NXDOMAIN)
sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "Hostname %s does not exist", q->request_hostname);
else {
const char *rc, *n;
char p[DECIMAL_STR_MAX(q->rcode)];
rc = dns_rcode_to_string(q->rcode);
if (!rc) {
sprintf(p, "%i", q->rcode);
rc = p;
}
n = strappenda(_BUS_ERROR_DNS, rc);
sd_bus_error_setf(&error, n, "Could not resolve hostname %s, server or network returned error %s", q->request_hostname, rc);
}
r = sd_bus_reply_method_error(q->request, &error);
break;
}
case DNS_QUERY_SUCCESS: {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
unsigned i, n, added = 0;
assert(q->packet);
r = dns_packet_skip_question(q->packet);
if (r < 0)
goto parse_fail;
r = sd_bus_message_new_method_return(q->request, &reply);
if (r < 0)
goto finish;
r = sd_bus_message_open_container(reply, 'a', "(yayi)");
if (r < 0)
goto finish;
n = be16toh(DNS_PACKET_HEADER(q->packet)->ancount) +
be16toh(DNS_PACKET_HEADER(q->packet)->nscount) +
be16toh(DNS_PACKET_HEADER(q->packet)->arcount);
for (i = 0; i < n; i++) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
r = dns_packet_read_rr(q->packet, &rr, NULL);
if (r < 0)
goto parse_fail;
if (rr->key.class != DNS_CLASS_IN)
continue;
if (!(q->request_family != AF_INET6 && rr->key.type == DNS_TYPE_A) &&
!(q->request_family != AF_INET && rr->key.type == DNS_TYPE_AAAA))
continue;
if (!dns_name_equal(rr->key.name, q->request_hostname))
continue;
r = sd_bus_message_open_container(reply, 'r', "yayi");
if (r < 0)
goto finish;
if (rr->key.type == DNS_TYPE_A) {
r = sd_bus_message_append(reply, "y", AF_INET);
if (r < 0)
goto finish;
r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
} else {
r = sd_bus_message_append(reply, "y", AF_INET6);
if (r < 0)
goto finish;
r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
}
if (r < 0)
goto finish;
r = sd_bus_message_append(reply, "i", q->packet->ifindex);
if (r < 0)
goto finish;
r = sd_bus_message_close_container(reply);
if (r < 0)
goto finish;
added ++;
}
if (added <= 0)
goto parse_fail;
r = sd_bus_message_close_container(reply);
if (r < 0)
goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL);
break;
}
parse_fail:
case DNS_QUERY_INVALID_REPLY:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
break;
case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
assert_not_reached("Unexpected query state");
}
finish:
if (r < 0)
log_error("Failed to send bus reply: %s", strerror(-r));
dns_query_free(q);
}
static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
const char *hostname;
uint8_t family;
DnsResourceKey keys[2];
DnsQuery *q;
unsigned n = 0;
int r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "sy", &hostname, &family);
if (r < 0)
return r;
if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %u", family);
if (!hostname_is_valid(hostname))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
if (family != AF_INET6) {
keys[n].class = DNS_CLASS_IN;
keys[n].type = DNS_TYPE_A;
keys[n].name = (char*) hostname;
n++;
}
if (family != AF_INET) {
keys[n].class = DNS_CLASS_IN;
keys[n].type = DNS_TYPE_AAAA;
keys[n].name = (char*) hostname;
n++;
}
r = dns_query_new(m, &q, keys, n);
if (r < 0)
return r;
q->request = sd_bus_message_ref(message);
q->request_family = family;
q->request_hostname = hostname;
q->complete = bus_method_resolve_hostname_complete;
r = dns_query_start(q);
if (r < 0) {
dns_query_free(q);
return r;
}
return 1;
}
static void bus_method_resolve_address_complete(DnsQuery *q) {
_cleanup_free_ char *ip = NULL;
int r;
assert(q);
in_addr_to_string(q->request_family, &q->request_address, &ip);
switch(q->state) {
case DNS_QUERY_SKIPPED:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
break;
case DNS_QUERY_TIMEOUT:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
break;
case DNS_QUERY_ATTEMPTS_MAX:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
break;
case DNS_QUERY_FAILURE: {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
if (q->rcode == DNS_RCODE_NXDOMAIN)
sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "No hostname known for address %s ", ip);
else {
const char *rc, *n;
char p[DECIMAL_STR_MAX(q->rcode)];
rc = dns_rcode_to_string(q->rcode);
if (!rc) {
sprintf(p, "%i", q->rcode);
rc = p;
}
n = strappenda(_BUS_ERROR_DNS, rc);
sd_bus_error_setf(&error, n, "Could not resolve address %s, server or network returned error %s", ip, rc);
}
r = sd_bus_reply_method_error(q->request, &error);
break;
}
case DNS_QUERY_SUCCESS: {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
unsigned i, n, added = 0;
_cleanup_free_ char *reverse = NULL;
assert(q->packet);
r = dns_name_reverse(q->request_family, &q->request_address, &reverse);
if (r < 0)
goto finish;
r = dns_packet_skip_question(q->packet);
if (r < 0)
goto parse_fail;
r = sd_bus_message_new_method_return(q->request, &reply);
if (r < 0)
goto finish;
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
goto finish;
n = be16toh(DNS_PACKET_HEADER(q->packet)->ancount) +
be16toh(DNS_PACKET_HEADER(q->packet)->nscount) +
be16toh(DNS_PACKET_HEADER(q->packet)->arcount);
for (i = 0; i < n; i++) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
r = dns_packet_read_rr(q->packet, &rr, NULL);
if (r < 0)
goto parse_fail;
if (rr->key.class != DNS_CLASS_IN)
continue;
if (rr->key.type != DNS_TYPE_PTR)
continue;
if (!dns_name_equal(rr->key.name, reverse))
continue;
r = sd_bus_message_append(reply, "s", rr->ptr.name);
if (r < 0)
goto finish;
added ++;
}
if (added <= 0)
goto parse_fail;
r = sd_bus_message_close_container(reply);
if (r < 0)
goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL);
break;
}
parse_fail:
case DNS_QUERY_INVALID_REPLY:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
break;
case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
assert_not_reached("Unexpected query state");
}
finish:
if (r < 0)
log_error("Failed to send bus reply: %s", strerror(-r));
dns_query_free(q);
}
static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(dns_resource_key_free) DnsResourceKey key = {};
Manager *m = userdata;
uint8_t family;
const void *d;
int ifindex;
DnsQuery *q;
size_t sz;
int r;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "y", &family);
if (r < 0)
return r;
if (!IN_SET(family, AF_INET, AF_INET6))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %u", family);
r = sd_bus_message_read_array(message, 'y', &d, &sz);
if (r < 0)
return r;
if ((family == AF_INET && sz != sizeof(struct in_addr)) ||
(family == AF_INET6 && sz != sizeof(struct in6_addr)))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
r = sd_bus_message_read(message, "i", &ifindex);
if (r < 0)
return r;
if (ifindex < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
key.class = DNS_CLASS_IN;
key.type = DNS_TYPE_PTR;
r = dns_name_reverse(family, d, &key.name);
if (r < 0)
return r;
r = dns_query_new(m, &q, &key, 1);
if (r < 0)
return r;
q->request = sd_bus_message_ref(message);
q->request_family = family;
memcpy(&q->request_address, d, sz);
q->complete = bus_method_resolve_address_complete;
r = dns_query_start(q);
if (r < 0) {
dns_query_free(q);
return r;
}
return 1;
}
static const sd_bus_vtable resolve_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("ResolveHostname", "sy", "a(yayi)", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveAddress", "yayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END,
};
static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
Manager *m = userdata;
assert(s);
assert(m);
m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
manager_connect_bus(m);
return 0;
}
int manager_connect_bus(Manager *m) {
int r;
assert(m);
if (m->bus)
return 0;
r = sd_bus_default_system(&m->bus);
if (r < 0) {
/* We failed to connect? Yuck, we must be in early
* boot. Let's try in 5s again. As soon as we have
* kdbus we can stop doing this... */
log_debug("Failed to connect to bus, trying again in 5s: %s", strerror(-r));
r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
if (r < 0) {
log_error("Failed to install bus reconnect time event: %s", strerror(-r));
return r;
}
return 0;
}
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1", resolve_vtable, m);
if (r < 0) {
log_error("Failed to register object: %s", strerror(-r));
return r;
}
r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
r = sd_bus_attach_event(m->bus, m->event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));
return r;
}
return 0;
}

381
src/resolve/resolved-dns-domain.c

@ -0,0 +1,381 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 "resolved-dns-domain.h"
int dns_label_unescape(const char **name, char *dest, size_t sz) {
const char *n;
char *d;
int r = 0;
assert(name);
assert(*name);
assert(dest);
n = *name;
d = dest;
for (;;) {
if (*n == '.') {
n++;
break;
}
if (*n == 0)
break;
if (sz <= 0)
return -ENOSPC;
if (*n == '\\') {
/* Escaped character */
n++;
if (*n == 0)
/* Ending NUL */
return -EINVAL;
else if (*n == '\\' || *n == '.') {
/* Escaped backslash or dot */
*(d++) = *(n++);
sz--;
r++;
} else if (n[0] >= '0' && n[0] <= '9') {
unsigned k;
/* Escaped literal ASCII character */
if (!(n[1] >= '0' && n[1] <= '9') ||
!(n[2] >= '0' && n[2] <= '9'))
return -EINVAL;
k = ((unsigned) (n[0] - '0') * 100) +
((unsigned) (n[1] - '0') * 10) +
((unsigned) (n[2] - '0'));
/* Don't allow CC characters or anything that doesn't fit in 8bit */
if (k < ' ' || k > 255 || k == 127)
return -EINVAL;
*(d++) = (char) k;
sz--;
r++;
n += 3;
} else
return -EINVAL;
} else if (*n >= ' ' && *n != 127) {
/* Normal character */
*(d++) = *(n++);
sz--;
r++;
} else
return -EINVAL;
}
/* Empty label that is not at the end? */
if (r == 0 && *n)
return -EINVAL;
if (sz >= 1)
*d = 0;
*name = n;
return r;
}
int dns_label_escape(const char *p, size_t l, char **ret) {
_cleanup_free_ char *s = NULL;
char *q;
int r;
assert(p);
assert(ret);
s = malloc(l * 4 + 1);
if (!s)
return -ENOMEM;
q = s;
while (l > 0) {
if (*p == '.' || *p == '\\') {
/* Dot or backslash */
*(q++) = '\\';
*(q++) = *p;
} else if (*p == '_' ||
*p == '-' ||
(*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z')) {
/* Proper character */
*(q++) = *p;
} else if (*p >= ' ' && *p != 127) {
/* Everything else */
*(q++) = '\\';
*(q++) = '0' + (char) ((unsigned) *p / 100);
*(q++) = '0' + (char) (((unsigned) *p / 10) % 10);
*(q++) = '0' + (char) ((unsigned) *p % 10);
} else
return -EINVAL;
p++;
l--;
}
*q = 0;
*ret = s;
r = q - s;
s = NULL;
return r;
}
int dns_name_normalize(const char *s, char **_ret) {
_cleanup_free_ char *ret = NULL;
size_t n = 0, allocated = 0;
const char *p = s;
bool first = true;
int r;
assert(s);
assert(_ret);
for (;;) {
_cleanup_free_ char *t = NULL;
char label[DNS_LABEL_MAX];
r = dns_label_unescape(&p, label, sizeof(label));
if (r < 0)
return r;
if (r == 0) {
if (*p != 0)
return -EINVAL;
break;
}
r = dns_label_escape(label, r, &t);
if (r < 0)
return r;
if (!GREEDY_REALLOC(ret, allocated, n + !first + strlen(t) + 1))
return -ENOMEM;
if (!first)
ret[n++] = '.';
else
first = false;
memcpy(ret + n, t, r);
n += r;
}
if (!GREEDY_REALLOC(ret, allocated, n + 1))
return -ENOMEM;
ret[n] = 0;
*_ret = ret;
ret = NULL;
return 0;
}
unsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]) {
const char *p = s;
unsigned long ul = 0;
int r;
assert(p);
while (*p) {
char label[DNS_LABEL_MAX+1];
r = dns_label_unescape(&p, label, sizeof(label));
if (r < 0)
break;
label[r] = 0;
ascii_strlower(label);
ul = hash_key[0] * ul + ul + string_hash_func(label, hash_key);
}
return ul;
}
int dns_name_compare_func(const void *a, const void *b) {
const char *x = a, *y = b;
int r, q;
assert(a);
assert(b);
for (;;) {
char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
if (*x == 0 && *y == 0)
return 0;
r = dns_label_unescape(&x, la, sizeof(la));
q = dns_label_unescape(&y, lb, sizeof(lb));
if (r < 0 || q < 0)
return r - q;
la[r] = lb[q] = 0;
r = strcasecmp(la, lb);
if (r != 0)
return r;
}
}
int dns_name_equal(const char *x, const char *y) {
int r, q;
assert(x);
assert(y);
for (;;) {
char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
if (*x == 0 && *y == 0)
return true;
r = dns_label_unescape(&x, la, sizeof(la));
if (r < 0)
return r;
q = dns_label_unescape(&y, lb, sizeof(lb));
if (q < 0)
return q;
la[r] = lb[q] = 0;
if (strcasecmp(la, lb))
return false;
}
}
int dns_name_endswith(const char *name, const char *suffix) {
const char *n, *s, *saved_n = NULL;
int r, q;
assert(name);
assert(suffix);
n = name;
s = suffix;
for (;;) {
char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1];
r = dns_label_unescape(&n, ln, sizeof(ln));
if (r < 0)
return r;
if (!saved_n)
saved_n = n;
q = dns_label_unescape(&s, ls, sizeof(ls));
if (r < 0)
return r;
if (r == 0 && q == 0)
return true;
if (r == 0 && saved_n == n)
return false;
ln[r] = ls[q] = 0;
if (r != q || strcasecmp(ln, ls)) {
/* Not the same, let's jump back, and try with the next label again */
s = suffix;
n = saved_n;
saved_n = NULL;
}
}
}
int dns_name_reverse(int family, const union in_addr_union *a, char **ret) {
const uint8_t *p;
int r;
assert(a);
assert(ret);
p = (const uint8_t*) a;
if (family == AF_INET)
r = asprintf(ret, "%u.%u.%u.%u.in-addr.arpa", p[3], p[2], p[1], p[0]);
else if (family == AF_INET6)
r = asprintf(ret, "%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.ip6.arpa",
hexchar(p[15]), hexchar(p[14]), hexchar(p[13]), hexchar(p[12]),
hexchar(p[11]), hexchar(p[10]), hexchar(p[ 9]), hexchar(p[ 8]),
hexchar(p[ 7]), hexchar(p[ 6]), hexchar(p[ 5]), hexchar(p[ 4]),
hexchar(p[ 3]), hexchar(p[ 2]), hexchar(p[ 1]), hexchar(p[ 0]));
else
return -EAFNOSUPPORT;
if (r < 0)
return -ENOMEM;
return 0;
}
int dns_name_root(const char *name) {
char label[DNS_LABEL_MAX+1];
int r;
assert(name);
r = dns_label_unescape(&name, label, sizeof(label));
if (r < 0)
return r;
return r == 0 && *name == 0;
}
int dns_name_single_label(const char *name) {
char label[DNS_LABEL_MAX+1];
int r;
assert(name);
r = dns_label_unescape(&name, label, sizeof(label));
if (r < 0)
return r;
if (r == 0)
return 0;
r = dns_label_unescape(&name, label, sizeof(label));
if (r < 0)
return r;
return r == 0 && *name == 0;
}

43
src/resolve/resolved-dns-domain.h

@ -0,0 +1,43 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 <sys/types.h>
#include "hashmap.h"
#include "in-addr-util.h"
#define DNS_LABEL_MAX 63
int dns_label_unescape(const char **name, char *dest, size_t sz);
int dns_label_escape(const char *p, size_t l, char **ret);
int dns_name_normalize(const char *s, char **_ret);
unsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]);
int dns_name_compare_func(const void *a, const void *b);
int dns_name_equal(const char *x, const char *y);
int dns_name_endswith(const char *name, const char *suffix);
int dns_name_reverse(int family, const union in_addr_union *a, char **ret);
int dns_name_root(const char *name);
int dns_name_single_label(const char *name);

734
src/resolve/resolved-dns-packet.c

@ -0,0 +1,734 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
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 "utf8.h"
#include "resolved-dns-domain.h"
#include "resolved-dns-packet.h"
int dns_packet_new(DnsPacket **ret, size_t mtu) {
DnsPacket *p;
size_t a;
assert(ret);
if (mtu <= 0)
a = DNS_PACKET_SIZE_START;
else
a = mtu;
if (a < DNS_PACKET_HEADER_SIZE)
a = DNS_PACKET_HEADER_SIZE;
p = malloc0(ALIGN(sizeof(DnsPacket)) + a);
if (!p)
return -ENOMEM;
p->size = p->rindex = DNS_PACKET_HEADER_SIZE;
p->allocated = a;
p->n_ref = 1;
*ret = p;
return 0;
}
int dns_packet_new_query(DnsPacket **ret, size_t mtu) {
DnsPacket *p;
DnsPacketHeader *h;
int r;
assert(ret);
r = dns_packet_new(&p, mtu);
if (r < 0)
return r;
h = DNS_PACKET_HEADER(p);
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0, 0, 0, 0, 1, 0, 0, 0, 0));
*ret = p;
return 0;
}
DnsPacket *dns_packet_ref(DnsPacket *p) {
if (!p)
return NULL;
assert(p->n_ref > 0);
p->n_ref++;
return p;
}
static void dns_packet_free(DnsPacket *p) {
char *s;
assert(p);
while ((s = hashmap_steal_first_key(p->names)))
free(s);
hashmap_free(p->names);
free(p->data);
free(p);
}
DnsPacket *dns_packet_unref(DnsPacket *p) {
if (!p)
return NULL;
assert(p->n_ref > 0);
if (p->n_ref == 1)
dns_packet_free(p);
else
p->n_ref--;
return NULL;
}
int dns_packet_validate(DnsPacket *p) {
assert(p);
if (p->size < DNS_PACKET_HEADER_SIZE)
return -EBADMSG;
return 0;
}
int dns_packet_validate_reply(DnsPacket *p) {
DnsPacketHeader *h;
int r;
assert(p);
r = dns_packet_validate(p);
if (r < 0)
return r;
h = DNS_PACKET_HEADER(p);
/* Check QR field */
if ((be16toh(h->flags) & 1) == 0)
return -EBADMSG;
/* Check opcode field */
if (((be16toh(h->flags) >> 1) & 15) != 0)
return -EBADMSG;
return 0;
}
static int dns_packet_extend(DnsPacket *p, size_t add, void **ret, size_t *start) {
assert(p);
if (p->size + add > p->allocated)
return -ENOMEM;
if (start)
*start = p->size;
if (ret)
*ret = (uint8_t*) DNS_PACKET_DATA(p) + p->size;
p->size += add;
return 0;
}
static void dns_packet_truncate(DnsPacket *p, size_t sz) {
Iterator i;
char *s;
void *n;
assert(p);
if (p->size <= sz)
return;
HASHMAP_FOREACH_KEY(s, n, p->names, i) {
if (PTR_TO_SIZE(n) < sz)
continue;
hashmap_remove(p->names, s);
free(s);
}
p->size = sz;
}
int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start) {
void *d;
int r;
assert(p);
r = dns_packet_extend(p, sizeof(uint8_t), &d, start);
if (r < 0)
return r;
((uint8_t*) d)[0] = v;
return 0;
}
int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start) {
void *d;
int r;
assert(p);
r = dns_packet_extend(p, sizeof(uint16_t), &d, start);
if (r < 0)
return r;
((uint8_t*) d)[0] = (uint8_t) (v >> 8);
((uint8_t*) d)[1] = (uint8_t) (v & 255);
return 0;
}
int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start) {
void *d;
size_t l;
int r;
assert(p);
assert(s);
l = strlen(s);
if (l > 255)
return -E2BIG;
r = dns_packet_extend(p, 1 + l, &d, start);
if (r < 0)
return r;
((uint8_t*) d)[0] = (uint8_t) l;
memcpy(((uint8_t*) d) + 1, s, l);
return 0;
}
int dns_packet_append_label(DnsPacket *p, const char *d, size_t l, size_t *start) {
void *w;
int r;
assert(p);
assert(d);
if (l > DNS_LABEL_MAX)
return -E2BIG;
r = dns_packet_extend(p, 1 + l, &w, start);
if (r < 0)
return r;
((uint8_t*) w)[0] = (uint8_t) l;
memcpy(((uint8_t*) w) + 1, d, l);
return 0;
}
int dns_packet_append_name(DnsPacket *p, const char *name, size_t *start) {
size_t saved_size;
int r;
assert(p);
assert(name);
saved_size = p->size;
while (*name) {
_cleanup_free_ char *s = NULL;
char label[DNS_LABEL_MAX];
size_t n;
n = PTR_TO_SIZE(hashmap_get(p->names, name));
if (n > 0) {
assert(n < p->size);
if (n < 0x4000) {
r = dns_packet_append_uint16(p, 0xC000 | n, NULL);
if (r < 0)
goto fail;
goto done;
}
}
s = strdup(name);
if (!s) {
r = -ENOMEM;
goto fail;
}
r = dns_label_unescape(&name, label, sizeof(label));
if (r < 0)
goto fail;
r = dns_packet_append_label(p, label, r, &n);
if (r < 0)
goto fail;
r = hashmap_ensure_allocated(&p->names, dns_name_hash_func, dns_name_compare_func);
if (r < 0)
goto fail;
r = hashmap_put(p->names, s, SIZE_TO_PTR(n));
if (r < 0)
goto fail;
s = NULL;
}
r = dns_packet_append_uint8(p, 0, NULL);
if (r < 0)
return r;
done:
if (start)
*start = saved_size;
return 0;
fail:
dns_packet_truncate(p, saved_size);
return r;
}
int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *k, size_t *start) {
size_t saved_size;
int r;
assert(p);
assert(k);
saved_size = p->size;
r = dns_packet_append_name(p, k->name, NULL);
if (r < 0)
goto fail;
r = dns_packet_append_uint16(p, k->type, NULL);
if (r < 0)
goto fail;
r = dns_packet_append_uint16(p, k->class, NULL);
if (r < 0)
goto fail;
if (start)
*start = saved_size;
return 0;
fail:
dns_packet_truncate(p, saved_size);
return r;
}
int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start) {
assert(p);
if (p->rindex + sz > p->size)
return -EMSGSIZE;
if (ret)
*ret = (uint8_t*) DNS_PACKET_DATA(p) + p->rindex;
if (start)
*start = p->rindex;
p->rindex += sz;
return 0;
}
static void dns_packet_rewind(DnsPacket *p, size_t idx) {
assert(p);
assert(idx <= p->size);
assert(idx >= DNS_PACKET_HEADER_SIZE);
p->rindex = idx;
}
int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start) {
const void *d;
int r;
assert(p);
r = dns_packet_read(p, sizeof(uint8_t), &d, start);
if (r < 0)
return r;
*ret = ((uint8_t*) d)[0];
return 0;
}
int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start) {
const void *d;
int r;
assert(p);
r = dns_packet_read(p, sizeof(uint16_t), &d, start);
if (r < 0)
return r;
*ret = (((uint16_t) ((uint8_t*) d)[0]) << 8) |
((uint16_t) ((uint8_t*) d)[1]);
return 0;
}
int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start) {
const void *d;
int r;
assert(p);
r = dns_packet_read(p, sizeof(uint32_t), &d, start);
if (r < 0)
return r;
*ret = (((uint32_t) ((uint8_t*) d)[0]) << 24) |
(((uint32_t) ((uint8_t*) d)[1]) << 16) |
(((uint32_t) ((uint8_t*) d)[2]) << 8) |
((uint32_t) ((uint8_t*) d)[3]);
return 0;
}
int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start) {
size_t saved_rindex;
const void *d;
char *t;
uint8_t c;
int r;
assert(p);
saved_rindex = p->rindex;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
r = dns_packet_read(p, c, &d, NULL);
if (r < 0)
goto fail;
if (memchr(d, 0, c)) {
r = -EBADMSG;
goto fail;
}
t = strndup(d, c);
if (!t) {
r = -ENOMEM;
goto fail;
}
if (!utf8_is_valid(t)) {
free(t);
r = -EBADMSG;
goto fail;
}
*ret = t;
if (start)
*start = saved_rindex;
return 0;
fail:
dns_packet_rewind(p, saved_rindex);
return r;
}
int dns_packet_read_name(DnsPacket *p, char **_ret, size_t *start) {
size_t saved_rindex, after_rindex = 0;
_cleanup_free_ char *ret = NULL;
size_t n = 0, allocated = 0;
bool first = true;
int r;
assert(p);
assert(_ret);
saved_rindex = p->rindex;
for (;;) {
uint8_t c, d;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
if (c == 0)
/* End of name */
break;
else if (c <= 63) {
_cleanup_free_ char *t = NULL;
const char *label;
/* Literal label */
r = dns_packet_read(p, c, (const void**) &label, NULL);
if (r < 0)
goto fail;
r = dns_label_escape(label, c, &t);
if (r < 0)
goto fail;
if (!GREEDY_REALLOC(ret, allocated, n + !first + strlen(t) + 1)) {
r = -ENOMEM;
goto fail;
}
if (!first)
ret[n++] = '.';
else
first = false;
memcpy(ret + n, t, c);
n += r;
continue;
} else if ((c & 0xc0) == 0xc0) {
uint16_t ptr;
/* Pointer */
r = dns_packet_read_uint8(p, &d, NULL);
if (r < 0)
goto fail;
ptr = (uint16_t) (c & ~0xc0) << 8 | (uint16_t) d;
if (ptr < DNS_PACKET_HEADER_SIZE || ptr >= saved_rindex) {
r = -EBADMSG;
goto fail;
}
if (after_rindex == 0)
after_rindex = p->rindex;
p->rindex = ptr;
} else
goto fail;
}
if (!GREEDY_REALLOC(ret, allocated, n + 1)) {
r = -ENOMEM;
goto fail;
}
ret[n] = 0;
if (after_rindex != 0)
p->rindex= after_rindex;
*_ret = ret;
ret = NULL;
if (start)
*start = saved_rindex;
return 0;
fail:
dns_packet_rewind(p, saved_rindex);
return r;
}
int dns_packet_read_key(DnsPacket *p, DnsResourceKey *ret, size_t *start) {
_cleanup_(dns_resource_key_free) DnsResourceKey k = {};
size_t saved_rindex;
int r;
assert(p);
assert(ret);
saved_rindex = p->rindex;
r = dns_packet_read_name(p, &k.name, NULL);
if (r < 0)
goto fail;
r = dns_packet_read_uint16(p, &k.type, NULL);
if (r < 0)
goto fail;
r = dns_packet_read_uint16(p, &k.class, NULL);
if (r < 0)
goto fail;
*ret = k;
zero(k);
if (start)
*start = saved_rindex;
return 0;
fail:
dns_packet_rewind(p, saved_rindex);
return r;
}
int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
size_t saved_rindex, offset;
uint16_t rdlength;
const void *d;
int r;
assert(p);
assert(ret);
saved_rindex = p->rindex;
rr = dns_resource_record_new();
if (!rr)
goto fail;
r = dns_packet_read_key(p, &rr->key, NULL);
if (r < 0)
goto fail;
r = dns_packet_read_uint32(p, &rr->ttl, NULL);
if (r < 0)
goto fail;
r = dns_packet_read_uint16(p, &rdlength, NULL);
if (r < 0)
goto fail;
if (p->rindex + rdlength > p->size) {
r = -EBADMSG;
goto fail;