2 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
17 #include "ares_setup.h"
19 #ifdef HAVE_NETINET_IN_H
20 # include <netinet/in.h>
23 #include "ares_nameser.h"
27 #include "ares_private.h"
30 ares_callback callback;
34 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen);
36 static void rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len)
41 unsigned char xorIndex;
47 state = &key->state[0];
48 for(counter = 0; counter < buffer_len; counter ++)
50 x = (unsigned char)((x + 1) % 256);
51 y = (unsigned char)((state[x] + y) % 256);
52 ARES_SWAP_BYTE(&state[x], &state[y]);
54 xorIndex = (unsigned char)((state[x] + state[y]) % 256);
56 buffer_ptr[counter] = (unsigned char)(buffer_ptr[counter]^state[xorIndex]);
62 static struct query* find_query_by_id(ares_channel channel, unsigned short id)
65 struct list_node* list_head;
66 struct list_node* list_node;
67 DNS_HEADER_SET_QID(((unsigned char*)&qid), id);
69 /* Find the query corresponding to this packet. */
70 list_head = &(channel->queries_by_qid[qid % ARES_QID_TABLE_SIZE]);
71 for (list_node = list_head->next; list_node != list_head;
72 list_node = list_node->next)
74 struct query *q = list_node->data;
82 /* a unique query id is generated using an rc4 key. Since the id may already
83 be used by a running query (as infrequent as it may be), a lookup is
84 performed per id generation. In practice this search should happen only
85 once per newly generated id
87 static unsigned short generate_unique_id(ares_channel channel)
92 id = ares__generate_new_id(&channel->id_key);
93 } while (find_query_by_id(channel, id));
95 return (unsigned short)id;
98 unsigned short ares__generate_new_id(rc4_key* key)
101 rc4(key, (unsigned char *)&r, sizeof(r));
105 void ares_query(ares_channel channel, const char *name, int dnsclass,
106 int type, ares_callback callback, void *arg)
108 struct qquery *qquery;
110 int qlen, rd, status;
112 /* Compose the query. */
113 rd = !(channel->flags & ARES_FLAG_NORECURSE);
114 status = ares_create_query(name, dnsclass, type, channel->next_id, rd, &qbuf,
115 &qlen, (channel->flags & ARES_FLAG_EDNS) ? channel->ednspsz : 0);
116 if (status != ARES_SUCCESS)
118 if (qbuf != NULL) ares_free(qbuf);
119 callback(arg, status, 0, NULL, 0);
123 channel->next_id = generate_unique_id(channel);
125 /* Allocate and fill in the query structure. */
126 qquery = ares_malloc(sizeof(struct qquery));
129 ares_free_string(qbuf);
130 callback(arg, ARES_ENOMEM, 0, NULL, 0);
133 qquery->callback = callback;
136 /* Send it off. qcallback will be called when we get an answer. */
137 ares_send(channel, qbuf, qlen, qcallback, qquery);
138 ares_free_string(qbuf);
141 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
143 struct qquery *qquery = (struct qquery *) arg;
144 unsigned int ancount;
147 if (status != ARES_SUCCESS)
148 qquery->callback(qquery->arg, status, timeouts, abuf, alen);
151 /* Pull the response code and answer count from the packet. */
152 rcode = DNS_HEADER_RCODE(abuf);
153 ancount = DNS_HEADER_ANCOUNT(abuf);
155 /* Convert errors. */
159 status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
162 status = ARES_EFORMERR;
165 status = ARES_ESERVFAIL;
168 status = ARES_ENOTFOUND;
171 status = ARES_ENOTIMP;
174 status = ARES_EREFUSED;
177 qquery->callback(qquery->arg, status, timeouts, abuf, alen);