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
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/nameser.h>
44 #if __APPLE_CC__ >= 1495
45 #include <arpa/nameser_compat.h>
57 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
59 #include "asterisk/logger.h"
60 #include "asterisk/options.h"
61 #include "asterisk/enum.h"
62 #include "asterisk/dns.h"
63 #include "asterisk/channel.h"
64 #include "asterisk/config.h"
65 #include "asterisk/utils.h"
77 #define TOPLEV "e164.arpa." /*!< The IETF Enum standard root, managed by the ITU */
79 /* Linked list from config file */
80 static struct enum_search {
82 struct enum_search *next;
85 static int enumver = 0;
87 AST_MUTEX_DEFINE_STATIC(enumlock);
92 } __attribute__ ((__packed__));
94 /*! \brief Parse NAPTR record information elements */
95 static int parse_ie(char *data, int maxdatalen, char *src, int srclen)
99 len = olen = (int)src[0];
103 ast_log(LOG_WARNING, "Want %d, got %d\n", len, srclen);
106 if (len > maxdatalen)
108 memcpy(data, src, len);
112 /*! \brief Parse DNS NAPTR record used in ENUM ---*/
113 static int parse_naptr(char *dst, int dstsize, char *tech, int techsize, char *answer, int len, char *naptrinput)
116 char tech_return[80];
117 char *oanswer = answer;
118 char flags[512] = "";
119 char services[512] = "";
121 char regexp[512] = "";
126 char *pattern, *subst, *d;
128 int regexp_len, size, backref;
129 int d_len = sizeof(temp) - 1;
131 regmatch_t pmatch[9];
133 tech_return[0] = '\0';
137 if (len < sizeof(struct naptr)) {
138 ast_log(LOG_WARNING, "NAPTR record length too short\n");
141 answer += sizeof(struct naptr);
142 len -= sizeof(struct naptr);
143 if ((res = parse_ie(flags, sizeof(flags) - 1, answer, len)) < 0) {
144 ast_log(LOG_WARNING, "Failed to get flags from NAPTR record\n");
150 if ((res = parse_ie(services, sizeof(services) - 1, answer, len)) < 0) {
151 ast_log(LOG_WARNING, "Failed to get services from NAPTR record\n");
157 if ((res = parse_ie(regexp, sizeof(regexp) - 1, answer, len)) < 0) {
158 ast_log(LOG_WARNING, "Failed to get regexp from NAPTR record\n");
165 if ((res = dn_expand((unsigned char *)oanswer, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
166 ast_log(LOG_WARNING, "Failed to expand hostname\n");
170 if (option_debug > 2) /* Advanced NAPTR debugging */
171 ast_log(LOG_DEBUG, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
172 naptrinput, flags, services, regexp, repl);
174 if (tolower(flags[0]) != 'u') {
175 ast_log(LOG_WARNING, "NAPTR Flag must be 'U' or 'u'.\n");
179 p = strstr(services, "e2u+");
181 p = strstr(services, "E2U+");
185 p = strchr(p, ':') + 1;
187 ast_copy_string(tech_return, p, sizeof(tech_return));
190 p = strstr(services, "+e2u");
192 p = strstr(services, "+E2U");
195 p = strchr(services, ':');
198 ast_copy_string(tech_return, services, sizeof(tech_return));
203 ast_copy_string(regexp, "!^\\+43(.*)$!\\1@bla.fasel!", sizeof(regexp) - 1);
206 regexp_len = strlen(regexp);
207 if (regexp_len < 7) {
208 ast_log(LOG_WARNING, "Regex too short to be meaningful.\n");
214 delim2 = strchr(regexp + 1, delim);
215 if ((delim2 == NULL) || (regexp[regexp_len-1] != delim)) {
216 ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n",regexp);
220 pattern = regexp + 1;
223 regexp[regexp_len-1] = 0;
226 printf("Pattern: %s\n", pattern);
227 printf("Subst: %s\n", subst);
228 printf("Input: %s\n", naptrinput);
232 * now do the regex wizardry.
235 if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
236 ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n",regexp);
240 if (preg.re_nsub > 9) {
241 ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
246 if (regexec(&preg, naptrinput, 9, pmatch, 0)) {
247 ast_log(LOG_WARNING, "NAPTR Regex match failed.\n");
255 while (*subst && (d_len > 0)) {
256 if ((subst[0] == '\\') && isdigit(subst[1]) && (pmatch[subst[1]-'0'].rm_so != -1)) {
257 backref = subst[1]-'0';
258 size = pmatch[backref].rm_eo - pmatch[backref].rm_so;
260 ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
263 memcpy(d, naptrinput + pmatch[backref].rm_so, size);
267 } else if (isprint(*subst)) {
271 ast_log(LOG_WARNING, "Error during regex substitution.\n");
276 ast_copy_string(dst, temp, dstsize);
277 dst[dstsize - 1] = '\0';
279 if (*tech != '\0'){ /* check if it is requested NAPTR */
280 if (!strncasecmp(tech, "ALL", techsize)){
281 return 1; /* return or count any RR */
283 if (!strncasecmp(tech_return, tech, sizeof(tech_return)<techsize?sizeof(tech_return):techsize)){
284 ast_copy_string(tech, tech_return, techsize);
285 return 1; /* we got out RR */
286 } else { /* go to the next RR in the DNS answer */
291 /* tech was not specified, return first parsed RR */
292 ast_copy_string(tech, tech_return, techsize);
297 /* do not return requested value, just count RRs and return thei number in dst */
298 #define ENUMLOOKUP_OPTIONS_COUNT 1
300 struct enum_naptr_rr {
301 struct naptr naptr; /* order and preference of RR */
302 char *result; /* result of naptr parsing,e.g.: tel:+5553 */
303 char *tech; /* Technology (from URL scheme) */
304 int sort_pos; /* sort position */
307 struct enum_context {
308 char *dst; /* Destination part of URL from ENUM */
309 int dstlen; /* Length */
310 char *tech; /* Technology (from URL scheme) */
311 int techlen; /* Length */
312 char *txt; /* TXT record in TXT lookup */
313 int txtlen; /* Length */
314 char *naptrinput; /* The number to lookup */
315 int position; /* used as counter for RRs or specifies position of required RR */
316 int options; /* options , see ENUMLOOKUP_OPTIONS_* defined above */
317 struct enum_naptr_rr *naptr_rrs; /* array of parsed NAPTR RRs */
318 int naptr_rrs_count; /* Size of array naptr_rrs */
321 /*! \brief Callback for TXT record lookup */
322 static int txt_callback(void *context, char *answer, int len, char *fullanswer)
324 struct enum_context *c = (struct enum_context *)context;
326 printf("ENUMTXT Called\n");
329 if (answer == NULL) {
335 /* skip over first byte, as for some reason it's a vertical tab character */
339 /* answer is not null-terminated, but should be */
340 /* this is safe to do, as answer has extra bytes on the end we can
341 safely overwrite with a null */
343 /* now increment len so that len includes the null, so that we can
344 compare apples to apples */
347 /* finally, copy the answer into c->txt */
348 ast_copy_string(c->txt, answer, len < c->txtlen ? len : (c->txtlen));
350 /* just to be safe, let's make sure c->txt is null terminated */
351 c->txt[(c->txtlen)-1] = '\0';
356 /*! \brief Callback from ENUM lookup function */
357 static int enum_callback(void *context, char *answer, int len, char *fullanswer)
359 struct enum_context *c = context;
363 res = parse_naptr(c->dst, c->dstlen, c->tech, c->techlen, answer, len, c->naptrinput);
366 ast_log(LOG_WARNING, "Failed to parse naptr :(\n");
368 } else if (res > 0 && !ast_strlen_zero(c->dst)){ /* ok, we got needed NAPTR */
369 if (c->options & ENUMLOOKUP_OPTIONS_COUNT){ /* counting RRs */
371 snprintf(c->dst, c->dstlen, "%d", c->position);
373 if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
375 memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
376 /* printf("order=%d, pref=%d\n", ntohs(c->naptr_rrs[c->naptr_rrs_count].naptr.order), ntohs(c->naptr_rrs[c->naptr_rrs_count].naptr.pref)); */
377 c->naptr_rrs[c->naptr_rrs_count].result = strdup(c->dst);
378 c->naptr_rrs[c->naptr_rrs_count].tech = strdup(c->tech);
379 c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
380 c->naptr_rrs_count++;
387 if (c->options & ENUMLOOKUP_OPTIONS_COUNT) { /* counting RRs */
388 snprintf(c->dst, c->dstlen, "%d", c->position);
394 /*! \brief ENUM lookup */
395 int ast_get_enum(struct ast_channel *chan, const char *number, char *dst, int dstlen, char *tech, int techlen, char* suffix, char* options)
397 struct enum_context context;
399 char naptrinput[512];
400 int pos = strlen(number) - 1;
403 struct enum_search *s = NULL;
405 /* for ISN rewrite */
412 if (number[0] == 'n') {
413 strncpy(naptrinput, number+1, sizeof(naptrinput));
415 strncpy(naptrinput, number, sizeof(naptrinput));
418 context.naptrinput = naptrinput; /* The number */
419 context.dst = dst; /* Return string */
420 context.dstlen = dstlen;
422 context.techlen = techlen;
424 context.position = 1;
425 context.naptr_rrs = NULL;
426 context.naptr_rrs_count = 0;
428 if (options != NULL) {
429 if (*options == 'c') {
430 context.options = ENUMLOOKUP_OPTIONS_COUNT;
431 context.position = 0;
433 context.position = atoi(options);
434 if (context.position < 1)
435 context.position = 1;
443 p1 = strchr(number, '*');
445 if (number[0] == 'n') { /* do not perform ISN rewrite ('n' is testing flag) */
447 k = 1; /* strip 'n' from number */
458 while(*p2 && newpos < 128){
467 if (isdigit(number[pos])) {
468 tmp[newpos++] = number[pos];
475 if (chan && ast_autoservice_start(chan) < 0)
479 ast_mutex_lock(&enumlock);
480 if (version != enumver) {
481 /* Ooh, a reload... */
487 if (suffix != NULL) {
488 strncpy(tmp + newpos, suffix, sizeof(tmp) - newpos - 1);
490 strncpy(tmp + newpos, s->toplev, sizeof(tmp) - newpos - 1);
492 ast_mutex_unlock(&enumlock);
495 ret = ast_search_dns(&context, tmp, C_IN, T_NAPTR, enum_callback);
502 ast_log(LOG_DEBUG, "No such number found: %s (%s)\n", tmp, strerror(errno));
507 if (context.naptr_rrs_count >= context.position && ! (context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
508 /* sort array by NAPTR order/preference */
509 for (k=0; k<context.naptr_rrs_count; k++) {
510 for (i=0; i<context.naptr_rrs_count; i++) {
511 /* use order first and then preference to compare */
512 if ((ntohs(context.naptr_rrs[k].naptr.order) < ntohs(context.naptr_rrs[i].naptr.order)
513 && context.naptr_rrs[k].sort_pos > context.naptr_rrs[i].sort_pos)
514 || (ntohs(context.naptr_rrs[k].naptr.order) > ntohs(context.naptr_rrs[i].naptr.order)
515 && context.naptr_rrs[k].sort_pos < context.naptr_rrs[i].sort_pos)){
516 z = context.naptr_rrs[k].sort_pos;
517 context.naptr_rrs[k].sort_pos = context.naptr_rrs[i].sort_pos;
518 context.naptr_rrs[i].sort_pos = z;
521 if (ntohs(context.naptr_rrs[k].naptr.order) == ntohs(context.naptr_rrs[i].naptr.order)) {
522 if ((ntohs(context.naptr_rrs[k].naptr.pref) < ntohs(context.naptr_rrs[i].naptr.pref)
523 && context.naptr_rrs[k].sort_pos > context.naptr_rrs[i].sort_pos)
524 || (ntohs(context.naptr_rrs[k].naptr.pref) > ntohs(context.naptr_rrs[i].naptr.pref)
525 && context.naptr_rrs[k].sort_pos < context.naptr_rrs[i].sort_pos)){
526 z = context.naptr_rrs[k].sort_pos;
527 context.naptr_rrs[k].sort_pos = context.naptr_rrs[i].sort_pos;
528 context.naptr_rrs[i].sort_pos = z;
533 for (k=0; k<context.naptr_rrs_count; k++) {
534 if (context.naptr_rrs[k].sort_pos == context.position-1) {
535 ast_copy_string(context.dst, context.naptr_rrs[k].result, dstlen);
536 ast_copy_string(context.tech, context.naptr_rrs[k].tech, techlen);
540 } else if (!(context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
544 ret |= ast_autoservice_stop(chan);
546 for (k=0; k<context.naptr_rrs_count; k++) {
547 free(context.naptr_rrs[k].result);
548 free(context.naptr_rrs[k].tech);
551 free(context.naptr_rrs);
556 /*! \brief Get TXT record from DNS.
557 Really has nothing to do with enum, but anyway...
559 int ast_get_txt(struct ast_channel *chan, const char *number, char *dst, int dstlen, char *tech, int techlen, char *txt, int txtlen)
561 struct enum_context context;
563 char naptrinput[512] = "+";
564 int pos = strlen(number) - 1;
567 struct enum_search *s = NULL;
570 strncat(naptrinput, number, sizeof(naptrinput) - 2);
572 context.naptrinput = naptrinput;
574 context.dstlen = dstlen;
576 context.techlen = techlen;
578 context.txtlen = txtlen;
583 tmp[newpos++] = number[pos--];
587 if (chan && ast_autoservice_start(chan) < 0)
591 ast_mutex_lock(&enumlock);
592 if (version != enumver) {
593 /* Ooh, a reload... */
600 strncpy(tmp + newpos, s->toplev, sizeof(tmp) - newpos - 1);
602 ast_mutex_unlock(&enumlock);
606 ret = ast_search_dns(&context, tmp, C_IN, T_TXT, txt_callback);
611 if (option_debug > 1)
612 ast_log(LOG_DEBUG, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
616 ret |= ast_autoservice_stop(chan);
620 /*! \brief Add enum tree to linked list */
621 static struct enum_search *enum_newtoplev(char *s)
623 struct enum_search *tmp;
625 if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
626 ast_copy_string(tmp->toplev, s, sizeof(tmp->toplev));
631 /*! \brief Initialize the ENUM support subsystem */
632 int ast_enum_init(void)
634 struct ast_config *cfg;
635 struct enum_search *s, *sl;
636 struct ast_variable *v;
638 /* Destroy existing list */
639 ast_mutex_lock(&enumlock);
647 cfg = ast_config_load("enum.conf");
650 v = ast_variable_browse(cfg, "general");
652 if (!strcasecmp(v->name, "search")) {
653 s = enum_newtoplev(v->value);
664 ast_config_destroy(cfg);
666 toplevs = enum_newtoplev(TOPLEV);
669 ast_mutex_unlock(&enumlock);
673 int ast_enum_reload(void)
675 return ast_enum_init();