Imported Upstream version 0.8
[platform/upstream/libasyncns.git] / libasyncns / asyncns-test.c
1 /***
2   This file is part of libasyncns.
3
4   Copyright 2005-2008 Lennart Poettering
5
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.
10
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.
15
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/>.
19 ***/
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <stdio.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <resolv.h>
33 #include <assert.h>
34 #include <signal.h>
35 #include <errno.h>
36
37 #if HAVE_ARPA_NAMESER_COMPAT_H
38 #include <arpa/nameser_compat.h>
39 #endif
40
41 #include "asyncns.h"
42
43 int main(int argc, char *argv[]) {
44     asyncns_t* asyncns = NULL;
45     asyncns_query_t *q1, *q2, *q3;
46     int r = 1, ret;
47     struct addrinfo *ai, hints;
48     struct sockaddr_in sa;
49     char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
50     unsigned char *srv;
51
52     signal(SIGCHLD, SIG_IGN);
53
54     if (!(asyncns = asyncns_new(2))) {
55         fprintf(stderr, "asyncns_new() failed\n");
56         goto fail;
57     }
58
59     /* Make a name -> address query */
60     memset(&hints, 0, sizeof(hints));
61     hints.ai_family = PF_UNSPEC;
62     hints.ai_socktype = SOCK_STREAM;
63
64     q1 = asyncns_getaddrinfo(asyncns, argc >= 2 ? argv[1] : "www.heise.de", NULL, &hints);
65
66     if (!q1)
67         fprintf(stderr, "asyncns_getaddrinfo(): %s\n", strerror(errno));
68
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);
74
75     q2 = asyncns_getnameinfo(asyncns, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1);
76
77     if (!q2)
78         fprintf(stderr, "asyncns_getnameinfo(): %s\n", strerror(errno));
79
80     /* Make a res_query() call */
81     q3 = asyncns_res_query(asyncns, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV);
82
83     if (!q3)
84         fprintf(stderr, "asyncns_res_query(): %s\n", strerror(errno));
85
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));
92             goto fail;
93         }
94     }
95
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);
99     else {
100         struct addrinfo *i;
101
102         for (i = ai; i; i = i->ai_next) {
103             char t[256];
104             const char *p = NULL;
105
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));
110
111             printf("%s\n", p);
112         }
113
114         asyncns_freeaddrinfo(ai);
115     }
116
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);
120     else
121         printf("%s -- %s\n", host, serv);
122
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");
128     } else {
129         int qdcount;
130         int ancount;
131         int len;
132         const unsigned char *pos = srv + sizeof(HEADER);
133         unsigned char *end = srv + ret;
134         HEADER *head = (HEADER *)srv;
135         char name[256];
136
137         qdcount = ntohs(head->qdcount);
138         ancount = ntohs(head->ancount);
139
140         printf("%d answers for srv lookup:\n", ancount);
141
142         /* Ignore the questions */
143         while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) {
144             assert(len >= 0);
145             pos += len + QFIXEDSZ;
146         }
147
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;
152             assert(len >= 0);
153             pos += len;
154             /* Ignore type, ttl, class and dlen */
155             pos += 10;
156
157             GETSHORT(pref, pos);
158             GETSHORT(weight, pos);
159             GETSHORT(port, 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);
163
164             pos += len;
165         }
166
167         asyncns_freeanswer(srv);
168     }
169
170     r = 0;
171
172 fail:
173
174     if (asyncns)
175         asyncns_free(asyncns);
176
177     return r;
178 }