/* gateways.c: map a telephone number to an appropriate SIP url. *//* Copyright 2001 Columbia University.  All rights reserved. */#include <string.h>#include "error.h"#include "uri.h"#include "telmap.h"#include <stdlib.h>#include "config.h"#include "strdupn.h"#ifdef USE_ENUM// Enum adding begin#include "formatter.h"#include "e164query.h"// Enum adding endtelmap_err tel2uri(uri_t *uri_dest, char *src);#endif#ifdef USE_TRIP#include "tripthread.h"#endif/* * map_tel() * Map a telephone number to an appropriate gateway. */telmap_err map_tel(uri_t *uri_dest, const uri_t *uri_src,                   const char *user_gateway_class){                   #ifdef USE_ENUM  //Declare the return variable for ENUM.  telmap_err telret;#endif  //Allocating memory to find gateway.  memset(uri_dest, 0, sizeof(uri_t));  /*   * If TRIP is present   *    try using trip   *    return if success   * If Enum is present   *    try using enum   *    return if success   * Try using map_gateway   *      */  debug(DEBUG_MISC, "map_tel", "Called\n");    //Seeing what to use to find an appropriate gateway.                                                          #ifdef USE_TRIP  if(config.StartTrip) {    char *gateway; //Gateway.    //Call the LSSessionManger in order to go thru the TRIB tables     //and get the gateway.    debug(DEBUG_MISC, "map_tel", "Calling get_trip_route\n");        gateway = get_trip_route(uri_src->host);        if (gateway != NULL && *gateway != '\0') {      *uri_dest = URI_Parse(gateway);      debug(DEBUG_MISC, "map_tel", "Mapped using TRIP to %s:%s@%s:%u\n",	    uri_dest->scheme ? uri_dest->scheme : "<No scheme>",	    uri_dest->user ? uri_dest->user : "<No user>",	    uri_dest->host ? uri_dest->host : "<No host>",	    uri_dest->port == 0 ? 5060 : uri_dest->port);            free_valid(gateway);      return TELMAP_OK;    }    free_valid(gateway);  }#endif#ifdef USE_ENUM  if (config.EnumRoot != NULL) {    telret = tel2uri(uri_dest, uri_src->host);    if (telret == TELMAP_OK) {       debug(DEBUG_MISC, "map_tel", "Mapped using ENUM to %s:%s@%s:%u\n",	    uri_dest->scheme ? uri_dest->scheme : "<No scheme>",	    uri_dest->user ? uri_dest->user : "<No user>",	    uri_dest->host ? uri_dest->host : "<No host>",	    uri_dest->port == 0 ? 5060 : uri_dest->port);            return telret;    } //End if.  }#endif  //If you can't search for a gateway with ENUM, use configuration database.  return map_gateway(config.database, 		     uri_dest, uri_src, user_gateway_class, NULL, NULL);} /* map_tel */#ifdef USE_TRIP/* * trip_gateway() * Map a telephone number to an appropritate gateway * * @para  uri_dest  -  if map succeed, uri_dest points to the mapped uri *                  -  trip_gateway allocate the mem for uri_dest * @para  src       -  telephone number for map, which consists of country *                  -  code, and local code (like 12129397402), without any *                  -  other characters * @return telmap_err */telmap_err trip_gateway(uri_t *uri_dest, char *src){  telmap_err trip_telret = TELMAP_ERR;  //Return value.  char *gateway; //Gateway.  //Call the LSSessionManger in order to go thru the TRIB tables and get the  //gateway.  gateway = get_trip_route(src);  if (gateway == NULL) {    trip_telret = TELMAP_NOTFOUND;  }  else{    *uri_dest = URI_Parse(gateway);    trip_telret = TELMAP_OK;  }   return trip_telret;}#endif#ifdef USE_ENUM/* * tel2uri() * Map a enum telephone number to an appropritate uri. * If multiple results finded, return the first none tel uri. * * @para  uri_dest  -  if map succeed, uri_dest points to the mapped uri *                  -  tel2url allocate the mem for uri_dest * @para  src       -  telephone number for map, which consists of country *                  -  code, and local code (like 12129397402), without any *                  -  other characters * @return telmap_err */#define MAX_ENUM_RESULTS 10telmap_err tel2uri(uri_t *uri_dest, char *src) {  char *filter=NULL;  char *e164;  char *e164_fqd;  ENUM_record *dns_results[MAX_ENUM_RESULTS];  int i, ret;  telmap_err telret = TELMAP_ERR;  /*   ** Check the given E.164 telephone number, strip out certain   ** allowable non-numeric characters.   */  if((e164=reformat_e164(src))==NULL)    return TELMAP_ERR;  /*   ** Create a fully-qualified ENUM domain name from the telephone number.   */  if((e164_fqd=format_e164_fqd(NULL, e164, config.EnumRoot))==NULL) {    free(e164);    return TELMAP_ERR;  }  /*   ** Resolve the given the fully-qualified domain and filter.   */  if((ret=resolve_enum(e164_fqd,filter,dns_results,MAX_ENUM_RESULTS))<1){    free(e164);    free(e164_fqd);    return TELMAP_ERR;    }  if (ret == 0) {    telret = TELMAP_NOTFOUND;    goto done;  }  // For multiple results, return the first none tel uri  // because it only supports one-to-one map currently  if (ret > 1) {    for (i = 0; i < ret; i++)      if (strcasecmp(dns_results[i]->service, "tel") == 0)	continue;    free(uri_dest);    *uri_dest = URI_Parse(dns_results[i]->uri);    telret = TELMAP_OK;    goto done;  }  if (ret == 1) {    if (strcasecmp(dns_results[0]->service, "tel") == 0)      telret = tel2uri(uri_dest, (dns_results[0]->uri) + 5 );    else {      free(uri_dest);      *uri_dest = URI_Parse(dns_results[0]->uri);      telret = TELMAP_OK;    }  }  done:  free(e164);  free(e164_fqd);  free_enum_results(dns_results, ret);    return telret;}#endif