Asterisk - The Open Source Telephony Project GIT-master-4c84066
Loading...
Searching...
No Matches
Data Structures | Functions
dns_resolver.h File Reference

DNS Resolver API. More...

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

Go to the source code of this file.

Data Structures

struct  ast_dns_resolver
 DNS resolver implementation. More...
 

Functions

int ast_dns_resolver_add_record (struct ast_dns_query *query, int rr_type, int rr_class, int ttl, const char *data, const size_t size)
 Add a DNS record to the result of a DNS query.
 
void ast_dns_resolver_completed (struct ast_dns_query *query)
 Mark a DNS query as having been completed.
 
void * ast_dns_resolver_get_data (const struct ast_dns_query *query)
 Retrieve resolver specific data.
 
int ast_dns_resolver_register (struct ast_dns_resolver *resolver)
 Register a DNS resolver.
 
int ast_dns_resolver_set_data (struct ast_dns_query *query, void *data)
 Set resolver specific data on a query.
 
int ast_dns_resolver_set_result (struct ast_dns_query *query, unsigned int secure, unsigned int bogus, unsigned int rcode, const char *canonical, const char *answer, size_t answer_size)
 Set result information for a DNS query.
 
void ast_dns_resolver_unregister (struct ast_dns_resolver *resolver)
 Unregister a DNS resolver.
 

Detailed Description

DNS Resolver API.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file dns_resolver.h.

Function Documentation

◆ ast_dns_resolver_add_record()

int ast_dns_resolver_add_record ( struct ast_dns_query query,
int  rr_type,
int  rr_class,
int  ttl,
const char *  data,
const size_t  size 
)

Add a DNS record to the result of a DNS query.

Parameters
queryThe DNS query
rr_typeResource record type
rr_classResource record class
ttlTTL of the record
dataThe raw DNS record
sizeThe size of the raw DNS record
Return values
0success
-1failure

Definition at line 536 of file dns_core.c.

537{
538 struct ast_dns_record *record;
539
540 if (rr_type < 0) {
541 ast_debug(2, "Query '%p': Could not add record, invalid resource record type '%d'\n",
542 query, rr_type);
543 return -1;
544 } else if (rr_type > 65536) {
545 ast_debug(2, "Query '%p': Could not add record, resource record type '%d' exceeds maximum\n",
546 query, rr_type);
547 return -1;
548 } else if (rr_class < 0) {
549 ast_debug(2, "Query '%p': Could not add record, invalid resource record class '%d'\n",
550 query, rr_class);
551 return -1;
552 } else if (rr_class > 65536) {
553 ast_debug(2, "Query '%p': Could not add record, resource record class '%d' exceeds maximum\n",
554 query, rr_class);
555 return -1;
556 } else if (ttl < 0) {
557 ast_debug(2, "Query '%p': Could not add record, invalid TTL '%d'\n",
558 query, ttl);
559 return -1;
560 } else if (!data || !size) {
561 ast_debug(2, "Query '%p': Could not add record, no data specified\n",
562 query);
563 return -1;
564 } else if (!query->result) {
565 ast_debug(2, "Query '%p': No result was set on the query, thus records can not be added\n",
566 query);
567 return -1;
568 }
569
570 record = allocate_dns_record(rr_type, query, data, size);
571 if (!record) {
572 return -1;
573 }
574
575 record->rr_type = rr_type;
576 record->rr_class = rr_class;
577 record->ttl = ttl;
578 record->data_len = size;
579 memcpy(record->data_ptr, data, size);
580
581 AST_LIST_INSERT_TAIL(&query->result->records, record, list);
582
583 return 0;
584}
static struct ast_dns_record * allocate_dns_record(unsigned int rr_type, struct ast_dns_query *query, const char *data, const size_t size)
Definition dns_core.c:525
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
struct ast_dns_result * result
Result of the DNS query.
For AST_LIST.
int ttl
Time-to-live of the record.
char data[0]
The raw DNS record.
int rr_class
Resource record class.
int rr_type
Resource record type.
size_t data_len
The size of the raw DNS record.
struct ast_dns_record::@224 list
Linked list information.
char * data_ptr
pointer to record-specific data.
struct ast_dns_result::dns_records records

References allocate_dns_record(), ast_debug, AST_LIST_INSERT_TAIL, ast_dns_record::data, ast_dns_record::data_len, ast_dns_record::data_ptr, ast_dns_record::list, ast_dns_result::records, ast_dns_query::result, ast_dns_record::rr_class, ast_dns_record::rr_type, and ast_dns_record::ttl.

