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
31 * - NAPTR records: http://ietf.nri.reston.va.us/rfc/rfc2915.txt
32 * - DNS SRV records: http://www.ietf.org/rfc/rfc2782.txt
33 * - ENUM http://www.ietf.org/rfc/rfc3761.txt
34 * - ENUM for H.323: http://www.ietf.org/rfc/rfc3762.txt
35 * - ENUM SIP: http://www.ietf.org/rfc/rfc3764.txt
36 * - IANA ENUM Services: http://www.iana.org/assignments/enum-services
39 * http://tools.ietf.org/wg/enum/draft-ietf-enum-combined/
40 * http://tools.ietf.org/wg/enum/draft-ietf-enum-branch-location-record/
42 * \par Possible improvement
43 * \todo Implement a caching mechanism for multile enum lookups
44 * - See http://bugs.digium.com/view.php?id=6739
45 * \todo The service type selection needs to be redone.
50 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/nameser.h>
56 #if __APPLE_CC__ >= 1495
57 #include <arpa/nameser_compat.h>
64 #include "asterisk/enum.h"
65 #include "asterisk/dns.h"
66 #include "asterisk/channel.h"
67 #include "asterisk/config.h"
68 #include "asterisk/utils.h"
69 #include "asterisk/manager.h"
81 static char ienum_branchlabel
[32] = "i";
82 /* how to do infrastructure enum branch location resolution? */
83 #define ENUMLOOKUP_BLR_CC 0
84 #define ENUMLOOKUP_BLR_TXT 1
85 #define ENUMLOOKUP_BLR_EBL 2
86 static int ebl_alg
= ENUMLOOKUP_BLR_CC
;
88 /* EBL record provisional type code */
91 AST_MUTEX_DEFINE_STATIC(enumlock
);
93 /*! \brief Determine the length of a country code when given an E.164 string */
95 * Input: E.164 number w/o leading +
97 * Output: number of digits in the country code
101 * 3 digits is the default length of a country code.
102 * country codes 1 and 7 are a single digit.
103 * the following country codes are two digits: 20, 27, 30-34, 36, 39,
104 * 40, 41, 43-49, 51-58, 60-66, 81, 82, 84, 86, 90-95, 98.
106 static int cclen(const char *number
)
111 if (!number
|| (strlen(number
) < 3)) {
115 strncpy(digits
, number
, 2);
117 if (!sscanf(digits
, "%d", &cc
)) {
121 if (cc
/ 10 == 1 || cc
/ 10 == 7)
124 if (cc
== 20 || cc
== 27 || (cc
>= 30 && cc
<= 34) || cc
== 36 ||
125 cc
== 39 || cc
== 40 || cc
== 41 || (cc
>= 40 && cc
<= 41) ||
126 (cc
>= 43 && cc
<= 49) || (cc
>= 51 && cc
<= 58) ||
127 (cc
>= 60 && cc
<= 66) || cc
== 81 || cc
== 82 || cc
== 84 ||
128 cc
== 86 || (cc
>= 90 && cc
<= 95) || cc
== 98) {
136 char txt
[1024]; /* TXT record in TXT lookup */
137 int txtlen
; /* Length */
140 /*! \brief Callback for TXT record lookup, /ol version */
141 static int txt_callback(void *context
, unsigned char *answer
, int len
, unsigned char *fullanswer
)
143 struct txt_context
*c
= context
;
146 c
->txt
[0] = 0; /* default to empty */
149 if (answer
== NULL
) {
155 * <character-string> is a single length octet followed by that number of characters.
156 * TXT-DATA One or more <character-string>s.
158 * We only take the first string here.
164 if (i
> len
) { /* illegal packet */
165 ast_log(LOG_WARNING
, "txt_callback: malformed TXT record.\n");
169 if (i
>= sizeof(c
->txt
)) { /* too long? */
170 ast_log(LOG_WARNING
, "txt_callback: TXT record too long.\n");
171 i
= sizeof(c
->txt
) - 1;
174 ast_copy_string(c
->txt
, (char *)answer
, i
+ 1); /* this handles the \0 termination */
180 /*! \brief Determine the branch location record as stored in a TXT record */
184 * Output: number of digits in the number before the i-enum branch
186 * Algorithm: Build <ienum_branchlabel>.c.c.<suffix> and look for a TXT lookup.
187 * Return atoi(TXT-record).
188 * Return -1 on not found.
191 static int blr_txt(const char *cc
, const char *suffix
)
193 struct txt_context context
;
194 char domain
[128] = "";
198 ast_mutex_lock(&enumlock
);
200 ast_verb(4, "blr_txt() cc='%s', suffix='%s', c_bl='%s'\n", cc
, suffix
, ienum_branchlabel
);
202 if (sizeof(domain
) < (strlen(cc
) * 2 + strlen(ienum_branchlabel
) + strlen(suffix
) + 2)) {
203 ast_mutex_unlock(&enumlock
);
204 ast_log(LOG_WARNING
, "ERROR: string sizing in blr_txt.\n");
208 p1
= domain
+ snprintf(domain
, sizeof(domain
), "%s.", ienum_branchlabel
);
209 ast_mutex_unlock(&enumlock
);
211 for (p2
= (char *) cc
+ strlen(cc
) - 1; p2
>= cc
; p2
--) {
219 ast_verb(4, "blr_txt() FQDN for TXT record: %s, cc was %s\n", domain
, cc
);
221 ret
= ast_search_dns(&context
, domain
, C_IN
, T_TXT
, txt_callback
);
224 ret
= atoi(context
.txt
);
226 if ((ret
>= 0) && (ret
< 20)) {
227 ast_verb(3, "blr_txt() BLR TXT record for %s is %d (apex: %s)\n", cc
, ret
, suffix
);
232 ast_verb(3, "blr_txt() BLR TXT record for %s not found (apex: %s)\n", cc
, suffix
);
239 char separator
[256]; /* label to insert */
240 int sep_len
; /* Length */
241 char apex
[256]; /* new Apex */
242 int apex_len
; /* Length */
245 /*! \brief Callback for EBL record lookup */
246 static int ebl_callback(void *context
, unsigned char *answer
, int len
, unsigned char *fullanswer
)
248 struct ebl_context
*c
= context
;
251 c
->pos
= 0; /* default to empty */
257 if (answer
== NULL
) {
261 /* draft-lendl-enum-branch-location-record-00
264 * +--+--+--+--+--+--+--+--+
266 * +--+--+--+--+--+--+--+--+
268 * +--+--+--+--+--+--+--+--+
270 * +--+--+--+--+--+--+--+--+
272 * where POSITION is a single byte, SEPARATOR is a <character-string>
273 * and APEX is a <domain-name>.
280 if ((c
->pos
> 15) || len
< 2) { /* illegal packet */
281 ast_log(LOG_WARNING
, "ebl_callback: malformed EBL record.\n");
287 if (i
> len
) { /* illegal packet */
288 ast_log(LOG_WARNING
, "ebl_callback: malformed EBL record.\n");
292 ast_copy_string(c
->separator
, (char *)answer
, i
+ 1);
298 if ((i
= dn_expand((unsigned char *)fullanswer
, (unsigned char *)answer
+ len
,
299 (unsigned char *)answer
, c
->apex
, sizeof(c
->apex
) - 1)) < 0) {
300 ast_log(LOG_WARNING
, "Failed to expand hostname\n");
309 /*! \brief Evaluate the I-ENUM branch as stored in an EBL record */
313 * Output: number of digits in the number before the i-enum branch
315 * Algorithm: Build <ienum_branchlabel>.c.c.<suffix> and look for an EBL record
316 * Return pos and fill in separator and apex.
317 * Return -1 on not found.
320 static int blr_ebl(const char *cc
, const char *suffix
, char *separator
, int sep_len
, char* apex
, int apex_len
)
322 struct ebl_context context
;
323 char domain
[128] = "";
327 ast_mutex_lock(&enumlock
);
329 ast_verb(4, "blr_ebl() cc='%s', suffix='%s', c_bl='%s'\n", cc
, suffix
, ienum_branchlabel
);
331 if (sizeof(domain
) < (strlen(cc
) * 2 + strlen(ienum_branchlabel
) + strlen(suffix
) + 2)) {
332 ast_mutex_unlock(&enumlock
);
333 ast_log(LOG_WARNING
, "ERROR: string sizing in blr_EBL.\n");
337 p1
= domain
+ snprintf(domain
, sizeof(domain
), "%s.", ienum_branchlabel
);
338 ast_mutex_unlock(&enumlock
);
340 for (p2
= (char *) cc
+ strlen(cc
) - 1; p2
>= cc
; p2
--) {
348 ast_verb(4, "blr_ebl() FQDN for EBL record: %s, cc was %s\n", domain
, cc
);
350 ret
= ast_search_dns(&context
, domain
, C_IN
, T_EBL
, ebl_callback
);
354 if ((ret
>= 0) && (ret
< 20)) {
355 ast_verb(3, "blr_txt() BLR EBL record for %s is %d/%s/%s)\n", cc
, ret
, context
.separator
, context
.apex
);
356 ast_copy_string(separator
, context
.separator
, sep_len
);
357 ast_copy_string(apex
, context
.apex
, apex_len
);
361 ast_verb(3, "blr_txt() BLR EBL record for %s not found (apex: %s)\n", cc
, suffix
);
365 /*! \brief Parse NAPTR record information elements */
366 static unsigned int parse_ie(char *data
, unsigned int maxdatalen
, unsigned char *src
, unsigned int srclen
)
368 unsigned int len
, olen
;
370 len
= olen
= (unsigned int) src
[0];
375 ast_log(LOG_WARNING
, "ENUM parsing failed: Wanted %d characters, got %d\n", len
, srclen
);
379 if (len
> maxdatalen
)
381 memcpy(data
, src
, len
);
386 /*! \brief Parse DNS NAPTR record used in ENUM ---*/
387 static int parse_naptr(unsigned char *dst
, int dstsize
, char *tech
, int techsize
, unsigned char *answer
, int len
, unsigned char *naptrinput
)
389 char tech_return
[80];
390 char *oanswer
= (char *)answer
;
391 char flags
[512] = "";
392 char services
[512] = "";
394 char regexp
[512] = "";
397 char errbuff
[512] = "";
400 char *pattern
, *subst
, *d
, *number
;
404 int d_len
= sizeof(temp
) - 1;
406 regmatch_t pmatch
[9];
408 tech_return
[0] = '\0';
411 if (len
< sizeof(struct naptr
)) {
412 ast_log(LOG_WARNING
, "NAPTR record length too short\n");
415 answer
+= sizeof(struct naptr
);
416 len
-= sizeof(struct naptr
);
417 if ((res
= parse_ie(flags
, sizeof(flags
) - 1, answer
, len
)) < 0) {
418 ast_log(LOG_WARNING
, "Failed to get flags from NAPTR record\n");
425 if ((res
= parse_ie(services
, sizeof(services
) - 1, answer
, len
)) < 0) {
426 ast_log(LOG_WARNING
, "Failed to get services from NAPTR record\n");
432 if ((res
= parse_ie(regexp
, sizeof(regexp
) - 1, answer
, len
)) < 0) {
433 ast_log(LOG_WARNING
, "Failed to get regexp from NAPTR record\n");
440 if ((res
= dn_expand((unsigned char *)oanswer
, (unsigned char *)answer
+ len
, (unsigned char *)answer
, repl
, sizeof(repl
) - 1)) < 0) {
441 ast_log(LOG_WARNING
, "Failed to expand hostname\n");
445 ast_debug(3, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
446 naptrinput
, flags
, services
, regexp
, repl
);
449 if (tolower(flags
[0]) != 'u') {
450 ast_log(LOG_WARNING
, "NAPTR Flag must be 'U' or 'u'.\n");
454 p
= strstr(services
, "e2u+");
456 p
= strstr(services
, "E2U+");
460 p
= strchr(p
, ':') + 1;
462 ast_copy_string(tech_return
, p
, sizeof(tech_return
));
465 p
= strstr(services
, "+e2u");
467 p
= strstr(services
, "+E2U");
470 p
= strchr(services
, ':');
473 ast_copy_string(tech_return
, services
, sizeof(tech_return
));
478 ast_copy_string(regexp, "!^\\+43(.*)$!\\1@bla.fasel!", sizeof(regexp) - 1);
481 regexp_len
= strlen(regexp
);
482 if (regexp_len
< 7) {
483 ast_log(LOG_WARNING
, "Regex too short to be meaningful.\n");
489 delim2
= strchr(regexp
+ 1, delim
);
490 if ((delim2
== NULL
) || (regexp
[regexp_len
- 1] != delim
)) {
491 ast_log(LOG_WARNING
, "Regex delimiter error (on \"%s\").\n", regexp
);
495 pattern
= regexp
+ 1;
498 regexp
[regexp_len
- 1] = 0;
501 * now do the regex wizardry.
504 if (regcomp(&preg
, pattern
, REG_EXTENDED
| REG_NEWLINE
)) {
505 ast_log(LOG_WARNING
, "NAPTR Regex compilation error (regex = \"%s\").\n", regexp
);
509 if (preg
.re_nsub
> 9) {
510 ast_log(LOG_WARNING
, "NAPTR Regex compilation error: too many subs.\n");
515 if (0 != (rc
= regexec(&preg
, (char *)naptrinput
, 0, pmatch
, 0))) {
516 regerror(rc
, &preg
, errbuff
, sizeof(errbuff
));
517 ast_log(LOG_WARNING
, "NAPTR Regex match failed. Reason: %s\n", errbuff
);
525 number
= (char *)(naptrinput
+ (*naptrinput
== '+'));
528 while (*subst
&& (d_len
> 0)) {
529 if ((subst
[0] == '\\' && isdigit(subst
[1]))) {
530 size
= strlen(number
);
531 //ast_log(LOG_WARNING, "size:%d: offset:%s: temp:%s:\n",size,offset,temp);
533 ast_log(LOG_WARNING
, "Not enough space during NAPTR regex substitution.\n");
536 memcpy(d
, number
, size
);
540 //ast_log(LOG_WARNING, "after dlen:%d: temp:%s:\n",d_len,temp);
541 } else if (isprint(*subst
)) {
545 ast_log(LOG_WARNING
, "Error during regex substitution.\n");
550 ast_copy_string((char *)dst
, temp
,dstsize
);
551 dst
[dstsize
- 1] = '\0';
552 // ast_log(LOG_WARNING, "after dst:%s: temp:%s:\n",dst,temp);
554 if (*tech
!= '\0'){ /* check if it is requested NAPTR */
555 if (!strncasecmp(tech
, "ALL", techsize
)){
556 return 0; /* return or count any RR */
558 if (!strncasecmp(tech_return
, tech
, sizeof(tech_return
) < techsize
? sizeof(tech_return
): techsize
)){
559 ast_copy_string(tech
, tech_return
, techsize
);
560 return 0; /* we got our RR */
561 } else { /* go to the next RR in the DNS answer */
566 /* tech was not specified, return first parsed RR */
567 ast_copy_string(tech
, tech_return
, techsize
);
572 /* do not return requested value, just count RRs and return thei number in dst */
573 #define ENUMLOOKUP_OPTIONS_COUNT 1
574 /* do an ISN style lookup */
575 #define ENUMLOOKUP_OPTIONS_ISN 2
576 /* do a infrastructure ENUM lookup */
577 #define ENUMLOOKUP_OPTIONS_IENUM 4
578 /* do a direct DNS lookup: no reversal */
579 #define ENUMLOOKUP_OPTIONS_DIRECT 8
581 /*! \brief Callback from ENUM lookup function */
582 static int enum_callback(void *context
, unsigned char *answer
, int len
, unsigned char *fullanswer
)
584 struct enum_context
*c
= context
;
588 res
= parse_naptr((unsigned char *)c
->dst
, c
->dstlen
, c
->tech
, c
->techlen
, answer
, len
, (unsigned char *)c
->naptrinput
);
591 ast_log(LOG_WARNING
, "Failed to parse naptr\n");
593 } else if ((res
== 0) && !ast_strlen_zero(c
->dst
)) { /* ok, we got needed NAPTR */
594 if (c
->options
& ENUMLOOKUP_OPTIONS_COUNT
) { /* counting RRs */
596 snprintf(c
->dst
, c
->dstlen
, "%d", c
->count
);
598 if ((p
= ast_realloc(c
->naptr_rrs
, sizeof(*c
->naptr_rrs
) * (c
->naptr_rrs_count
+ 1)))) {
600 memcpy(&c
->naptr_rrs
[c
->naptr_rrs_count
].naptr
, answer
, sizeof(c
->naptr_rrs
->naptr
));
601 c
->naptr_rrs
[c
->naptr_rrs_count
].result
= ast_strdup(c
->dst
);
602 c
->naptr_rrs
[c
->naptr_rrs_count
].tech
= ast_strdup(c
->tech
);
603 c
->naptr_rrs
[c
->naptr_rrs_count
].sort_pos
= c
->naptr_rrs_count
;
604 c
->naptr_rrs_count
++;
615 int ast_get_enum(struct ast_channel
*chan
, const char *number
, char *dst
, int dstlen
, char *tech
, int techlen
, char* suffix
, char* options
, unsigned int record
, struct enum_context
**argcontext
)
617 struct enum_context
*context
;
622 char naptrinput
[128];
625 /* for ISN rewrite */
633 struct timeval time_start
, time_end
;
635 if (ast_strlen_zero(suffix
)) {
636 ast_log(LOG_WARNING
, "ast_get_enum need a suffix parameter now.\n");
640 ast_verb(2, "ast_get_enum(num='%s', tech='%s', suffix='%s', options='%s', record=%d\n", number
, tech
, suffix
, options
, record
);
643 We don't need that any more, that "n" preceding the number has been replaced by a flag
644 in the options paramter.
645 ast_copy_string(naptrinput, number, sizeof(naptrinput));
648 * The "number" parameter includes a leading '+' if it's a full E.164 number (and not ISN)
649 * We need to preserve that as the regex inside NAPTRs expect the +.
651 * But for the domain generation, the '+' is a nuissance, so we get rid of it.
653 ast_copy_string(naptrinput
, number
[0] == 'n' ? number
+ 1 : number
, sizeof(naptrinput
));
654 if (number
[0] == '+') {
658 if (!(context
= ast_calloc(1, sizeof(*context
))))
661 if((p3
= strchr(naptrinput
, '*'))) {
665 context
->naptrinput
= naptrinput
; /* The number */
666 context
->dst
= dst
; /* Return string */
667 context
->dstlen
= dstlen
;
668 context
->tech
= tech
;
669 context
->techlen
= techlen
;
670 context
->options
= 0;
671 context
->position
= record
> 0 ? record
: 1;
673 context
->naptr_rrs
= NULL
;
674 context
->naptr_rrs_count
= 0;
679 * c Return count, not URI
680 * i Use infrastructure ENUM
681 * s Do ISN transformation
682 * d Direct DNS query: no reversing.
685 if (options
!= NULL
) {
686 if (strchr(options
,'s')) {
687 context
->options
|= ENUMLOOKUP_OPTIONS_ISN
;
688 } else if (strchr(options
,'i')) {
689 context
->options
|= ENUMLOOKUP_OPTIONS_IENUM
;
690 } else if (strchr(options
,'d')) {
691 context
->options
|= ENUMLOOKUP_OPTIONS_DIRECT
;
693 if (strchr(options
,'c')) {
694 context
->options
|= ENUMLOOKUP_OPTIONS_COUNT
;
696 if (strchr(number
,'*')) {
697 context
->options
|= ENUMLOOKUP_OPTIONS_ISN
;
700 ast_verb(2, "ENUM options(%s): pos=%d, options='%d'\n", options
, context
->position
, context
->options
);
701 ast_debug(1, "ast_get_enum(): n='%s', tech='%s', suffix='%s', options='%d', record='%d'\n",
702 number
, tech
, suffix
, context
->options
, context
->position
);
705 * This code does more than simple RFC3261 ENUM. All these rewriting
706 * schemes have in common that they build the FQDN for the NAPTR lookup
708 * - a number which needs be flipped and "."-seperated (left)
709 * - some fixed string (middle)
712 * The RFC3261 ENUM is: left=full number, middle="", apex=from args.
713 * ISN: number = "middle*left", apex=from args
714 * I-ENUM: EBL parameters build the split, can change apex
715 * Direct: left="", middle=argument, apex=from args
719 /* default: the whole number will be flipped, no middle domain component */
720 ast_copy_string(left
, number
, sizeof(left
));
723 * I-ENUM can change the apex, thus we copy it
725 ast_copy_string(apex
, suffix
, sizeof(apex
));
727 if ((context
->options
& ENUMLOOKUP_OPTIONS_ISN
) && (p1
= strchr(number
, '*'))) {
729 ast_copy_string(left
, number
, sizeof(left
));
730 ast_copy_string(middle
, p1
, sizeof(middle
) - 1);
733 ast_verb(2, "ISN ENUM: left=%s, middle='%s'\n", left
, middle
);
734 /* Direct DNS lookup rewrite */
735 } else if (context
->options
& ENUMLOOKUP_OPTIONS_DIRECT
) {
736 left
[0] = 0; /* nothing to flip around */
737 ast_copy_string(middle
, number
, sizeof(middle
) - 1);
740 ast_verb(2, "DIRECT ENUM: middle='%s'\n", middle
);
741 /* Infrastructure ENUM rewrite */
742 } else if (context
->options
& ENUMLOOKUP_OPTIONS_IENUM
) {
745 char sep
[256], n_apex
[256];
746 int cc_len
= cclen(number
);
748 ast_mutex_lock(&enumlock
);
749 ast_copy_string(sep
, ienum_branchlabel
, sizeof(sep
)); /* default */
750 ast_mutex_unlock(&enumlock
);
753 case ENUMLOOKUP_BLR_EBL
:
754 ast_copy_string(cc
, number
, cc_len
); /* cclen() never returns more than 3 */
755 sdl
= blr_ebl(cc
, suffix
, sep
, sizeof(sep
) - 1, n_apex
, sizeof(n_apex
) - 1);
758 ast_copy_string(apex
, n_apex
, sizeof(apex
));
759 ast_verb(2, "EBL ENUM: sep=%s, apex='%s'\n", sep
, n_apex
);
764 case ENUMLOOKUP_BLR_TXT
:
765 ast_copy_string(cc
, number
, cc_len
); /* cclen() never returns more than 3 */
766 sdl
= blr_txt(cc
, suffix
);
772 case ENUMLOOKUP_BLR_CC
: /* BLR is at the country-code level */
778 if (sdl
> strlen(number
)) { /* Number too short for this sdl? */
779 ast_log(LOG_WARNING
, "I-ENUM: subdomain location %d behind number %s\n", sdl
, number
);
782 ast_copy_string(left
, number
+ sdl
, sizeof(left
));
784 ast_mutex_lock(&enumlock
);
785 ast_copy_string(middle
, sep
, sizeof(middle
) - 1);
787 ast_mutex_unlock(&enumlock
);
789 /* check the space we need for middle */
790 if ((sdl
* 2 + strlen(middle
) + 2) > sizeof(middle
)) {
791 ast_log(LOG_WARNING
, "ast_get_enum: not enough space for I-ENUM rewrite.\n");
795 p1
= middle
+ strlen(middle
);
796 for (p2
= (char *) number
+ sdl
- 1; p2
>= number
; p2
--) {
804 ast_verb(2, "I-ENUM: cclen=%d, left=%s, middle='%s', apex='%s'\n", cc_len
, left
, middle
, apex
);
807 if (strlen(left
) * 2 + 2 > sizeof(domain
)) {
808 ast_log(LOG_WARNING
, "string to long in ast_get_enum\n");
812 /* flip left into domain */
814 for (p2
= left
+ strlen(left
); p2
>= left
; p2
--) {
822 if (chan
&& ast_autoservice_start(chan
) < 0) {
827 spaceleft
= sizeof(tmp
) - 2;
828 ast_copy_string(tmp
, domain
, spaceleft
);
829 spaceleft
-= strlen(domain
);
832 strncat(tmp
, middle
, spaceleft
);
833 spaceleft
-= strlen(middle
);
836 strncat(tmp
,apex
,spaceleft
);
837 time_start
= ast_tvnow();
838 ret
= ast_search_dns(context
, tmp
, C_IN
, T_NAPTR
, enum_callback
);
839 time_end
= ast_tvnow();
841 ast_verb(2, "ast_get_enum() profiling: %s, %s, %d ms\n",
842 (ret
== 0) ? "OK" : "FAIL", tmp
, ast_tvdiff_ms(time_end
, time_start
));
845 ast_debug(1, "No such number found: %s (%s)\n", tmp
, strerror(errno
));
850 if (context
->naptr_rrs_count
>= context
->position
&& ! (context
->options
& ENUMLOOKUP_OPTIONS_COUNT
)) {
851 /* sort array by NAPTR order/preference */
852 for (k
= 0; k
< context
->naptr_rrs_count
; k
++) {
853 for (i
= 0; i
< context
->naptr_rrs_count
; i
++) {
854 /* use order first and then preference to compare */
855 if ((ntohs(context
->naptr_rrs
[k
].naptr
.order
) < ntohs(context
->naptr_rrs
[i
].naptr
.order
)
856 && context
->naptr_rrs
[k
].sort_pos
> context
->naptr_rrs
[i
].sort_pos
)
857 || (ntohs(context
->naptr_rrs
[k
].naptr
.order
) > ntohs(context
->naptr_rrs
[i
].naptr
.order
)
858 && context
->naptr_rrs
[k
].sort_pos
< context
->naptr_rrs
[i
].sort_pos
)) {
859 z
= context
->naptr_rrs
[k
].sort_pos
;
860 context
->naptr_rrs
[k
].sort_pos
= context
->naptr_rrs
[i
].sort_pos
;
861 context
->naptr_rrs
[i
].sort_pos
= z
;
864 if (ntohs(context
->naptr_rrs
[k
].naptr
.order
) == ntohs(context
->naptr_rrs
[i
].naptr
.order
)) {
865 if ((ntohs(context
->naptr_rrs
[k
].naptr
.pref
) < ntohs(context
->naptr_rrs
[i
].naptr
.pref
)
866 && context
->naptr_rrs
[k
].sort_pos
> context
->naptr_rrs
[i
].sort_pos
)
867 || (ntohs(context
->naptr_rrs
[k
].naptr
.pref
) > ntohs(context
->naptr_rrs
[i
].naptr
.pref
)
868 && context
->naptr_rrs
[k
].sort_pos
< context
->naptr_rrs
[i
].sort_pos
)) {
869 z
= context
->naptr_rrs
[k
].sort_pos
;
870 context
->naptr_rrs
[k
].sort_pos
= context
->naptr_rrs
[i
].sort_pos
;
871 context
->naptr_rrs
[i
].sort_pos
= z
;
876 for (k
= 0; k
< context
->naptr_rrs_count
; k
++) {
877 if (context
->naptr_rrs
[k
].sort_pos
== context
->position
- 1) {
878 ast_copy_string(context
->dst
, context
->naptr_rrs
[k
].result
, dstlen
);
879 ast_copy_string(context
->tech
, context
->naptr_rrs
[k
].tech
, techlen
);
883 } else if (!(context
->options
& ENUMLOOKUP_OPTIONS_COUNT
)) {
885 } else if ((context
->options
& ENUMLOOKUP_OPTIONS_COUNT
)) {
886 snprintf(context
->dst
,context
->dstlen
,"%d",context
->count
);
890 ret
|= ast_autoservice_stop(chan
);
893 for (k
= 0; k
< context
->naptr_rrs_count
; k
++) {
894 ast_free(context
->naptr_rrs
[k
].result
);
895 ast_free(context
->naptr_rrs
[k
].tech
);
897 ast_free(context
->naptr_rrs
);
900 *argcontext
= context
;
905 /*!\brief Get TXT record from DNS.
906 * Really has nothing to do with enum, but anyway...
908 * Actually, there is now an internet-draft which describes how callerID should
909 * be stored in ENUM domains: draft-ietf-enum-cnam-04.txt
911 * The algorithm implemented here will thus be obsolete soon.
913 int ast_get_txt(struct ast_channel
*chan
, const char *number
, char *txt
, int txtlen
, char *suffix
)
915 struct txt_context context
;
917 int pos
= strlen(number
) - 1;
921 ast_debug(4, "ast_get_txt: Number = '%s', suffix = '%s'\n", number
, suffix
);
923 if (chan
&& ast_autoservice_start(chan
) < 0) {
932 if (isdigit(number
[pos
])) {
933 tmp
[newpos
++] = number
[pos
];
939 ast_copy_string(&tmp
[newpos
], suffix
, sizeof(tmp
) - newpos
);
942 ast_debug(2, "No such number found in ENUM: %s (%s)\n", tmp
, strerror(errno
));
945 ast_copy_string(txt
, context
.txt
, txtlen
);
948 ret
|= ast_autoservice_stop(chan
);
953 /*! \brief Initialize the ENUM support subsystem */
954 static int private_enum_init(int reload
)
956 struct ast_config
*cfg
;
958 struct ast_flags config_flags
= { reload
? CONFIG_FLAG_FILEUNCHANGED
: 0 };
960 if ((cfg
= ast_config_load2("enum.conf", "enum", config_flags
)) == CONFIG_STATUS_FILEUNCHANGED
)
963 /* Destroy existing list */
964 ast_mutex_lock(&enumlock
);
966 if ((string
= ast_variable_retrieve(cfg
, "ienum", "branchlabel"))) {
967 ast_copy_string(ienum_branchlabel
, string
, sizeof(ienum_branchlabel
));
970 if ((string
= ast_variable_retrieve(cfg
, "ienum", "ebl_alg"))) {
971 ebl_alg
= ENUMLOOKUP_BLR_CC
; /* default */
973 if (!strcasecmp(string
, "txt"))
974 ebl_alg
= ENUMLOOKUP_BLR_TXT
;
975 else if (!strcasecmp(string
, "ebl"))
976 ebl_alg
= ENUMLOOKUP_BLR_EBL
;
977 else if (!strcasecmp(string
, "cc"))
978 ebl_alg
= ENUMLOOKUP_BLR_CC
;
980 ast_log(LOG_WARNING
, "No valid parameter for ienum/ebl_alg.\n");
982 ast_config_destroy(cfg
);
984 ast_mutex_unlock(&enumlock
);
985 manager_event(EVENT_FLAG_SYSTEM
, "Reload", "Module: Enum\r\nStatus: Enabled\r\nMessage: ENUM reload Requested\r\n");
989 int ast_enum_init(void)
991 return private_enum_init(0);
994 int ast_enum_reload(void)
996 return private_enum_init(1);