reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ares_query.c
Go to the documentation of this file.
1/* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
14 */
15
16#include <sys/types.h>
17
18#ifndef WIN32
19#include <netinet/in.h>
20#ifndef __CYGWIN__
21# include "ares_nameser.h"
22#endif
23#endif
24
25#include <stdlib.h>
26#include "ares.h"
27#include "ares_dns.h"
28#include "ares_private.h"
29#include "ares_local.h"
30
31struct qquery {
33 void *arg;
34};
35
36static void qcallback(void *arg, int status, unsigned char *abuf, int alen);
37
38void ares_query(ares_channel channel, const char *name, int dnsclass,
39 int type, ares_callback callback, void *arg)
40{
41 struct qquery *qquery;
42 unsigned char *qbuf;
43 int qlen, rd, status;
44
45 /* See if query can be handled by local pseudo-domain DNS */
46 if (ares_local_query(channel, name, dnsclass, type, callback, arg) != 0)
47 {
48 /* printf("ares_query: query for %s was handled locally\n",name); */
49 return;
50 }
51
52 /* Compose the query. */
53 rd = !(channel->flags & ARES_FLAG_NORECURSE);
54 qbuf = 0;
55 status = ares_mkquery(name, dnsclass, type, channel->next_id, rd, &qbuf,
56 &qlen);
57 /* WATCHOUT: qbuf is always allocated except in case of ENOMEM */
58 channel->next_id++;
59 if (status != ARES_SUCCESS)
60 {
61 if (qbuf)
62 ares_free_string((char*)qbuf);
63 callback(arg, status, NULL, 0);
64 return;
65 }
66
67 /* Allocate and fill in the query structure. */
68 qquery = malloc(sizeof(struct qquery));
69 if (!qquery)
70 {
71 ares_free_string((char*)qbuf);
73 return;
74 }
76 qquery->arg = arg;
77
78 /* Send it off. qcallback will be called when we get an answer. */
79 ares_send(channel, qbuf, qlen, qcallback, qquery);
80 ares_free_string((char*)qbuf);
81}
82
83static void qcallback(void *arg, int status, unsigned char *abuf, int alen)
84{
85 struct qquery *qquery = (struct qquery *) arg;
86 unsigned int ancount;
87 int rcode;
88
89 if (status != ARES_SUCCESS)
90 qquery->callback(qquery->arg, status, abuf, alen);
91 else
92 {
93 /* Pull the response code and answer count from the packet. */
94 rcode = DNS_HEADER_RCODE(abuf);
96
97 /* Convert errors. */
98 switch (rcode)
99 {
100 case NOERROR:
101 status = (ancount > 0) ? ARES_SUCCESS : ARES_ENODATA;
102 break;
103 case FORMERR:
104 status = ARES_EFORMERR;
105 break;
106 case SERVFAIL:
107 status = ARES_ESERVFAIL;
108 break;
109 case NXDOMAIN:
110 status = ARES_ENOTFOUND;
111 break;
112 case NOTIMP:
113 status = ARES_ENOTIMP;
114 break;
115 case REFUSED:
116 status = ARES_EREFUSED;
117 break;
118 }
119 qquery->callback(qquery->arg, status, abuf, alen);
120 }
121 free(qquery);
122}
static void callback(void *arg, int status, unsigned char *abuf, int alen)
Definition adig.c:388
#define ARES_SUCCESS
Definition ares.h:79
#define ARES_ENOMEM
Definition ares.h:98
#define ARES_ENODATA
Definition ares.h:82
void ares_free_string(char *str)
#define ARES_EREFUSED
Definition ares.h:87
#define ARES_ENOTFOUND
Definition ares.h:85
#define DNS_HEADER_ANCOUNT(h)
Definition ares.h:49
int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id, int rd, unsigned char **buf, int *buflen)
#define ARES_ENOTIMP
Definition ares.h:86
#define ARES_FLAG_NORECURSE
Definition ares.h:105
void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen, ares_callback callback, void *arg)
Definition ares_send.c:33
void(* ares_callback)(void *arg, int status, unsigned char *abuf, int alen)
Definition ares.h:172
#define ARES_ESERVFAIL
Definition ares.h:84
#define ARES_EFORMERR
Definition ares.h:83
#define DNS_HEADER_RCODE(h)
Definition ares_dns.h:38
int ares_local_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)
Definition ares_local.c:20
#define NOTIMP
#define NOERROR
#define NXDOMAIN
#define FORMERR
#define REFUSED
#define SERVFAIL
static void qcallback(void *arg, int status, unsigned char *abuf, int alen)
Definition ares_query.c:83
void ares_query(ares_channel channel, const char *name, int dnsclass, int type, ares_callback callback, void *arg)
Definition ares_query.c:38
unsigned short next_id
void * arg
Definition ares_query.c:33
ares_callback callback
Definition ares_query.c:32
void list()
Definition userAdmin.cxx:66