Asterisk - The Open Source Telephony Project  GIT-master-9ed6387
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"

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.

Referenced by do_reload().

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.

References ast_mutex_lock, ast_mutex_unlock, ast_dnsmgr_entry::changed, and ast_dnsmgr_entry::lock.

Referenced by iax2_do_register().

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 }
ast_mutex_t lock
Definition: dnsmgr.c:79
#define ast_mutex_lock(a)
Definition: lock.h:187
unsigned int changed
Definition: dnsmgr.c:74
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ 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.

References ast_dnsmgr_get_family().

132 {
133  return ast_dnsmgr_get_family(name, result, service, 0);
134 }
enum ast_cc_service_type service
Definition: chan_sip.c:946
static const char name[]
Definition: cdr_mysql.c:74
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

◆ 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.

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

Referenced by ast_dnsmgr_get(), and internal_dnsmgr_lookup().

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 
125  AST_RWLIST_INSERT_HEAD(&entry_list, entry, list);
127 
128  return entry;
129 }
ast_mutex_t lock
Definition: dnsmgr.c:79
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
enum ast_cc_service_type service
Definition: chan_sip.c:946
Definition: dnsmgr.c:66
#define NULL
Definition: resample.c:96
unsigned int family
Definition: dnsmgr.c:72
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:717
struct ast_sockaddr * result
Definition: dnsmgr.c:68
#define ast_strlen_zero(a)
Definition: muted.c:73
static const char name[]
Definition: cdr_mysql.c:74
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static PGresult * result
Definition: cel_pgsql.c:88
Definition: search.h:40
#define ast_mutex_init(pmutex)
Definition: lock.h:184
char * service
Definition: dnsmgr.c:70

◆ 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.

References internal_dnsmgr_lookup(), and NULL.

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

192 {
193  return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
194 }
enum ast_cc_service_type service
Definition: chan_sip.c:946
#define NULL
Definition: resample.c:96
static const char name[]
Definition: cdr_mysql.c:74
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

◆ 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.

References internal_dnsmgr_lookup().

Referenced by __sip_subscribe_mwi_do(), build_peer(), and transmit_register().

197 {
198  return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
199 }
enum ast_cc_service_type service
Definition: chan_sip.c:946
void * data
Definition: dnsmgr.c:76
static const char name[]
Definition: cdr_mysql.c:74
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

◆ 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.

References dnsmgr_refresh().

Referenced by build_peer(), iax2_do_register(), and sip_reg_timeout().

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

◆ ast_dnsmgr_release()

void ast_dnsmgr_release ( struct ast_dnsmgr_entry entry)

Free a DNS manager entry.

Parameters
entrythe DNS manager entry to free
Returns
nothing

Definition at line 136 of file dnsmgr.c.

References ast_debug, ast_free, ast_mutex_destroy, AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and ast_dnsmgr_entry::lock.

Referenced by __cleanup_registration(), __shutdown_mwi_subscription(), delete_users(), destroy_sip_transport_state(), match_and_cleanup_peer_sched(), and peer_destructor().

137 {
138  if (!entry) {
139  return;
140  }
141 
143  AST_RWLIST_REMOVE(&entry_list, entry, list);
145  ast_debug(6, "removing dns manager for '%s'\n", entry->name);
146 
147  ast_mutex_destroy(&entry->lock);
148  ast_free(entry);
149 }
ast_mutex_t lock
Definition: dnsmgr.c:79
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_free(a)
Definition: astmm.h:182
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:884
#define ast_mutex_destroy(a)
Definition: lock.h:186

◆ 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.

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, ast_dnsmgr_entry::changed, ast_dnsmgr_entry::data, ast_dnsmgr_entry::family, ast_sockaddr::len, ast_dnsmgr_entry::lock, LOG_NOTICE, ast_dnsmgr_entry::result, ast_dnsmgr_entry::service, ast_sockaddr::ss, and ast_dnsmgr_entry::update_func.

