srv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * \arg See also \ref AstENUM
  25. *
  26. */
  27. #include <sys/types.h>
  28. #include <netinet/in.h>
  29. #include <arpa/nameser.h>
  30. #if __APPLE_CC__ >= 1495
  31. #include <arpa/nameser_compat.h>
  32. #endif
  33. #include <resolv.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/channel.h"
  40. #include "asterisk/logger.h"
  41. #include "asterisk/srv.h"
  42. #include "asterisk/dns.h"
  43. #include "asterisk/options.h"
  44. #include "asterisk/utils.h"
  45. #ifdef __APPLE__
  46. #undef T_SRV
  47. #define T_SRV 33
  48. #endif
  49. struct srv {
  50. unsigned short priority;
  51. unsigned short weight;
  52. unsigned short portnum;
  53. } __attribute__ ((__packed__));
  54. static int parse_srv(char *host, int hostlen, int *portno, char *answer, int len, char *msg)
  55. {
  56. int res = 0;
  57. struct srv *srv = (struct srv *)answer;
  58. char repl[256] = "";
  59. if (len < sizeof(struct srv)) {
  60. printf("Length too short\n");
  61. return -1;
  62. }
  63. answer += sizeof(struct srv);
  64. len -= sizeof(struct srv);
  65. if ((res = dn_expand((unsigned char *)msg, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
  66. ast_log(LOG_WARNING, "Failed to expand hostname\n");
  67. return -1;
  68. }
  69. if (res && strcmp(repl, ".")) {
  70. if (option_verbose > 3)
  71. ast_verbose( VERBOSE_PREFIX_3 "parse_srv: SRV mapped to host %s, port %d\n", repl, ntohs(srv->portnum));
  72. if (host) {
  73. ast_copy_string(host, repl, hostlen);
  74. host[hostlen-1] = '\0';
  75. }
  76. if (portno)
  77. *portno = ntohs(srv->portnum);
  78. return 0;
  79. }
  80. return -1;
  81. }
  82. struct srv_context {
  83. char *host;
  84. int hostlen;
  85. int *port;
  86. };
  87. static int srv_callback(void *context, char *answer, int len, char *fullanswer)
  88. {
  89. struct srv_context *c = (struct srv_context *)context;
  90. if (parse_srv(c->host, c->hostlen, c->port, answer, len, fullanswer)) {
  91. ast_log(LOG_WARNING, "Failed to parse srv\n");
  92. return -1;
  93. }
  94. if (!ast_strlen_zero(c->host))
  95. return 1;
  96. return 0;
  97. }
  98. int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
  99. {
  100. struct srv_context context;
  101. int ret;
  102. context.host = host;
  103. context.hostlen = hostlen;
  104. context.port = port;
  105. if (chan && ast_autoservice_start(chan) < 0)
  106. return -1;
  107. ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback);
  108. if (chan)
  109. ret |= ast_autoservice_stop(chan);
  110. if (ret <= 0) {
  111. host[0] = '\0';
  112. *port = -1;
  113. return ret;
  114. }
  115. return ret;
  116. }