1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <errno.h>
5 #include <net/if_arp.h>
6 #include <netinet/in.h>
7 #include <resolv.h>
8 #include <stdio.h>
9
10 #include "sd-resolve.h"
11
12 #include "alloc-util.h"
13 #include "macro.h"
14 #include "socket-util.h"
15 #include "string-util.h"
16 #include "time-util.h"
17
18 #define TEST_TIMEOUT_USEC (20*USEC_PER_SEC)
19
getaddrinfo_handler(sd_resolve_query * q,int ret,const struct addrinfo * ai,void * userdata)20 static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
21 const struct addrinfo *i;
22
23 assert_se(q);
24
25 if (ret != 0) {
26 log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
27 return 0;
28 }
29
30 for (i = ai; i; i = i->ai_next) {
31 _cleanup_free_ char *addr = NULL;
32
33 assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
34 puts(addr);
35 }
36
37 printf("canonical name: %s\n", strna(ai->ai_canonname));
38
39 return 0;
40 }
41
getnameinfo_handler(sd_resolve_query * q,int ret,const char * host,const char * serv,void * userdata)42 static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
43 assert_se(q);
44
45 if (ret != 0) {
46 log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
47 return 0;
48 }
49
50 printf("Host: %s — Serv: %s\n", strna(host), strna(serv));
51 return 0;
52 }
53
main(int argc,char * argv[])54 int main(int argc, char *argv[]) {
55 _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL;
56 _cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL;
57 int r;
58
59 struct addrinfo hints = {
60 .ai_family = AF_UNSPEC,
61 .ai_socktype = SOCK_STREAM,
62 .ai_flags = AI_CANONNAME,
63 };
64
65 union sockaddr_union sa = {
66 .in.sin_family = AF_INET,
67 .in.sin_port = htobe16(80),
68 };
69
70 assert_se(sd_resolve_default(&resolve) >= 0);
71
72 /* Test a floating resolver query */
73 r = sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
74 if (r < 0)
75 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
76
77 /* Make a name -> address query */
78 r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
79 if (r < 0)
80 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
81
82 /* Make an address -> name query */
83 sa.in.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
84 r = sd_resolve_getnameinfo(resolve, &q2, &sa.sa, SOCKADDR_LEN(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL);
85 if (r < 0)
86 log_error_errno(r, "sd_resolve_getnameinfo(): %m");
87
88 /* Wait until all queries are completed */
89 for (;;) {
90 r = sd_resolve_wait(resolve, TEST_TIMEOUT_USEC);
91 if (r == 0)
92 break;
93 if (r == -ETIMEDOUT) {
94 /* Let's catch timeouts here, so that we can run safely in a CI that has no reliable DNS. Note
95 * that we invoke exit() directly here, as the stuck NSS call will not allow us to exit
96 * cleanly. */
97
98 log_notice_errno(r, "sd_resolve_wait() timed out, but that's OK");
99 exit(EXIT_SUCCESS);
100 }
101 if (r < 0) {
102 log_error_errno(r, "sd_resolve_wait(): %m");
103 assert_not_reached();
104 }
105 }
106
107 return 0;
108 }
109