srv.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * Funding provided by nic.at
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief DNS SRV Record Lookup Support for Asterisk
  23. *
  24. * \author Mark Spencer <markster@digium.com>
  25. *
  26. * \arg See also \ref AstENUM
  27. *
  28. * \note Funding provided by nic.at
  29. */
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <netinet/in.h>
  33. #include <arpa/nameser.h>
  34. #ifdef __APPLE__
  35. #if __APPLE_CC__ >= 1495
  36. #include <arpa/nameser_compat.h>
  37. #endif
  38. #endif
  39. #include <resolv.h>
  40. #include "asterisk/channel.h"
  41. #include "asterisk/srv.h"
  42. #include "asterisk/dns.h"
  43. #include "asterisk/utils.h"
  44. #include "asterisk/linkedlists.h"
  45. #ifdef __APPLE__
  46. #undef T_SRV
  47. #define T_SRV 33
  48. #endif
  49. struct srv_entry {
  50. unsigned short priority;
  51. unsigned short weight;
  52. unsigned short port;
  53. unsigned int weight_sum;
  54. AST_LIST_ENTRY(srv_entry) list;
  55. char host[1];
  56. };
  57. struct srv_context {
  58. unsigned int have_weights:1;
  59. AST_LIST_HEAD_NOLOCK(srv_entries, srv_entry) entries;
  60. };
  61. static int parse_srv(unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result)
  62. {
  63. struct srv {
  64. unsigned short priority;
  65. unsigned short weight;
  66. unsigned short port;
  67. } __attribute__((__packed__)) *srv = (struct srv *) answer;
  68. int res = 0;
  69. char repl[256] = "";
  70. struct srv_entry *entry;
  71. if (len < sizeof(*srv))
  72. return -1;
  73. answer += sizeof(*srv);
  74. len -= sizeof(*srv);
  75. if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) {
  76. ast_log(LOG_WARNING, "Failed to expand hostname\n");
  77. return -1;
  78. }
  79. /* the magic value "." for the target domain means that this service
  80. is *NOT* available at the domain we searched */
  81. if (!strcmp(repl, "."))
  82. return -1;
  83. if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl))))
  84. return -1;
  85. entry->priority = ntohs(srv->priority);
  86. entry->weight = ntohs(srv->weight);
  87. entry->port = ntohs(srv->port);
  88. strcpy(entry->host, repl);
  89. *result = entry;
  90. return 0;
  91. }
  92. static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
  93. {
  94. struct srv_context *c = (struct srv_context *) context;
  95. struct srv_entry *entry = NULL;
  96. struct srv_entry *current;
  97. if (parse_srv(answer, len, fullanswer, &entry))
  98. return -1;
  99. if (entry->weight)
  100. c->have_weights = 1;
  101. AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) {
  102. /* insert this entry just before the first existing
  103. entry with a higher priority */
  104. if (current->priority <= entry->priority)
  105. continue;
  106. AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
  107. entry = NULL;
  108. break;
  109. }
  110. AST_LIST_TRAVERSE_SAFE_END;
  111. /* if we didn't find a place to insert the entry before an existing
  112. entry, then just add it to the end */
  113. if (entry)
  114. AST_LIST_INSERT_TAIL(&c->entries, entry, list);
  115. return 0;
  116. }
  117. /* Do the bizarre SRV record weight-handling algorithm
  118. involving sorting and random number generation...
  119. See RFC 2782 if you want know why this code does this
  120. */
  121. static void process_weights(struct srv_context *context)
  122. {
  123. struct srv_entry *current;
  124. struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  125. while (AST_LIST_FIRST(&context->entries)) {
  126. unsigned int random_weight;
  127. unsigned int weight_sum;
  128. unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority;
  129. struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  130. weight_sum = 0;
  131. AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) {
  132. if (current->priority != cur_priority)
  133. break;
  134. AST_LIST_MOVE_CURRENT(&temp_list, list);
  135. }
  136. AST_LIST_TRAVERSE_SAFE_END;
  137. while (AST_LIST_FIRST(&temp_list)) {
  138. weight_sum = 0;
  139. AST_LIST_TRAVERSE(&temp_list, current, list)
  140. current->weight_sum = weight_sum += current->weight;
  141. /* if all the remaining entries have weight == 0,
  142. then just append them to the result list and quit */
  143. if (weight_sum == 0) {
  144. AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
  145. break;
  146. }
  147. random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
  148. AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
  149. if (current->weight < random_weight)
  150. continue;
  151. AST_LIST_MOVE_CURRENT(&newlist, list);
  152. break;
  153. }
  154. AST_LIST_TRAVERSE_SAFE_END;
  155. }
  156. }
  157. /* now that the new list has been ordered,
  158. put it in place */
  159. AST_LIST_APPEND_LIST(&context->entries, &newlist, list);
  160. }
  161. int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
  162. {
  163. struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
  164. struct srv_entry *current;
  165. int ret;
  166. if (chan && ast_autoservice_start(chan) < 0)
  167. return -1;
  168. ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
  169. if (context.have_weights)
  170. process_weights(&context);
  171. if (chan)
  172. ret |= ast_autoservice_stop(chan);
  173. /* TODO: there could be a "." entry in the returned list of
  174. answers... if so, this requires special handling */
  175. /* the list of entries will be sorted in the proper selection order
  176. already, so we just need the first one (if any) */
  177. if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
  178. ast_copy_string(host, current->host, hostlen);
  179. *port = current->port;
  180. ast_free(current);
  181. ast_verb(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
  182. service, host, *port);
  183. } else {
  184. host[0] = '\0';
  185. *port = -1;
  186. }
  187. while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list)))
  188. ast_free(current);
  189. return ret;
  190. }