Asterisk - The Open Source Telephony Project GIT-master-7921072
Data Structures | Macros | Functions | Variables
dnsmgr.c File Reference

Background DNS update manager. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include <regex.h>
#include <signal.h>
#include "asterisk/module.h"
#include "asterisk/dnsmgr.h"
#include "asterisk/linkedlists.h"
#include "asterisk/utils.h"
#include "asterisk/config.h"
#include "asterisk/sched.h"
#include "asterisk/cli.h"
#include "asterisk/manager.h"
#include "asterisk/acl.h"
Include dependency graph for dnsmgr.c:

Go to the source code of this file.

Data Structures

struct  ast_dnsmgr_entry
 
struct  entry_list
 
struct  refresh_info
 

Macros

#define REFRESH_DEFAULT   300
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
int ast_dnsmgr_changed (struct ast_dnsmgr_entry *entry)
 Check is see if a dnsmgr entry has changed. More...
 
struct ast_dnsmgr_entryast_dnsmgr_get (const char *name, struct ast_sockaddr *result, const char *service)
 Allocate a new DNS manager entry. More...
 
struct ast_dnsmgr_entryast_dnsmgr_get_family (const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
 Allocate a new DNS manager entry. More...
 
int ast_dnsmgr_lookup (const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
 Allocate and initialize a DNS manager entry. More...
 
int ast_dnsmgr_lookup_cb (const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service, dns_update_func func, void *data)
 Allocate and initialize a DNS manager entry, with update callback. More...
 
int ast_dnsmgr_refresh (struct ast_dnsmgr_entry *entry)
 Force a refresh of a dnsmgr entry. More...
 
void ast_dnsmgr_release (struct ast_dnsmgr_entry *entry)
 Free a DNS manager entry. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int dnsmgr_refresh (struct ast_dnsmgr_entry *entry, int verbose)
 
void dnsmgr_start_refresh (void)
 
static void * do_refresh (void *data)
 
static int do_reload (int loading)
 
static char * handle_cli_refresh (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_status (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int internal_dnsmgr_lookup (const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service, dns_update_func func, void *data)
 
static int load_module (void)
 
static int refresh_list (const void *data)
 
static int reload_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "DNS Manager" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CORE, .requires = "extconfig", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_cli_entry cli_refresh = { .handler = handle_cli_refresh , .summary = "Performs an immediate refresh" ,}
 
static struct ast_cli_entry cli_reload = { .handler = handle_cli_reload , .summary = "Reloads the DNS manager configuration" ,}
 
static struct ast_cli_entry cli_status = { .handler = handle_cli_status , .summary = "Display the DNS manager status" ,}
 
static int enabled
 
static struct entry_list entry_list = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
static struct refresh_info master_refresh_info
 
static int refresh_interval
 
static ast_mutex_t refresh_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 
static int refresh_sched = -1
 
static pthread_t refresh_thread = AST_PTHREADT_NULL
 
static struct ast_sched_contextsched
 

Detailed Description

Background DNS update manager.

Author
Kevin P. Fleming kpfle.nosp@m.ming.nosp@m.@digi.nosp@m.um.c.nosp@m.om
Bug:
There is a minor race condition. In the event that an IP address of a dnsmgr managed host changes, there is the potential for the consumer of that address to access the in_addr data at the same time that the dnsmgr thread is in the middle of updating it to the new address.

Definition in file dnsmgr.c.

Macro Definition Documentation

◆ REFRESH_DEFAULT

#define REFRESH_DEFAULT   300

Definition at line 89 of file dnsmgr.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 527 of file dnsmgr.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 527 of file dnsmgr.c.

◆ ast_dnsmgr_changed()

int ast_dnsmgr_changed ( struct ast_dnsmgr_entry entry)

Check is see if a dnsmgr entry has changed.

Return values
non-zeroif the dnsmgr entry has changed since the last call to this function
zeroif the dnsmgr entry has not changed since the last call to this function

Definition at line 247 of file dnsmgr.c.

248{
249 int changed;
250
251 ast_mutex_lock(&entry->lock);
252
253 changed = entry->changed;
254 entry->changed = 0;
255
256 ast_mutex_unlock(&entry->lock);
257
258 return changed;
259}
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
Definition: search.h:40

References ast_mutex_lock, and ast_mutex_unlock.

Referenced by iax2_do_register().

◆ ast_dnsmgr_get()

struct ast_dnsmgr_entry * ast_dnsmgr_get ( const char *  name,
struct ast_sockaddr result,
const char *  service 
)

Allocate a new DNS manager entry.

Parameters
namethe hostname
resultwhere the DNS manager should store the IP address as it refreshes it.
service

This function allocates a new DNS manager entry object, and fills it with the provided hostname and IP address. This function does not force an initial lookup of the IP address. So, generally, this should be used when the initial address is already known.

Returns
a DNS manager entry
Version
1.6.1 result changed from struct in_addr to struct sockaddr_in to store port number
1.8.0 result changed from struct ast_sockaddr_in to ast_sockaddr for IPv6 support

Definition at line 131 of file dnsmgr.c.

132{
134}
enum ast_cc_service_type service
Definition: ccss.c:383
static PGresult * result
Definition: cel_pgsql.c:84
struct ast_dnsmgr_entry * ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
Allocate a new DNS manager entry.
Definition: dnsmgr.c:106
static const char name[]
Definition: format_mp3.c:68

References ast_dnsmgr_get_family(), name, result, and service.

◆ ast_dnsmgr_get_family()

struct ast_dnsmgr_entry * ast_dnsmgr_get_family ( const char *  name,
struct ast_sockaddr result,
const char *  service,
unsigned int  family 
)

Allocate a new DNS manager entry.

Parameters
namethe hostname
resultwhere the DNS manager should store the IP address as it refreshes it.
service
familyAddress family to filter DNS addresses.

This function allocates a new DNS manager entry object, and fills it with the provided hostname and IP address. This function does not force an initial lookup of the IP address. So, generally, this should be used when the initial address is already known.

Returns
a DNS manager entry

Definition at line 106 of file dnsmgr.c.

107{
108 struct ast_dnsmgr_entry *entry;
109 int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
110
111 if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, total_size))) {
112 return NULL;
113 }
114
115 entry->result = result;
116 ast_mutex_init(&entry->lock);
117 strcpy(entry->name, name);
118 if (service) {
119 entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
120 strcpy(entry->service, service);
121 }
122 entry->family = family;
123
127
128 return entry;
129}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#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.
Definition: linkedlists.h:151
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:718
#define ast_mutex_init(pmutex)
Definition: lock.h:186
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Definition: dnsmgr.c:66
unsigned int family
Definition: dnsmgr.c:72

References ast_calloc, ast_mutex_init, AST_RWLIST_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_strlen_zero(), ast_dnsmgr_entry::family, name, NULL, result, and service.

Referenced by ast_dnsmgr_get(), and internal_dnsmgr_lookup().

◆ ast_dnsmgr_lookup()

int ast_dnsmgr_lookup ( const char *  name,
struct ast_sockaddr result,
struct ast_dnsmgr_entry **  dnsmgr,
const char *  service 
)

Allocate and initialize a DNS manager entry.

Parameters
namethe hostname
resultwhere to store the IP address as the DNS manager refreshes it. The address family is used as an input parameter to filter the returned addresses. If it is 0, both IPv4 and IPv6 addresses can be returned.
dnsmgrWhere to store the allocate DNS manager entry
service
Note
This function allocates a new DNS manager entry object, and fills it with the provided hostname and IP address. This function does force an initial lookup, so it may block for some period of time.
Return values
0success
non-zerofailure
Version
1.6.1 result changed from struct in_addr to struct aockaddr_in to store port number

Definition at line 191 of file dnsmgr.c.

192{
194}
static int internal_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service, dns_update_func func, void *data)
Definition: dnsmgr.c:151

