Asterisk - The Open Source Telephony Project  18.5.0
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 };

Function Documentation

◆ ast_dns_get_nameservers()

struct ao2_container* ast_dns_get_nameservers ( void  )

Retrieve the configured nameservers of the system.

Definition at line 583 of file dns.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ast_inet_ntoa(), 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().

584 {
585 #ifdef HAVE_RES_NINIT
586  struct __res_state dnsstate;
587 #endif
588  struct __res_state *state;
589  struct ao2_container *nameservers;
590  int i;
591 
593  if (!nameservers) {
594  return NULL;
595  }
596 
597 #ifdef HAVE_RES_NINIT
598  memset(&dnsstate, 0, sizeof(dnsstate));
599  res_ninit(&dnsstate);
600  state = &dnsstate;
601 #else
602  ast_mutex_lock(&res_lock);
603  res_init();
604  state = &_res;
605 #endif
606 
607  for (i = 0; i < state->nscount; i++) {
608  ast_str_container_add(nameservers, ast_inet_ntoa(state->nsaddr_list[i].sin_addr));
609  }
610 
611 #ifdef HAVE_RES_NINIT
612 #ifdef HAVE_RES_NDESTROY
613  res_ndestroy(&dnsstate);
614 #else
615  res_nclose(&dnsstate);
616 #endif
617 #else
618 #ifdef HAVE_RES_CLOSE
619  res_close();
620 #endif
621  ast_mutex_unlock(&res_lock);
622 #endif
623 
624  return nameservers;
625 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define ast_mutex_lock(a)
Definition: lock.h:187
#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:201
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
Definition: main/utils.c:782
Generic container type.
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:206
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ 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").
answerThe full DNS response.
lenThe length of the full DNS response.
callbackCallback function for handling the discovered resource records from the DNS search.
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 493 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(), ast_get_txt(), ast_srv_lookup(), blr_ebl(), and blr_txt().

496 {
497 #ifdef HAVE_RES_NINIT
498  struct __res_state dnsstate;
499 #endif
500  unsigned char answer[MAX_SIZE];
501  int res, ret = -1;
502 
503 #ifdef HAVE_RES_NINIT
504  memset(&dnsstate, 0, sizeof(dnsstate));
505  res_ninit(&dnsstate);
506  res = res_nsearch(&dnsstate, dname, class, type, answer, sizeof(answer));
507 #else
508  ast_mutex_lock(&res_lock);
509  res_init();
510  res = res_search(dname, class, type, answer, sizeof(answer));
511 #endif
512  if (res > 0) {
513  if ((res = dns_parse_answer(context, class, type, answer, res, callback)) < 0) {
514  ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
515  ret = -1;
516  } else if (res == 0) {
517  ast_debug(1, "No matches found in DNS for %s\n", dname);
518  ret = 0;
519  } else
520  ret = 1;
521  }
522 #ifdef HAVE_RES_NINIT
523 #ifdef HAVE_RES_NDESTROY
524  res_ndestroy(&dnsstate);
525 #else
526  res_nclose(&dnsstate);
527 #endif
528 #else
529 #ifdef HAVE_RES_CLOSE
530  res_close();
531 #endif
532  ast_mutex_unlock(&res_lock);
533 #endif
534 
535  return ret;
536 }
static const char type[]
Definition: chan_ooh323.c:109
#define LOG_WARNING
Definition: logger.h:274
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static int answer(void *data)
Definition: chan_pjsip.c:682
#define MAX_SIZE
The maximum size permitted for the answer from the DNS server.
Definition: dns.c:47
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
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:330
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ 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 538 of file dns.c.

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

Referenced by dns_system_resolver_process_query().

541 {
542  int ret, dns_response_len;
543  unsigned char dns_response[MAX_SIZE];
544 
545  /* Assert that the callbacks are not NULL */
546  ast_assert(response_handler != NULL);
547  ast_assert(record_handler != NULL);
548 
549  /* Try the DNS search. */
550  dns_response_len = dns_search_res(dname,
551  rr_class,
552  rr_type,
553  dns_response,
554  sizeof(dns_response));
555 
556  if (dns_response_len < 0) {
557  ast_debug(1, "DNS search failed for %s\n", dname);
558  response_handler(context, (unsigned char *)"", 0, NXDOMAIN);
559  return AST_DNS_SEARCH_FAILURE;
560  }
561 
562  /* Parse records from DNS response */
564  rr_class,
565  rr_type,
566  dns_response,
567  dns_response_len,
568  response_handler,
569  record_handler);
570 
571  /* Handle the return code from parsing the DNS response */
572  if (ret == AST_DNS_SEARCH_FAILURE) {
573  /* Parsing Error */
574  ast_log(LOG_WARNING, "DNS Parse error for %s\n", dname);
575  } else if (ret == AST_DNS_SEARCH_NO_RECORDS) {
576  /* No results found */
577  ast_debug(1, "DNS search yielded no results for %s\n", dname);
578  }
579 
580  return ret;
581 }
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:409
#define LOG_WARNING
Definition: logger.h:274
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
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
#define MAX_SIZE
The maximum size permitted for the answer from the DNS server.
Definition: dns.c:47
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116