summaryrefslogtreecommitdiff
path: root/src/resolve
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-07-30 19:23:27 +0200
committerLennart Poettering <lennart@poettering.net>2014-07-30 19:24:13 +0200
commit2d4c5cbc0ed3ccb09dc086a040088b454c22c644 (patch)
tree86566a69e81b72e996ef37b9a9e78cc1d52357de /src/resolve
parent6f4d55a8ae04e344a41fc2adef12ad99c2bb2927 (diff)
resolved: add API for resolving specific RRs
Diffstat (limited to 'src/resolve')
-rw-r--r--src/resolve/resolved-bus.c163
-rw-r--r--src/resolve/resolved-dns-rr.c233
-rw-r--r--src/resolve/resolved-dns-rr.h5
3 files changed, 329 insertions, 72 deletions
diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c
index d0c21894e3..0b8ecf1b82 100644
--- a/src/resolve/resolved-bus.c
+++ b/src/resolve/resolved-bus.c
@@ -157,13 +157,13 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
for (i = 0; i < answer->n_rrs; i++) {
r = dns_question_matches_rr(q->question, answer->rrs[i]);
if (r < 0)
- goto parse_fail;
+ goto finish;
if (r == 0) {
/* Hmm, if this is not an address record,
maybe it's a cname? If so, remember this */
r = dns_question_matches_cname(q->question, answer->rrs[i]);
if (r < 0)
- goto parse_fail;
+ goto finish;
if (r > 0)
cname = dns_resource_record_ref(answer->rrs[i]);
@@ -204,7 +204,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
for (i = 0; i < answer->n_rrs; i++) {
r = dns_question_matches_rr(q->question, answer->rrs[i]);
if (r < 0)
- goto parse_fail;
+ goto finish;
if (r == 0)
continue;
@@ -230,6 +230,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
r = sd_bus_reply_method_errno(q->request, -r, NULL);
goto finish;
}
+
return;
}
}
@@ -245,14 +246,12 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL);
- goto finish;
-
-parse_fail:
- r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
finish:
- if (r < 0)
- log_error("Failed to send bus reply: %s", strerror(-r));
+ if (r < 0) {
+ log_error("Failed to send hostname reply: %s", strerror(-r));
+ sd_bus_reply_method_errno(q->request, -r, NULL);
+ }
dns_query_free(q);
}
@@ -356,7 +355,7 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
for (i = 0; i < answer->n_rrs; i++) {
r = dns_question_matches_rr(q->question, answer->rrs[i]);
if (r < 0)
- goto parse_fail;
+ goto finish;
if (r == 0)
continue;
@@ -382,14 +381,12 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL);
- goto finish;
-
-parse_fail:
- r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
finish:
- if (r < 0)
- log_error("Failed to send bus reply: %s", strerror(-r));
+ if (r < 0) {
+ log_error("Failed to send address reply: %s", strerror(-r));
+ sd_bus_reply_method_errno(q->request, -r, NULL);
+ }
dns_query_free(q);
}
@@ -469,10 +466,144 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
return 1;
}
+static void bus_method_resolve_record_complete(DnsQuery *q) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
+ unsigned added = 0, i;
+ int r;
+
+ assert(q);
+
+ if (q->state != DNS_QUERY_SUCCESS) {
+ r = reply_query_state(q);
+ goto finish;
+ }
+
+ r = sd_bus_message_new_method_return(q->request, &reply);
+ if (r < 0)
+ goto finish;
+
+ r = sd_bus_message_open_container(reply, 'a', "(qqay)");
+ if (r < 0)
+ goto finish;
+
+ if (q->answer) {
+ answer = dns_answer_ref(q->answer);
+
+ for (i = 0; i < answer->n_rrs; i++) {
+ _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
+ size_t start;
+
+ r = dns_question_matches_rr(q->question, answer->rrs[i]);
+ if (r < 0)
+ goto finish;
+ if (r == 0)
+ continue;
+
+ r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
+ if (r < 0)
+ goto finish;
+
+ r = dns_packet_append_rr(p, answer->rrs[i], &start);
+ if (r < 0)
+ goto finish;
+
+ r = sd_bus_message_open_container(reply, 'r', "qqay");
+ if (r < 0)
+ goto finish;
+
+ r = sd_bus_message_append(reply, "qq", answer->rrs[i]->key->class, answer->rrs[i]->key->type);
+ if (r < 0)
+ goto finish;
+
+ r = sd_bus_message_append_array(reply, 'y', DNS_PACKET_DATA(p) + start, p->size - start);
+ if (r < 0)
+ goto finish;
+
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ goto finish;
+
+ added ++;
+ }
+ }
+
+ if (added <= 0) {
+ r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", q->request_hostname);
+ goto finish;
+ }
+
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ goto finish;
+
+ r = sd_bus_send(q->manager->bus, reply, NULL);
+
+finish:
+ if (r < 0) {
+ log_error("Failed to send record reply: %s", strerror(-r));
+ sd_bus_reply_method_errno(q->request, -r, NULL);
+ }
+
+ dns_query_free(q);
+}
+
+static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+ _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
+ _cleanup_free_ char *reverse = NULL;
+ Manager *m = userdata;
+ DnsQuery *q;
+ int r;
+ uint16_t class, type;
+ const char *name;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read(message, "sqq", &name, &class, &type);
+ if (r < 0)
+ return r;
+
+ question = dns_question_new(1);
+ if (!question)
+ return -ENOMEM;
+
+ key = dns_resource_key_new(class, type, name);
+ if (!key)
+ return -ENOMEM;
+
+ r = dns_question_add(question, key);
+ if (r < 0)
+ return r;
+
+ r = dns_query_new(m, &q, question);
+ if (r < 0)
+ return r;
+
+ q->request = sd_bus_message_ref(message);
+ q->request_hostname = name;
+ q->complete = bus_method_resolve_record_complete;
+
+ r = dns_query_go(q);
+ if (r < 0) {
+ dns_query_free(q);
+
+ if (r == -ESRCH)
+ sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+
+ return r;
+ }
+
+ return 1;
+}
+
static const sd_bus_vtable resolve_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("ResolveHostname", "si", "a(iayi)s", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveAddress", "iayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ResolveRecord", "sqq", "a(qqay)", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END,
};
diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c
index 8b8858848c..e1200c9bc0 100644
--- a/src/resolve/resolved-dns-rr.c
+++ b/src/resolve/resolved-dns-rr.c
@@ -159,6 +159,31 @@ int dns_resource_key_compare_func(const void *a, const void *b) {
return 0;
}
+int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
+ char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
+ const char *c, *t;
+ char *s;
+
+ c = dns_class_to_string(key->class);
+ if (!c) {
+ sprintf(cbuf, "%i", key->class);
+ c = cbuf;
+ }
+
+ t = dns_type_to_string(key->type);
+ if (!t){
+ sprintf(tbuf, "%i", key->type);
+ t = tbuf;
+ }
+
+ s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
DnsResourceRecord *rr;
@@ -267,16 +292,24 @@ int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecor
if (r <= 0)
return r;
- if (IN_SET(a->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
+ switch (a->key->type) {
+
+ case DNS_TYPE_PTR:
+ case DNS_TYPE_NS:
+ case DNS_TYPE_CNAME:
return dns_name_equal(a->ptr.name, b->ptr.name);
- else if (a->key->type == DNS_TYPE_HINFO)
- return strcasecmp(a->hinfo.cpu, b->hinfo.cpu) == 0 &&
- strcasecmp(a->hinfo.os, b->hinfo.os) == 0;
- else if (a->key->type == DNS_TYPE_A)
+
+ case DNS_TYPE_HINFO:
+ return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
+ strcaseeq(a->hinfo.os, b->hinfo.os);
+
+ case DNS_TYPE_A:
return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
- else if (a->key->type == DNS_TYPE_AAAA)
+
+ case DNS_TYPE_AAAA:
return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
- else if (a->key->type == DNS_TYPE_SOA) {
+
+ case DNS_TYPE_SOA:
r = dns_name_equal(a->soa.mname, b->soa.mname);
if (r <= 0)
return r;
@@ -289,83 +322,171 @@ int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecor
a->soa.retry == b->soa.retry &&
a->soa.expire == b->soa.expire &&
a->soa.minimum == b->soa.minimum;
- } else
+ default:
return a->generic.size == b->generic.size &&
memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
+ }
}
-const char *dns_class_to_string(uint16_t class) {
+int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
+ _cleanup_free_ char *k = NULL;
+ char *s;
+ int r;
- switch (class) {
+ assert(rr);
- case DNS_CLASS_IN:
- return "IN";
+ r = dns_resource_key_to_string(rr->key, &k);
+ if (r < 0)
+ return r;
- case DNS_CLASS_ANY:
- return "ANY";
- }
+ switch (rr->key->type) {
- return NULL;
-}
+ case DNS_TYPE_PTR:
+ case DNS_TYPE_NS:
+ case DNS_TYPE_CNAME:
+ s = strjoin(k, " ", rr->ptr.name, NULL);
+ if (!s)
+ return -ENOMEM;
-const char *dns_type_to_string(uint16_t type) {
+ break;
- switch (type) {
+ case DNS_TYPE_HINFO:
+ s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
- case DNS_TYPE_A:
- return "A";
+ case DNS_TYPE_A: {
+ _cleanup_free_ char *x = NULL;
- case DNS_TYPE_NS:
- return "NS";
+ r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
+ if (r < 0)
+ return r;
- case DNS_TYPE_CNAME:
- return "CNAME";
+ s = strjoin(k, " ", x, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+ }
- case DNS_TYPE_SOA:
- return "SOA";
+ case DNS_TYPE_AAAA: {
+ _cleanup_free_ char *x = NULL;
- case DNS_TYPE_PTR:
- return "PTR";
+ r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
+ if (r < 0)
+ return r;
- case DNS_TYPE_HINFO:
- return "HINFO";
+ s = strjoin(k, " ", x, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+ }
- case DNS_TYPE_MX:
- return "MX";
+ case DNS_TYPE_SOA:
+ r = asprintf(&s, "%s %s %s %u %u %u %u %u",
+ k,
+ strna(rr->soa.mname),
+ strna(rr->soa.rname),
+ rr->soa.serial,
+ rr->soa.refresh,
+ rr->soa.retry,
+ rr->soa.expire,
+ rr->soa.minimum);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+
+ default: {
+ _cleanup_free_ char *x = NULL;
+
+ x = hexmem(rr->generic.data, rr->generic.size);
+ if (!x)
+ return -ENOMEM;
+
+ s = strjoin(k, " ", x, NULL);
+ if (!s)
+ return -ENOMEM;
+ break;
+ }}
+
+ *ret = s;
+ return 0;
+}
- case DNS_TYPE_TXT:
- return "TXT";
+const char *dns_class_to_string(uint16_t class) {
- case DNS_TYPE_AAAA:
- return "AAAA";
+ switch (class) {
- case DNS_TYPE_SRV:
- return "SRV";
+ case DNS_CLASS_IN:
+ return "IN";
- case DNS_TYPE_SSHFP:
- return "SSHFP";
+ case DNS_CLASS_ANY:
+ return "ANY";
+ }
- case DNS_TYPE_DNAME:
- return "DNAME";
+ return NULL;
+}
- case DNS_TYPE_ANY:
- return "ANY";
+int dns_class_from_string(const char *s, uint16_t *class) {
+ assert(s);
+ assert(class);
+
+ if (strcaseeq(s, "IN"))
+ *class = DNS_CLASS_IN;
+ else if (strcaseeq(s, "ANY"))
+ *class = DNS_TYPE_ANY;
+ else
+ return -EINVAL;
- case DNS_TYPE_OPT:
- return "OPT";
+ return 0;
+}
- case DNS_TYPE_TKEY:
- return "TKEY";
+static const struct {
+ uint16_t type;
+ const char *name;
+} dns_types[] = {
+ { DNS_TYPE_A, "A" },
+ { DNS_TYPE_NS, "NS" },
+ { DNS_TYPE_CNAME, "CNAME" },
+ { DNS_TYPE_SOA, "SOA" },
+ { DNS_TYPE_PTR, "PTR" },
+ { DNS_TYPE_HINFO, "HINFO" },
+ { DNS_TYPE_MX, "MX" },
+ { DNS_TYPE_TXT, "TXT" },
+ { DNS_TYPE_AAAA, "AAAA" },
+ { DNS_TYPE_SRV, "SRV" },
+ { DNS_TYPE_SSHFP, "SSHFP" },
+ { DNS_TYPE_DNAME, "DNAME" },
+ { DNS_TYPE_ANY, "ANY" },
+ { DNS_TYPE_OPT, "OPT" },
+ { DNS_TYPE_TKEY, "TKEY" },
+ { DNS_TYPE_TSIG, "TSIG" },
+ { DNS_TYPE_IXFR, "IXFR" },
+ { DNS_TYPE_AXFR, "AXFR" },
+};
- case DNS_TYPE_TSIG:
- return "TSIG";
- case DNS_TYPE_IXFR:
- return "IXFR";
+const char *dns_type_to_string(uint16_t type) {
+ unsigned i;
- case DNS_TYPE_AXFR:
- return "AXFR";
- }
+ for (i = 0; i < ELEMENTSOF(dns_types); i++)
+ if (dns_types[i].type == type)
+ return dns_types[i].name;
return NULL;
}
+
+int dns_type_from_string(const char *s, uint16_t *type) {
+ unsigned i;
+
+ assert(s);
+ assert(type);
+
+ for (i = 0; i < ELEMENTSOF(dns_types); i++)
+ if (strcaseeq(dns_types[i].name, s)) {
+ *type = dns_types[i].type;
+ return 0;
+ }
+
+ return -EINVAL;
+}
diff --git a/src/resolve/resolved-dns-rr.h b/src/resolve/resolved-dns-rr.h
index 50bb74c67b..524630071d 100644
--- a/src/resolve/resolved-dns-rr.h
+++ b/src/resolve/resolved-dns-rr.h
@@ -137,6 +137,7 @@ int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord
int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr);
unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]);
int dns_resource_key_compare_func(const void *a, const void *b);
+int dns_resource_key_to_string(const DnsResourceKey *key, char **ret);
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref);
DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key);
@@ -145,7 +146,11 @@ 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);
int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b);
+int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret);
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref);
const char *dns_type_to_string(uint16_t type);
+int dns_type_from_string(const char *name, uint16_t *type);
+
const char *dns_class_to_string(uint16_t type);
+int dns_class_from_string(const char *name, uint16_t *class);