References internal_dnsmgr_lookup(), name, NULL, result, and service.

Referenced by __iax2_do_register_s(), build_peer(), iax2_append_register(), and transport_apply().

◆ ast_dnsmgr_lookup_cb()

int ast_dnsmgr_lookup_cb ( const char *  name,
struct ast_sockaddr result,
struct ast_dnsmgr_entry **  dnsmgr,
const char *  service,
dns_update_func  func,
void *  data 
)

Allocate and initialize a DNS manager entry, with update callback.

Parameters
namethe hostname
resultThe addr which is intended to be updated in the update callback when DNS manager calls it on refresh. The address family is used as an input parameter to filter the returned addresses. If it is 0, both IPv4 and IPv6 addresses can be returned.
dnsmgrWhere to store the allocate DNS manager entry
service
funcThe update callback function The update callback will be called when DNS manager detects that an IP address has been changed. Instead of updating the addr itself, DNS manager will call this callback function with the old and new addresses. It is the responsibility of the callback to perform any updates
dataA pointer to data that will be passed through to the callback function
Note
This function allocates a new DNS manager entry object, and fills it with the provided hostname and IP address. This function does force an initial lookup, so it may block for some period of time.
Return values
0success
non-zerofailure

Definition at line 196 of file dnsmgr.c.

