Sat Mar 24 23:28:38 2007

Asterisk developer's documentation


enum.h File Reference

DNS and ENUM functions. More...

#include "asterisk/channel.h"

Include dependency graph for enum.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int ast_enum_init (void)
int ast_enum_reload (void)
int ast_get_enum (struct ast_channel *chan, const char *number, char *location, int maxloc, char *technology, int maxtech, char *suffix, char *options)
 Lookup entry in ENUM Returns 1 if found, 0 if not found, -1 on hangup.
int ast_get_txt (struct ast_channel *chan, const char *number, char *location, int maxloc, char *technology, int maxtech, char *txt, int maxtxt)
 Lookup DNS TXT record (used by app TXTCIDnum.


Detailed Description

DNS and ENUM functions.

Definition in file enum.h.


Function Documentation

int ast_enum_init void   ) 
 

Definition at line 622 of file enum.c.

References ast_config_destroy(), ast_config_load(), ast_mutex_lock(), ast_mutex_unlock(), ast_variable_browse(), cfg, enum_newtoplev(), free, ast_variable::name, ast_variable::next, s, TOPLEV, toplevs, and ast_variable::value.

Referenced by ast_enum_reload().

00623 {
00624    struct ast_config *cfg;
00625    struct enum_search *s, *sl;
00626    struct ast_variable *v;
00627 
00628    /* Destroy existing list */
00629    ast_mutex_lock(&enumlock);
00630    s = toplevs;
00631    while(s) {
00632       sl = s;
00633       s = s->next;
00634       free(sl);
00635    }
00636    toplevs = NULL;
00637    cfg = ast_config_load("enum.conf");
00638    if (cfg) {
00639       sl = NULL;
00640       v = ast_variable_browse(cfg, "general");
00641       while(v) {
00642          if (!strcasecmp(v->name, "search")) {
00643             s = enum_newtoplev(v->value);
00644             if (s) {
00645                if (sl)
00646                   sl->next = s;
00647                else
00648                   toplevs = s;
00649                sl = s;
00650             }
00651          }
00652          v = v->next;
00653       }
00654       ast_config_destroy(cfg);
00655    } else {
00656       toplevs = enum_newtoplev(TOPLEV);
00657    }
00658    enumver++;
00659    ast_mutex_unlock(&enumlock);
00660    return 0;
00661 }

int ast_enum_reload void   ) 
 

Definition at line 663 of file enum.c.

References ast_enum_init().

Referenced by ast_module_reload().

00664 {
00665    return ast_enum_init();
00666 }

int ast_get_enum struct ast_channel chan,
const char *  number,
char *  location,
int  maxloc,
char *  technology,
int  maxtech,
char *  suffix,
char *  options
 

Lookup entry in ENUM Returns 1 if found, 0 if not found, -1 on hangup.

