1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2005-2008 Lennart Poettering
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.
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.
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/>.
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
34 #include "sd-resolve.h"
35 #include "resolve-util.h"
38 int main(int argc, char *argv[]) {
40 _cleanup_resolve_unref_ sd_resolve *resolve = NULL;
41 _cleanup_resolve_addrinfo_free_ struct addrinfo *ai = NULL;
42 _cleanup_free_ unsigned char *srv = NULL;
43 sd_resolve_query *q1 = NULL, *q2 = NULL, *q3 = NULL;
44 struct addrinfo hints = {};
45 struct sockaddr_in sa = {};
46 _cleanup_free_ char *host = NULL, *serv = NULL;
48 assert_se(sd_resolve_new(&resolve) >= 0);
50 /* Make a name -> address query */
51 hints.ai_family = PF_UNSPEC;
52 hints.ai_socktype = SOCK_STREAM;
53 hints.ai_flags = AI_CANONNAME;
55 r = sd_resolve_getaddrinfo(resolve, &q1, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
57 log_error("sd_resolve_getaddrinfo(): %s\n", strerror(-r));
59 /* Make an address -> name query */
60 sa.sin_family = AF_INET;
61 sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
62 sa.sin_port = htons(80);
64 r = sd_resolve_getnameinfo(resolve, &q2, (struct sockaddr*) &sa, sizeof(sa), 0, true, true);
66 log_error("sd_resolve_getnameinfo(): %s\n", strerror(-r));
68 /* Make a res_query() call */
69 r = sd_resolve_res_query(resolve, &q3, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
71 log_error("sd_resolve_res_query(): %s\n", strerror(-r));
73 /* Wait until the three queries are completed */
74 while (sd_resolve_is_done(q1) == 0 ||
75 sd_resolve_is_done(q2) == 0 ||
76 sd_resolve_is_done(q3) == 0) {
78 r = sd_resolve_wait(resolve, (uint64_t) -1);
80 log_error("sd_resolve_wait(): %s\n", strerror(-r));
81 assert_not_reached("sd_resolve_wait() failed");
85 /* Interpret the result of the name -> addr query */
86 r = sd_resolve_getaddrinfo_done(q1, &ai);
88 log_error("error: %s %i\n", gai_strerror(r), r);
92 for (i = ai; i; i = i->ai_next) {
96 if (i->ai_family == PF_INET)
97 p = inet_ntop(AF_INET, &((struct sockaddr_in*) i->ai_addr)->sin_addr, t, sizeof(t));
98 else if (i->ai_family == PF_INET6)
99 p = inet_ntop(AF_INET6, &((struct sockaddr_in6*) i->ai_addr)->sin6_addr, t, sizeof(t));
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("%s -- %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");
124 const unsigned char *pos = srv + sizeof(HEADER);
125 unsigned char *end = srv + r;
126 HEADER *head = (HEADER *)srv;
129 qdcount = ntohs(head->qdcount);
130 ancount = ntohs(head->ancount);
132 printf("%d answers for srv lookup:\n", ancount);
134 /* Ignore the questions */
135 while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
137 pos += len + QFIXEDSZ;
140 /* Parse the answers */
141 while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
142 /* Ignore the initial string */
143 uint16_t pref, weight, port;
146 /* Ignore type, ttl, class and dlen */
150 GETSHORT(weight, pos);
152 len = dn_expand(srv, end, pos, name, 255);
153 printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
154 pref, weight, port, name);