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 
00032 /*! \li \ref dnsmgr.c uses the configuration file \ref dnsmgr.conf
00033  * \addtogroup configuration_file Configuration Files
00034  */
00035 
00036 /*!
00037  * \page dnsmgr.conf dnsmgr.conf
00038  * \verbinclude dnsmgr.conf.sample
00039  */
00040 
00041 
00042 /*** MODULEINFO
00043    <support_level>core</support_level>
00044  ***/
00045 
00046 #include "asterisk.h"
00047 
00048 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 433498 $")
00049 
00050 #include "asterisk/_private.h"
00051 #include <regex.h>
00052 #include <signal.h>
00053 
00054 #include "asterisk/dnsmgr.h"
00055 #include "asterisk/linkedlists.h"
00056 #include "asterisk/utils.h"
00057 #include "asterisk/config.h"
00058 #include "asterisk/sched.h"
00059 #include "asterisk/cli.h"
00060 #include "asterisk/manager.h"
00061 #include "asterisk/acl.h"
00062 
00063 static struct ast_sched_context *sched;
00064 static int refresh_sched = -1;
00065 static pthread_t refresh_thread = AST_PTHREADT_NULL;
00066 
00067 struct ast_dnsmgr_entry {
00068    /*! where we will store the resulting IP address and port number */
00069    struct ast_sockaddr *result;
00070    /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */
00071    char *service;
00072    /*! Address family to filter DNS responses. */
00073    unsigned int family;
00074    /*! Set to 1 if the entry changes */
00075    unsigned int changed:1;
00076    /*! Data to pass back to update_func */
00077    void *data;
00078    /*! The callback function to execute on address update */
00079    dns_update_func update_func;
00080    ast_mutex_t lock;
00081    AST_RWLIST_ENTRY(ast_dnsmgr_entry) list;
00082    /*! just 1 here, but we use calloc to allocate the correct size */
00083    char name[1];
00084 };
00085 
00086 static AST_RWLIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry);
00087 
00088 AST_MUTEX_DEFINE_STATIC(refresh_lock);
00089 
00090 #define REFRESH_DEFAULT 300
00091 
00092 static int enabled;
00093 static int refresh_interval;
00094 
00095 struct refresh_info {
00096    struct entry_list *entries;
00097    int verbose;
00098    unsigned int regex_present:1;
00099    regex_t filter;
00100 };
00101 
00102 static struct refresh_info master_refresh_info = {
00103    .entries = &entry_list,
00104    .verbose = 0,
00105 };
00106 
00107 struct ast_dnsmgr_entry *ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
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 }
00131 
00132 struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
00133 {
00134    return ast_dnsmgr_get_family(name, result, service, 0);
00135 }
00136 
00137 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
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 }
00151 
00152 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)
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 }
00191 
00192 int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
00193 {
00194    return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
00195 }
00196 
00197 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)
00198 {
00199    return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
00200 }
00201 
00202 /*
00203  * Refresh a dnsmgr entry
00204  */
00205 static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
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 }
00239 
00240 int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
00241 {
00242    return dnsmgr_refresh(entry, 0);
00243 }
00244 
00245 /*
00246  * Check if dnsmgr entry has changed from since last call to this function
00247  */
00248 int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
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 }
00261 
00262 static void *do_refresh(void *data)
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 }
00272 
00273 static int refresh_list(const void *data)
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 }
00302 
00303 void dnsmgr_start_refresh(void)
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 }
00310 
00311 static int do_reload(int loading);
00312 
00313 static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 }
00332 
00333 static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 }
00376 
00377 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 }
00406 
00407 static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration");
00408 static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh");
00409 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status");
00410 
00411 static void dnsmgr_shutdown(void)
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 }
00430 
00431 int dnsmgr_init(void)
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 }
00445 
00446 int dnsmgr_reload(void)
00447 {
00448    return do_reload(0);
00449 }
00450 
00451 static int do_reload(int loading)
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 }

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