Merged revisions 116463 via svnmerge from
[asterisk-bristuff.git] / main / enum.c
blob6f821c26d333fa5aa47bd2f1ec07eccfa6df886c
1 /*
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.
21 /*! \file
23 * \brief ENUM Support for Asterisk
25 * \author Mark Spencer <markster@digium.com>
27 * \arg Funding provided by nic.at
29 * \par Enum standards
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
38 * - I-ENUM:
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.
48 #include "asterisk.h"
50 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/nameser.h>
55 #ifdef __APPLE__
56 #if __APPLE_CC__ >= 1495
57 #include <arpa/nameser_compat.h>
58 #endif
59 #endif
60 #include <resolv.h>
61 #include <ctype.h>
62 #include <regex.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"
71 #ifdef __APPLE__
72 #undef T_NAPTR
73 #define T_NAPTR 35
74 #endif
76 #ifdef __APPLE__
77 #undef T_TXT
78 #define T_TXT 16
79 #endif
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 */
89 #define T_EBL 65300
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
98 * 0 on invalid number
100 * Algorithm:
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)
108 int cc;
109 char digits[3] = "";
111 if (!number || (strlen(number) < 3)) {
112 return 0;
115 strncpy(digits, number, 2);
117 if (!sscanf(digits, "%d", &cc)) {
118 return 0;
121 if (cc / 10 == 1 || cc / 10 == 7)
122 return 1;
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) {
129 return 2;
132 return 3;
135 struct txt_context {
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;
144 unsigned int i;
146 c->txt[0] = 0; /* default to empty */
147 c->txtlen = 0;
149 if (answer == NULL) {
150 return 0;
153 /* RFC1035:
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.
161 i = *answer++;
162 len -= 1;
164 if (i > len) { /* illegal packet */
165 ast_log(LOG_WARNING, "txt_callback: malformed TXT record.\n");
166 return 0;
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 */
175 c->txtlen = i;
177 return 1;
180 /*! \brief Determine the branch location record as stored in a TXT record */
182 * Input: CC code
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] = "";
195 char *p1, *p2;
196 int ret;
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");
205 return -1;
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--) {
212 if (isdigit(*p2)) {
213 *p1++ = *p2;
214 *p1++ = '.';
217 strcat(p1, suffix);
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);
223 if (ret > 0) {
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);
228 return ret;
232 ast_verb(3, "blr_txt() BLR TXT record for %s not found (apex: %s)\n", cc, suffix);
234 return -1;
237 struct ebl_context {
238 unsigned char pos;
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;
249 unsigned int i;
251 c->pos = 0; /* default to empty */
252 c->separator[0] = 0;
253 c->sep_len = 0;
254 c->apex[0] = 0;
255 c->apex_len = 0;
257 if (answer == NULL) {
258 return 0;
261 /* draft-lendl-enum-branch-location-record-00
263 * 0 1 2 3 4 5 6 7
264 * +--+--+--+--+--+--+--+--+
265 * | POSITION |
266 * +--+--+--+--+--+--+--+--+
267 * / SEPARATOR /
268 * +--+--+--+--+--+--+--+--+
269 * / APEX /
270 * +--+--+--+--+--+--+--+--+
272 * where POSITION is a single byte, SEPARATOR is a <character-string>
273 * and APEX is a <domain-name>.
277 c->pos = *answer++;
278 len -= 1;
280 if ((c->pos > 15) || len < 2) { /* illegal packet */
281 ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
282 return 0;
285 i = *answer++;
286 len -= 1;
287 if (i > len) { /* illegal packet */
288 ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
289 return 0;
292 ast_copy_string(c->separator, (char *)answer, i + 1);
293 c->sep_len = i;
295 answer += i;
296 len -= i;
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");
301 return 0;
303 c->apex[i] = 0;
304 c->apex_len = i;
306 return 1;
309 /*! \brief Evaluate the I-ENUM branch as stored in an EBL record */
311 * Input: CC code
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] = "";
324 char *p1,*p2;
325 int ret;
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");
334 return -1;
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--) {
341 if (isdigit(*p2)) {
342 *p1++ = *p2;
343 *p1++ = '.';
346 strcat(p1, suffix);
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);
351 if (ret > 0) {
352 ret = context.pos;
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);
358 return ret;
361 ast_verb(3, "blr_txt() BLR EBL record for %s not found (apex: %s)\n", cc, suffix);
362 return -1;
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];
371 src++;
372 srclen--;
374 if (len > srclen) {
375 ast_log(LOG_WARNING, "ENUM parsing failed: Wanted %d characters, got %d\n", len, srclen);
376 return -1;
379 if (len > maxdatalen)
380 len = maxdatalen;
381 memcpy(data, src, len);
383 return olen + 1;
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] = "";
393 char *p;
394 char regexp[512] = "";
395 char repl[512] = "";
396 char temp[512] = "";
397 char errbuff[512] = "";
398 char delim;
399 char *delim2;
400 char *pattern, *subst, *d, *number;
401 int res;
402 int regexp_len, rc;
403 int size;
404 int d_len = sizeof(temp) - 1;
405 regex_t preg;
406 regmatch_t pmatch[9];
408 tech_return[0] = '\0';
409 dst[0] = '\0';
411 if (len < sizeof(struct naptr)) {
412 ast_log(LOG_WARNING, "NAPTR record length too short\n");
413 return -1;
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");
419 return -1;
420 } else {
421 answer += res;
422 len -= res;
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");
427 return -1;
428 } else {
429 answer += res;
430 len -= res;
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");
434 return -1;
435 } else {
436 answer += res;
437 len -= res;
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");
442 return -1;
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");
451 return -1;
454 p = strstr(services, "e2u+");
455 if (p == NULL)
456 p = strstr(services, "E2U+");
457 if (p){
458 p = p + 4;
459 if (strchr(p, ':')){
460 p = strchr(p, ':') + 1;
462 ast_copy_string(tech_return, p, sizeof(tech_return));
463 } else {
465 p = strstr(services, "+e2u");
466 if (p == NULL)
467 p = strstr(services, "+E2U");
468 if (p) {
469 *p = 0;
470 p = strchr(services, ':');
471 if (p)
472 *p = 0;
473 ast_copy_string(tech_return, services, sizeof(tech_return));
477 /* DEDBUGGING STUB
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");
484 return -1;
488 delim = regexp[0];
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);
492 return -1;
495 pattern = regexp + 1;
496 *delim2 = 0;
497 subst = delim2 + 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);
506 return -1;
509 if (preg.re_nsub > 9) {
510 ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
511 regfree(&preg);
512 return -1;
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);
518 regfree(&preg);
519 return -1;
521 regfree(&preg);
523 d = temp;
525 number = (char *)(naptrinput + (*naptrinput == '+'));
527 d_len--;
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);
532 if (size > d_len) {
533 ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
534 return -1;
536 memcpy(d, number, size);
537 d_len -= size;
538 subst += 2;
539 d += size;
540 //ast_log(LOG_WARNING, "after dlen:%d: temp:%s:\n",d_len,temp);
541 } else if (isprint(*subst)) {
542 *d++ = *subst++;
543 d_len--;
544 } else {
545 ast_log(LOG_WARNING, "Error during regex substitution.\n");
546 return -1;
549 *d = 0;
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 */
562 return 1;
566 /* tech was not specified, return first parsed RR */
567 ast_copy_string(tech, tech_return, techsize);
569 return 0;
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;
585 void *p = NULL;
586 int res;
588 res = parse_naptr((unsigned char *)c->dst, c->dstlen, c->tech, c->techlen, answer, len, (unsigned char *)c->naptrinput);
590 if (res < 0) {
591 ast_log(LOG_WARNING, "Failed to parse naptr\n");
592 return -1;
593 } else if ((res == 0) && !ast_strlen_zero(c->dst)) { /* ok, we got needed NAPTR */
594 if (c->options & ENUMLOOKUP_OPTIONS_COUNT) { /* counting RRs */
595 c->count++;
596 snprintf(c->dst, c->dstlen, "%d", c->count);
597 } else {
598 if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
599 c->naptr_rrs = p;
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++;
606 c->dst[0] = 0;
608 return 0;
611 return 0;
614 /* ENUM lookup */
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;
618 char tmp[512];
619 char domain[256];
620 char left[128];
621 char middle[128];
622 char naptrinput[128];
623 char apex[128] = "";
624 int ret = -1;
625 /* for ISN rewrite */
626 char *p1 = NULL;
627 char *p2 = NULL;
628 char *p3 = NULL;
629 int k = 0;
630 int i = 0;
631 int z = 0;
632 int spaceleft = 0;
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");
637 return -1;
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] == '+') {
655 number++;
658 if (!(context = ast_calloc(1, sizeof(*context))))
659 return -1;
661 if((p3 = strchr(naptrinput, '*'))) {
662 *p3='\0';
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;
672 context->count = 0;
673 context->naptr_rrs = NULL;
674 context->naptr_rrs_count = 0;
677 * Process options:
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
707 * by concatenating
708 * - a number which needs be flipped and "."-seperated (left)
709 * - some fixed string (middle)
710 * - an Apex. (apex)
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));
721 middle[0] = '\0';
723 * I-ENUM can change the apex, thus we copy it
725 ast_copy_string(apex, suffix, sizeof(apex));
726 /* ISN rewrite */
727 if ((context->options & ENUMLOOKUP_OPTIONS_ISN) && (p1 = strchr(number, '*'))) {
728 *p1++ = '\0';
729 ast_copy_string(left, number, sizeof(left));
730 ast_copy_string(middle, p1, sizeof(middle) - 1);
731 strcat(middle, ".");
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);
738 strcat(middle, ".");
740 ast_verb(2, "DIRECT ENUM: middle='%s'\n", middle);
741 /* Infrastructure ENUM rewrite */
742 } else if (context->options & ENUMLOOKUP_OPTIONS_IENUM) {
743 int sdl = 0;
744 char cc[8];
745 char sep[256], n_apex[256];
746 int cc_len = cclen(number);
747 sdl = cc_len;
748 ast_mutex_lock(&enumlock);
749 ast_copy_string(sep, ienum_branchlabel, sizeof(sep)); /* default */
750 ast_mutex_unlock(&enumlock);
752 switch (ebl_alg) {
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);
757 if (sdl >= 0) {
758 ast_copy_string(apex, n_apex, sizeof(apex));
759 ast_verb(2, "EBL ENUM: sep=%s, apex='%s'\n", sep, n_apex);
760 } else {
761 sdl = cc_len;
763 break;
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);
768 if (sdl < 0)
769 sdl = cc_len;
770 break;
772 case ENUMLOOKUP_BLR_CC: /* BLR is at the country-code level */
773 default:
774 sdl = cc_len;
775 break;
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);
780 return 0;
782 ast_copy_string(left, number + sdl, sizeof(left));
784 ast_mutex_lock(&enumlock);
785 ast_copy_string(middle, sep, sizeof(middle) - 1);
786 strcat(middle, ".");
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");
792 return -1;
795 p1 = middle + strlen(middle);
796 for (p2 = (char *) number + sdl - 1; p2 >= number; p2--) {
797 if (isdigit(*p2)) {
798 *p1++ = *p2;
799 *p1++ = '.';
802 *p1 = '\0';
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");
809 return -1;
812 /* flip left into domain */
813 p1 = domain;
814 for (p2 = left + strlen(left); p2 >= left; p2--) {
815 if (isdigit(*p2)) {
816 *p1++ = *p2;
817 *p1++ = '.';
820 *p1 = '\0';
822 if (chan && ast_autoservice_start(chan) < 0) {
823 ast_free(context);
824 return -1;
827 spaceleft = sizeof(tmp) - 2;
828 ast_copy_string(tmp, domain, spaceleft);
829 spaceleft -= strlen(domain);
831 if (*middle) {
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));
844 if (ret < 0) {
845 ast_debug(1, "No such number found: %s (%s)\n", tmp, strerror(errno));
846 strcpy(dst, "0");
847 ret = 0;
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;
862 continue;
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);
880 break;
883 } else if (!(context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
884 context->dst[0] = 0;
885 } else if ((context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
886 snprintf(context->dst,context->dstlen,"%d",context->count);
889 if (chan)
890 ret |= ast_autoservice_stop(chan);
892 if (!argcontext) {
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);
898 ast_free(context);
899 } else
900 *argcontext = context;
902 return ret;
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;
916 char tmp[259 + 512];
917 int pos = strlen(number) - 1;
918 int newpos = 0;
919 int ret = -1;
921 ast_debug(4, "ast_get_txt: Number = '%s', suffix = '%s'\n", number, suffix);
923 if (chan && ast_autoservice_start(chan) < 0) {
924 return -1;
927 if (pos > 128) {
928 pos = 128;
931 while (pos >= 0) {
932 if (isdigit(number[pos])) {
933 tmp[newpos++] = number[pos];
934 tmp[newpos++] = '.';
936 pos--;
939 ast_copy_string(&tmp[newpos], suffix, sizeof(tmp) - newpos);
941 if (ret < 0) {
942 ast_debug(2, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
943 ret = 0;
944 } else {
945 ast_copy_string(txt, context.txt, txtlen);
947 if (chan) {
948 ret |= ast_autoservice_stop(chan);
950 return ret;
953 /*! \brief Initialize the ENUM support subsystem */
954 static int private_enum_init(int reload)
956 struct ast_config *cfg;
957 const char *string;
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)
961 return 0;
963 /* Destroy existing list */
964 ast_mutex_lock(&enumlock);
965 if (cfg) {
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;
979 else
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");
986 return 0;
989 int ast_enum_init(void)
991 return private_enum_init(0);
994 int ast_enum_reload(void)
996 return private_enum_init(1);