dns_srv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 DNS SRV Record Support
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include <netinet/in.h>
  29. #include <arpa/nameser.h>
  30. #include <resolv.h>
  31. #include "asterisk/dns_core.h"
  32. #include "asterisk/dns_srv.h"
  33. #include "asterisk/linkedlists.h"
  34. #include "asterisk/dns_internal.h"
  35. #include "asterisk/utils.h"
  36. struct ast_dns_record *dns_srv_alloc(struct ast_dns_query *query, const char *data, const size_t size)
  37. {
  38. uint16_t priority;
  39. uint16_t weight;
  40. uint16_t port;
  41. const char *ptr;
  42. const char *end_of_record;
  43. struct ast_dns_srv_record *srv;
  44. int host_size;
  45. char host[NI_MAXHOST] = "";
  46. size_t host_len;
  47. ptr = dns_find_record(data, size, query->result->answer, query->result->answer_size);
  48. ast_assert(ptr != NULL);
  49. end_of_record = ptr + size;
  50. /* PRIORITY */
  51. ptr += dns_parse_short((unsigned char *) ptr, &priority);
  52. if (ptr >= end_of_record) {
  53. return NULL;
  54. }
  55. /* WEIGHT */
  56. ptr += dns_parse_short((unsigned char *) ptr, &weight);
  57. if (ptr >= end_of_record) {
  58. return NULL;
  59. }
  60. /* PORT */
  61. ptr += dns_parse_short((unsigned char *) ptr, &port);
  62. if (ptr >= end_of_record) {
  63. return NULL;
  64. }
  65. /*
  66. * The return value from dn_expand represents the size of the replacement
  67. * in the buffer which MAY be compressed. Since the expanded replacement
  68. * is NULL terminated, you can use strlen() to get the expanded size.
  69. */
  70. host_size = dn_expand((unsigned char *)query->result->answer,
  71. (unsigned char *) end_of_record, (unsigned char *) ptr, host, sizeof(host) - 1);
  72. if (host_size < 0) {
  73. ast_log(LOG_ERROR, "Failed to expand domain name: %s\n", strerror(errno));
  74. return NULL;
  75. }
  76. if (!strcmp(host, ".")) {
  77. return NULL;
  78. }
  79. host_len = strlen(host) + 1;
  80. srv = ast_calloc(1, sizeof(*srv) + size + host_len);
  81. if (!srv) {
  82. return NULL;
  83. }
  84. srv->priority = priority;
  85. srv->weight = weight;
  86. srv->port = port;
  87. srv->host = srv->data + size;
  88. ast_copy_string((char *)srv->host, host, host_len); /* SAFE */
  89. srv->generic.data_ptr = srv->data;
  90. return (struct ast_dns_record *)srv;
  91. }
  92. /* This implementation was taken from the existing srv.c which, after reading the RFC, implements it
  93. * as it should.
  94. */
  95. void dns_srv_sort(struct ast_dns_result *result)
  96. {
  97. struct ast_dns_record *current;
  98. struct dns_records newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  99. while (AST_LIST_FIRST(&result->records)) {
  100. unsigned short cur_priority = ((struct ast_dns_srv_record *)(AST_LIST_FIRST(&result->records)))->priority;
  101. struct dns_records temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  102. /* Find the lowest current priority to work on, but if the priority is already zero there is no lower priority */
  103. if (cur_priority) {
  104. AST_LIST_TRAVERSE(&result->records, current, list) {
  105. if (((struct ast_dns_srv_record *)current)->priority < cur_priority) {
  106. cur_priority = ((struct ast_dns_srv_record *)current)->priority;
  107. }
  108. }
  109. }
  110. /* Find all records which match this priority */
  111. AST_LIST_TRAVERSE_SAFE_BEGIN(&result->records, current, list) {
  112. if (((struct ast_dns_srv_record *)current)->priority != cur_priority) {
  113. continue;
  114. }
  115. AST_LIST_REMOVE_CURRENT(list);
  116. /* Records with a weight of zero must always be at the head */
  117. if (((struct ast_dns_srv_record *)current)->weight == 0) {
  118. AST_LIST_INSERT_HEAD(&temp_list, current, list);
  119. } else {
  120. AST_LIST_INSERT_TAIL(&temp_list, current, list);
  121. }
  122. }
  123. AST_LIST_TRAVERSE_SAFE_END;
  124. /* Apply weighting - as each record is passed the sum of all previous weights (plus its own) is stored away, and then a random weight
  125. * is calculated. The first record with a weight sum greater than the random weight is put in the new list and the whole thing starts
  126. * once again.
  127. */
  128. while (AST_LIST_FIRST(&temp_list)) {
  129. unsigned int weight_sum = 0;
  130. unsigned int random_weight;
  131. AST_LIST_TRAVERSE(&temp_list, current, list) {
  132. ((struct ast_dns_srv_record *)current)->weight_sum = weight_sum += ((struct ast_dns_srv_record *)current)->weight;
  133. }
  134. /* if all the remaining entries have weight == 0,
  135. then just append them to the result list and quit */
  136. if (weight_sum == 0) {
  137. AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
  138. break;
  139. }
  140. random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
  141. AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
  142. if (((struct ast_dns_srv_record *)current)->weight_sum < random_weight) {
  143. continue;
  144. }
  145. AST_LIST_MOVE_CURRENT(&newlist, list);
  146. break;
  147. }
  148. AST_LIST_TRAVERSE_SAFE_END;
  149. }
  150. }
  151. /* now that the new list has been ordered,
  152. put it in place */
  153. AST_LIST_APPEND_LIST(&result->records, &newlist, list);
  154. }
  155. const char *ast_dns_srv_get_host(const struct ast_dns_record *record)
  156. {
  157. struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
  158. ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
  159. return srv->host;
  160. }
  161. unsigned short ast_dns_srv_get_priority(const struct ast_dns_record *record)
  162. {
  163. struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
  164. ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
  165. return srv->priority;
  166. }
  167. unsigned short ast_dns_srv_get_weight(const struct ast_dns_record *record)
  168. {
  169. struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
  170. ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
  171. return srv->weight;
  172. }
  173. unsigned short ast_dns_srv_get_port(const struct ast_dns_record *record)
  174. {
  175. struct ast_dns_srv_record *srv = (struct ast_dns_srv_record *) record;
  176. ast_assert(ast_dns_record_get_rr_type(record) == T_SRV);
  177. return srv->port;
  178. }