00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 69708 $")
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
00057 struct in_addr *result;
00058
00059 struct in_addr last;
00060
00061 int changed:1;
00062 ast_mutex_t lock;
00063 AST_LIST_ENTRY(ast_dnsmgr_entry) list;
00064
00065 char name[1];
00066 };
00067
00068 static AST_LIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry);
00069
00070 AST_MUTEX_DEFINE_STATIC(refresh_lock);
00071
00072 #define REFRESH_DEFAULT 300
00073
00074 static int enabled;
00075 static int refresh_interval;
00076
00077 struct refresh_info {
00078 struct entry_list *entries;
00079 int verbose;
00080 unsigned int regex_present:1;
00081 regex_t filter;
00082 };
00083
00084 static struct refresh_info master_refresh_info = {
00085 .entries = &entry_list,
00086 .verbose = 0,
00087 };
00088
00089 struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct in_addr *result)
00090 {
00091 struct ast_dnsmgr_entry *entry;
00092
00093 if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, sizeof(*entry) + strlen(name))))
00094 return NULL;
00095
00096 entry->result = result;
00097 ast_mutex_init(&entry->lock);
00098 strcpy(entry->name, name);
00099 memcpy(&entry->last, result, sizeof(entry->last));
00100
00101 AST_LIST_LOCK(&entry_list);
00102 AST_LIST_INSERT_HEAD(&entry_list, entry, list);
00103 AST_LIST_UNLOCK(&entry_list);
00104
00105 return entry;
00106 }
00107
00108 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
00109 {
00110 if (!entry)
00111 return;
00112
00113 AST_LIST_LOCK(&entry_list);
00114 AST_LIST_REMOVE(&entry_list, entry, list);
00115 AST_LIST_UNLOCK(&entry_list);
00116 if (option_verbose > 3)
00117 ast_verbose(VERBOSE_PREFIX_4 "removing dns manager for '%s'\n", entry->name);
00118
00119 ast_mutex_destroy(&entry->lock);
00120 free(entry);
00121 }
00122
00123 int ast_dnsmgr_lookup(const char *name, struct in_addr *result, struct ast_dnsmgr_entry **dnsmgr)
00124 {
00125 struct ast_hostent ahp;
00126 struct hostent *hp;
00127
00128 if (ast_strlen_zero(name) || !result || !dnsmgr)
00129 return -1;
00130
00131 if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name))
00132 return 0;
00133
00134 if (option_verbose > 3)
00135 ast_verbose(VERBOSE_PREFIX_4 "doing dnsmgr_lookup for '%s'\n", name);
00136
00137
00138
00139 if (inet_aton(name, result))
00140 return 0;
00141
00142
00143 if ((hp = ast_gethostbyname(name, &ahp)))
00144 memcpy(result, hp->h_addr, sizeof(result));
00145
00146
00147 if (!enabled)
00148 return 0;
00149
00150 if (option_verbose > 2)
00151 ast_verbose(VERBOSE_PREFIX_2 "adding dns manager for '%s'\n", name);
00152 *dnsmgr = ast_dnsmgr_get(name, result);
00153 return !*dnsmgr;
00154 }
00155
00156
00157
00158
00159 static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
00160 {
00161 struct ast_hostent ahp;
00162 struct hostent *hp;
00163 char iabuf[INET_ADDRSTRLEN];
00164 char iabuf2[INET_ADDRSTRLEN];
00165 struct in_addr tmp;
00166 int changed = 0;
00167
00168 ast_mutex_lock(&entry->lock);
00169 if (verbose && (option_verbose > 2))
00170 ast_verbose(VERBOSE_PREFIX_2 "refreshing '%s'\n", entry->name);
00171
00172 if ((hp = ast_gethostbyname(entry->name, &ahp))) {
00173
00174 memcpy(&tmp, hp->h_addr, sizeof(tmp));
00175 if (tmp.s_addr != entry->last.s_addr) {
00176 ast_copy_string(iabuf, ast_inet_ntoa(entry->last), sizeof(iabuf));
00177 ast_copy_string(iabuf2, ast_inet_ntoa(tmp), sizeof(iabuf2));
00178 ast_log(LOG_NOTICE, "host '%s' changed from %s to %s\n",
00179 entry->name, iabuf, iabuf2);
00180 memcpy(entry->result, hp->h_addr, sizeof(entry->result));
00181 memcpy(&entry->last, hp->h_addr, sizeof(entry->last));
00182 changed = entry->changed = 1;
00183 }
00184
00185 }
00186 ast_mutex_unlock(&entry->lock);
00187 return changed;
00188 }
00189
00190 int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
00191 {
00192 return dnsmgr_refresh(entry, 0);
00193 }
00194
00195
00196
00197
00198 int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
00199 {
00200 int changed;
00201
00202 ast_mutex_lock(&entry->lock);
00203
00204 changed = entry->changed;
00205 entry->changed = 0;
00206
00207 ast_mutex_unlock(&entry->lock);
00208
00209 return changed;
00210 }
00211
00212 static void *do_refresh(void *data)
00213 {
00214 for (;;) {
00215 pthread_testcancel();
00216 usleep((ast_sched_wait(sched)*1000));
00217 pthread_testcancel();
00218 ast_sched_runq(sched);
00219 }
00220 return NULL;
00221 }
00222
00223 static int refresh_list(void *data)
00224 {
00225 struct refresh_info *info = data;
00226 struct ast_dnsmgr_entry *entry;
00227
00228
00229 if (ast_mutex_trylock(&refresh_lock)) {
00230 if (info->verbose)
00231 ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
00232 return -1;
00233 }
00234
00235 if (option_verbose > 2)
00236 ast_verbose(VERBOSE_PREFIX_2 "Refreshing DNS lookups.\n");
00237 AST_LIST_LOCK(info->entries);
00238 AST_LIST_TRAVERSE(info->entries, entry, list) {
00239 if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0))
00240 continue;
00241
00242 dnsmgr_refresh(entry, info->verbose);
00243 }
00244 AST_LIST_UNLOCK(info->entries);
00245
00246 ast_mutex_unlock(&refresh_lock);
00247
00248
00249 return refresh_interval * 1000;
00250 }
00251
00252 void dnsmgr_start_refresh(void)
00253 {
00254 if (refresh_sched > -1) {
00255 ast_sched_del(sched, refresh_sched);
00256 refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00257 }
00258 }
00259
00260 static int do_reload(int loading);
00261
00262 static int handle_cli_reload(int fd, int argc, char *argv[])
00263 {
00264 if (argc > 2)
00265 return RESULT_SHOWUSAGE;
00266
00267 do_reload(0);
00268 return 0;
00269 }
00270
00271 static int handle_cli_refresh(int fd, int argc, char *argv[])
00272 {
00273 struct refresh_info info = {
00274 .entries = &entry_list,
00275 .verbose = 1,
00276 };
00277
00278 if (argc > 3)
00279 return RESULT_SHOWUSAGE;
00280
00281 if (argc == 3) {
00282 if (regcomp(&info.filter, argv[2], REG_EXTENDED | REG_NOSUB))
00283 return RESULT_SHOWUSAGE;
00284 else
00285 info.regex_present = 1;
00286 }
00287
00288 refresh_list(&info);
00289
00290 if (info.regex_present)
00291 regfree(&info.filter);
00292
00293 return 0;
00294 }
00295
00296 static int handle_cli_status(int fd, int argc, char *argv[])
00297 {
00298 int count = 0;
00299 struct ast_dnsmgr_entry *entry;
00300
00301 if (argc > 2)
00302 return RESULT_SHOWUSAGE;
00303
00304 ast_cli(fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
00305 ast_cli(fd, "Refresh Interval: %d seconds\n", refresh_interval);
00306 AST_LIST_LOCK(&entry_list);
00307 AST_LIST_TRAVERSE(&entry_list, entry, list)
00308 count++;
00309 AST_LIST_UNLOCK(&entry_list);
00310 ast_cli(fd, "Number of entries: %d\n", count);
00311
00312 return 0;
00313 }
00314
00315 static struct ast_cli_entry cli_reload = {
00316 { "dnsmgr", "reload", NULL },
00317 handle_cli_reload, "Reloads the DNS manager configuration",
00318 "Usage: dnsmgr reload\n"
00319 " Reloads the DNS manager configuration.\n"
00320 };
00321
00322 static struct ast_cli_entry cli_refresh = {
00323 { "dnsmgr", "refresh", NULL },
00324 handle_cli_refresh, "Performs an immediate refresh",
00325 "Usage: dnsmgr refresh [pattern]\n"
00326 " Peforms an immediate refresh of the managed DNS entries.\n"
00327 " Optional regular expression pattern is used to filter the entries to refresh.\n",
00328 };
00329
00330 static struct ast_cli_entry cli_status = {
00331 { "dnsmgr", "status", NULL },
00332 handle_cli_status, "Display the DNS manager status",
00333 "Usage: dnsmgr status\n"
00334 " Displays the DNS manager status.\n"
00335 };
00336
00337 int dnsmgr_init(void)
00338 {
00339 if (!(sched = sched_context_create())) {
00340 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
00341 return -1;
00342 }
00343 ast_cli_register(&cli_reload);
00344 ast_cli_register(&cli_status);
00345 return do_reload(1);
00346 }
00347
00348 int dnsmgr_reload(void)
00349 {
00350 return do_reload(0);
00351 }
00352
00353 static int do_reload(int loading)
00354 {
00355 struct ast_config *config;
00356 const char *interval_value;
00357 const char *enabled_value;
00358 int interval;
00359 int was_enabled;
00360 int res = -1;
00361
00362
00363 ast_mutex_lock(&refresh_lock);
00364
00365
00366 refresh_interval = REFRESH_DEFAULT;
00367 was_enabled = enabled;
00368 enabled = 0;
00369
00370 if (refresh_sched > -1)
00371 ast_sched_del(sched, refresh_sched);
00372
00373 if ((config = ast_config_load("dnsmgr.conf"))) {
00374 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
00375 enabled = ast_true(enabled_value);
00376 }
00377 if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) {
00378 if (sscanf(interval_value, "%d", &interval) < 1)
00379 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value);
00380 else if (interval < 0)
00381 ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
00382 else
00383 refresh_interval = interval;
00384 }
00385 ast_config_destroy(config);
00386 }
00387
00388 if (enabled && refresh_interval)
00389 ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
00390
00391
00392
00393 if (enabled) {
00394 if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
00395 if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
00396 ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
00397 }
00398 ast_cli_register(&cli_refresh);
00399 }
00400
00401 refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
00402 res = 0;
00403 }
00404
00405
00406 else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
00407
00408 pthread_cancel(refresh_thread);
00409 pthread_kill(refresh_thread, SIGURG);
00410 pthread_join(refresh_thread, NULL);
00411 refresh_thread = AST_PTHREADT_NULL;
00412 ast_cli_unregister(&cli_refresh);
00413 res = 0;
00414 }
00415 else
00416 res = 0;
00417
00418 ast_mutex_unlock(&refresh_lock);
00419
00420 return res;
00421 }