fix some formatting and add some comments (issue #5403)
[asterisk/asterisk.git] / dns.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005 Thorsten Lockert
5  *
6  * Written by Thorsten Lockert <tholo@trollphone.org>
7  *
8  * Funding provided by Troll Phone Networks AS
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*
22  *
23  * DNS Support for Asterisk
24  *
25  */
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <resolv.h>
32 #include <unistd.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/dns.h"
41 #include "asterisk/endian.h"
42
43 #define MAX_SIZE 4096
44
45 typedef struct {
46         unsigned        id :16;         /* query identification number */
47 #if __BYTE_ORDER == __BIG_ENDIAN
48                         /* fields in third byte */
49         unsigned        qr: 1;          /* response flag */
50         unsigned        opcode: 4;      /* purpose of message */
51         unsigned        aa: 1;          /* authoritive answer */
52         unsigned        tc: 1;          /* truncated message */
53         unsigned        rd: 1;          /* recursion desired */
54                         /* fields in fourth byte */
55         unsigned        ra: 1;          /* recursion available */
56         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
57         unsigned        ad: 1;          /* authentic data from named */
58         unsigned        cd: 1;          /* checking disabled by resolver */
59         unsigned        rcode :4;       /* response code */
60 #endif
61 #if __BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __PDP_ENDIAN
62                         /* fields in third byte */
63         unsigned        rd :1;          /* recursion desired */
64         unsigned        tc :1;          /* truncated message */
65         unsigned        aa :1;          /* authoritive answer */
66         unsigned        opcode :4;      /* purpose of message */
67         unsigned        qr :1;          /* response flag */
68                         /* fields in fourth byte */
69         unsigned        rcode :4;       /* response code */
70         unsigned        cd: 1;          /* checking disabled by resolver */
71         unsigned        ad: 1;          /* authentic data from named */
72         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
73         unsigned        ra :1;          /* recursion available */
74 #endif
75                         /* remaining bytes */
76         unsigned        qdcount :16;    /* number of question entries */
77         unsigned        ancount :16;    /* number of answer entries */
78         unsigned        nscount :16;    /* number of authority entries */
79         unsigned        arcount :16;    /* number of resource entries */
80 } dns_HEADER;
81
82 struct dn_answer {
83         unsigned short rtype;
84         unsigned short class;
85         unsigned int ttl;
86         unsigned short size;
87 } __attribute__ ((__packed__));
88
89 static int skip_name(char *s, int len)
90 {
91         int x = 0;
92
93         while (x < len) {
94                 if (*s == '\0') {
95                         s++;
96                         x++;
97                         break;
98                 }
99                 if ((*s & 0xc0) == 0xc0) {
100                         s += 2;
101                         x += 2;
102                         break;
103                 }
104                 x += *s + 1;
105                 s += *s + 1;
106         }
107         if (x >= len)
108                 return -1;
109         return x;
110 }
111
112 /*--- dns_parse_answer: Parse DNS lookup result, call callback */
113 static int dns_parse_answer(void *context,
114         int class, int type, char *answer, int len,
115         int (*callback)(void *context, char *answer, int len, char *fullanswer))
116 {
117         char *fullanswer = answer;
118         struct dn_answer *ans;
119         dns_HEADER *h;
120         int res;
121         int x;
122
123         h = (dns_HEADER *)answer;
124         answer += sizeof(dns_HEADER);
125         len -= sizeof(dns_HEADER);
126
127         for (x = 0; x < ntohs(h->qdcount); x++) {
128                 if ((res = skip_name(answer, len)) < 0) {
129                         ast_log(LOG_WARNING, "Couldn't skip over name\n");
130                         return -1;
131                 }
132                 answer += res + 4;      /* Skip name and QCODE / QCLASS */
133                 len -= res + 4;
134                 if (len < 0) {
135                         ast_log(LOG_WARNING, "Strange query size\n");
136                         return -1;
137                 }
138         }
139
140         for (x = 0; x < ntohs(h->ancount); x++) {
141                 if ((res = skip_name(answer, len)) < 0) {
142                         ast_log(LOG_WARNING, "Failed skipping name\n");
143                         return -1;
144                 }
145                 answer += res;
146                 len -= res;
147                 ans = (struct dn_answer *)answer;
148                 answer += sizeof(struct dn_answer);
149                 len -= sizeof(struct dn_answer);
150                 if (len < 0) {
151                         ast_log(LOG_WARNING, "Strange result size\n");
152                         return -1;
153                 }
154                 if (len < 0) {
155                         ast_log(LOG_WARNING, "Length exceeds frame\n");
156                         return -1;
157                 }
158
159                 if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) {
160                         if (callback) {
161                                 if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) {
162                                         ast_log(LOG_WARNING, "Failed to parse result\n");
163                                         return -1;
164                                 }
165                                 if (res > 0)
166                                         return 1;
167                         }
168                 }
169                 answer += ntohs(ans->size);
170                 len -= ntohs(ans->size);
171         }
172         return 0;
173 }
174
175 #if defined(res_ninit)
176 #define HAS_RES_NINIT
177 #else
178 AST_MUTEX_DEFINE_STATIC(res_lock);
179 #if 0
180 #warning "Warning, res_ninit is missing...  Could have reentrancy issues"
181 #endif
182 #endif
183
184 /*--- ast_search_dns: Lookup record in DNS */
185 int ast_search_dns(void *context,
186            const char *dname, int class, int type,
187            int (*callback)(void *context, char *answer, int len, char *fullanswer))
188 {
189 #ifdef HAS_RES_NINIT
190         struct __res_state dnsstate;
191 #endif
192         char answer[MAX_SIZE];
193         int res, ret = -1;
194
195 #ifdef HAS_RES_NINIT
196 #ifdef MAKE_VALGRIND_HAPPY
197         memset(&dnsstate, 0, sizeof(dnsstate));
198 #endif  
199         res_ninit(&dnsstate);
200         res = res_nsearch(&dnsstate, dname, class, type, (unsigned char *)answer, sizeof(answer));
201 #else
202         ast_mutex_lock(&res_lock);
203         res_init();
204         res = res_search(dname, class, type, answer, sizeof(answer));
205 #endif
206         if (res > 0) {
207                 if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
208                         ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
209                         ret = -1;
210                 }
211                 else if (ret == 0) {
212                         ast_log(LOG_DEBUG, "No matches found in DNS for %s\n", dname);
213                         ret = 0;
214                 }
215                 else
216                         ret = 1;
217         }
218 #ifdef HAS_RES_NINIT
219         res_nclose(&dnsstate);
220 #else
221 #ifndef __APPLE__
222         res_close();
223 #endif
224         ast_mutex_unlock(&res_lock);
225 #endif
226         return ret;
227 }