dnsmgr.c File Reference

Background DNS update manager. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include <regex.h>
#include <signal.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

Defines

#define REFRESH_DEFAULT   300

Functions

static void __fini_entry_list (void)
static void __init_entry_list (void)
int ast_dnsmgr_changed (struct ast_dnsmgr_entry *entry)
 Check is see if a dnsmgr entry has changed.
struct ast_dnsmgr_entryast_dnsmgr_get (const char *name, struct ast_sockaddr *result, const char *service)
 Allocate a new DNS manager entry.
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.
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.
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.
int ast_dnsmgr_refresh (struct ast_dnsmgr_entry *entry)
 Force a refresh of a dnsmgr entry.
void ast_dnsmgr_release (struct ast_dnsmgr_entry *entry)
 Free a DNS manager entry.
int dnsmgr_init (void)
static int dnsmgr_refresh (struct ast_dnsmgr_entry *entry, int verbose)
int dnsmgr_reload (void)
static void dnsmgr_shutdown (void)
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 refresh_list (const void *data)

Variables

static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh")
static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration")
static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status")
static int enabled
static struct refresh_info master_refresh_info
static int refresh_interval
static ast_mutex_t refresh_lock = { PTHREAD_MUTEX_INITIALIZER , NULL, 1 }
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 <kpfleming@digium.com>
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.


Define Documentation

#define REFRESH_DEFAULT   300

Definition at line 90 of file dnsmgr.c.

Referenced by do_reload().


Function Documentation

static void __fini_entry_list ( void   )  [static]

Definition at line 86 of file dnsmgr.c.

00095 {

static void __init_entry_list ( void   )  [static]

Definition at line 86 of file dnsmgr.c.

00095 {

int ast_dnsmgr_changed ( struct ast_dnsmgr_entry entry  ) 

Check is see if a dnsmgr entry has changed.

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

Definition at line 248 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().

00249 {
00250    int changed;
00251 
00252    ast_mutex_lock(&entry->lock);
00253 
00254    changed = entry->changed;
00255    entry->changed = 0;
00256 
00257    ast_mutex_unlock(&entry->lock);
00258 
00259    return changed;
00260 }

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

Allocate a new DNS manager entry.

Parameters:
name the hostname
result where 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 132 of file dnsmgr.c.

References ast_dnsmgr_get_family().

00133 {
00134    return ast_dnsmgr_get_family(name, result, service, 0);
00135 }

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

Allocate a new DNS manager entry.

Parameters:
name the hostname
result where the DNS manager should store the IP address as it refreshes it.
service 
family Address 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 107 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, ast_dnsmgr_entry::name, NULL, ast_dnsmgr_entry::result, and ast_dnsmgr_entry::service.

Referenced by ast_dnsmgr_get(), and internal_dnsmgr_lookup().

00108 {
00109    struct ast_dnsmgr_entry *entry;
00110    int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
00111 
00112    if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, total_size))) {
00113       return NULL;
00114    }
00115 
00116    entry->result = result;
00117    ast_mutex_init(&entry->lock);
00118    strcpy(entry->name, name);
00119    if (service) {
00120       entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
00121       strcpy(entry->service, service);
00122    }
00123    entry->family = family;
00124 
00125    AST_RWLIST_WRLOCK(&entry_list);
00126    AST_RWLIST_INSERT_HEAD(&entry_list, entry, list);
00127    AST_RWLIST_UNLOCK(&entry_list);
00128 
00129    return entry;
00130 }

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:
name the hostname
result where 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.
dnsmgr Where 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:
0 success
non-zero failure
Version:
1.6.1 result changed from struct in_addr to struct aockaddr_in to store port number

Definition at line 192 of file dnsmgr.c.

References internal_dnsmgr_lookup(), and NULL.

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

00193 {
00194    return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
00195 }

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:
name the hostname
result The 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.
dnsmgr Where to store the allocate DNS manager entry
service 
func The 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
data A 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:
0 success
non-zero failure

Definition at line 197 of file dnsmgr.c.

References internal_dnsmgr_lookup().

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

00198 {
00199    return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
00200 }

int ast_dnsmgr_refresh ( struct ast_dnsmgr_entry entry  ) 

Force a refresh of a dnsmgr entry.

Return values:
non-zero if the result is different than the previous result
zero if the result is the same as the previous result

Definition at line 240 of file dnsmgr.c.

References dnsmgr_refresh().

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

00241 {
00242    return dnsmgr_refresh(entry, 0);
00243 }

void ast_dnsmgr_release ( struct ast_dnsmgr_entry entry  ) 

Free a DNS manager entry.

