resolved-bus.c revision b9d394ea565fd742bcdd34e8dd61ae07c58a9565
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
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 "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:
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: {
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)];
if (!rc) {
rc = p;
}
sd_bus_error_setf(&error, n, "Could not resolve hostname %s, server or network returned error %s", q->request_hostname, rc);
}
break;
}
case DNS_QUERY_SUCCESS: {
unsigned i, n, added = 0;
r = dns_packet_skip_question(q->packet);
if (r < 0)
goto parse_fail;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
for (i = 0; i < n; i++) {
if (r < 0)
goto parse_fail;
continue;
continue;
continue;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
} else {
if (r < 0)
goto finish;
}
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
added ++;
}
if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Hostname %s does not have RR of this type", q->request_hostname);
break;
}
if (r < 0)
goto finish;
break;
}
case DNS_QUERY_INVALID_REPLY:
break;
case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
assert_not_reached("Unexpected query state");
}
if (r < 0)
dns_query_free(q);
}
static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *hostname;
DnsQuery *q;
unsigned n = 0;
int r;
assert(m);
if (r < 0)
return r;
if (!hostname_is_valid(hostname))
n++;
}
n++;
}
r = dns_query_new(m, &q, keys, n);
if (r < 0)
return r;
q->request_family = family;
q->request_hostname = hostname;
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) {
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:
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: {
if (q->rcode == DNS_RCODE_NXDOMAIN)
else {
const char *rc, *n;
char p[DECIMAL_STR_MAX(q->rcode)];
if (!rc) {
rc = p;
}
sd_bus_error_setf(&error, n, "Could not resolve address %s, server or network returned error %s", ip, rc);
}
break;
}
case DNS_QUERY_SUCCESS: {
unsigned i, n, added = 0;
if (r < 0)
goto finish;
r = dns_packet_skip_question(q->packet);
if (r < 0)
goto parse_fail;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
for (i = 0; i < n; i++) {
if (r < 0)
goto parse_fail;
continue;
continue;
continue;
if (r < 0)
goto finish;
added ++;
}
if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address %s does not have RR of this type", ip);
break;
}
if (r < 0)
goto finish;
break;
}
case DNS_QUERY_INVALID_REPLY:
break;
case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
assert_not_reached("Unexpected query state");
}
if (r < 0)
dns_query_free(q);
}
static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
const void *d;
int ifindex;
DnsQuery *q;
int r;
assert(m);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (ifindex < 0)
if (r < 0)
return r;
if (r < 0)
return r;
q->request_family = family;
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_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),
};
assert(s);
assert(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... */
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) {
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) {
return r;
}
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
return 0;
}