Newer
Older
Kevin P. Fleming
committed
/*
* Asterisk -- An open source telephony toolkit.
Kevin P. Fleming
committed
*
Kevin P. Fleming
committed
*
Russell Bryant
committed
* Mark Spencer <markster@digium.com>
Kevin P. Fleming
committed
* Oleksiy Krivoshey <oleksiyk@gmail.com>
Russell Bryant
committed
* Russell Bryant <russelb@clemson.edu>
* Brett Bryant <bbryant@digium.com>
Kevin P. Fleming
committed
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
Kevin P. Fleming
committed
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
*
* \brief ENUM Functions
*
* \author Mark Spencer <markster@digium.com>
* \author Oleksiy Krivoshey <oleksiyk@gmail.com>
* \author Russell Bryant <russelb@clemson.edu>
* \author Brett Bryant <bbryant@digium.com>
* \arg See also AstENUM
Kevin P. Fleming
committed
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
Kevin P. Fleming
committed
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/enum.h"
Kevin P. Fleming
committed
Tilghman Lesher
committed
static char *synopsis = "Syntax: ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])\n";
Kevin P. Fleming
committed
static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
Kevin P. Fleming
committed
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(number);
AST_APP_ARG(tech);
AST_APP_ARG(options);
AST_APP_ARG(record);
AST_APP_ARG(zone);
);
int res = 0;
char tech[80];
char dest[256] = "", tmp[2] = "", num[AST_MAX_EXTENSION] = "";
buf[0] = '\0';
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "%s", synopsis);
return -1;
}
AST_STANDARD_APP_ARGS(args, data);
if (args.argc < 1) {
ast_log(LOG_WARNING, "%s", synopsis);
Brett Bryant
committed
if (args.tech && !ast_strlen_zero(args.tech)) {
ast_copy_string(tech,args.tech, sizeof(tech));
} else {
ast_copy_string(tech,"sip",sizeof(tech));
}
Brett Bryant
committed
if (!args.zone) {
Kevin P. Fleming
committed
args.zone = "e164.arpa";
Brett Bryant
committed
}
if (!args.options) {
Brett Bryant
committed
}
if (args.record) {
record = atoi(args.record) ? atoi(args.record) : record;
}
/* strip any '-' signs from number */
Kevin P. Fleming
committed
for (s = p = args.number; *s; s++) {
if (*s != '-') {
snprintf(tmp, sizeof(tmp), "%c", *s);
strncat(num, tmp, sizeof(num) - strlen(num) - 1);
Brett Bryant
committed
res = ast_get_enum(chan, num, dest, sizeof(dest), tech, sizeof(tech), args.zone, args.options, record, NULL);
Brett Bryant
committed
if (p && strcasecmp(tech, "ALL") && !strchr(args.options, 'u')) {
Brett Bryant
committed
} else {
Brett Bryant
committed
}
Kevin P. Fleming
committed
}
unsigned int enum_datastore_id;
struct enum_result_datastore {
struct enum_context *context;
unsigned int id;
};
static void erds_destroy(struct enum_result_datastore *data)
{
int k;
for (k = 0; k < data->context->naptr_rrs_count; k++) {
Tilghman Lesher
committed
ast_free(data->context->naptr_rrs[k].result);
ast_free(data->context->naptr_rrs[k].tech);
}
Tilghman Lesher
committed
ast_free(data->context->naptr_rrs);
ast_free(data->context);
ast_free(data);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
}
static void erds_destroy_cb(void *data)
{
struct enum_result_datastore *erds = data;
erds_destroy(erds);
}
const struct ast_datastore_info enum_result_datastore_info = {
.type = "ENUMQUERY",
.destroy = erds_destroy_cb,
};
static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct enum_result_datastore *erds;
struct ast_datastore *datastore;
char *parse, tech[128], dest[128];
int res = -1;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(number);
AST_APP_ARG(tech);
AST_APP_ARG(zone);
);
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "ENUMQUERY requires at least a number as an argument...\n");
goto finish;
}
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (!chan) {
ast_log(LOG_ERROR, "ENUMQUERY cannot be used without a channel!\n");
goto finish;
}
if (!args.zone)
args.zone = "e164.zone";
ast_copy_string(tech, args.tech ? args.tech : "sip", sizeof(tech));
if (!(erds = ast_calloc(1, sizeof(*erds))))
goto finish;
if (!(erds->context = ast_calloc(1, sizeof(*erds->context)))) {
Tilghman Lesher
committed
ast_free(erds);
goto finish;
}
erds->id = ast_atomic_fetchadd_int((int *) &enum_datastore_id, 1);
snprintf(buf, len, "%u", erds->id);
Kevin P. Fleming
committed
if (!(datastore = ast_datastore_alloc(&enum_result_datastore_info, buf))) {
Tilghman Lesher
committed
ast_free(erds->context);
ast_free(erds);
goto finish;
}
ast_get_enum(chan, args.number, dest, sizeof(dest), tech, sizeof(tech), args.zone, "", 1, &erds->context);
datastore->data = erds;
Russell Bryant
committed
ast_channel_lock(chan);
ast_channel_datastore_add(chan, datastore);
Russell Bryant
committed
ast_channel_unlock(chan);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
res = 0;
finish:
return res;
}
static int enum_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
struct enum_result_datastore *erds;
struct ast_datastore *datastore;
char *parse, *p;
unsigned int num;
int res = -1, k;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(id);
AST_APP_ARG(resultnum);
);
if (ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "ENUMRESULT requires two arguments (id and resultnum)\n");
goto finish;
}
if (!chan) {
ast_log(LOG_ERROR, "ENUMRESULT can not be used without a channel!\n");
goto finish;
}
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(args.id)) {
ast_log(LOG_ERROR, "A result ID must be provided to ENUMRESULT\n");
goto finish;
}
if (ast_strlen_zero(args.resultnum)) {
ast_log(LOG_ERROR, "A result number must be given to ENUMRESULT!\n");
goto finish;
}
Russell Bryant
committed
ast_channel_lock(chan);
datastore = ast_channel_datastore_find(chan, &enum_result_datastore_info, args.id);
ast_channel_unlock(chan);
if (!datastore) {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
ast_log(LOG_WARNING, "No ENUM results found for query id!\n");
goto finish;
}
erds = datastore->data;
if (!strcasecmp(args.resultnum, "getnum")) {
snprintf(buf, len, "%u", erds->context->naptr_rrs_count);
res = 0;
goto finish;
}
if (sscanf(args.resultnum, "%u", &num) != 1) {
ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to ENUMRESULT!\n", args.resultnum);
goto finish;
}
if (!num || num > erds->context->naptr_rrs_count) {
ast_log(LOG_WARNING, "Result number %u is not valid for ENUM query results for ID %s!\n", num, args.id);
goto finish;
}
for (k = 0; k < erds->context->naptr_rrs_count; k++) {
if (num - 1 != erds->context->naptr_rrs[k].sort_pos)
continue;
p = strchr(erds->context->naptr_rrs[k].result, ':');
if (p && strcasecmp(erds->context->naptr_rrs[k].tech, "ALL"))
ast_copy_string(buf, p + 1, len);
else
ast_copy_string(buf, erds->context->naptr_rrs[k].result, len);
break;
}
res = 0;
finish:
return res;
}
static struct ast_custom_function enum_query_function = {
.name = "ENUMQUERY",
.synopsis = "Initiate an ENUM query",
Tilghman Lesher
committed
.syntax = "ENUMQUERY(number[,Method-type[,zone-suffix]])",
.desc = "This will do a ENUM lookup of the given phone number.\n"
"If no method-tpye is given, the default will be sip. If no\n"
"zone-suffix is given, the default will be \"e164.arpa\".\n"
"The result of this function will be a numeric ID that can\n"
"be used to retrieve the results using the ENUMRESULT function.\n",
.read = enum_query_read,
};
static struct ast_custom_function enum_result_function = {
.name = "ENUMRESULT",
.synopsis = "Retrieve results from a ENUMQUERY",
Tilghman Lesher
committed
.syntax = "ENUMRESULT(id,resultnum)",
.desc = "This function will retrieve results from a previous use\n"
"of the ENUMQUERY function.\n"
" id - This argument is the identifier returned by the ENUMQUERY function.\n"
" resultnum - This is the number of the result that you want to retrieve.\n"
" Results start at 1. If this argument is specified as \"getnum\",\n"
" then it will return the total number of results that are available.\n",
.read = enum_result_read,
};
static struct ast_custom_function enum_function = {
.name = "ENUMLOOKUP",
.synopsis =
"General or specific querying of NAPTR records for ENUM or ENUM-like DNS pointers",
Tilghman Lesher
committed
"ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])",
.desc =
"Option 'c' returns an integer count of the number of NAPTRs of a certain RR type.\n"
"Combination of 'c' and Method-type of 'ALL' will return a count of all NAPTRs for the record.\n"
Brett Bryant
committed
"Option 'u' returns the full URI and does not strip off the URI-scheme.\n"
"Option 's' triggers ISN specific rewriting\n"
"Option 'i' looks for branches into an Infrastructure ENUM tree\n"
"Option 'd' for a direct DNS lookup without any flipping of digits\n"
"Defaults are: Method-type=sip, no options, record=1, zone-suffix=e164.arpa\n\n"
"For more information, see doc/asterisk.pdf",
Kevin P. Fleming
committed
};
static int function_txtcidname(struct ast_channel *chan, const char *cmd,
Russell Bryant
committed
{
int res;
Brett Bryant
committed
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(number);
AST_APP_ARG(zone);
);
Russell Bryant
committed
buf[0] = '\0';
Brett Bryant
committed
ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n");
return -1;
}
AST_STANDARD_APP_ARGS(args, data);
if (args.argc < 1) {
ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n");
Russell Bryant
committed
}
Brett Bryant
committed
if (!args.zone) {
args.zone = "e164.arpa";
}
Brett Bryant
committed
res = ast_get_txt(chan, args.number, buf, len, args.zone);
Russell Bryant
committed
}
static struct ast_custom_function txtcidname_function = {
.name = "TXTCIDNAME",
.synopsis = "TXTCIDNAME looks up a caller name via DNS",
Brett Bryant
committed
.syntax = "TXTCIDNAME(<number>[,zone-suffix])",
.desc =
"This function looks up the given phone number in DNS to retrieve\n"
"the caller id name. The result will either be blank or be the value\n"
Brett Bryant
committed
"found in the TXT record in DNS. The default zone-suffix is e164.arpa.\n",
Russell Bryant
committed
};
static int unload_module(void)
Kevin P. Fleming
committed
{
int res = 0;
res |= ast_custom_function_unregister(&enum_result_function);
res |= ast_custom_function_unregister(&enum_query_function);
res |= ast_custom_function_unregister(&enum_function);
res |= ast_custom_function_unregister(&txtcidname_function);
return res;
Kevin P. Fleming
committed
}
static int load_module(void)
Kevin P. Fleming
committed
{
int res = 0;
res |= ast_custom_function_register(&enum_result_function);
res |= ast_custom_function_register(&enum_query_function);
res |= ast_custom_function_register(&enum_function);
res |= ast_custom_function_register(&txtcidname_function);
Russell Bryant
committed
return res;
Kevin P. Fleming
committed
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ENUM related dialplan functions");