197{
198 return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
199}

References ast_dnsmgr_entry::data, internal_dnsmgr_lookup(), name, result, and service.

◆ ast_dnsmgr_refresh()

int ast_dnsmgr_refresh ( struct ast_dnsmgr_entry entry)

Force a refresh of a dnsmgr entry.

Return values
non-zeroif the result is different than the previous result
zeroif the result is the same as the previous result

Definition at line 239 of file dnsmgr.c.

240{
241 return dnsmgr_refresh(entry, 0);
242}
static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
Definition: dnsmgr.c:204

References dnsmgr_refresh().

Referenced by build_peer(), and iax2_do_register().

◆ ast_dnsmgr_release()

void ast_dnsmgr_release ( struct ast_dnsmgr_entry entry)

Free a DNS manager entry.

Parameters
entrythe DNS manager entry to free

Definition at line 136 of file dnsmgr.c.

137{
138 if (!entry) {
139 return;
140 }
141
145 ast_debug(6, "removing dns manager for '%s'\n", entry->name);
146
147 ast_mutex_destroy(&entry->lock);
149}
#define ast_free(a)
Definition: astmm.h:180
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:885
#define ast_mutex_destroy(a)
Definition: lock.h:188

References ast_debug, ast_free, ast_mutex_destroy, AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by delete_users(), destroy_sip_transport_state(), and peer_destructor().

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 527 of file dnsmgr.c.

◆ dnsmgr_refresh()

static int dnsmgr_refresh ( struct ast_dnsmgr_entry entry,
int  verbose 
)
static

Definition at line 204 of file dnsmgr.c.

205{
206 struct ast_sockaddr tmp = { .len = 0, };
207 int changed = 0;
208
209 ast_mutex_lock(&entry->lock);
210
211 ast_debug(6, "refreshing '%s'\n", entry->name);
212
213 tmp.ss.ss_family = entry->family;
214 if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
215 if (!ast_sockaddr_port(&tmp)) {
217 }
218 if (ast_sockaddr_cmp(&tmp, entry->result)) {
219 const char *old_addr = ast_strdupa(ast_sockaddr_stringify(entry->result));
220 const char *new_addr = ast_strdupa(ast_sockaddr_stringify(&tmp));
221
222 if (entry->update_func) {
223 entry->update_func(entry->result, &tmp, entry->data);
224 } else {
225 ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
226 entry->name, old_addr, new_addr);
227
228 ast_sockaddr_copy(entry->result, &tmp);
229 changed = entry->changed = 1;
230 }
231 }
232 }
233
234 ast_mutex_unlock(&entry->lock);
235
236 return changed;
237}
int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
Get the IP address given a hostname and optional service.
Definition: acl.c:896
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
#define LOG_NOTICE
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:517
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:532
Socket address structure.
Definition: netsock2.h:97
char * data
Definition: search.h:42

References ast_debug, ast_get_ip_or_srv(), ast_log, ast_mutex_lock, ast_mutex_unlock, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_port, ast_sockaddr_set_port, ast_sockaddr_stringify(), ast_strdupa, entry::data, LOG_NOTICE, and tmp().

Referenced by ast_dnsmgr_refresh(), and refresh_list().

◆ dnsmgr_start_refresh()

void dnsmgr_start_refresh ( void  )

Provided by dnsmgr.c

Definition at line 302 of file dnsmgr.c.

303{
304 if (refresh_sched > -1) {
307 }
308}
static int refresh_list(const void *data)
Definition: dnsmgr.c:272
static struct refresh_info master_refresh_info
Definition: dnsmgr.c:101
static int refresh_sched
Definition: dnsmgr.c:63
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Adds a scheduled event with rescheduling support.
Definition: sched.c:526
Definition: sched.c:76

References ast_sched_add_variable(), AST_SCHED_DEL, master_refresh_info, refresh_list(), and refresh_sched.

Referenced by asterisk_daemon().

◆ do_refresh()

static void * do_refresh ( void *  data)
static

