srv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 ast_extension_states "AstENUM"
  27. *
  28. * \note Funding provided by nic.at
  29. */
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. #include <netinet/in.h>
  35. #include <arpa/nameser.h>
  36. #ifdef __APPLE__
  37. #include <arpa/nameser_compat.h>
  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. struct srv_entry *prev;
  60. unsigned int num_records;
  61. AST_LIST_HEAD_NOLOCK(srv_entries, srv_entry) entries;
  62. };
  63. static int parse_srv(unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result)
  64. {
  65. struct srv {
  66. unsigned short priority;
  67. unsigned short weight;
  68. unsigned short port;
  69. } __attribute__((__packed__)) *srv = (struct srv *) answer;
  70. int res = 0;
  71. char repl[256] = "";
  72. struct srv_entry *entry;
  73. if (len < sizeof(*srv))
  74. return -1;
  75. answer += sizeof(*srv);
  76. len -= sizeof(*srv);
  77. if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) {
  78. ast_log(LOG_WARNING, "Failed to expand hostname\n");
  79. return -1;
  80. }
  81. /* the magic value "." for the target domain means that this service
  82. is *NOT* available at the domain we searched */
  83. if (!strcmp(repl, "."))
  84. return -1;
  85. if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl))))
  86. return -1;
  87. entry->priority = ntohs(srv->priority);
  88. entry->weight = ntohs(srv->weight);
  89. entry->port = ntohs(srv->port);
  90. strcpy(entry->host, repl);
  91. *result = entry;
  92. return 0;
  93. }
  94. static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
  95. {
  96. struct srv_context *c = (struct srv_context *) context;
  97. struct srv_entry *entry = NULL;
  98. struct srv_entry *current;
  99. if (parse_srv(answer, len, fullanswer, &entry))
  100. return -1;
  101. if (entry->weight)
  102. c->have_weights = 1;
  103. AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) {
  104. /* insert this entry just before the first existing
  105. entry with a higher priority */
  106. if (current->priority <= entry->priority)
  107. continue;
  108. AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
  109. entry = NULL;
  110. break;
  111. }
  112. AST_LIST_TRAVERSE_SAFE_END;
  113. /* if we didn't find a place to insert the entry before an existing
  114. entry, then just add it to the end */
  115. if (entry)
  116. AST_LIST_INSERT_TAIL(&c->entries, entry, list);
  117. return 0;
  118. }
  119. /* Do the bizarre SRV record weight-handling algorithm
  120. involving sorting and random number generation...
  121. See RFC 2782 if you want know why this code does this
  122. */
  123. static void process_weights(struct srv_context *context)
  124. {
  125. struct srv_entry *current;
  126. struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  127. while (AST_LIST_FIRST(&context->entries)) {
  128. unsigned int random_weight;
  129. unsigned int weight_sum;
  130. unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority;
  131. struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  132. weight_sum = 0;
  133. AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) {
  134. if (current->priority != cur_priority)
  135. break;
  136. AST_LIST_MOVE_CURRENT(&temp_list, list);
  137. }
  138. AST_LIST_TRAVERSE_SAFE_END;
  139. while (AST_LIST_FIRST(&temp_list)) {
  140. weight_sum = 0;
  141. AST_LIST_TRAVERSE(&temp_list, current, list)
  142. current->weight_sum = weight_sum += current->weight;
  143. /* if all the remaining entries have weight == 0,
  144. then just append them to the result list and quit */
  145. if (weight_sum == 0) {
  146. AST_LIST_APPEND_LIST(&newlist, &temp_list, list);
  147. break;
  148. }
  149. random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0)));
  150. AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) {
  151. if (current->weight < random_weight)
  152. continue;
  153. AST_LIST_MOVE_CURRENT(&newlist, list);
  154. break;
  155. }
  156. AST_LIST_TRAVERSE_SAFE_END;
  157. }
  158. }
  159. /* now that the new list has been ordered,
  160. put it in place */
  161. AST_LIST_APPEND_LIST(&context->entries, &newlist, list);
  162. }
  163. int ast_srv_lookup(struct srv_context **context, const char *service, const char **host, unsigned short *port)
  164. {
  165. struct srv_entry *cur;
  166. if (*context == NULL) {
  167. if (!(*context = ast_calloc(1, sizeof(struct srv_context)))) {
  168. return -1;
  169. }
  170. AST_LIST_HEAD_INIT_NOLOCK(&(*context)->entries);
  171. if (((ast_search_dns(*context, service, C_IN, T_SRV, srv_callback)) < 1) ||
  172. AST_LIST_EMPTY(&(*context)->entries)) {
  173. ast_free(*context);
  174. *context = NULL;
  175. return -1;
  176. }
  177. if ((*context)->have_weights) {
  178. process_weights(*context);
  179. }
  180. (*context)->prev = AST_LIST_FIRST(&(*context)->entries);
  181. *host = (*context)->prev->host;
  182. *port = (*context)->prev->port;
  183. AST_LIST_TRAVERSE(&(*context)->entries, cur, list) {
  184. ++((*context)->num_records);
  185. }
  186. return 0;
  187. }
  188. if (((*context)->prev = AST_LIST_NEXT((*context)->prev, list))) {
  189. /* Retrieve next item in result */
  190. *host = (*context)->prev->host;
  191. *port = (*context)->prev->port;
  192. return 0;
  193. } else {
  194. /* No more results */
  195. while ((cur = AST_LIST_REMOVE_HEAD(&(*context)->entries, list))) {
  196. ast_free(cur);
  197. }
  198. ast_free(*context);
  199. *context = NULL;
  200. return 1;
  201. }
  202. }
  203. void ast_srv_cleanup(struct srv_context **context)
  204. {
  205. const char *host;
  206. unsigned short port;
  207. if (*context) {
  208. /* We have a context to clean up. */
  209. while (!(ast_srv_lookup(context, NULL, &host, &port))) {
  210. }
  211. }
  212. }
  213. int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
  214. {
  215. struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE };
  216. struct srv_entry *current;
  217. int ret;
  218. if (chan && ast_autoservice_start(chan) < 0) {
  219. return -1;
  220. }
  221. ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
  222. if (context.have_weights) {
  223. process_weights(&context);
  224. }
  225. if (chan) {
  226. ret |= ast_autoservice_stop(chan);
  227. }
  228. /* TODO: there could be a "." entry in the returned list of
  229. answers... if so, this requires special handling */
  230. /* the list of entries will be sorted in the proper selection order
  231. already, so we just need the first one (if any) */
  232. if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
  233. ast_copy_string(host, current->host, hostlen);
  234. *port = current->port;
  235. ast_free(current);
  236. ast_debug(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
  237. service, host, *port);
  238. } else {
  239. host[0] = '\0';
  240. *port = -1;
  241. }
  242. while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) {
  243. ast_free(current);
  244. }
  245. return ret;
  246. }
  247. unsigned int ast_srv_get_record_count(struct srv_context *context)
  248. {
  249. return context->num_records;
  250. }
  251. int ast_srv_get_nth_record(struct srv_context *context, int record_num, const char **host,
  252. unsigned short *port, unsigned short *priority, unsigned short *weight)
  253. {
  254. int i = 1;
  255. int res = -1;
  256. struct srv_entry *entry;
  257. if (record_num < 1 || record_num > context->num_records) {
  258. return res;
  259. }
  260. AST_LIST_TRAVERSE(&context->entries, entry, list) {
  261. if (i == record_num) {
  262. *host = entry->host;
  263. *port = entry->port;
  264. *priority = entry->priority;
  265. *weight = entry->weight;
  266. res = 0;
  267. break;
  268. }
  269. ++i;
  270. }
  271. return res;
  272. }