12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/nameser.h>
- #ifdef __APPLE__
- #if __APPLE_CC__ >= 1495
- #include <arpa/nameser_compat.h>
- #endif
- #endif
- #include <resolv.h>
- #include <ctype.h>
- #include <regex.h>
- #include "asterisk/enum.h"
- #include "asterisk/dns.h"
- #include "asterisk/channel.h"
- #include "asterisk/config.h"
- #include "asterisk/utils.h"
- #include "asterisk/manager.h"
- #ifdef __APPLE__
- #undef T_NAPTR
- #define T_NAPTR 35
- #endif
- #ifdef __APPLE__
- #undef T_TXT
- #define T_TXT 16
- #endif
- static char ienum_branchlabel[32] = "i";
- #define ENUMLOOKUP_BLR_CC 0
- #define ENUMLOOKUP_BLR_TXT 1
- #define ENUMLOOKUP_BLR_EBL 2
- static int ebl_alg = ENUMLOOKUP_BLR_CC;
- #define T_EBL 65300
- AST_MUTEX_DEFINE_STATIC(enumlock);
- static int cclen(const char *number)
- {
- int cc;
- char digits[3] = "";
- if (!number || (strlen(number) < 3)) {
- return 0;
- }
- strncpy(digits, number, 2);
- if (!sscanf(digits, "%30d", &cc)) {
- return 0;
- }
- if (cc / 10 == 1 || cc / 10 == 7)
- return 1;
- if (cc == 20 || cc == 27 || (cc >= 30 && cc <= 34) || cc == 36 ||
- cc == 39 || cc == 40 || cc == 41 || (cc >= 40 && cc <= 41) ||
- (cc >= 43 && cc <= 49) || (cc >= 51 && cc <= 58) ||
- (cc >= 60 && cc <= 66) || cc == 81 || cc == 82 || cc == 84 ||
- cc == 86 || (cc >= 90 && cc <= 95) || cc == 98) {
- return 2;
- }
- return 3;
- }
- struct txt_context {
- char txt[1024];
- int txtlen;
- };
- static int txt_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
- {
- struct txt_context *c = context;
- unsigned int i;
- c->txt[0] = 0;
- c->txtlen = 0;
- if (answer == NULL) {
- return 0;
- }
-
- i = *answer++;
- len -= 1;
- if (i > len) {
- ast_log(LOG_WARNING, "txt_callback: malformed TXT record.\n");
- return 0;
- }
- if (i >= sizeof(c->txt)) {
- ast_log(LOG_WARNING, "txt_callback: TXT record too long.\n");
- i = sizeof(c->txt) - 1;
- }
- ast_copy_string(c->txt, (char *)answer, i + 1);
- c->txtlen = i;
- return 1;
- }
- static int blr_txt(const char *cc, const char *suffix)
- {
- struct txt_context context;
- char domain[128] = "";
- char *p1, *p2;
- int ret;
- ast_mutex_lock(&enumlock);
- ast_verb(4, "blr_txt() cc='%s', suffix='%s', c_bl='%s'\n", cc, suffix, ienum_branchlabel);
- if (sizeof(domain) < (strlen(cc) * 2 + strlen(ienum_branchlabel) + strlen(suffix) + 2)) {
- ast_mutex_unlock(&enumlock);
- ast_log(LOG_WARNING, "ERROR: string sizing in blr_txt.\n");
- return -1;
- }
- p1 = domain + snprintf(domain, sizeof(domain), "%s.", ienum_branchlabel);
- ast_mutex_unlock(&enumlock);
- for (p2 = (char *) cc + strlen(cc) - 1; p2 >= cc; p2--) {
- if (isdigit(*p2)) {
- *p1++ = *p2;
- *p1++ = '.';
- }
- }
- strcat(p1, suffix);
- ast_verb(4, "blr_txt() FQDN for TXT record: %s, cc was %s\n", domain, cc);
- ret = ast_search_dns(&context, domain, C_IN, T_TXT, txt_callback);
- if (ret > 0) {
- ret = atoi(context.txt);
- if ((ret >= 0) && (ret < 20)) {
- ast_verb(3, "blr_txt() BLR TXT record for %s is %d (apex: %s)\n", cc, ret, suffix);
- return ret;
- }
- }
- ast_verb(3, "blr_txt() BLR TXT record for %s not found (apex: %s)\n", cc, suffix);
- return -1;
- }
- struct ebl_context {
- unsigned char pos;
- char separator[256];
- int sep_len;
- char apex[256];
- int apex_len;
- };
- static int ebl_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
- {
- struct ebl_context *c = context;
- int i;
- c->pos = 0;
- c->separator[0] = 0;
- c->sep_len = 0;
- c->apex[0] = 0;
- c->apex_len = 0;
- if (answer == NULL) {
- return 0;
- }
-
- c->pos = *answer++;
- len -= 1;
- if ((c->pos > 15) || len < 2) {
- ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
- return 0;
- }
- i = *answer++;
- len -= 1;
- if (i > len) {
- ast_log(LOG_WARNING, "ebl_callback: malformed EBL record.\n");
- return 0;
- }
- ast_copy_string(c->separator, (char *)answer, i + 1);
- c->sep_len = i;
- answer += i;
- len -= i;
- if ((i = dn_expand((unsigned char *)fullanswer, (unsigned char *)answer + len,
- (unsigned char *)answer, c->apex, sizeof(c->apex) - 1)) < 0) {
- ast_log(LOG_WARNING, "Failed to expand hostname\n");
- return 0;
- }
- c->apex[i] = 0;
- c->apex_len = i;
- return 1;
- }
- static int blr_ebl(const char *cc, const char *suffix, char *separator, int sep_len, char* apex, int apex_len)
- {
- struct ebl_context context;
- char domain[128] = "";
- char *p1,*p2;
- int ret;
- ast_mutex_lock(&enumlock);
- ast_verb(4, "blr_ebl() cc='%s', suffix='%s', c_bl='%s'\n", cc, suffix, ienum_branchlabel);
- if (sizeof(domain) < (strlen(cc) * 2 + strlen(ienum_branchlabel) + strlen(suffix) + 2)) {
- ast_mutex_unlock(&enumlock);
- ast_log(LOG_WARNING, "ERROR: string sizing in blr_EBL.\n");
- return -1;
- }
- p1 = domain + snprintf(domain, sizeof(domain), "%s.", ienum_branchlabel);
- ast_mutex_unlock(&enumlock);
- for (p2 = (char *) cc + strlen(cc) - 1; p2 >= cc; p2--) {
- if (isdigit(*p2)) {
- *p1++ = *p2;
- *p1++ = '.';
- }
- }
- strcat(p1, suffix);
- ast_verb(4, "blr_ebl() FQDN for EBL record: %s, cc was %s\n", domain, cc);
- ret = ast_search_dns(&context, domain, C_IN, T_EBL, ebl_callback);
- if (ret > 0) {
- ret = context.pos;
- if ((ret >= 0) && (ret < 20)) {
- ast_verb(3, "blr_txt() BLR EBL record for %s is %d/%s/%s)\n", cc, ret, context.separator, context.apex);
- ast_copy_string(separator, context.separator, sep_len);
- ast_copy_string(apex, context.apex, apex_len);
- return ret;
- }
- }
- ast_verb(3, "blr_txt() BLR EBL record for %s not found (apex: %s)\n", cc, suffix);
- return -1;
- }
- static unsigned int parse_ie(char *data, unsigned int maxdatalen, unsigned char *src, unsigned int srclen)
- {
- unsigned int len, olen;
- len = olen = (unsigned int) src[0];
- src++;
- srclen--;
- if (len > srclen) {
- ast_log(LOG_WARNING, "ENUM parsing failed: Wanted %u characters, got %u\n", len, srclen);
- return -1;
- }
- if (len > maxdatalen)
- len = maxdatalen;
- memcpy(data, src, len);
- return olen + 1;
- }
- static int parse_naptr(unsigned char *dst, int dstsize, char *tech, int techsize, unsigned char *answer, int len, unsigned char *naptrinput)
- {
- char tech_return[80];
- char *oanswer = (char *)answer;
- char flags[512] = "";
- char services[512] = "";
- char *p;
- char regexp[512] = "";
- char repl[512] = "";
- char tempdst[512] = "";
- char errbuff[512] = "";
- char delim;
- char *delim2;
- char *pattern, *subst, *d;
- int res;
- int regexp_len, rc;
- static const int max_bt = 10;
- int size, matchindex;
- size_t d_len = sizeof(tempdst) - 1;
- regex_t preg;
- regmatch_t pmatch[max_bt];
- tech_return[0] = '\0';
- dst[0] = '\0';
- if (len < sizeof(struct naptr)) {
- ast_log(LOG_WARNING, "NAPTR record length too short\n");
- return -1;
- }
- answer += sizeof(struct naptr);
- len -= sizeof(struct naptr);
- if ((res = parse_ie(flags, sizeof(flags) - 1, answer, len)) < 0) {
- ast_log(LOG_WARNING, "Failed to get flags from NAPTR record\n");
- return -1;
- } else {
- answer += res;
- len -= res;
- }
- if ((res = parse_ie(services, sizeof(services) - 1, answer, len)) < 0) {
- ast_log(LOG_WARNING, "Failed to get services from NAPTR record\n");
- return -1;
- } else {
- answer += res;
- len -= res;
- }
- if ((res = parse_ie(regexp, sizeof(regexp) - 1, answer, len)) < 0) {
- ast_log(LOG_WARNING, "Failed to get regexp from NAPTR record\n");
- return -1;
- } else {
- answer += res;
- len -= res;
- }
- if ((res = dn_expand((unsigned char *)oanswer, (unsigned char *)answer + len, (unsigned char *)answer, repl, sizeof(repl) - 1)) < 0) {
- ast_log(LOG_WARNING, "Failed to expand hostname\n");
- return -1;
- }
- ast_debug(3, "NAPTR input='%s', flags='%s', services='%s', regexp='%s', repl='%s'\n",
- naptrinput, flags, services, regexp, repl);
- if (tolower(flags[0]) != 'u') {
- ast_log(LOG_WARNING, "NAPTR Flag must be 'U' or 'u'.\n");
- return -1;
- }
- p = strstr(services, "e2u+");
- if (p == NULL)
- p = strstr(services, "E2U+");
- if (p){
- p = p + 4;
- if (strchr(p, ':')){
- p = strchr(p, ':') + 1;
- }
- ast_copy_string(tech_return, p, sizeof(tech_return));
- } else {
- p = strstr(services, "+e2u");
- if (p == NULL)
- p = strstr(services, "+E2U");
- if (p) {
- *p = 0;
- p = strchr(services, ':');
- if (p)
- *p = 0;
- ast_copy_string(tech_return, services, sizeof(tech_return));
- }
- }
- regexp_len = strlen(regexp);
- if (regexp_len < 7) {
- ast_log(LOG_WARNING, "Regex too short to be meaningful.\n");
- return -1;
- }
-
- delim = regexp[0];
- delim2 = strchr(regexp + 1, delim);
- if ((delim2 == NULL) || (regexp[regexp_len - 1] != delim)) {
- ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
- return -1;
- } else if (strchr((delim2 + 1), delim) == NULL) {
- ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
- return -1;
- }
- pattern = regexp + 1;
- *delim2 = 0;
- subst = delim2 + 1;
- regexp[regexp_len - 1] = 0;
- if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
- ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n", regexp);
- return -1;
- }
- if (preg.re_nsub > ARRAY_LEN(pmatch)) {
- ast_log(LOG_WARNING, "NAPTR Regex compilation error: too many subs.\n");
- regfree(&preg);
- return -1;
- }
-
- if ((rc = regexec(&preg, (char *) naptrinput, max_bt, pmatch, 0))) {
- regerror(rc, &preg, errbuff, sizeof(errbuff));
- ast_log(LOG_WARNING, "NAPTR Regex match failed. Reason: %s\n", errbuff);
- regfree(&preg);
- return -1;
- }
- regfree(&preg);
- d = tempdst;
- d_len--;
-
- while (*subst && (d_len > 0)) {
- if ((subst[0] == '\\') && isdigit(subst[1])) {
- matchindex = (int) (subst[1] - '0');
- if (matchindex >= ARRAY_LEN(pmatch)) {
- ast_log(LOG_WARNING, "Error during regex substitution. Invalid pmatch index.\n");
- return -1;
- }
-
- size = pmatch[matchindex].rm_eo - pmatch[matchindex].rm_so;
- if (size > d_len) {
- ast_log(LOG_WARNING, "Not enough space during NAPTR regex substitution.\n");
- return -1;
- }
-
- if ((strlen((char *) naptrinput) >= pmatch[matchindex].rm_eo) && (pmatch[matchindex].rm_so <= pmatch[matchindex].rm_eo)) {
- memcpy(d, (naptrinput + (int) pmatch[matchindex].rm_so), size);
- d_len -= size;
- subst += 2;
- d += size;
- } else {
- ast_log(LOG_WARNING, "Error during regex substitution. Invalid backreference index.\n");
- return -1;
- }
- } else if (isprint(*subst)) {
- *d++ = *subst++;
- d_len--;
- } else {
- ast_log(LOG_WARNING, "Error during regex substitution.\n");
- return -1;
- }
- }
- *d = 0;
- ast_copy_string((char *) dst, tempdst, dstsize);
- dst[dstsize - 1] = '\0';
- if (*tech != '\0'){
- if (!strncasecmp(tech, "ALL", techsize)){
- return 0;
- }
- if (!strncasecmp(tech_return, tech, sizeof(tech_return) < techsize ? sizeof(tech_return): techsize)){
- ast_copy_string(tech, tech_return, techsize);
- return 0;
- } else {
- return 1;
- }
- }
-
- ast_copy_string(tech, tech_return, techsize);
- return 0;
- }
- #define ENUMLOOKUP_OPTIONS_COUNT 1
- #define ENUMLOOKUP_OPTIONS_ISN 2
- #define ENUMLOOKUP_OPTIONS_IENUM 4
- #define ENUMLOOKUP_OPTIONS_DIRECT 8
- static int enum_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
- {
- struct enum_context *c = context;
- void *p = NULL;
- int res;
- res = parse_naptr((unsigned char *)c->dst, c->dstlen, c->tech, c->techlen, answer, len, (unsigned char *)c->naptrinput);
- if (res < 0) {
- ast_log(LOG_WARNING, "Failed to parse naptr\n");
- return -1;
- } else if ((res == 0) && !ast_strlen_zero(c->dst)) {
- if (c->options & ENUMLOOKUP_OPTIONS_COUNT) {
- c->count++;
- snprintf(c->dst, c->dstlen, "%d", c->count);
- } else {
- if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
- c->naptr_rrs = p;
- memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
- c->naptr_rrs[c->naptr_rrs_count].result = ast_strdup(c->dst);
- c->naptr_rrs[c->naptr_rrs_count].tech = ast_strdup(c->tech);
- c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
- c->naptr_rrs_count++;
- }
- c->dst[0] = 0;
- }
- return 0;
- }
- return 0;
- }
- 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)
- {
- struct enum_context *context;
- char tmp[512];
- char domain[256];
- char left[128];
- char middle[128];
- char naptrinput[128];
- char apex[128] = "";
- int ret = -1;
-
- char *p1 = NULL;
- char *p2 = NULL;
- char *p3 = NULL;
- int k = 0;
- int i = 0;
- int z = 0;
- int spaceleft = 0;
- struct timeval time_start, time_end;
- if (ast_strlen_zero(suffix)) {
- ast_log(LOG_WARNING, "ast_get_enum need a suffix parameter now.\n");
- return -1;
- }
- ast_debug(2, "num='%s', tech='%s', suffix='%s', options='%s', record=%u\n", number, tech, suffix, options, record);
- ast_copy_string(naptrinput, number[0] == 'n' ? number + 1 : number, sizeof(naptrinput));
- if (number[0] == '+') {
- number++;
- }
- if (!(context = ast_calloc(1, sizeof(*context)))) {
- return -1;
- }
- if ((p3 = strchr(naptrinput, '*'))) {
- *p3='\0';
- }
- context->naptrinput = naptrinput;
- context->dst = dst;
- context->dstlen = dstlen;
- context->tech = tech;
- context->techlen = techlen;
- context->options = 0;
- context->position = record > 0 ? record : 1;
- context->count = 0;
- context->naptr_rrs = NULL;
- context->naptr_rrs_count = 0;
-
- if (options != NULL) {
- if (strchr(options,'s')) {
- context->options |= ENUMLOOKUP_OPTIONS_ISN;
- } else if (strchr(options,'i')) {
- context->options |= ENUMLOOKUP_OPTIONS_IENUM;
- } else if (strchr(options,'d')) {
- context->options |= ENUMLOOKUP_OPTIONS_DIRECT;
- }
- if (strchr(options,'c')) {
- context->options |= ENUMLOOKUP_OPTIONS_COUNT;
- }
- if (strchr(number,'*')) {
- context->options |= ENUMLOOKUP_OPTIONS_ISN;
- }
- }
- ast_debug(2, "ENUM options(%s): pos=%d, options='%d'\n", options, context->position, context->options);
- ast_debug(1, "n='%s', tech='%s', suffix='%s', options='%d', record='%d'\n",
- number, tech, suffix, context->options, context->position);
-
-
- ast_copy_string(left, number, sizeof(left));
- middle[0] = '\0';
-
- ast_copy_string(apex, suffix, sizeof(apex));
-
- if ((context->options & ENUMLOOKUP_OPTIONS_ISN) && (p1 = strchr(number, '*'))) {
- *p1++ = '\0';
- ast_copy_string(left, number, sizeof(left));
- ast_copy_string(middle, p1, sizeof(middle) - 1);
- strcat(middle, ".");
- ast_debug(2, "ISN ENUM: left=%s, middle='%s'\n", left, middle);
-
- } else if (context->options & ENUMLOOKUP_OPTIONS_DIRECT) {
- left[0] = 0;
- ast_copy_string(middle, number, sizeof(middle) - 1);
- strcat(middle, ".");
- ast_debug(2, "DIRECT ENUM: middle='%s'\n", middle);
-
- } else if (context->options & ENUMLOOKUP_OPTIONS_IENUM) {
- int sdl = 0;
- char cc[8];
- char sep[256], n_apex[256];
- int cc_len = cclen(number);
- sdl = cc_len;
- ast_mutex_lock(&enumlock);
- ast_copy_string(sep, ienum_branchlabel, sizeof(sep));
- ast_mutex_unlock(&enumlock);
- switch (ebl_alg) {
- case ENUMLOOKUP_BLR_EBL:
- ast_copy_string(cc, number, cc_len);
- sdl = blr_ebl(cc, suffix, sep, sizeof(sep) - 1, n_apex, sizeof(n_apex) - 1);
- if (sdl >= 0) {
- ast_copy_string(apex, n_apex, sizeof(apex));
- ast_debug(2, "EBL ENUM: sep=%s, apex='%s'\n", sep, n_apex);
- } else {
- sdl = cc_len;
- }
- break;
- case ENUMLOOKUP_BLR_TXT:
- ast_copy_string(cc, number, cc_len);
- sdl = blr_txt(cc, suffix);
- if (sdl < 0) {
- sdl = cc_len;
- }
- break;
- case ENUMLOOKUP_BLR_CC:
- default:
- sdl = cc_len;
- break;
- }
- if (sdl > strlen(number)) {
- ast_log(LOG_WARNING, "I-ENUM: subdomain location %d behind number %s\n", sdl, number);
- ast_free(context);
- return 0;
- }
- ast_copy_string(left, number + sdl, sizeof(left));
- ast_mutex_lock(&enumlock);
- ast_copy_string(middle, sep, sizeof(middle) - 1);
- strcat(middle, ".");
- ast_mutex_unlock(&enumlock);
-
- if ((sdl * 2 + strlen(middle) + 2) > sizeof(middle)) {
- ast_log(LOG_WARNING, "ast_get_enum: not enough space for I-ENUM rewrite.\n");
- ast_free(context);
- return -1;
- }
- p1 = middle + strlen(middle);
- for (p2 = (char *) number + sdl - 1; p2 >= number; p2--) {
- if (isdigit(*p2)) {
- *p1++ = *p2;
- *p1++ = '.';
- }
- }
- *p1 = '\0';
- ast_debug(2, "I-ENUM: cclen=%d, left=%s, middle='%s', apex='%s'\n", cc_len, left, middle, apex);
- }
- if (strlen(left) * 2 + 2 > sizeof(domain)) {
- ast_log(LOG_WARNING, "string to long in ast_get_enum\n");
- ast_free(context);
- return -1;
- }
-
- p1 = domain;
- for (p2 = left + strlen(left); p2 >= left; p2--) {
- if (isdigit(*p2)) {
- *p1++ = *p2;
- *p1++ = '.';
- }
- }
- *p1 = '\0';
- if (chan && ast_autoservice_start(chan) < 0) {
- ast_free(context);
- return -1;
- }
- spaceleft = sizeof(tmp) - 2;
- ast_copy_string(tmp, domain, spaceleft);
- spaceleft -= strlen(domain);
- if (*middle) {
- strncat(tmp, middle, spaceleft);
- spaceleft -= strlen(middle);
- }
- strncat(tmp,apex,spaceleft);
- time_start = ast_tvnow();
- ret = ast_search_dns(context, tmp, C_IN, T_NAPTR, enum_callback);
- time_end = ast_tvnow();
- ast_debug(2, "profiling: %s, %s, %" PRIi64 " ms\n",
- (ret == 0) ? "OK" : "FAIL", tmp, ast_tvdiff_ms(time_end, time_start));
- if (ret < 0) {
- ast_debug(1, "No such number found: %s (%s)\n", tmp, strerror(errno));
- context->naptr_rrs_count = -1;
- strcpy(dst, "0");
- ret = 0;
- }
- if (context->naptr_rrs_count >= context->position && ! (context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
-
- for (k = 0; k < context->naptr_rrs_count; k++) {
- for (i = 0; i < context->naptr_rrs_count; i++) {
-
- if ((ntohs(context->naptr_rrs[k].naptr.order) < ntohs(context->naptr_rrs[i].naptr.order)
- && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
- || (ntohs(context->naptr_rrs[k].naptr.order) > ntohs(context->naptr_rrs[i].naptr.order)
- && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
- z = context->naptr_rrs[k].sort_pos;
- context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
- context->naptr_rrs[i].sort_pos = z;
- continue;
- }
- if (ntohs(context->naptr_rrs[k].naptr.order) == ntohs(context->naptr_rrs[i].naptr.order)) {
- if ((ntohs(context->naptr_rrs[k].naptr.pref) < ntohs(context->naptr_rrs[i].naptr.pref)
- && context->naptr_rrs[k].sort_pos > context->naptr_rrs[i].sort_pos)
- || (ntohs(context->naptr_rrs[k].naptr.pref) > ntohs(context->naptr_rrs[i].naptr.pref)
- && context->naptr_rrs[k].sort_pos < context->naptr_rrs[i].sort_pos)) {
- z = context->naptr_rrs[k].sort_pos;
- context->naptr_rrs[k].sort_pos = context->naptr_rrs[i].sort_pos;
- context->naptr_rrs[i].sort_pos = z;
- }
- }
- }
- }
- for (k = 0; k < context->naptr_rrs_count; k++) {
- if (context->naptr_rrs[k].sort_pos == context->position - 1) {
- ast_copy_string(context->dst, context->naptr_rrs[k].result, dstlen);
- ast_copy_string(context->tech, context->naptr_rrs[k].tech, techlen);
- break;
- }
- }
- } else if (!(context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
- context->dst[0] = 0;
- } else if ((context->options & ENUMLOOKUP_OPTIONS_COUNT)) {
- snprintf(context->dst, context->dstlen, "%d", context->naptr_rrs_count + context->count);
- }
- if (chan) {
- ret |= ast_autoservice_stop(chan);
- }
- if (!argcontext) {
- for (k = 0; k < context->naptr_rrs_count; k++) {
- ast_free(context->naptr_rrs[k].result);
- ast_free(context->naptr_rrs[k].tech);
- }
- ast_free(context->naptr_rrs);
- ast_free(context);
- } else {
- *argcontext = context;
- }
- return ret;
- }
- int ast_get_txt(struct ast_channel *chan, const char *number, char *txt, int txtlen, char *suffix)
- {
- struct txt_context context;
- char tmp[259 + 512];
- int pos = strlen(number) - 1;
- int newpos = 0;
- int ret = -1;
- ast_debug(4, "ast_get_txt: Number = '%s', suffix = '%s'\n", number, suffix);
- if (chan && ast_autoservice_start(chan) < 0) {
- return -1;
- }
- if (pos > 128) {
- pos = 128;
- }
- while (pos >= 0) {
- if (isdigit(number[pos])) {
- tmp[newpos++] = number[pos];
- tmp[newpos++] = '.';
- }
- pos--;
- }
- ast_copy_string(&tmp[newpos], suffix, sizeof(tmp) - newpos);
- if (ret < 0) {
- ast_debug(2, "No such number found in ENUM: %s (%s)\n", tmp, strerror(errno));
- ret = 0;
- } else {
- ast_copy_string(txt, context.txt, txtlen);
- }
- if (chan) {
- ret |= ast_autoservice_stop(chan);
- }
- return ret;
- }
- static int private_enum_init(int reload)
- {
- struct ast_config *cfg;
- const char *string;
- struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
- if ((cfg = ast_config_load2("enum.conf", "enum", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
- return 0;
- if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
- return 0;
- }
-
- ast_mutex_lock(&enumlock);
- if (cfg) {
- if ((string = ast_variable_retrieve(cfg, "ienum", "branchlabel"))) {
- ast_copy_string(ienum_branchlabel, string, sizeof(ienum_branchlabel));
- }
- if ((string = ast_variable_retrieve(cfg, "ienum", "ebl_alg"))) {
- ebl_alg = ENUMLOOKUP_BLR_CC;
- if (!strcasecmp(string, "txt"))
- ebl_alg = ENUMLOOKUP_BLR_TXT;
- else if (!strcasecmp(string, "ebl"))
- ebl_alg = ENUMLOOKUP_BLR_EBL;
- else if (!strcasecmp(string, "cc"))
- ebl_alg = ENUMLOOKUP_BLR_CC;
- else
- ast_log(LOG_WARNING, "No valid parameter for ienum/ebl_alg.\n");
- }
- ast_config_destroy(cfg);
- }
- ast_mutex_unlock(&enumlock);
- manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: Enum\r\nStatus: Enabled\r\nMessage: ENUM reload Requested\r\n");
- return 0;
- }
- int ast_enum_init(void)
- {
- return private_enum_init(0);
- }
- int ast_enum_reload(void)
- {
- return private_enum_init(1);
- }
|