Referenced by AST_TEST_DEFINE(), AST_TEST_DEFINE(), dns_system_resolver_add_record(), naptr_thread(), resolution_thread(), resolution_thread(), srv_thread(), and unbound_resolver_callback().

◆ ast_dns_resolver_completed()

void ast_dns_resolver_completed ( struct ast_dns_query query)

Mark a DNS query as having been completed.

Parameters
queryThe DNS query

Definition at line 600 of file dns_core.c.

601{
602 if (query->result) {
604 }
605
606 query->callback(query);
607}
int ast_dns_query_get_rr_type(const struct ast_dns_query *query)
Get the record resource type of a DNS query.
Definition dns_core.c:62
static void sort_result(int rr_type, struct ast_dns_result *result)
Definition dns_core.c:593
ast_dns_resolve_callback callback
Callback to invoke upon completion.

References ast_dns_query_get_rr_type(), ast_dns_query::callback, ast_dns_query::result, and sort_result().

Referenced by dns_system_resolver_process_query(), naptr_thread(), resolution_thread(), resolution_thread(), resolution_thread(), srv_thread(), and unbound_resolver_callback().

◆ ast_dns_resolver_get_data()

void * ast_dns_resolver_get_data ( const struct ast_dns_query query)

Retrieve resolver specific data.

Parameters
queryThe DNS query
Returns
the resolver specific data
Note
The reference count of the resolver data is NOT incremented on return

Definition at line 452 of file dns_core.c.

453{
454 return query->resolver_data;
455}
void * resolver_data
Resolver-specific data.

References ast_dns_query::resolver_data.

Referenced by AST_TEST_DEFINE(), and unbound_resolver_cancel().

◆ ast_dns_resolver_register()

int ast_dns_resolver_register ( struct ast_dns_resolver resolver)

Register a DNS resolver.

Parameters
resolverA DNS resolver implementation
Return values
0success
-1failure

Definition at line 633 of file dns_core.c.

634{
635 struct ast_dns_resolver *iter;
636 int inserted = 0;
637
638 if (!resolver) {
639 return -1;
640 } else if (ast_strlen_zero(resolver->name)) {
641 ast_log(LOG_ERROR, "Registration of DNS resolver failed as it does not have a name\n");
642 return -1;
643 } else if (!resolver->resolve) {
644 ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the resolve callback which is required\n",
645 resolver->name);
646 return -1;
647 } else if (!resolver->cancel) {
648 ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the cancel callback which is required\n",
649 resolver->name);
650 return -1;
651 }
652
654
655 AST_LIST_TRAVERSE(&resolvers, iter, next) {
656 if (!strcmp(iter->name, resolver->name)) {
657 ast_log(LOG_ERROR, "A DNS resolver with the name '%s' is already registered\n", resolver->name);
659 return -1;
660 }
661 }
662
664 if (iter->priority > resolver->priority) {
665 AST_RWLIST_INSERT_BEFORE_CURRENT(resolver, next);
666 inserted = 1;
667 break;
668 }
669 }
671
672 if (!inserted) {
673 AST_RWLIST_INSERT_TAIL(&resolvers, resolver, next);
674 }
675
677
678 ast_verb(5, "Registered DNS resolver '%s' with priority '%d'\n", resolver->name, resolver->priority);
679
680 return 0;
681}
#define ast_log
Definition astobj2.c:42
#define LOG_ERROR
#define ast_verb(level,...)
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_RWLIST_TRAVERSE_SAFE_END
#define AST_RWLIST_INSERT_TAIL
#define AST_RWLIST_INSERT_BEFORE_CURRENT
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
DNS resolver implementation.
unsigned int priority
Priority for this resolver if multiple exist, lower being higher priority.
int(* cancel)(struct ast_dns_query *query)
Cancel resolution of a DNS query.
const char * name
The name of the resolver implementation.
int(* resolve)(struct ast_dns_query *query)
Perform resolution of a DNS query.

References AST_LIST_TRAVERSE, ast_log, AST_RWLIST_INSERT_BEFORE_CURRENT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strlen_zero(), ast_verb, ast_dns_resolver::cancel, LOG_ERROR, ast_dns_resolver::name, ast_dns_resolver::priority, and ast_dns_resolver::resolve.

Referenced by ast_dns_system_resolver_init(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), invalid_record_test(), load_module(), nominal_test(), off_nominal_test(), and query_set_test().

◆ ast_dns_resolver_set_data()

int ast_dns_resolver_set_data ( struct ast_dns_query query,
void *  data 
)