Parameters:
entry the DNS manager entry to free
Returns:
nothing

Definition at line 137 of file dnsmgr.c.

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

Referenced by cleanup_registration(), delete_users(), match_and_cleanup_peer_sched(), peer_destructor(), transport_destroy(), and unload_module().

00138 {
00139    if (!entry) {
00140       return;
00141    }
00142 
00143    AST_RWLIST_WRLOCK(&entry_list);
00144    AST_RWLIST_REMOVE(&entry_list, entry, list);
00145    AST_RWLIST_UNLOCK(&entry_list);
00146    ast_debug(6, "removing dns manager for '%s'\n", entry->name);
00147 
00148    ast_mutex_destroy(&entry->lock);
00149    ast_free(entry);
00150 }

int dnsmgr_init ( void   ) 

Provided by dnsmgr.c

Definition at line 431 of file dnsmgr.c.

References ast_cli_register(), ast_log, ast_register_cleanup(), ast_sched_context_create(), cli_refresh, cli_reload, cli_status, dnsmgr_shutdown(), do_reload(), and LOG_ERROR.

Referenced by main().

00432 {
00433    if (!(sched = ast_sched_context_create())) {
00434       ast_log(LOG_ERROR, "Unable to create schedule context.\n");
00435       return -1;
00436    }
00437    ast_cli_register(&cli_reload);
00438    ast_cli_register(&cli_status);
00439    ast_cli_register(&cli_refresh);
00440 
00441    ast_register_cleanup(dnsmgr_shutdown);
00442 
00443    return do_reload(1);
00444 }

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

Definition at line 205 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::name, 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().

00206 {
00207    struct ast_sockaddr tmp = { .len = 0, };
00208    int changed = 0;
00209 
00210    ast_mutex_lock(&entry->lock);
00211 
00212    ast_debug(6, "refreshing '%s'\n", entry->name);
00213 
00214    tmp.ss.ss_family = entry->family;
00215    if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
00216       if (!ast_sockaddr_port(&tmp)) {
00217          ast_sockaddr_set_port(&tmp, ast_sockaddr_port(entry->result));
00218       }
00219       if (ast_sockaddr_cmp(&tmp, entry->result)) {
00220          const char *old_addr = ast_strdupa(ast_sockaddr_stringify(entry->result));
00221          const char *new_addr = ast_strdupa(ast_sockaddr_stringify(&tmp));
00222 
00223          if (entry->update_func) {
00224             entry->update_func(entry->result, &tmp, entry->data);
00225          } else {
00226             ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
00227                   entry->name, old_addr, new_addr);
00228 
00229             ast_sockaddr_copy(entry->result, &tmp);
00230             changed = entry->changed = 1;
00231          }
00232       }
00233    }
00234 
00235    ast_mutex_unlock(&entry->lock);
00236 
00237    return changed;
00238 }

int dnsmgr_reload ( void   ) 

Provided by dnsmgr.c

Definition at line 446 of file dnsmgr.c.

References do_reload().

00447 {
00448    return do_reload(0);
00449 }

static void dnsmgr_shutdown ( void   )  [static]

Definition at line 411 of file dnsmgr.c.

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

Referenced by dnsmgr_init().

00412 {
00413    ast_cli_unregister(&cli_reload);
00414    ast_cli_unregister(&cli_status);
00415    ast_cli_unregister(&cli_refresh);
00416 
00417    /* Destroy refresh thread. */
00418    ast_mutex_lock(&refresh_lock);
00419    if (refresh_thread != AST_PTHREADT_NULL) {
00420       /* wake up the thread so it will exit */
00421       pthread_cancel(refresh_thread);
00422       pthread_kill(refresh_thread, SIGURG);
00423       pthread_join(refresh_thread, NULL);
00424       refresh_thread = AST_PTHREADT_NULL;
00425    }
00426    ast_mutex_unlock(&refresh_lock);
00427 
00428    ast_sched_context_destroy(sched);
00429 }

void dnsmgr_start_refresh ( void   ) 

Provided by dnsmgr.c

Definition at line 303 of file dnsmgr.c.

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

Referenced by main().

00304 {
00305    if (refresh_sched > -1) {
00306       AST_SCHED_DEL(sched, refresh_sched);
00307       refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00308    }
00309 }

static void* do_refresh ( void *  data  )  [static]

Definition at line 262 of file dnsmgr.c.

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

Referenced by do_reload(), and load_module().

00263 {
00264    for (;;) {
00265       pthread_testcancel();
00266       usleep((ast_sched_wait(sched)*1000));
00267       pthread_testcancel();
00268       ast_sched_runq(sched);
00269    }
00270    return NULL;
00271 }

