dns_core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Core DNS Functionality
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/_private.h"
  29. #include "asterisk/linkedlists.h"
  30. #include "asterisk/astobj2.h"
  31. #include "asterisk/strings.h"
  32. #include "asterisk/sched.h"
  33. #include "asterisk/dns_core.h"
  34. #include "asterisk/dns_srv.h"
  35. #include "asterisk/dns_tlsa.h"
  36. #include "asterisk/dns_recurring.h"
  37. #include "asterisk/dns_resolver.h"
  38. #include "asterisk/dns_internal.h"
  39. #include "asterisk/netsock2.h"
  40. #include <netinet/in.h>
  41. #include <arpa/nameser.h>
  42. AST_RWLIST_HEAD_STATIC(resolvers, ast_dns_resolver);
  43. static struct ast_sched_context *sched;
  44. struct ast_sched_context *ast_dns_get_sched(void)
  45. {
  46. return sched;
  47. }
  48. const char *ast_dns_query_get_name(const struct ast_dns_query *query)
  49. {
  50. return query->name;
  51. }
  52. int ast_dns_query_get_rr_type(const struct ast_dns_query *query)
  53. {
  54. return query->rr_type;
  55. }
  56. int ast_dns_query_get_rr_class(const struct ast_dns_query *query)
  57. {
  58. return query->rr_class;
  59. }
  60. void *ast_dns_query_get_data(const struct ast_dns_query *query)
  61. {
  62. return query->user_data;
  63. }
  64. struct ast_dns_result *ast_dns_query_get_result(const struct ast_dns_query *query)
  65. {
  66. return query->result;
  67. }
  68. unsigned int ast_dns_result_get_secure(const struct ast_dns_result *result)
  69. {
  70. return result->secure;
  71. }
  72. unsigned int ast_dns_result_get_bogus(const struct ast_dns_result *result)
  73. {
  74. return result->bogus;
  75. }
  76. unsigned int ast_dns_result_get_rcode(const struct ast_dns_result *result)
  77. {
  78. return result->rcode;
  79. }
  80. const char *ast_dns_result_get_canonical(const struct ast_dns_result *result)
  81. {
  82. return result->canonical;
  83. }
  84. const struct ast_dns_record *ast_dns_result_get_records(const struct ast_dns_result *result)
  85. {
  86. return AST_LIST_FIRST(&result->records);
  87. }
  88. const char *ast_dns_result_get_answer(const struct ast_dns_result *result)
  89. {
  90. return result->answer;
  91. }
  92. int ast_dns_result_get_lowest_ttl(const struct ast_dns_result *result)
  93. {
  94. int ttl = 0;
  95. const struct ast_dns_record *record;
  96. if (ast_dns_result_get_rcode(result) == NXDOMAIN) {
  97. return 0;
  98. }
  99. for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) {
  100. if (!ttl || (ast_dns_record_get_ttl(record) && (ast_dns_record_get_ttl(record) < ttl))) {
  101. ttl = ast_dns_record_get_ttl(record);
  102. }
  103. }
  104. return ttl;
  105. }
  106. void ast_dns_result_free(struct ast_dns_result *result)
  107. {
  108. struct ast_dns_record *record;
  109. if (!result) {
  110. return;
  111. }
  112. while ((record = AST_LIST_REMOVE_HEAD(&result->records, list))) {
  113. ast_free(record);
  114. }
  115. ast_free(result);
  116. }
  117. int ast_dns_record_get_rr_type(const struct ast_dns_record *record)
  118. {
  119. return record->rr_type;
  120. }
  121. int ast_dns_record_get_rr_class(const struct ast_dns_record *record)
  122. {
  123. return record->rr_class;
  124. }
  125. int ast_dns_record_get_ttl(const struct ast_dns_record *record)
  126. {
  127. return record->ttl;
  128. }
  129. const char *ast_dns_record_get_data(const struct ast_dns_record *record)
  130. {
  131. return record->data_ptr;
  132. }
  133. size_t ast_dns_record_get_data_size(const struct ast_dns_record *record)
  134. {
  135. return record->data_len;
  136. }
  137. const struct ast_dns_record *ast_dns_record_get_next(const struct ast_dns_record *record)
  138. {
  139. return AST_LIST_NEXT(record, list);
  140. }
  141. /*! \brief Destructor for an active DNS query */
  142. static void dns_query_active_destroy(void *data)
  143. {
  144. struct ast_dns_query_active *active = data;
  145. ao2_cleanup(active->query);
  146. }
  147. /*! \brief \brief Destructor for a DNS query */
  148. static void dns_query_destroy(void *data)
  149. {
  150. struct ast_dns_query *query = data;
  151. ao2_cleanup(query->user_data);
  152. ao2_cleanup(query->resolver_data);
  153. ast_dns_result_free(query->result);
  154. }
  155. struct ast_dns_query *dns_query_alloc(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
  156. {
  157. struct ast_dns_query *query;
  158. if (ast_strlen_zero(name)) {
  159. ast_log(LOG_WARNING, "Could not perform asynchronous resolution, no name provided\n");
  160. return NULL;
  161. } else if (rr_type > 65536) {
  162. ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', resource record type '%d' exceeds maximum\n",
  163. name, rr_type);
  164. return NULL;
  165. } else if (rr_type < 0) {
  166. ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', invalid resource record type '%d'\n",
  167. name, rr_type);
  168. return NULL;
  169. } else if (rr_class > 65536) {
  170. ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', resource record class '%d' exceeds maximum\n",
  171. name, rr_class);
  172. return NULL;
  173. } else if (rr_class < 0) {
  174. ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', invalid resource class '%d'\n",
  175. name, rr_class);
  176. return NULL;
  177. } else if (!callback) {
  178. ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', no callback provided\n",
  179. name);
  180. return NULL;
  181. }
  182. query = ao2_alloc_options(sizeof(*query) + strlen(name) + 1, dns_query_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  183. if (!query) {
  184. return NULL;
  185. }
  186. query->callback = callback;
  187. query->user_data = ao2_bump(data);
  188. query->rr_type = rr_type;
  189. query->rr_class = rr_class;
  190. strcpy(query->name, name); /* SAFE */
  191. AST_RWLIST_RDLOCK(&resolvers);
  192. query->resolver = AST_RWLIST_FIRST(&resolvers);
  193. AST_RWLIST_UNLOCK(&resolvers);
  194. if (!query->resolver) {
  195. ast_log(LOG_ERROR, "Attempted to do a DNS query for '%s' of class '%d' and type '%d' but no resolver is available\n",
  196. name, rr_class, rr_type);
  197. ao2_ref(query, -1);
  198. return NULL;
  199. }
  200. return query;
  201. }
  202. struct ast_dns_query_active *ast_dns_resolve_async(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
  203. {
  204. struct ast_dns_query_active *active;
  205. active = ao2_alloc_options(sizeof(*active), dns_query_active_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  206. if (!active) {
  207. return NULL;
  208. }
  209. active->query = dns_query_alloc(name, rr_type, rr_class, callback, data);
  210. if (!active->query) {
  211. ao2_ref(active, -1);
  212. return NULL;
  213. }
  214. if (active->query->resolver->resolve(active->query)) {
  215. ast_log(LOG_ERROR, "Resolver '%s' returned an error when resolving '%s' of class '%d' and type '%d'\n",
  216. active->query->resolver->name, name, rr_class, rr_type);
  217. ao2_ref(active, -1);
  218. return NULL;
  219. }
  220. return active;
  221. }
  222. int ast_dns_resolve_cancel(struct ast_dns_query_active *active)
  223. {
  224. return active->query->resolver->cancel(active->query);
  225. }
  226. /*! \brief Structure used for signaling back for synchronous resolution completion */
  227. struct dns_synchronous_resolve {
  228. /*! \brief Lock used for signaling */
  229. ast_mutex_t lock;
  230. /*! \brief Condition used for signaling */
  231. ast_cond_t cond;
  232. /*! \brief Whether the query has completed */
  233. unsigned int completed;
  234. /*! \brief The result from the query */
  235. struct ast_dns_result *result;
  236. };
  237. /*! \brief Destructor for synchronous resolution structure */
  238. static void dns_synchronous_resolve_destroy(void *data)
  239. {
  240. struct dns_synchronous_resolve *synchronous = data;
  241. ast_mutex_destroy(&synchronous->lock);
  242. ast_cond_destroy(&synchronous->cond);
  243. /* This purposely does not unref result as it has been passed to the caller */
  244. }
  245. /*! \brief Callback used to implement synchronous resolution */
  246. static void dns_synchronous_resolve_callback(const struct ast_dns_query *query)
  247. {
  248. struct dns_synchronous_resolve *synchronous = ast_dns_query_get_data(query);
  249. synchronous->result = query->result;
  250. ((struct ast_dns_query *)query)->result = NULL;
  251. ast_mutex_lock(&synchronous->lock);
  252. synchronous->completed = 1;
  253. ast_cond_signal(&synchronous->cond);
  254. ast_mutex_unlock(&synchronous->lock);
  255. }
  256. int ast_dns_resolve(const char *name, int rr_type, int rr_class, struct ast_dns_result **result)
  257. {
  258. struct dns_synchronous_resolve *synchronous;
  259. struct ast_dns_query_active *active;
  260. if (ast_strlen_zero(name)) {
  261. ast_log(LOG_WARNING, "Could not perform synchronous resolution, no name provided\n");
  262. return -1;
  263. } else if (rr_type > 65536) {
  264. ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', resource record type '%d' exceeds maximum\n",
  265. name, rr_type);
  266. return -1;
  267. } else if (rr_type < 0) {
  268. ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', invalid resource record type '%d'\n",
  269. name, rr_type);
  270. return -1;
  271. } else if (rr_class > 65536) {
  272. ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', resource record class '%d' exceeds maximum\n",
  273. name, rr_class);
  274. return -1;
  275. } else if (rr_class < 0) {
  276. ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', invalid resource class '%d'\n",
  277. name, rr_class);
  278. return -1;
  279. } else if (!result) {
  280. ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', no result pointer provided for storing results\n",
  281. name);
  282. return -1;
  283. }
  284. synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  285. if (!synchronous) {
  286. return -1;
  287. }
  288. ast_mutex_init(&synchronous->lock);
  289. ast_cond_init(&synchronous->cond, NULL);
  290. active = ast_dns_resolve_async(name, rr_type, rr_class, dns_synchronous_resolve_callback, synchronous);
  291. if (active) {
  292. /* Wait for resolution to complete */
  293. ast_mutex_lock(&synchronous->lock);
  294. while (!synchronous->completed) {
  295. ast_cond_wait(&synchronous->cond, &synchronous->lock);
  296. }
  297. ast_mutex_unlock(&synchronous->lock);
  298. ao2_ref(active, -1);
  299. }
  300. *result = synchronous->result;
  301. ao2_ref(synchronous, -1);
  302. return *result ? 0 : -1;
  303. }
  304. int ast_dns_resolve_ipv6_and_ipv4(struct ast_sockaddr *address, const char *host, const char *port)
  305. {
  306. RAII_VAR(struct ast_dns_query_set *, queries, ast_dns_query_set_create(), ao2_cleanup);
  307. int i;
  308. int rc;
  309. if (!queries) {
  310. ast_log(LOG_ERROR, "Couldn't allocate DNS query structure\n");
  311. return -1;
  312. }
  313. rc = ast_dns_query_set_add(queries, host, ns_t_aaaa, ns_c_in);
  314. if (rc != 0) {
  315. ast_log(LOG_ERROR, "Couldn't add 'AAAA' DNS query for '%s'\n", host);
  316. return -1;
  317. }
  318. rc = ast_dns_query_set_add(queries, host, ns_t_a, ns_c_in);
  319. if (rc != 0) {
  320. ast_log(LOG_ERROR, "Couldn't add 'A' DNS query for '%s'\n", host);
  321. return -1;
  322. }
  323. rc = ast_query_set_resolve(queries);
  324. if (rc != 0) {
  325. ast_log(LOG_ERROR, "Query set resolve failure for '%s'\n", host);
  326. return -1;
  327. }
  328. for (i = 0; i < ast_dns_query_set_num_queries(queries); ++i) {
  329. struct ast_dns_query *query = ast_dns_query_set_get(queries, i);
  330. struct ast_dns_result *result = ast_dns_query_get_result(query);
  331. const struct ast_dns_record *record;
  332. in_port_t in_port = 0;
  333. if (!ast_strlen_zero(port)) {
  334. in_port = htons(atoi(port));
  335. }
  336. for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) {
  337. size_t data_size = ast_dns_record_get_data_size(record);
  338. const unsigned char *data = (unsigned char *)ast_dns_record_get_data(record);
  339. int rr_type = ast_dns_record_get_rr_type(record);
  340. if (rr_type == ns_t_aaaa && data_size == 16) {
  341. struct sockaddr_in6 sin6 = { 0, };
  342. sin6.sin6_port = in_port;
  343. memcpy(&sin6.sin6_addr, data, data_size);
  344. sin6.sin6_family = AF_INET6;
  345. memcpy(&address->ss, &sin6, sizeof(sin6));
  346. address->len = sizeof(sin6);
  347. return 0;
  348. } else if (rr_type == ns_t_a && data_size == 4) {
  349. struct sockaddr_in sin4 = { 0, };
  350. sin4.sin_port = in_port;
  351. memcpy(&sin4.sin_addr, data, data_size);
  352. sin4.sin_family = AF_INET;
  353. memcpy(&address->ss, &sin4, sizeof(sin4));
  354. address->len = sizeof(sin4);
  355. return 0;
  356. } else {
  357. ast_debug(3, "Unrecognized rr_type '%u' or data_size '%zu' from DNS query for host '%s'\n",
  358. rr_type, data_size, host);
  359. continue;
  360. }
  361. }
  362. }
  363. return -1;
  364. }
  365. int ast_dns_resolver_set_data(struct ast_dns_query *query, void *data)
  366. {
  367. if (query->resolver_data) {
  368. return -1;
  369. }
  370. query->resolver_data = ao2_bump(data);
  371. return 0;
  372. }
  373. void *ast_dns_resolver_get_data(const struct ast_dns_query *query)
  374. {
  375. return query->resolver_data;
  376. }
  377. int ast_dns_resolver_set_result(struct ast_dns_query *query, unsigned int secure, unsigned int bogus,
  378. unsigned int rcode, const char *canonical, const char *answer, size_t answer_size)
  379. {
  380. char *buf_ptr;
  381. if (secure && bogus) {
  382. ast_debug(2, "Query '%p': Could not set result information, it can not be both secure and bogus\n",
  383. query);
  384. return -1;
  385. }
  386. if (ast_strlen_zero(canonical)) {
  387. ast_debug(2, "Query '%p': Could not set result information since no canonical name was provided\n",
  388. query);
  389. return -1;
  390. }
  391. if (!answer) {
  392. answer = "";
  393. answer_size = 0;
  394. ast_debug(2, "Query '%p': Assuming zero-sized answer on NULL input\n", query);
  395. }
  396. ast_dns_result_free(query->result);
  397. query->result = ast_calloc(1, sizeof(*query->result) + strlen(canonical) + 1 + answer_size);
  398. if (!query->result) {
  399. return -1;
  400. }
  401. query->result->secure = secure;
  402. query->result->bogus = bogus;
  403. query->result->rcode = rcode;
  404. buf_ptr = query->result->buf;
  405. strcpy(buf_ptr, canonical); /* SAFE */
  406. query->result->canonical = buf_ptr;
  407. buf_ptr += strlen(canonical) + 1;
  408. memcpy(buf_ptr, answer, answer_size); /* SAFE */
  409. query->result->answer = buf_ptr;
  410. query->result->answer_size = answer_size;
  411. return 0;
  412. }
  413. static struct ast_dns_record *generic_record_alloc(struct ast_dns_query *query, const char *data, const size_t size)
  414. {
  415. struct ast_dns_record *record;
  416. record = ast_calloc(1, sizeof(*record) + size);
  417. if (!record) {
  418. return NULL;
  419. }
  420. record->data_ptr = record->data;
  421. return record;
  422. }
  423. typedef struct ast_dns_record *(*dns_alloc_fn)(struct ast_dns_query *query, const char *data, const size_t size);
  424. static dns_alloc_fn dns_alloc_table [] = {
  425. [T_TXT] = dns_txt_alloc,
  426. [T_NAPTR] = dns_naptr_alloc,
  427. [T_SRV] = dns_srv_alloc,
  428. };
  429. static struct ast_dns_record *allocate_dns_record(unsigned int rr_type, struct ast_dns_query *query, const char *data, const size_t size)
  430. {
  431. dns_alloc_fn allocator = generic_record_alloc;
  432. if (rr_type < ARRAY_LEN(dns_alloc_table) && dns_alloc_table[rr_type]) {
  433. allocator = dns_alloc_table[rr_type];
  434. }
  435. return allocator(query, data, size);
  436. }
  437. int ast_dns_resolver_add_record(struct ast_dns_query *query, int rr_type, int rr_class, int ttl, const char *data, const size_t size)
  438. {
  439. struct ast_dns_record *record;
  440. if (rr_type < 0) {
  441. ast_debug(2, "Query '%p': Could not add record, invalid resource record type '%d'\n",
  442. query, rr_type);
  443. return -1;
  444. } else if (rr_type > 65536) {
  445. ast_debug(2, "Query '%p': Could not add record, resource record type '%d' exceeds maximum\n",
  446. query, rr_type);
  447. return -1;
  448. } else if (rr_class < 0) {
  449. ast_debug(2, "Query '%p': Could not add record, invalid resource record class '%d'\n",
  450. query, rr_class);
  451. return -1;
  452. } else if (rr_class > 65536) {
  453. ast_debug(2, "Query '%p': Could not add record, resource record class '%d' exceeds maximum\n",
  454. query, rr_class);
  455. return -1;
  456. } else if (ttl < 0) {
  457. ast_debug(2, "Query '%p': Could not add record, invalid TTL '%d'\n",
  458. query, ttl);
  459. return -1;
  460. } else if (!data || !size) {
  461. ast_debug(2, "Query '%p': Could not add record, no data specified\n",
  462. query);
  463. return -1;
  464. } else if (!query->result) {
  465. ast_debug(2, "Query '%p': No result was set on the query, thus records can not be added\n",
  466. query);
  467. return -1;
  468. }
  469. record = allocate_dns_record(rr_type, query, data, size);
  470. if (!record) {
  471. return -1;
  472. }
  473. record->rr_type = rr_type;
  474. record->rr_class = rr_class;
  475. record->ttl = ttl;
  476. record->data_len = size;
  477. memcpy(record->data_ptr, data, size);
  478. AST_LIST_INSERT_TAIL(&query->result->records, record, list);
  479. return 0;
  480. }
  481. typedef void (*dns_sort_fn)(struct ast_dns_result *result);
  482. static dns_sort_fn dns_sort_table [] = {
  483. [T_NAPTR] = dns_naptr_sort,
  484. [T_SRV] = dns_srv_sort,
  485. };
  486. static void sort_result(int rr_type, struct ast_dns_result *result)
  487. {
  488. if (dns_sort_table[rr_type]) {
  489. dns_sort_table[rr_type](result);
  490. }
  491. }
  492. void ast_dns_resolver_completed(struct ast_dns_query *query)
  493. {
  494. if (query->result) {
  495. sort_result(ast_dns_query_get_rr_type(query), query->result);
  496. }
  497. query->callback(query);
  498. }
  499. static void dns_shutdown(void)
  500. {
  501. if (sched) {
  502. ast_sched_context_destroy(sched);
  503. sched = NULL;
  504. }
  505. }
  506. int dns_core_init(void)
  507. {
  508. sched = ast_sched_context_create();
  509. if (!sched) {
  510. return -1;
  511. }
  512. if (ast_sched_start_thread(sched)) {
  513. return -1;
  514. }
  515. ast_register_cleanup(dns_shutdown);
  516. return 0;
  517. }
  518. int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
  519. {
  520. struct ast_dns_resolver *iter;
  521. int inserted = 0;
  522. if (!resolver) {
  523. return -1;
  524. } else if (ast_strlen_zero(resolver->name)) {
  525. ast_log(LOG_ERROR, "Registration of DNS resolver failed as it does not have a name\n");
  526. return -1;
  527. } else if (!resolver->resolve) {
  528. ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the resolve callback which is required\n",
  529. resolver->name);
  530. return -1;
  531. } else if (!resolver->cancel) {
  532. ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the cancel callback which is required\n",
  533. resolver->name);
  534. return -1;
  535. }
  536. AST_RWLIST_WRLOCK(&resolvers);
  537. AST_LIST_TRAVERSE(&resolvers, iter, next) {
  538. if (!strcmp(iter->name, resolver->name)) {
  539. ast_log(LOG_ERROR, "A DNS resolver with the name '%s' is already registered\n", resolver->name);
  540. AST_RWLIST_UNLOCK(&resolvers);
  541. return -1;
  542. }
  543. }
  544. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&resolvers, iter, next) {
  545. if (iter->priority > resolver->priority) {
  546. AST_RWLIST_INSERT_BEFORE_CURRENT(resolver, next);
  547. inserted = 1;
  548. break;
  549. }
  550. }
  551. AST_RWLIST_TRAVERSE_SAFE_END;
  552. if (!inserted) {
  553. AST_RWLIST_INSERT_TAIL(&resolvers, resolver, next);
  554. }
  555. AST_RWLIST_UNLOCK(&resolvers);
  556. ast_verb(5, "Registered DNS resolver '%s' with priority '%d'\n", resolver->name, resolver->priority);
  557. return 0;
  558. }
  559. void ast_dns_resolver_unregister(struct ast_dns_resolver *resolver)
  560. {
  561. struct ast_dns_resolver *iter;
  562. if (!resolver) {
  563. return;
  564. }
  565. AST_RWLIST_WRLOCK(&resolvers);
  566. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&resolvers, iter, next) {
  567. if (resolver == iter) {
  568. AST_RWLIST_REMOVE_CURRENT(next);
  569. break;
  570. }
  571. }
  572. AST_RWLIST_TRAVERSE_SAFE_END;
  573. AST_RWLIST_UNLOCK(&resolvers);
  574. ast_verb(5, "Unregistered DNS resolver '%s'\n", resolver->name);
  575. }
  576. char *dns_find_record(const char *record, size_t record_size, const char *response, size_t response_size)
  577. {
  578. size_t remaining_size = response_size;
  579. const char *search_base = response;
  580. char *record_offset;
  581. while (1) {
  582. record_offset = memchr(search_base, record[0], remaining_size);
  583. ast_assert(record_offset != NULL);
  584. ast_assert(search_base + remaining_size - record_offset >= record_size);
  585. if (!memcmp(record_offset, record, record_size)) {
  586. return record_offset;
  587. }
  588. remaining_size -= record_offset - search_base;
  589. search_base = record_offset + 1;
  590. }
  591. }
  592. int dns_parse_short(unsigned char *cur, uint16_t *val)
  593. {
  594. /* This assignment takes a big-endian 16-bit value and stores it in the
  595. * machine's native byte order. Using this method allows us to avoid potential
  596. * alignment issues in case the order is not on a short-addressable boundary.
  597. * See http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html for
  598. * more information
  599. */
  600. *val = (cur[1] << 0) | (cur[0] << 8);
  601. return sizeof(*val);
  602. }
  603. int dns_parse_string(char *cur, uint8_t *size, char **val)
  604. {
  605. *size = *cur++;
  606. *val = cur;
  607. return *size + 1;
  608. }