Debian Codegraph @

funcs/func_enum.c

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