Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_enum.c
Go to the documentation of this file.
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
22/*! \file
23 *
24 * \brief ENUM Functions
25 *
26 * \author Mark Spencer <markster@digium.com>
27 * \author Oleksiy Krivoshey <oleksiyk@gmail.com>
28 * \author Russell Bryant <russelb@clemson.edu>
29 * \author Brett Bryant <bbryant@digium.com>
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
166static char *synopsis = "Syntax: ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])\n";
167
168static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
169 char *buf, size_t len)
170{
173 AST_APP_ARG(tech);
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)) {
187 return -1;
188 }
189
191
192 if (args.argc < 1) {
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
232static unsigned int enum_datastore_id;
233
236 unsigned int id;
237};
238
239static 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
253static void erds_destroy_cb(void *data)
254{
255 struct enum_result_datastore *erds = data;
256 erds_destroy(erds);
257}
258
260 .type = "ENUMQUERY",
261 .destroy = erds_destroy_cb,
262};
263
264static 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
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
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
324finish:
325
326 return res;
327}
328
329static 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;
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
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);
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
401
402 break;
403 }
404
405 res = 0;
406
407finish:
408
409 return res;
410}
411
413 .name = "ENUMQUERY",
414 .read = enum_query_read,
415};
416
418 .name = "ENUMRESULT",
419 .read = enum_result_read,
420};
421
423 .name = "ENUMLOOKUP",
424 .read = function_enum,
425};
426
427static int function_txtcidname(struct ast_channel *chan, const char *cmd,
428 char *data, char *buf, size_t len)
429{
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
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
459 .name = "TXTCIDNAME",
460 .read = function_txtcidname,
461};
462
463static int unload_module(void)
464{
465 int res = 0;
466
471
472 return res;
473}
474
475static int load_module(void)
476{
477 int res = 0;
478
483
484 return res;
485}
486
487AST_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",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2414
#define ast_channel_lock(chan)
Definition: channel.h:2970
#define ast_channel_unlock(chan)
Definition: channel.h:2971
#define AST_MAX_EXTENSION
Definition: channel.h:134
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2428
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
DNS and ENUM functions.
int ast_get_txt(struct ast_channel *chan, const char *number, char *txt, int maxtxt, char *suffix)
Lookup DNS TXT record (used by app TXTCIDnum)
Definition: enum.c:995
int ast_get_enum(struct ast_channel *chan, const char *number, char *location, int maxloc, char *technology, int maxtech, char *suffix, char *options, unsigned int record, struct enum_context **argcontext)
Lookup entry in ENUM.
Definition: enum.c:649
Generic File Format Support. Should be included by clients of the file handling routines....
static char * synopsis
Definition: func_enum.c:166
static void erds_destroy(struct enum_result_datastore *data)
Definition: func_enum.c:239
static struct ast_custom_function txtcidname_function
Definition: func_enum.c:458
static struct ast_custom_function enum_query_function
Definition: func_enum.c:412
static struct ast_custom_function enum_result_function
Definition: func_enum.c:417
static int function_enum(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_enum.c:168
static int enum_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_enum.c:329
static void erds_destroy_cb(void *data)
Definition: func_enum.c:253
static int load_module(void)
Definition: func_enum.c:475
static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_enum.c:264
static unsigned int enum_datastore_id
Definition: func_enum.c:232
static int unload_module(void)
Definition: func_enum.c:463
static struct ast_custom_function enum_function
Definition: func_enum.c:422
static int function_txtcidname(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_enum.c:427
static const struct ast_datastore_info enum_result_datastore_info
Definition: func_enum.c:259
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_ERROR
#define LOG_WARNING
Asterisk locking-related definitions:
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:761
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Structure for a data store type.
Definition: datastore.h:31
const char * type
Definition: datastore.h:32
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
int naptr_rrs_count
Definition: enum.h:54
struct enum_naptr_rr * naptr_rrs
Definition: enum.h:53
char * result
Definition: enum.h:37
char * tech
Definition: enum.h:38
int sort_pos
Definition: enum.h:39
struct enum_context * context
Definition: func_enum.c:235
unsigned int id
Definition: func_enum.c:236
Number structure.
Definition: app_followme.c:157
const char * args
static struct test_options options
Utility functions.