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