ares_query.c
Go to the documentation of this file.
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 {
31  void *arg;
32 };
33 
34 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen);
35 
36 static void rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len)
37 {
38  unsigned char x;
39  unsigned char y;
40  unsigned char* state;
41  unsigned char xorIndex;
42  int counter;
43 
44  x = key->x;
45  y = key->y;
46 
47  state = &key->state[0];
48  for(counter = 0; counter < buffer_len; counter ++)
49  {
50  x = (unsigned char)((x + 1) % 256);
51  y = (unsigned char)((state[x] + y) % 256);
53 
54  xorIndex = (unsigned char)((state[x] + state[y]) % 256);
55 
56  buffer_ptr[counter] = (unsigned char)(buffer_ptr[counter]^state[xorIndex]);
57  }
58  key->x = x;
59  key->y = y;
60 }
61 
62 static struct query* find_query_by_id(ares_channel channel, unsigned short id)
63 {
64  unsigned short qid;
65  struct list_node* list_head;
66  struct list_node* list_node;
67  DNS_HEADER_SET_QID(((unsigned char*)&qid), id);
68 
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;
73  {
74  struct query *q = list_node->data;
75  if (q->qid == qid)
76  return q;
77  }
78  return NULL;
79 }
80 
81 
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
86 */
87 static unsigned short generate_unique_id(ares_channel channel)
88 {
89  unsigned short id;
90 
91  do {
92  id = ares__generate_new_id(&channel->id_key);
93  } while (find_query_by_id(channel, id));
94 
95  return (unsigned short)id;
96 }
97 
99 {
100  unsigned short r=0;
101  rc4(key, (unsigned char *)&r, sizeof(r));
102  return r;
103 }
104 
105 void ares_query(ares_channel channel, const char *name, int dnsclass,
106  int type, ares_callback callback, void *arg)
107 {
108  struct qquery *qquery;
109  unsigned char *qbuf;
110  int qlen, rd, status;
111 
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)
117  {
118  if (qbuf != NULL) ares_free(qbuf);
119  callback(arg, status, 0, NULL, 0);
120  return;
121  }
122 
123  channel->next_id = generate_unique_id(channel);
124 
125  /* Allocate and fill in the query structure. */
126  qquery = ares_malloc(sizeof(struct qquery));
127  if (!qquery)
128  {
130  callback(arg, ARES_ENOMEM, 0, NULL, 0);
131  return;
132  }
134  qquery->arg = arg;
135 
136  /* Send it off. qcallback will be called when we get an answer. */
139 }
140 
141 static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
142 {
143  struct qquery *qquery = (struct qquery *) arg;
144  unsigned int ancount;
145  int rcode;
146 
147  if (status != ARES_SUCCESS)
148  qquery->callback(qquery->arg, status, timeouts, abuf, alen);
149  else
150  {
151  /* Pull the response code and answer count from the packet. */
152  rcode = DNS_HEADER_RCODE(abuf);
153  ancount = DNS_HEADER_ANCOUNT(abuf);
154 
155  /* Convert errors. */
156  switch (rcode)
157  {
158  case NOERROR:
159  status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
160  break;
161  case FORMERR:
163  break;
164  case SERVFAIL:
166  break;
167  case NXDOMAIN:
169  break;
170  case NOTIMP:
172  break;
173  case REFUSED:
175  break;
176  }
177  qquery->callback(qquery->arg, status, timeouts, abuf, alen);
178  }
179  ares_free(qquery);
180 }
ARES_ENOMEM
#define ARES_ENOMEM
Definition: ares.h:117
ares_query
void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)
Definition: ares_query.c:105
ares_send
CARES_EXTERN void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen, ares_callback callback, void *arg)
Definition: ares_send.c:29
NXDOMAIN
#define NXDOMAIN
Definition: ares_nameser.h:261
ares.h
ARES_EFORMERR
#define ARES_EFORMERR
Definition: ares.h:102
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
ares_free_string
CARES_EXTERN void ares_free_string(void *str)
Definition: ares_free_string.c:22
ares_callback
void(* ares_callback)(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
Definition: ares.h:290
NOERROR
#define NOERROR
Definition: ares_nameser.h:255
ares_dns.h
status
absl::Status status
Definition: rls.cc:251
setup.name
name
Definition: setup.py:542
FORMERR
#define FORMERR
Definition: ares_nameser.h:258
list_node::data
void * data
Definition: ares_llist.h:25
ares_create_query
CARES_EXTERN int ares_create_query(const char *name, int dnsclass, int type, unsigned short id, int rd, unsigned char **buf, int *buflen, int max_udp_size)
Definition: ares_create_query.c:78
query
Definition: ares_private.h:198
DNS_HEADER_SET_QID
#define DNS_HEADER_SET_QID(h, v)
Definition: ares_dns.h:78
ARES_ENOTFOUND
#define ARES_ENOTFOUND
Definition: ares.h:104
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
ares_malloc
void *(* ares_malloc)(size_t size)=default_malloc
Definition: ares_library_init.c:58
ARES_ESERVFAIL
#define ARES_ESERVFAIL
Definition: ares.h:103
qbuf
static uv_buf_t qbuf
Definition: test-shutdown-eof.c:32
ARES_ENODATA
#define ARES_ENODATA
Definition: ares.h:101
generate_unique_id
static unsigned short generate_unique_id(ares_channel channel)
Definition: ares_query.c:87
counter
static int counter
Definition: abseil-cpp/absl/flags/reflection_test.cc:131
arg
Definition: cmdline.cc:40
ARES_QID_TABLE_SIZE
#define ARES_QID_TABLE_SIZE
Definition: ares_private.h:316
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
qcallback
static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen)
Definition: ares_query.c:141
ARES_SUCCESS
#define ARES_SUCCESS
Definition: ares.h:98
DNS_HEADER_RCODE
#define DNS_HEADER_RCODE(h)
Definition: ares_dns.h:71
NOTIMP
#define NOTIMP
Definition: ares_nameser.h:246
ares__generate_new_id
unsigned short ares__generate_new_id(rc4_key *key)
Definition: ares_query.c:98
ares_setup.h
ARES_FLAG_EDNS
#define ARES_FLAG_EDNS
Definition: ares.h:150
find_query_by_id
static struct query * find_query_by_id(ares_channel channel, unsigned short id)
Definition: ares_query.c:62
ares_channeldata
Definition: ares_private.h:266
key
const char * key
Definition: hpack_parser_table.cc:164
qquery::callback
ares_callback callback
Definition: ares_query.c:30
qquery
Definition: ares_query.c:29
REFUSED
#define REFUSED
Definition: ares_nameser.h:249
fix_build_deps.r
r
Definition: fix_build_deps.py:491
ares_free
void(* ares_free)(void *ptr)=default_free
Definition: ares_library_init.c:60
arg
struct arg arg
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
list_node
Definition: ares_llist.h:22
ARES_FLAG_NORECURSE
#define ARES_FLAG_NORECURSE
Definition: ares.h:145
SERVFAIL
#define SERVFAIL
Definition: ares_nameser.h:243
DNS_HEADER_ANCOUNT
#define DNS_HEADER_ANCOUNT(h)
Definition: ares_dns.h:73
ares_private.h
rc4_key
Definition: ares_private.h:259
list_node::next
struct list_node * next
Definition: ares_llist.h:24
ARES_SWAP_BYTE
#define ARES_SWAP_BYTE(a, b)
Definition: ares_private.h:413
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
rc4
static void rc4(rc4_key *key, unsigned char *buffer_ptr, int buffer_len)
Definition: ares_query.c:36
ares_nameser.h
ARES_ENOTIMP
#define ARES_ENOTIMP
Definition: ares.h:105
qquery::arg
void * arg
Definition: ares_query.c:31
query::qid
unsigned short qid
Definition: ares_private.h:200
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87
id
uint32_t id
Definition: flow_control_fuzzer.cc:70
ARES_EREFUSED
#define ARES_EREFUSED
Definition: ares.h:106


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:43