srv.c 7.9 KB

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