Hold private lock while handling digits
[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 #if defined(res_ninit)
155 #define HAS_RES_NINIT
156 #else
157 AST_MUTEX_DEFINE_STATIC(res_lock);
158 #if 0
159 #warning "Warning, res_ninit is missing...  Could have reentrancy issues"
160 #endif
161 #endif
162
163 int ast_search_dns(void *context,
164                                    const char *dname, int class, int type,
165                                    int (*callback)(void *context, u_char *answer, int len, u_char *fullanswer))
166 {
167 #ifdef HAS_RES_NINIT
168         struct __res_state dnsstate;
169 #endif
170         char answer[MAX_SIZE];
171         int res, ret = -1;
172
173 #ifdef HAS_RES_NINIT
174         res_ninit(&dnsstate);
175         res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
176 #else
177         ast_mutex_lock(&res_lock);
178         res_init();
179         res = res_search(dname, class, type, answer, sizeof(answer));
180 #endif
181         if (res > 0) {
182                 if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
183                         ast_log(LOG_WARNING, "Parse error\n");
184                         ret = -1;
185                 }
186                 else if (ret == 0) {
187                         ast_log(LOG_DEBUG, "No matches found\n");
188                         ret = 0;
189                 }
190                 else
191                         ret = 1;
192         }
193 #ifdef HAS_RES_NINIT
194         res_nclose(&dnsstate);
195 #else
196 #ifndef __APPLE__
197         res_close();
198 #endif
199         ast_mutex_unlock(&res_lock);
200 #endif
201         return ret;
202 }