Sat Aug 6 00:39:28 2011

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 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 271123 $")
00029 
00030 #include <sys/types.h>
00031 #include <netinet/in.h>
00032 #include <sys/socket.h>
00033 #include <arpa/inet.h>
00034 #include <resolv.h>
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <unistd.h>
00038 #include <stdlib.h>
00039 #include <regex.h>
00040 #include <signal.h>
00041 
00042 #include "asterisk/dnsmgr.h"
00043 #include "asterisk/linkedlists.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/config.h"
00046 #include "asterisk/logger.h"
00047 #include "asterisk/sched.h"
00048 #include "asterisk/options.h"
00049 #include "asterisk/cli.h"
00050 
00051 static struct sched_context *sched;
00052 static int refresh_sched = -1;
00053 static pthread_t refresh_thread = AST_PTHREADT_NULL;
00054 
00055 struct ast_dnsmgr_entry {
00056    /*! where we will store the resulting address */
00057    struct in_addr *result;
00058    /*! Set to 1 if the entry changes */
00059    int changed:1;
00060    ast_mutex_t lock;
00061    AST_LIST_ENTRY(ast_dnsmgr_entry) list;
00062    /*! just 1 here, but we use calloc to allocate the correct size */
00063    char name[1];
00064 };
00065 
00066 static AST_LIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry);
00067 
00068 AST_MUTEX_DEFINE_STATIC(refresh_lock);
00069 
00070 #define REFRESH_DEFAULT 300
00071 
00072 static int enabled;
00073 static int refresh_interval;
00074 
00075 struct refresh_info {
00076    struct entry_list *entries;
00077    int verbose;
00078    unsigned int regex_present:1;
00079    regex_t filter;
00080 };
00081 
00082 static struct refresh_info master_refresh_info = {
00083    .entries = &entry_list,
00084    .verbose = 0,
00085 };
00086 
00087 struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct in_addr *result)
00088 {
00089    struct ast_dnsmgr_entry *entry;
00090 
00091    if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, sizeof(*entry) + strlen(name))))
00092       return NULL;
00093 
00094    entry->result = result;
00095    ast_mutex_init(&entry->lock);
00096    strcpy(entry->name, name);
00097 
00098    AST_LIST_LOCK(&entry_list);
00099    AST_LIST_INSERT_HEAD(&entry_list, entry, list);
00100    AST_LIST_UNLOCK(&entry_list);
00101 
00102    return entry;
00103 }
00104 
00105 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
00106 {
00107    if (!entry)
00108       return;
00109 
00110    AST_LIST_LOCK(&entry_list);
00111    AST_LIST_REMOVE(&entry_list, entry, list);
00112    AST_LIST_UNLOCK(&entry_list);
00113    if (option_verbose > 3)
00114       ast_verbose(VERBOSE_PREFIX_4 "removing dns manager for '%s'\n", entry->name);
00115 
00116    ast_mutex_destroy(&entry->lock);
00117    free(entry);
00118 }
00119 
00120 int ast_dnsmgr_lookup(const char *name, struct in_addr *result, struct ast_dnsmgr_entry **dnsmgr)
00121 {
00122    struct ast_hostent ahp;
00123    struct hostent *hp;
00124 
00125    if (ast_strlen_zero(name) || !result || !dnsmgr)
00126       return -1;
00127 
00128    if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name))
00129       return 0;
00130 
00131    if (option_verbose > 3)
00132       ast_verbose(VERBOSE_PREFIX_4 "doing dnsmgr_lookup for '%s'\n", name);
00133 
00134    /* if it's actually an IP address and not a name,
00135       there's no need for a managed lookup */
00136    if (inet_aton(name, result))
00137       return 0;
00138 
00139    /* do a lookup now but add a manager so it will automagically get updated in the background */
00140    if ((hp = ast_gethostbyname(name, &ahp)))
00141       memcpy(result, hp->h_addr, sizeof(result));
00142    
00143    /* if dnsmgr is not enable don't bother adding an entry */
00144    if (!enabled)
00145       return 0;
00146    
00147    if (option_verbose > 2)
00148       ast_verbose(VERBOSE_PREFIX_2 "adding dns manager for '%s'\n", name);
00149    *dnsmgr = ast_dnsmgr_get(name, result);
00150    return !*dnsmgr;
00151 }
00152 
00153 /*
00154  * Refresh a dnsmgr entry
00155  */
00156 static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
00157 {
00158    struct ast_hostent ahp;
00159    struct hostent *hp;
00160    char iabuf[INET_ADDRSTRLEN];
00161    char iabuf2[INET_ADDRSTRLEN];
00162    struct in_addr tmp;
00163    int changed = 0;
00164         
00165    ast_mutex_lock(&entry->lock);
00166    if (verbose && (option_verbose > 2))
00167       ast_verbose(VERBOSE_PREFIX_2 "refreshing '%s'\n", entry->name);
00168 
00169    if ((hp = ast_gethostbyname(entry->name, &ahp))) {
00170       /* check to see if it has changed, do callback if requested (where de callback is defined ????) */
00171       memcpy(&tmp, hp->h_addr, sizeof(tmp));
00172       if (tmp.s_addr != entry->result->s_addr) {
00173          ast_copy_string(iabuf, ast_inet_ntoa(*entry->result), sizeof(iabuf));
00174          ast_copy_string(iabuf2, ast_inet_ntoa(tmp), sizeof(iabuf2));
00175          ast_log(LOG_NOTICE, "host '%s' changed from %s to %s\n", 
00176             entry->name, iabuf, iabuf2);
00177          memcpy(entry->result, hp->h_addr, sizeof(entry->result));
00178          changed = entry->changed = 1;
00179       } 
00180       
00181    }
00182    ast_mutex_unlock(&entry->lock);
00183    return changed;
00184 }
00185 
00186 int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
00187 {
00188    return dnsmgr_refresh(entry, 0);
00189 }
00190 
00191 /*
00192  * Check if dnsmgr entry has changed from since last call to this function
00193  */
00194 int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry) 
00195 {
00196    int changed;
00197 
00198    ast_mutex_lock(&entry->lock);
00199 
00200    changed = entry->changed;
00201    entry->changed = 0;
00202 
00203    ast_mutex_unlock(&entry->lock);
00204    
00205    return changed;
00206 }
00207 
00208 static void *do_refresh(void *data)
00209 {
00210    for (;;) {
00211       pthread_testcancel();
00212       usleep((ast_sched_wait(sched)*1000));
00213       pthread_testcancel();
00214       ast_sched_runq(sched);
00215    }
00216    return NULL;
00217 }
00218 
00219 static int refresh_list(const void *data)
00220 {
00221    struct refresh_info *info = (struct refresh_info *)data;
00222    struct ast_dnsmgr_entry *entry;
00223 
00224    /* if a refresh or reload is already in progress, exit now */
00225    if (ast_mutex_trylock(&refresh_lock)) {
00226       if (info->verbose)
00227          ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
00228       return -1;
00229    }
00230 
00231    if (option_verbose > 2)
00232       ast_verbose(VERBOSE_PREFIX_2 "Refreshing DNS lookups.\n");
00233    AST_LIST_LOCK(info->entries);
00234    AST_LIST_TRAVERSE(info->entries, entry, list) {
00235       if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0))
00236           continue;
00237 
00238       dnsmgr_refresh(entry, info->verbose);
00239    }
00240    AST_LIST_UNLOCK(info->entries);
00241 
00242    ast_mutex_unlock(&refresh_lock);
00243 
00244    /* automatically reschedule based on the interval */
00245    return refresh_interval * 1000;
00246 }
00247 
00248 void dnsmgr_start_refresh(void)
00249 {
00250    if (refresh_sched > -1) {
00251       AST_SCHED_DEL(sched, refresh_sched);
00252       refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00253    }
00254 }
00255 
00256 static int do_reload(int loading);
00257 
00258 static int handle_cli_reload(int fd, int argc, char *argv[])
00259 {
00260    if (argc > 2)
00261       return RESULT_SHOWUSAGE;
00262 
00263    do_reload(0);
00264    return 0;
00265 }
00266 
00267 static int handle_cli_refresh(int fd, int argc, char *argv[])
00268 {
00269    struct refresh_info info = {
00270       .entries = &entry_list,
00271       .verbose = 1,
00272    };
00273 
00274    if(!enabled) {
00275       ast_cli(fd, "DNS Manager is disabled.\n");
00276       return 0;
00277    }
00278 
00279    if (argc > 3)
00280       return RESULT_SHOWUSAGE;
00281 
00282    if (argc == 3) {
00283       if (regcomp(&info.filter, argv[2], REG_EXTENDED | REG_NOSUB))
00284          return RESULT_SHOWUSAGE;
00285       else
00286          info.regex_present = 1;
00287    }
00288 
00289    refresh_list(&info);
00290 
00291    if (info.regex_present)
00292       regfree(&info.filter);
00293 
00294    return 0;
00295 }
00296 
00297 static int handle_cli_status(int fd, int argc, char *argv[])
00298 {
00299    int count = 0;
00300    struct ast_dnsmgr_entry *entry;
00301 
00302    if (argc > 2)
00303       return RESULT_SHOWUSAGE;
00304 
00305    ast_cli(fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
00306    ast_cli(fd, "Refresh Interval: %d seconds\n", refresh_interval);
00307    AST_LIST_LOCK(&entry_list);
00308    AST_LIST_TRAVERSE(&entry_list, entry, list)
00309       count++;
00310    AST_LIST_UNLOCK(&entry_list);
00311    ast_cli(fd, "Number of entries: %d\n", count);
00312 
00313    return 0;
00314 }
00315 
00316 static struct ast_cli_entry cli_reload = {
00317    { "dnsmgr", "reload", NULL },
00318    handle_cli_reload, "Reloads the DNS manager configuration",
00319    "Usage: dnsmgr reload\n"
00320    "       Reloads the DNS manager configuration.\n"
00321 };
00322 
00323 static struct ast_cli_entry cli_refresh = {
00324    { "dnsmgr", "refresh", NULL },
00325    handle_cli_refresh, "Performs an immediate refresh",
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 };
00330 
00331 static struct ast_cli_entry cli_status = {
00332    { "dnsmgr", "status", NULL },
00333    handle_cli_status, "Display the DNS manager status",
00334    "Usage: dnsmgr status\n"
00335    "       Displays the DNS manager status.\n"
00336 };
00337 
00338 int dnsmgr_init(void)
00339 {
00340    if (!(sched = sched_context_create())) {
00341       ast_log(LOG_ERROR, "Unable to create schedule context.\n");
00342       return -1;
00343    }
00344    ast_cli_register(&cli_reload);
00345    ast_cli_register(&cli_status);
00346    ast_cli_register(&cli_refresh);
00347    return do_reload(1);
00348 }
00349 
00350 int dnsmgr_reload(void)
00351 {
00352    return do_reload(0);
00353 }
00354 
00355 static int do_reload(int loading)
00356 {
00357    struct ast_config *config;
00358    const char *interval_value;
00359    const char *enabled_value;
00360    int interval;
00361    int was_enabled;
00362    int res = -1;
00363 
00364    /* ensure that no refresh cycles run while the reload is in progress */
00365    ast_mutex_lock(&refresh_lock);
00366 
00367    /* reset defaults in preparation for reading config file */
00368    refresh_interval = REFRESH_DEFAULT;
00369    was_enabled = enabled;
00370    enabled = 0;
00371 
00372    AST_SCHED_DEL(sched, refresh_sched);
00373 
00374    if ((config = ast_config_load("dnsmgr.conf"))) {
00375       if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
00376          enabled = ast_true(enabled_value);
00377       }
00378       if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) {
00379          if (sscanf(interval_value, "%30d", &interval) < 1)
00380             ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value);
00381          else if (interval < 0)
00382             ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
00383          else
00384             refresh_interval = interval;
00385       }
00386       ast_config_destroy(config);
00387    }
00388 
00389    if (enabled && refresh_interval)
00390       ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
00391 
00392    /* if this reload enabled the manager, create the background thread
00393       if it does not exist */
00394    if (enabled) {
00395       if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
00396          if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
00397             ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
00398          }
00399       }
00400       /* make a background refresh happen right away */
00401       refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00402       res = 0;
00403    }
00404    /* if this reload disabled the manager and there is a background thread,
00405       kill it */
00406    else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
00407       /* wake up the thread so it will exit */
00408       pthread_cancel(refresh_thread);
00409       pthread_kill(refresh_thread, SIGURG);
00410       pthread_join(refresh_thread, NULL);
00411       refresh_thread = AST_PTHREADT_NULL;
00412       res = 0;
00413    }
00414    else
00415       res = 0;
00416 
00417    ast_mutex_unlock(&refresh_lock);
00418 
00419    return res;
00420 }

Generated on Sat Aug 6 00:39:28 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7