dnsmgr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005-2006, Kevin P. Fleming
  5. *
  6. * Kevin P. Fleming <kpfleming@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Background DNS update manager
  21. *
  22. * \author Kevin P. Fleming <kpfleming@digium.com>
  23. *
  24. * \bug There is a minor race condition. In the event that an IP address
  25. * of a dnsmgr managed host changes, there is the potential for the consumer
  26. * of that address to access the in_addr data at the same time that the dnsmgr
  27. * thread is in the middle of updating it to the new address.
  28. */
  29. /*! \li \ref dnsmgr.c uses the configuration file \ref dnsmgr.conf
  30. * \addtogroup configuration_file Configuration Files
  31. */
  32. /*!
  33. * \page dnsmgr.conf dnsmgr.conf
  34. * \verbinclude dnsmgr.conf.sample
  35. */
  36. /*** MODULEINFO
  37. <support_level>core</support_level>
  38. ***/
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include "asterisk/_private.h"
  42. #include <regex.h>
  43. #include <signal.h>
  44. #include "asterisk/dnsmgr.h"
  45. #include "asterisk/linkedlists.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/config.h"
  48. #include "asterisk/sched.h"
  49. #include "asterisk/cli.h"
  50. #include "asterisk/manager.h"
  51. #include "asterisk/acl.h"
  52. static struct ast_sched_context *sched;
  53. static int refresh_sched = -1;
  54. static pthread_t refresh_thread = AST_PTHREADT_NULL;
  55. struct ast_dnsmgr_entry {
  56. /*! where we will store the resulting IP address and port number */
  57. struct ast_sockaddr *result;
  58. /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */
  59. char *service;
  60. /*! Address family to filter DNS responses. */
  61. unsigned int family;
  62. /*! Set to 1 if the entry changes */
  63. unsigned int changed:1;
  64. /*! Data to pass back to update_func */
  65. void *data;
  66. /*! The callback function to execute on address update */
  67. dns_update_func update_func;
  68. ast_mutex_t lock;
  69. AST_RWLIST_ENTRY(ast_dnsmgr_entry) list;
  70. /*! just 1 here, but we use calloc to allocate the correct size */
  71. char name[1];
  72. };
  73. static AST_RWLIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry);
  74. AST_MUTEX_DEFINE_STATIC(refresh_lock);
  75. #define REFRESH_DEFAULT 300
  76. static int enabled;
  77. static int refresh_interval;
  78. struct refresh_info {
  79. struct entry_list *entries;
  80. int verbose;
  81. unsigned int regex_present:1;
  82. regex_t filter;
  83. };
  84. static struct refresh_info master_refresh_info = {
  85. .entries = &entry_list,
  86. .verbose = 0,
  87. };
  88. struct ast_dnsmgr_entry *ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
  89. {
  90. struct ast_dnsmgr_entry *entry;
  91. int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
  92. if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, total_size))) {
  93. return NULL;
  94. }
  95. entry->result = result;
  96. ast_mutex_init(&entry->lock);
  97. strcpy(entry->name, name);
  98. if (service) {
  99. entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
  100. strcpy(entry->service, service);
  101. }
  102. entry->family = family;
  103. AST_RWLIST_WRLOCK(&entry_list);
  104. AST_RWLIST_INSERT_HEAD(&entry_list, entry, list);
  105. AST_RWLIST_UNLOCK(&entry_list);
  106. return entry;
  107. }
  108. struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
  109. {
  110. return ast_dnsmgr_get_family(name, result, service, 0);
  111. }
  112. void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
  113. {
  114. if (!entry) {
  115. return;
  116. }
  117. AST_RWLIST_WRLOCK(&entry_list);
  118. AST_RWLIST_REMOVE(&entry_list, entry, list);
  119. AST_RWLIST_UNLOCK(&entry_list);
  120. ast_debug(6, "removing dns manager for '%s'\n", entry->name);
  121. ast_mutex_destroy(&entry->lock);
  122. ast_free(entry);
  123. }
  124. 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)
  125. {
  126. unsigned int family;
  127. if (ast_strlen_zero(name) || !result || !dnsmgr) {
  128. return -1;
  129. }
  130. if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) {
  131. return 0;
  132. }
  133. /* Lookup address family filter. */
  134. family = result->ss.ss_family;
  135. /*
  136. * If it's actually an IP address and not a name, there's no
  137. * need for a managed lookup.
  138. */
  139. if (ast_sockaddr_parse(result, name, PARSE_PORT_FORBID)) {
  140. return 0;
  141. }
  142. ast_debug(6, "doing dnsmgr_lookup for '%s'\n", name);
  143. /* do a lookup now but add a manager so it will automagically get updated in the background */
  144. ast_get_ip_or_srv(result, name, service);
  145. /* if dnsmgr is not enable don't bother adding an entry */
  146. if (!enabled) {
  147. return 0;
  148. }
  149. ast_debug(6, "adding dns manager for '%s'\n", name);
  150. *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
  151. (*dnsmgr)->update_func = func;
  152. (*dnsmgr)->data = data;
  153. return !*dnsmgr;
  154. }
  155. int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
  156. {
  157. return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
  158. }
  159. 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)
  160. {
  161. return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
  162. }
  163. /*
  164. * Refresh a dnsmgr entry
  165. */
  166. static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
  167. {
  168. struct ast_sockaddr tmp = { .len = 0, };
  169. int changed = 0;
  170. ast_mutex_lock(&entry->lock);
  171. ast_debug(6, "refreshing '%s'\n", entry->name);
  172. tmp.ss.ss_family = entry->family;
  173. if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
  174. if (!ast_sockaddr_port(&tmp)) {
  175. ast_sockaddr_set_port(&tmp, ast_sockaddr_port(entry->result));
  176. }
  177. if (ast_sockaddr_cmp(&tmp, entry->result)) {
  178. const char *old_addr = ast_strdupa(ast_sockaddr_stringify(entry->result));
  179. const char *new_addr = ast_strdupa(ast_sockaddr_stringify(&tmp));
  180. if (entry->update_func) {
  181. entry->update_func(entry->result, &tmp, entry->data);
  182. } else {
  183. ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
  184. entry->name, old_addr, new_addr);
  185. ast_sockaddr_copy(entry->result, &tmp);
  186. changed = entry->changed = 1;
  187. }
  188. }
  189. }
  190. ast_mutex_unlock(&entry->lock);
  191. return changed;
  192. }
  193. int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
  194. {
  195. return dnsmgr_refresh(entry, 0);
  196. }
  197. /*
  198. * Check if dnsmgr entry has changed from since last call to this function
  199. */
  200. int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
  201. {
  202. int changed;
  203. ast_mutex_lock(&entry->lock);
  204. changed = entry->changed;
  205. entry->changed = 0;
  206. ast_mutex_unlock(&entry->lock);
  207. return changed;
  208. }
  209. static void *do_refresh(void *data)
  210. {
  211. for (;;) {
  212. pthread_testcancel();
  213. usleep((ast_sched_wait(sched)*1000));
  214. pthread_testcancel();
  215. ast_sched_runq(sched);
  216. }
  217. return NULL;
  218. }
  219. static int refresh_list(const void *data)
  220. {
  221. struct refresh_info *info = (struct refresh_info *)data;
  222. struct ast_dnsmgr_entry *entry;
  223. /* if a refresh or reload is already in progress, exit now */
  224. if (ast_mutex_trylock(&refresh_lock)) {
  225. if (info->verbose) {
  226. ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
  227. }
  228. return -1;
  229. }
  230. ast_debug(6, "Refreshing DNS lookups.\n");
  231. AST_RWLIST_RDLOCK(info->entries);
  232. AST_RWLIST_TRAVERSE(info->entries, entry, list) {
  233. if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
  234. continue;
  235. }
  236. dnsmgr_refresh(entry, info->verbose);
  237. }
  238. AST_RWLIST_UNLOCK(info->entries);
  239. ast_mutex_unlock(&refresh_lock);
  240. /* automatically reschedule based on the interval */
  241. return refresh_interval * 1000;
  242. }
  243. void dnsmgr_start_refresh(void)
  244. {
  245. if (refresh_sched > -1) {
  246. AST_SCHED_DEL(sched, refresh_sched);
  247. refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
  248. }
  249. }
  250. static int do_reload(int loading);
  251. static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  252. {
  253. switch (cmd) {
  254. case CLI_INIT:
  255. e->command = "dnsmgr reload";
  256. e->usage =
  257. "Usage: dnsmgr reload\n"
  258. " Reloads the DNS manager configuration.\n";
  259. return NULL;
  260. case CLI_GENERATE:
  261. return NULL;
  262. }
  263. if (a->argc > 2) {
  264. return CLI_SHOWUSAGE;
  265. }
  266. do_reload(0);
  267. return CLI_SUCCESS;
  268. }
  269. static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  270. {
  271. struct refresh_info info = {
  272. .entries = &entry_list,
  273. .verbose = 1,
  274. };
  275. switch (cmd) {
  276. case CLI_INIT:
  277. e->command = "dnsmgr refresh";
  278. e->usage =
  279. "Usage: dnsmgr refresh [pattern]\n"
  280. " Peforms an immediate refresh of the managed DNS entries.\n"
  281. " Optional regular expression pattern is used to filter the entries to refresh.\n";
  282. return NULL;
  283. case CLI_GENERATE:
  284. return NULL;
  285. }
  286. if (!enabled) {
  287. ast_cli(a->fd, "DNS Manager is disabled.\n");
  288. return 0;
  289. }
  290. if (a->argc > 3) {
  291. return CLI_SHOWUSAGE;
  292. }
  293. if (a->argc == 3) {
  294. if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) {
  295. return CLI_SHOWUSAGE;
  296. } else {
  297. info.regex_present = 1;
  298. }
  299. }
  300. refresh_list(&info);
  301. if (info.regex_present) {
  302. regfree(&info.filter);
  303. }
  304. return CLI_SUCCESS;
  305. }
  306. static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  307. {
  308. int count = 0;
  309. struct ast_dnsmgr_entry *entry;
  310. switch (cmd) {
  311. case CLI_INIT:
  312. e->command = "dnsmgr status";
  313. e->usage =
  314. "Usage: dnsmgr status\n"
  315. " Displays the DNS manager status.\n";
  316. return NULL;
  317. case CLI_GENERATE:
  318. return NULL;
  319. }
  320. if (a->argc > 2) {
  321. return CLI_SHOWUSAGE;
  322. }
  323. ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
  324. ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
  325. AST_RWLIST_RDLOCK(&entry_list);
  326. AST_RWLIST_TRAVERSE(&entry_list, entry, list)
  327. count++;
  328. AST_RWLIST_UNLOCK(&entry_list);
  329. ast_cli(a->fd, "Number of entries: %d\n", count);
  330. return CLI_SUCCESS;
  331. }
  332. static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration");
  333. static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh");
  334. static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status");
  335. static void dnsmgr_shutdown(void)
  336. {
  337. ast_cli_unregister(&cli_reload);
  338. ast_cli_unregister(&cli_status);
  339. ast_cli_unregister(&cli_refresh);
  340. /* Destroy refresh thread. */
  341. ast_mutex_lock(&refresh_lock);
  342. if (refresh_thread != AST_PTHREADT_NULL) {
  343. /* wake up the thread so it will exit */
  344. pthread_cancel(refresh_thread);
  345. pthread_kill(refresh_thread, SIGURG);
  346. pthread_join(refresh_thread, NULL);
  347. refresh_thread = AST_PTHREADT_NULL;
  348. }
  349. ast_mutex_unlock(&refresh_lock);
  350. ast_sched_context_destroy(sched);
  351. }
  352. int dnsmgr_init(void)
  353. {
  354. if (!(sched = ast_sched_context_create())) {
  355. ast_log(LOG_ERROR, "Unable to create schedule context.\n");
  356. return -1;
  357. }
  358. ast_cli_register(&cli_reload);
  359. ast_cli_register(&cli_status);
  360. ast_cli_register(&cli_refresh);
  361. ast_register_cleanup(dnsmgr_shutdown);
  362. return do_reload(1);
  363. }
  364. int dnsmgr_reload(void)
  365. {
  366. return do_reload(0);
  367. }
  368. static int do_reload(int loading)
  369. {
  370. struct ast_config *config;
  371. struct ast_variable *v;
  372. struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
  373. int interval;
  374. int was_enabled;
  375. if ((config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
  376. return 0;
  377. }
  378. /* ensure that no refresh cycles run while the reload is in progress */
  379. ast_mutex_lock(&refresh_lock);
  380. /* reset defaults in preparation for reading config file */
  381. refresh_interval = REFRESH_DEFAULT;
  382. was_enabled = enabled;
  383. enabled = 0;
  384. if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
  385. ast_mutex_unlock(&refresh_lock);
  386. return 0;
  387. }
  388. AST_SCHED_DEL(sched, refresh_sched);
  389. for (v = ast_variable_browse(config, "general"); v; v = v->next) {
  390. if (!strcasecmp(v->name, "enable")) {
  391. enabled = ast_true(v->value);
  392. } else if (!strcasecmp(v->name, "refreshinterval")) {
  393. if (sscanf(v->value, "%30d", &interval) < 1) {
  394. ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
  395. } else if (interval < 0) {
  396. ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
  397. } else {
  398. refresh_interval = interval;
  399. }
  400. }
  401. }
  402. ast_config_destroy(config);
  403. if (enabled && refresh_interval) {
  404. ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
  405. }
  406. /* if this reload enabled the manager, create the background thread
  407. if it does not exist */
  408. if (enabled) {
  409. if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
  410. if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
  411. ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
  412. }
  413. }
  414. /* make a background refresh happen right away */
  415. refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
  416. /* if this reload disabled the manager and there is a background thread, kill it */
  417. } else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
  418. /* wake up the thread so it will exit */
  419. pthread_cancel(refresh_thread);
  420. pthread_kill(refresh_thread, SIGURG);
  421. pthread_join(refresh_thread, NULL);
  422. refresh_thread = AST_PTHREADT_NULL;
  423. }
  424. ast_mutex_unlock(&refresh_lock);
  425. return 0;
  426. }