srv.c 8.0 KB

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