Referenced by ast_dnsmgr_refresh(), and refresh_list().

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 }
struct sockaddr_storage ss
Definition: netsock2.h:98
ast_mutex_t lock
Definition: dnsmgr.c:79
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:171
static int tmp()
Definition: bt_open.c:389
#define ast_mutex_lock(a)
Definition: lock.h:187
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
unsigned int family
Definition: dnsmgr.c:72
Socket address structure.
Definition: netsock2.h:97
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:521
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
dns_update_func update_func
Definition: dnsmgr.c:78
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
struct ast_sockaddr * result
Definition: dnsmgr.c:68
void * data
Definition: dnsmgr.c:76
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:537
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:260
#define LOG_NOTICE
Definition: logger.h:263
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:897
unsigned int changed
Definition: dnsmgr.c:74
#define ast_mutex_unlock(a)
Definition: lock.h:188
char * service
Definition: dnsmgr.c:70

◆ dnsmgr_start_refresh()

void dnsmgr_start_refresh ( void  )

Provided by dnsmgr.c

Definition at line 302 of file dnsmgr.c.

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

Referenced by asterisk_daemon().

303 {
304  if (refresh_sched > -1) {
307  }
308 }
static struct refresh_info master_refresh_info
Definition: dnsmgr.c:101
Definition: sched.c:76
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:524
static int refresh_sched
Definition: dnsmgr.c:63
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
static int refresh_list(const void *data)
Definition: dnsmgr.c:272

◆ do_refresh()

static void* do_refresh ( void *  data)
static

Definition at line 261 of file dnsmgr.c.

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

Referenced by do_reload().

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:755
Definition: sched.c:76
#define NULL
Definition: resample.c:96
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:431

◆ do_reload()

static int do_reload ( int  loading)
static

Definition at line 450 of file dnsmgr.c.

References ast_config_destroy(), ast_config_load2(), ast_log, AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_CORE, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, 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(), ASTERISK_GPL_KEY, config, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, do_refresh(), enabled, load_module(), LOG_ERROR, LOG_NOTICE, LOG_WARNING, ast_variable::name, ast_variable::next, NULL, REFRESH_DEFAULT, refresh_interval, refresh_list(), refresh_lock, refresh_sched, refresh_thread, reload(), reload_module(), unload_module(), and ast_variable::value.

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

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 
470  if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
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  }
490  ast_config_destroy(config);
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 }
struct ast_variable * next
static struct refresh_info master_refresh_info
Definition: dnsmgr.c:101
char * config
Definition: conf2ael.c:66
static int refresh_interval
Definition: dnsmgr.c:92
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
#define LOG_WARNING
Definition: logger.h:274
#define CONFIG_STATUS_FILEINVALID
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:3154
Structure for variables, used for configurations and for channel variables.
Definition: sched.c:76
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:524
#define REFRESH_DEFAULT
Definition: dnsmgr.c:89
static pthread_t refresh_thread
Definition: dnsmgr.c:64
static int refresh_sched
Definition: dnsmgr.c:63
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:507
#define ast_log
Definition: astobj2.c:42
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
#define AST_PTHREADT_NULL
Definition: lock.h:66
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define CONFIG_STATUS_FILEUNCHANGED
static ast_mutex_t refresh_lock
Definition: dnsmgr.c:87
#define LOG_ERROR
Definition: logger.h:285
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1822
static int refresh_list(const void *data)
Definition: dnsmgr.c:272
#define LOG_NOTICE
Definition: logger.h:263
Structure used to handle boolean flags.
Definition: utils.h:199
#define CONFIG_STATUS_FILEMISSING
static void * do_refresh(void *data)
Definition: dnsmgr.c:261
#define ast_mutex_unlock(a)
Definition: lock.h:188
static int enabled
Definition: dnsmgr.c:91

◆ 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.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, enabled, refresh_info::entries, entry_list, ast_cli_args::fd, refresh_info::filter, NULL, refresh_list(), refresh_info::regex_present, and ast_cli_entry::usage.

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  " Peforms 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 
367  refresh_list(&info);
368 
369  if (info.regex_present) {
370  regfree(&info.filter);
371  }
372 
373  return CLI_SUCCESS;
374 }
regex_t filter
Definition: dnsmgr.c:98
const int argc
Definition: cli.h:160
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
const int fd
Definition: cli.h:159
const char *const * argv
Definition: cli.h:161
unsigned int regex_present
Definition: dnsmgr.c:97
#define CLI_SHOWUSAGE
Definition: cli.h:45
def info(msg)
static int refresh_list(const void *data)
Definition: dnsmgr.c:272
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
static struct entry_list entry_list
static int enabled
Definition: dnsmgr.c:91

