dnsmgr.c 13 KB

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