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.
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, "%30d", &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] = "";
396 char tempdst[512] = "";
397 char errbuff[512] = "";
400 char *pattern, *subst, *d;
403 static const int max_bt = 10; /* max num of regexp backreference allowed, must remain 10 to guarantee a valid backreference index */
404 int size, matchindex; /* size is the size of the backreference sub. */
405 size_t d_len = sizeof(tempdst) - 1;
407 regmatch_t pmatch[max_bt];
409 tech_return[0] = '\0';
412 if (len < sizeof(struct naptr)) {
413 ast_log(LOG_WARNING, "NAPTR record length too short\n");
416 answer += sizeof(struct naptr);
417 len -= sizeof(struct naptr);
418 if ((res = parse_ie(flags, sizeof(flags) - 1, answer, len)) < 0) {
419 ast_log(LOG_WARNING, "Failed to get flags from NAPTR record\n");
426 if ((res = parse_ie(services, sizeof(services) - 1, answer, len)) < 0) {
427 ast_log(LOG_WARNING, "Failed to get services from NAPTR record\n");
433 if ((res = parse_ie(regexp, sizeof(regexp) - 1, answer, len)) < 0) {
434 ast_log(LOG_WARNING, "Failed to get regexp from NAPTR record\n");
441 if ((res = dn_expand((unsigned char *)oanswer, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
442 ast_log(LOG_WARNING, "Failed to expand hostname\n");
446 ast_debug(3, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
447 naptrinput, flags, services, regexp, repl);
450 if (tolower(flags[0]) != 'u') {
451 ast_log(LOG_WARNING, "NAPTR Flag must be 'U' or 'u'.\n");
455 p = strstr(services, "e2u+");
457 p = strstr(services, "E2U+");
461 p = strchr(p, ':') + 1;
463 ast_copy_string(tech_return, p, sizeof(tech_return));
466 p = strstr(services, "+e2u");
468 p = strstr(services, "+E2U");
471 p = strchr(services, ':');
474 ast_copy_string(tech_return, services, sizeof(tech_return));
478 regexp_len = strlen(regexp);
479 if (regexp_len < 7) {
480 ast_log(LOG_WARNING, "Regex too short to be meaningful.\n");
484 /* this takes the first character of the regexp (which is a delimiter)
485 * and uses that character to find the index of the second delimiter */
487 delim2 = strchr(regexp + 1, delim);
488 if ((delim2 == NULL) || (regexp[regexp_len - 1] != delim)) { /* is the second delimiter found, and is the end of the regexp a delimiter */
489 ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
491 } 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 */
492 ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
495 pattern = regexp + 1; /* pattern is the regex without the begining and ending delimiter */
496 *delim2 = 0; /* zero out the middle delimiter */
497 subst = delim2 + 1; /* dst substring is everything after the second delimiter. */
498 regexp[regexp_len - 1] = 0; /* zero out the last delimiter */
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 > ARRAY_LEN(pmatch)) {
510 ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
514 /* pmatch is an array containing the substring indexes for the regex backreference sub.
515 * max_bt is the maximum number of backreferences allowed to be stored in pmatch */
516 if ((rc = regexec(&preg, (char *) naptrinput, max_bt, pmatch, 0))) {
517 regerror(rc, &preg, errbuff, sizeof(errbuff));
518 ast_log(LOG_WARNING, "NAPTR Regex match failed. Reason: %s\n", errbuff);
527 /* perform the backreference sub. Search the subst for backreferences,
528 * when a backreference is found, retrieve the backreferences number.
529 * use the backreference number as an index for pmatch to retrieve the
530 * beginning and ending indexes of the substring to insert as the backreference.
531 * if no backreference is found, continue copying the subst into tempdst */
532 while (*subst && (d_len > 0)) {
533 if ((subst[0] == '\\') && isdigit(subst[1])) { /* is this character the beginning of a backreference */
534 matchindex = (int) (subst[1] - '0');
535 if (matchindex >= ARRAY_LEN(pmatch)) {
536 ast_log(LOG_WARNING, "Error during regex substitution. Invalid pmatch index.\n");
539 /* pmatch len is 10. we are garanteed a single char 0-9 is a valid index */
540 size = pmatch[matchindex].rm_eo - pmatch[matchindex].rm_so;
542 ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
545 /* are the pmatch indexes valid for the input length */
546 if ((strlen((char *) naptrinput) >= pmatch[matchindex].rm_eo) && (pmatch[matchindex].rm_so <= pmatch[matchindex].rm_eo)) {
547 memcpy(d, (naptrinput + (int) pmatch[matchindex].rm_so), size); /* copy input substring into backreference marker */
549 subst += 2; /* skip over backreference characters to next valid character */
552 ast_log(LOG_WARNING, "Error during regex substitution. Invalid backreference index.\n");
555 } else if (isprint(*subst)) {
559 ast_log(LOG_WARNING, "Error during regex substitution.\n");
564 ast_copy_string((char *) dst, tempdst, dstsize);
565 dst[dstsize - 1] = '\0';
567 if (*tech != '\0'){ /* check if it is requested NAPTR */
568 if (!strncasecmp(tech, "ALL", techsize)){
569 return 0; /* return or count any RR */
571 if (!strncasecmp(tech_return, tech, sizeof(tech_return) < techsize ? sizeof(tech_return): techsize)){
572 ast_copy_string(tech, tech_return, techsize);
573 return 0; /* we got our RR */
574 } else { /* go to the next RR in the DNS answer */
579 /* tech was not specified, return first parsed RR */
580 ast_copy_string(tech, tech_return, techsize);
585 /* do not return requested value, just count RRs and return thei number in dst */
586 #define ENUMLOOKUP_OPTIONS_COUNT 1
587 /* do an ISN style lookup */
588 #define ENUMLOOKUP_OPTIONS_ISN 2
589 /* do a infrastructure ENUM lookup */
590 #define ENUMLOOKUP_OPTIONS_IENUM 4
591 /* do a direct DNS lookup: no reversal */
592 #define ENUMLOOKUP_OPTIONS_DIRECT 8
594 /*! \brief Callback from ENUM lookup function */
595 static int enum_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
597 struct enum_context *c = context;
601 res = parse_naptr((unsigned char *)c->dst, c->dstlen, c->tech, c->techlen, answer, len, (unsigned char *)c->naptrinput);
604 ast_log(LOG_WARNING, "Failed to parse naptr\n");
606 } else if ((res == 0) && !ast_strlen_zero(c->dst)) { /* ok, we got needed NAPTR */
607 if (c->options & ENUMLOOKUP_OPTIONS_COUNT) { /* counting RRs */
609 snprintf(c->dst, c->dstlen, "%d", c->count);
611 if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
613 memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
614 c->naptr_rrs[c->naptr_rrs_count].result = ast_strdup(c->dst);
615 c->naptr_rrs[c->naptr_rrs_count].tech = ast_strdup(c->tech);
616 c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
617 c->naptr_rrs_count++;
628 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)
630 struct enum_context *context;
635 char naptrinput[128];
638 /* for ISN rewrite */
646 struct timeval time_start, time_end;
648 if (ast_strlen_zero(suffix)) {
649 ast_log(LOG_WARNING, "ast_get_enum need a suffix parameter now.\n");
653 ast_verb(2, "ast_get_enum(num='%s', tech='%s', suffix='%s', options='%s', record=%d\n", number, tech, suffix, options, record);
656 We don't need that any more, that "n" preceding the number has been replaced by a flag
657 in the options paramter.
658 ast_copy_string(naptrinput, number, sizeof(naptrinput));
661 * The "number" parameter includes a leading '+' if it's a full E.164 number (and not ISN)
662 * We need to preserve that as the regex inside NAPTRs expect the +.
664 * But for the domain generation, the '+' is a nuissance, so we get rid of it.
666 ast_copy_string(naptrinput, number[0] == 'n' ? number + 1 : number, sizeof(naptrinput));
667 if (number[0] == '+') {
671 if (!(context = ast_calloc(1, sizeof(*context))))
674 if((p3 = strchr(naptrinput, '*'))) {
678 context->naptrinput = naptrinput; /* The number */
679 context->dst = dst; /* Return string */
680 context->dstlen = dstlen;
681 context->tech = tech;
682 context->techlen = techlen;
683 context->options = 0;
684 context->position = record > 0 ? record : 1;
686 context->naptr_rrs = NULL;
687 context->naptr_rrs_count = 0;
692 * c Return count, not URI
693 * i Use infrastructure ENUM
694 * s Do ISN transformation
695 * d Direct DNS query: no reversing.
698 if (options != NULL) {
699 if (strchr(options,'s')) {
700 context->options |= ENUMLOOKUP_OPTIONS_ISN;
701 } else if (strchr(options,'i')) {
702 context->options |= ENUMLOOKUP_OPTIONS_IENUM;
703 } else if (strchr(options,'d')) {
704 context->options |= ENUMLOOKUP_OPTIONS_DIRECT;
706 if (strchr(options,'c')) {
707 context->options |= ENUMLOOKUP_OPTIONS_COUNT;
709 if (strchr(number,'*')) {
710 context->options |= ENUMLOOKUP_OPTIONS_ISN;
713 ast_verb(2, "ENUM options(%s): pos=%d, options='%d'\n", options, context->position, context->options);
714 ast_debug(1, "ast_get_enum(): n='%s', tech='%s', suffix='%s', options='%d', record='%d'\n",
715 number, tech, suffix, context->options, context->position);
718 * This code does more than simple RFC3261 ENUM. All these rewriting
719 * schemes have in common that they build the FQDN for the NAPTR lookup
721 * - a number which needs be flipped and "."-seperated (left)
722 * - some fixed string (middle)
725 * The RFC3261 ENUM is: left=full number, middle="", apex=from args.
726 * ISN: number = "middle*left", apex=from args
727 * I-ENUM: EBL parameters build the split, can change apex
728 * Direct: left="", middle=argument, apex=from args
732 /* default: the whole number will be flipped, no middle domain component */
733 ast_copy_string(left, number, sizeof(left));
736 * I-ENUM can change the apex, thus we copy it
738 ast_copy_string(apex, suffix, sizeof(apex));
740 if ((context->options & ENUMLOOKUP_OPTIONS_ISN) && (p1 = strchr(number, '*'))) {
742 ast_copy_string(left, number, sizeof(left));
743 ast_copy_string(middle, p1, sizeof(middle) - 1);
746 ast_verb(2, "ISN ENUM: left=%s, middle='%s'\n", left, middle);
747 /* Direct DNS lookup rewrite */
748 } else if (context->options & ENUMLOOKUP_OPTIONS_DIRECT) {
749 left[0] = 0; /* nothing to flip around */
750 ast_copy_string(middle, number, sizeof(middle) - 1);
753 ast_verb(2, "DIRECT ENUM: middle='%s'\n", middle);
754 /* Infrastructure ENUM rewrite */
755 } else if (context->options & ENUMLOOKUP_OPTIONS_IENUM) {
758 char sep[256], n_apex[256];
759 int cc_len = cclen(number);
761 ast_mutex_lock(&enumlock);
762 ast_copy_string(sep, ienum_branchlabel, sizeof(sep)); /* default */
763 ast_mutex_unlock(&enumlock);
766 case ENUMLOOKUP_BLR_EBL:
767 ast_copy_string(cc, number, cc_len); /* cclen() never returns more than 3 */
768 sdl = blr_ebl(cc, suffix, sep, sizeof(sep) - 1, n_apex, sizeof(n_apex) - 1);
771 ast_copy_string(apex, n_apex, sizeof(apex));
772 ast_verb(2, "EBL ENUM: sep=%s, apex='%s'\n", sep, n_apex);
777 case ENUMLOOKUP_BLR_TXT:
778 ast_copy_string(cc, number, cc_len); /* cclen() never returns more than 3 */
779 sdl = blr_txt(cc, suffix);
785 case ENUMLOOKUP_BLR_CC: /* BLR is at the country-code level */
791 if (sdl > strlen(number)) { /* Number too short for this sdl? */
792 ast_log(LOG_WARNING, "I-ENUM: subdomain location %d behind number %s\n", sdl, number);
795 ast_copy_string(left, number + sdl, sizeof(left));
797 ast_mutex_lock(&enumlock);
798 ast_copy_string(middle, sep, sizeof(middle) - 1);
800 ast_mutex_unlock(&enumlock);
802 /* check the space we need for middle */
803 if ((sdl * 2 + strlen(middle) + 2) > sizeof(middle)) {
804 ast_log(LOG_WARNING, "ast_get_enum: not enough space for I-ENUM rewrite.\n");
808 p1 = middle + strlen(middle);
809 for (p2 = (char *) number + sdl - 1; p2 >= number; p2--) {
817 ast_verb(2, "I-ENUM: cclen=%d, left=%s, middle='%s', apex='%s'\n", cc_len, left, middle, apex);
820 if (strlen(left) * 2 + 2 > sizeof(domain)) {
821 ast_log(LOG_WARNING, "string to long in ast_get_enum\n");
825 /* flip left into domain */
827 for (p2 = left + strlen(left); p2 >= left; p2--) {
835 if (chan && ast_autoservice_start(chan) < 0) {
840 spaceleft = sizeof(tmp) - 2;
841 ast_copy_string(tmp, domain, spaceleft);
842 spaceleft -= strlen(domain);
845 strncat(tmp, middle, spaceleft);
846 spaceleft -= strlen(middle);
849 strncat(tmp,apex,spaceleft);
850 time_start = ast_tvnow();
851 ret = ast_search_dns(context, tmp, C_IN, T_NAPTR, enum_callback);
852 time_end = ast_tvnow();
854 ast_verb(2, "ast_get_enum() profiling: %s, %s, %" PRIi64 " ms\n",
855 (ret == 0) ? "OK" : "FAIL", tmp, ast_tvdiff_ms(time_end, time_start));
858 ast_debug(1, "No such number found: %s (%s)\n", tmp, strerror(errno));
863 if (context->naptr_rrs_count >= context->position && ! (context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
864 /* sort array by NAPTR order/preference */
865 for (k = 0; k < context->naptr_rrs_count; k++) {
866 for (i = 0; i < context->naptr_rrs_count; i++) {
867 /* use order first and then preference to compare */
868 if ((ntohs(context->naptr_rrs[k].naptr.order) < ntohs(context->naptr_rrs[i].naptr.order)
869 && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
870 || (ntohs(context->naptr_rrs[k].naptr.order) > ntohs(context->naptr_rrs[i].naptr.order)
871 && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
872 z = context->naptr_rrs[k].sort_pos;
873 context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
874 context->naptr_rrs[i].sort_pos = z;
877 if (ntohs(context->naptr_rrs[k].naptr.order) == ntohs(context->naptr_rrs[i].naptr.order)) {
878 if ((ntohs(context->naptr_rrs[k].naptr.pref) < ntohs(context->naptr_rrs[i].naptr.pref)
879 && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
880 || (ntohs(context->naptr_rrs[k].naptr.pref) > ntohs(context->naptr_rrs[i].naptr.pref)
881 && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
882 z = context->naptr_rrs[k].sort_pos;
883 context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
884 context->naptr_rrs[i].sort_pos = z;
889 for (k = 0; k < context->naptr_rrs_count; k++) {
890 if (context->naptr_rrs[k].sort_pos == context->position - 1) {
891 ast_copy_string(context->dst, context->naptr_rrs[k].result, dstlen);
892 ast_copy_string(context->tech, context->naptr_rrs[k].tech, techlen);
896 } else if (!(context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
898 } else if ((context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
899 snprintf(context->dst,context->dstlen,"%d",context->count);
903 ret |= ast_autoservice_stop(chan);
906 for (k = 0; k < context->naptr_rrs_count; k++) {
907 ast_free(context->naptr_rrs[k].result);
908 ast_free(context->naptr_rrs[k].tech);
910 ast_free(context->naptr_rrs);
913 *argcontext = context;
918 int ast_get_txt(struct ast_channel *chan, const char *number, char *txt, int txtlen, char *suffix)
920 struct txt_context context;
922 int pos = strlen(number) - 1;
926 ast_debug(4, "ast_get_txt: Number = '%s', suffix = '%s'\n", number, suffix);
928 if (chan && ast_autoservice_start(chan) < 0) {
937 if (isdigit(number[pos])) {
938 tmp[newpos++] = number[pos];
944 ast_copy_string(&tmp[newpos], suffix, sizeof(tmp) - newpos);
947 ast_debug(2, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
950 ast_copy_string(txt, context.txt, txtlen);
953 ret |= ast_autoservice_stop(chan);
958 /*! \brief Initialize the ENUM support subsystem */
959 static int private_enum_init(int reload)
961 struct ast_config *cfg;
963 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
965 if ((cfg = ast_config_load2("enum.conf", "enum", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
967 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
971 /* Destroy existing list */
972 ast_mutex_lock(&enumlock);
974 if ((string = ast_variable_retrieve(cfg, "ienum", "branchlabel"))) {
975 ast_copy_string(ienum_branchlabel, string, sizeof(ienum_branchlabel));
978 if ((string = ast_variable_retrieve(cfg, "ienum", "ebl_alg"))) {
979 ebl_alg = ENUMLOOKUP_BLR_CC; /* default */
981 if (!strcasecmp(string, "txt"))
982 ebl_alg = ENUMLOOKUP_BLR_TXT;
983 else if (!strcasecmp(string, "ebl"))
984 ebl_alg = ENUMLOOKUP_BLR_EBL;
985 else if (!strcasecmp(string, "cc"))
986 ebl_alg = ENUMLOOKUP_BLR_CC;
988 ast_log(LOG_WARNING, "No valid parameter for ienum/ebl_alg.\n");
990 ast_config_destroy(cfg);
992 ast_mutex_unlock(&enumlock);
993 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: Enum\r\nStatus: Enabled\r\nMessage: ENUM reload Requested\r\n");
997 int ast_enum_init(void)
999 return private_enum_init(0);
1002 int ast_enum_reload(void)
1004 return private_enum_init(1);