func_enum.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Oleksiy Krivoshey <oleksiyk@gmail.com>
  8. * Russell Bryant <russelb@clemson.edu>
  9. * Brett Bryant <bbryant@digium.com>
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. * This program is free software, distributed under the terms of
  18. * the GNU General Public License Version 2. See the LICENSE file
  19. * at the top of the source tree.
  20. */
  21. /*! \file
  22. *
  23. * \brief ENUM Functions
  24. *
  25. * \author Mark Spencer <markster@digium.com>
  26. * \author Oleksiy Krivoshey <oleksiyk@gmail.com>
  27. * \author Russell Bryant <russelb@clemson.edu>
  28. * \author Brett Bryant <bbryant@digium.com>
  29. *
  30. * \arg See also AstENUM
  31. *
  32. * \ingroup functions
  33. */
  34. /*** MODULEINFO
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/module.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/pbx.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/lock.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/enum.h"
  46. #include "asterisk/app.h"
  47. /*** DOCUMENTATION
  48. <function name="ENUMQUERY" language="en_US">
  49. <synopsis>
  50. Initiate an ENUM query.
  51. </synopsis>
  52. <syntax>
  53. <parameter name="number" required="true" />
  54. <parameter name="method-type">
  55. <para>If no <replaceable>method-type</replaceable> is given, the default will be
  56. <literal>sip</literal>.</para>
  57. </parameter>
  58. <parameter name="zone-suffix">
  59. <para>If no <replaceable>zone-suffix</replaceable> is given, the default will be
  60. <literal>e164.arpa</literal></para>
  61. </parameter>
  62. </syntax>
  63. <description>
  64. <para>This will do a ENUM lookup of the given phone number.</para>
  65. </description>
  66. </function>
  67. <function name="ENUMRESULT" language="en_US">
  68. <synopsis>
  69. Retrieve results from a ENUMQUERY.
  70. </synopsis>
  71. <syntax>
  72. <parameter name="id" required="true">
  73. <para>The identifier returned by the ENUMQUERY function.</para>
  74. </parameter>
  75. <parameter name="resultnum" required="true">
  76. <para>The number of the result that you want to retrieve.</para>
  77. <para>Results start at <literal>1</literal>. If this argument is specified
  78. as <literal>getnum</literal>, then it will return the total number of results
  79. that are available or -1 on error.</para>
  80. </parameter>
  81. </syntax>
  82. <description>
  83. <para>This function will retrieve results from a previous use
  84. of the ENUMQUERY function.</para>
  85. </description>
  86. </function>
  87. <function name="ENUMLOOKUP" language="en_US">
  88. <synopsis>
  89. General or specific querying of NAPTR records for ENUM or ENUM-like DNS pointers.
  90. </synopsis>
  91. <syntax>
  92. <parameter name="number" required="true" />
  93. <parameter name="method-type">
  94. <para>If no <replaceable>method-type</replaceable> is given, the default will be
  95. <literal>sip</literal>.</para>
  96. </parameter>
  97. <parameter name="options">
  98. <optionlist>
  99. <option name="c">
  100. <para>Returns an integer count of the number of NAPTRs of a certain RR type.</para>
  101. <para>Combination of <literal>c</literal> and Method-type of <literal>ALL</literal> will
  102. return a count of all NAPTRs for the record or -1 on error.</para>
  103. </option>
  104. <option name="u">
  105. <para>Returns the full URI and does not strip off the URI-scheme.</para>
  106. </option>
  107. <option name="s">
  108. <para>Triggers ISN specific rewriting.</para>
  109. </option>
  110. <option name="i">
  111. <para>Looks for branches into an Infrastructure ENUM tree.</para>
  112. </option>
  113. <option name="d">
  114. <para>for a direct DNS lookup without any flipping of digits.</para>
  115. </option>
  116. </optionlist>
  117. </parameter>
  118. <parameter name="record#">
  119. <para>If no <replaceable>record#</replaceable> is given,
  120. defaults to <literal>1</literal>.</para>
  121. </parameter>
  122. <parameter name="zone-suffix">
  123. <para>If no <replaceable>zone-suffix</replaceable> is given, the default will be
  124. <literal>e164.arpa</literal></para>
  125. </parameter>
  126. </syntax>
  127. <description>
  128. <para>For more information see <filename>doc/AST.pdf</filename>.</para>
  129. </description>
  130. </function>
  131. <function name="TXTCIDNAME" language="en_US">
  132. <synopsis>
  133. TXTCIDNAME looks up a caller name via DNS.
  134. </synopsis>
  135. <syntax>
  136. <parameter name="number" required="true" />
  137. <parameter name="zone-suffix">
  138. <para>If no <replaceable>zone-suffix</replaceable> is given, the default will be
  139. <literal>e164.arpa</literal></para>
  140. </parameter>
  141. </syntax>
  142. <description>
  143. <para>This function looks up the given phone number in DNS to retrieve
  144. the caller id name. The result will either be blank or be the value
  145. found in the TXT record in DNS.</para>
  146. </description>
  147. </function>
  148. ***/
  149. static char *synopsis = "Syntax: ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])\n";
  150. static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
  151. char *buf, size_t len)
  152. {
  153. AST_DECLARE_APP_ARGS(args,
  154. AST_APP_ARG(number);
  155. AST_APP_ARG(tech);
  156. AST_APP_ARG(options);
  157. AST_APP_ARG(record);
  158. AST_APP_ARG(zone);
  159. );
  160. char tech[80];
  161. char dest[256] = "", tmp[2] = "", num[AST_MAX_EXTENSION] = "";
  162. char *s, *p;
  163. unsigned int record = 1;
  164. buf[0] = '\0';
  165. if (ast_strlen_zero(data)) {
  166. ast_log(LOG_WARNING, "%s", synopsis);
  167. return -1;
  168. }
  169. AST_STANDARD_APP_ARGS(args, data);
  170. if (args.argc < 1) {
  171. ast_log(LOG_WARNING, "%s", synopsis);
  172. return -1;
  173. }
  174. if (args.tech && !ast_strlen_zero(args.tech)) {
  175. ast_copy_string(tech,args.tech, sizeof(tech));
  176. } else {
  177. ast_copy_string(tech,"sip",sizeof(tech));
  178. }
  179. if (!args.zone) {
  180. args.zone = "e164.arpa";
  181. }
  182. if (!args.options) {
  183. args.options = "";
  184. }
  185. if (args.record) {
  186. record = atoi(args.record) ? atoi(args.record) : record;
  187. }
  188. /* strip any '-' signs from number */
  189. for (s = p = args.number; *s; s++) {
  190. if (*s != '-') {
  191. snprintf(tmp, sizeof(tmp), "%c", *s);
  192. strncat(num, tmp, sizeof(num) - strlen(num) - 1);
  193. }
  194. }
  195. ast_get_enum(chan, num, dest, sizeof(dest), tech, sizeof(tech), args.zone, args.options, record, NULL);
  196. p = strchr(dest, ':');
  197. if (p && strcasecmp(tech, "ALL") && !strchr(args.options, 'u')) {
  198. ast_copy_string(buf, p + 1, len);
  199. } else {
  200. ast_copy_string(buf, dest, len);
  201. }
  202. return 0;
  203. }
  204. static unsigned int enum_datastore_id;
  205. struct enum_result_datastore {
  206. struct enum_context *context;
  207. unsigned int id;
  208. };
  209. static void erds_destroy(struct enum_result_datastore *data)
  210. {
  211. int k;
  212. for (k = 0; k < data->context->naptr_rrs_count; k++) {
  213. ast_free(data->context->naptr_rrs[k].result);
  214. ast_free(data->context->naptr_rrs[k].tech);
  215. }
  216. ast_free(data->context->naptr_rrs);
  217. ast_free(data->context);
  218. ast_free(data);
  219. }
  220. static void erds_destroy_cb(void *data)
  221. {
  222. struct enum_result_datastore *erds = data;
  223. erds_destroy(erds);
  224. }
  225. static const struct ast_datastore_info enum_result_datastore_info = {
  226. .type = "ENUMQUERY",
  227. .destroy = erds_destroy_cb,
  228. };
  229. static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  230. {
  231. struct enum_result_datastore *erds;
  232. struct ast_datastore *datastore;
  233. char *parse, tech[128], dest[128];
  234. int res = -1;
  235. AST_DECLARE_APP_ARGS(args,
  236. AST_APP_ARG(number);
  237. AST_APP_ARG(tech);
  238. AST_APP_ARG(zone);
  239. );
  240. if (ast_strlen_zero(data)) {
  241. ast_log(LOG_WARNING, "ENUMQUERY requires at least a number as an argument...\n");
  242. goto finish;
  243. }
  244. parse = ast_strdupa(data);
  245. AST_STANDARD_APP_ARGS(args, parse);
  246. if (!chan) {
  247. ast_log(LOG_ERROR, "ENUMQUERY cannot be used without a channel!\n");
  248. goto finish;
  249. }
  250. if (!args.zone)
  251. args.zone = "e164.zone";
  252. ast_copy_string(tech, args.tech ? args.tech : "sip", sizeof(tech));
  253. if (!(erds = ast_calloc(1, sizeof(*erds))))
  254. goto finish;
  255. if (!(erds->context = ast_calloc(1, sizeof(*erds->context)))) {
  256. ast_free(erds);
  257. goto finish;
  258. }
  259. erds->id = ast_atomic_fetchadd_int((int *) &enum_datastore_id, 1);
  260. snprintf(buf, len, "%u", erds->id);
  261. if (!(datastore = ast_datastore_alloc(&enum_result_datastore_info, buf))) {
  262. ast_free(erds->context);
  263. ast_free(erds);
  264. goto finish;
  265. }
  266. ast_get_enum(chan, args.number, dest, sizeof(dest), tech, sizeof(tech), args.zone, "", 1, &erds->context);
  267. datastore->data = erds;
  268. ast_channel_lock(chan);
  269. ast_channel_datastore_add(chan, datastore);
  270. ast_channel_unlock(chan);
  271. res = 0;
  272. finish:
  273. return res;
  274. }
  275. static int enum_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  276. {
  277. struct enum_result_datastore *erds;
  278. struct ast_datastore *datastore;
  279. char *parse, *p;
  280. unsigned int num;
  281. int res = -1, k;
  282. AST_DECLARE_APP_ARGS(args,
  283. AST_APP_ARG(id);
  284. AST_APP_ARG(resultnum);
  285. );
  286. if (ast_strlen_zero(data)) {
  287. ast_log(LOG_WARNING, "ENUMRESULT requires two arguments (id and resultnum)\n");
  288. goto finish;
  289. }
  290. if (!chan) {
  291. ast_log(LOG_ERROR, "ENUMRESULT can not be used without a channel!\n");
  292. goto finish;
  293. }
  294. parse = ast_strdupa(data);
  295. AST_STANDARD_APP_ARGS(args, parse);
  296. if (ast_strlen_zero(args.id)) {
  297. ast_log(LOG_ERROR, "A result ID must be provided to ENUMRESULT\n");
  298. goto finish;
  299. }
  300. if (ast_strlen_zero(args.resultnum)) {
  301. ast_log(LOG_ERROR, "A result number must be given to ENUMRESULT!\n");
  302. goto finish;
  303. }
  304. ast_channel_lock(chan);
  305. datastore = ast_channel_datastore_find(chan, &enum_result_datastore_info, args.id);
  306. ast_channel_unlock(chan);
  307. if (!datastore) {
  308. ast_log(LOG_WARNING, "No ENUM results found for query id!\n");
  309. goto finish;
  310. }
  311. erds = datastore->data;
  312. if (!strcasecmp(args.resultnum, "getnum")) {
  313. snprintf(buf, len, "%d", erds->context->naptr_rrs_count);
  314. res = 0;
  315. goto finish;
  316. }
  317. if (sscanf(args.resultnum, "%30u", &num) != 1) {
  318. ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to ENUMRESULT!\n", args.resultnum);
  319. goto finish;
  320. }
  321. if (!num || num > erds->context->naptr_rrs_count) {
  322. ast_log(LOG_WARNING, "Result number %u is not valid for ENUM query results for ID %s!\n", num, args.id);
  323. goto finish;
  324. }
  325. for (k = 0; k < erds->context->naptr_rrs_count; k++) {
  326. if (num - 1 != erds->context->naptr_rrs[k].sort_pos)
  327. continue;
  328. p = strchr(erds->context->naptr_rrs[k].result, ':');
  329. if (p && strcasecmp(erds->context->naptr_rrs[k].tech, "ALL"))
  330. ast_copy_string(buf, p + 1, len);
  331. else
  332. ast_copy_string(buf, erds->context->naptr_rrs[k].result, len);
  333. break;
  334. }
  335. res = 0;
  336. finish:
  337. return res;
  338. }
  339. static struct ast_custom_function enum_query_function = {
  340. .name = "ENUMQUERY",
  341. .read = enum_query_read,
  342. };
  343. static struct ast_custom_function enum_result_function = {
  344. .name = "ENUMRESULT",
  345. .read = enum_result_read,
  346. };
  347. static struct ast_custom_function enum_function = {
  348. .name = "ENUMLOOKUP",
  349. .read = function_enum,
  350. };
  351. static int function_txtcidname(struct ast_channel *chan, const char *cmd,
  352. char *data, char *buf, size_t len)
  353. {
  354. AST_DECLARE_APP_ARGS(args,
  355. AST_APP_ARG(number);
  356. AST_APP_ARG(zone);
  357. );
  358. buf[0] = '\0';
  359. if (ast_strlen_zero(data)) {
  360. ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n");
  361. return -1;
  362. }
  363. AST_STANDARD_APP_ARGS(args, data);
  364. if (args.argc < 1) {
  365. ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n");
  366. return -1;
  367. }
  368. if (!args.zone) {
  369. args.zone = "e164.arpa";
  370. }
  371. ast_get_txt(chan, args.number, buf, len, args.zone);
  372. return 0;
  373. }
  374. static struct ast_custom_function txtcidname_function = {
  375. .name = "TXTCIDNAME",
  376. .read = function_txtcidname,
  377. };
  378. static int unload_module(void)
  379. {
  380. int res = 0;
  381. res |= ast_custom_function_unregister(&enum_result_function);
  382. res |= ast_custom_function_unregister(&enum_query_function);
  383. res |= ast_custom_function_unregister(&enum_function);
  384. res |= ast_custom_function_unregister(&txtcidname_function);
  385. return res;
  386. }
  387. static int load_module(void)
  388. {
  389. int res = 0;
  390. res |= ast_custom_function_register(&enum_result_function);
  391. res |= ast_custom_function_register(&enum_query_function);
  392. res |= ast_custom_function_register(&enum_function);
  393. res |= ast_custom_function_register(&txtcidname_function);
  394. return res;
  395. }
  396. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ENUM related dialplan functions");