Parameters:
chan Channel
number E164 number with or without the leading +
location Number returned (or SIP uri)
maxloc Max length
technology Technology (from url scheme in response) You can set it to get particular answer RR, if there are many techs in DNS response, example: "sip" If you need any record, then set it to empty string
maxtech Max length
suffix Zone suffix (if is NULL then use enum.conf 'search' variable)
options Options ('c' to count number of NAPTR RR, or number - the position of required RR in the answer list

Definition at line 384 of file enum.c.

References ast_autoservice_start(), ast_mutex_lock(), context, ENUMLOOKUP_OPTIONS_COUNT, enum_context::naptrinput, s, and toplevs.

Referenced by enumlookup_exec(), and function_enum().

00385 {
00386    struct enum_context context;
00387    char tmp[259 + 512];
00388    char naptrinput[512];
00389    int pos = strlen(number) - 1;
00390    int newpos = 0;
00391    int ret = -1;
00392    struct enum_search *s = NULL;
00393    int version = -1;
00394    /* for ISN rewrite */
00395    char *p1 = NULL;
00396    char *p2 = NULL;
00397    int k = 0;
00398    int i = 0;
00399    int z = 0;
00400 
00401    if (number[0] == 'n') {
00402       strncpy(naptrinput, number+1, sizeof(naptrinput));
00403    } else {
00404       strncpy(naptrinput, number, sizeof(naptrinput));
00405    }
00406 
00407    context.naptrinput = naptrinput; /* The number */
00408    context.dst = dst;         /* Return string */
00409    context.dstlen = dstlen;
00410    context.tech = tech;
00411    context.techlen = techlen;
00412    context.options = 0;
00413    context.position = 1;
00414    context.naptr_rrs = NULL;
00415    context.naptr_rrs_count = 0;
00416 
00417    if (options != NULL){
00418       if (*options == 'c'){
00419          context.options = ENUMLOOKUP_OPTIONS_COUNT;
00420          context.position = 0;
00421       } else {
00422          context.position = atoi(options);
00423          if (context.position < 1)
00424             context.position = 1;
00425       }
00426    }
00427 
00428    if (pos > 128)
00429       pos = 128;
00430 
00431    /* ISN rewrite */
00432    p1 = strchr(number, '*');
00433 
00434    if (number[0] == 'n') { /* do not perform ISN rewrite ('n' is testing flag) */
00435       p1 = NULL;
00436       k = 1; /* strip 'n' from number */
00437    }
00438 
00439    if (p1 != NULL) {
00440       p2 = p1+1;
00441       while (p1 > number){
00442          p1--;
00443          tmp[newpos++] = *p1;
00444          tmp[newpos++] = '.';
00445       }
00446       if (*p2) {
00447          while(*p2 && newpos < 128){
00448             tmp[newpos++] = *p2;
00449             p2++;
00450          }
00451          tmp[newpos++] = '.';
00452       }
00453 
00454    } else {
00455       while (pos >= k) {
00456          if (isdigit(number[pos])) {
00457             tmp[newpos++] = number[pos];
00458             tmp[newpos++] = '.';
00459          }
00460          pos--;
00461       }
00462    }
00463 
00464    if (chan && ast_autoservice_start(chan) < 0)
00465       return -1;
00466 
00467    for (;;) {
00468       ast_mutex_lock(&enumlock);
00469       if (version != enumver) {
00470          /* Ooh, a reload... */
00471          s = toplevs;
00472          version = enumver;
00473       } else {
00474          s = s->next;
00475       }
00476       if (suffix != NULL) {
00477          strncpy(tmp + newpos, suffix, sizeof(tmp) - newpos - 1);
00478       } else if (s) {
00479          strncpy(tmp + newpos, s->toplev, sizeof(tmp) - newpos - 1);
00480       }
00481       ast_mutex_unlock(&enumlock);
00482       if (!s)
00483          break;
00484       ret = ast_search_dns(&context, tmp, C_IN, T_NAPTR, enum_callback);
00485       if (ret > 0)
00486          break;
00487       if (suffix != NULL)
00488                        break;
00489    }
00490    if (ret < 0) {
00491       ast_log(LOG_DEBUG, "No such number found: %s (%s)\n", tmp, strerror(errno));
00492       ret = 0;
00493    }
00494 
00495        if (context.naptr_rrs_count >= context.position && ! (context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
00496                /* sort array by NAPTR order/preference */
00497                for (k=0; k<context.naptr_rrs_count; k++) {
00498                        for (i=0; i<context.naptr_rrs_count; i++) {
00499                                /* use order first and then preference to compare */
00500                                if ((ntohs(context.naptr_rrs[k].naptr.order) < ntohs(context.naptr_rrs[i].naptr.order)
00501                                                && context.naptr_rrs[k].sort_pos > context.naptr_rrs[i].sort_pos)
00502                                        || (ntohs(context.naptr_rrs[k].naptr.order) > ntohs(context.naptr_rrs[i].naptr.order)
00503                                                && context.naptr_rrs[k].sort_pos < context.naptr_rrs[i].sort_pos)){
00504                                        z = context.naptr_rrs[k].sort_pos;
00505                                        context.naptr_rrs[k].sort_pos = context.naptr_rrs[i].sort_pos;
00506                                        context.naptr_rrs[i].sort_pos = z;
00507                                        continue;
00508                                }
00509                                if (ntohs(context.naptr_rrs[k].naptr.order) == ntohs(context.naptr_rrs[i].naptr.order)) {
00510                                        if ((ntohs(context.naptr_rrs[k].naptr.pref) < ntohs(context.naptr_rrs[i].naptr.pref)
00511                                                        && context.naptr_rrs[k].sort_pos > context.naptr_rrs[i].sort_pos)
00512                                                || (ntohs(context.naptr_rrs[k].naptr.pref) > ntohs(context.naptr_rrs[i].naptr.pref)
00513                                                        && context.naptr_rrs[k].sort_pos < context.naptr_rrs[i].sort_pos)){
00514                                                z = context.naptr_rrs[k].sort_pos;
00515                                                context.naptr_rrs[k].sort_pos = context.naptr_rrs[i].sort_pos;
00516                                                context.naptr_rrs[i].sort_pos = z;
00517                                        }
00518                                }
00519                        }
00520                }
00521                for (k=0; k<context.naptr_rrs_count; k++) {
00522                        if (context.naptr_rrs[k].sort_pos == context.position-1) {
00523                                ast_copy_string(context.dst, context.naptr_rrs[k].result, dstlen);
00524                                ast_copy_string(context.tech, context.naptr_rrs[k].tech, techlen);
00525                                break;
00526                        }
00527                }
00528        } else if (!(context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
00529                context.dst[0] = 0;
00530        }
00531 
00532    if (chan)
00533       ret |= ast_autoservice_stop(chan);
00534 
00535    for (k=0; k<context.naptr_rrs_count; k++) {
00536       free(context.naptr_rrs[k].result);
00537       free(context.naptr_rrs[k].tech);
00538    }
00539 
00540    free(context.naptr_rrs);
00541 
00542    return ret;
00543 }

int ast_get_txt struct ast_channel chan,
const char *  number,
char *  location,
int  maxloc,
char *  technology,
int  maxtech,
char *  txt,
int  maxtxt
 

Lookup DNS TXT record (used by app TXTCIDnum.

Parameters:
chan Channel
number E164 number with or without the leading +
location Number returned (or SIP uri)
maxloc Max length of number
technology Technology (not used in TXT records)
maxtech Max length
txt Text string (return value)
maxtxt Max length of "txt"

Definition at line 548 of file enum.c.

References ast_autoservice_start(), ast_mutex_lock(), context, enum_context::naptrinput, s, and toplevs.

Referenced by function_txtcidname(), and txtcidname_exec().

00549 {
00550    struct enum_context context;
00551    char tmp[259 + 512];
00552    char naptrinput[512] = "+";
00553    int pos = strlen(number) - 1;
00554    int newpos = 0;
00555    int ret = -1;
00556    struct enum_search *s = NULL;
00557    int version = -1;
00558 
00559    strncat(naptrinput, number, sizeof(naptrinput) - 2);
00560 
00561    context.naptrinput = naptrinput;
00562    context.dst = dst;
00563    context.dstlen = dstlen;
00564    context.tech = tech;
00565    context.techlen = techlen;
00566    context.txt = txt;
00567    context.txtlen = txtlen;
00568 
00569    if (pos > 128)
00570       pos = 128;
00571    while (pos >= 0) {
00572       tmp[newpos++] = number[pos--];
00573       tmp[newpos++] = '.';
00574    }
00575 
00576    if (chan && ast_autoservice_start(chan) < 0)
00577       return -1;
00578 
00579    for (;;) {
00580       ast_mutex_lock(&enumlock);
00581       if (version != enumver) {
00582          /* Ooh, a reload... */
00583          s = toplevs;
00584          version = enumver;
00585       } else {
00586          s = s->next;
00587       }
00588       if (s) {
00589          strncpy(tmp + newpos, s->toplev, sizeof(tmp) - newpos - 1);
00590       }
00591       ast_mutex_unlock(&enumlock);
00592       if (!s)
00593          break;
00594 
00595       ret = ast_search_dns(&context, tmp, C_IN, T_TXT, txt_callback);
00596       if (ret > 0)
00597          break;
00598    }
00599    if (ret < 0) {
00600       ast_log(LOG_DEBUG, "No such number found: %s (%s)\n", tmp, strerror(errno));
00601       ret = 0;
00602    }
00603    if (chan)
00604       ret |= ast_autoservice_stop(chan);
00605    return ret;
00606 }


Generated on Sat Mar 24 23:28:38 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.6