static int do_reload ( int  loading  )  [static]

Definition at line 451 of file dnsmgr.c.

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(), and ast_variable::value.

Referenced by dnsmgr_init(), dnsmgr_reload(), and handle_cli_reload().

00452 {
00453    struct ast_config *config;
00454    struct ast_variable *v;
00455    struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
00456    int interval;
00457    int was_enabled;
00458 
00459    if ((config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
00460       return 0;
00461    }
00462 
00463    /* ensure that no refresh cycles run while the reload is in progress */
00464    ast_mutex_lock(&refresh_lock);
00465 
00466    /* reset defaults in preparation for reading config file */
00467    refresh_interval = REFRESH_DEFAULT;
00468    was_enabled = enabled;
00469    enabled = 0;
00470 
00471    if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
00472       ast_mutex_unlock(&refresh_lock);
00473       return 0;
00474    }
00475 
00476    AST_SCHED_DEL(sched, refresh_sched);
00477 
00478    for (v = ast_variable_browse(config, "general"); v; v = v->next) {
00479       if (!strcasecmp(v->name, "enable")) {
00480          enabled = ast_true(v->value);
00481       } else if (!strcasecmp(v->name, "refreshinterval")) {
00482          if (sscanf(v->value, "%30d", &interval) < 1) {
00483             ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
00484          } else if (interval < 0) {
00485             ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
00486          } else {
00487             refresh_interval = interval;
00488          }
00489       }
00490    }
00491    ast_config_destroy(config);
00492 
00493    if (enabled && refresh_interval) {
00494       ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
00495    }
00496 
00497    /* if this reload enabled the manager, create the background thread
00498       if it does not exist */
00499    if (enabled) {
00500       if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
00501          if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
00502             ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
00503          }
00504       }
00505       /* make a background refresh happen right away */
00506       refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00507    /* if this reload disabled the manager and there is a background thread, kill it */
00508    } else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
00509       /* wake up the thread so it will exit */
00510       pthread_cancel(refresh_thread);
00511       pthread_kill(refresh_thread, SIGURG);
00512       pthread_join(refresh_thread, NULL);
00513       refresh_thread = AST_PTHREADT_NULL;
00514    }
00515 
00516    ast_mutex_unlock(&refresh_lock);
00517 
00518    return 0;
00519 }

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

Definition at line 333 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, ast_cli_args::fd, refresh_info::filter, NULL, refresh_list(), refresh_info::regex_present, and ast_cli_entry::usage.

00334 {
00335    struct refresh_info info = {
00336       .entries = &entry_list,
00337       .verbose = 1,
00338    };
00339    switch (cmd) {
00340    case CLI_INIT:
00341       e->command = "dnsmgr refresh";
00342       e->usage =
00343          "Usage: dnsmgr refresh [pattern]\n"
00344          "       Peforms an immediate refresh of the managed DNS entries.\n"
00345          "       Optional regular expression pattern is used to filter the entries to refresh.\n";
00346       return NULL;
00347    case CLI_GENERATE:
00348       return NULL;
00349    }
00350 
00351    if (!enabled) {
00352       ast_cli(a->fd, "DNS Manager is disabled.\n");
00353       return 0;
00354    }
00355 
00356    if (a->argc > 3) {
00357       return CLI_SHOWUSAGE;
00358    }
00359 
00360    if (a->argc == 3) {
00361       if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) {
00362          return CLI_SHOWUSAGE;
00363       } else {
00364          info.regex_present = 1;
00365       }
00366    }
00367 
00368    refresh_list(&info);
00369 
00370    if (info.regex_present) {
00371       regfree(&info.filter);
00372    }
00373 
00374    return CLI_SUCCESS;
00375 }

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

Definition at line 313 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.

00314 {
00315    switch (cmd) {
00316    case CLI_INIT:
00317       e->command = "dnsmgr reload";
00318       e->usage =
00319          "Usage: dnsmgr reload\n"
00320          "       Reloads the DNS manager configuration.\n";
00321       return NULL;
00322    case CLI_GENERATE:
00323       return NULL;
00324    }
00325    if (a->argc > 2) {
00326       return CLI_SHOWUSAGE;
00327    }
00328 
00329    do_reload(0);
00330    return CLI_SUCCESS;
00331 }

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

Definition at line 377 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.

