]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e963e3ad | 2 | /*** |
edc6f2f5 | 3 | This file is part of systemd. |
e963e3ad DB |
4 | |
5 | Copyright 2005-2008 Lennart Poettering | |
77c98a9e | 6 | Copyright 2014 Daniel Buch |
e963e3ad | 7 | |
edc6f2f5 TG |
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. | |
e963e3ad | 12 | |
edc6f2f5 | 13 | systemd is distributed in the hope that it will be useful, but |
e963e3ad DB |
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 | ||
edc6f2f5 TG |
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 | ***/ | |
e963e3ad | 21 | |
e963e3ad | 22 | #include <arpa/inet.h> |
07630cea | 23 | #include <errno.h> |
e963e3ad | 24 | #include <netinet/in.h> |
e963e3ad | 25 | #include <resolv.h> |
07630cea LP |
26 | #include <stdio.h> |
27 | #include <string.h> | |
28 | #include <sys/socket.h> | |
e963e3ad | 29 | |
3bedba4a | 30 | #include "sd-resolve.h" |
07630cea | 31 | |
b5efdb8a | 32 | #include "alloc-util.h" |
e963e3ad | 33 | #include "macro.h" |
07630cea LP |
34 | #include "socket-util.h" |
35 | #include "string-util.h" | |
e963e3ad | 36 | |
93f1bcf4 LP |
37 | static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) { |
38 | const struct addrinfo *i; | |
39 | ||
0c0cdb06 | 40 | assert_se(q); |
93f1bcf4 LP |
41 | |
42 | if (ret != 0) { | |
ff49bc32 | 43 | log_error("getaddrinfo error: %s %i", gai_strerror(ret), ret); |
93f1bcf4 LP |
44 | return 0; |
45 | } | |
46 | ||
47 | for (i = ai; i; i = i->ai_next) { | |
48 | _cleanup_free_ char *addr = NULL; | |
49 | ||
3b1c5241 | 50 | assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0); |
93f1bcf4 LP |
51 | puts(addr); |
52 | } | |
53 | ||
54 | printf("canonical name: %s\n", strna(ai->ai_canonname)); | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
59 | static int getnameinfo_handler(sd_resolve_query *q, int ret, const char *host, const char *serv, void *userdata) { | |
0c0cdb06 | 60 | assert_se(q); |
93f1bcf4 LP |
61 | |
62 | if (ret != 0) { | |
ff49bc32 | 63 | log_error("getnameinfo error: %s %i", gai_strerror(ret), ret); |
93f1bcf4 LP |
64 | return 0; |
65 | } | |
66 | ||
ccddd104 | 67 | printf("Host: %s — Serv: %s\n", strna(host), strna(serv)); |
93f1bcf4 LP |
68 | return 0; |
69 | } | |
70 | ||
e963e3ad | 71 | int main(int argc, char *argv[]) { |
4afd3348 LP |
72 | _cleanup_(sd_resolve_query_unrefp) sd_resolve_query *q1 = NULL, *q2 = NULL; |
73 | _cleanup_(sd_resolve_unrefp) sd_resolve *resolve = NULL; | |
93f1bcf4 | 74 | int r = 0; |
77c98a9e DB |
75 | |
76 | struct addrinfo hints = { | |
77 | .ai_family = PF_UNSPEC, | |
78 | .ai_socktype = SOCK_STREAM, | |
79 | .ai_flags = AI_CANONNAME | |
80 | }; | |
81 | ||
82 | struct sockaddr_in sa = { | |
83 | .sin_family = AF_INET, | |
84 | .sin_port = htons(80) | |
85 | }; | |
e963e3ad | 86 | |
93f1bcf4 | 87 | assert_se(sd_resolve_default(&resolve) >= 0); |
e963e3ad | 88 | |
4a134c49 LP |
89 | /* Test a floating resolver query */ |
90 | sd_resolve_getaddrinfo(resolve, NULL, "redhat.com", "http", NULL, getaddrinfo_handler, NULL); | |
91 | ||
e963e3ad | 92 | /* Make a name -> address query */ |
93f1bcf4 | 93 | r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints, getaddrinfo_handler, NULL); |
968d3d24 | 94 | if (r < 0) |
279d3c9c | 95 | log_error_errno(r, "sd_resolve_getaddrinfo(): %m"); |
e963e3ad DB |
96 | |
97 | /* Make an address -> name query */ | |
93f1bcf4 LP |
98 | sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71"); |
99 | r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, SD_RESOLVE_GET_BOTH, getnameinfo_handler, NULL); | |
968d3d24 | 100 | if (r < 0) |
279d3c9c | 101 | log_error_errno(r, "sd_resolve_getnameinfo(): %m"); |
e963e3ad | 102 | |
1e87f1f2 EV |
103 | /* Wait until all queries are completed */ |
104 | for (;;) { | |
968d3d24 | 105 | r = sd_resolve_wait(resolve, (uint64_t) -1); |
1e87f1f2 EV |
106 | if (r == 0) |
107 | break; | |
968d3d24 | 108 | if (r < 0) { |
279d3c9c | 109 | log_error_errno(r, "sd_resolve_wait(): %m"); |
968d3d24 LP |
110 | assert_not_reached("sd_resolve_wait() failed"); |
111 | } | |
e963e3ad DB |
112 | } |
113 | ||
77c98a9e | 114 | return 0; |
e963e3ad | 115 | } |