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 #ifdef HAVE_ARPA_NAMESER_H
23 # include <arpa/nameser.h>
24 #else
25 # include "nameser.h"
26 #endif
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 # include <arpa/nameser_compat.h>
29 #endif
30
31 #include "ares.h"
32 #include "ares_dns.h"
33 #include "ares_private.h"
34
35 struct qquery {
36 ares_callback callback;
37 void *arg;
38 };
39
40 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen);
41
rc4(rc4_key * key,unsigned char * buffer_ptr,int buffer_len)42 static void rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len)
43 {
44 unsigned char x;
45 unsigned char y;
46 unsigned char* state;
47 unsigned char xorIndex;
48 short counter;
49
50 x = key->x;
51 y = key->y;
52
53 state = &key->state[0];
54 for(counter = 0; counter < buffer_len; counter ++)
55 {
56 x = (unsigned char)((x + 1) % 256);
57 y = (unsigned char)((state[x] + y) % 256);
58 ARES_SWAP_BYTE(&state[x], &state[y]);
59
60 xorIndex = (unsigned char)((state[x] + state[y]) % 256);
61
62 buffer_ptr[counter] = (unsigned char)(buffer_ptr[counter]^state[xorIndex]);
63 }
64 key->x = x;
65 key->y = y;
66 }
67
find_query_by_id(ares_channel channel,unsigned short id)68 static struct query* find_query_by_id(ares_channel channel, unsigned short id)
69 {
70 unsigned short qid;
71 struct list_node* list_head;
72 struct list_node* list_node;
73 DNS_HEADER_SET_QID(((unsigned char*)&qid), id);
74
75 /* Find the query corresponding to this packet. */
76 list_head = &(channel->queries_by_qid[qid % ARES_QID_TABLE_SIZE]);
77 for (list_node = list_head->next; list_node != list_head;
78 list_node = list_node->next)
79 {
80 struct query *q = list_node->data;
81 if (q->qid == qid)
82 return q;
83 }
84 return NULL;
85 }
86
87
88 /* a unique query id is generated using an rc4 key. Since the id may already
89 be used by a running query (as infrequent as it may be), a lookup is
90 performed per id generation. In practice this search should happen only
91 once per newly generated id
92 */
generate_unique_id(ares_channel channel)93 static unsigned short generate_unique_id(ares_channel channel)
94 {
95 unsigned short id;
96
97 do {
98 id = ares__generate_new_id(&channel->id_key);
99 } while (find_query_by_id(channel, id));
100
101 return (unsigned short)id;
102 }
103
ares__generate_new_id(rc4_key * key)104 unsigned short ares__generate_new_id(rc4_key* key)
105 {
106 unsigned short r=0;
107 rc4(key, (unsigned char *)&r, sizeof(r));
108 return r;
109 }
110
ares_query(ares_channel channel,const char * name,int dnsclass,int type,ares_callback callback,void * arg)111 void ares_query(ares_channel channel, const char *name, int dnsclass,
112 int type, ares_callback callback, void *arg)
113 {
114 struct qquery *qquery;
115 unsigned char *qbuf;
116 int qlen, rd, status;
117
118 /* Compose the query. */
119 rd = !(channel->flags & ARES_FLAG_NORECURSE);
120 status = ares_create_query(name, dnsclass, type, channel->next_id, rd, &qbuf,
121 &qlen, (channel->flags & ARES_FLAG_EDNS) ? channel->ednspsz : 0);
122 if (status != ARES_SUCCESS)
123 {
124 if (qbuf != NULL) ares_free(qbuf);
125 callback(arg, status, 0, NULL, 0);
126 return;
127 }
128
129 channel->next_id = generate_unique_id(channel);
130
131 /* Allocate and fill in the query structure. */
132 qquery = ares_malloc(sizeof(struct qquery));
133 if (!qquery)
134 {
135 ares_free_string(qbuf);
136 callback(arg, ARES_ENOMEM, 0, NULL, 0);
137 return;
138 }
139 qquery->callback = callback;
140 qquery->arg = arg;
141
142 /* Send it off. qcallback will be called when we get an answer. */
143 ares_send(channel, qbuf, qlen, qcallback, qquery);
144 ares_free_string(qbuf);
145 }
146
qcallback(void * arg,int status,int timeouts,unsigned char * abuf,int alen)147 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
148 {
149 struct qquery *qquery = (struct qquery *) arg;
150 unsigned int ancount;
151 int rcode;
152
153 if (status != ARES_SUCCESS)
154 qquery->callback(qquery->arg, status, timeouts, abuf, alen);
155 else
156 {
157 /* Pull the response code and answer count from the packet. */
158 rcode = DNS_HEADER_RCODE(abuf);
159 ancount = DNS_HEADER_ANCOUNT(abuf);
160
161 /* Convert errors. */
162 switch (rcode)
163 {
164 case NOERROR:
165 status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
166 break;
167 case FORMERR:
168 status = ARES_EFORMERR;
169 break;
170 case SERVFAIL:
171 status = ARES_ESERVFAIL;
172 break;
173 case NXDOMAIN:
174 status = ARES_ENOTFOUND;
175 break;
176 case NOTIMP:
177 status = ARES_ENOTIMP;
178 break;
179 case REFUSED:
180 status = ARES_EREFUSED;
181 break;
182 }
183 qquery->callback(qquery->arg, status, timeouts, abuf, alen);
184 }
185 ares_free(qquery);
186 }
187