]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-resolve/test-resolve.c
Merge pull request #7486 from keszybz/coverity-fixes
[thirdparty/systemd.git] / src / libsystemd / sd-resolve / test-resolve.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2005-2008 Lennart Poettering
6 Copyright 2014 Daniel Buch
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <arpa/inet.h>
23 #include <errno.h>
24 #include <netinet/in.h>
25 #include <resolv.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/socket.h>
29
30 #include "sd-resolve.h"
31
32 #include "alloc-util.h"
33 #include "macro.h"
34 #include "socket-util.h"
35 #include "string-util.h"
36
37 #define TEST_TIMEOUT_USEC (20*USEC_PER_SEC)
38
39 static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) {
40 const struct addrinfo *i;
41
42 assert_se(q);
43
44 if (ret != 0) {
45 log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret);
46 return 0;
47 }
48
49 for (i = ai; i; i = i->ai_next) {
50 _cleanup_free_ char *addr = NULL;
51
52 assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
53 puts(addr);
54 }
55
56 printf("canonical name: %s\n", strna(ai->ai_canonname));
57
58 return 0;
59 }
60
61 static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) {
62 assert_se(q);
63
64 if (ret != 0) {
65 log_error("getnameinfo error: %s %i", gai_strerror(ret), ret);
66 return 0;
67 }
68
69 printf("Host: %s — Serv: %s\n", strna(host), strna(serv));
70 return 0;
71 }
72
73 int main(int argc, char *argv[]) {
74 _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL;
75 _cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL;
76 int r = 0;
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 };
88
89 assert_se(sd_resolve_default(&resolve) >= 0);
90
91 /* Test a floating resolver query */
92 sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL);
93
94 /* Make a name -> address query */
95 r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL);
96 if (r < 0)
97 log_error_errno(r, "sd_resolve_getaddrinfo(): %m");
98
99 /* Make an address -> name query */
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);
102 if (r < 0)
103 log_error_errno(r, "sd_resolve_getnameinfo(): %m");
104
105 /* Wait until all queries are completed */
106 for (;;) {
107 r = sd_resolve_wait(resolve, TEST_TIMEOUT_USEC);
108 if (r == 0)
109 break;
110 if (r == -ETIMEDOUT) {
111 /* Let's catch time-outs here, so that we can run safely in a CI that has no reliable DNS. Note
112 * that we invoke exit() directly here, as the stuck NSS call will not allow us to exit
113 * cleanly. */
114
115 log_notice_errno(r, "sd_resolve_wait() timed out, but that's OK");
116 exit(EXIT_SUCCESS);
117 break;
118 }
119 if (r < 0) {
120 log_error_errno(r, "sd_resolve_wait(): %m");
121 assert_not_reached("sd_resolve_wait() failed");
122 }
123 }
124
125 return 0;
126 }