#include "asterisk.h"
#include "asterisk/network.h"
#include <arpa/nameser.h>
#include <resolv.h>
#include "asterisk/channel.h"
#include "asterisk/dns.h"
#include "asterisk/endian.h"
Go to the source code of this file.
Data Structures | |
struct | dn_answer |
struct | dns_HEADER |
Defines | |
#define | MAX_SIZE 4096 |
Functions | |
int | ast_search_dns (void *context, const char *dname, int class, int type, int(*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer)) |
Perform DNS lookup (used by DNS, enum and SRV lookups). | |
static int | dns_parse_answer (void *context, int class, int type, unsigned char *answer, int len, int(*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer)) |
Parse DNS lookup result, call callback. | |
static int | skip_name (unsigned char *s, int len) |
Definition in file dns.c.
#define MAX_SIZE 4096 |
int ast_search_dns | ( | void * | context, | |
const char * | dname, | |||
int | class, | |||
int | type, | |||
int(*)(void *context, unsigned char *answer, int len, unsigned char *fullanswer) | callback | |||
) |
Perform DNS lookup (used by DNS, enum and SRV lookups).
Definition at line 255 of file dns.c.
References ast_debug, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dns_parse_answer(), LOG_WARNING, and MAX_SIZE.
Referenced by ast_get_enum(), ast_get_srv(), blr_ebl(), and blr_txt().
00258 { 00259 #ifdef HAVE_RES_NINIT 00260 struct __res_state dnsstate; 00261 #endif 00262 unsigned char answer[MAX_SIZE]; 00263 int res, ret = -1; 00264 00265 #ifdef HAVE_RES_NINIT 00266 memset(&dnsstate, 0, sizeof(dnsstate)); 00267 res_ninit(&dnsstate); 00268 res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer)); 00269 #else 00270 ast_mutex_lock(&res_lock); 00271 res_init(); 00272 res = res_search(dname, class, type, answer, sizeof(answer)); 00273 #endif 00274 if (res > 0) { 00275 if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) { 00276 ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname); 00277 ret = -1; 00278 } else if (res == 0) { 00279 ast_debug(1, "No matches found in DNS for %s\n", dname); 00280 ret = 0; 00281 } else 00282 ret = 1; 00283 } 00284 #ifdef HAVE_RES_NINIT 00285 #ifdef HAVE_RES_NDESTROY 00286 res_ndestroy(&dnsstate); 00287 #else 00288 res_nclose(&dnsstate); 00289 #endif 00290 #else 00291 #ifndef __APPLE__ 00292 res_close(); 00293 #endif 00294 ast_mutex_unlock(&res_lock); 00295 #endif 00296 00297 return ret; 00298 }
static int dns_parse_answer | ( | void * | context, | |
int | class, | |||
int | type, | |||
unsigned char * | answer, | |||
int | len, | |||
int(*)(void *context, unsigned char *answer, int len, unsigned char *fullanswer) | callback | |||
) | [static] |
Parse DNS lookup result, call callback.
Definition at line 185 of file dns.c.
References dns_HEADER::ancount, ast_log(), dn_answer::class, LOG_WARNING, dns_HEADER::qdcount, dn_answer::rtype, dn_answer::size, and skip_name().
Referenced by ast_search_dns().
00188 { 00189 unsigned char *fullanswer = answer; 00190 struct dn_answer *ans; 00191 dns_HEADER *h; 00192 int ret = 0; 00193 int res; 00194 int x; 00195 00196 h = (dns_HEADER *)answer; 00197 answer += sizeof(dns_HEADER); 00198 len -= sizeof(dns_HEADER); 00199 00200 for (x = 0; x < ntohs(h->qdcount); x++) { 00201 if ((res = skip_name(answer, len)) < 0) { 00202 ast_log(LOG_WARNING, "Couldn't skip over name\n"); 00203 return -1; 00204 } 00205 answer += res + 4; /* Skip name and QCODE / QCLASS */ 00206 len -= res + 4; 00207 if (len < 0) { 00208 ast_log(LOG_WARNING, "Strange query size\n"); 00209 return -1; 00210 } 00211 } 00212 00213 for (x = 0; x < ntohs(h->ancount); x++) { 00214 if ((res = skip_name(answer, len)) < 0) { 00215 ast_log(LOG_WARNING, "Failed skipping name\n"); 00216 return -1; 00217 } 00218 answer += res; 00219 len -= res; 00220 ans = (struct dn_answer *)answer; 00221 answer += sizeof(struct dn_answer); 00222 len -= sizeof(struct dn_answer); 00223 if (len < 0) { 00224 ast_log(LOG_WARNING, "Strange result size\n"); 00225 return -1; 00226 } 00227 if (len < 0) { 00228 ast_log(LOG_WARNING, "Length exceeds frame\n"); 00229 return -1; 00230 } 00231 00232 if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) { 00233 if (callback) { 00234 if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) { 00235 ast_log(LOG_WARNING, "Failed to parse result\n"); 00236 return -1; 00237 } 00238 ret = 1; 00239 } 00240 } 00241 answer += ntohs(ans->size); 00242 len -= ntohs(ans->size); 00243 } 00244 return ret; 00245 }
static int skip_name | ( | unsigned char * | s, | |
int | len | |||
) | [static] |
Definition at line 161 of file dns.c.
Referenced by dns_parse_answer().
00162 { 00163 int x = 0; 00164 00165 while (x < len) { 00166 if (*s == '\0') { 00167 s++; 00168 x++; 00169 break; 00170 } 00171 if ((*s & 0xc0) == 0xc0) { 00172 s += 2; 00173 x += 2; 00174 break; 00175 } 00176 x += *s + 1; 00177 s += *s + 1; 00178 } 00179 if (x >= len) 00180 return -1; 00181 return x; 00182 }