Definition at line 261 of file dnsmgr.c.

262{
263 for (;;) {
264 pthread_testcancel();
265 usleep((ast_sched_wait(sched)*1000));
266 pthread_testcancel();
268 }
269 return NULL;
270}
int ast_sched_runq(struct ast_sched_context *con)
Runs the queue.
Definition: sched.c:786
int ast_sched_wait(struct ast_sched_context *con) attribute_warn_unused_result
Determines number of seconds until the next outstanding event to take place.
Definition: sched.c:433

References ast_sched_runq(), ast_sched_wait(), and NULL.

Referenced by do_reload().

◆ do_reload()

static int do_reload ( int  loading)
static

Definition at line 450 of file dnsmgr.c.

451{
452 struct ast_config *config;
453 struct ast_variable *v;
454 struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
455 int interval;
456 int was_enabled;
457
458 if ((config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
459 return 0;
460 }
461
462 /* ensure that no refresh cycles run while the reload is in progress */
464
465 /* reset defaults in preparation for reading config file */
467 was_enabled = enabled;
468 enabled = 0;
469
472 return 0;
473 }
474
476
477 for (v = ast_variable_browse(config, "general"); v; v = v->next) {
478 if (!strcasecmp(v->name, "enable")) {
479 enabled = ast_true(v->value);
480 } else if (!strcasecmp(v->name, "refreshinterval")) {
481 if (sscanf(v->value, "%30d", &interval) < 1) {
482 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
483 } else if (interval < 0) {
484 ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
485 } else {
486 refresh_interval = interval;
487 }
488 }
489 }
491
492 if (enabled && refresh_interval) {
493 ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
494 }
495
496 /* if this reload enabled the manager, create the background thread
497 if it does not exist */
498 if (enabled) {
499 if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
501 ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
502 }
503 }
504 /* make a background refresh happen right away */
506 /* if this reload disabled the manager and there is a background thread, kill it */
507 } else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
508 /* wake up the thread so it will exit */
509 pthread_cancel(refresh_thread);
510 pthread_kill(refresh_thread, SIGURG);
511 pthread_join(refresh_thread, NULL);
513 }
514
516
517 return 0;
518}
static const char config[]
Definition: chan_ooh323.c:111
static int enabled
Definition: dnsmgr.c:91
static int refresh_interval
Definition: dnsmgr.c:92
static void * do_refresh(void *data)
Definition: dnsmgr.c:261
static ast_mutex_t refresh_lock
Definition: dnsmgr.c:87
#define REFRESH_DEFAULT
Definition: dnsmgr.c:89
static pthread_t refresh_thread
Definition: dnsmgr.c:64
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3321
#define CONFIG_STATUS_FILEMISSING
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
@ CONFIG_FLAG_FILEUNCHANGED
#define LOG_ERROR
#define LOG_WARNING
#define AST_PTHREADT_NULL
Definition: lock.h:66
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
Structure used to handle boolean flags.
Definition: utils.h:199
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592

References ast_config_destroy(), ast_config_load2(), ast_log, ast_mutex_lock, ast_mutex_unlock, ast_pthread_create_background, AST_PTHREADT_NULL, ast_sched_add_variable(), AST_SCHED_DEL, ast_true(), ast_variable_browse(), config, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, do_refresh(), enabled, LOG_ERROR, LOG_NOTICE, LOG_WARNING, master_refresh_info, ast_variable::name, ast_variable::next, NULL, REFRESH_DEFAULT, refresh_interval, refresh_list(), refresh_lock, refresh_sched, refresh_thread, and ast_variable::value.

Referenced by handle_cli_reload(), load_module(), and reload_module().

◆ handle_cli_refresh()

static char * handle_cli_refresh ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 332 of file dnsmgr.c.

333{
334 struct refresh_info info = {
335 .entries = &entry_list,
336 .verbose = 1,
337 };
338 switch (cmd) {
339 case CLI_INIT:
340 e->command = "dnsmgr refresh";
341 e->usage =
342 "Usage: dnsmgr refresh [pattern]\n"
343 " Performs an immediate refresh of the managed DNS entries.\n"
344 " Optional regular expression pattern is used to filter the entries to refresh.\n";
345 return NULL;
346 case CLI_GENERATE:
347 return NULL;
348 }
349
350 if (!enabled) {
351 ast_cli(a->fd, "DNS Manager is disabled.\n");
352 return 0;
353 }
354
355 if (a->argc > 3) {
356 return CLI_SHOWUSAGE;
357 }
358
359 if (a->argc == 3) {
360 if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) {
361 return CLI_SHOWUSAGE;
362 } else {
363 info.regex_present = 1;
364 }
365 }
366
368
369 if (info.regex_present) {
370 regfree(&info.filter);
371 }
372
373 return CLI_SUCCESS;
374}
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
static struct entry_list entry_list
def info(msg)
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
static struct test_val a