00378 {
00379    int count = 0;
00380    struct ast_dnsmgr_entry *entry;
00381    switch (cmd) {
00382    case CLI_INIT:
00383       e->command = "dnsmgr status";
00384       e->usage =
00385          "Usage: dnsmgr status\n"
00386          "       Displays the DNS manager status.\n";
00387       return NULL;
00388    case CLI_GENERATE:
00389       return NULL;
00390    }
00391 
00392    if (a->argc > 2) {
00393       return CLI_SHOWUSAGE;
00394    }
00395 
00396    ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
00397    ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
00398    AST_RWLIST_RDLOCK(&entry_list);
00399    AST_RWLIST_TRAVERSE(&entry_list, entry, list)
00400       count++;
00401    AST_RWLIST_UNLOCK(&entry_list);
00402    ast_cli(a->fd, "Number of entries: %d\n", count);
00403 
00404    return CLI_SUCCESS;
00405 }

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 152 of file dnsmgr.c.

References ast_debug, ast_dnsmgr_get_family(), ast_get_ip_or_srv(), ast_sockaddr_parse(), ast_strlen_zero, enabled, ast_dnsmgr_entry::family, PARSE_PORT_FORBID, and ast_sockaddr::ss.

Referenced by ast_dnsmgr_lookup(), and ast_dnsmgr_lookup_cb().

00153 {
00154    unsigned int family;
00155 
00156    if (ast_strlen_zero(name) || !result || !dnsmgr) {
00157       return -1;
00158    }
00159 
00160    if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) {
00161       return 0;
00162    }
00163 
00164    /* Lookup address family filter. */
00165    family = result->ss.ss_family;
00166 
00167    /*
00168     * If it's actually an IP address and not a name, there's no
00169     * need for a managed lookup.
00170     */
00171    if (ast_sockaddr_parse(result, name, PARSE_PORT_FORBID)) {
00172       return 0;
00173    }
00174 
00175    ast_debug(6, "doing dnsmgr_lookup for '%s'\n", name);
00176 
00177    /* do a lookup now but add a manager so it will automagically get updated in the background */
00178    ast_get_ip_or_srv(result, name, service);
00179 
00180    /* if dnsmgr is not enable don't bother adding an entry */
00181    if (!enabled) {
00182       return 0;
00183    }
00184 
00185    ast_debug(6, "adding dns manager for '%s'\n", name);
00186    *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
00187    (*dnsmgr)->update_func = func;
00188    (*dnsmgr)->data = data;
00189    return !*dnsmgr;
00190 }

static int refresh_list ( const void *  data  )  [static]

Definition at line 273 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, LOG_WARNING, ast_dnsmgr_entry::name, NULL, refresh_interval, refresh_info::regex_present, and refresh_info::verbose.

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

00274 {
00275    struct refresh_info *info = (struct refresh_info *)data;
00276    struct ast_dnsmgr_entry *entry;
00277 
00278    /* if a refresh or reload is already in progress, exit now */
00279    if (ast_mutex_trylock(&refresh_lock)) {
00280       if (info->verbose) {
00281          ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
00282       }
00283       return -1;
00284    }
00285 
00286    ast_debug(6, "Refreshing DNS lookups.\n");
00287    AST_RWLIST_RDLOCK(info->entries);
00288    AST_RWLIST_TRAVERSE(info->entries, entry, list) {
00289       if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
00290          continue;
00291       }
00292 
00293       dnsmgr_refresh(entry, info->verbose);
00294    }
00295    AST_RWLIST_UNLOCK(info->entries);
00296 
00297    ast_mutex_unlock(&refresh_lock);
00298 
00299    /* automatically reschedule based on the interval */
00300    return refresh_interval * 1000;
00301 }


Variable Documentation

struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh") [static]

Definition at line 408 of file dnsmgr.c.

Referenced by dnsmgr_init(), and dnsmgr_shutdown().

struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration") [static]

Definition at line 407 of file dnsmgr.c.

Referenced by dnsmgr_init(), and dnsmgr_shutdown().

struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status") [static]

Definition at line 409 of file dnsmgr.c.

int enabled [static]

Initial value:

 {
   .entries = &entry_list,
   .verbose = 0,
}

Definition at line 102 of file dnsmgr.c.

Referenced by dnsmgr_start_refresh(), and do_reload().

int refresh_interval [static]

Definition at line 93 of file dnsmgr.c.

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

ast_mutex_t refresh_lock = { PTHREAD_MUTEX_INITIALIZER , NULL, 1 } [static]

Definition at line 88 of file dnsmgr.c.

int refresh_sched = -1 [static]

Definition at line 64 of file dnsmgr.c.

pthread_t refresh_thread = AST_PTHREADT_NULL [static]

Definition at line 65 of file dnsmgr.c.

struct ast_sched_context* sched [static]

Definition at line 63 of file dnsmgr.c.


Generated on Thu Apr 16 06:30:45 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6