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. AST_RWLIST_WRLOCK(&entry_list);
  110. AST_RWLIST_REMOVE(&entry_list, entry, list);
  111. AST_RWLIST_UNLOCK(&entry_list);
  112. ast_verb(6, "removing dns manager for '%s'\n", entry->name);
  113. ast_mutex_destroy(&entry->lock);
  114. ast_free(entry);
  115. }
  116. 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)
  117. {
  118. unsigned int family;
  119. if (ast_strlen_zero(name) || !result || !dnsmgr) {
  120. return -1;
  121. }
  122. if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) {
  123. return 0;
  124. }
  125. /* Lookup address family filter. */
  126. family = result->ss.ss_family;
  127. /*
  128. * If it's actually an IP address and not a name, there's no
  129. * need for a managed lookup.
  130. */
  131. if (ast_sockaddr_parse(result, name, PARSE_PORT_FORBID)) {
  132. return 0;
  133. }
  134. ast_verb(6, "doing dnsmgr_lookup for '%s'\n", name);
  135. /* do a lookup now but add a manager so it will automagically get updated in the background */
  136. ast_get_ip_or_srv(result, name, service);
  137. /* if dnsmgr is not enable don't bother adding an entry */
  138. if (!enabled) {
  139. return 0;
  140. }
  141. ast_verb(6, "adding dns manager for '%s'\n", name);
  142. *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
  143. (*dnsmgr)->update_func = func;
  144. (*dnsmgr)->data = data;
  145. return !*dnsmgr;
  146. }
  147. int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
  148. {
  149. return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
  150. }
  151. 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)
  152. {
  153. return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
  154. }
  155. /*
  156. * Refresh a dnsmgr entry
  157. */
  158. static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
  159. {
  160. struct ast_sockaddr tmp = { .len = 0, };
  161. int changed = 0;
  162. ast_mutex_lock(&entry->lock);
  163. if (verbose) {
  164. ast_verb(6, "refreshing '%s'\n", entry->name);
  165. }
  166. tmp.ss.ss_family = entry->family;
  167. if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service)) {
  168. if (!ast_sockaddr_port(&tmp)) {
  169. ast_sockaddr_set_port(&tmp, ast_sockaddr_port(entry->result));
  170. }
  171. if (ast_sockaddr_cmp(&tmp, entry->result)) {
  172. const char *old_addr = ast_strdupa(ast_sockaddr_stringify(entry->result));
  173. const char *new_addr = ast_strdupa(ast_sockaddr_stringify(&tmp));
  174. if (entry->update_func) {
  175. entry->update_func(entry->result, &tmp, entry->data);
  176. } else {
  177. ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
  178. entry->name, old_addr, new_addr);
  179. ast_sockaddr_copy(entry->result, &tmp);
  180. changed = entry->changed = 1;
  181. }
  182. }
  183. }
  184. ast_mutex_unlock(&entry->lock);
  185. return changed;
  186. }
  187. int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
  188. {
  189. return dnsmgr_refresh(entry, 0);
  190. }
  191. /*
  192. * Check if dnsmgr entry has changed from since last call to this function
  193. */
  194. int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
  195. {
  196. int changed;
  197. ast_mutex_lock(&entry->lock);
  198. changed = entry->changed;
  199. entry->changed = 0;
  200. ast_mutex_unlock(&entry->lock);
  201. return changed;
  202. }
  203. static void *do_refresh(void *data)
  204. {
  205. for (;;) {
  206. pthread_testcancel();
  207. usleep((ast_sched_wait(sched)*1000));
  208. pthread_testcancel();
  209. ast_sched_runq(sched);
  210. }
  211. return NULL;
  212. }
  213. static int refresh_list(const void *data)
  214. {
  215. struct refresh_info *info = (struct refresh_info *)data;
  216. struct ast_dnsmgr_entry *entry;
  217. /* if a refresh or reload is already in progress, exit now */
  218. if (ast_mutex_trylock(&refresh_lock)) {
  219. if (info->verbose)
  220. ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
  221. return -1;
  222. }
  223. ast_verb(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. dnsmgr_refresh(entry, info->verbose);
  229. }
  230. AST_RWLIST_UNLOCK(info->entries);
  231. ast_mutex_unlock(&refresh_lock);
  232. /* automatically reschedule based on the interval */
  233. return refresh_interval * 1000;
  234. }
  235. void dnsmgr_start_refresh(void)
  236. {
  237. if (refresh_sched > -1) {
  238. AST_SCHED_DEL(sched, refresh_sched);
  239. refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
  240. }
  241. }
  242. static int do_reload(int loading);
  243. static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  244. {
  245. switch (cmd) {
  246. case CLI_INIT:
  247. e->command = "dnsmgr reload";
  248. e->usage =
  249. "Usage: dnsmgr reload\n"
  250. " Reloads the DNS manager configuration.\n";
  251. return NULL;
  252. case CLI_GENERATE:
  253. return NULL;
  254. }
  255. if (a->argc > 2)
  256. return CLI_SHOWUSAGE;
  257. do_reload(0);
  258. return CLI_SUCCESS;
  259. }
  260. static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  261. {
  262. struct refresh_info info = {
  263. .entries = &entry_list,
  264. .verbose = 1,
  265. };
  266. switch (cmd) {
  267. case CLI_INIT:
  268. e->command = "dnsmgr refresh";
  269. e->usage =
  270. "Usage: dnsmgr refresh [pattern]\n"
  271. " Peforms an immediate refresh of the managed DNS entries.\n"
  272. " Optional regular expression pattern is used to filter the entries to refresh.\n";
  273. return NULL;
  274. case CLI_GENERATE:
  275. return NULL;
  276. }
  277. if (!enabled) {
  278. ast_cli(a->fd, "DNS Manager is disabled.\n");
  279. return 0;
  280. }
  281. if (a->argc > 3) {
  282. return CLI_SHOWUSAGE;
  283. }
  284. if (a->argc == 3) {
  285. if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) {
  286. return CLI_SHOWUSAGE;
  287. } else {
  288. info.regex_present = 1;
  289. }
  290. }
  291. refresh_list(&info);
  292. if (info.regex_present) {
  293. regfree(&info.filter);
  294. }
  295. return CLI_SUCCESS;
  296. }
  297. static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  298. {
  299. int count = 0;
  300. struct ast_dnsmgr_entry *entry;
  301. switch (cmd) {
  302. case CLI_INIT:
  303. e->command = "dnsmgr status";
  304. e->usage =
  305. "Usage: dnsmgr status\n"
  306. " Displays the DNS manager status.\n";
  307. return NULL;
  308. case CLI_GENERATE:
  309. return NULL;
  310. }
  311. if (a->argc > 2)
  312. return CLI_SHOWUSAGE;
  313. ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
  314. ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
  315. AST_RWLIST_RDLOCK(&entry_list);
  316. AST_RWLIST_TRAVERSE(&entry_list, entry, list)
  317. count++;
  318. AST_RWLIST_UNLOCK(&entry_list);
  319. ast_cli(a->fd, "Number of entries: %d\n", count);
  320. return CLI_SUCCESS;
  321. }
  322. static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration");
  323. static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh");
  324. static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status");
  325. static void dnsmgr_shutdown(void)
  326. {
  327. ast_cli_unregister(&cli_reload);
  328. ast_cli_unregister(&cli_status);
  329. ast_cli_unregister(&cli_refresh);
  330. /* Destroy refresh thread. */
  331. ast_mutex_lock(&refresh_lock);
  332. if (refresh_thread != AST_PTHREADT_NULL) {
  333. /* wake up the thread so it will exit */
  334. pthread_cancel(refresh_thread);
  335. pthread_kill(refresh_thread, SIGURG);
  336. pthread_join(refresh_thread, NULL);
  337. refresh_thread = AST_PTHREADT_NULL;
  338. }
  339. ast_mutex_unlock(&refresh_lock);
  340. ast_sched_context_destroy(sched);
  341. }
  342. int dnsmgr_init(void)
  343. {
  344. if (!(sched = ast_sched_context_create())) {
  345. ast_log(LOG_ERROR, "Unable to create schedule context.\n");
  346. return -1;
  347. }
  348. ast_cli_register(&cli_reload);
  349. ast_cli_register(&cli_status);
  350. ast_cli_register(&cli_refresh);
  351. ast_register_atexit(dnsmgr_shutdown);
  352. return do_reload(1);
  353. }
  354. int dnsmgr_reload(void)
  355. {
  356. return do_reload(0);
  357. }
  358. static int do_reload(int loading)
  359. {
  360. struct ast_config *config;
  361. struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
  362. const char *interval_value;
  363. const char *enabled_value;
  364. int interval;
  365. int was_enabled;
  366. int res = -1;
  367. config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags);
  368. if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
  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. AST_SCHED_DEL(sched, refresh_sched);
  378. if (config) {
  379. if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
  380. enabled = ast_true(enabled_value);
  381. }
  382. if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) {
  383. if (sscanf(interval_value, "%30d", &interval) < 1)
  384. ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value);
  385. else if (interval < 0)
  386. ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
  387. else
  388. refresh_interval = interval;
  389. }
  390. ast_config_destroy(config);
  391. }
  392. if (enabled && refresh_interval)
  393. ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
  394. /* if this reload enabled the manager, create the background thread
  395. if it does not exist */
  396. if (enabled) {
  397. if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
  398. if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
  399. ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
  400. }
  401. }
  402. /* make a background refresh happen right away */
  403. refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
  404. res = 0;
  405. }
  406. /* if this reload disabled the manager and there is a background thread,
  407. kill it */
  408. else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
  409. /* wake up the thread so it will exit */
  410. pthread_cancel(refresh_thread);
  411. pthread_kill(refresh_thread, SIGURG);
  412. pthread_join(refresh_thread, NULL);
  413. refresh_thread = AST_PTHREADT_NULL;
  414. res = 0;
  415. }
  416. else
  417. res = 0;
  418. ast_mutex_unlock(&refresh_lock);
  419. manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled");
  420. return res;
  421. }