References a, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, enabled, entry_list, sip_to_pjsip::info(), NULL, refresh_list(), and ast_cli_entry::usage.

◆ handle_cli_reload()

static char * handle_cli_reload ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 312 of file dnsmgr.c.

313{
314 switch (cmd) {
315 case CLI_INIT:
316 e->command = "dnsmgr reload";
317 e->usage =
318 "Usage: dnsmgr reload\n"
319 " Reloads the DNS manager configuration.\n";
320 return NULL;
321 case CLI_GENERATE:
322 return NULL;
323 }
324 if (a->argc > 2) {
325 return CLI_SHOWUSAGE;
326 }
327
328 do_reload(0);
329 return CLI_SUCCESS;
330}
static int do_reload(int loading)
Definition: dnsmgr.c:450

References a, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, do_reload(), NULL, and ast_cli_entry::usage.

◆ handle_cli_status()

static char * handle_cli_status ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 376 of file dnsmgr.c.

377{
378 int count = 0;
379 struct ast_dnsmgr_entry *entry;
380 switch (cmd) {
381 case CLI_INIT:
382 e->command = "dnsmgr status";
383 e->usage =
384 "Usage: dnsmgr status\n"
385 " Displays the DNS manager status.\n";
386 return NULL;
387 case CLI_GENERATE:
388 return NULL;
389 }
390
391 if (a->argc > 2) {
392 return CLI_SHOWUSAGE;
393 }
394
395 ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
396 ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
399 count++;
401 ast_cli(a->fd, "Number of entries: %d\n", count);
402
403 return CLI_SUCCESS;
404}
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494

References a, ast_cli(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, enabled, NULL, refresh_interval, and ast_cli_entry::usage.

◆ internal_dnsmgr_lookup()

static int internal_dnsmgr_lookup ( const char *  name,
struct ast_sockaddr result,
struct ast_dnsmgr_entry **  dnsmgr,
const char *  service,
dns_update_func  func,
void *  data 
)
static

Definition at line 151 of file dnsmgr.c.

152{
153 unsigned int family;
154
155 if (ast_strlen_zero(name) || !result || !dnsmgr) {
156 return -1;
157 }
158
159 if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) {
160 return 0;
161 }
162
163 /* Lookup address family filter. */
164 family = result->ss.ss_family;
165
166 /*
167 * If it's actually an IP address and not a name, there's no
168 * need for a managed lookup.
169 */
171 return 0;
172 }
173
174 ast_debug(6, "doing dnsmgr_lookup for '%s'\n", name);
175
176 /* do a lookup now but add a manager so it will automagically get updated in the background */
178
179 /* if dnsmgr is not enable don't bother adding an entry */
180 if (!enabled) {
181 return 0;
182 }
183
184 ast_debug(6, "adding dns manager for '%s'\n", name);
185 *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
186 (*dnsmgr)->update_func = func;
187 (*dnsmgr)->data = data;
188 return !*dnsmgr;
189}
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230

References ast_debug, ast_dnsmgr_get_family(), ast_get_ip_or_srv(), ast_sockaddr_parse(), ast_strlen_zero(), ast_dnsmgr_entry::data, enabled, ast_dnsmgr_entry::family, name, PARSE_PORT_FORBID, result, and service.

Referenced by ast_dnsmgr_lookup(), and ast_dnsmgr_lookup_cb().

◆ load_module()

static int load_module ( void  )
static

Definition at line 432 of file dnsmgr.c.

433{
434 if (!(sched = ast_sched_context_create())) {
435 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
437 }
441
443}
#define ast_cli_register(e)
Registers a command or an array of commands.
Definition: cli.h:256
static struct ast_cli_entry cli_status
Definition: dnsmgr.c:408
static struct ast_cli_entry cli_refresh
Definition: dnsmgr.c:407
static struct ast_cli_entry cli_reload
Definition: dnsmgr.c:406
@ AST_MODULE_LOAD_FAILURE
Module could not be loaded properly.
Definition: module.h:102
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Definition: sched.c:238

