resolve-host.c revision 2d4c5cbc0ed3ccb09dc086a040088b454c22c644
/*-*- 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"
static int arg_family = AF_UNSPEC;
static int arg_ifindex = 0;
unsigned c = 0;
int r;
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);
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);
if (r < 0)
return bus_log_parse_error(r);
log_debug("%s: skipping entry with family %hu (%s)", name, family, af_to_name(family) ?: "unknown");
continue;
}
log_error("%s: systemd-resolved returned address of invalid size %zu for family %s",
continue;
}
if (ifindex < 0) {
log_error("%s: systemd-resolved returned invalid interface index %i",
continue;
}
if (ifindex > 0) {
char *t;
if (!t) {
continue;
}
}
log_debug("%s: skipping entry with ifindex %i (%s)",
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_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 (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;
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);
_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 (n == 0) {
return -ESRCH;
}
return 0;
}
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 Filter by interface\n"
" -t --type=TYPE Query RR with DNS type\n"
" -c --class=CLASS Query RR with DNS class\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':
if (r < 0) {
return r;
}
break;
case 'c':
if (r < 0) {
return r;
}
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;
}