]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-resolve/test-resolve.c
2 This file is part of systemd.
4 Copyright 2005-2008 Lennart Poettering
5 Copyright 2014 Daniel Buch
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <arpa/inet.h>
23 #include <netinet/in.h>
27 #include <sys/socket.h>
29 #include "sd-resolve.h"
31 #include "alloc-util.h"
33 #include "socket-util.h"
34 #include "string-util.h"
36 static int getaddrinfo_handler(sd_resolve_query
*q
, int ret
, const struct addrinfo
*ai
, void *userdata
) {
37 const struct addrinfo
*i
;
42 log_error("getaddrinfo error: %s %i", gai_strerror(ret
), ret
);
46 for (i
= ai
; i
; i
= i
->ai_next
) {
47 _cleanup_free_
char *addr
= NULL
;
49 assert_se(sockaddr_pretty(i
->ai_addr
, i
->ai_addrlen
, false, true, &addr
) == 0);
53 printf("canonical name: %s\n", strna(ai
->ai_canonname
));
58 static int getnameinfo_handler(sd_resolve_query
*q
, int ret
, const char *host
, const char *serv
, void *userdata
) {
62 log_error("getnameinfo error: %s %i", gai_strerror(ret
), ret
);
66 printf("Host: %s — Serv: %s\n", strna(host
), strna(serv
));
70 int main(int argc
, char *argv
[]) {
71 _cleanup_(sd_resolve_query_unrefp
) sd_resolve_query
*q1
= NULL
, *q2
= NULL
;
72 _cleanup_(sd_resolve_unrefp
) sd_resolve
*resolve
= NULL
;
75 struct addrinfo hints
= {
76 .ai_family
= PF_UNSPEC
,
77 .ai_socktype
= SOCK_STREAM
,
78 .ai_flags
= AI_CANONNAME
81 struct sockaddr_in sa
= {
82 .sin_family
= AF_INET
,
86 assert_se(sd_resolve_default(&resolve
) >= 0);
88 /* Test a floating resolver query */
89 sd_resolve_getaddrinfo(resolve
, NULL
, "redhat.com", "http", NULL
, getaddrinfo_handler
, NULL
);
91 /* Make a name -> address query */
92 r
= sd_resolve_getaddrinfo(resolve
, &q1
, argc
>= 2 ? argv
[1] : "www.heise.de", NULL
, &hints
, getaddrinfo_handler
, NULL
);
94 log_error_errno(r
, "sd_resolve_getaddrinfo(): %m");
96 /* Make an address -> name query */
97 sa
.sin_addr
.s_addr
= inet_addr(argc
>= 3 ? argv
[2] : "193.99.144.71");
98 r
= sd_resolve_getnameinfo(resolve
, &q2
, (struct sockaddr
*) &sa
, sizeof(sa
), 0, SD_RESOLVE_GET_BOTH
, getnameinfo_handler
, NULL
);
100 log_error_errno(r
, "sd_resolve_getnameinfo(): %m");
102 /* Wait until all queries are completed */
104 r
= sd_resolve_wait(resolve
, (uint64_t) -1);
108 log_error_errno(r
, "sd_resolve_wait(): %m");
109 assert_not_reached("sd_resolve_wait() failed");