Fix DNS variable name
[asterisk/asterisk.git] / dns.c
1 /*
2  * DNS Support for Asterisk
3  *
4  * Written by Thorsten Lockert <tholo@trollphone.org>
5  *
6  * Funding provided by Troll Phone Networks AS
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/nameser.h>
16 #include <resolv.h>
17 #include <unistd.h>
18
19 #include <asterisk/logger.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/dns.h>
22
23 #define MAX_SIZE 4096
24
25 typedef struct {
26         unsigned        id :16;         /* query identification number */
27 #if BYTE_ORDER == BIG_ENDIAN
28                         /* fields in third byte */
29         unsigned        qr: 1;          /* response flag */
30         unsigned        opcode: 4;      /* purpose of message */
31         unsigned        aa: 1;          /* authoritive answer */
32         unsigned        tc: 1;          /* truncated message */
33         unsigned        rd: 1;          /* recursion desired */
34                         /* fields in fourth byte */
35         unsigned        ra: 1;          /* recursion available */
36         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
37         unsigned        ad: 1;          /* authentic data from named */
38         unsigned        cd: 1;          /* checking disabled by resolver */
39         unsigned        rcode :4;       /* response code */
40 #endif
41 #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
42                         /* fields in third byte */
43         unsigned        rd :1;          /* recursion desired */
44         unsigned        tc :1;          /* truncated message */
45         unsigned        aa :1;          /* authoritive answer */
46         unsigned        opcode :4;      /* purpose of message */
47         unsigned        qr :1;          /* response flag */
48                         /* fields in fourth byte */
49         unsigned        rcode :4;       /* response code */
50         unsigned        cd: 1;          /* checking disabled by resolver */
51         unsigned        ad: 1;          /* authentic data from named */
52         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
53         unsigned        ra :1;          /* recursion available */
54 #endif
55                         /* remaining bytes */
56         unsigned        qdcount :16;    /* number of question entries */
57         unsigned        ancount :16;    /* number of answer entries */
58         unsigned        nscount :16;    /* number of authority entries */
59         unsigned        arcount :16;    /* number of resource entries */
60 } dns_HEADER;
61
62 struct dn_answer {
63         unsigned short rtype;
64         unsigned short class;
65         unsigned int ttl;
66         unsigned short size;
67 } __attribute__ ((__packed__));
68
69 static int skip_name(u_char *s, int len)
70 {
71         int x = 0;
72
73         while (x < len) {
74                 if (*s == '\0') {
75                         s++;
76                         x++;
77                         break;
78                 }
79                 if ((*s & 0xc0) == 0xc0) {
80                         s += 2;
81                         x += 2;
82                         break;
83                 }
84                 x += *s + 1;
85                 s += *s + 1;
86         }
87         if (x >= len)
88                 return -1;
89         return x;
90 }
91
92 static int dns_parse_answer(void *context,
93                                                         int class, int type, u_char *answer, int len,
94                                                         int (*callback)(void *context, u_char *answer, int len, u_char *fullanswer))
95 {
96         u_char *fullanswer = answer;
97         struct dn_answer *ans;
98         dns_HEADER *h;
99         int res;
100         int x;
101
102     h = (dns_HEADER *)answer;
103         answer += sizeof(dns_HEADER);
104         len -= sizeof(dns_HEADER);
105
106         for (x = 0; x < ntohs(h->qdcount); x++) {
107                 if ((res = skip_name(answer, len)) < 0) {
108                         ast_log(LOG_WARNING, "Couldn't skip over name\n");
109                         return -1;
110                 }
111                 answer += res + 4;      /* Skip name and QCODE / QCLASS */
112                 len -= res + 4;
113                 if (len < 0) {
114                         ast_log(LOG_WARNING, "Strange query size\n");
115                         return -1;
116                 }
117         }
118
119         for (x = 0; x < ntohs(h->ancount); x++) {
120                 if ((res = skip_name(answer, len)) < 0) {
121                         ast_log(LOG_WARNING, "Failed skipping name\n");
122                         return -1;
123                 }
124                 answer += res;
125                 len -= res;
126                 ans = (struct dn_answer *)answer;
127                 answer += sizeof(struct dn_answer);
128                 len -= sizeof(struct dn_answer);
129                 if (len < 0) {
130                         ast_log(LOG_WARNING, "Strange result size\n");
131                         return -1;
132                 }
133                 if (len < 0) {
134                         ast_log(LOG_WARNING, "Length exceeds frame\n");
135                         return -1;
136                 }
137
138                 if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) {
139                         if (callback) {
140                                 if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) {
141                                         ast_log(LOG_WARNING, "Failed to parse result\n");
142                                         return -1;
143                                 }
144                                 if (res > 0)
145                                         return 1;
146                         }
147                 }
148                 answer += ntohs(ans->size);
149                 len -= ntohs(ans->size);
150         }
151         return 0;
152 }
153
154 int ast_search_dns(void *context,
155                                    const char *dname, int class, int type,
156                                    int (*callback)(void *context, u_char *answer, int len, u_char *fullanswer))
157 {
158 #ifdef __Linux__
159         struct __res_state dnsstate;
160 #endif
161         char answer[MAX_SIZE];
162         int res, ret = -1;
163
164 #ifdef __Linux__
165         res_ninit(&dnsstate);
166         res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
167 #else
168         res_init();
169         res = res_search(dname, class, type, answer, sizeof(answer));
170 #endif
171         if (res > 0) {
172                 if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
173                         ast_log(LOG_WARNING, "Parse error\n");
174                         ret = -1;
175                 }
176                 else if (ret == 0) {
177                         ast_log(LOG_DEBUG, "No matches found\n");
178                         ret = 0;
179                 }
180                 else
181                         ret = 1;
182         }
183 #if defined(__Linux__)
184         res_nclose(&dnsstate);
185 #else
186 #ifndef __APPLE__
187         res_close();
188 #endif
189 #endif
190         return ret;
191 }