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 https://issues.asterisk.org/view.php?id=6739
45 * \todo The service type selection needs to be redone.
48 /*! \li \ref enum.c uses the configuration file \ref enum.conf
49 * \addtogroup configuration_file Configuration Files
53 * \page enum.conf enum.conf
54 * \verbinclude enum.conf.sample
58 <support_level>core</support_level>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/nameser.h>
67 #include <arpa/nameser_compat.h>
73 #include "asterisk/enum.h"
74 #include "asterisk/dns.h"
75 #include "asterisk/channel.h"
76 #include "asterisk/config.h"
77 #include "asterisk/utils.h"
78 #include "asterisk/manager.h"
90 static char ienum_branchlabel[32] = "i";
91 /* how to do infrastructure enum branch location resolution? */
92 #define ENUMLOOKUP_BLR_CC 0
93 #define ENUMLOOKUP_BLR_TXT 1
94 #define ENUMLOOKUP_BLR_EBL 2
95 static int ebl_alg = ENUMLOOKUP_BLR_CC;
97 /* EBL record provisional type code */
100 AST_MUTEX_DEFINE_STATIC(enumlock);
102 /*! \brief Determine the length of a country code when given an E.164 string */
104 * Input: E.164 number w/o leading +
106 * Output: number of digits in the country code
107 * 0 on invalid number
110 * 3 digits is the default length of a country code.
111 * country codes 1 and 7 are a single digit.
112 * the following country codes are two digits: 20, 27, 30-34, 36, 39,
113 * 40, 41, 43-49, 51-58, 60-66, 81, 82, 84, 86, 90-95, 98.
115 static int cclen(const char *number)
120 if (!number || (strlen(number) < 3)) {
124 strncpy(digits, number, 2);
126 if (!sscanf(digits, "%30d", &cc)) {
130 if (cc / 10 == 1 || cc / 10 == 7)
133 if (cc == 20 || cc == 27 || (cc >= 30 && cc <= 34) || cc == 36 ||
134 cc == 39 || cc == 40 || cc == 41 || (cc >= 40 && cc <= 41) ||
135 (cc >= 43 && cc <= 49) || (cc >= 51 && cc <= 58) ||
136 (cc >= 60 && cc <= 66) || cc == 81 || cc == 82 || cc == 84 ||
137 cc == 86 || (cc >= 90 && cc <= 95) || cc == 98) {
145 char txt[1024]; /* TXT record in TXT lookup */
146 int txtlen; /* Length */
149 /*! \brief Callback for TXT record lookup, /ol version */
150 static int txt_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
152 struct txt_context *c = context;
155 c->txt[0] = 0; /* default to empty */
158 if (answer == NULL) {
164 * <character-string> is a single length octet followed by that number of characters.
165 * TXT-DATA One or more <character-string>s.
167 * We only take the first string here.
173 if (i > len) { /* illegal packet */
174 ast_log(LOG_WARNING, "txt_callback: malformed TXT record.\n");
178 if (i >= sizeof(c->txt)) { /* too long? */
179 ast_log(LOG_WARNING, "txt_callback: TXT record too long.\n");
180 i = sizeof(c->txt) - 1;
183 ast_copy_string(c->txt, (char *)answer, i + 1); /* this handles the \0 termination */
189 /*! \brief Determine the branch location record as stored in a TXT record */
193 * Output: number of digits in the number before the i-enum branch
195 * Algorithm: Build <ienum_branchlabel>.c.c.<suffix> and look for a TXT lookup.
196 * Return atoi(TXT-record).
197 * Return -1 on not found.
200 static int blr_txt(const char *cc, const char *suffix)
202 struct txt_context context;
203 char domain[128] = "";
207 ast_mutex_lock(&enumlock);
209 ast_verb(4, "blr_txt() cc='%s', suffix='%s', c_bl='%s'\n", cc, suffix, ienum_branchlabel);
211 if (sizeof(domain) < (strlen(cc) * 2 + strlen(ienum_branchlabel) + strlen(suffix) + 2)) {
212 ast_mutex_unlock(&enumlock);
213 ast_log(LOG_WARNING, "ERROR: string sizing in blr_txt.\n");
217 p1 = domain + snprintf(domain, sizeof(domain), "%s.", ienum_branchlabel);
218 ast_mutex_unlock(&enumlock);
220 for (p2 = (char *) cc + strlen(cc) - 1; p2 >= cc; p2--) {
228 ast_verb(4, "blr_txt() FQDN for TXT record: %s, cc was %s\n", domain, cc);
230 ret = ast_search_dns(&context, domain, C_IN, T_TXT, txt_callback);
233 ret = atoi(context.txt);
235 if ((ret >= 0) && (ret < 20)) {
236 ast_verb(3, "blr_txt() BLR TXT record for %s is %d (apex: %s)\n", cc, ret, suffix);
241 ast_verb(3, "blr_txt() BLR TXT record for %s not found (apex: %s)\n", cc, suffix);
248 char separator[256]; /* label to insert */
249 int sep_len; /* Length */
250 char apex[256]; /* new Apex */
251 int apex_len; /* Length */
254 /*! \brief Callback for EBL record lookup */
255 static int ebl_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
257 struct ebl_context *c = context;
260 c->pos = 0; /* default to empty */
266 if (answer == NULL) {
270 /* draft-lendl-enum-branch-location-record-00
273 * +--+--+--+--+--+--+--+--+
275 * +--+--+--+--+--+--+--+--+
277 * +--+--+--+--+--+--+--+--+
279 * +--+--+--+--+--+--+--+--+
281 * where POSITION is a single byte, SEPARATOR is a <character-string>
282 * and APEX is a <domain-name>.
289 if ((c->pos > 15) || len < 2) { /* illegal packet */
290 ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
296 if (i > len) { /* illegal packet */
297 ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
301 ast_copy_string(c->separator, (char *)answer, i + 1);
307 if ((i = dn_expand((unsigned char *)fullanswer, (unsigned char *)answer + len,
308 (unsigned char *)answer, c->apex, sizeof(c->apex) - 1)) < 0) {
309 ast_log(LOG_WARNING, "Failed to expand hostname\n");
318 /*! \brief Evaluate the I-ENUM branch as stored in an EBL record */
322 * Output: number of digits in the number before the i-enum branch
324 * Algorithm: Build <ienum_branchlabel>.c.c.<suffix> and look for an EBL record
325 * Return pos and fill in separator and apex.
326 * Return -1 on not found.
329 static int blr_ebl(const char *cc, const char *suffix, char *separator, int sep_len, char* apex, int apex_len)
331 struct ebl_context context;
332 char domain[128] = "";
336 ast_mutex_lock(&enumlock);
338 ast_verb(4, "blr_ebl() cc='%s', suffix='%s', c_bl='%s'\n", cc, suffix, ienum_branchlabel);
340 if (sizeof(domain) < (strlen(cc) * 2 + strlen(ienum_branchlabel) + strlen(suffix) + 2)) {
341 ast_mutex_unlock(&enumlock);
342 ast_log(LOG_WARNING, "ERROR: string sizing in blr_EBL.\n");
346 p1 = domain + snprintf(domain, sizeof(domain), "%s.", ienum_branchlabel);
347 ast_mutex_unlock(&enumlock);
349 for (p2 = (char *) cc + strlen(cc) - 1; p2 >= cc; p2--) {
357 ast_verb(4, "blr_ebl() FQDN for EBL record: %s, cc was %s\n", domain, cc);
359 ret = ast_search_dns(&context, domain, C_IN, T_EBL, ebl_callback);
363 if ((ret >= 0) && (ret < 20)) {
364 ast_verb(3, "blr_txt() BLR EBL record for %s is %d/%s/%s)\n", cc, ret, context.separator, context.apex);
365 ast_copy_string(separator, context.separator, sep_len);
366 ast_copy_string(apex, context.apex, apex_len);
370 ast_verb(3, "blr_txt() BLR EBL record for %s not found (apex: %s)\n", cc, suffix);
374 /*! \brief Parse NAPTR record information elements */
375 static unsigned int parse_ie(char *data, unsigned int maxdatalen, unsigned char *src, unsigned int srclen)
377 unsigned int len, olen;
379 len = olen = (unsigned int) src[0];
384 ast_log(LOG_WARNING, "ENUM parsing failed: Wanted %u characters, got %u\n", len, srclen);
388 if (len > maxdatalen)
390 memcpy(data, src, len);
395 /*! \brief Parse DNS NAPTR record used in ENUM ---*/
396 static int parse_naptr(unsigned char *dst, int dstsize, char *tech, int techsize, unsigned char *answer, int len, unsigned char *naptrinput)
398 char tech_return[80];
399 char *oanswer = (char *)answer;
400 char flags[512] = "";
401 char services[512] = "";
403 char regexp[512] = "";
405 char tempdst[512] = "";
406 char errbuff[512] = "";
409 char *pattern, *subst, *d;
412 static const int max_bt = 10; /* max num of regexp backreference allowed, must remain 10 to guarantee a valid backreference index */
413 int size, matchindex; /* size is the size of the backreference sub. */
414 size_t d_len = sizeof(tempdst) - 1;
416 regmatch_t pmatch[max_bt];
418 tech_return[0] = '\0';
421 if (len < sizeof(struct naptr)) {
422 ast_log(LOG_WARNING, "NAPTR record length too short\n");
425 answer += sizeof(struct naptr);
426 len -= sizeof(struct naptr);
427 if ((res = parse_ie(flags, sizeof(flags) - 1, answer, len)) < 0) {
428 ast_log(LOG_WARNING, "Failed to get flags from NAPTR record\n");
435 if ((res = parse_ie(services, sizeof(services) - 1, answer, len)) < 0) {
436 ast_log(LOG_WARNING, "Failed to get services from NAPTR record\n");
442 if ((res = parse_ie(regexp, sizeof(regexp) - 1, answer, len)) < 0) {
443 ast_log(LOG_WARNING, "Failed to get regexp from NAPTR record\n");
450 if ((res = dn_expand((unsigned char *)oanswer, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
451 ast_log(LOG_WARNING, "Failed to expand hostname\n");
455 ast_debug(3, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
456 naptrinput, flags, services, regexp, repl);
459 if (tolower(flags[0]) != 'u') {
460 ast_log(LOG_WARNING, "NAPTR Flag must be 'U' or 'u'.\n");
464 p = strstr(services, "e2u+");
466 p = strstr(services, "E2U+");
470 p = strchr(p, ':') + 1;
472 ast_copy_string(tech_return, p, sizeof(tech_return));
475 p = strstr(services, "+e2u");
477 p = strstr(services, "+E2U");
480 p = strchr(services, ':');
483 ast_copy_string(tech_return, services, sizeof(tech_return));
487 regexp_len = strlen(regexp);
488 if (regexp_len < 7) {
489 ast_log(LOG_WARNING, "Regex too short to be meaningful.\n");
493 /* this takes the first character of the regexp (which is a delimiter)
494 * and uses that character to find the index of the second delimiter */
496 delim2 = strchr(regexp + 1, delim);
497 if ((delim2 == NULL) || (regexp[regexp_len - 1] != delim)) { /* is the second delimiter found, and is the end of the regexp a delimiter */
498 ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
500 } else if (strchr((delim2 + 1), delim) == NULL) { /* if the second delimiter is found, make sure there is a third instance. this could be the end one instead of the middle */
501 ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
504 pattern = regexp + 1; /* pattern is the regex without the begining and ending delimiter */
505 *delim2 = 0; /* zero out the middle delimiter */
506 subst = delim2 + 1; /* dst substring is everything after the second delimiter. */
507 regexp[regexp_len - 1] = 0; /* zero out the last delimiter */
510 * now do the regex wizardry.
513 if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
514 ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n", regexp);
518 if (preg.re_nsub > ARRAY_LEN(pmatch)) {
519 ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
523 /* pmatch is an array containing the substring indexes for the regex backreference sub.
524 * max_bt is the maximum number of backreferences allowed to be stored in pmatch */
525 if ((rc = regexec(&preg, (char *) naptrinput, max_bt, pmatch, 0))) {
526 regerror(rc, &preg, errbuff, sizeof(errbuff));
527 ast_log(LOG_WARNING, "NAPTR Regex match failed. Reason: %s\n", errbuff);
536 /* perform the backreference sub. Search the subst for backreferences,
537 * when a backreference is found, retrieve the backreferences number.
538 * use the backreference number as an index for pmatch to retrieve the
539 * beginning and ending indexes of the substring to insert as the backreference.
540 * if no backreference is found, continue copying the subst into tempdst */
541 while (*subst && (d_len > 0)) {
542 if ((subst[0] == '\\') && isdigit(subst[1])) { /* is this character the beginning of a backreference */
543 matchindex = (int) (subst[1] - '0');
544 if (matchindex >= ARRAY_LEN(pmatch)) {
545 ast_log(LOG_WARNING, "Error during regex substitution. Invalid pmatch index.\n");
548 /* pmatch len is 10. we are garanteed a single char 0-9 is a valid index */
549 size = pmatch[matchindex].rm_eo - pmatch[matchindex].rm_so;
551 ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
554 /* are the pmatch indexes valid for the input length */
555 if ((strlen((char *) naptrinput) >= pmatch[matchindex].rm_eo) && (pmatch[matchindex].rm_so <= pmatch[matchindex].rm_eo)) {
556 memcpy(d, (naptrinput + (int) pmatch[matchindex].rm_so), size); /* copy input substring into backreference marker */
558 subst += 2; /* skip over backreference characters to next valid character */
561 ast_log(LOG_WARNING, "Error during regex substitution. Invalid backreference index.\n");
564 } else if (isprint(*subst)) {
568 ast_log(LOG_WARNING, "Error during regex substitution.\n");
573 ast_copy_string((char *) dst, tempdst, dstsize);
574 dst[dstsize - 1] = '\0';
576 if (*tech != '\0'){ /* check if it is requested NAPTR */
577 if (!strncasecmp(tech, "ALL", techsize)){
578 return 0; /* return or count any RR */
580 if (!strncasecmp(tech_return, tech, sizeof(tech_return) < techsize ? sizeof(tech_return): techsize)){
581 ast_copy_string(tech, tech_return, techsize);
582 return 0; /* we got our RR */
583 } else { /* go to the next RR in the DNS answer */
588 /* tech was not specified, return first parsed RR */
589 ast_copy_string(tech, tech_return, techsize);
594 /* do not return requested value, just count RRs and return thei number in dst */
595 #define ENUMLOOKUP_OPTIONS_COUNT 1
596 /* do an ISN style lookup */
597 #define ENUMLOOKUP_OPTIONS_ISN 2
598 /* do a infrastructure ENUM lookup */
599 #define ENUMLOOKUP_OPTIONS_IENUM 4
600 /* do a direct DNS lookup: no reversal */
601 #define ENUMLOOKUP_OPTIONS_DIRECT 8
603 /*! \brief Callback from ENUM lookup function */
604 static int enum_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
606 struct enum_context *c = context;
610 res = parse_naptr((unsigned char *)c->dst, c->dstlen, c->tech, c->techlen, answer, len, (unsigned char *)c->naptrinput);
613 ast_log(LOG_WARNING, "Failed to parse naptr\n");
615 } else if ((res == 0) && !ast_strlen_zero(c->dst)) { /* ok, we got needed NAPTR */
616 if (c->options & ENUMLOOKUP_OPTIONS_COUNT) { /* counting RRs */
618 snprintf(c->dst, c->dstlen, "%d", c->count);
620 if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
622 memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
623 c->naptr_rrs[c->naptr_rrs_count].result = ast_strdup(c->dst);
624 c->naptr_rrs[c->naptr_rrs_count].tech = ast_strdup(c->tech);
625 c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
626 c->naptr_rrs_count++;
637 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)
639 struct enum_context *context;
644 char naptrinput[128];
647 /* for ISN rewrite */
655 struct timeval time_start, time_end;
657 if (ast_strlen_zero(suffix)) {
658 ast_log(LOG_WARNING, "ast_get_enum need a suffix parameter now.\n");
662 ast_debug(2, "num='%s', tech='%s', suffix='%s', options='%s', record=%u\n", number, tech, suffix, options, record);
665 We don't need that any more, that "n" preceding the number has been replaced by a flag
666 in the options paramter.
667 ast_copy_string(naptrinput, number, sizeof(naptrinput));
670 * The "number" parameter includes a leading '+' if it's a full E.164 number (and not ISN)
671 * We need to preserve that as the regex inside NAPTRs expect the +.
673 * But for the domain generation, the '+' is a nuissance, so we get rid of it.
675 ast_copy_string(naptrinput, number[0] == 'n' ? number + 1 : number, sizeof(naptrinput));
676 if (number[0] == '+') {
680 if (!(context = ast_calloc(1, sizeof(*context)))) {
684 if ((p3 = strchr(naptrinput, '*'))) {
688 context->naptrinput = naptrinput; /* The number */
689 context->dst = dst; /* Return string */
690 context->dstlen = dstlen;
691 context->tech = tech;
692 context->techlen = techlen;
693 context->options = 0;
694 context->position = record > 0 ? record : 1;
696 context->naptr_rrs = NULL;
697 context->naptr_rrs_count = 0;
702 * c Return count, not URI
703 * i Use infrastructure ENUM
704 * s Do ISN transformation
705 * d Direct DNS query: no reversing.
708 if (options != NULL) {
709 if (strchr(options,'s')) {
710 context->options |= ENUMLOOKUP_OPTIONS_ISN;
711 } else if (strchr(options,'i')) {
712 context->options |= ENUMLOOKUP_OPTIONS_IENUM;
713 } else if (strchr(options,'d')) {
714 context->options |= ENUMLOOKUP_OPTIONS_DIRECT;
716 if (strchr(options,'c')) {
717 context->options |= ENUMLOOKUP_OPTIONS_COUNT;
719 if (strchr(number,'*')) {
720 context->options |= ENUMLOOKUP_OPTIONS_ISN;
723 ast_debug(2, "ENUM options(%s): pos=%d, options='%d'\n", options, context->position, context->options);
724 ast_debug(1, "n='%s', tech='%s', suffix='%s', options='%d', record='%d'\n",
725 number, tech, suffix, context->options, context->position);
728 * This code does more than simple RFC3261 ENUM. All these rewriting
729 * schemes have in common that they build the FQDN for the NAPTR lookup
731 * - a number which needs be flipped and "."-seperated (left)
732 * - some fixed string (middle)
735 * The RFC3261 ENUM is: left=full number, middle="", apex=from args.
736 * ISN: number = "middle*left", apex=from args
737 * I-ENUM: EBL parameters build the split, can change apex
738 * Direct: left="", middle=argument, apex=from args
742 /* default: the whole number will be flipped, no middle domain component */
743 ast_copy_string(left, number, sizeof(left));
746 * I-ENUM can change the apex, thus we copy it
748 ast_copy_string(apex, suffix, sizeof(apex));
750 if ((context->options & ENUMLOOKUP_OPTIONS_ISN) && (p1 = strchr(number, '*'))) {
752 ast_copy_string(left, number, sizeof(left));
753 ast_copy_string(middle, p1, sizeof(middle) - 1);
755 ast_debug(2, "ISN ENUM: left=%s, middle='%s'\n", left, middle);
756 /* Direct DNS lookup rewrite */
757 } else if (context->options & ENUMLOOKUP_OPTIONS_DIRECT) {
758 left[0] = 0; /* nothing to flip around */
759 ast_copy_string(middle, number, sizeof(middle) - 1);
761 ast_debug(2, "DIRECT ENUM: middle='%s'\n", middle);
762 /* Infrastructure ENUM rewrite */
763 } else if (context->options & ENUMLOOKUP_OPTIONS_IENUM) {
766 char sep[256], n_apex[256];
767 int cc_len = cclen(number);
769 ast_mutex_lock(&enumlock);
770 ast_copy_string(sep, ienum_branchlabel, sizeof(sep)); /* default */
771 ast_mutex_unlock(&enumlock);
774 case ENUMLOOKUP_BLR_EBL:
775 ast_copy_string(cc, number, cc_len); /* cclen() never returns more than 3 */
776 sdl = blr_ebl(cc, suffix, sep, sizeof(sep) - 1, n_apex, sizeof(n_apex) - 1);
779 ast_copy_string(apex, n_apex, sizeof(apex));
780 ast_debug(2, "EBL ENUM: sep=%s, apex='%s'\n", sep, n_apex);
785 case ENUMLOOKUP_BLR_TXT:
786 ast_copy_string(cc, number, cc_len); /* cclen() never returns more than 3 */
787 sdl = blr_txt(cc, suffix);
794 case ENUMLOOKUP_BLR_CC: /* BLR is at the country-code level */
800 if (sdl > strlen(number)) { /* Number too short for this sdl? */
801 ast_log(LOG_WARNING, "I-ENUM: subdomain location %d behind number %s\n", sdl, number);
805 ast_copy_string(left, number + sdl, sizeof(left));
807 ast_mutex_lock(&enumlock);
808 ast_copy_string(middle, sep, sizeof(middle) - 1);
810 ast_mutex_unlock(&enumlock);
812 /* check the space we need for middle */
813 if ((sdl * 2 + strlen(middle) + 2) > sizeof(middle)) {
814 ast_log(LOG_WARNING, "ast_get_enum: not enough space for I-ENUM rewrite.\n");
819 p1 = middle + strlen(middle);
820 for (p2 = (char *) number + sdl - 1; p2 >= number; p2--) {
828 ast_debug(2, "I-ENUM: cclen=%d, left=%s, middle='%s', apex='%s'\n", cc_len, left, middle, apex);
831 if (strlen(left) * 2 + 2 > sizeof(domain)) {
832 ast_log(LOG_WARNING, "string to long in ast_get_enum\n");
837 /* flip left into domain */
839 for (p2 = left + strlen(left); p2 >= left; p2--) {
847 if (chan && ast_autoservice_start(chan) < 0) {
852 spaceleft = sizeof(tmp) - 2;
853 ast_copy_string(tmp, domain, spaceleft);
854 spaceleft -= strlen(domain);
857 strncat(tmp, middle, spaceleft);
858 spaceleft -= strlen(middle);
861 strncat(tmp,apex,spaceleft);
862 time_start = ast_tvnow();
863 ret = ast_search_dns(context, tmp, C_IN, T_NAPTR, enum_callback);
864 time_end = ast_tvnow();
866 ast_debug(2, "profiling: %s, %s, %" PRIi64 " ms\n",
867 (ret == 0) ? "OK" : "FAIL", tmp, ast_tvdiff_ms(time_end, time_start));
870 ast_debug(1, "No such number found: %s (%s)\n", tmp, strerror(errno));
871 context->naptr_rrs_count = -1;
876 if (context->naptr_rrs_count >= context->position && ! (context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
877 /* sort array by NAPTR order/preference */
878 for (k = 0; k < context->naptr_rrs_count; k++) {
879 for (i = 0; i < context->naptr_rrs_count; i++) {
880 /* use order first and then preference to compare */
881 if ((ntohs(context->naptr_rrs[k].naptr.order) < ntohs(context->naptr_rrs[i].naptr.order)
882 && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
883 || (ntohs(context->naptr_rrs[k].naptr.order) > ntohs(context->naptr_rrs[i].naptr.order)
884 && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
885 z = context->naptr_rrs[k].sort_pos;
886 context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
887 context->naptr_rrs[i].sort_pos = z;
890 if (ntohs(context->naptr_rrs[k].naptr.order) == ntohs(context->naptr_rrs[i].naptr.order)) {
891 if ((ntohs(context->naptr_rrs[k].naptr.pref) < ntohs(context->naptr_rrs[i].naptr.pref)
892 && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
893 || (ntohs(context->naptr_rrs[k].naptr.pref) > ntohs(context->naptr_rrs[i].naptr.pref)
894 && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
895 z = context->naptr_rrs[k].sort_pos;
896 context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
897 context->naptr_rrs[i].sort_pos = z;
902 for (k = 0; k < context->naptr_rrs_count; k++) {
903 if (context->naptr_rrs[k].sort_pos == context->position - 1) {
904 ast_copy_string(context->dst, context->naptr_rrs[k].result, dstlen);
905 ast_copy_string(context->tech, context->naptr_rrs[k].tech, techlen);
909 } else if (!(context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
911 } else if ((context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
912 snprintf(context->dst, context->dstlen, "%d", context->naptr_rrs_count + context->count);
916 ret |= ast_autoservice_stop(chan);
920 for (k = 0; k < context->naptr_rrs_count; k++) {
921 ast_free(context->naptr_rrs[k].result);
922 ast_free(context->naptr_rrs[k].tech);
924 ast_free(context->naptr_rrs);
927 *argcontext = context;
933 int ast_get_txt(struct ast_channel *chan, const char *number, char *txt, int txtlen, char *suffix)
935 struct txt_context context;
937 int pos = strlen(number) - 1;
941 ast_debug(4, "ast_get_txt: Number = '%s', suffix = '%s'\n", number, suffix);
943 if (chan && ast_autoservice_start(chan) < 0) {
952 if (isdigit(number[pos])) {
953 tmp[newpos++] = number[pos];
959 ast_copy_string(&tmp[newpos], suffix, sizeof(tmp) - newpos);
962 ast_debug(2, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
965 ast_copy_string(txt, context.txt, txtlen);
968 ret |= ast_autoservice_stop(chan);
973 /*! \brief Initialize the ENUM support subsystem */
974 static int private_enum_init(int reload)
976 struct ast_config *cfg;
978 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
980 if ((cfg = ast_config_load2("enum.conf", "enum", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
982 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
986 /* Destroy existing list */
987 ast_mutex_lock(&enumlock);
989 if ((string = ast_variable_retrieve(cfg, "ienum", "branchlabel"))) {
990 ast_copy_string(ienum_branchlabel, string, sizeof(ienum_branchlabel));
993 if ((string = ast_variable_retrieve(cfg, "ienum", "ebl_alg"))) {
994 ebl_alg = ENUMLOOKUP_BLR_CC; /* default */
996 if (!strcasecmp(string, "txt"))
997 ebl_alg = ENUMLOOKUP_BLR_TXT;
998 else if (!strcasecmp(string, "ebl"))
999 ebl_alg = ENUMLOOKUP_BLR_EBL;
1000 else if (!strcasecmp(string, "cc"))
1001 ebl_alg = ENUMLOOKUP_BLR_CC;
1003 ast_log(LOG_WARNING, "No valid parameter for ienum/ebl_alg.\n");
1005 ast_config_destroy(cfg);
1007 ast_mutex_unlock(&enumlock);
1011 int ast_enum_init(void)
1013 return private_enum_init(0);
1016 int ast_enum_reload(void)
1018 return private_enum_init(1);