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>
41 int main(int argc, char *argv[]) {
43 _cleanup_asyncns_free_ asyncns_t *asyncns = NULL;
44 _cleanup_asyncns_addrinfo_free_ struct addrinfo *ai = NULL;
45 _cleanup_asyncns_answer_free_ unsigned char *srv = NULL;
46 asyncns_query_t *q1, *q2, *q3;
47 struct addrinfo hints = {};
48 struct sockaddr_in sa = {};
49 char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
51 signal(SIGCHLD, SIG_IGN);
53 asyncns = asyncns_new(2);
57 /* Make a name -> address query */
58 hints.ai_family = PF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
61 q1 = asyncns_getaddrinfo(asyncns, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
64 fprintf(stderr, "asyncns_getaddrinfo(): %s\n", strerror(errno));
66 /* Make an address -> name query */
67 sa.sin_family = AF_INET;
68 sa.sin_addr.s_addr = inet_addr(argc >= 3 ? argv[2] : "193.99.144.71");
69 sa.sin_port = htons(80);
71 q2 = asyncns_getnameinfo(asyncns, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
74 fprintf(stderr, "asyncns_getnameinfo(): %s\n", strerror(errno));
76 /* Make a res_query() call */
77 q3 = asyncns_res_query(asyncns, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
80 fprintf(stderr, "asyncns_res_query(): %s\n", strerror(errno));
82 /* Wait until the three queries are completed */
83 while (!asyncns_isdone(asyncns, q1)
84 || !asyncns_isdone(asyncns, q2)
85 || !asyncns_isdone(asyncns, q3)) {
86 if (asyncns_wait(asyncns, 1) < 0) {
87 fprintf(stderr, "asyncns_wait(): %s\n", strerror(errno));
91 /* Interpret the result of the name -> addr query */
92 if ((ret = asyncns_getaddrinfo_done(asyncns, q1, &ai)))
93 fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
97 for (i = ai; i; i = i->ai_next) {
101 if (i->ai_family == PF_INET)
102 p = inet_ntop(AF_INET, &((struct sockaddr_in*) i->ai_addr)->sin_addr, t, sizeof(t));
103 else if (i->ai_family == PF_INET6)
104 p = inet_ntop(AF_INET6, &((struct sockaddr_in6*) i->ai_addr)->sin6_addr, t, sizeof(t));
110 /* Interpret the result of the addr -> name query */
111 if ((ret = asyncns_getnameinfo_done(asyncns, q2, host, sizeof(host), serv, sizeof(serv))))
112 fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret);
114 printf("%s -- %s\n", host, serv);
116 /* Interpret the result of the SRV lookup */
117 if ((ret = asyncns_res_done(asyncns, q3, &srv)) < 0) {
118 fprintf(stderr, "error: %s %i\n", strerror(errno), ret);
119 } else if (ret == 0) {
120 fprintf(stderr, "No reply for SRV lookup\n");
125 const unsigned char *pos = srv + sizeof(HEADER);
126 unsigned char *end = srv + ret;
127 HEADER *head = (HEADER *)srv;
130 qdcount = ntohs(head->qdcount);
131 ancount = ntohs(head->ancount);
133 printf("%d answers for srv lookup:\n", ancount);
135 /* Ignore the questions */
136 while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
138 pos += len + QFIXEDSZ;
141 /* Parse the answers */
142 while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
143 /* Ignore the initial string */
144 uint16_t pref, weight, port;
147 /* Ignore type, ttl, class and dlen */
151 GETSHORT(weight, pos);
153 len = dn_expand(srv, end, pos, name, 255);
154 printf("\tpreference: %2d weight: %2d port: %d host: %s\n",
155 pref, weight, port, name);