Asterisk - The Open Source Telephony Project  21.4.1
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 = "da6642af068ee5e6490c5b1d2cc1d238" , .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.

Function Documentation

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_dnsmgr_entry::changed.

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 }
unsigned int changed
Definition: dnsmgr.c:74
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 }
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
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_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_dnsmgr_entry::family, ast_dnsmgr_entry::result, and ast_dnsmgr_entry::service.

Referenced by ast_dnsmgr_get().

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 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
Definition: dnsmgr.c:66
unsigned int family
Definition: dnsmgr.c:72
struct ast_sockaddr * result
Definition: dnsmgr.c:68
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
Definition: search.h:40
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
char * service
Definition: dnsmgr.c:70
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.

Referenced by build_peer().

192 {
193  return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
194 }
char * service
Definition: dnsmgr.c:70
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 }
void * data
Definition: dnsmgr.c:76
char * service
Definition: dnsmgr.c:70
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.

Referenced by build_peer().

240 {
241  return dnsmgr_refresh(entry, 0);
242 }
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.

References ast_debug, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

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 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
void dnsmgr_start_refresh ( void  )

Provided by dnsmgr.c

Definition at line 302 of file dnsmgr.c.

References ast_sched_add_variable(), and AST_SCHED_DEL.

303 {
304  if (refresh_sched > -1) {
305  AST_SCHED_DEL(sched, refresh_sched);
306  refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
307  }
308 }
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:526
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46

Variable Documentation

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

Definition at line 101 of file dnsmgr.c.