| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#include "asterisk/network.h" |
| 39 |
#include <arpa/nameser.h> |
| 40 |
#include <resolv.h> |
| 41 |
|
| 42 |
#include "asterisk/channel.h" |
| 43 |
#include "asterisk/dns.h" |
| 44 |
#include "asterisk/endian.h" |
| 45 |
|
| 46 |
|
| 47 |
#define MAX_SIZE 4096 |
| 48 |
|
| 49 |
#ifdef __PDP_ENDIAN |
| 50 |
#if __BYTE_ORDER == __PDP_ENDIAN |
| 51 |
#define DETERMINED_BYTE_ORDER __LITTLE_ENDIAN |
| 52 |
#endif |
| 53 |
#endif |
| 54 |
#if __BYTE_ORDER == __BIG_ENDIAN |
| 55 |
#define DETERMINED_BYTE_ORDER __BIG_ENDIAN |
| 56 |
#endif |
| 57 |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 58 |
#define DETERMINED_BYTE_ORDER __LITTLE_ENDIAN |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifndef HAVE_RES_NINIT |
| 62 |
AST_MUTEX_DEFINE_STATIC(res_lock); |
| 63 |
#endif |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
typedef struct { |
| 125 |
unsigned id:16; |
| 126 |
#if DETERMINED_BYTE_ORDER == __BIG_ENDIAN |
| 127 |
|
| 128 |
unsigned qr:1; |
| 129 |
unsigned opcode:4; |
| 130 |
unsigned aa:1; |
| 131 |
unsigned tc:1; |
| 132 |
unsigned rd:1; |
| 133 |
|
| 134 |
unsigned ra:1; |
| 135 |
unsigned unused:1; |
| 136 |
unsigned ad:1; |
| 137 |
unsigned cd:1; |
| 138 |
unsigned rcode:4; |
| 139 |
#endif |
| 140 |
#if DETERMINED_BYTE_ORDER == __LITTLE_ENDIAN |
| 141 |
|
| 142 |
unsigned rd:1; |
| 143 |
unsigned tc:1; |
| 144 |
unsigned aa:1; |
| 145 |
unsigned opcode:4; |
| 146 |
unsigned qr:1; |
| 147 |
|
| 148 |
unsigned rcode:4; |
| 149 |
unsigned cd:1; |
| 150 |
unsigned ad:1; |
| 151 |
unsigned unused:1; |
| 152 |
unsigned ra:1; |
| 153 |
#endif |
| 154 |
|
| 155 |
unsigned qdcount:16; |
| 156 |
unsigned ancount:16; |
| 157 |
unsigned nscount:16; |
| 158 |
unsigned arcount:16; |
| 159 |
} dns_HEADER; |
| 160 |
|
| 161 |
struct dn_answer { |
| 162 |
unsigned short rtype; |
| 163 |
unsigned short class; |
| 164 |
unsigned int ttl; |
| 165 |
unsigned short size; |
| 166 |
} __attribute__((__packed__)); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static int skip_name(unsigned char *s, int len) |
| 180 |
{ |
| 181 |
int x = 0; |
| 182 |
|
| 183 |
while (x < len) { |
| 184 |
if (*s == '\0') { |
| 185 |
s++; |
| 186 |
x++; |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
if ((*s & 0xc0) == 0xc0) { |
| 191 |
s += 2; |
| 192 |
x += 2; |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
x += *s + 1; |
| 197 |
s += *s + 1; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
if (x >= len) { |
| 202 |
return AST_DNS_SEARCH_FAILURE; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
return x; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static int dns_advance_field(unsigned char **dns_response, int remaining_len, int field_size) |
| 224 |
{ |
| 225 |
if (dns_response == NULL || field_size < 0 || remaining_len < field_size) { |
| 226 |
return AST_DNS_SEARCH_FAILURE; |
| 227 |
} |
| 228 |
|
| 229 |
*dns_response += field_size; |
| 230 |
remaining_len -= field_size; |
| 231 |
|
| 232 |
return remaining_len; |
| 233 |
} |
| 234 |
|
| 235 |
#ifndef HAVE_RES_NINIT |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
static int dns_search_res(const char *dname, int rr_class, int rr_type, |
| 251 |
unsigned char *dns_response, int dns_response_len) |
| 252 |
{ |
| 253 |
|
| 254 |
int ret = AST_DNS_SEARCH_FAILURE; |
| 255 |
|
| 256 |
ast_mutex_lock(&res_lock); |
| 257 |
res_init(); |
| 258 |
ret = res_search(dname, |
| 259 |
rr_class, |
| 260 |
rr_type, |
| 261 |
dns_response, |
| 262 |
dns_response_len); |
| 263 |
|
| 264 |
#ifdef HAVE_RES_CLOSE |
| 265 |
res_close(); |
| 266 |
#endif |
| 267 |
|
| 268 |
ast_mutex_unlock(&res_lock); |
| 269 |
|
| 270 |
return ret; |
| 271 |
} |
| 272 |
#else |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
static int dns_search_res(const char *dname, int rr_class, int rr_type, |
| 288 |
unsigned char *dns_response, int dns_response_len) |
| 289 |
{ |
| 290 |
|
| 291 |
int ret = AST_DNS_SEARCH_FAILURE; |
| 292 |
struct __res_state dns_state; |
| 293 |
|
| 294 |
memset(&dns_state, 0, sizeof(dns_state)); |
| 295 |
res_ninit(&dns_state); |
| 296 |
ret = res_nsearch(&dns_state, |
| 297 |
dname, |
| 298 |
rr_class, |
| 299 |
rr_type, |
| 300 |
dns_response, |
| 301 |
dns_response_len); |
| 302 |
|
| 303 |
#ifdef HAVE_RES_NDESTROY |
| 304 |
res_ndestroy(&dns_state); |
| 305 |
#else |
| 306 |
res_nclose(&dns_state); |
| 307 |
#endif |
| 308 |
|
| 309 |
return ret; |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
static int dns_parse_answer(void *context, |
| 330 |
int class, int type, unsigned char *answer, int len, |
| 331 |
int (*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer)) |
| 332 |
{ |
| 333 |
unsigned char *fullanswer = answer; |
| 334 |
struct dn_answer *ans; |
| 335 |
dns_HEADER *h; |
| 336 |
int ret = 0; |
| 337 |
int res; |
| 338 |
int x; |
| 339 |
|
| 340 |
h = (dns_HEADER *)answer; |
| 341 |
answer += sizeof(dns_HEADER); |
| 342 |
len -= sizeof(dns_HEADER); |
| 343 |
|
| 344 |
for (x = 0; x < ntohs(h->qdcount); x++) { |
| 345 |
if ((res = skip_name(answer, len)) < 0) { |
| 346 |
ast_log(LOG_WARNING, "Couldn't skip over name\n"); |
| 347 |
return -1; |
| 348 |
} |
| 349 |
answer += res + 4; |
| 350 |
len -= res + 4; |
| 351 |
if (len < 0) { |
| 352 |
ast_log(LOG_WARNING, "Strange query size\n"); |
| 353 |
return -1; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
for (x = 0; x < ntohs(h->ancount); x++) { |
| 358 |
if ((res = skip_name(answer, len)) < 0) { |
| 359 |
ast_log(LOG_WARNING, "Failed skipping name\n"); |
| 360 |
return -1; |
| 361 |
} |
| 362 |
answer += res; |
| 363 |
len -= res; |
| 364 |
ans = (struct dn_answer *)answer; |
| 365 |
answer += sizeof(struct dn_answer); |
| 366 |
len -= sizeof(struct dn_answer); |
| 367 |
if (len < 0) { |
| 368 |
ast_log(LOG_WARNING, "Length of DNS answer exceeds frame\n"); |
| 369 |
return -1; |
| 370 |
} |
| 371 |
|
| 372 |
if (ntohs(ans->class) == class && ntohs(ans->rtype) == type) { |
| 373 |
if (callback) { |
| 374 |
if ((res = callback(context, answer, ntohs(ans->size), fullanswer)) < 0) { |
| 375 |
ast_log(LOG_WARNING, "Failed to parse result\n"); |
| 376 |
return -1; |
| 377 |
} |
| 378 |
ret = 1; |
| 379 |
} |
| 380 |
} |
| 381 |
answer += ntohs(ans->size); |
| 382 |
len -= ntohs(ans->size); |
| 383 |
} |
| 384 |
return ret; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static int dns_parse_answer_ex(void *context, int rr_class, int rr_type, unsigned char *answer, int answer_len, |
| 408 |
int (*response_handler)(void *context, unsigned char *dns_response, int dns_response_len, int rcode), |
| 409 |
int (*record_handler)(void *context, unsigned char *record, int record_len, int ttl)) |
| 410 |
{ |
| 411 |
unsigned char *dns_response = answer; |
| 412 |
dns_HEADER *dns_header = (dns_HEADER *)answer; |
| 413 |
|
| 414 |
struct dn_answer *ans; |
| 415 |
int res, x, pos, dns_response_len, ret; |
| 416 |
|
| 417 |
dns_response_len = answer_len; |
| 418 |
ret = AST_DNS_SEARCH_NO_RECORDS; |
| 419 |
|
| 420 |
|
| 421 |
response_handler(context, dns_response, dns_response_len, ntohs(dns_header->rcode)); |
| 422 |
|
| 423 |
|
| 424 |
if (answer_len == 0) { |
| 425 |
return ret; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
if ((pos = dns_advance_field(&answer, answer_len, sizeof(dns_HEADER))) < 0) { |
| 430 |
ast_log(LOG_WARNING, "Length of DNS answer exceeds available search frames\n"); |
| 431 |
return AST_DNS_SEARCH_FAILURE; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
for (x = 0; x < ntohs(dns_header->qdcount); x++) { |
| 436 |
if ((res = skip_name(answer, pos)) < 0) { |
| 437 |
ast_log(LOG_WARNING, "Failed skipping name\n"); |
| 438 |
return AST_DNS_SEARCH_FAILURE; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
if ((pos = dns_advance_field(&answer, pos, res + 4)) < 0) { |
| 443 |
return AST_DNS_SEARCH_FAILURE; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
for (x = 0; x < ntohs(dns_header->ancount); x++) { |
| 449 |
if ((res = skip_name(answer, pos)) < 0) { |
| 450 |
ast_log(LOG_WARNING, "Failed skipping name\n"); |
| 451 |
return AST_DNS_SEARCH_FAILURE; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
if ((pos = dns_advance_field(&answer, pos, res)) < 0) { |
| 456 |
ast_log(LOG_WARNING, "Length of DNS answer exceeds available search frames\n"); |
| 457 |
return AST_DNS_SEARCH_FAILURE; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
ans = (struct dn_answer *) answer; |
| 462 |
|
| 463 |
|
| 464 |
if ((pos = dns_advance_field(&answer, pos, sizeof(struct dn_answer))) < 0) { |
| 465 |
ast_log(LOG_WARNING, "Length of DNS answer exceeds available search frames\n"); |
| 466 |
return AST_DNS_SEARCH_FAILURE; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
if (ntohs(ans->class) == rr_class && ntohs(ans->rtype) == rr_type) { |
| 471 |
|
| 472 |
record_handler(context, answer, ntohs(ans->size), ntohl(ans->ttl)); |
| 473 |
|
| 474 |
ret = AST_DNS_SEARCH_SUCCESS; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
pos = dns_advance_field(&answer, pos, ntohs(ans->size)); |
| 480 |
} |
| 481 |
|
| 482 |
return ret; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
int ast_search_dns(void *context, |
| 492 |
const char *dname, int class, int type, |
| 493 |
int (*callback)(void *context, unsigned char *answer, int len, unsigned char *fullanswer)) |
| 494 |
{ |
| 495 |
#ifdef HAVE_RES_NINIT |
| 496 |
struct __res_state dnsstate; |
| 497 |
#endif |
| 498 |
unsigned char answer[MAX_SIZE]; |
| 499 |
int res, ret = -1; |
| 500 |
|
| 501 |
#ifdef HAVE_RES_NINIT |
| 502 |
memset(&dnsstate, 0, sizeof(dnsstate)); |
| 503 |
res_ninit(&dnsstate); |
| 504 |
res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer)); |
| 505 |
#else |
| 506 |
ast_mutex_lock(&res_lock); |
| 507 |
res_init(); |
| 508 |
res = res_search(dname, class, type, answer, sizeof(answer)); |
| 509 |
#endif |
| 510 |
if (res > 0) { |
| 511 |
if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) { |
| 512 |
ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname); |
| 513 |
ret = -1; |
| 514 |
} else if (res == 0) { |
| 515 |
ast_debug(1, "No matches found in DNS for %s\n", dname); |
| 516 |
ret = 0; |
| 517 |
} else |
| 518 |
ret = 1; |
| 519 |
} |
| 520 |
#ifdef HAVE_RES_NINIT |
| 521 |
#ifdef HAVE_RES_NDESTROY |
| 522 |
res_ndestroy(&dnsstate); |
| 523 |
#else |
| 524 |
res_nclose(&dnsstate); |
| 525 |
#endif |
| 526 |
#else |
| 527 |
#ifdef HAVE_RES_CLOSE |
| 528 |
res_close(); |
| 529 |
#endif |
| 530 |
ast_mutex_unlock(&res_lock); |
| 531 |
#endif |
| 532 |
|
| 533 |
return ret; |
| 534 |
} |
| 535 |
|
| 536 |
enum ast_dns_search_result ast_search_dns_ex(void *context, const char *dname, int rr_class, int rr_type, |
| 537 |
int (*response_handler)(void *context, unsigned char *dns_response, int dns_response_len, int rcode), |
| 538 |
int (*record_handler)(void *context, unsigned char *record, int record_len, int ttl)) |
| 539 |
{ |
| 540 |
int ret, dns_response_len; |
| 541 |
unsigned char dns_response[MAX_SIZE]; |
| 542 |
|
| 543 |
|
| 544 |
ast_assert(response_handler != NULL); |
| 545 |
ast_assert(record_handler != NULL); |
| 546 |
|
| 547 |
|
| 548 |
dns_response_len = dns_search_res(dname, |
| 549 |
rr_class, |
| 550 |
rr_type, |
| 551 |
dns_response, |
| 552 |
sizeof(dns_response)); |
| 553 |
|
| 554 |
if (dns_response_len < 0) { |
| 555 |
ast_debug(1, "DNS search failed for %s\n", dname); |
| 556 |
response_handler(context, (unsigned char *)"", 0, NXDOMAIN); |
| 557 |
return AST_DNS_SEARCH_FAILURE; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
ret = dns_parse_answer_ex(context, |
| 562 |
rr_class, |
| 563 |
rr_type, |
| 564 |
dns_response, |
| 565 |
dns_response_len, |
| 566 |
response_handler, |
| 567 |
record_handler); |
| 568 |
|
| 569 |
|
| 570 |
if (ret == AST_DNS_SEARCH_FAILURE) { |
| 571 |
|
| 572 |
ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname); |
| 573 |
} else if (ret == AST_DNS_SEARCH_NO_RECORDS) { |
| 574 |
|
| 575 |
ast_debug(1, "DNS search yielded no results for %s\n", dname); |
| 576 |
} |
| 577 |
|
| 578 |
return ret; |
| 579 |
} |
| 580 |
|
| 581 |
struct ao2_container *ast_dns_get_nameservers(void) |
| 582 |
{ |
| 583 |
#ifdef HAVE_RES_NINIT |
| 584 |
struct __res_state dnsstate; |
| 585 |
#endif |
| 586 |
struct __res_state *state; |
| 587 |
struct ao2_container *nameservers; |
| 588 |
int i; |
| 589 |
|
| 590 |
nameservers = ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 3); |
| 591 |
if (!nameservers) { |
| 592 |
return NULL; |
| 593 |
} |
| 594 |
|
| 595 |
#ifdef HAVE_RES_NINIT |
| 596 |
memset(&dnsstate, 0, sizeof(dnsstate)); |
| 597 |
res_ninit(&dnsstate); |
| 598 |
state = &dnsstate; |
| 599 |
#else |
| 600 |
ast_mutex_lock(&res_lock); |
| 601 |
res_init(); |
| 602 |
state = &_res; |
| 603 |
#endif |
| 604 |
|
| 605 |
for (i = 0; i < state->nscount; i++) { |
| 606 |
char addr[INET6_ADDRSTRLEN]; |
| 607 |
const char *addrp = NULL; |
| 608 |
|
| 609 |
|
| 610 |
if (state->nsaddr_list[i].sin_family) { |
| 611 |
addrp = inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, addr, sizeof(addr)); |
| 612 |
#if defined(HAVE_RES_NINIT) && defined(HAVE_STRUCT___RES_STATE__U__EXT_NSADDRS) |
| 613 |
} else if (state->_u._ext.nsaddrs[i]) { |
| 614 |
addrp = inet_ntop(AF_INET6, &state->_u._ext.nsaddrs[i]->sin6_addr, addr, sizeof(addr)); |
| 615 |
#endif |
| 616 |
} |
| 617 |
|
| 618 |
if (addrp) { |
| 619 |
ast_debug(1, "Discovered nameserver: %s\n", addrp); |
| 620 |
ast_str_container_add(nameservers, addrp); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
#ifdef HAVE_RES_NINIT |
| 625 |
#ifdef HAVE_RES_NDESTROY |
| 626 |
res_ndestroy(&dnsstate); |
| 627 |
#else |
| 628 |
res_nclose(&dnsstate); |
| 629 |
#endif |
| 630 |
#else |
| 631 |
#ifdef HAVE_RES_CLOSE |
| 632 |
res_close(); |
| 633 |
#endif |
| 634 |
ast_mutex_unlock(&res_lock); |
| 635 |
#endif |
| 636 |
|
| 637 |
return nameservers; |
| 638 |
} |