/[resiprocate]/main/contrib/ares/ares_query.c
ViewVC logotype

Contents of /main/contrib/ares/ares_query.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5295 - (show annotations) (download)
Mon Aug 22 00:30:05 2005 UTC (14 years, 3 months ago) by jason
File MIME type: text/plain
File size: 3101 byte(s)
merged 5270:HEAD from b-directory-reorg
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
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.
14 */
15
16 #include <sys/types.h>
17
18 #ifndef WIN32
19 #include <netinet/in.h>
20 #ifndef __CYGWIN__
21 # include <arpa/nameser.h>
22 #endif
23 #endif
24
25 #include <stdlib.h>
26 #include "ares.h"
27 #include "ares_dns.h"
28 #include "ares_private.h"
29 #include "ares_local.h"
30
31 struct qquery {
32 ares_callback callback;
33 void *arg;
34 };
35
36 static void qcallback(void *arg, int status, unsigned char *abuf, int alen);
37
38 void ares_query(ares_channel channel, const char *name, int dnsclass,
39 int type, ares_callback callback, void *arg)
40 {
41 struct qquery *qquery;
42 unsigned char *qbuf;
43 int qlen, rd, status;
44
45 /* See if query can be handled by local pseudo-domain DNS */
46 if (ares_local_query(channel, name, dnsclass, type, callback, arg) != 0)
47 {
48 /* printf("ares_query: query for %s was handled locally\n",name); */
49 return;
50 }
51
52 /* Compose the query. */
53 rd = !(channel->flags & ARES_FLAG_NORECURSE);
54 status = ares_mkquery(name, dnsclass, type, channel->next_id, rd, &qbuf,
55 &qlen);
56 channel->next_id++;
57 if (status != ARES_SUCCESS)
58 {
59 callback(arg, status, NULL, 0);
60 return;
61 }
62
63 /* Allocate and fill in the query structure. */
64 qquery = malloc(sizeof(struct qquery));
65 if (!qquery)
66 {
67 ares_free_string((char*)qbuf);
68 callback(arg, ARES_ENOMEM, NULL, 0);
69 return;
70 }
71 qquery->callback = callback;
72 qquery->arg = arg;
73
74 /* Send it off. qcallback will be called when we get an answer. */
75 ares_send(channel, qbuf, qlen, qcallback, qquery);
76 ares_free_string((char*)qbuf);
77 }
78
79 static void qcallback(void *arg, int status, unsigned char *abuf, int alen)
80 {
81 struct qquery *qquery = (struct qquery *) arg;
82 unsigned int ancount;
83 int rcode;
84
85 if (status != ARES_SUCCESS)
86 qquery->callback(qquery->arg, status, abuf, alen);
87 else
88 {
89 /* Pull the response code and answer count from the packet. */
90 rcode = DNS_HEADER_RCODE(abuf);
91 ancount = DNS_HEADER_ANCOUNT(abuf);
92
93 /* Convert errors. */
94 switch (rcode)
95 {
96 case NOERROR:
97 status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
98 break;
99 case FORMERR:
100 status = ARES_EFORMERR;
101 break;
102 case SERVFAIL:
103 status = ARES_ESERVFAIL;
104 break;
105 case NXDOMAIN:
106 status = ARES_ENOTFOUND;
107 break;
108 case NOTIMP:
109 status = ARES_ENOTIMP;
110 break;
111 case REFUSED:
112 status = ARES_EREFUSED;
113 break;
114 }
115 qquery->callback(qquery->arg, status, abuf, alen);
116 }
117 free(qquery);
118 }

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27