]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-resolve/test-resolve.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / libsystemd / sd-resolve / test-resolve.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2005-2008 Lennart Poettering
7 Copyright 2014 Daniel Buch
8
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.
13
14 systemd is distributed in the hope that it will be useful, but
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
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 ***/
22
23 #include <arpa/inet.h>
24 #include <errno.h>
25 #include <netinet/in.h>
26 #include <resolv.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/socket.h>
30
31 #include "sd-resolve.h"
32
33 #include "macro.h"
34 #include "resolve-util.h"
35 #include "socket-util.h"
36 #include "string-util.h"
37
38 static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
39 const struct addrinfo *i;
40
41 assert_se(q);
42
43 if (ret != 0) {
44 log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
45 return 0;
46 }
47
48 for (i = ai; i; i = i->ai_next) {
49 _cleanup_free_ char *addr = NULL;
50
51 assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
52 puts(addr);
53 }
54
55 printf("canonical name: %s\n", strna(ai->ai_canonname));
56
57 return 0;
58 }
59
60 static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
61 assert_se(q);
62
63 if (ret != 0) {
64 log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
65 return 0;
66 }
67
68 printf("Host: %s -- Serv: %s\n", strna(host), strna(serv));
69 return 0;
70 }
71
72 int main(int argc, char *argv[]) {
73 _cleanup_resolve_query_unref_ sd_resolve_query *q1 = NULL, *q2 = NULL;
74 _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
75 int r = 0;
76
77 struct addrinfo hints = {
78 .ai_family = PF_UNSPEC,
79 .ai_socktype = SOCK_STREAM,
80 .ai_flags = AI_CANONNAME
81 };
82
83 struct sockaddr_in sa = {
84 .sin_family = AF_INET,
85 .sin_port = htons(80)
86 };
87
88 assert_se(sd_resolve_default(&resolve) >= 0);
89
90 /* Test a floating resolver query */
91 sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
92
93 /* Make a name -> address query */
94 r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
95 if (r < 0)
96 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
97
98 /* Make an address -> name query */
99 sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
100 r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
101 if (r < 0)
102 log_error_errno(r, "sd_resolve_getnameinfo(): %m");
103
104 /* Wait until the two queries are completed */
105 while (sd_resolve_query_is_done(q1) == 0 ||
106 sd_resolve_query_is_done(q2) == 0) {
107
108 r = sd_resolve_wait(resolve, (uint64_t) -1);
109 if (r < 0) {
110 log_error_errno(r, "sd_resolve_wait(): %m");
111 assert_not_reached("sd_resolve_wait() failed");
112 }
113 }
114
115 return 0;
116 }