1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 static const char rcsid[] = "$Id: ares_query.c,v 1.5 2000/09/21 19:15:58 ghudson Exp $";
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h>
24 #include "ares_private.h"
27 ares_callback callback;
31 static void qcallback(void *arg, int status, unsigned char *abuf, int alen);
33 void ares_query(ares_channel channel, const char *name, int dnsclass,
34 int type, ares_callback callback, void *arg)
36 struct qquery *qquery;
40 /* Compose the query. */
41 rd = !(channel->flags & ARES_FLAG_NORECURSE);
42 status = ares_mkquery(name, dnsclass, type, channel->next_id, rd, &qbuf,
45 if (status != ARES_SUCCESS)
47 callback(arg, status, NULL, 0);
51 /* Allocate and fill in the query structure. */
52 qquery = malloc(sizeof(struct qquery));
55 ares_free_string(qbuf);
56 callback(arg, ARES_ENOMEM, NULL, 0);
59 qquery->callback = callback;
62 /* Send it off. qcallback will be called when we get an answer. */
63 ares_send(channel, qbuf, qlen, qcallback, qquery);
64 ares_free_string(qbuf);
67 static void qcallback(void *arg, int status, unsigned char *abuf, int alen)
69 struct qquery *qquery = (struct qquery *) arg;
73 if (status != ARES_SUCCESS)
74 qquery->callback(qquery->arg, status, abuf, alen);
77 /* Pull the response code and answer count from the packet. */
78 rcode = DNS_HEADER_RCODE(abuf);
79 ancount = DNS_HEADER_ANCOUNT(abuf);
85 status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
88 status = ARES_EFORMERR;
91 status = ARES_ESERVFAIL;
94 status = ARES_ENOTFOUND;
97 status = ARES_ENOTIMP;
100 status = ARES_EREFUSED;
103 qquery->callback(qquery->arg, status, abuf, alen);