Asterisk - The Open Source Telephony Project GIT-master-a358458
Enumerations | Functions
dns.h File Reference

DNS support for Asterisk. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  ast_dns_search_result { AST_DNS_SEARCH_FAILURE = -1 , AST_DNS_SEARCH_NO_RECORDS = 0 , AST_DNS_SEARCH_SUCCESS = 1 }
 DNS search return values. More...
 

Functions

struct ao2_containerast_dns_get_nameservers (void)
 Retrieve the configured nameservers of the system. More...
 
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) More...
 
enum ast_dns_search_result ast_search_dns_ex (void *context, const char *dname, int rr_class, int rr_type, int(*response_handler)(void *context, unsigned char *dns_response, int dns_response_len, int rcode), int(*record_handler)(void *context, unsigned char *record, int record_len, int ttl))
 Extended version of the DNS search function. More...
 

Detailed Description

DNS support for Asterisk.

Author
Thorsten Lockert tholo.nosp@m.@tro.nosp@m.llpho.nosp@m.ne.o.nosp@m.rg

Definition in file dns.h.

Enumeration Type Documentation

◆ ast_dns_search_result

DNS search return values.

Enumerator
AST_DNS_SEARCH_FAILURE 

DNS search resulted in failure

AST_DNS_SEARCH_NO_RECORDS 

DNS search yielded no results

AST_DNS_SEARCH_SUCCESS 

DNS search yielded at least one discovered record

Definition at line 28 of file dns.h.

28 {
29 AST_DNS_SEARCH_FAILURE = -1, /*!< DNS search resulted in failure */
30 AST_DNS_SEARCH_NO_RECORDS = 0, /*!< DNS search yielded no results */
31 AST_DNS_SEARCH_SUCCESS = 1 /*!< DNS search yielded at least one discovered record */
32};
@ AST_DNS_SEARCH_SUCCESS
Definition: dns.h:31
@ AST_DNS_SEARCH_FAILURE
Definition: dns.h:29
@ AST_DNS_SEARCH_NO_RECORDS
Definition: dns.h:30

Function Documentation

◆ ast_dns_get_nameservers()

struct ao2_container * ast_dns_get_nameservers ( void  )

Retrieve the configured nameservers of the system.

Definition at line 581 of file dns.c.

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
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 /* glibc sets sin_family to 0 when the nameserver is an IPv6 address */
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}
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
enum cc_state state
Definition: ccss.c:393
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
#define NULL
Definition: resample.c:96
struct ao2_container * ast_str_container_alloc_options(enum ao2_alloc_opts opts, int buckets)
Allocates a hash container for bare strings.
Definition: strings.c:200
int ast_str_container_add(struct ao2_container *str_container, const char *add)
Adds a string to a string container allocated by ast_str_container_alloc.
Definition: strings.c:205
Generic container type.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ast_debug, ast_mutex_lock, ast_mutex_unlock, ast_str_container_add(), ast_str_container_alloc_options(), NULL, and state.

Referenced by system_create_resolver_and_set_nameservers().

◆ ast_search_dns()

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)

Parameters
contextVoid pointer containing data to use in the callback function.
dnameDomain name to lookup (host, SRV domain, TXT record name).
classRecord Class (see "man res_search").
typeRecord type (see "man res_search").
callbackCallback function for handling the discovered resource records from the DNS search. len gets the length of the full DNS response.
Return values
-1on search failure
0on no records found
1on success
Note
Asterisk DNS is synchronus at this time. This means that if your DNS service does not work, Asterisk may lock while waiting for a response.

Perform DNS lookup (used by DNS, enum and SRV lookups)

Note
Asterisk DNS is synchronus at this time. This means that if your DNS does not work properly, Asterisk might not start properly or a channel may lock.

Definition at line 491 of file dns.c.

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}
#define ast_log
Definition: astobj2.c:42
static const char type[]
Definition: chan_ooh323.c:109
static int answer(void *data)
Definition: chan_pjsip.c:683
#define MAX_SIZE
The maximum size permitted for the answer from the DNS server.
Definition: dns.c:47
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.
Definition: dns.c:329
#define LOG_WARNING

References answer(), ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, voicemailpwcheck::context, dns_parse_answer(), LOG_WARNING, MAX_SIZE, and type.

Referenced by ast_get_enum(), ast_get_srv(), ast_get_txt(), ast_srv_lookup(), blr_ebl(), and blr_txt().

◆ ast_search_dns_ex()

enum ast_dns_search_result ast_search_dns_ex ( void *  context,
const char *  dname,
int  rr_class,
int  rr_type,
int(*)(void *context, unsigned char *dns_response, int dns_response_len, int rcode)  response_handler,
int(*)(void *context, unsigned char *record, int record_len, int ttl)  record_handler 
)

Extended version of the DNS search function.

Performs a DNS lookup, (used by DNS, enum and SRV lookups), parses the results and notifies the observer with the response and discovered records via invoking the provided callbacks (used by ast_dns_system_resolver).

Parameters
contextVoid pointer containing data to use in the handler functions.
dnameDomain name to lookup (host, SRV domain, TXT record name).
rr_classRecord Class (see "man res_search").
rr_typeRecord type (see "man res_search").
response_handlerCallback function for handling the DNS response. Invoked upon completion of the DNS search.
record_handlerCallback function for handling the discovered resource records from the DNS search. Invoked n times, where n is the number of records discovered while parsing the DNS response.
Return values
AST_DNS_SEARCH_FAILUREon search failure
AST_DNS_SEARCH_NO_RECORDSon no records found
AST_DNS_SEARCH_SUCCESSon success
Note
Asterisk DNS is synchronus at this time. This means that if your DNS service does not work, Asterisk may lock while waiting for a response.

Definition at line 536 of file dns.c.

539{
540 int ret, dns_response_len;
541 unsigned char dns_response[MAX_SIZE];
542
543 /* Assert that the callbacks are not NULL */
544 ast_assert(response_handler != NULL);
545 ast_assert(record_handler != NULL);
546
547 /* Try the DNS search. */
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);
558 }
559
560 /* Parse records from DNS response */
562 rr_class,
563 rr_type,
564 dns_response,
565 dns_response_len,
566 response_handler,
567 record_handler);
568
569 /* Handle the return code from parsing the DNS response */
570 if (ret == AST_DNS_SEARCH_FAILURE) {
571 /* Parsing Error */
572 ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
573 } else if (ret == AST_DNS_SEARCH_NO_RECORDS) {
574 /* No results found */
575 ast_debug(1, "DNS search yielded no results for %s\n", dname);
576 }
577
578 return ret;
579}
static int dns_search_res(const char *dname, int rr_class, int rr_type, unsigned char *dns_response, int dns_response_len)
Handles the DNS search if the system has RES_NINIT.
Definition: dns.c:287
static int dns_parse_answer_ex(void *context, int rr_class, int rr_type, unsigned char *answer, int answer_len, int(*response_handler)(void *context, unsigned char *dns_response, int dns_response_len, int rcode), int(*record_handler)(void *context, unsigned char *record, int record_len, int ttl))
Extended version of the DNS Parsing function.
Definition: dns.c:407
#define ast_assert(a)
Definition: utils.h:739

References ast_assert, ast_debug, AST_DNS_SEARCH_FAILURE, AST_DNS_SEARCH_NO_RECORDS, ast_log, voicemailpwcheck::context, dns_parse_answer_ex(), dns_search_res(), LOG_WARNING, MAX_SIZE, and NULL.

Referenced by dns_system_resolver_process_query().