From b8e6c26b7b9608dffc84645f17fe4cf534af18dd Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Tue, 30 Oct 2012 13:54:38 -0700 Subject: Imported Upstream version 1.7.5 --- ares_query.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 ares_query.c (limited to 'ares_query.c') diff --git a/ares_query.c b/ares_query.c new file mode 100644 index 0000000..63652e2 --- /dev/null +++ b/ares_query.c @@ -0,0 +1,183 @@ + +/* Copyright 1998 by the Massachusetts Institute of Technology. + * + * Permission to use, copy, modify, and distribute this + * software and its documentation for any purpose and without + * fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting + * documentation, and that the name of M.I.T. not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ + +#include "ares_setup.h" + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_ARPA_NAMESER_H +# include +#else +# include "nameser.h" +#endif +#ifdef HAVE_ARPA_NAMESER_COMPAT_H +# include +#endif + +#include +#include "ares.h" +#include "ares_dns.h" +#include "ares_private.h" + +struct qquery { + ares_callback callback; + void *arg; +}; + +static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen); + +void ares__rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len) +{ + unsigned char x; + unsigned char y; + unsigned char* state; + unsigned char xorIndex; + short counter; + + x = key->x; + y = key->y; + + state = &key->state[0]; + for(counter = 0; counter < buffer_len; counter ++) + { + x = (unsigned char)((x + 1) % 256); + y = (unsigned char)((state[x] + y) % 256); + ARES_SWAP_BYTE(&state[x], &state[y]); + + xorIndex = (unsigned char)((state[x] + state[y]) % 256); + + buffer_ptr[counter] = (unsigned char)(buffer_ptr[counter]^state[xorIndex]); + } + key->x = x; + key->y = y; +} + +static struct query* find_query_by_id(ares_channel channel, unsigned short id) +{ + unsigned short qid; + struct list_node* list_head; + struct list_node* list_node; + DNS_HEADER_SET_QID(((unsigned char*)&qid), id); + + /* Find the query corresponding to this packet. */ + list_head = &(channel->queries_by_qid[qid % ARES_QID_TABLE_SIZE]); + for (list_node = list_head->next; list_node != list_head; + list_node = list_node->next) + { + struct query *q = list_node->data; + if (q->qid == qid) + return q; + } + return NULL; +} + + +/* a unique query id is generated using an rc4 key. Since the id may already + be used by a running query (as infrequent as it may be), a lookup is + performed per id generation. In practice this search should happen only + once per newly generated id +*/ +static unsigned short generate_unique_id(ares_channel channel) +{ + unsigned short id; + + do { + id = ares__generate_new_id(&channel->id_key); + } while (find_query_by_id(channel, id)); + + return (unsigned short)id; +} + +void ares_query(ares_channel channel, const char *name, int dnsclass, + int type, ares_callback callback, void *arg) +{ + struct qquery *qquery; + unsigned char *qbuf; + int qlen, rd, status; + + /* Compose the query. */ + rd = !(channel->flags & ARES_FLAG_NORECURSE); + status = ares_mkquery(name, dnsclass, type, channel->next_id, rd, &qbuf, + &qlen); + if (status != ARES_SUCCESS) + { + if (qbuf != NULL) free(qbuf); + callback(arg, status, 0, NULL, 0); + return; + } + + channel->next_id = generate_unique_id(channel); + + /* Allocate and fill in the query structure. */ + qquery = malloc(sizeof(struct qquery)); + if (!qquery) + { + ares_free_string(qbuf); + callback(arg, ARES_ENOMEM, 0, NULL, 0); + return; + } + qquery->callback = callback; + qquery->arg = arg; + + /* Send it off. qcallback will be called when we get an answer. */ + ares_send(channel, qbuf, qlen, qcallback, qquery); + ares_free_string(qbuf); +} + +static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen) +{ + struct qquery *qquery = (struct qquery *) arg; + unsigned int ancount; + int rcode; + + if (status != ARES_SUCCESS) + qquery->callback(qquery->arg, status, timeouts, abuf, alen); + else + { + /* Pull the response code and answer count from the packet. */ + rcode = DNS_HEADER_RCODE(abuf); + ancount = DNS_HEADER_ANCOUNT(abuf); + + /* Convert errors. */ + switch (rcode) + { + case NOERROR: + status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA; + break; + case FORMERR: + status = ARES_EFORMERR; + break; + case SERVFAIL: + status = ARES_ESERVFAIL; + break; + case NXDOMAIN: + status = ARES_ENOTFOUND; + break; + case NOTIMP: + status = ARES_ENOTIMP; + break; + case REFUSED: + status = ARES_EREFUSED; + break; + } + qquery->callback(qquery->arg, status, timeouts, abuf, alen); + } + free(qquery); +} -- cgit v1.2.3 From b6627e2c19506bd30328969ea8aab73fa0945f36 Mon Sep 17 00:00:00 2001 From: Yu Jiung Date: Wed, 9 Nov 2016 11:15:33 +0900 Subject: Imported Upstream version 1.10.0 Change-Id: Ic7f3b0ad7dca62abe4cfa642a5eb5e190eb7a8c9 --- ares_query.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'ares_query.c') diff --git a/ares_query.c b/ares_query.c index 63652e2..4bc9c25 100644 --- a/ares_query.c +++ b/ares_query.c @@ -16,9 +16,6 @@ #include "ares_setup.h" -#ifdef HAVE_SYS_SOCKET_H -# include -#endif #ifdef HAVE_NETINET_IN_H # include #endif @@ -31,7 +28,6 @@ # include #endif -#include #include "ares.h" #include "ares_dns.h" #include "ares_private.h" @@ -43,7 +39,7 @@ struct qquery { static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, int alen); -void ares__rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len) +static void rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len) { unsigned char x; unsigned char y; @@ -105,6 +101,13 @@ static unsigned short generate_unique_id(ares_channel channel) return (unsigned short)id; } +unsigned short ares__generate_new_id(rc4_key* key) +{ + unsigned short r=0; + rc4(key, (unsigned char *)&r, sizeof(r)); + return r; +} + void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg) { @@ -114,8 +117,8 @@ void ares_query(ares_channel channel, const char *name, int dnsclass, /* Compose the query. */ rd = !(channel->flags & ARES_FLAG_NORECURSE); - status = ares_mkquery(name, dnsclass, type, channel->next_id, rd, &qbuf, - &qlen); + status = ares_create_query(name, dnsclass, type, channel->next_id, rd, &qbuf, + &qlen, (channel->flags & ARES_FLAG_EDNS) ? channel->ednspsz : 0); if (status != ARES_SUCCESS) { if (qbuf != NULL) free(qbuf); -- cgit v1.2.3 From eb886f120599b2a184db20b527db6dfdfcb7852e Mon Sep 17 00:00:00 2001 From: Yu Jiung Date: Wed, 9 Nov 2016 11:18:45 +0900 Subject: Imported Upstream version 1.11.0 Change-Id: I238c24b75a10aa902d9bc4076ed68b76b5e2a750 --- ares_query.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ares_query.c') diff --git a/ares_query.c b/ares_query.c index 4bc9c25..b38b8a6 100644 --- a/ares_query.c +++ b/ares_query.c @@ -121,7 +121,7 @@ void ares_query(ares_channel channel, const char *name, int dnsclass, &qlen, (channel->flags & ARES_FLAG_EDNS) ? channel->ednspsz : 0); if (status != ARES_SUCCESS) { - if (qbuf != NULL) free(qbuf); + if (qbuf != NULL) ares_free(qbuf); callback(arg, status, 0, NULL, 0); return; } @@ -129,7 +129,7 @@ void ares_query(ares_channel channel, const char *name, int dnsclass, channel->next_id = generate_unique_id(channel); /* Allocate and fill in the query structure. */ - qquery = malloc(sizeof(struct qquery)); + qquery = ares_malloc(sizeof(struct qquery)); if (!qquery) { ares_free_string(qbuf); @@ -182,5 +182,5 @@ static void qcallback(void *arg, int status, int timeouts, unsigned char *abuf, } qquery->callback(qquery->arg, status, timeouts, abuf, alen); } - free(qquery); + ares_free(qquery); } -- cgit v1.2.3