[systemd-commits] 2 commits - .gitignore Makefile.am src/resolve src/resolve-host
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Jul 30 07:49:10 PDT 2014
.gitignore | 1
Makefile.am | 10 +
src/resolve-host/Makefile | 1
src/resolve-host/resolve-host.c | 270 ++++++++++++++++++++++++++++++++++++++
src/resolve/resolved-dns-answer.c | 24 +++
src/resolve/resolved-dns-answer.h | 1
src/resolve/resolved-dns-rr.c | 10 +
src/resolve/resolved-dns-rr.h | 1
src/resolve/resolved-dns-scope.c | 28 ++-
src/resolve/resolved-dns-zone.c | 83 +++++++----
src/resolve/resolved-dns-zone.h | 2
11 files changed, 389 insertions(+), 42 deletions(-)
New commits:
commit bdef7319e4145fec3160468a1ef9775d682f2fa4
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Wed Jul 16 22:55:23 2014 +0200
resolved: add tool to query resolved
diff --git a/.gitignore b/.gitignore
index 41fff4f..73e8b6b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -100,6 +100,7 @@
/systemd-remount-api-vfs
/systemd-remount-fs
/systemd-reply-password
+/systemd-resolve-host
/systemd-resolved
/systemd-rfkill
/systemd-run
diff --git a/Makefile.am b/Makefile.am
index 9a3913b..9e243eb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4847,6 +4847,16 @@ libnss_resolve_la_LIBADD = \
lib_LTLIBRARIES += \
libnss_resolve.la
+systemd_resolve_host_SOURCES = \
+ src/resolve-host/resolve-host.c
+
+systemd_resolve_host_LDADD = \
+ libsystemd-internal.la \
+ libsystemd-shared.la
+
+rootlibexec_PROGRAMS += \
+ systemd-resolve-host
+
endif
# ------------------------------------------------------------------------------
diff --git a/src/resolve-host/Makefile b/src/resolve-host/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/src/resolve-host/Makefile
@@ -0,0 +1 @@
+../Makefile
\ No newline at end of file
diff --git a/src/resolve-host/resolve-host.c b/src/resolve-host/resolve-host.c
new file mode 100644
index 0000000..61e2c39
--- /dev/null
+++ b/src/resolve-host/resolve-host.c
@@ -0,0 +1,270 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Zbigniew JÄdrzejewski-Szmek
+
+ 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 <arpa/inet.h>
+#include <net/if.h>
+#include <getopt.h>
+
+#include "sd-bus.h"
+#include "bus-util.h"
+#include "bus-error.h"
+#include "bus-errors.h"
+#include "in-addr-util.h"
+#include "af-list.h"
+#include "build.h"
+
+#define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
+
+static int arg_family = AF_UNSPEC;
+static int arg_ifindex = 0;
+
+static int resolve_host(sd_bus *bus, const char *name, int _family, int _ifindex) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ unsigned c = 0;
+ int r;
+
+ assert(name);
+
+ log_debug("Resolving %s (family %s)",
+ name, af_to_name(_family));
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ &req,
+ "org.freedesktop.resolve1",
+ "/org/freedesktop/resolve1",
+ "org.freedesktop.resolve1.Manager",
+ "ResolveHostname");
+ if (r < 0) {
+ log_error("sd_bus_message_new_method_call: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_bus_message_set_auto_start(req, false);
+ if (r < 0) {
+ log_error("sd_bus_message_set_auto_start: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_bus_message_append(req, "si", name, AF_UNSPEC);
+ if (r < 0) {
+ log_error("sd_bus_message_append: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
+ if (r < 0) {
+ log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
+ return r;
+ }
+
+ r = sd_bus_message_enter_container(reply, 'a', "(iayi)");
+ if (r < 0) {
+ log_error("%s: failed to parse reply: %s", name, bus_error_message(&error, r));
+ return r;
+ }
+
+ while ((r = sd_bus_message_enter_container(reply, 'r', "iayi")) > 0) {
+ const void *a;
+ int family, ifindex;
+ size_t sz;
+ _cleanup_free_ char *pretty = NULL;
+ char ifname[IF_NAMESIZE] = "";
+
+ r = sd_bus_message_read(reply, "i", &family);
+ if (r < 0) {
+ log_error("Cannot parse message, aborting.");
+ return -EBADMSG;
+ }
+
+ r = sd_bus_message_read_array(reply, 'y', &a, &sz);
+ if (r < 0) {
+ log_error("Cannot parse message, aborting.");
+ return -EBADMSG;
+ }
+
+ r = sd_bus_message_read(reply, "i", &ifindex);
+ if (r < 0) {
+ log_error("Cannot parse message, aborting.");
+ return -EBADMSG;
+ }
+
+ r = sd_bus_message_exit_container(reply);
+ if (r < 0) {
+ log_error("Cannot parse message, aborting.");
+ return -EBADMSG;
+ }
+
+ if ((_family != AF_UNSPEC && family != _family) ||
+ !IN_SET(family, AF_INET, AF_INET6)) {
+ log_debug("%s: skipping entry with family %hu (%s)",
+ name, family, af_to_name(family) ?: "unknown");
+ continue;
+ }
+
+ if (sz != FAMILY_ADDRESS_SIZE(family)) {
+ log_error("%s: systemd-resolved returned address of invalid size %zu for family %s",
+ name, sz, af_to_name(family) ?: "unknown");
+ continue;
+ }
+
+ if (ifindex < 0) {
+ log_error("%s: systemd-resolved returned invalid interface index %i",
+ name, ifindex);
+ continue;
+ }
+
+ if (ifindex > 0) {
+ char *t;
+
+ t = if_indextoname(ifindex, ifname);
+ if (!t) {
+ log_error("Failed to resolve interface name for index %i", ifindex);
+ continue;
+ }
+ }
+
+ if (_ifindex > 0 && ifindex > 0 && ifindex != _ifindex) {
+ log_debug("%s: skipping entry with ifindex %i (%s)",
+ name, ifindex, ifname);
+ continue;
+ }
+
+ r = in_addr_to_string(family, a, &pretty);
+ if (r < 0) {
+ log_error("%s: failed to print address: %s", name, strerror(-r));
+ continue;
+ }
+
+ log_info("%*s%s %s%s%.*s",
+ (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
+ pretty,
+ *ifname ? "%" : "", (int) sizeof(ifname), *ifname ? ifname: "");
+
+ c++;
+ }
+
+ if (c == 0) {
+ log_error("%s: no addresses found", name);
+ return -ENONET;
+ }
+
+ return sd_bus_message_exit_container(reply);
+}
+
+static void help(void) {
+ printf("%s [OPTIONS...]\n\n"
+ "Resolve IPv4 or IPv6 addresses.\n\n"
+ "Options:\n"
+ " -4 Resolve IPv4 addresses\n"
+ " -6 Resolve IPv6 addresses\n"
+ " -i INTERFACE Filter by interface\n"
+ " -h --help Show this help and exit\n"
+ " --version Print version string and exit\n"
+ , program_invocation_short_name
+ );
+}
+
+static int parse_argv(int argc, char *argv[]) {
+ enum {
+ ARG_VERSION = 0x100,
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
+ {}
+ };
+
+ int c;
+
+ assert(argc >= 0);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "h46i:", options, NULL)) >= 0)
+ switch(c) {
+
+ case 'h':
+ help();
+ return 0; /* done */;
+
+ case ARG_VERSION:
+ puts(PACKAGE_STRING);
+ puts(SYSTEMD_FEATURES);
+ return 0 /* done */;
+
+ case '4':
+ arg_family = AF_INET;
+ break;
+
+ case '6':
+ arg_family = AF_INET6;
+ break;
+
+ case 'i':
+ arg_ifindex = if_nametoindex(optarg);
+ if (arg_ifindex <= 0) {
+ log_error("Unknown interfaces %s: %m", optarg);
+ return -EINVAL;
+ }
+ break;
+
+ case '?':
+ return -EINVAL;
+
+ default:
+ assert_not_reached("Unhandled option");
+ }
+
+ return 1 /* work to do */;
+}
+
+
+int main(int argc, char **argv) {
+ _cleanup_bus_unref_ sd_bus *bus = NULL;
+ int r;
+
+ log_parse_environment();
+ log_open();
+
+ r = parse_argv(argc, argv);
+ if (r <= 0)
+ goto end;
+
+ r = sd_bus_open_system(&bus);
+ if (r < 0) {
+ log_error("sd_bus_open_system: %s", strerror(-r));
+ goto end;
+ }
+
+ while (argv[optind]) {
+ int k;
+
+ k = resolve_host(bus, argv[optind++], arg_family, arg_ifindex);
+ if (r == 0)
+ r = k;
+ }
+
+ end:
+ return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
commit 8bf52d3d17d364438191077d0750b8b80b5dc53a
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Jul 30 16:30:25 2014 +0200
resolved: include SOA records in LLMNR replies for non-existing RRs to allow negative caching
diff --git a/src/resolve/resolved-dns-answer.c b/src/resolve/resolved-dns-answer.c
index 6096959..b6883a3 100644
--- a/src/resolve/resolved-dns-answer.c
+++ b/src/resolve/resolved-dns-answer.c
@@ -97,6 +97,30 @@ int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr) {
return 1;
}
+int dns_answer_add_soa(DnsAnswer *a, const char *name) {
+ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL;
+
+ soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name);
+ if (!soa)
+ return -ENOMEM;
+
+ soa->soa.mname = strdup(name);
+ if (!soa->soa.mname)
+ return -ENOMEM;
+
+ soa->soa.rname = strappend("root.", name);
+ if (!soa->soa.rname)
+ return -ENOMEM;
+
+ soa->soa.serial = 1;
+ soa->soa.refresh = 1;
+ soa->soa.retry = 1;
+ soa->soa.expire = 1;
+ soa->soa.minimum = 1;
+
+ return dns_answer_add(a, soa);
+}
+
int dns_answer_contains(DnsAnswer *a, DnsResourceKey *key) {
unsigned i;
int r;
diff --git a/src/resolve/resolved-dns-answer.h b/src/resolve/resolved-dns-answer.h
index 268bb38..917bfce 100644
--- a/src/resolve/resolved-dns-answer.h
+++ b/src/resolve/resolved-dns-answer.h
@@ -38,6 +38,7 @@ DnsAnswer *dns_answer_ref(DnsAnswer *a);
DnsAnswer *dns_answer_unref(DnsAnswer *a);
int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr);
+int dns_answer_add_soa(DnsAnswer *a, const char *name);
int dns_answer_contains(DnsAnswer *a, DnsResourceKey *key);
int dns_answer_find_soa(DnsAnswer *a, DnsResourceKey *key, DnsResourceRecord **ret);
diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c
index 5097eff..8b88588 100644
--- a/src/resolve/resolved-dns-rr.c
+++ b/src/resolve/resolved-dns-rr.c
@@ -172,6 +172,16 @@ DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
return rr;
}
+DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
+ _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+
+ key = dns_resource_key_new(class, type, name);
+ if (!key)
+ return NULL;
+
+ return dns_resource_record_new(key);
+}
+
DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
if (!rr)
return NULL;
diff --git a/src/resolve/resolved-dns-rr.h b/src/resolve/resolved-dns-rr.h
index a9d14fc..50bb74c 100644
--- a/src/resolve/resolved-dns-rr.h
+++ b/src/resolve/resolved-dns-rr.h
@@ -140,6 +140,7 @@ int dns_resource_key_compare_func(const void *a, const void *b);
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref);
DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key);
+DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name);
DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr);
DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr);
int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name);
diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c
index b17de0c..5141a8d 100644
--- a/src/resolve/resolved-dns-scope.c
+++ b/src/resolve/resolved-dns-scope.c
@@ -389,14 +389,14 @@ int dns_scope_good_dns_server(DnsScope *s, int family, const union in_addr_union
return !!manager_find_dns_server(s->manager, family, address);
}
-static int dns_scope_make_reply_packet(DnsScope *s, uint16_t id, int rcode, DnsQuestion *q, DnsAnswer *a, DnsPacket **ret) {
+static int dns_scope_make_reply_packet(DnsScope *s, uint16_t id, int rcode, DnsQuestion *q, DnsAnswer *answer, DnsAnswer *soa, DnsPacket **ret) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
unsigned i;
int r;
assert(s);
- if (q->n_keys <= 0 && a->n_rrs <= 0)
+ if (q->n_keys <= 0 && answer->n_rrs <= 0 && soa->n_rrs <= 0)
return -EINVAL;
r = dns_packet_new(&p, s->protocol, 0);
@@ -425,14 +425,24 @@ static int dns_scope_make_reply_packet(DnsScope *s, uint16_t id, int rcode, DnsQ
DNS_PACKET_HEADER(p)->qdcount = htobe16(q->n_keys);
}
- if (a) {
- for (i = 0; i < a->n_rrs; i++) {
- r = dns_packet_append_rr(p, a->rrs[i], NULL);
+ if (answer) {
+ for (i = 0; i < answer->n_rrs; i++) {
+ r = dns_packet_append_rr(p, answer->rrs[i], NULL);
if (r < 0)
return r;
}
- DNS_PACKET_HEADER(p)->ancount = htobe16(a->n_rrs);
+ DNS_PACKET_HEADER(p)->ancount = htobe16(answer->n_rrs);
+ }
+
+ if (soa) {
+ for (i = 0; i < soa->n_rrs; i++) {
+ r = dns_packet_append_rr(p, soa->rrs[i], NULL);
+ if (r < 0)
+ return r;
+ }
+
+ DNS_PACKET_HEADER(p)->arcount = htobe16(soa->n_rrs);
}
*ret = p;
@@ -443,7 +453,7 @@ static int dns_scope_make_reply_packet(DnsScope *s, uint16_t id, int rcode, DnsQ
void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
_cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
- _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
+ _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
int r, fd;
assert(s);
@@ -475,7 +485,7 @@ void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
return;
}
- r = dns_zone_lookup(&s->zone, p->question, &answer);
+ r = dns_zone_lookup(&s->zone, p->question, &answer, &soa);
if (r < 0) {
log_debug("Failed to lookup key: %s", strerror(-r));
return;
@@ -485,7 +495,7 @@ void dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0);
- r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, &reply);
+ r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, &reply);
if (r < 0) {
log_debug("Failed to build reply packet: %s", strerror(-r));
return;
diff --git a/src/resolve/resolved-dns-zone.c b/src/resolve/resolved-dns-zone.c
index 65dc177..b51f503 100644
--- a/src/resolve/resolved-dns-zone.c
+++ b/src/resolve/resolved-dns-zone.c
@@ -192,21 +192,23 @@ int dns_zone_put(DnsZone *z, DnsResourceRecord *rr) {
return 0;
}
-int dns_zone_lookup(DnsZone *z, DnsQuestion *q, DnsAnswer **ret) {
- _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
- bool has_other_rrs = false;
- unsigned i, n = 0;
+int dns_zone_lookup(DnsZone *z, DnsQuestion *q, DnsAnswer **ret_answer, DnsAnswer **ret_soa) {
+ _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
+ unsigned i, n_answer = 0, n_soa = 0;
int r;
assert(z);
assert(q);
- assert(ret);
+ assert(ret_answer);
+ assert(ret_soa);
if (q->n_keys <= 0) {
- *ret = NULL;
+ *ret_answer = NULL;
+ *ret_soa = NULL;
return 0;
}
+ /* First iteration, count what we have */
for (i = 0; i < q->n_keys; i++) {
DnsZoneItem *j;
@@ -220,40 +222,46 @@ int dns_zone_lookup(DnsZone *z, DnsQuestion *q, DnsAnswer **ret) {
j = hashmap_get(z->by_name, DNS_RESOURCE_KEY_NAME(q->keys[i]));
LIST_FOREACH(by_name, j, j) {
- has_other_rrs = true;
-
k = dns_resource_key_match_rr(q->keys[i], j->rr);
if (k < 0)
return k;
if (k == 0)
- continue;
-
- n++;
+ n_soa++;
+ else
+ n_answer++;
}
} else {
j = hashmap_get(z->by_key, q->keys[i]);
- if (!j) {
+ if (j) {
+ LIST_FOREACH(by_key, j, j)
+ n_answer++;
+ } else {
if (hashmap_get(z->by_name, DNS_RESOURCE_KEY_NAME(q->keys[i])))
- has_other_rrs = true;
-
- continue;
+ n_soa ++;
}
-
- LIST_FOREACH(by_key, j, j)
- n++;
}
}
- if (n <= 0) {
- *ret = NULL;
- return has_other_rrs;
+ if (n_answer <= 0 && n_soa <= 0) {
+ *ret_answer = NULL;
+ *ret_soa = NULL;
+ return 0;
}
- answer = dns_answer_new(n);
- if (!answer)
- return -ENOMEM;
+ if (n_answer > 0) {
+ answer = dns_answer_new(n_answer);
+ if (!answer)
+ return -ENOMEM;
+ }
+ if (n_soa > 0) {
+ soa = dns_answer_new(n_soa);
+ if (!soa)
+ return -ENOMEM;
+ }
+
+ /* Second iteration, actually add the RRs to the answers */
for (i = 0; i < q->n_keys; i++) {
DnsZoneItem *j;
@@ -267,25 +275,36 @@ int dns_zone_lookup(DnsZone *z, DnsQuestion *q, DnsAnswer **ret) {
if (k < 0)
return k;
if (k == 0)
- continue;
-
- r = dns_answer_add(answer, j->rr);
+ r = dns_answer_add_soa(soa, DNS_RESOURCE_KEY_NAME(q->keys[i]));
+ else
+ r = dns_answer_add(answer, j->rr);
if (r < 0)
return r;
}
} else {
j = hashmap_get(z->by_key, q->keys[i]);
- LIST_FOREACH(by_key, j, j) {
- r = dns_answer_add(answer, j->rr);
- if (r < 0)
- return r;
+ if (j) {
+ LIST_FOREACH(by_key, j, j) {
+ r = dns_answer_add(answer, j->rr);
+ if (r < 0)
+ return r;
+ }
+ } else {
+ if (hashmap_get(z->by_name, DNS_RESOURCE_KEY_NAME(q->keys[i]))) {
+ r = dns_answer_add_soa(soa, DNS_RESOURCE_KEY_NAME(q->keys[i]));
+ if (r < 0)
+ return r;
+ }
}
}
}
- *ret = answer;
+ *ret_answer = answer;
answer = NULL;
+ *ret_soa = soa;
+ soa = NULL;
+
return 1;
}
diff --git a/src/resolve/resolved-dns-zone.h b/src/resolve/resolved-dns-zone.h
index 89d0bbe..5e7a1f7 100644
--- a/src/resolve/resolved-dns-zone.h
+++ b/src/resolve/resolved-dns-zone.h
@@ -37,4 +37,4 @@ void dns_zone_flush(DnsZone *z);
int dns_zone_put(DnsZone *z, DnsResourceRecord *rr);
void dns_zone_remove_rr(DnsZone *z, DnsResourceRecord *rr);
-int dns_zone_lookup(DnsZone *z, DnsQuestion *q, DnsAnswer **answer);
+int dns_zone_lookup(DnsZone *z, DnsQuestion *q, DnsAnswer **answer, DnsAnswer **soa);
More information about the systemd-commits
mailing list