2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Funding provided by nic.at
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * \brief ENUM Support for Asterisk
25 * \author Mark Spencer <markster@digium.com>
27 * \arg Funding provided by nic.at
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
34 #if __APPLE_CC__ >= 1495
35 #include <arpa/nameser_compat.h>
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include "asterisk/logger.h"
50 #include "asterisk/options.h"
51 #include "asterisk/enum.h"
52 #include "asterisk/dns.h"
53 #include "asterisk/channel.h"
54 #include "asterisk/config.h"
55 #include "asterisk/utils.h"
67 #define TOPLEV "e164.arpa." /*!< The IETF Enum standard root, managed by the ITU */
69 /* Linked list from config file */
70 static struct enum_search {
72 struct enum_search *next;
75 static int enumver = 0;
77 AST_MUTEX_DEFINE_STATIC(enumlock);
82 } __attribute__ ((__packed__));
84 /*! \brief Parse NAPTR record information elements */
85 static int parse_ie(char *data, int maxdatalen, char *src, int srclen)
89 len = olen = (int)src[0];
93 ast_log(LOG_WARNING, "Want %d, got %d\n", len, srclen);
98 memcpy(data, src, len);
102 /*! \brief Parse DNS NAPTR record used in ENUM ---*/
103 static int parse_naptr(char *dst, int dstsize, char *tech, int techsize, char *answer, int len, char *naptrinput)
106 char tech_return[80];
107 char *oanswer = answer;
108 char flags[512] = "";
109 char services[512] = "";
111 char regexp[512] = "";
116 char *pattern, *subst, *d;
118 int regexp_len, size, backref;
119 int d_len = sizeof(temp) - 1;
121 regmatch_t pmatch[9];
123 tech_return[0] = '\0';
127 if (len < sizeof(struct naptr)) {
128 ast_log(LOG_WARNING, "NAPTR record length too short\n");
131 answer += sizeof(struct naptr);
132 len -= sizeof(struct naptr);
133 if ((res = parse_ie(flags, sizeof(flags) - 1, answer, len)) < 0) {
134 ast_log(LOG_WARNING, "Failed to get flags from NAPTR record\n");
140 if ((res = parse_ie(services, sizeof(services) - 1, answer, len)) < 0) {
141 ast_log(LOG_WARNING, "Failed to get services from NAPTR record\n");
147 if ((res = parse_ie(regexp, sizeof(regexp) - 1, answer, len)) < 0) {
148 ast_log(LOG_WARNING, "Failed to get regexp from NAPTR record\n");
155 if ((res = dn_expand((unsigned char *)oanswer, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
156 ast_log(LOG_WARNING, "Failed to expand hostname\n");
160 if (option_debug > 2) /* Advanced NAPTR debugging */
161 ast_log(LOG_DEBUG, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
162 naptrinput, flags, services, regexp, repl);
164 if (tolower(flags[0]) != 'u') {
165 ast_log(LOG_WARNING, "NAPTR Flag must be 'U' or 'u'.\n");
169 p = strstr(services, "e2u+");
171 p = strstr(services, "E2U+");
175 p = strchr(p, ':') + 1;
177 ast_copy_string(tech_return, p, sizeof(tech_return));
180 p = strstr(services, "+e2u");
182 p = strstr(services, "+E2U");
185 p = strchr(services, ':');
188 ast_copy_string(tech_return, services, sizeof(tech_return));
193 ast_copy_string(regexp, "!^\\+43(.*)$!\\1@bla.fasel!", sizeof(regexp) - 1);
196 regexp_len = strlen(regexp);
197 if (regexp_len < 7) {
198 ast_log(LOG_WARNING, "Regex too short to be meaningful.\n");
204 delim2 = strchr(regexp + 1, delim);
205 if ((delim2 == NULL) || (regexp[regexp_len-1] != delim)) {
206 ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n",regexp);
210 pattern = regexp + 1;
213 regexp[regexp_len-1] = 0;
216 printf("Pattern: %s\n", pattern);
217 printf("Subst: %s\n", subst);
218 printf("Input: %s\n", naptrinput);
222 * now do the regex wizardry.
225 if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
226 ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n",regexp);
230 if (preg.re_nsub > 9) {
231 ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
236 if (regexec(&preg, naptrinput, 9, pmatch, 0)) {
237 ast_log(LOG_WARNING, "NAPTR Regex match failed.\n");
245 while (*subst && (d_len > 0)) {
246 if ((subst[0] == '\\') && isdigit(subst[1]) && (pmatch[subst[1]-'0'].rm_so != -1)) {
247 backref = subst[1]-'0';
248 size = pmatch[backref].rm_eo - pmatch[backref].rm_so;
250 ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
253 memcpy(d, naptrinput + pmatch[backref].rm_so, size);
257 } else if (isprint(*subst)) {
261 ast_log(LOG_WARNING, "Error during regex substitution.\n");
266 ast_copy_string(dst, temp, dstsize);
267 dst[dstsize - 1] = '\0';
269 if (*tech != '\0'){ /* check if it is requested NAPTR */
270 if (!strncasecmp(tech, "ALL", techsize)){
271 return 1; /* return or count any RR */
273 if (!strncasecmp(tech_return, tech, sizeof(tech_return)<techsize?sizeof(tech_return):techsize)){
274 ast_copy_string(tech, tech_return, techsize);
275 return 1; /* we got out RR */
276 } else { /* go to the next RR in the DNS answer */
281 /* tech was not specified, return first parsed RR */
282 ast_copy_string(tech, tech_return, techsize);
287 /* do not return requested value, just count RRs and return thei number in dst */
288 #define ENUMLOOKUP_OPTIONS_COUNT 1
290 struct enum_naptr_rr {
291 struct naptr naptr; /* order and preference of RR */
292 char *result; /* result of naptr parsing,e.g.: tel:+5553 */
293 char *tech; /* Technology (from URL scheme) */
294 int sort_pos; /* sort position */
297 struct enum_context {
298 char *dst; /* Destination part of URL from ENUM */
299 int dstlen; /* Length */
300 char *tech; /* Technology (from URL scheme) */
301 int techlen; /* Length */
302 char *txt; /* TXT record in TXT lookup */
303 int txtlen; /* Length */
304 char *naptrinput; /* The number to lookup */
305 int position; /* used as counter for RRs or specifies position of required RR */
306 int options; /* options , see ENUMLOOKUP_OPTIONS_* defined above */
307 struct enum_naptr_rr *naptr_rrs; /* array of parsed NAPTR RRs */
308 int naptr_rrs_count; /* Size of array naptr_rrs */
311 /*! \brief Callback for TXT record lookup */
312 static int txt_callback(void *context, char *answer, int len, char *fullanswer)
314 struct enum_context *c = (struct enum_context *)context;
316 printf("ENUMTXT Called\n");
319 if (answer == NULL) {
325 /* skip over first byte, as for some reason it's a vertical tab character */
329 /* answer is not null-terminated, but should be */
330 /* this is safe to do, as answer has extra bytes on the end we can
331 safely overwrite with a null */
333 /* now increment len so that len includes the null, so that we can
334 compare apples to apples */
337 /* finally, copy the answer into c->txt */
338 ast_copy_string(c->txt, answer, len < c->txtlen ? len : (c->txtlen));
340 /* just to be safe, let's make sure c->txt is null terminated */
341 c->txt[(c->txtlen)-1] = '\0';
346 /*! \brief Callback from ENUM lookup function */
347 static int enum_callback(void *context, char *answer, int len, char *fullanswer)
349 struct enum_context *c = context;
353 res = parse_naptr(c->dst, c->dstlen, c->tech, c->techlen, answer, len, c->naptrinput);
356 ast_log(LOG_WARNING, "Failed to parse naptr :(\n");
358 } else if (res > 0 && !ast_strlen_zero(c->dst)){ /* ok, we got needed NAPTR */
359 if (c->options & ENUMLOOKUP_OPTIONS_COUNT){ /* counting RRs */
361 snprintf(c->dst, c->dstlen, "%d", c->position);
363 if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
365 memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
366 /* printf("order=%d, pref=%d\n", ntohs(c->naptr_rrs[c->naptr_rrs_count].naptr.order), ntohs(c->naptr_rrs[c->naptr_rrs_count].naptr.pref)); */
367 c->naptr_rrs[c->naptr_rrs_count].result = strdup(c->dst);
368 c->naptr_rrs[c->naptr_rrs_count].tech = strdup(c->tech);
369 c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
370 c->naptr_rrs_count++;
377 if (c->options & ENUMLOOKUP_OPTIONS_COUNT) { /* counting RRs */
378 snprintf(c->dst, c->dstlen, "%d", c->position);
384 /*! \brief ENUM lookup */
385 int ast_get_enum(struct ast_channel *chan, const char *number, char *dst, int dstlen, char *tech, int techlen, char* suffix, char* options)
387 struct enum_context context;
389 char naptrinput[512];
390 int pos = strlen(number) - 1;
393 struct enum_search *s = NULL;
395 /* for ISN rewrite */
402 if (number[0] == 'n') {
403 strncpy(naptrinput, number+1, sizeof(naptrinput));
405 strncpy(naptrinput, number, sizeof(naptrinput));
408 context.naptrinput = naptrinput; /* The number */
409 context.dst = dst; /* Return string */
410 context.dstlen = dstlen;
412 context.techlen = techlen;
414 context.position = 1;
415 context.naptr_rrs = NULL;
416 context.naptr_rrs_count = 0;
418 if (options != NULL) {
419 if (*options == 'c') {
420 context.options = ENUMLOOKUP_OPTIONS_COUNT;
421 context.position = 0;
423 context.position = atoi(options);
424 if (context.position < 1)
425 context.position = 1;
433 p1 = strchr(number, '*');
435 if (number[0] == 'n') { /* do not perform ISN rewrite ('n' is testing flag) */
437 k = 1; /* strip 'n' from number */
448 while(*p2 && newpos < 128){
457 if (isdigit(number[pos])) {
458 tmp[newpos++] = number[pos];
465 if (chan && ast_autoservice_start(chan) < 0)
469 ast_mutex_lock(&enumlock);
470 if (version != enumver) {
471 /* Ooh, a reload... */
477 if (suffix != NULL) {
478 strncpy(tmp + newpos, suffix, sizeof(tmp) - newpos - 1);
480 strncpy(tmp + newpos, s->toplev, sizeof(tmp) - newpos - 1);
482 ast_mutex_unlock(&enumlock);
485 ret = ast_search_dns(&context, tmp, C_IN, T_NAPTR, enum_callback);
492 ast_log(LOG_DEBUG, "No such number found: %s (%s)\n", tmp, strerror(errno));
497 if (context.naptr_rrs_count >= context.position && ! (context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
498 /* sort array by NAPTR order/preference */
499 for (k=0; k<context.naptr_rrs_count; k++) {
500 for (i=0; i<context.naptr_rrs_count; i++) {
501 /* use order first and then preference to compare */
502 if ((ntohs(context.naptr_rrs[k].naptr.order) < ntohs(context.naptr_rrs[i].naptr.order)
503 && context.naptr_rrs[k].sort_pos > context.naptr_rrs[i].sort_pos)
504 || (ntohs(context.naptr_rrs[k].naptr.order) > ntohs(context.naptr_rrs[i].naptr.order)
505 && context.naptr_rrs[k].sort_pos < context.naptr_rrs[i].sort_pos)){
506 z = context.naptr_rrs[k].sort_pos;
507 context.naptr_rrs[k].sort_pos = context.naptr_rrs[i].sort_pos;
508 context.naptr_rrs[i].sort_pos = z;
511 if (ntohs(context.naptr_rrs[k].naptr.order) == ntohs(context.naptr_rrs[i].naptr.order)) {
512 if ((ntohs(context.naptr_rrs[k].naptr.pref) < ntohs(context.naptr_rrs[i].naptr.pref)
513 && context.naptr_rrs[k].sort_pos > context.naptr_rrs[i].sort_pos)
514 || (ntohs(context.naptr_rrs[k].naptr.pref) > ntohs(context.naptr_rrs[i].naptr.pref)
515 && context.naptr_rrs[k].sort_pos < context.naptr_rrs[i].sort_pos)){
516 z = context.naptr_rrs[k].sort_pos;
517 context.naptr_rrs[k].sort_pos = context.naptr_rrs[i].sort_pos;
518 context.naptr_rrs[i].sort_pos = z;
523 for (k=0; k<context.naptr_rrs_count; k++) {
524 if (context.naptr_rrs[k].sort_pos == context.position-1) {
525 ast_copy_string(context.dst, context.naptr_rrs[k].result, dstlen);
526 ast_copy_string(context.tech, context.naptr_rrs[k].tech, techlen);
530 } else if (!(context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
534 ret |= ast_autoservice_stop(chan);
536 for (k=0; k<context.naptr_rrs_count; k++) {
537 free(context.naptr_rrs[k].result);
538 free(context.naptr_rrs[k].tech);
541 free(context.naptr_rrs);
546 /*! \brief Get TXT record from DNS.
547 Really has nothing to do with enum, but anyway...
549 int ast_get_txt(struct ast_channel *chan, const char *number, char *dst, int dstlen, char *tech, int techlen, char *txt, int txtlen)
551 struct enum_context context;
553 char naptrinput[512] = "+";
554 int pos = strlen(number) - 1;
557 struct enum_search *s = NULL;
560 strncat(naptrinput, number, sizeof(naptrinput) - 2);
562 context.naptrinput = naptrinput;
564 context.dstlen = dstlen;
566 context.techlen = techlen;
568 context.txtlen = txtlen;
573 tmp[newpos++] = number[pos--];
577 if (chan && ast_autoservice_start(chan) < 0)
581 ast_mutex_lock(&enumlock);
582 if (version != enumver) {
583 /* Ooh, a reload... */
590 strncpy(tmp + newpos, s->toplev, sizeof(tmp) - newpos - 1);
592 ast_mutex_unlock(&enumlock);
596 ret = ast_search_dns(&context, tmp, C_IN, T_TXT, txt_callback);
601 if (option_debug > 1)
602 ast_log(LOG_DEBUG, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
606 ret |= ast_autoservice_stop(chan);
610 /*! \brief Add enum tree to linked list */
611 static struct enum_search *enum_newtoplev(char *s)
613 struct enum_search *tmp;
615 if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
616 ast_copy_string(tmp->toplev, s, sizeof(tmp->toplev));
621 /*! \brief Initialize the ENUM support subsystem */
622 int ast_enum_init(void)
624 struct ast_config *cfg;
625 struct enum_search *s, *sl;
626 struct ast_variable *v;
628 /* Destroy existing list */
629 ast_mutex_lock(&enumlock);
637 cfg = ast_config_load("enum.conf");
640 v = ast_variable_browse(cfg, "general");
642 if (!strcasecmp(v->name, "search")) {
643 s = enum_newtoplev(v->value);
654 ast_config_destroy(cfg);
656 toplevs = enum_newtoplev(TOPLEV);
659 ast_mutex_unlock(&enumlock);
663 int ast_enum_reload(void)
665 return ast_enum_init();