1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2005-2008 Lennart Poettering
7 Copyright 2014 Daniel Buch
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
35 #include "socket-util.h"
36 #include "sd-resolve.h"
37 #include "resolve-util.h"
40 int main(int argc, char *argv[]) {
42 _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
43 _cleanup_resolve_addrinfo_free_ struct addrinfo *ai = NULL;
44 _cleanup_free_ unsigned char *srv = NULL;
45 _cleanup_free_ char *host = NULL, *serv = NULL;
46 sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL;
48 struct addrinfo hints = {
49 .ai_family = PF_UNSPEC,
50 .ai_socktype = SOCK_STREAM,
51 .ai_flags = AI_CANONNAME
54 struct sockaddr_in sa = {
55 .sin_family = AF_INET,
59 assert_se(sd_resolve_new(&resolve) >= 0);
61 /* Make a name -> address query */
62 r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
64 log_error("sd_resolve_getaddrinfo(): %s\n", strerror(-r));
66 /* Make an address -> name query */
67 sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71"),
68 r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, true, true);
70 log_error("sd_resolve_getnameinfo(): %s\n", strerror(-r));
72 /* Make a res_query() call */
73 r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
75 log_error("sd_resolve_res_query(): %s\n", strerror(-r));
77 /* Wait until the three queries are completed */
78 while (sd_resolve_is_done(q1) == 0 ||
79 sd_resolve_is_done(q2) == 0 ||
80 sd_resolve_is_done(q3) == 0) {
82 r = sd_resolve_wait(resolve, (uint64_t) -1);
84 log_error("sd_resolve_wait(): %s\n", strerror(-r));
85 assert_not_reached("sd_resolve_wait() failed");
89 /* Interpret the result of the name -> addr query */
90 r = sd_resolve_getaddrinfo_done(q1, &ai);
92 log_error("error: %s %i\n", gai_strerror(r), r);
96 for (i = ai; i; i = i->ai_next) {
97 _cleanup_free_ char *addr = NULL;
99 assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, &addr) == 0);
104 printf("canonical name: %s\n", strna(ai->ai_canonname));
107 /* Interpret the result of the addr -> name query */
108 r = sd_resolve_getnameinfo_done(q2, &host, &serv);
110 log_error("error: %s %i\n", gai_strerror(r), r);
112 printf("Host: %s -- Serv: %s\n", host, serv);
114 /* Interpret the result of the SRV lookup */
115 r = sd_resolve_res_done(q3, &srv);
117 log_error("error: %s %i\n", strerror(-r), r);
119 log_error("No reply for SRV lookup\n");
121 int qdcount, ancount, len;
122 const unsigned char *pos = srv + sizeof(HEADER);
123 unsigned char *end = srv + r;
124 HEADER *head = (HEADER *)srv;
127 qdcount = ntohs(head->qdcount);
128 ancount = ntohs(head->ancount);
130 printf("%d answers for srv lookup:\n", ancount);
132 /* Ignore the questions */
133 while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
135 pos += len + QFIXEDSZ;
138 /* Parse the answers */
139 while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
140 /* Ignore the initial string */
141 uint16_t pref, weight, port;
144 /* Ignore type, ttl, class and dlen */
148 GETSHORT(weight, pos);
150 len = dn_expand(srv, end, pos, name, 255);
151 printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
152 pref, weight, port, name);