Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
dns_system_resolver.c File Reference

The default DNS resolver for Asterisk. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/astobj2.h"
#include "asterisk/dns.h"
#include "asterisk/dns_core.h"
#include "asterisk/dns_resolver.h"
#include "asterisk/linkedlists.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/utils.h"
Include dependency graph for dns_system_resolver.c:

Go to the source code of this file.

Macros

#define DNS_SYSTEM_RESOLVER_FAILURE   -1
 Resolver return code upon failure. More...
 
#define DNS_SYSTEM_RESOLVER_PRIORITY   INT_MAX
 The consideration priority for this resolver implementation. More...
 
#define DNS_SYSTEM_RESOLVER_SUCCESS   0
 Resolver return code upon success. More...
 

Functions

int ast_dns_system_resolver_init (void)
 Initializes the resolver. More...
 
static int dns_system_resolver_add_record (void *context, unsigned char *record, int record_len, int ttl)
 Callback to handle processing resource records. More...
 
static int dns_system_resolver_cancel (struct ast_dns_query *query)
 Cancels processing resolution for a given query. More...
 
static void dns_system_resolver_destroy (void)
 Destructor. More...
 
static int dns_system_resolver_process_query (void *data)
 Callback to handle processing the query from the ast_taskprocessor instance. More...
 
static int dns_system_resolver_resolve (struct ast_dns_query *query)
 Resolves a DNS query. More...
 
static int dns_system_resolver_set_response (void *context, unsigned char *dns_response, int dns_response_len, int rcode)
 Callback to handle initializing the results field. More...
 

Variables

struct ast_dns_resolver dns_system_resolver_base
 The base definition for the dns_system_resolver. More...
 
static struct ast_taskprocessordns_system_resolver_tp
 The task processor to use for making DNS searches asynchronous. More...
 

Detailed Description

The default DNS resolver for Asterisk.

Author
Ashley Sanders asand.nosp@m.ers@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file dns_system_resolver.c.

Macro Definition Documentation

◆ DNS_SYSTEM_RESOLVER_FAILURE

#define DNS_SYSTEM_RESOLVER_FAILURE   -1

Resolver return code upon failure.

Definition at line 46 of file dns_system_resolver.c.

Referenced by ast_dns_system_resolver_init(), and dns_system_resolver_cancel().

◆ DNS_SYSTEM_RESOLVER_PRIORITY

#define DNS_SYSTEM_RESOLVER_PRIORITY   INT_MAX

The consideration priority for this resolver implementation.

Definition at line 40 of file dns_system_resolver.c.

◆ DNS_SYSTEM_RESOLVER_SUCCESS

#define DNS_SYSTEM_RESOLVER_SUCCESS   0

Resolver return code upon success.

Definition at line 43 of file dns_system_resolver.c.

Referenced by ast_dns_system_resolver_init(), and dns_system_resolver_set_response().

Function Documentation

◆ ast_dns_system_resolver_init()

int ast_dns_system_resolver_init ( void  )

Initializes the resolver.

Return values
0on success
-1on failure

Definition at line 243 of file dns_system_resolver.c.

References ast_dns_resolver_register(), ast_register_cleanup(), ast_taskprocessor_get(), dns_system_resolver_destroy(), DNS_SYSTEM_RESOLVER_FAILURE, DNS_SYSTEM_RESOLVER_SUCCESS, and TPS_REF_DEFAULT.

Referenced by asterisk_daemon().

