2014-01-11 15:54:22 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
2014-01-06 13:41:59 +01:00
|
|
|
/***
|
2014-01-11 15:54:22 +01:00
|
|
|
This file is part of systemd.
|
2014-01-06 13:41:59 +01:00
|
|
|
|
|
|
|
Copyright 2005-2008 Lennart Poettering
|
2014-03-26 14:17:07 +01:00
|
|
|
Copyright 2014 Daniel Buch
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2014-01-11 15:54:22 +01:00
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
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.
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2014-01-11 15:54:22 +01:00
|
|
|
systemd is distributed in the hope that it will be useful, but
|
2014-01-06 13:41:59 +01:00
|
|
|
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.
|
|
|
|
|
2014-01-11 15:54:22 +01:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
2014-01-06 13:41:59 +01:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <errno.h>
|
2014-01-06 13:41:59 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <resolv.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2014-01-14 18:18:43 +01:00
|
|
|
#include "sd-resolve.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-01-06 13:41:59 +01:00
|
|
|
#include "macro.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "socket-util.h"
|
|
|
|
#include "string-util.h"
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2014-04-29 15:44:31 +02:00
|
|
|
static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
|
|
|
|
const struct addrinfo *i;
|
|
|
|
|
2015-01-22 22:53:42 +01:00
|
|
|
assert_se(q);
|
2014-04-29 15:44:31 +02:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2014-11-28 14:26:31 +01:00
|
|
|
log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
|
2014-04-29 15:44:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = ai; i; i = i->ai_next) {
|
|
|
|
_cleanup_free_ char *addr = NULL;
|
|
|
|
|
2015-03-10 12:41:59 +01:00
|
|
|
assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
|
2014-04-29 15:44:31 +02:00
|
|
|
puts(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("canonical name: %s\n", strna(ai->ai_canonname));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
|
2015-01-22 22:53:42 +01:00
|
|
|
assert_se(q);
|
2014-04-29 15:44:31 +02:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2014-11-28 14:26:31 +01:00
|
|
|
log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
|
2014-04-29 15:44:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Host: %s -- Serv: %s\n", strna(host), strna(serv));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-06 13:41:59 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL;
|
|
|
|
_cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL;
|
2014-04-29 15:44:31 +02:00
|
|
|
int r = 0;
|
2014-03-26 14:17:07 +01:00
|
|
|
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = PF_UNSPEC,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_flags = AI_CANONNAME
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sockaddr_in sa = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(80)
|
|
|
|
};
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2014-04-29 15:44:31 +02:00
|
|
|
assert_se(sd_resolve_default(&resolve) >= 0);
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2014-05-15 16:34:38 +02:00
|
|
|
/* Test a floating resolver query */
|
|
|
|
sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
|
|
|
|
|
2014-01-06 13:41:59 +01:00
|
|
|
/* Make a name -> address query */
|
2014-04-29 15:44:31 +02:00
|
|
|
r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
|
2014-01-27 20:12:14 +01:00
|
|
|
if (r < 0)
|
2014-11-28 14:45:55 +01:00
|
|
|
log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
|
2014-01-06 13:41:59 +01:00
|
|
|
|
|
|
|
/* Make an address -> name query */
|
2014-04-29 15:44:31 +02:00
|
|
|
sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
|
|
|
|
r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
|
2014-01-27 20:12:14 +01:00
|
|
|
if (r < 0)
|
2014-11-28 14:45:55 +01:00
|
|
|
log_error_errno(r, "sd_resolve_getnameinfo(): %m");
|
2014-01-06 13:41:59 +01:00
|
|
|
|
2015-08-27 20:47:13 +02:00
|
|
|
/* Wait until the two queries are completed */
|
2014-04-29 15:44:31 +02:00
|
|
|
while (sd_resolve_query_is_done(q1) == 0 ||
|
2015-08-27 20:47:13 +02:00
|
|
|
sd_resolve_query_is_done(q2) == 0) {
|
2014-01-27 20:12:14 +01:00
|
|
|
|
|
|
|
r = sd_resolve_wait(resolve, (uint64_t) -1);
|
|
|
|
if (r < 0) {
|
2014-11-28 14:45:55 +01:00
|
|
|
log_error_errno(r, "sd_resolve_wait(): %m");
|
2014-01-27 20:12:14 +01:00
|
|
|
assert_not_reached("sd_resolve_wait() failed");
|
|
|
|
}
|
2014-01-06 13:41:59 +01:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:17:07 +01:00
|
|
|
return 0;
|
2014-01-06 13:41:59 +01:00
|
|
|
}
|