• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16 
17 #include "ares_setup.h"
18 
19 #ifdef HAVE_NETINET_IN_H
20 #  include <netinet/in.h>
21 #endif
22 
23 #include "ares_nameser.h"
24 
25 #include "ares.h"
26 #include "ares_dns.h"
27 #include "ares_private.h"
28 
29 struct qquery {
30   ares_callback callback;
31   void *arg;
32 };
33 
34 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen);
35 
find_query_by_id(ares_channel channel,unsigned short id)36 static struct query* find_query_by_id(ares_channel channel, unsigned short id)
37 {
38   unsigned short qid;
39   struct list_node* list_head;
40   struct list_node* list_node;
41   DNS_HEADER_SET_QID(((unsigned char*)&qid), id);
42 
43   /* Find the query corresponding to this packet. */
44   list_head = &(channel->queries_by_qid[qid % ARES_QID_TABLE_SIZE]);
45   for (list_node = list_head->next; list_node != list_head;
46        list_node = list_node->next)
47     {
48        struct query *q = list_node->data;
49        if (q->qid == qid)
50 	  return q;
51     }
52   return NULL;
53 }
54 
55 /* a unique query id is generated using an rc4 key. Since the id may already
56    be used by a running query (as infrequent as it may be), a lookup is
57    performed per id generation. In practice this search should happen only
58    once per newly generated id
59 */
generate_unique_id(ares_channel channel)60 static unsigned short generate_unique_id(ares_channel channel)
61 {
62   unsigned short id;
63 
64   do {
65     id = ares__generate_new_id(channel->rand_state);
66   } while (find_query_by_id(channel, id));
67 
68   return (unsigned short)id;
69 }
70 
ares_query(ares_channel channel,const char * name,int dnsclass,int type,ares_callback callback,void * arg)71 void ares_query(ares_channel channel, const char *name, int dnsclass,
72                 int type, ares_callback callback, void *arg)
73 {
74   struct qquery *qquery;
75   unsigned char *qbuf;
76   int qlen, rd, status;
77 
78   /* Compose the query. */
79   rd = !(channel->flags & ARES_FLAG_NORECURSE);
80   status = ares_create_query(name, dnsclass, type, channel->next_id, rd, &qbuf,
81               &qlen, (channel->flags & ARES_FLAG_EDNS) ? channel->ednspsz : 0);
82   if (status != ARES_SUCCESS)
83     {
84       if (qbuf != NULL) ares_free(qbuf);
85       callback(arg, status, 0, NULL, 0);
86       return;
87     }
88 
89   channel->next_id = generate_unique_id(channel);
90 
91   /* Allocate and fill in the query structure. */
92   qquery = ares_malloc(sizeof(struct qquery));
93   if (!qquery)
94     {
95       ares_free_string(qbuf);
96       callback(arg, ARES_ENOMEM, 0, NULL, 0);
97       return;
98     }
99   qquery->callback = callback;
100   qquery->arg = arg;
101 
102   /* Send it off.  qcallback will be called when we get an answer. */
103   ares_send(channel, qbuf, qlen, qcallback, qquery);
104   ares_free_string(qbuf);
105 }
106 
qcallback(void * arg,int status,int timeouts,unsigned char * abuf,int alen)107 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
108 {
109   struct qquery *qquery = (struct qquery *) arg;
110   unsigned int ancount;
111   int rcode;
112 
113   if (status != ARES_SUCCESS)
114     qquery->callback(qquery->arg, status, timeouts, abuf, alen);
115   else
116     {
117       /* Pull the response code and answer count from the packet. */
118       rcode = DNS_HEADER_RCODE(abuf);
119       ancount = DNS_HEADER_ANCOUNT(abuf);
120 
121       /* Convert errors. */
122       switch (rcode)
123         {
124         case NOERROR:
125           status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
126           break;
127         case FORMERR:
128           status = ARES_EFORMERR;
129           break;
130         case SERVFAIL:
131           status = ARES_ESERVFAIL;
132           break;
133         case NXDOMAIN:
134           status = ARES_ENOTFOUND;
135           break;
136         case NOTIMP:
137           status = ARES_ENOTIMP;
138           break;
139         case REFUSED:
140           status = ARES_EREFUSED;
141           break;
142         }
143       qquery->callback(qquery->arg, status, timeouts, abuf, alen);
144     }
145   ares_free(qquery);
146 }
147