Sat Mar 10 01:54:16 2012

Asterisk developer's documentation


dnsmgr.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005-2006, Kevin P. Fleming
00005  *
00006  * Kevin P. Fleming <kpfleming@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Background DNS update manager
00022  *
00023  * \author Kevin P. Fleming <kpfleming@digium.com> 
00024  *
00025  * \bug There is a minor race condition.  In the event that an IP address
00026  * of a dnsmgr managed host changes, there is the potential for the consumer
00027  * of that address to access the in_addr data at the same time that the dnsmgr
00028  * thread is in the middle of updating it to the new address.
00029  */
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 354884 $")
00034 
00035 #include "asterisk/_private.h"
00036 #include <regex.h>
00037 #include <signal.h>
00038 
00039 #include "asterisk/dnsmgr.h"
00040 #include "asterisk/linkedlists.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/config.h"
00043 #include "asterisk/sched.h"
00044 #include "asterisk/cli.h"
00045 #include "asterisk/manager.h"
00046 #include "asterisk/acl.h"
00047 
00048 static struct sched_context *sched;
00049 static int refresh_sched = -1;
00050 static pthread_t refresh_thread = AST_PTHREADT_NULL;
00051 
00052 struct ast_dnsmgr_entry {
00053    /*! where we will store the resulting IP address and port number */
00054    struct ast_sockaddr *result;
00055    /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */
00056    char *service;
00057    /*! Address family to filter DNS responses. */
00058    unsigned int family;
00059    /*! Set to 1 if the entry changes */
00060    unsigned int changed:1;
00061    /*! Data to pass back to update_func */
00062    void *data;
00063    /*! The callback function to execute on address update */
00064    dns_update_func update_func;
00065    ast_mutex_t lock;
00066    AST_RWLIST_ENTRY(ast_dnsmgr_entry) list;
00067    /*! just 1 here, but we use calloc to allocate the correct size */
00068    char name[1];
00069 };
00070 
00071 static AST_RWLIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry);
00072 
00073 AST_MUTEX_DEFINE_STATIC(refresh_lock);
00074 
00075 #define REFRESH_DEFAULT 300
00076 
00077 static int enabled;
00078 static int refresh_interval;
00079 
00080 struct refresh_info {
00081    struct entry_list *entries;
00082    int verbose;
00083    unsigned int regex_present:1;
00084    regex_t filter;
00085 };
00086 
00087 static struct refresh_info master_refresh_info = {
00088    .entries = &entry_list,
00089    .verbose = 0,
00090 };
00091 
00092 struct ast_dnsmgr_entry *ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
00093 {
00094    struct ast_dnsmgr_entry *entry;
00095    int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
00096 
00097    if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, total_size))) {
00098       return NULL;
00099    }
00100 
00101    entry->result = result;
00102    ast_mutex_init(&entry->lock);
00103    strcpy(entry->name, name);
00104    if (service) {
00105       entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
00106       strcpy(entry->service, service);
00107    }
00108    entry->family = family;
00109 
00110    AST_RWLIST_WRLOCK(&entry_list);
00111    AST_RWLIST_INSERT_HEAD(&entry_list, entry, list);
00112    AST_RWLIST_UNLOCK(&entry_list);
00113 
00114    return entry;
00115 }
00116 
00117 struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
00118 {
00119    return ast_dnsmgr_get_family(name, result, service, 0);
00120 }
00121 
00122 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
00123 {
00124    if (!entry)
00125       return;
00126 
00127    AST_RWLIST_WRLOCK(&entry_list);
00128    AST_RWLIST_REMOVE(&entry_list, entry, list);
00129    AST_RWLIST_UNLOCK(&entry_list);
00130    ast_verb(4, "removing dns manager for '%s'\n", entry->name);
00131 
00132    ast_mutex_destroy(&entry->lock);
00133    ast_free(entry);
00134 }
00135 
00136 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)
00137 {
00138    unsigned int family;
00139 
00140    if (ast_strlen_zero(name) || !result || !dnsmgr) {
00141       return -1;
00142    }
00143 
00144    if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) {
00145       return 0;
00146    }
00147 
00148    /* Lookup address family filter. */
00149    family = result->ss.ss_family;
00150 
00151    /*
00152     * If it's actually an IP address and not a name, there's no
00153     * need for a managed lookup.
00154     */
00155    if (ast_sockaddr_parse(result, name, PARSE_PORT_FORBID)) {
00156       return 0;
00157    }
00158 
00159    ast_verb(4, "doing dnsmgr_lookup for '%s'\n", name);
00160 
00161    /* do a lookup now but add a manager so it will automagically get updated in the background */
00162    ast_get_ip_or_srv(result, name, service);
00163    
00164    /* if dnsmgr is not enable don't bother adding an entry */
00165    if (!enabled) {
00166       return 0;
00167    }
00168    
00169    ast_verb(3, "adding dns manager for '%s'\n", name);
00170    *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
00171    (*dnsmgr)->update_func = func;
00172    (*dnsmgr)->data = data;
00173    return !*dnsmgr;
00174 }
00175 
00176 int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
00177 {
00178    return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
00179 }
00180 
00181 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)
00182 {
00183    return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
00184 }
00185 
00186 /*
00187  * Refresh a dnsmgr entry
00188  */
00189 static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
00190 {
00191    struct ast_sockaddr tmp = { .len = 0, };
00192    int changed = 0;
00193 
00194    ast_mutex_lock(&entry->lock);
00195 
00196    if (verbose) {
00197       ast_verb(3, "refreshing '%s'\n", entry->name);
00198    }
00199 
00200    tmp.ss.ss_family = entry->family;
00201    if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
00202       if (!ast_sockaddr_port(&tmp)) {
00203          ast_sockaddr_set_port(&tmp, ast_sockaddr_port(entry->result));
00204       }
00205       if (ast_sockaddr_cmp(&tmp, entry->result)) {
00206          const char *old_addr = ast_strdupa(ast_sockaddr_stringify(entry->result));
00207          const char *new_addr = ast_strdupa(ast_sockaddr_stringify(&tmp));
00208 
00209          if (entry->update_func) {
00210             entry->update_func(entry->result, &tmp, entry->data);
00211          } else {
00212             ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
00213                   entry->name, old_addr, new_addr);
00214 
00215             ast_sockaddr_copy(entry->result, &tmp);
00216             changed = entry->changed = 1;
00217          }
00218       }
00219    }
00220 
00221    ast_mutex_unlock(&entry->lock);
00222 
00223    return changed;
00224 }
00225 
00226 int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
00227 {
00228    return dnsmgr_refresh(entry, 0);
00229 }
00230 
00231 /*
00232  * Check if dnsmgr entry has changed from since last call to this function
00233  */
00234 int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry) 
00235 {
00236    int changed;
00237 
00238    ast_mutex_lock(&entry->lock);
00239 
00240    changed = entry->changed;
00241    entry->changed = 0;
00242 
00243    ast_mutex_unlock(&entry->lock);
00244    
00245    return changed;
00246 }
00247 
00248 static void *do_refresh(void *data)
00249 {
00250    for (;;) {
00251       pthread_testcancel();
00252       usleep((ast_sched_wait(sched)*1000));
00253       pthread_testcancel();
00254       ast_sched_runq(sched);
00255    }
00256    return NULL;
00257 }
00258 
00259 static int refresh_list(const void *data)
00260 {
00261    struct refresh_info *info = (struct refresh_info *)data;
00262    struct ast_dnsmgr_entry *entry;
00263 
00264    /* if a refresh or reload is already in progress, exit now */
00265    if (ast_mutex_trylock(&refresh_lock)) {
00266       if (info->verbose)
00267          ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
00268       return -1;
00269    }
00270 
00271    ast_verb(3, "Refreshing DNS lookups.\n");
00272    AST_RWLIST_RDLOCK(info->entries);
00273    AST_RWLIST_TRAVERSE(info->entries, entry, list) {
00274       if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0))
00275           continue;
00276 
00277       dnsmgr_refresh(entry, info->verbose);
00278    }
00279    AST_RWLIST_UNLOCK(info->entries);
00280 
00281    ast_mutex_unlock(&refresh_lock);
00282 
00283    /* automatically reschedule based on the interval */
00284    return refresh_interval * 1000;
00285 }
00286 
00287 void dnsmgr_start_refresh(void)
00288 {
00289    if (refresh_sched > -1) {
00290       AST_SCHED_DEL(sched, refresh_sched);
00291       refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00292    }
00293 }
00294 
00295 static int do_reload(int loading);
00296 
00297 static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00298 {
00299    switch (cmd) {
00300    case CLI_INIT:
00301       e->command = "dnsmgr reload";
00302       e->usage = 
00303          "Usage: dnsmgr reload\n"
00304          "       Reloads the DNS manager configuration.\n";
00305       return NULL;
00306    case CLI_GENERATE:
00307       return NULL;   
00308    }
00309    if (a->argc > 2)
00310       return CLI_SHOWUSAGE;
00311 
00312    do_reload(0);
00313    return CLI_SUCCESS;
00314 }
00315 
00316 static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00317 {
00318    struct refresh_info info = {
00319       .entries = &entry_list,
00320       .verbose = 1,
00321    };
00322    switch (cmd) {
00323    case CLI_INIT:
00324       e->command = "dnsmgr refresh";
00325       e->usage = 
00326          "Usage: dnsmgr refresh [pattern]\n"
00327          "       Peforms an immediate refresh of the managed DNS entries.\n"
00328          "       Optional regular expression pattern is used to filter the entries to refresh.\n";
00329       return NULL;
00330    case CLI_GENERATE:
00331       return NULL;   
00332    }
00333 
00334    if (!enabled) {
00335       ast_cli(a->fd, "DNS Manager is disabled.\n");
00336       return 0;
00337    }
00338 
00339    if (a->argc > 3) {
00340       return CLI_SHOWUSAGE;
00341    }
00342 
00343    if (a->argc == 3) {
00344       if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) {
00345          return CLI_SHOWUSAGE;
00346       } else {
00347          info.regex_present = 1;
00348       }
00349    }
00350 
00351    refresh_list(&info);
00352 
00353    if (info.regex_present) {
00354       regfree(&info.filter);
00355    }
00356 
00357    return CLI_SUCCESS;
00358 }
00359 
00360 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00361 {
00362    int count = 0;
00363    struct ast_dnsmgr_entry *entry;
00364    switch (cmd) {
00365    case CLI_INIT:
00366       e->command = "dnsmgr status";
00367       e->usage = 
00368          "Usage: dnsmgr status\n"
00369          "       Displays the DNS manager status.\n";
00370       return NULL;
00371    case CLI_GENERATE:
00372       return NULL;   
00373    }
00374 
00375    if (a->argc > 2)
00376       return CLI_SHOWUSAGE;
00377 
00378    ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
00379    ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
00380    AST_RWLIST_RDLOCK(&entry_list);
00381    AST_RWLIST_TRAVERSE(&entry_list, entry, list)
00382       count++;
00383    AST_RWLIST_UNLOCK(&entry_list);
00384    ast_cli(a->fd, "Number of entries: %d\n", count);
00385 
00386    return CLI_SUCCESS;
00387 }
00388 
00389 static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration");
00390 static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh");
00391 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status");
00392 
00393 int dnsmgr_init(void)
00394 {
00395    if (!(sched = sched_context_create())) {
00396       ast_log(LOG_ERROR, "Unable to create schedule context.\n");
00397       return -1;
00398    }
00399    ast_cli_register(&cli_reload);
00400    ast_cli_register(&cli_status);
00401    ast_cli_register(&cli_refresh);
00402    return do_reload(1);
00403 }
00404 
00405 int dnsmgr_reload(void)
00406 {
00407    return do_reload(0);
00408 }
00409 
00410 static int do_reload(int loading)
00411 {
00412    struct ast_config *config;
00413    struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
00414    const char *interval_value;
00415    const char *enabled_value;
00416    int interval;
00417    int was_enabled;
00418    int res = -1;
00419 
00420    config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags);
00421    if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
00422       return 0;
00423    }
00424 
00425    /* ensure that no refresh cycles run while the reload is in progress */
00426    ast_mutex_lock(&refresh_lock);
00427 
00428    /* reset defaults in preparation for reading config file */
00429    refresh_interval = REFRESH_DEFAULT;
00430    was_enabled = enabled;
00431    enabled = 0;
00432 
00433    AST_SCHED_DEL(sched, refresh_sched);
00434 
00435    if (config) {
00436       if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
00437          enabled = ast_true(enabled_value);
00438       }
00439       if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) {
00440          if (sscanf(interval_value, "%30d", &interval) < 1)
00441             ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value);
00442          else if (interval < 0)
00443             ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
00444          else
00445             refresh_interval = interval;
00446       }
00447       ast_config_destroy(config);
00448    }
00449 
00450    if (enabled && refresh_interval)
00451       ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
00452 
00453    /* if this reload enabled the manager, create the background thread
00454       if it does not exist */
00455    if (enabled) {
00456       if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
00457          if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
00458             ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
00459          }
00460       }
00461       /* make a background refresh happen right away */
00462       refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00463       res = 0;
00464    }
00465    /* if this reload disabled the manager and there is a background thread,
00466       kill it */
00467    else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
00468       /* wake up the thread so it will exit */
00469       pthread_cancel(refresh_thread);
00470       pthread_kill(refresh_thread, SIGURG);
00471       pthread_join(refresh_thread, NULL);
00472       refresh_thread = AST_PTHREADT_NULL;
00473       res = 0;
00474    }
00475    else
00476       res = 0;
00477 
00478    ast_mutex_unlock(&refresh_lock);
00479    manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled");
00480 
00481    return res;
00482 }

Generated on Sat Mar 10 01:54:16 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7