References ast_cli_register, ast_log, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_sched_context_create(), cli_refresh, cli_reload, cli_status, do_reload(), and LOG_ERROR.

◆ refresh_list()

static int refresh_list ( const void *  data)
static

Definition at line 272 of file dnsmgr.c.

273{
274 struct refresh_info *info = (struct refresh_info *)data;
275 struct ast_dnsmgr_entry *entry;
276
277 /* if a refresh or reload is already in progress, exit now */
279 if (info->verbose) {
280 ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
281 }
282 return -1;
283 }
284
285 ast_debug(6, "Refreshing DNS lookups.\n");
286 AST_RWLIST_RDLOCK(info->entries);
287 AST_RWLIST_TRAVERSE(info->entries, entry, list) {
288 if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
289 continue;
290 }
291
292 dnsmgr_refresh(entry, info->verbose);
293 }
294 AST_RWLIST_UNLOCK(info->entries);
295
297
298 /* automatically reschedule based on the interval */
299 return refresh_interval * 1000;
300}
#define ast_mutex_trylock(a)
Definition: lock.h:191

References ast_debug, ast_log, ast_mutex_trylock, ast_mutex_unlock, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, dnsmgr_refresh(), sip_to_pjsip::info(), LOG_WARNING, NULL, refresh_interval, and refresh_lock.

Referenced by dnsmgr_start_refresh(), do_reload(), and handle_cli_refresh().

◆ reload_module()

static int reload_module ( void  )
static

Definition at line 445 of file dnsmgr.c.

446{
447 return do_reload(0);
448}

References do_reload().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 410 of file dnsmgr.c.

411{
415
416 /* Destroy refresh thread. */
419 /* wake up the thread so it will exit */
420 pthread_cancel(refresh_thread);
421 pthread_kill(refresh_thread, SIGURG);
422 pthread_join(refresh_thread, NULL);
424 }
426
428
429 return 0;
430}
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: main/cli.c:2427
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:271

References ast_cli_unregister(), ast_mutex_lock, ast_mutex_unlock, AST_PTHREADT_NULL, ast_sched_context_destroy(), cli_refresh, cli_reload, cli_status, NULL, refresh_lock, and refresh_thread.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "DNS Manager" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CORE, .requires = "extconfig", }
static

Definition at line 527 of file dnsmgr.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 527 of file dnsmgr.c.

◆ cli_refresh

struct ast_cli_entry cli_refresh = { .handler = handle_cli_refresh , .summary = "Performs an immediate refresh" ,}
static

Definition at line 407 of file dnsmgr.c.

Referenced by load_module(), and unload_module().

◆ cli_reload

struct ast_cli_entry cli_reload = { .handler = handle_cli_reload , .summary = "Reloads the DNS manager configuration" ,}
static

Definition at line 406 of file dnsmgr.c.

Referenced by load_module(), and unload_module().

◆ cli_status

struct ast_cli_entry cli_status = { .handler = handle_cli_status , .summary = "Display the DNS manager status" ,}
static

Definition at line 408 of file dnsmgr.c.

Referenced by load_module(), and unload_module().

◆ enabled

int enabled
static

◆ entry_list

struct entry_list entry_list = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static

Referenced by handle_cli_refresh().

◆ master_refresh_info

struct refresh_info master_refresh_info
static
Initial value:
= {
.entries = &entry_list,
.verbose = 0,
}

Definition at line 101 of file dnsmgr.c.

Referenced by dnsmgr_start_refresh(), and do_reload().

◆ refresh_interval

int refresh_interval
static

Definition at line 92 of file dnsmgr.c.

Referenced by do_reload(), handle_cli_status(), and refresh_list().

◆ refresh_lock

ast_mutex_t refresh_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static

Definition at line 87 of file dnsmgr.c.

Referenced by do_reload(), refresh_list(), and unload_module().

◆ refresh_sched

int refresh_sched = -1
static

Definition at line 63 of file dnsmgr.c.

Referenced by dnsmgr_start_refresh(), and do_reload().

◆ refresh_thread

pthread_t refresh_thread = AST_PTHREADT_NULL
static

Definition at line 64 of file dnsmgr.c.

Referenced by do_reload(), and unload_module().

◆ sched

struct ast_sched_context* sched
static

Definition at line 62 of file dnsmgr.c.