Set resolver specific data on a query.

Parameters
queryThe DNS query
dataThe resolver specific data
Note
The resolver data MUST be an ao2 object
This function increments the reference count of the resolver data, it does NOT steal
Once resolver specific data has been set it can not be changed
Return values
0success
-1failure, resolver data is already set

Definition at line 441 of file dns_core.c.

442{
443 if (query->resolver_data) {
444 return -1;
445 }
446
447 query->resolver_data = ao2_bump(data);
448
449 return 0;
450}
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition astobj2.h:480

References ao2_bump, and ast_dns_query::resolver_data.

Referenced by AST_TEST_DEFINE(), and unbound_resolver_resolve().

◆ ast_dns_resolver_set_result()

int ast_dns_resolver_set_result ( struct ast_dns_query query,
unsigned int  secure,
unsigned int  bogus,
unsigned int  rcode,
const char *  canonical,
const char *  answer,
size_t  answer_size 
)

Set result information for a DNS query.

Parameters
queryThe DNS query
secureWhether the result is secured or not
bogusWhether the result is bogus or not
rcodeOptional response code
canonicalThe canonical name
answerThe raw DNS answer
answer_sizeThe size of the raw DNS answer

Zero-sized and NULL answers are permitted by this function. This may be necessary if the query fails at an early stage and no actual DNS response has been received from a DNS server.

Return values
0success
-1failure

Definition at line 457 of file dns_core.c.

459{
460 char *buf_ptr;
461
462 if (secure && bogus) {
463 ast_debug(2, "Query '%p': Could not set result information, it can not be both secure and bogus\n",
464 query);
465 return -1;
466 }
467
468 if (ast_strlen_zero(canonical)) {
469 ast_debug(2, "Query '%p': Could not set result information since no canonical name was provided\n",
470 query);
471 return -1;
472 }
473
474 if (!answer) {
475 answer = "";
476 answer_size = 0;
477 ast_debug(2, "Query '%p': Assuming zero-sized answer on NULL input\n", query);
478 }
479
481
482 query->result = ast_calloc(1, sizeof(*query->result) + strlen(canonical) + 1 + answer_size);
483 if (!query->result) {
484 return -1;
485 }
486
487 query->result->secure = secure;
488 query->result->bogus = bogus;
489 query->result->rcode = rcode;
490
491 buf_ptr = query->result->buf;
492 strcpy(buf_ptr, canonical); /* SAFE */
493 query->result->canonical = buf_ptr;
494
495 buf_ptr += strlen(canonical) + 1;
496 memcpy(buf_ptr, answer, answer_size); /* SAFE */
497 query->result->answer = buf_ptr;
498 query->result->answer_size = answer_size;
499
500 return 0;
501}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
static int answer(void *data)
Definition chan_pjsip.c:687
void ast_dns_result_free(struct ast_dns_result *result)
Free the DNS result information.
Definition dns_core.c:130
char buf[0]
Buffer for dynamic data.
const char * canonical
The canonical name.
size_t answer_size
The size of the raw DNS answer.
unsigned int bogus
Whether the result is bogus.
unsigned int secure
Whether the result is secure.
const char * answer
The raw DNS answer.
unsigned int rcode
Optional rcode, set if an error occurred.

References answer(), ast_dns_result::answer, ast_dns_result::answer_size, ast_calloc, ast_debug, ast_dns_result_free(), ast_strlen_zero(), ast_dns_result::bogus, ast_dns_result::buf, ast_dns_result::canonical, ast_dns_result::rcode, ast_dns_query::result, and ast_dns_result::secure.

Referenced by AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), dns_system_resolver_set_response(), naptr_thread(), resolution_thread(), resolution_thread(), resolution_thread(), srv_thread(), and unbound_resolver_callback().

◆ ast_dns_resolver_unregister()

void ast_dns_resolver_unregister ( struct ast_dns_resolver resolver)

Unregister a DNS resolver.

Parameters
resolverA DNS resolver implementation

Definition at line 683 of file dns_core.c.

684{
685 struct ast_dns_resolver *iter;
686
687 if (!resolver) {
688 return;
689 }
690
693 if (resolver == iter) {
695 break;
696 }
697 }
700
701 ast_verb(5, "Unregistered DNS resolver '%s'\n", resolver->name);
702}
#define AST_RWLIST_REMOVE_CURRENT

References AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, and ast_dns_resolver::name.

Referenced by AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), dns_system_resolver_destroy(), invalid_record_test(), nominal_test(), off_nominal_test(), and query_set_test().