2 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifdef HAVE_ARPA_NAMESER_H
39 #include <arpa/nameser.h>
46 RCSID("$Id: resolve.c,v 1.22 1999/12/02 16:58:52 joda Exp $");
48 #if defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND)
50 #define DECL(X) {#X, T_##X}
70 string_to_type(const char *name)
72 struct stot *p = stot;
73 for(p = stot; p->name; p++)
74 if(strcasecmp(name, p->name) == 0)
80 type_to_string(int type)
82 struct stot *p = stot;
83 for(p = stot; p->name; p++)
90 dns_free_data(struct dns_reply *r)
92 struct resource_record *rr;
95 for(rr = r->head; rr;){
96 struct resource_record *tmp = rr;
107 static struct dns_reply*
108 parse_reply(unsigned char *data, int len)
115 struct resource_record **rr;
117 r = calloc(1, sizeof(*r));
123 /* doesn't work on Crays */
124 memcpy(&r->h, p, sizeof(HEADER));
127 memcpy(&r->h, p, 12); /* XXX this will probably be mostly garbage */
130 status = dn_expand(data, data + len, p, host, sizeof(host));
135 r->q.domain = strdup(host);
136 if(r->q.domain == NULL) {
141 r->q.type = (p[0] << 8 | p[1]);
143 r->q.class = (p[0] << 8 | p[1]);
146 while(p < data + len){
147 int type, class, ttl, size;
148 status = dn_expand(data, data + len, p, host, sizeof(host));
154 type = (p[0] << 8) | p[1];
156 class = (p[0] << 8) | p[1];
158 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
160 size = (p[0] << 8) | p[1];
162 *rr = (struct resource_record*)calloc(1,
163 sizeof(struct resource_record));
168 (*rr)->domain = strdup(host);
169 if((*rr)->domain == NULL) {
174 (*rr)->class = class;
181 status = dn_expand(data, data + len, p, host, sizeof(host));
186 (*rr)->u.txt = strdup(host);
187 if((*rr)->u.txt == NULL) {
194 status = dn_expand(data, data + len, p + 2, host, sizeof(host));
199 (*rr)->u.mx = (struct mx_record*)malloc(sizeof(struct mx_record) +
201 if((*rr)->u.mx == NULL) {
205 (*rr)->u.mx->preference = (p[0] << 8) | p[1];
206 strcpy((*rr)->u.mx->domain, host);
210 status = dn_expand(data, data + len, p + 6, host, sizeof(host));
216 (struct srv_record*)malloc(sizeof(struct srv_record) +
218 if((*rr)->u.srv == NULL) {
222 (*rr)->u.srv->priority = (p[0] << 8) | p[1];
223 (*rr)->u.srv->weight = (p[2] << 8) | p[3];
224 (*rr)->u.srv->port = (p[4] << 8) | p[5];
225 strcpy((*rr)->u.srv->target, host);
229 (*rr)->u.txt = (char*)malloc(size + 1);
230 if((*rr)->u.txt == NULL) {
234 strncpy((*rr)->u.txt, (char*)p + 1, *p);
235 (*rr)->u.txt[*p] = 0;
240 (*rr)->u.data = (unsigned char*)malloc(size);
241 if(size != 0 && (*rr)->u.data == NULL) {
245 memcpy((*rr)->u.data, p, size);
254 static struct dns_reply *
255 dns_lookup_int(const char *domain, int rr_class, int rr_type)
257 unsigned char reply[1024];
259 struct dns_reply *r = NULL;
260 u_long old_options = 0;
262 if (_resolve_debug) {
263 old_options = _res.options;
264 _res.options |= RES_DEBUG;
265 fprintf(stderr, "dns_lookup(%s, %d, %s)\n", domain,
266 rr_class, type_to_string(rr_type));
268 len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
269 if (_resolve_debug) {
270 _res.options = old_options;
271 fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n",
272 domain, rr_class, type_to_string(rr_type), len);
275 r = parse_reply(reply, len);
280 dns_lookup(const char *domain, const char *type_name)
284 type = string_to_type(type_name);
287 fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n",
291 return dns_lookup_int(domain, C_IN, type);
294 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
297 dns_lookup(const char *domain, const char *type_name)
303 dns_free_data(struct dns_reply *r)
311 main(int argc, char **argv)
314 struct resource_record *rr;
315 r = dns_lookup(argv[1], argv[2]);
317 printf("No reply.\n");
320 for(rr = r->head; rr;rr=rr->next){
321 printf("%s %s %d ", rr->domain, type_to_string(rr->type), rr->ttl);
324 printf("%s\n", (char*)rr->u.data);
327 printf("%d.%d.%d.%d\n",
328 ((unsigned char*)rr->u.data)[0],
329 ((unsigned char*)rr->u.data)[1],
330 ((unsigned char*)rr->u.data)[2],
331 ((unsigned char*)rr->u.data)[3]);
335 struct mx_record *mx = (struct mx_record*)rr->u.data;
336 printf("%d %s\n", mx->preference, mx->domain);
340 struct srv_record *srv = (struct srv_record*)rr->u.data;
341 printf("%d %d %d %s\n", srv->priority, srv->weight,
342 srv->port, srv->target);