◆ 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.

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

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 }
const int argc
Definition: cli.h:160
Definition: cli.h:152
#define NULL
Definition: resample.c:96
static int do_reload(int loading)
Definition: dnsmgr.c:450
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44

◆ 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.

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

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);
398  AST_RWLIST_TRAVERSE(&entry_list, entry, list)
399  count++;
401  ast_cli(a->fd, "Number of entries: %d\n", count);
402 
403  return CLI_SUCCESS;
404 }
static int refresh_interval
Definition: dnsmgr.c:92
const int argc
Definition: cli.h:160
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: cli.h:152
Definition: dnsmgr.c:66
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
const int fd
Definition: cli.h:159
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
Definition: search.h:40
static int enabled
Definition: dnsmgr.c:91

◆ 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.

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, PARSE_PORT_FORBID, and ast_sockaddr::ss.

Referenced by ast_dnsmgr_lookup(), and ast_dnsmgr_lookup_cb().

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  */
170  if (ast_sockaddr_parse(result, name, PARSE_PORT_FORBID)) {
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 */
177  ast_get_ip_or_srv(result, name, service);
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 }
struct sockaddr_storage ss
Definition: netsock2.h:98
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
enum ast_cc_service_type service
Definition: chan_sip.c:946
unsigned int family
Definition: dnsmgr.c:72
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
void * data
Definition: dnsmgr.c:76
#define ast_strlen_zero(a)
Definition: muted.c:73
static const char name[]
Definition: cdr_mysql.c:74
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:897
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 int enabled
Definition: dnsmgr.c:91

◆ load_module()

static int load_module ( void  )
static

Definition at line 432 of file dnsmgr.c.

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

Referenced by do_reload().

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

◆ refresh_list()

static int refresh_list ( const void *  data)
static

Definition at line 272 of file dnsmgr.c.

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

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

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 }
regex_t filter
Definition: dnsmgr.c:98
static int refresh_interval
Definition: dnsmgr.c:92
struct entry_list * entries
Definition: dnsmgr.c:95
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: dnsmgr.c:66
#define NULL
Definition: resample.c:96
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
#define ast_mutex_trylock(a)
Definition: lock.h:189
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
static ast_mutex_t refresh_lock
Definition: dnsmgr.c:87
unsigned int regex_present
Definition: dnsmgr.c:97
def info(msg)
static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
Definition: dnsmgr.c:204
int verbose
Definition: dnsmgr.c:96
Definition: search.h:40
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ reload_module()

static int reload_module ( void  )
static

Definition at line 445 of file dnsmgr.c.

References do_reload().

Referenced by do_reload().

446 {
447  return do_reload(0);
448 }
static int do_reload(int loading)
Definition: dnsmgr.c:450

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 410 of file dnsmgr.c.

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

Referenced by do_reload().

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:2348
static struct ast_cli_entry cli_reload
Definition: dnsmgr.c:406
Definition: sched.c:76
static pthread_t refresh_thread
Definition: dnsmgr.c:64
#define ast_mutex_lock(a)
Definition: lock.h:187
static struct ast_cli_entry cli_refresh
Definition: dnsmgr.c:407
#define NULL
Definition: resample.c:96
#define AST_PTHREADT_NULL
Definition: lock.h:66
static struct ast_cli_entry cli_status
Definition: dnsmgr.c:408
static ast_mutex_t refresh_lock
Definition: dnsmgr.c:87
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:269
#define ast_mutex_unlock(a)
Definition: lock.h:188

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.

◆ 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.

◆ 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.

◆ 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,
}
static struct entry_list entry_list

Definition at line 101 of file dnsmgr.c.

◆ 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.