244 {
245  /* Register the base resolver */
247 
248  if (res) {
250  }
251 
252  /* Instantiate the task processor */
253  dns_system_resolver_tp = ast_taskprocessor_get("dns_system_resolver_tp",
255 
256  /* Return error if the task processor failed to instantiate */
257  if (!dns_system_resolver_tp) {
259  }
260 
261  /* Register the cleanup function */
263 
265 }
struct ast_taskprocessor * ast_taskprocessor_get(const char *name, enum ast_tps_options create)
Get a reference to a taskprocessor with the specified name and create the taskprocessor if necessary...
return a reference to a taskprocessor, create one if it does not exist
Definition: taskprocessor.h:75
#define DNS_SYSTEM_RESOLVER_SUCCESS
Resolver return code upon success.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
Register a DNS resolver.
Definition: dns_core.c:630
static void dns_system_resolver_destroy(void)
Destructor.
struct ast_dns_resolver dns_system_resolver_base
The base definition for the dns_system_resolver.
#define DNS_SYSTEM_RESOLVER_FAILURE
Resolver return code upon failure.
static struct ast_taskprocessor * dns_system_resolver_tp
The task processor to use for making DNS searches asynchronous.

◆ dns_system_resolver_add_record()

static int dns_system_resolver_add_record ( void *  context,
unsigned char *  record,
int  record_len,
int  ttl 
)
static

Callback to handle processing resource records.

Adds an individual resource record discovered with ast_search_dns_ex to the ast_dns_query currently being resolved.

Definition at line 84 of file dns_system_resolver.c.

References ast_dns_query_get_rr_class(), ast_dns_query_get_rr_type(), ast_dns_resolver_add_record(), and context.

Referenced by dns_system_resolver_process_query().

85 {
86  struct ast_dns_query *query = context;
87 
88  /* Add the record to the query.*/
89  return ast_dns_resolver_add_record(query,
92  ttl,
93  (const char*) record,
94  record_len);
95 }
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.
Definition: dns_core.c:535
int ast_dns_query_get_rr_class(const struct ast_dns_query *query)
Get the record resource class of a DNS query.
Definition: dns_core.c:67
A DNS query.
Definition: dns_internal.h:137
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 char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ dns_system_resolver_cancel()

static int dns_system_resolver_cancel ( struct ast_dns_query query)
static

Cancels processing resolution for a given query.

Note
The system API calls block so there is no way to cancel them. Therefore, this function always returns failure when invoked.

Definition at line 110 of file dns_system_resolver.c.

References DNS_SYSTEM_RESOLVER_FAILURE.

111 {
113 }
#define DNS_SYSTEM_RESOLVER_FAILURE
Resolver return code upon failure.

◆ dns_system_resolver_destroy()

static void dns_system_resolver_destroy ( void  )
static

Destructor.

Definition at line 120 of file dns_system_resolver.c.

References ast_dns_resolver_unregister(), and ast_taskprocessor_unreference().

Referenced by ast_dns_system_resolver_init().

121 {
122  /* Unreference the task processor */
124 
125  /* Unregister the base resolver */
127 }
void ast_dns_resolver_unregister(struct ast_dns_resolver *resolver)
Unregister a DNS resolver.
Definition: dns_core.c:680
struct ast_dns_resolver dns_system_resolver_base
The base definition for the dns_system_resolver.
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
static struct ast_taskprocessor * dns_system_resolver_tp
The task processor to use for making DNS searches asynchronous.

◆ dns_system_resolver_process_query()

static int dns_system_resolver_process_query ( void *  data)
static

Callback to handle processing the query from the ast_taskprocessor instance.

Definition at line 140 of file dns_system_resolver.c.

References ao2_ref, ast_debug, ast_dns_query_get_name(), ast_dns_query_get_rr_class(), ast_dns_query_get_rr_type(), ast_dns_resolver_completed(), AST_DNS_SEARCH_FAILURE, AST_DNS_SEARCH_NO_RECORDS, ast_search_dns_ex(), dns_system_resolver_add_record(), and dns_system_resolver_set_response().

Referenced by dns_system_resolver_resolve().

141 {
142  struct ast_dns_query *query = data;
143 
144  /* Perform the DNS search */
145  enum ast_dns_search_result res = ast_search_dns_ex(query,
146  ast_dns_query_get_name(query),
151 
152  /* Handle the possible return values from the DNS search */
153  if (res == AST_DNS_SEARCH_FAILURE) {
154  ast_debug(1, "DNS search failed for query: '%s'\n",
155  ast_dns_query_get_name(query));
156  } else if (res == AST_DNS_SEARCH_NO_RECORDS) {
157  ast_debug(1, "DNS search failed to yield any results for query: '%s'\n",
158  ast_dns_query_get_name(query));
159  }
160 
161  /* Mark the query as complete */
163 
164  /* Reduce the reference count on the query object */
165  ao2_ref(query, -1);
166 
167  return res;
168 }
const char * ast_dns_query_get_name(const struct ast_dns_query *query)
Get the name queried in a DNS query.
Definition: dns_core.c:57
int ast_dns_query_get_rr_class(const struct ast_dns_query *query)
Get the record resource class of a DNS query.
Definition: dns_core.c:67
static int dns_system_resolver_set_response(void *context, unsigned char *dns_response, int dns_response_len, int rcode)
Callback to handle initializing the results field.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
ast_dns_search_result
DNS search return values.
Definition: dns.h:28
#define ao2_ref(o, delta)
Definition: astobj2.h:464
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.
Definition: dns.c:538
void ast_dns_resolver_completed(struct ast_dns_query *query)
Mark a DNS query as having been completed.
Definition: dns_core.c:599
static int dns_system_resolver_add_record(void *context, unsigned char *record, int record_len, int ttl)
Callback to handle processing resource records.
A DNS query.
Definition: dns_internal.h:137
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

◆ dns_system_resolver_resolve()

static int dns_system_resolver_resolve ( struct ast_dns_query query)
static

Resolves a DNS query.

Definition at line 180 of file dns_system_resolver.c.

References ao2_bump, ao2_ref, ast_dns_query_get_name(), ast_log, ast_taskprocessor_push(), dns_system_resolver_process_query(), and LOG_ERROR.

181 {
182  /* Add query processing handler to the task processor */
185  ao2_bump(query));
186 
187  /* The query processing handler was not added to the task processor */
188  if (res < 0) {
189  ast_log(LOG_ERROR, "Failed to perform async DNS resolution of '%s'\n",
190  ast_dns_query_get_name(query));
191  ao2_ref(query, -1);
192  }
193 
194  /* Return the result of adding the query processing handler to the task processor */
195  return res;
196 }
const char * ast_dns_query_get_name(const struct ast_dns_query *query)
Get the name queried in a DNS query.
Definition: dns_core.c:57
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
static int dns_system_resolver_process_query(void *data)
Callback to handle processing the query from the ast_taskprocessor instance.
int ast_taskprocessor_push(struct ast_taskprocessor *tps, int(*task_exe)(void *datap), void *datap) attribute_warn_unused_result
Push a task into the specified taskprocessor queue and signal the taskprocessor thread.
static struct ast_taskprocessor * dns_system_resolver_tp
The task processor to use for making DNS searches asynchronous.

◆ dns_system_resolver_set_response()

static int dns_system_resolver_set_response ( void *  context,
unsigned char *  dns_response,
int  dns_response_len,
int  rcode 
)
static

Callback to handle initializing the results field.

Definition at line 210 of file dns_system_resolver.c.

References ast_dns_query_get_name(), ast_dns_query_get_result(), ast_dns_resolver_set_result(), ast_log, context, DNS_SYSTEM_RESOLVER_SUCCESS, and LOG_ERROR.

Referenced by dns_system_resolver_process_query().

211 {
212  struct ast_dns_query *query = context;
213  int res;
214 
215  /* Instantiate the query's result field (if necessary). */
216  if (!ast_dns_query_get_result(query)) {
217  res = ast_dns_resolver_set_result(query,
218  0,
219  0,
220  rcode,
221  ast_dns_query_get_name(query),
222  (const char*) dns_response,
223  dns_response_len);
224 
225  if (res) {
226  /* There was a problem instantiating the results field. */
227  ast_log(LOG_ERROR, "Could not instantiate the results field for query: '%s'\n",
228  ast_dns_query_get_name(query));
229  }
230  } else {
232  }
233 
234  return res;
235 }
const char * ast_dns_query_get_name(const struct ast_dns_query *query)
Get the name queried in a DNS query.
Definition: dns_core.c:57
#define DNS_SYSTEM_RESOLVER_SUCCESS
Resolver return code upon success.
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
struct ast_dns_result * ast_dns_query_get_result(const struct ast_dns_query *query)
Get the result information for a DNS query.
Definition: dns_core.c:77
A DNS query.
Definition: dns_internal.h:137
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
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.
Definition: dns_core.c:456

Variable Documentation

◆ dns_system_resolver_base

struct ast_dns_resolver dns_system_resolver_base

The base definition for the dns_system_resolver.

Definition at line 61 of file dns_system_resolver.c.

◆ dns_system_resolver_tp

struct ast_taskprocessor* dns_system_resolver_tp
static

The task processor to use for making DNS searches asynchronous.

Definition at line 58 of file dns_system_resolver.c.