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