header ordering fixes for FreeBSD (pending a global merge into asterisk.h) (bug ...
[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.h"
20
21 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
22
23 #include "asterisk/logger.h"
24 #include "asterisk/channel.h"
25 #include "asterisk/dns.h"
26 #include "asterisk/endian.h"
27
28 #define MAX_SIZE 4096
29
30 typedef struct {
31         unsigned        id :16;         /* query identification number */
32 #if __BYTE_ORDER == __BIG_ENDIAN
33                         /* fields in third byte */
34         unsigned        qr: 1;          /* response flag */
35         unsigned        opcode: 4;      /* purpose of message */
36         unsigned        aa: 1;          /* authoritive answer */
37         unsigned        tc: 1;          /* truncated message */
38         unsigned        rd: 1;          /* recursion desired */
39                         /* fields in fourth byte */
40         unsigned        ra: 1;          /* recursion available */
41         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
42         unsigned        ad: 1;          /* authentic data from named */
43         unsigned        cd: 1;          /* checking disabled by resolver */
44         unsigned        rcode :4;       /* response code */
45 #endif
46 #if __BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __PDP_ENDIAN
47                         /* fields in third byte */
48         unsigned        rd :1;          /* recursion desired */
49         unsigned        tc :1;          /* truncated message */
50         unsigned        aa :1;          /* authoritive answer */
51         unsigned        opcode :4;      /* purpose of message */
52         unsigned        qr :1;          /* response flag */
53                         /* fields in fourth byte */
54         unsigned        rcode :4;       /* response code */
55         unsigned        cd: 1;          /* checking disabled by resolver */
56         unsigned        ad: 1;          /* authentic data from named */
57         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
58         unsigned        ra :1;          /* recursion available */
59 #endif
60                         /* remaining bytes */
61         unsigned        qdcount :16;    /* number of question entries */
62         unsigned        ancount :16;    /* number of answer entries */
63         unsigned        nscount :16;    /* number of authority entries */
64         unsigned        arcount :16;    /* number of resource entries */
65 } dns_HEADER;
66
67 struct dn_answer {
68         unsigned short rtype;
69         unsigned short class;
70         unsigned int ttl;
71         unsigned short size;
72 } __attribute__ ((__packed__));
73
74 static int skip_name(u_char *s, int len)
75 {
76         int x = 0;
77
78         while (x < len) {
79                 if (*s == '\0') {
80                         s++;
81                         x++;
82                         break;
83                 }
84                 if ((*s & 0xc0) == 0xc0) {
85                         s += 2;
86                         x += 2;
87                         break;
88                 }
89                 x += *s + 1;
90                 s += *s + 1;
91         }
92         if (x >= len)
93                 return -1;
94         return x;
95 }
96
97 /*--- dns_parse_answer: Parse DNS lookup result, call callback */
98 static int dns_parse_answer(void *context,
99         int class, int type, u_char *answer, int len,
100         int (*callback)(void *context, u_char *answer, int len, u_char *fullanswer))
101 {
102         u_char *fullanswer = answer;
103         struct dn_answer *ans;
104         dns_HEADER *h;
105         int res;
106         int x;
107
108         h = (dns_HEADER *)answer;
109         answer += sizeof(dns_HEADER);
110         len -= sizeof(dns_HEADER);
111
112         for (x = 0; x < ntohs(h->qdcount); x++) {
113                 if ((res = skip_name(answer, len)) < 0) {
114                         ast_log(LOG_WARNING, "Couldn't skip over name\n");
115                         return -1;
116                 }
117                 answer += res + 4;      /* Skip name and QCODE / QCLASS */
118                 len -= res + 4;
119                 if (len < 0) {
120                         ast_log(LOG_WARNING, "Strange query size\n");
121                         return -1;
122                 }
123         }
124
125         for (x = 0; x < ntohs(h->ancount); x++) {
126                 if ((res = skip_name(answer, len)) < 0) {
127                         ast_log(LOG_WARNING, "Failed skipping name\n");
128                         return -1;
129                 }
130                 answer += res;
131                 len -= res;
132                 ans = (struct dn_answer *)answer;
133                 answer += sizeof(struct dn_answer);
134                 len -= sizeof(struct dn_answer);
135                 if (len < 0) {
136                         ast_log(LOG_WARNING, "Strange result size\n");
137                         return -1;
138                 }
139                 if (len < 0) {
140                         ast_log(LOG_WARNING, "Length exceeds frame\n");
141                         return -1;
142                 }
143
144                 if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) {
145                         if (callback) {
146                                 if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) {
147                                         ast_log(LOG_WARNING, "Failed to parse result\n");
148                                         return -1;
149                                 }
150                                 if (res > 0)
151                                         return 1;
152                         }
153                 }
154                 answer += ntohs(ans->size);
155                 len -= ntohs(ans->size);
156         }
157         return 0;
158 }
159
160 #if defined(res_ninit)
161 #define HAS_RES_NINIT
162 #else
163 AST_MUTEX_DEFINE_STATIC(res_lock);
164 #if 0
165 #warning "Warning, res_ninit is missing...  Could have reentrancy issues"
166 #endif
167 #endif
168
169 /*--- ast_search_dns: Lookup record in DNS */
170 int ast_search_dns(void *context,
171            const char *dname, int class, int type,
172            int (*callback)(void *context, u_char *answer, int len, u_char *fullanswer))
173 {
174 #ifdef HAS_RES_NINIT
175         struct __res_state dnsstate;
176 #endif
177         char answer[MAX_SIZE];
178         int res, ret = -1;
179
180 #ifdef HAS_RES_NINIT
181         res_ninit(&dnsstate);
182         res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
183 #else
184         ast_mutex_lock(&res_lock);
185         res_init();
186         res = res_search(dname, class, type, answer, sizeof(answer));
187 #endif
188         if (res > 0) {
189                 if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
190                         ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
191                         ret = -1;
192                 }
193                 else if (ret == 0) {
194                         ast_log(LOG_DEBUG, "No matches found in DNS for %s\n", dname);
195                         ret = 0;
196                 }
197                 else
198                         ret = 1;
199         }
200 #ifdef HAS_RES_NINIT
201         res_nclose(&dnsstate);
202 #else
203 #ifndef __APPLE__
204         res_close();
205 #endif
206         ast_mutex_unlock(&res_lock);
207 #endif
208         return ret;
209 }