]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-resolve/test-resolve.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / libsystemd / sd-resolve / test-resolve.c
CommitLineData
edc6f2f5
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
e963e3ad 3/***
edc6f2f5 4 This file is part of systemd.
e963e3ad
DB
5
6 Copyright 2005-2008 Lennart Poettering
77c98a9e 7 Copyright 2014 Daniel Buch
e963e3ad 8
edc6f2f5
TG
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
e963e3ad 13
edc6f2f5 14 systemd is distributed in the hope that it will be useful, but
e963e3ad
DB
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
edc6f2f5
TG
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
e963e3ad 22
e963e3ad 23#include <arpa/inet.h>
07630cea 24#include <errno.h>
e963e3ad 25#include <netinet/in.h>
e963e3ad 26#include <resolv.h>
07630cea
LP
27#include <stdio.h>
28#include <string.h>
29#include <sys/socket.h>
e963e3ad 30
3bedba4a 31#include "sd-resolve.h"
07630cea 32
b5efdb8a 33#include "alloc-util.h"
e963e3ad 34#include "macro.h"
07630cea
LP
35#include "resolve-util.h"
36#include "socket-util.h"
37#include "string-util.h"
e963e3ad 38
93f1bcf4
LP
39static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
40 const struct addrinfo *i;
41
0c0cdb06 42 assert_se(q);
93f1bcf4
LP
43
44 if (ret != 0) {
ff49bc32 45 log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
93f1bcf4
LP
46 return 0;
47 }
48
49 for (i = ai; i; i = i->ai_next) {
50 _cleanup_free_ char *addr = NULL;
51
3b1c5241 52 assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
93f1bcf4
LP
53 puts(addr);
54 }
55
56 printf("canonical name: %s\n", strna(ai->ai_canonname));
57
58 return 0;
59}
60
61static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
0c0cdb06 62 assert_se(q);
93f1bcf4
LP
63
64 if (ret != 0) {
ff49bc32 65 log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
93f1bcf4
LP
66 return 0;
67 }
68
69 printf("Host: %s -- Serv: %s\n", strna(host), strna(serv));
70 return 0;
71}
72
e963e3ad 73int main(int argc, char *argv[]) {
1fac0c31 74 _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL;
968d3d24 75 _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
93f1bcf4 76 int r = 0;
77c98a9e
DB
77
78 struct addrinfo hints = {
79 .ai_family = PF_UNSPEC,
80 .ai_socktype = SOCK_STREAM,
81 .ai_flags = AI_CANONNAME
82 };
83
84 struct sockaddr_in sa = {
85 .sin_family = AF_INET,
86 .sin_port = htons(80)
87 };
e963e3ad 88
93f1bcf4 89 assert_se(sd_resolve_default(&resolve) >= 0);
e963e3ad 90
4a134c49
LP
91 /* Test a floating resolver query */
92 sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
93
e963e3ad 94 /* Make a name -> address query */
93f1bcf4 95 r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
968d3d24 96 if (r < 0)
279d3c9c 97 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
e963e3ad
DB
98
99 /* Make an address -> name query */
93f1bcf4
LP
100 sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
101 r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
968d3d24 102 if (r < 0)
279d3c9c 103 log_error_errno(r, "sd_resolve_getnameinfo(): %m");
e963e3ad 104
1fac0c31 105 /* Wait until the two queries are completed */
93f1bcf4 106 while (sd_resolve_query_is_done(q1) == 0 ||
1fac0c31 107 sd_resolve_query_is_done(q2) == 0) {
968d3d24
LP
108
109 r = sd_resolve_wait(resolve, (uint64_t) -1);
110 if (r < 0) {
279d3c9c 111 log_error_errno(r, "sd_resolve_wait(): %m");
968d3d24
LP
112 assert_not_reached("sd_resolve_wait() failed");
113 }
e963e3ad
DB
114 }
115
77c98a9e 116 return 0;
e963e3ad 117}