resolve-host.c revision 4b548ef382007e40bd8fb3affdce9f843d0d63ac
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Zbigniew Jędrzejewski-Szmek
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 <getopt.h>
#include "sd-bus.h"
#include "af-list.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "escape.h"
#include "in-addr-util.h"
#include "parse-util.h"
#include "resolved-def.h"
#include "resolved-dns-packet.h"
static int arg_family = AF_UNSPEC;
static int arg_ifindex = 0;
static bool arg_legend = true;
static bool arg_resolve_service = false;
char rtt_str[FORMAT_TIMESTAMP_MAX];
if (!arg_legend)
return;
if (flags == 0)
return;
if (flags != 0)
printf(" protocol%s%s%s",
}
unsigned c = 0;
int r;
log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveHostname");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
const void *a;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
continue;
}
log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
return -EINVAL;
}
ifname[0] = 0;
if (r < 0)
printf("%*s%s %s%s%s\n",
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
printf("%*s%s (%s)\n",
if (c == 0) {
return -ESRCH;
}
return 0;
}
static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
unsigned c = 0;
int r;
if (ifindex <= 0)
if (r < 0)
return log_oom();
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveAddress");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_create_error(r);
const char *n;
if (r < 0)
return r;
if (r < 0)
return r;
ifname[0] = 0;
printf("%*s%*s%*s%s %s\n",
c == 0 ? ":" : " ",
n);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (c == 0) {
return -ESRCH;
}
return 0;
}
const char *percent, *a;
int ifi = 0;
int r;
if (percent) {
if (ifi <= 0)
return -EINVAL;
}
} else
a = s;
if (r < 0)
return r;
return 0;
}
unsigned n = 0;
int r;
log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(arg_class), dns_type_to_string(arg_type), isempty(ifname) ? "*" : ifname);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveRecord");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
_cleanup_free_ char *s = NULL;
uint16_t c, t;
int ifindex;
const void *d;
size_t l;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
if (r < 0)
return log_oom();
p->refuse_compression = true;
r = dns_packet_append_blob(p, d, l, NULL);
if (r < 0)
return log_oom();
if (r < 0) {
log_error("Failed to parse RR.");
return r;
}
r = dns_resource_record_to_string(rr, &s);
if (r < 0) {
log_error("Failed to format RR.");
return r;
}
ifname[0] = 0;
n++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (n == 0) {
return -ESRCH;
}
return 0;
}
const char *p;
unsigned c;
int r;
if (name)
log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name, type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
else if (type)
log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
else
log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveService");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
indent =
c = 0;
if (r < 0)
return bus_log_parse_error(r);
if (name)
if (type)
printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
c == 0 ? ":" : " ",
if (r < 0)
return bus_log_parse_error(r);
const void *a;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
continue;
}
log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
return -EINVAL;
}
ifname[0] = 0;
if (r < 0)
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
c = 0;
if (!escaped)
return log_oom();
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
if (r < 0)
return bus_log_parse_error(r);
if (isempty(canonical_name))
if (isempty(canonical_type))
if (canonical_name)
if (canonical_type)
}
return 0;
}
static void help_dns_types(void) {
int i;
const char *t;
if (arg_legend)
puts("Known DNS RR types:");
for (i = 0; i < _DNS_TYPE_MAX; i++) {
t = dns_type_to_string(i);
if (t)
puts(t);
}
}
static void help_dns_classes(void) {
int i;
const char *t;
if (arg_legend)
puts("Known DNS RR classes:");
for (i = 0; i < _DNS_CLASS_MAX; i++) {
t = dns_class_to_string(i);
if (t)
puts(t);
}
}
static void help(void) {
printf("%s [OPTIONS...] NAME...\n"
"%s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n\n"
"Resolve domain names, IPv4 or IPv6 addresses, resource records, and services.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" -4 Resolve IPv4 addresses\n"
" -6 Resolve IPv6 addresses\n"
" -i INTERFACE Look on interface\n"
" -p --protocol=PROTOCOL Look via protocol\n"
" -t --type=TYPE Query RR with DNS type\n"
" -c --class=CLASS Query RR with DNS class\n"
" --service Resolve service (SRV)\n"
" --service-address=BOOL Do [not] resolve address for services\n"
" --service-txt=BOOL Do [not] resolve TXT records for services\n"
" --cname=BOOL Do [not] follow CNAME redirects\n"
" --search=BOOL Do [not] use search domains\n"
" --legend=BOOL Do [not] print column headers\n"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c, r;
switch(c) {
case 'h':
help();
return 0; /* done */;
case ARG_VERSION:
return version();
case '4':
break;
case '6':
break;
case 'i': {
int ifi;
arg_ifindex = ifi;
else {
if (ifi <= 0)
arg_ifindex = ifi;
}
break;
}
case 't':
return 0;
}
r = dns_type_from_string(optarg);
if (r < 0) {
return r;
}
break;
case 'c':
return 0;
}
r = dns_class_from_string(optarg);
if (r < 0) {
return r;
}
break;
case ARG_LEGEND:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --legend= argument");
arg_legend = r;
break;
case 'p':
else {
return -EINVAL;
}
break;
case ARG_SERVICE:
arg_resolve_service = true;
break;
case ARG_CNAME:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --cname= argument.");
if (r == 0)
else
break;
case ARG_SERVICE_ADDRESS:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-address= argument.");
if (r == 0)
else
break;
case ARG_SERVICE_TXT:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-txt= argument.");
if (r == 0)
else
break;
case ARG_SEARCH:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --search argument.");
if (r == 0)
else
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("--class= may only be used in conjunction with --type=.");
return -EINVAL;
}
if (arg_type != 0 && arg_resolve_service) {
log_error("--service and --type= may not be combined.");
return -EINVAL;
}
return 1 /* work to do */;
}
int r;
log_open();
if (r <= 0)
goto finish;
log_error("No arguments passed");
r = -EINVAL;
goto finish;
}
r = sd_bus_open_system(&bus);
if (r < 0) {
log_error_errno(r, "sd_bus_open_system: %m");
goto finish;
}
if (arg_resolve_service) {
log_error("Domain specification required.");
r = -EINVAL;
goto finish;
else {
log_error("Too many arguments");
r = -EINVAL;
goto finish;
}
goto finish;
}
union in_addr_union a;
if (arg_type != 0)
else {
if (k >= 0)
else
}
if (r == 0)
r = k;
optind++;
}
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}