func_srv.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010 Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief SRV Functions
  19. *
  20. * \author Mark Michelson <mmichelson@digium.com>
  21. *
  22. * \ingroup functions
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/module.h"
  30. #include "asterisk/srv.h"
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/app.h"
  33. #include "asterisk/datastore.h"
  34. #include "asterisk/channel.h"
  35. /*** DOCUMENTATION
  36. <function name="SRVQUERY" language="en_US">
  37. <synopsis>
  38. Initiate an SRV query.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="service" required="true">
  42. <para>The service for which to look up SRV records. An example would be something
  43. like <literal>_sip._udp.example.com</literal></para>
  44. </parameter>
  45. </syntax>
  46. <description>
  47. <para>This will do an SRV lookup of the given service.</para>
  48. </description>
  49. </function>
  50. <function name="SRVRESULT" language="en_US">
  51. <synopsis>
  52. Retrieve results from an SRVQUERY.
  53. </synopsis>
  54. <syntax>
  55. <parameter name="id" required="true">
  56. <para>The identifier returned by the SRVQUERY function.</para>
  57. </parameter>
  58. <parameter name="resultnum" required="true">
  59. <para>The number of the result that you want to retrieve.</para>
  60. <para>Results start at <literal>1</literal>. If this argument is specified
  61. as <literal>getnum</literal>, then it will return the total number of results
  62. that are available.</para>
  63. </parameter>
  64. </syntax>
  65. <description>
  66. <para>This function will retrieve results from a previous use
  67. of the SRVQUERY function.</para>
  68. </description>
  69. </function>
  70. ***/
  71. struct srv_result_datastore {
  72. struct srv_context *context;
  73. char id[1];
  74. };
  75. static void srds_destroy_cb(void *data)
  76. {
  77. struct srv_result_datastore *datastore = data;
  78. ast_srv_cleanup(&datastore->context);
  79. ast_free(datastore);
  80. }
  81. static const struct ast_datastore_info srv_result_datastore_info = {
  82. .type = "SRVQUERY",
  83. .destroy = srds_destroy_cb,
  84. };
  85. static struct srv_context *srv_datastore_setup(const char *service, struct ast_channel *chan)
  86. {
  87. struct srv_result_datastore *srds;
  88. struct ast_datastore *datastore;
  89. const char *host;
  90. unsigned short port;
  91. if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
  92. return NULL;
  93. }
  94. ast_autoservice_start(chan);
  95. if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
  96. ast_autoservice_stop(chan);
  97. ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
  98. ast_free(srds);
  99. return NULL;
  100. }
  101. ast_autoservice_stop(chan);
  102. strcpy(srds->id, service);
  103. if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
  104. ast_srv_cleanup(&srds->context);
  105. ast_free(srds);
  106. return NULL;
  107. }
  108. datastore->data = srds;
  109. ast_channel_lock(chan);
  110. ast_channel_datastore_add(chan, datastore);
  111. ast_channel_unlock(chan);
  112. return srds->context;
  113. }
  114. static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  115. {
  116. struct ast_datastore *datastore;
  117. if (!chan) {
  118. ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
  119. return -1;
  120. }
  121. if (ast_strlen_zero(data)) {
  122. ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
  123. return -1;
  124. }
  125. /* If they already called SRVQUERY for this service once,
  126. * we need to kill the old datastore.
  127. */
  128. ast_channel_lock(chan);
  129. datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
  130. ast_channel_unlock(chan);
  131. if (datastore) {
  132. ast_channel_datastore_remove(chan, datastore);
  133. ast_datastore_free(datastore);
  134. }
  135. if (!srv_datastore_setup(data, chan)) {
  136. return -1;
  137. }
  138. ast_copy_string(buf, data, len);
  139. return 0;
  140. }
  141. static struct ast_custom_function srv_query_function = {
  142. .name = "SRVQUERY",
  143. .read = srv_query_read,
  144. };
  145. static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  146. {
  147. struct srv_result_datastore *srds;
  148. struct ast_datastore *datastore;
  149. struct srv_context *srv_context;
  150. char *parse;
  151. const char *host;
  152. unsigned short port, priority, weight;
  153. unsigned int num;
  154. AST_DECLARE_APP_ARGS(args,
  155. AST_APP_ARG(id);
  156. AST_APP_ARG(resultnum);
  157. AST_APP_ARG(field);
  158. );
  159. if (!chan) {
  160. ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
  161. return -1;
  162. }
  163. if (ast_strlen_zero(data)) {
  164. ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
  165. return -1;
  166. }
  167. parse = ast_strdupa(data);
  168. AST_STANDARD_APP_ARGS(args, parse);
  169. ast_channel_lock(chan);
  170. datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
  171. ast_channel_unlock(chan);
  172. if (!datastore) {
  173. /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
  174. * No problem, we'll do the SRV lookup now.
  175. */
  176. srv_context = srv_datastore_setup(args.id, chan);
  177. if (!srv_context) {
  178. return -1;
  179. }
  180. } else {
  181. srds = datastore->data;
  182. srv_context = srds->context;
  183. }
  184. if (!strcasecmp(args.resultnum, "getnum")) {
  185. snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
  186. return 0;
  187. }
  188. if (ast_strlen_zero(args.field)) {
  189. ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
  190. return -1;
  191. }
  192. if (sscanf(args.resultnum, "%30u", &num) != 1) {
  193. ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
  194. return -1;
  195. }
  196. if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
  197. ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
  198. return -1;
  199. }
  200. if (!strcasecmp(args.field, "host")) {
  201. ast_copy_string(buf, host, len);
  202. } else if (!strcasecmp(args.field, "port")) {
  203. snprintf(buf, len, "%d", port);
  204. } else if (!strcasecmp(args.field, "priority")) {
  205. snprintf(buf, len, "%d", priority);
  206. } else if (!strcasecmp(args.field, "weight")) {
  207. snprintf(buf, len, "%d", weight);
  208. } else {
  209. ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. static struct ast_custom_function srv_result_function = {
  215. .name = "SRVRESULT",
  216. .read = srv_result_read,
  217. };
  218. static int unload_module(void)
  219. {
  220. int res = 0;
  221. res |= ast_custom_function_unregister(&srv_query_function);
  222. res |= ast_custom_function_unregister(&srv_result_function);
  223. return res;
  224. }
  225. static int load_module(void)
  226. {
  227. int res = ast_custom_function_register(&srv_query_function);
  228. if (res < 0) {
  229. return AST_MODULE_LOAD_DECLINE;
  230. }
  231. res = ast_custom_function_register(&srv_result_function);
  232. if (res < 0) {
  233. return AST_MODULE_LOAD_DECLINE;
  234. }
  235. return AST_MODULE_LOAD_SUCCESS;;
  236. }
  237. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SRV related dialplan functions");