#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <regex.h>
#include <signal.h>
#include "asterisk.h"
#include "asterisk/dnsmgr.h"
#include "asterisk/linkedlists.h"
#include "asterisk/utils.h"
#include "asterisk/config.h"
#include "asterisk/logger.h"
#include "asterisk/sched.h"
#include "asterisk/options.h"
#include "asterisk/cli.h"
Go to the source code of this file.
Data Structures | |
struct | ast_dnsmgr_entry |
Defines | |
#define | REFRESH_DEFAULT 300 |
Functions | |
ast_dnsmgr_entry * | ast_dnsmgr_get (const char *name, struct in_addr *result) |
int | ast_dnsmgr_lookup (const char *name, struct in_addr *result, struct ast_dnsmgr_entry **dnsmgr) |
void | ast_dnsmgr_release (struct ast_dnsmgr_entry *entry) |
AST_LIST_HEAD (entry_list, ast_dnsmgr_entry) | |
int | dnsmgr_init (void) |
void | dnsmgr_reload (void) |
void | dnsmgr_start_refresh (void) |
void * | do_refresh (void *data) |
int | do_reload (int loading) |
int | handle_cli_refresh (int fd, int argc, char *argv[]) |
int | handle_cli_reload (int fd, int argc, char *argv[]) |
int | handle_cli_status (int fd, int argc, char *argv[]) |
int | refresh_list (void *data) |
Variables | |
ast_cli_entry | cli_refresh |
ast_cli_entry | cli_reload |
ast_cli_entry | cli_status |
refresh_info | master_refresh_info |
int | refresh_sched = -1 |
pthread_t | refresh_thread = AST_PTHREADT_NULL |
sched_context * | sched |
Definition in file dnsmgr.c.
|
|
|
Definition at line 81 of file dnsmgr.c. References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_strlen_zero(), calloc, name, result, and ast_dnsmgr_entry::result. Referenced by ast_dnsmgr_lookup(). 00082 { 00083 struct ast_dnsmgr_entry *entry; 00084 00085 if (!result || ast_strlen_zero(name)) 00086 return NULL; 00087 00088 entry = calloc(1, sizeof(*entry) + strlen(name)); 00089 if (!entry) 00090 return NULL; 00091 00092 entry->result = result; 00093 strcpy(entry->name, name); 00094 00095 AST_LIST_LOCK(&entry_list); 00096 AST_LIST_INSERT_HEAD(&entry_list, entry, list); 00097 AST_LIST_UNLOCK(&entry_list); 00098 00099 return entry; 00100 }
|
|
Definition at line 113 of file dnsmgr.c. References ahp, ast_dnsmgr_get(), ast_gethostbyname(), ast_strlen_zero(), ast_verbose(), hp, name, option_verbose, result, VERBOSE_PREFIX_2, and VERBOSE_PREFIX_3. Referenced by build_peer(). 00114 { 00115 if (ast_strlen_zero(name) || !result || !dnsmgr) 00116 return -1; 00117 00118 if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) 00119 return 0; 00120 00121 if (option_verbose > 3) 00122 ast_verbose(VERBOSE_PREFIX_3 "doing lookup for '%s'\n", name); 00123 00124 /* if it's actually an IP address and not a name, 00125 there's no need for a managed lookup */ 00126 if (inet_aton(name, result)) 00127 return 0; 00128 00129 /* if the manager is disabled, do a direct lookup and return the result, 00130 otherwise register a managed lookup for the name */ 00131 if (!enabled) { 00132 struct ast_hostent ahp; 00133 struct hostent *hp; 00134 00135 if ((hp = ast_gethostbyname(name, &ahp))) 00136 memcpy(result, hp->h_addr, sizeof(result)); 00137 return 0; 00138 } else { 00139 if (option_verbose > 2) 00140 ast_verbose(VERBOSE_PREFIX_2 "adding manager for '%s'\n", name); 00141 *dnsmgr = ast_dnsmgr_get(name, result); 00142 return !*dnsmgr; 00143 } 00144 }
|
|
Definition at line 102 of file dnsmgr.c. References AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, and free. Referenced by destroy_peer(), and sip_destroy_peer(). 00103 { 00104 if (!entry) 00105 return; 00106 00107 AST_LIST_LOCK(&entry_list); 00108 AST_LIST_REMOVE(&entry_list, entry, list); 00109 AST_LIST_UNLOCK(&entry_list); 00110 free(entry); 00111 }
|
|
Definition at line 60 of file dnsmgr.c. 00069 { 00070 struct entry_list *entries; 00071 int verbose; 00072 unsigned int regex_present:1; 00073 regex_t filter; 00074 };
|
|
Definition at line 285 of file dnsmgr.c. References ast_cli_register(), AST_LIST_HEAD_INIT, ast_log(), cli_reload, cli_status, do_reload(), LOG_ERROR, sched, and sched_context_create(). Referenced by main(). 00286 { 00287 sched = sched_context_create(); 00288 if (!sched) { 00289 ast_log(LOG_ERROR, "Unable to create schedule context.\n"); 00290 return -1; 00291 } 00292 AST_LIST_HEAD_INIT(&entry_list); 00293 ast_cli_register(&cli_reload); 00294 ast_cli_register(&cli_status); 00295 return do_reload(1); 00296 }
|
|
Definition at line 298 of file dnsmgr.c. References do_reload(). Referenced by ast_module_reload(). 00299 { 00300 do_reload(0); 00301 }
|
|
Definition at line 194 of file dnsmgr.c. References ast_sched_add_variable(), ast_sched_del(), master_refresh_info, refresh_list(), refresh_sched, and sched. Referenced by main(). 00195 { 00196 if (refresh_sched > -1) { 00197 ast_sched_del(sched, refresh_sched); 00198 refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1); 00199 } 00200 }
|
|
Definition at line 146 of file dnsmgr.c. References ast_sched_runq(), ast_sched_wait(), and sched. Referenced by do_reload(). 00147 { 00148 for (;;) { 00149 pthread_testcancel(); 00150 usleep(ast_sched_wait(sched)); 00151 pthread_testcancel(); 00152 ast_sched_runq(sched); 00153 } 00154 return NULL; 00155 }
|
|
Definition at line 303 of file dnsmgr.c. References ast_cli_register(), ast_cli_unregister(), ast_config_destroy(), ast_config_load(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_pthread_create, ast_sched_add_variable(), ast_sched_del(), ast_true(), ast_variable_retrieve(), cli_refresh, do_refresh(), LOG_ERROR, LOG_NOTICE, LOG_WARNING, master_refresh_info, refresh_list(), refresh_sched, refresh_thread, and sched. 00304 { 00305 struct ast_config *config; 00306 const char *interval_value; 00307 const char *enabled_value; 00308 int interval; 00309 int was_enabled; 00310 int res = -1; 00311 00312 /* ensure that no refresh cycles run while the reload is in progress */ 00313 ast_mutex_lock(&refresh_lock); 00314 00315 /* reset defaults in preparation for reading config file */ 00316 refresh_interval = REFRESH_DEFAULT; 00317 was_enabled = enabled; 00318 enabled = 0; 00319 00320 if (refresh_sched > -1) 00321 ast_sched_del(sched, refresh_sched); 00322 00323 if ((config = ast_config_load("dnsmgr.conf"))) { 00324 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) { 00325 enabled = ast_true(enabled_value); 00326 } 00327 if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) { 00328 if (sscanf(interval_value, "%d", &interval) < 1) 00329 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value); 00330 else if (interval < 0) 00331 ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval); 00332 else 00333 refresh_interval = interval; 00334 } 00335 ast_config_destroy(config); 00336 } 00337 00338 if (enabled && refresh_interval) 00339 ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval); 00340 00341 /* if this reload enabled the manager, create the background thread 00342 if it does not exist */ 00343 if (enabled && !was_enabled && (refresh_thread == AST_PTHREADT_NULL)) { 00344 if (ast_pthread_create(&refresh_thread, NULL, do_refresh, NULL) < 0) { 00345 ast_log(LOG_ERROR, "Unable to start refresh thread.\n"); 00346 } 00347 else { 00348 ast_cli_register(&cli_refresh); 00349 /* make a background refresh happen right away */ 00350 refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1); 00351 res = 0; 00352 } 00353 } 00354 /* if this reload disabled the manager and there is a background thread, 00355 kill it */ 00356 else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) { 00357 /* wake up the thread so it will exit */ 00358 pthread_cancel(refresh_thread); 00359 pthread_kill(refresh_thread, SIGURG); 00360 pthread_join(refresh_thread, NULL); 00361 refresh_thread = AST_PTHREADT_NULL; 00362 ast_cli_unregister(&cli_refresh); 00363 res = 0; 00364 } 00365 else 00366 res = 0; 00367 00368 ast_mutex_unlock(&refresh_lock); 00369 00370 return res; 00371 }
|
|
Definition at line 213 of file dnsmgr.c. References refresh_list(). 00214 { 00215 struct refresh_info info = { 00216 .entries = &entry_list, 00217 .verbose = 1, 00218 }; 00219 00220 if (argc > 3) 00221 return RESULT_SHOWUSAGE; 00222 00223 if (argc == 3) { 00224 if (regcomp(&info.filter, argv[2], REG_EXTENDED | REG_NOSUB)) 00225 return RESULT_SHOWUSAGE; 00226 else 00227 info.regex_present = 1; 00228 } 00229 00230 refresh_list(&info); 00231 00232 if (info.regex_present) 00233 regfree(&info.filter); 00234 00235 return 0; 00236 }
|
|
Definition at line 204 of file dnsmgr.c. References do_reload(). 00205 { 00206 if (argc > 2) 00207 return RESULT_SHOWUSAGE; 00208 00209 do_reload(0); 00210 return 0; 00211 }
|
|
Definition at line 238 of file dnsmgr.c. References ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, and AST_LIST_UNLOCK. 00239 { 00240 int count = 0; 00241 struct ast_dnsmgr_entry *entry; 00242 00243 if (argc > 2) 00244 return RESULT_SHOWUSAGE; 00245 00246 ast_cli(fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled"); 00247 ast_cli(fd, "Refresh Interval: %d seconds\n", refresh_interval); 00248 AST_LIST_LOCK(&entry_list); 00249 AST_LIST_TRAVERSE(&entry_list, entry, list) 00250 count++; 00251 AST_LIST_UNLOCK(&entry_list); 00252 ast_cli(fd, "Number of entries: %d\n", count); 00253 00254 return 0; 00255 }
|
|
Definition at line 157 of file dnsmgr.c. References ahp, ast_gethostbyname(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_mutex_trylock(), ast_mutex_unlock(), ast_verbose(), hp, LOG_WARNING, option_verbose, ast_dnsmgr_entry::result, and VERBOSE_PREFIX_2. Referenced by dnsmgr_start_refresh(), do_reload(), and handle_cli_refresh(). 00158 { 00159 struct refresh_info *info = data; 00160 struct ast_dnsmgr_entry *entry; 00161 struct ast_hostent ahp; 00162 struct hostent *hp; 00163 00164 /* if a refresh or reload is already in progress, exit now */ 00165 if (ast_mutex_trylock(&refresh_lock)) { 00166 if (info->verbose) 00167 ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n"); 00168 return -1; 00169 } 00170 00171 if (option_verbose > 2) 00172 ast_verbose(VERBOSE_PREFIX_2 "Refreshing DNS lookups.\n"); 00173 AST_LIST_LOCK(info->entries); 00174 AST_LIST_TRAVERSE(info->entries, entry, list) { 00175 if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) 00176 continue; 00177 00178 if (info->verbose && (option_verbose > 2)) 00179 ast_verbose(VERBOSE_PREFIX_2 "refreshing '%s'\n", entry->name); 00180 00181 if ((hp = ast_gethostbyname(entry->name, &ahp))) { 00182 /* check to see if it has changed, do callback if requested */ 00183 memcpy(entry->result, hp->h_addr, sizeof(entry->result)); 00184 } 00185 } 00186 AST_LIST_UNLOCK(info->entries); 00187 00188 ast_mutex_unlock(&refresh_lock); 00189 00190 /* automatically reschedule based on the interval */ 00191 return refresh_interval * 1000; 00192 }
|
|
Definition at line 266 of file dnsmgr.c. Referenced by do_reload(). |
|
Definition at line 257 of file dnsmgr.c. Referenced by dnsmgr_init(). |
|
Definition at line 276 of file dnsmgr.c. Referenced by dnsmgr_init(). |
|
Initial value: { .entries = &entry_list, .verbose = 0, } Definition at line 76 of file dnsmgr.c. Referenced by dnsmgr_start_refresh(), and do_reload(). |
|
Definition at line 51 of file dnsmgr.c. Referenced by dnsmgr_start_refresh(), and do_reload(). |
|
Definition at line 52 of file dnsmgr.c. Referenced by do_reload(). |
|
Definition at line 50 of file dnsmgr.c. Referenced by dnsmgr_init(), dnsmgr_start_refresh(), do_refresh(), and do_reload(). |