2 This file is part of libasyncns.
4 Copyright 2005-2008 Lennart Poettering
6 libasyncns is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation, either version 2.1 of the
9 License, or (at your option) any later version.
11 libasyncns is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libasyncns. If not, see
18 <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
37 #if HAVE_ARPA_NAMESER_COMPAT_H
38 #include <arpa/nameser_compat.h>
43 int main(int argc, char *argv[]) {
44 asyncns_t* asyncns = NULL;
45 asyncns_query_t *q1, *q2, *q3;
47 struct addrinfo *ai, hints;
48 struct sockaddr_in sa;
49 char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
52 signal(SIGCHLD, SIG_IGN);
54 if (!(asyncns = asyncns_new(2))) {
55 fprintf(stderr, "asyncns_new() failed\n");
59 /* Make a name -> address query */
60 memset(&hints, 0, sizeof(hints));
61 hints.ai_family = PF_UNSPEC;
62 hints.ai_socktype = SOCK_STREAM;
64 q1 = asyncns_getaddrinfo(asyncns, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
67 fprintf(stderr, "asyncns_getaddrinfo(): %s\n", strerror(errno));
69 /* Make an address -> name query */
70 memset(&sa, 0, sizeof(sa));
71 sa.sin_family = AF_INET;
72 sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
73 sa.sin_port = htons(80);
75 q2 = asyncns_getnameinfo(asyncns, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
78 fprintf(stderr, "asyncns_getnameinfo(): %s\n", strerror(errno));
80 /* Make a res_query() call */
81 q3 = asyncns_res_query(asyncns, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
84 fprintf(stderr, "asyncns_res_query(): %s\n", strerror(errno));
86 /* Wait until the three queries are completed */
87 while (!asyncns_isdone(asyncns, q1)
88 || !asyncns_isdone(asyncns, q2)
89 || !asyncns_isdone(asyncns, q3)) {
90 if (asyncns_wait(asyncns, 1) < 0) {
91 fprintf(stderr, "asyncns_wait(): %s\n", strerror(errno));
96 /* Interpret the result of the name -> addr query */
97 if ((ret = asyncns_getaddrinfo_done(asyncns, q1, &ai)))
98 fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
102 for (i = ai; i; i = i->ai_next) {
104 const char *p = NULL;
106 if (i->ai_family == PF_INET)
107 p = inet_ntop(AF_INET, &((struct sockaddr_in*) i->ai_addr)->sin_addr, t, sizeof(t));
108 else if (i->ai_family == PF_INET6)
109 p = inet_ntop(AF_INET6, &((struct sockaddr_in6*) i->ai_addr)->sin6_addr, t, sizeof(t));
114 asyncns_freeaddrinfo(ai);
117 /* Interpret the result of the addr -> name query */
118 if ((ret = asyncns_getnameinfo_done(asyncns, q2, host, sizeof(host), serv, sizeof(serv))))
119 fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
121 printf("%s -- %s\n", host, serv);
123 /* Interpret the result of the SRV lookup */
124 if ((ret = asyncns_res_done(asyncns, q3, &srv)) < 0) {
125 fprintf(stderr, "error: %s %i\n", strerror(errno), ret);
126 } else if (ret == 0) {
127 fprintf(stderr, "No reply for SRV lookup\n");
132 const unsigned char *pos = srv + sizeof(HEADER);
133 unsigned char *end = srv + ret;
134 HEADER *head = (HEADER *)srv;
137 qdcount = ntohs(head->qdcount);
138 ancount = ntohs(head->ancount);
140 printf("%d answers for srv lookup:\n", ancount);
142 /* Ignore the questions */
143 while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
145 pos += len + QFIXEDSZ;
148 /* Parse the answers */
149 while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
150 /* Ignore the initial string */
151 uint16_t pref, weight, port;
154 /* Ignore type, ttl, class and dlen */
158 GETSHORT(weight, pos);
160 len = dn_expand(srv, end, pos, name, 255);
161 printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
162 pref, weight, port, name);
167 asyncns_freeanswer(srv);
175 asyncns_free(asyncns);