resolve-host.c revision 51323288fc628a5cac50914df915545d685b793e
/*-*- 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 "bus-util.h"
#include "bus-error.h"
#include "bus-errors.h"
#include "in-addr-util.h"
#include "af-list.h"
#include "build.h"
#include "resolved-dns-packet.h"
#include "resolved-def.h"
static int arg_family = AF_UNSPEC;
static int arg_ifindex = 0;
static int arg_type = 0;
static bool arg_legend = true;
if (!arg_legend)
return;
return;
if (flags != 0)
printf(" protocol%s%s%s",
if (ifindex > 0) {
}
}
unsigned c = 0;
int r, ifindex;
log_debug("Resolving %s (family %s, ifindex %i).", name, af_to_name(arg_family) ?: "*", arg_ifindex);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveHostname");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_set_auto_start(req, false);
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);
if (r < 0)
return bus_log_parse_error(r);
const void *a;
int family;
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",
continue;
}
if (ifindex > 0) {
char *t;
if (!t) {
continue;
}
}
if (r < 0) {
continue;
}
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;
const char *n;
int r;
if (r < 0)
return log_oom();
if (ifindex > 0) {
char *t;
if (!t) {
return -errno;
}
}
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveAddress");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_set_auto_start(req, false);
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_parse_error(r);
if (r < 0)
return bus_log_create_error(r);
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 (r < 0 || ifi <= 0) {
if (ifi <= 0)
return -EINVAL;
}
} else
a = s;
if (r < 0)
return r;
return 0;
}
unsigned n = 0;
int r, ifindex;
log_debug("Resolving %s %s %s.", name, dns_class_to_string(arg_class), dns_type_to_string(arg_type));
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveRecord");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_set_auto_start(req, false);
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);
if (r < 0)
return bus_log_parse_error(r);
_cleanup_free_ char *s = NULL;
uint16_t c, t;
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();
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;
}
printf("%s\n", s);
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;
}
static void help_dns_types(void) {
int i;
const char *t;
if (arg_legend)
puts("Known dns 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 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...]\n\n"
"Resolve IPv4 or IPv6 addresses.\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"
" --no-legend 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 0 /* done */;
case '4':
break;
case '6':
break;
case 'i':
if (arg_ifindex <= 0) {
return -errno;
}
break;
case 't':
return 0;
}
if (arg_type < 0) {
return arg_type;
}
break;
case 'c':
return 0;
}
if (r < 0) {
return r;
}
break;
case ARG_NO_LEGEND:
arg_legend = false;
break;
case 'p':
else {
return -EINVAL;
}
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("--class= may only be used in conjunction with --type=");
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) {
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;
}