<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][17078] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=17078">17078</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2010-03-23 11:16:48 -0500 (Tue, 23 Mar 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>in progress mod_lcr with limit integration</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchbranchesrupa_limitsrcmodapplicationsmod_lcrmod_lcrc">freeswitch/branches/rupa_limit/src/mod/applications/mod_lcr/mod_lcr.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchbranchesrupa_limitsrcmodapplicationsmod_lcrmod_lcrc"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/mod/applications/mod_lcr/mod_lcr.c (17077 => 17078)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/mod/applications/mod_lcr/mod_lcr.c        2010-03-23 16:12:11 UTC (rev 17077)
+++ freeswitch/branches/rupa_limit/src/mod/applications/mod_lcr/mod_lcr.c        2010-03-23 16:16:48 UTC (rev 17078)
</span><span class="lines">@@ -1,6 +1,7 @@
</span><ins>+/* WTF: do not checkin yet */
</ins><span class="cx"> /*
</span><span class="cx"> * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
</span><del>- * Copyright (C) 2005-2010, Anthony Minessale II <anthm@freeswitch.org>
</del><ins>+ * Copyright (C) 2005-2009, Anthony Minessale II <anthm@freeswitch.org>
</ins><span class="cx"> *
</span><span class="cx"> * Version: MPL 1.1
</span><span class="cx"> *
</span><span class="lines">@@ -35,42 +36,24 @@
</span><span class="cx"> #define LCR_SYNTAX "lcr <digits> [<lcr profile>] [caller_id] [intrastate] [as xml]"
</span><span class="cx"> #define LCR_ADMIN_SYNTAX "lcr_admin show profiles"
</span><span class="cx">
</span><del>-/* SQL Query places */
-/* these now make up a map that describes the location of
- the field based on name */
-#define LCR_DIGITS_PLACE 0
-#define LCR_CARRIER_PLACE 1
-#define LCR_RATE_PLACE 2
-#define LCR_GW_PREFIX_PLACE 3
-#define LCR_GW_SUFFIX_PLACE 4
-#define LCR_LSTRIP_PLACE 5
-#define LCR_TSTRIP_PLACE 6
-#define LCR_PREFIX_PLACE 7
-#define LCR_SUFFIX_PLACE 8
-#define LCR_CODEC_PLACE 9
-#define LCR_CID_PLACE 10
-#define LCR_USER_RATE_PLACE 11
</del><ins>+#define LCR_HEADERS_COUNT 7
</ins><span class="cx">
</span><del>-#define LCR_QUERY_COLS_REQUIRED 9
-#define LCR_QUERY_COLS 12
-
-#define LCR_HEADERS_COUNT 6
-
</del><span class="cx"> #define LCR_HEADERS_DIGITS 0
</span><span class="cx"> #define LCR_HEADERS_CARRIER 1
</span><span class="cx"> #define LCR_HEADERS_RATE 2
</span><span class="cx"> #define LCR_HEADERS_DIALSTRING 3
</span><span class="cx"> #define LCR_HEADERS_CODEC 4
</span><span class="cx"> #define LCR_HEADERS_CID 5
</span><ins>+#define LCR_HEADERS_LIMIT 6
</ins><span class="cx">
</span><del>-#define LCR_
</del><span class="cx"> static char headers[LCR_HEADERS_COUNT][32] = {
</span><span class="cx">         "Digit Match",
</span><span class="cx">         "Carrier",
</span><span class="cx">         "Rate",
</span><span class="cx">         "Dialstring",
</span><span class="cx">         "Codec",
</span><del>-        "CID Regexp"
</del><ins>+        "CID Regexp",
+        "Limit",
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> /* sql for random function */
</span><span class="lines">@@ -93,6 +76,10 @@
</span><span class="cx">         size_t digit_len;
</span><span class="cx">         char *codec;
</span><span class="cx">         char *cid;
</span><ins>+        char *limit_realm;
+        char *limit_id;
+        int limit_max;
+        switch_event_t *fields;
</ins><span class="cx">         struct lcr_obj *prev;
</span><span class="cx">         struct lcr_obj *next;
</span><span class="cx"> };
</span><span class="lines">@@ -104,6 +91,7 @@
</span><span class="cx">         size_t codec;
</span><span class="cx">         size_t cid;
</span><span class="cx">         size_t dialstring;
</span><ins>+        size_t limit;
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> typedef struct lcr_obj lcr_obj_t;
</span><span class="lines">@@ -117,12 +105,16 @@
</span><span class="cx">         uint16_t id;
</span><span class="cx">         char *order_by;
</span><span class="cx">         char *custom_sql;
</span><ins>+        char *export_fields_str;
+        int export_fields_cnt;
+        char **export_fields;
+        char *limit_type;
</ins><span class="cx">         switch_bool_t custom_sql_has_percent;
</span><span class="cx">         switch_bool_t custom_sql_has_vars;
</span><span class="cx">         switch_bool_t profile_has_intrastate;
</span><span class="cx">         switch_bool_t profile_has_intralata;
</span><span class="cx">         switch_bool_t profile_has_npanxx;
</span><del>-
</del><ins>+        
</ins><span class="cx">         switch_bool_t reorder_by_rate;
</span><span class="cx">         switch_bool_t quote_in_list;
</span><span class="cx">         switch_bool_t info_in_headers;
</span><span class="lines">@@ -173,21 +165,21 @@
</span><span class="cx">         char *src_regex = NULL;
</span><span class="cx">         char *dst_regex = NULL;
</span><span class="cx">         switch_channel_t *channel = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (!zstr(cid)) {
</span><span class="cx">                 len = strlen(cid);
</span><span class="cx">         } else {
</span><span class="cx">                 goto done;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         src = switch_core_strdup(pool, cid);
</span><span class="cx">         /* check that this is a valid regexp and split the string */
</span><del>-
-        if ((src[0] == '/') && src[len - 1] == '/') {
</del><ins>+        
+        if ((src[0] == '/') && src[len-1] == '/') {
</ins><span class="cx">                 /* strip leading / trailing slashes */
</span><del>-                src[len - 1] = '\0';
</del><ins>+                src[len-1] = '\0';
</ins><span class="cx">                 src++;
</span><del>-
</del><ins>+                
</ins><span class="cx">                 /* break on first / */
</span><span class="cx">                 dst = strchr(src, '/');
</span><span class="cx">                 *dst = '\0';
</span><span class="lines">@@ -216,12 +208,12 @@
</span><span class="cx">                         switch_safe_free(tmp_regex);
</span><span class="cx">                         dst = dst_regex;
</span><span class="cx">                 }
</span><del>-
</del><ins>+                        
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "expanded src: %s, dst: %s\n", src, dst);
</span><span class="cx">         }
</span><del>-
</del><ins>+
</ins><span class="cx">         if ((proceed = switch_regex_perform(number, src, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
</span><del>-                len = (uint32_t) (strlen(src) + strlen(dst) + 10) * proceed;        /* guestimate size */
</del><ins>+                len = (uint32_t) (strlen(src) + strlen(dst) + 10) * proceed; /* guestimate size */
</ins><span class="cx">                 if (!(substituted = switch_core_alloc(pool, len))) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error!\n");
</span><span class="cx">                         goto done;
</span><span class="lines">@@ -233,71 +225,93 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_regex_safe_free(re);
</span><del>-
</del><ins>+        
</ins><span class="cx">         return substituted;
</span><del>-
- done:
</del><ins>+        
+done:
</ins><span class="cx">         switch_regex_safe_free(re);
</span><span class="cx">         return number;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static char *get_bridge_data(switch_memory_pool_t *pool, char *dialed_number, char *caller_id, lcr_route cur_route, profile_t *profile,
-                                                         switch_core_session_t *session)
</del><ins>+static char *get_bridge_data(switch_memory_pool_t *pool, char *dialed_number, char *caller_id, lcr_route cur_route, profile_t *profile, switch_core_session_t *session)
</ins><span class="cx"> {
</span><span class="cx">         size_t lstrip;
</span><del>-        size_t tstrip;
</del><ins>+        size_t tstrip;
</ins><span class="cx">         char *data = NULL;
</span><span class="cx">         char *destination_number = NULL;
</span><del>-        char *orig_destination_number = NULL;
</del><ins>+        char *orig_destination_number = NULL;
</ins><span class="cx">         char *codec = NULL;
</span><span class="cx">         char *cid = NULL;
</span><span class="cx">         char *header = NULL;
</span><span class="cx">         char *user_rate = NULL;
</span><ins>+        char *export_fields = NULL;
</ins><span class="cx">
</span><span class="cx">         orig_destination_number = destination_number = switch_core_strdup(pool, dialed_number);
</span><del>-
</del><ins>+        
</ins><span class="cx">         tstrip = ((cur_route->digit_len - cur_route->tstrip) + 1);
</span><span class="cx">         lstrip = cur_route->lstrip;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (cur_route->tstrip > 0) {
</span><span class="cx">                 if (strlen(destination_number) > tstrip) {
</span><span class="cx">                         destination_number[tstrip] = '\0';
</span><del>-                } else {
</del><ins>+                }
+                else {
</ins><span class="cx">                         destination_number[0] = '\0';
</span><del>-                }
-        }
</del><ins>+                         }
+ }
</ins><span class="cx">         if (cur_route->lstrip > 0) {
</span><span class="cx">                 if (strlen(destination_number) > lstrip) {
</span><span class="cx">                         destination_number += lstrip;
</span><del>-                } else {
</del><ins>+                }
+                else {
</ins><span class="cx">                         destination_number[0] = '\0';
</span><span class="cx">                 }
</span><del>-        }
</del><ins>+        }        
</ins><span class="cx">         codec = "";
</span><span class="cx">         if (!zstr(cur_route->codec)) {
</span><span class="cx">                 codec = switch_core_sprintf(pool, ",absolute_codec_string=%s", cur_route->codec);
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         cid = "";
</span><span class="cx">         if (!zstr(cur_route->cid)) {
</span><del>-                cid = switch_core_sprintf(pool, ",origination_caller_id_number=%s", do_cid(pool, cur_route->cid, caller_id, session));
</del><ins>+                cid = switch_core_sprintf(pool, ",origination_caller_id_number=%s",
+                                                                 do_cid(pool, cur_route->cid, caller_id, session));
</ins><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         header = "";
</span><span class="cx">         if (profile->info_in_headers) {
</span><del>-                header = switch_core_sprintf(pool, ",sip_h_X-LCR-INFO=lcr_rate=%s;lcr_carrier=%s", cur_route->rate_str, cur_route->carrier_name);
</del><ins>+                header = switch_core_sprintf(pool, ",sip_h_X-LCR-INFO=lcr_rate=%s;lcr_carrier=%s",
+                                                                         cur_route->rate_str,
+                                                                         cur_route->carrier_name);
</ins><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (zstr(cur_route->user_rate_str)) {
</span><span class="cx">                 user_rate = "";
</span><span class="cx">         } else {
</span><span class="cx">                 user_rate = switch_core_sprintf(pool, ",lcr_user_rate=%s", cur_route->user_rate_str);
</span><span class="cx">         }
</span><del>-
-        data =
-                switch_core_sprintf(pool, "[lcr_carrier=%s,lcr_rate=%s%s%s%s%s]%s%s%s%s%s", cur_route->carrier_name, cur_route->rate_str, user_rate, codec, cid,
-                                                        header, cur_route->gw_prefix, cur_route->prefix, destination_number, cur_route->suffix, cur_route->gw_suffix);
-
</del><ins>+        
+        export_fields = "";
+        if (profile->export_fields_cnt > 0) {
+                int i = 0;
+                char *val = NULL;
+                for (i = 0; i < profile->export_fields_cnt; i++) {
+                        val = switch_event_get_header(cur_route->fields, profile->export_fields[i]);
+                        if (val) {
+                                export_fields = switch_core_sprintf(pool, "%s,%s=%s",
+                                                                                                        export_fields,
+                                                                                                        profile->export_fields[i],
+                                                                                                        val);
+                        }
+                }
+        }
+        
+        data = switch_core_sprintf(pool, "[lcr_carrier=%s,lcr_rate=%s%s%s%s%s%s]%s%s%s%s%s"
+                                                                , cur_route->carrier_name, cur_route->rate_str
+                                                                , user_rate, codec, cid, header, export_fields
+                                                                , cur_route->gw_prefix, cur_route->prefix
+                                                                , destination_number, cur_route->suffix, cur_route->gw_suffix);
+                        
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Returning Dialstring %s\n", data);
</span><span class="cx">         return data;
</span><span class="cx"> }
</span><span class="lines">@@ -305,13 +319,13 @@
</span><span class="cx"> static profile_t *locate_profile(const char *profile_name)
</span><span class="cx"> {
</span><span class="cx">         profile_t *profile = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (zstr(profile_name)) {
</span><span class="cx">                 profile = globals.default_profile;
</span><span class="cx">         } else if (!(profile = switch_core_hash_find(globals.profile_hash, profile_name))) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error invalid profile %s\n", profile_name);
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         return profile;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -324,6 +338,7 @@
</span><span class="cx">         maxes->rate = 8;
</span><span class="cx">         maxes->codec = (headers[LCR_HEADERS_CODEC] == NULL ? 0 : strlen(headers[LCR_HEADERS_CODEC]));
</span><span class="cx">         maxes->cid = (headers[LCR_HEADERS_CID] == NULL ? 0 : strlen(headers[LCR_HEADERS_CID]));
</span><ins>+        maxes->limit = (headers[LCR_HEADERS_LIMIT] == NULL ? 0 : strlen(headers[LCR_HEADERS_LIMIT]));
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static switch_status_t process_max_lengths(max_obj_t *maxes, lcr_route routes, char *destination_number)
</span><span class="lines">@@ -344,41 +359,48 @@
</span><span class="cx">         for (current = routes; current; current = current->next) {
</span><span class="cx">                 size_t this_len;
</span><span class="cx">
</span><del>-                if (current->carrier_name != NULL) {
</del><ins>+                if (current->carrier_name) {
</ins><span class="cx">                         this_len = strlen(current->carrier_name);
</span><span class="cx">                         if (this_len > maxes->carrier_name) {
</span><span class="cx">                                 maxes->carrier_name = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current->dialstring != NULL) {
</del><ins>+                if (current->dialstring) {
</ins><span class="cx">                         this_len = strlen(current->dialstring);
</span><span class="cx">                         if (this_len > maxes->dialstring) {
</span><span class="cx">                                 maxes->dialstring = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current->digit_str != NULL) {
</del><ins>+                if (current->digit_str) {
</ins><span class="cx">                         if (current->digit_len > maxes->digit_str) {
</span><span class="cx">                                 maxes->digit_str = current->digit_len;
</span><del>-                        }
</del><ins>+                        }
</ins><span class="cx">                 }
</span><del>-                if (current->rate_str != NULL) {
</del><ins>+                if (current->rate_str) {
</ins><span class="cx">                         this_len = strlen(current->rate_str);
</span><span class="cx">                         if (this_len > maxes->rate) {
</span><span class="cx">                                 maxes->rate = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current->codec != NULL) {
-                        this_len = strlen(current->codec);
</del><ins>+                if (current->codec) {
+                        this_len= strlen(current->codec);
</ins><span class="cx">                         if (this_len > maxes->codec) {
</span><span class="cx">                                 maxes->codec = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current->cid != NULL) {
</del><ins>+                if (current->cid) {
</ins><span class="cx">                         this_len = strlen(current->cid);
</span><span class="cx">                         if (this_len > maxes->cid) {
</span><span class="cx">                                 maxes->cid = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+
+                if (current->limit_realm && current->limit_id) {
+                        this_len = strlen(current->limit_realm) + strlen(current->limit_id) + 5;
+                        if (this_len > maxes->limit) {
+                                maxes->limit = this_len;
+                        }
+                }
</ins><span class="cx">         }
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="lines">@@ -387,14 +409,13 @@
</span><span class="cx"> {
</span><span class="cx">         switch_cache_db_connection_options_t options = { {0} };
</span><span class="cx">         switch_cache_db_handle_t *dbh = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (!zstr(globals.odbc_dsn)) {
</span><span class="cx">                 options.odbc_options.dsn = globals.odbc_dsn;
</span><span class="cx">                 options.odbc_options.user = globals.odbc_user;
</span><span class="cx">                 options.odbc_options.pass = globals.odbc_pass;
</span><span class="cx">
</span><del>-                if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS)
-                        dbh = NULL;
</del><ins>+                if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
</ins><span class="cx">         }
</span><span class="cx">         return dbh;
</span><span class="cx"> }
</span><span class="lines">@@ -425,23 +446,23 @@
</span><span class="cx">                 db_random = "random()";
</span><span class="cx">                 return SWITCH_TRUE;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         return SWITCH_FALSE;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /* make a new string with digits only */
</span><del>-static char *string_digitsonly(switch_memory_pool_t *pool, const char *str)
</del><ins>+static char *string_digitsonly(switch_memory_pool_t *pool, const char *str)
</ins><span class="cx"> {
</span><span class="cx">         char *p, *np, *newstr;
</span><span class="cx">         size_t len;
</span><span class="cx">
</span><del>-        p = (char *) str;
</del><ins>+        p = (char *)str;
</ins><span class="cx">
</span><span class="cx">         len = strlen(str);
</span><del>-        newstr = switch_core_alloc(pool, len + 1);
</del><ins>+        newstr = switch_core_alloc(pool, len+1);
</ins><span class="cx">         np = newstr;
</span><del>-
-        while (*p) {
</del><ins>+        
+        while(*p) {
</ins><span class="cx">                 if (switch_isdigit(*p)) {
</span><span class="cx">                         *np = *p;
</span><span class="cx">                         np++;
</span><span class="lines">@@ -473,10 +494,14 @@
</span><span class="cx">
</span><span class="cx">         digit_len = strlen(digits);
</span><span class="cx">         digits_copy = switch_core_strdup(pool, digits);
</span><del>-
</del><ins>+        
</ins><span class="cx">         for (n = digit_len; n > 0; n--) {
</span><span class="cx">                 digits_copy[n] = '\0';
</span><del>-                dig_stream.write_function(&dig_stream, "%s%s%s%s", (n == digit_len ? "" : ", "), (quote ? "'" : ""), digits_copy, (quote ? "'" : ""));
</del><ins>+                dig_stream.write_function(&dig_stream, "%s%s%s%s",
+                                                                        (n==digit_len ? "" : ", "),
+                                                                        (quote ? "'" : ""),
+                                                                        digits_copy,
+                                                                        (quote ? "'" : ""));
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         ret = switch_core_strdup(pool, dig_stream.data);
</span><span class="lines">@@ -487,41 +512,42 @@
</span><span class="cx"> /* format the custom sql */
</span><span class="cx"> static char *format_custom_sql(const char *custom_sql, callback_t *cb_struct, const char *digits)
</span><span class="cx"> {
</span><del>-        char *tmpSQL = NULL;
-        char *newSQL = NULL;
</del><ins>+        char * tmpSQL = NULL;
+        char * newSQL = NULL;
</ins><span class="cx">         switch_channel_t *channel;
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* first replace %s with digits to maintain backward compat */
</span><span class="cx">         if (cb_struct->profile->custom_sql_has_percent == SWITCH_TRUE) {
</span><span class="cx">                 tmpSQL = switch_string_replace(custom_sql, "%q", digits);
</span><span class="cx">                 newSQL = tmpSQL;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* expand the vars */
</span><span class="cx">         if (cb_struct->profile->custom_sql_has_vars == SWITCH_TRUE) {
</span><span class="cx">                 if (cb_struct->session) {
</span><span class="cx">                         channel = switch_core_session_get_channel(cb_struct->session);
</span><span class="cx">                         switch_assert(channel);
</span><span class="cx">                         /*
</span><del>-                         newSQL = switch_channel_expand_variables_escape(channel,
-                         tmpSQL ? tmpSQL : custom_sql,
-                         escape_sql);
-                         */
-                        newSQL = switch_channel_expand_variables(channel, tmpSQL ? tmpSQL : custom_sql);
</del><ins>+                        newSQL = switch_channel_expand_variables_escape(channel,
+                                                                                                                        tmpSQL ? tmpSQL : custom_sql,
+                                                                                                                        escape_sql);
+                        */
+                        newSQL = switch_channel_expand_variables(channel,
+                                                                                                                tmpSQL ? tmpSQL : custom_sql);
</ins><span class="cx">                 } else if (cb_struct->event) {
</span><span class="cx">                         /* use event system to expand vars */
</span><span class="cx">                         newSQL = switch_event_expand_headers(cb_struct->event, tmpSQL ? tmpSQL : custom_sql);
</span><del>-
</del><ins>+                        
</ins><span class="cx">                 } else {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_CRIT,
</span><del>-                                                         "mod_lcr called without a valid session while using a custom_sql that has channel variables.\n");
</del><ins>+                                                                "mod_lcr called without a valid session while using a custom_sql that has channel variables.\n");
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (tmpSQL != newSQL) {
</span><span class="cx">                 switch_safe_free(tmpSQL);
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (newSQL == NULL) {
</span><span class="cx">                 return (char *) custom_sql;
</span><span class="cx">         } else {
</span><span class="lines">@@ -533,10 +559,10 @@
</span><span class="cx"> {
</span><span class="cx">         switch_bool_t retval = SWITCH_FALSE;
</span><span class="cx">         switch_cache_db_handle_t *dbh = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (globals.odbc_dsn && (dbh = lcr_get_db_handle())) {
</span><span class="cx">                 if (switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, NULL)
</span><del>-                        == SWITCH_ODBC_FAIL) {
</del><ins>+                                == SWITCH_ODBC_FAIL) {
</ins><span class="cx">                         retval = SWITCH_FALSE;
</span><span class="cx">                 } else {
</span><span class="cx">                         retval = SWITCH_TRUE;
</span><span class="lines">@@ -546,54 +572,64 @@
</span><span class="cx">         return retval;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/* CF = compare field */
+#define CF(x) !strcmp(x, columnNames[i])
+
</ins><span class="cx"> static int route_add_callback(void *pArg, int argc, char **argv, char **columnNames)
</span><span class="cx"> {
</span><span class="cx">         lcr_route additional = NULL;
</span><span class="cx">         lcr_route current = NULL;
</span><span class="cx">         callback_t *cbt = (callback_t *) pArg;
</span><span class="cx">         char *key = NULL;
</span><ins>+        int i = 0;
</ins><span class="cx">
</span><span class="cx">         switch_memory_pool_t *pool = cbt->pool;
</span><ins>+        
+        additional = switch_core_alloc(pool, sizeof(lcr_obj_t));
+        additional->fields = switch_core_alloc(pool, sizeof(switch_event_t));
</ins><span class="cx">
</span><del>-
-        if (argc < LCR_QUERY_COLS_REQUIRED) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
-                                                 "Unexpected number of columns returned for SQL. Returned column count: %d. "
-                                                 "If using a custom sql for this profile, verify it is correct. Otherwise file a bug report.\n", argc);
-                return SWITCH_STATUS_GENERR;
</del><ins>+        for (i = 0; i < argc ; i++) {
+                if (CF("lcr_digits")) {
+                        additional->digit_len = strlen(argv[i]);
+                        additional->digit_str = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_prefix")) {
+                        additional->prefix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_suffix")) {
+                        additional->suffix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_carrier_name")) {
+                        additional->carrier_name = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_rate_field")) {
+                        additional->rate = (float)atof(switch_str_nil(argv[i]));
+                        additional->rate_str = switch_core_sprintf(pool, "%0.5f", additional->rate);
+                } else if (CF("lcr_gw_prefix")) {
+                        additional->gw_prefix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_gw_suffix")) {
+                        additional->gw_suffix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_lead_strip")) {
+                        additional->lstrip = atoi(switch_str_nil(argv[i]));
+                } else if (CF("lcr_trail_strip")) {
+                        additional->tstrip = atoi(switch_str_nil(argv[i]));
+                } else if (CF("lcr_codec")) {
+                        additional->codec = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_cid")) {
+                        additional->cid = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_user_rate")) {
+                        additional->user_rate = (float)atof(switch_str_nil(argv[i]));
+                        additional->user_rate_str = switch_core_sprintf(pool, "%0.5f", additional->user_rate);
+                } else if (CF("lcr_limit_realm")) {
+                        additional->limit_realm = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_limit_id")) {
+                        additional->limit_id = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF("lcr_limit_max")) {
+                        additional->limit_max = (float)atof(switch_str_nil(argv[i]));
+                }
+                
+                /* add all fields to the fields event */
+                switch_event_add_header_string(additional->fields, SWITCH_STACK_BOTTOM, columnNames[i], argv[i]);
</ins><span class="cx">         }
</span><del>-
-        if (zstr(argv[LCR_GW_PREFIX_PLACE]) && zstr(argv[LCR_GW_SUFFIX_PLACE])) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                 "There's no way to dial this Gateway: Carrier: \"%s\" Prefix: \"%s\", Suffix \"%s\"\n",
-                                                 switch_str_nil(argv[LCR_CARRIER_PLACE]), switch_str_nil(argv[LCR_GW_PREFIX_PLACE]), switch_str_nil(argv[LCR_GW_SUFFIX_PLACE]));
-                return SWITCH_STATUS_SUCCESS;
-        }
-
</del><ins>+        
</ins><span class="cx">         cbt->matches++;
</span><del>-        additional = switch_core_alloc(pool, sizeof(lcr_obj_t));
-
-        additional->digit_len = strlen(argv[LCR_DIGITS_PLACE]);
-        additional->digit_str = switch_core_strdup(pool, switch_str_nil(argv[LCR_DIGITS_PLACE]));
-        additional->suffix = switch_core_strdup(pool, switch_str_nil(argv[LCR_SUFFIX_PLACE]));
-        additional->prefix = switch_core_strdup(pool, switch_str_nil(argv[LCR_PREFIX_PLACE]));
-        additional->carrier_name = switch_core_strdup(pool, switch_str_nil(argv[LCR_CARRIER_PLACE]));
-        additional->rate = (float) atof(switch_str_nil(argv[LCR_RATE_PLACE]));
-        additional->rate_str = switch_core_sprintf(pool, "%0.5f", additional->rate);
-        additional->gw_prefix = switch_core_strdup(pool, switch_str_nil(argv[LCR_GW_PREFIX_PLACE]));
-        additional->gw_suffix = switch_core_strdup(pool, switch_str_nil(argv[LCR_GW_SUFFIX_PLACE]));
-        additional->lstrip = atoi(switch_str_nil(argv[LCR_LSTRIP_PLACE]));
-        additional->tstrip = atoi(switch_str_nil(argv[LCR_TSTRIP_PLACE]));
-        if (argc > LCR_CODEC_PLACE) {
-                additional->codec = switch_core_strdup(pool, switch_str_nil(argv[LCR_CODEC_PLACE]));
-        }
-        if (argc > LCR_CID_PLACE) {
-                additional->cid = switch_core_strdup(pool, switch_str_nil(argv[LCR_CID_PLACE]));
-        }
-        if (argc > LCR_USER_RATE_PLACE) {
-                additional->user_rate = (float) atof(switch_str_nil(argv[LCR_USER_RATE_PLACE]));
-                additional->user_rate_str = switch_core_sprintf(pool, "%0.5f", additional->user_rate);
-        }
</del><ins>+        
</ins><span class="cx">         additional->dialstring = get_bridge_data(pool, cbt->lookup_number, cbt->cid, additional, cbt->profile, cbt->session);
</span><span class="cx">
</span><span class="cx">         if (cbt->head == NULL) {
</span><span class="lines">@@ -608,12 +644,14 @@
</span><span class="cx">                 return SWITCH_STATUS_SUCCESS;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-
</del><ins>+        
</ins><span class="cx">         for (current = cbt->head; current; current = current->next) {
</span><del>-
</del><ins>+        
</ins><span class="cx">                 key = switch_core_sprintf(pool, "%s:%s", additional->gw_prefix, additional->gw_suffix);
</span><span class="cx">                 if (switch_core_hash_find(cbt->dedup_hash, key)) {
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Ignoring Duplicate route for termination point (%s)\n", key);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
+                                                                "Ignoring Duplicate route for termination point (%s)\n",
+                                                                key);
</ins><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="lines">@@ -633,12 +671,13 @@
</span><span class="cx">                         if (current->rate > additional->rate) {
</span><span class="cx">                                 /* insert myself here */
</span><span class="cx">                                 if (current->prev != NULL) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding %s before %s\n", additional->carrier_name, current->carrier_name);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding %s before %s\n",
+                                                                                additional->carrier_name, current->carrier_name);
</ins><span class="cx">                                         current->prev->next = additional;
</span><span class="cx">                                 } else {
</span><span class="cx">                                         /* put this one at the head */
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Inserting %s to list head before %s\n",
-                                                                         additional->carrier_name, current->carrier_name);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Inserting %s to list head before %s\n",
+                                                                                additional->carrier_name, current->carrier_name);
</ins><span class="cx">                                         cbt->head = additional;
</span><span class="cx">                                 }
</span><span class="cx">                                 additional->next = current;
</span><span class="lines">@@ -650,7 +689,7 @@
</span><span class="cx">                                 break;
</span><span class="cx">                         } else if (current->next == NULL) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "adding %s to end of list after %s\n",
</span><del>-                                                                 additional->carrier_name, current->carrier_name);
</del><ins>+                                                                        additional->carrier_name, current->carrier_name);
</ins><span class="cx">                                 current->next = additional;
</span><span class="cx">                                 additional->prev = current;
</span><span class="cx">                                 if (switch_core_hash_insert(cbt->dedup_hash, key, additional) != SWITCH_STATUS_SUCCESS) {
</span><span class="lines">@@ -668,7 +707,7 @@
</span><span class="cx"> {
</span><span class="cx">         int count = 0;
</span><span class="cx">         callback_t *cbt = (callback_t *) pArg;
</span><del>-
</del><ins>+        
</ins><span class="cx">         count = atoi(argv[1]);
</span><span class="cx">         if (count == 1) {
</span><span class="cx">                 if (!strcmp(argv[0], "state")) {
</span><span class="lines">@@ -678,49 +717,50 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Type: %s, Count: %d\n", argv[0], count);
-
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Type: %s, Count: %d\n", argv[0], count);
+        
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static switch_status_t is_intrastatelata(callback_t *cb_struct)
</span><span class="cx"> {
</span><span class="cx">         char *sql = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* extract npa nxx - make some assumptions about format:
</span><del>-         e164 format without the +
-         NANP only (so 11 digits starting with 1)
-         */
-        if (!cb_struct->lookup_number || (strlen(cb_struct->lookup_number) != 11 && *cb_struct->lookup_number != '1')) {
</del><ins>+                e164 format without the +
+                NANP only (so 11 digits starting with 1)
+        */
+        if (!cb_struct->lookup_number || !(strlen(cb_struct->lookup_number) == 11 && *cb_struct->lookup_number == '1')) {
</ins><span class="cx">                 /* dest doesn't appear to be NANP number */
</span><span class="cx">                 return SWITCH_STATUS_GENERR;
</span><span class="cx">         }
</span><del>-        if (!cb_struct->cid || (strlen(cb_struct->cid) != 11 && *cb_struct->cid != '1')) {
</del><ins>+        if (!cb_struct->cid || !(strlen(cb_struct->cid) == 11 && *cb_struct->cid == '1')) {
</ins><span class="cx">                 /* cid not NANP */
</span><span class="cx">                 return SWITCH_STATUS_GENERR;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* assume that if the area code (plus leading 1) are the same we're intrastate */
</span><span class="cx">         /* probably a bad assumption */
</span><span class="cx">         /*
</span><del>-         if (!strncmp(cb_struct->lookup_number, cb_struct->cid, 4)) {
-         cb_struct->intrastate = SWITCH_TRUE;
-         return SWITCH_STATUS_SUCCESS;
-         }
-         */
-
</del><ins>+        if (!strncmp(cb_struct->lookup_number, cb_struct->cid, 4)) {
+                cb_struct->intrastate = SWITCH_TRUE;
+                return SWITCH_STATUS_SUCCESS;
+        }
+        */
+        
</ins><span class="cx">         sql = switch_core_sprintf(cb_struct->pool,
</span><del>-                                                         "SELECT 'state', count(DISTINCT state) FROM npa_nxx_company_ocn WHERE (npa=%3.3s AND nxx=%3.3s) OR (npa=%3.3s AND nxx=%3.3s)"
-                                                         " UNION "
-                                                         "SELECT 'lata', count(DISTINCT lata) FROM npa_nxx_company_ocn WHERE (npa=%3.3s AND nxx=%3.3s) OR (npa=%3.3s AND nxx=%3.3s)",
-                                                         cb_struct->lookup_number + 1, cb_struct->lookup_number + 4,
-                                                         cb_struct->cid + 1, cb_struct->cid + 4,
-                                                         cb_struct->lookup_number + 1, cb_struct->lookup_number + 4, cb_struct->cid + 1, cb_struct->cid + 4);
</del><ins>+                                                                "SELECT 'state', count(DISTINCT state) FROM npa_nxx_company_ocn WHERE (npa=%3.3s AND nxx=%3.3s) OR (npa=%3.3s AND nxx=%3.3s)"
+                                                                " UNION "
+                                                                "SELECT 'lata', count(DISTINCT lata) FROM npa_nxx_company_ocn WHERE (npa=%3.3s AND nxx=%3.3s) OR (npa=%3.3s AND nxx=%3.3s)",
+                                                                cb_struct->lookup_number+1, cb_struct->lookup_number+4,
+                                                                cb_struct->cid+1, cb_struct->cid+4,
+                                                                cb_struct->lookup_number+1, cb_struct->lookup_number+4,
+                                                                cb_struct->cid+1, cb_struct->cid+4);
</ins><span class="cx">
</span><del>-        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
</ins><span class="cx">
</span><del>-        return (lcr_execute_sql_callback(sql, intrastatelata_callback, cb_struct));
-
</del><ins>+        return(lcr_execute_sql_callback(sql, intrastatelata_callback, cb_struct));
+        
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static switch_status_t lcr_do_lookup(callback_t *cb_struct)
</span><span class="lines">@@ -736,26 +776,26 @@
</span><span class="cx">         char *safe_sql = NULL;
</span><span class="cx">         char *rate_field = NULL;
</span><span class="cx">         char *user_rate_field = NULL;
</span><del>-
</del><ins>+        
</ins><span class="cx">         switch_assert(cb_struct->lookup_number != NULL);
</span><span class="cx">
</span><span class="cx">         digits_copy = string_digitsonly(cb_struct->pool, digits);
</span><span class="cx">         if (zstr(digits_copy)) {
</span><span class="cx">                 return SWITCH_STATUS_GENERR;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* allocate the dedup hash */
</span><span class="cx">         if (switch_core_hash_init(&cb_struct->dedup_hash, cb_struct->pool) != SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_ERROR, "Error initializing the dedup hash\n");
</span><span class="cx">                 return SWITCH_STATUS_GENERR;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         digits_expanded = expand_digits(cb_struct->pool, digits_copy, cb_struct->profile->quote_in_list);
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (profile->profile_has_npanxx == SWITCH_TRUE) {
</span><span class="cx">                 is_intrastatelata(cb_struct);
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* set our rate field based on env and profile */
</span><span class="cx">         if (cb_struct->intralata == SWITCH_TRUE && profile->profile_has_intralata == SWITCH_TRUE) {
</span><span class="cx">                 rate_field = switch_core_strdup(cb_struct->pool, "intralata_rate");
</span><span class="lines">@@ -769,7 +809,7 @@
</span><span class="cx">         }
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_DEBUG, "intra routing [state:%d lata:%d] so rate field is [%s]\n",
</span><span class="cx">                                          cb_struct->intrastate, cb_struct->intralata, rate_field);
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* set some channel vars if we have a session */
</span><span class="cx">         if (cb_struct->session) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_DEBUG, "we have a session\n");
</span><span class="lines">@@ -796,7 +836,6 @@
</span><span class="cx">         /* format the custom_sql */
</span><span class="cx">         safe_sql = format_custom_sql(profile->custom_sql, cb_struct, digits_copy);
</span><span class="cx">         if (!safe_sql) {
</span><del>-                switch_event_safe_destroy(&cb_struct->event);
</del><span class="cx">                 switch_core_hash_destroy(&cb_struct->dedup_hash);
</span><span class="cx">                 return SWITCH_STATUS_GENERR;
</span><span class="cx">         }
</span><span class="lines">@@ -806,13 +845,12 @@
</span><span class="cx">                 /* channel_expand_variables returned the same string to us, no need to free */
</span><span class="cx">                 switch_safe_free(safe_sql);
</span><span class="cx">         }
</span><ins>+        
+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_DEBUG, "SQL: %s\n", (char *)sql_stream.data);
+        
+        lookup_status = lcr_execute_sql_callback((char *)sql_stream.data, route_add_callback, cb_struct);
</ins><span class="cx">
</span><del>-        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct->session), SWITCH_LOG_DEBUG, "SQL: %s\n", (char *) sql_stream.data);
-
-        lookup_status = lcr_execute_sql_callback((char *) sql_stream.data, route_add_callback, cb_struct);
-
</del><span class="cx">         switch_safe_free(sql_stream.data);
</span><del>-        switch_event_safe_destroy(&cb_struct->event);
</del><span class="cx">         switch_core_hash_destroy(&cb_struct->dedup_hash);
</span><span class="cx">
</span><span class="cx">         if (lookup_status) {
</span><span class="lines">@@ -832,15 +870,16 @@
</span><span class="cx">         switch_event_create(&event, SWITCH_EVENT_MESSAGE);
</span><span class="cx">         routes.event = event;
</span><span class="cx">         routes.pool = pool;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (!(routes.profile = locate_profile(lcr_profile))) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown profile: %s\n", lcr_profile);
</span><span class="cx">                 return SWITCH_FALSE;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         routes.lookup_number = "15555551212";
</span><span class="cx">         routes.cid = "18005551212";
</span><del>-        return (lcr_do_lookup(&routes) == SWITCH_STATUS_SUCCESS) ? SWITCH_TRUE : SWITCH_FALSE;
</del><ins>+        return (lcr_do_lookup(&routes) == SWITCH_STATUS_SUCCESS) ?
+         SWITCH_TRUE : SWITCH_FALSE;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static switch_status_t lcr_load_config()
</span><span class="lines">@@ -876,16 +915,19 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* initialize sql here, 'cause we need to verify custom_sql for each profile below */
</span><span class="cx">         if (globals.odbc_dsn) {
</span><del>-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dsn is \"%s\", user is \"%s\"\n", globals.odbc_dsn, globals.odbc_user);
</del><ins>+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
+                                                 , "dsn is \"%s\", user is \"%s\"\n"
+                                                 , globals.odbc_dsn, globals.odbc_user
+                                                 );
</ins><span class="cx">                 if (!(dbh = lcr_get_db_handle())) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
</span><span class="cx">                         switch_goto_status(SWITCH_STATUS_FALSE, done);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (set_db_random() == SWITCH_TRUE) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Database RANDOM function set to %s\n", db_random);
</span><span class="cx">         } else {
</span><span class="lines">@@ -904,9 +946,11 @@
</span><span class="cx">                         char *info_in_headers = NULL;
</span><span class="cx">                         char *id_s = NULL;
</span><span class="cx">                         char *custom_sql = NULL;
</span><ins>+                        char *export_fields = NULL;
+                        char *limit_type = NULL;
</ins><span class="cx">                         int argc, x = 0;
</span><span class="cx">                         char *argv[4] = { 0 };
</span><del>-
</del><ins>+                        
</ins><span class="cx">                         SWITCH_STANDARD_STREAM(order_by);
</span><span class="cx">
</span><span class="cx">                         for (param = switch_xml_child(x_profile, "param"); param; param = param->next) {
</span><span class="lines">@@ -914,12 +958,12 @@
</span><span class="cx">
</span><span class="cx">                                 var = (char *) switch_xml_attr_soft(param, "name");
</span><span class="cx">                                 val = (char *) switch_xml_attr_soft(param, "value");
</span><del>-
-                                if (!strcasecmp(var, "order_by") && !zstr(val)) {
</del><ins>+                                
+                                if (!strcasecmp(var, "order_by") && !zstr(val)) {
</ins><span class="cx">                                         thisorder = &order_by;
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "param val is %s\n", val);
</span><span class="cx">                                         if ((argc = switch_separate_string(val, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
</span><del>-                                                for (x = 0; x < argc; x++) {
</del><ins>+                                                for (x=0; x<argc; x++) {
</ins><span class="cx">                                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "arg #%d/%d is %s\n", x, argc, argv[x]);
</span><span class="cx">                                                         if (!zstr(argv[x])) {
</span><span class="cx">                                                                 if (!strcasecmp(argv[x], "quality")) {
</span><span class="lines">@@ -954,78 +998,85 @@
</span><span class="cx">                                         info_in_headers = val;
</span><span class="cx">                                 } else if (!strcasecmp(var, "quote_in_list") && !zstr(val)) {
</span><span class="cx">                                         quote_in_list = val;
</span><ins>+                                } else if (!strcasecmp(var, "export_fields") && !zstr(val)) {
+                                        export_fields = val;
+                                } else if (!strcasecmp(var, "limit_type") && !zstr(val)) {
+                                        limit_type = val;
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><del>-
</del><ins>+                        
</ins><span class="cx">                         if (zstr(name)) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No name specified.\n");
</span><span class="cx">                         } else {
</span><span class="cx">                                 profile = switch_core_alloc(globals.pool, sizeof(*profile));
</span><span class="cx">                                 memset(profile, 0, sizeof(profile_t));
</span><span class="cx">                                 profile->name = switch_core_strdup(globals.pool, name);
</span><del>-
-                                if (!zstr((char *) order_by.data)) {
-                                        profile->order_by = switch_core_strdup(globals.pool, (char *) order_by.data);
</del><ins>+                                
+                                if (!zstr((char *)order_by.data)) {
+                                        profile->order_by = switch_core_strdup(globals.pool, (char *)order_by.data);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         /* default to rate */
</span><span class="cx">                                         profile->order_by = ", ${lcr_rate_field}";
</span><span class="cx">                                 }
</span><span class="cx">
</span><span class="cx">                                 if (!zstr(id_s)) {
</span><del>-                                        profile->id = (uint16_t) atoi(id_s);
</del><ins>+                                        profile->id = (uint16_t)atoi(id_s);
</ins><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 /* SWITCH_STANDARD_STREAM doesn't use pools. but we only have to free sql_stream.data */
</span><span class="cx">                                 SWITCH_STANDARD_STREAM(sql_stream);
</span><span class="cx">                                 if (zstr(custom_sql)) {
</span><span class="cx">                                         /* use default sql */
</span><del>-                                        sql_stream.write_function(&sql_stream,
-                                                                                         "SELECT l.digits, c.carrier_name, l.${lcr_rate_field}, cg.prefix AS gw_prefix, cg.suffix AS gw_suffix, l.lead_strip, l.trail_strip, l.prefix, l.suffix ");
</del><ins>+                                        sql_stream.write_function(&sql_stream,
+                                                                                         "SELECT l.digits AS lcr_digits, c.carrier_name AS lcr_carrier_name, l.${lcr_rate_field} AS lcr_rate_field, cg.prefix AS lcr_gw_prefix, cg.suffix AS lcr_gw_suffix, l.lead_strip AS lcr_lead_strip, l.trail_strip AS lcr_trail_strip, l.prefix AS lcr_prefix, l.suffix AS lcr_suffix "
+                                                                                         );
</ins><span class="cx">                                         if (db_check("SELECT codec from carrier_gateway limit 1") == SWITCH_TRUE) {
</span><del>-                                                sql_stream.write_function(&sql_stream, ", cg.codec ");
</del><ins>+                                                sql_stream.write_function(&sql_stream, ", cg.codec AS lcr_codec ");
</ins><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "codec field defined.\n");
</span><span class="cx">                                         } else {
</span><del>-                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                                 "codec field not defined, please update your lcr carrier_gateway database schema.\n");
</del><ins>+                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "codec field not defined, please update your lcr carrier_gateway database schema.\n");
</ins><span class="cx">                                         }
</span><span class="cx">                                         if (db_check("SELECT cid from lcr limit 1") == SWITCH_TRUE) {
</span><del>-                                                sql_stream.write_function(&sql_stream, ", l.cid ");
</del><ins>+                                                sql_stream.write_function(&sql_stream, ", l.cid AS lcr_cid ");
</ins><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "cid field defined.\n");
</span><span class="cx">                                         } else {
</span><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "cid field not defined, please update your lcr database schema.\n");
</span><span class="cx">                                         }
</span><del>-                                        sql_stream.write_function(&sql_stream,
-                                                                                         "FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id WHERE c.enabled = '1' AND cg.enabled = '1' AND l.enabled = '1' AND digits IN (");
</del><ins>+                                        sql_stream.write_function(&sql_stream, "FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id WHERE c.enabled = '1' AND cg.enabled = '1' AND l.enabled = '1' AND digits IN (");
</ins><span class="cx">                                         sql_stream.write_function(&sql_stream, "${lcr_query_expanded_digits}");
</span><span class="cx">                                         sql_stream.write_function(&sql_stream, ") AND CURRENT_TIMESTAMP BETWEEN date_start AND date_end ");
</span><span class="cx">                                         if (profile->id > 0) {
</span><span class="cx">                                                 sql_stream.write_function(&sql_stream, "AND lcr_profile=%d ", profile->id);
</span><span class="cx">                                         }
</span><del>-                                        sql_stream.write_function(&sql_stream, "ORDER BY digits DESC%s", profile->order_by);
</del><ins>+                                        sql_stream.write_function(&sql_stream, "ORDER BY digits DESC%s",
+                                                                                                                        profile->order_by);
</ins><span class="cx">                                         if (db_random) {
</span><span class="cx">                                                 sql_stream.write_function(&sql_stream, ", %s", db_random);
</span><span class="cx">                                         }
</span><span class="cx">                                         sql_stream.write_function(&sql_stream, ";");
</span><del>-
</del><ins>+                                        
</ins><span class="cx">                                         custom_sql = sql_stream.data;
</span><span class="cx">                                 }
</span><del>-
-
</del><ins>+                                
+                                
</ins><span class="cx">                                 profile->profile_has_intralata = db_check("SELECT intralata_rate FROM lcr LIMIT 1");
</span><span class="cx">                                 if (profile->profile_has_intralata != SWITCH_TRUE) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                         "no \"intralata_rate\" field found in the \"lcr\" table, routing by intralata rates will be disabled until the field is added and mod_lcr is reloaded\n");
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+                                                                         "no \"intralata_rate\" field found in the \"lcr\" table, routing by intralata rates will be disabled until the field is added and mod_lcr is reloaded\n"
+                                                                         );
</ins><span class="cx">                                 }
</span><span class="cx">                                 profile->profile_has_intrastate = db_check("SELECT intrastate_rate FROM lcr LIMIT 1");
</span><span class="cx">                                 if (profile->profile_has_intrastate != SWITCH_TRUE) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                         "no \"intrastate_rate\" field found in the \"lcr\" table, routing by intrastate rates will be disabled until the field is added and mod_lcr is reloaded\n");
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+                                                                         "no \"intrastate_rate\" field found in the \"lcr\" table, routing by intrastate rates will be disabled until the field is added and mod_lcr is reloaded\n"
+                                                                         );
</ins><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 profile->profile_has_npanxx = db_check("SELECT npa, nxx, state FROM npa_nxx_company_ocn LIMIT 1");
</span><span class="cx">                                 if (profile->profile_has_npanxx != SWITCH_TRUE) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                         "no \"npa_nxx_company_ocn\" table found in the \"lcr\" database, automatic intrastate detection will be disabled until the table is added and mod_lcr is reloaded\n");
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+                                                                         "no \"npa_nxx_company_ocn\" table found in the \"lcr\" database, automatic intrastate detection will be disabled until the table is added and mod_lcr is reloaded\n"
+                                                                         );
</ins><span class="cx">                                 }
</span><span class="cx">
</span><span class="cx">                                 if (switch_string_var_check_const(custom_sql) || switch_string_has_escaped_data(custom_sql)) {
</span><span class="lines">@@ -1034,20 +1085,43 @@
</span><span class="cx">                                 if (strstr(custom_sql, "%")) {
</span><span class="cx">                                         profile->custom_sql_has_percent = SWITCH_TRUE;
</span><span class="cx">                                 }
</span><del>-                                profile->custom_sql = switch_core_strdup(globals.pool, (char *) custom_sql);
-
</del><ins>+                                profile->custom_sql = switch_core_strdup(globals.pool, (char *)custom_sql);
+                                
</ins><span class="cx">                                 if (!zstr(reorder_by_rate)) {
</span><span class="cx">                                         profile->reorder_by_rate = switch_true(reorder_by_rate);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 if (!zstr(info_in_headers)) {
</span><span class="cx">                                         profile->info_in_headers = switch_true(info_in_headers);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 if (!zstr(quote_in_list)) {
</span><span class="cx">                                         profile->quote_in_list = switch_true(quote_in_list);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
+                                if (!zstr(export_fields)) {
+                                        int argc2 = 0;
+                                        char *argv2[50] = { 0 };
+                                        char **argvdup = NULL;
+                                        char *dup = switch_core_strdup(globals.pool, export_fields);
+                                        argc2 = switch_separate_string(dup, ',', argv2, (sizeof(argv2) / sizeof(argv2[0])));
+                                        profile->export_fields_str = switch_core_strdup(globals.pool, export_fields);
+                                        profile->export_fields_cnt = argc2;
+                                        argvdup = switch_core_alloc(globals.pool, sizeof(argv2));
+                                        memcpy(argvdup, argv2, sizeof(argv2));
+                                        profile->export_fields = argvdup;
+                                }
+                                
+                                if (!zstr(limit_type)) {
+                                        if (!strcasecmp(limit_type, "hash")) {
+                                                profile->limit_type = "hash";
+                                        } else {
+                                                profile->limit_type = "sql";
+                                        }
+                                } else {
+                                        profile->limit_type = "sql";
+                                }
+                                
</ins><span class="cx">                                 switch_core_hash_insert(globals.profile_hash, profile->name, profile);
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Loaded lcr profile %s.\n", profile->name);
</span><span class="cx">                                 /* test the profile */
</span><span class="lines">@@ -1060,7 +1134,7 @@
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Removing INVALID Profile %s.\n", profile->name);
</span><span class="cx">                                         switch_core_hash_delete(globals.profile_hash, profile->name);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                         }
</span><span class="cx">                         switch_safe_free(order_by.data);
</span><span class="cx">                         switch_safe_free(sql_stream.data);
</span><span class="lines">@@ -1068,7 +1142,7 @@
</span><span class="cx">         } else {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No lcr profiles defined.\n");
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         /* define default profile */
</span><span class="cx">         if (!globals.default_profile) {
</span><span class="cx">                 profile = switch_core_alloc(globals.pool, sizeof(*profile));
</span><span class="lines">@@ -1079,12 +1153,206 @@
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Setting system defined default profile.");
</span><span class="cx">         }
</span><span class="cx">
</span><del>- done:
</del><ins>+done:
</ins><span class="cx">         switch_cache_db_release_db_handle(&dbh);
</span><span class="cx">         switch_xml_free(xml);
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/* fake chan_lcr */
+switch_endpoint_interface_t *lcr_endpoint_interface;
+static switch_call_cause_t lcr_outgoing_channel(switch_core_session_t *session,
+                                                                                                 switch_event_t *var_event,
+                                                                                                 switch_caller_profile_t *outbound_profile,
+                                                                                                 switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags,
+                                                                                                 switch_call_cause_t *cancel_cause);
+switch_io_routines_t lcr_io_routines = {
+        /*.outgoing_channel */ lcr_outgoing_channel
+};
+
+static switch_call_cause_t lcr_outgoing_channel(switch_core_session_t *session,
+                                                                                                 switch_event_t *var_event,
+                                                                                                 switch_caller_profile_t *outbound_profile,
+                                                                                                 switch_core_session_t **new_session, switch_memory_pool_t **new_pool, switch_originate_flag_t flags,
+                                                                                                 switch_call_cause_t *cancel_cause)
+{
+        switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+        char *dest = NULL;
+        switch_originate_flag_t myflags = SOF_NONE;
+        const char *cid_name_override = NULL;
+        const char *cid_num_override = NULL;
+        switch_channel_t *new_channel = NULL;
+        unsigned int timelimit = 60;
+        const char *skip, *var;
+
+        switch_memory_pool_t *pool = NULL;
+        callback_t routes = { 0 };
+        lcr_route cur_route = { 0 };
+        char *lcr_profile = NULL;
+        switch_event_t *event = NULL;
+        const char *intrastate = NULL;
+        const char *intralata = NULL;
+        switch_core_session_t *mysession = NULL;
+        switch_channel_t *channel = NULL;
+        switch_caller_profile_t *caller_profile = NULL;
+        
+        dest = strdup(outbound_profile->destination_number);
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Entering lcr endpoint for %s\n", dest);
+        
+        if (!dest) {
+                goto done;
+        }
+        
+        switch_core_new_memory_pool(&pool);
+        routes.pool = pool;
+
+        if (var_event && (skip = switch_event_get_header(var_event, "lcr_recurse_variables")) && switch_false(skip)) {
+                if ((var = switch_event_get_header(var_event, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, "leg_timeout"))) {
+                        timelimit = atoi(var);
+                }
+                var_event = NULL;
+        }
+
+        if (session) {
+                mysession = session;
+                channel = switch_core_session_get_channel(session);
+                if ((var = switch_channel_get_variable(channel, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, "leg_timeout"))) {
+                        timelimit = atoi(var);
+                }
+                routes.session = session;
+                caller_profile = switch_channel_get_caller_profile(channel);
+                intrastate = switch_channel_get_variable(channel, "intrastate");
+                intralata = switch_channel_get_variable(channel, "intralata");
+                cid_name_override = switch_channel_get_variable(channel, "origination_caller_id_name");
+                cid_num_override = switch_channel_get_variable(channel, "origination_caller_id_number");
+                if (zstr(cid_name_override)) {
+                        cid_name_override = switch_channel_get_variable(channel, "effective_caller_id_name");
+                }
+                if (zstr(cid_num_override)) {
+                        cid_num_override = switch_channel_get_variable(channel, "effective_caller_id_number");
+                }
+                if ((var = switch_channel_get_variable(channel, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, "leg_timeout"))) {
+                        timelimit = atoi(var);
+                }
+        } else if (var_event) {
+                char *session_uuid = switch_event_get_header(var_event, "ent_originate_aleg_uuid");
+                if (session_uuid) {
+                        mysession = switch_core_session_locate(session_uuid);
+                }
+                cid_name_override = switch_event_get_header(var_event, "origination_caller_id_name");
+                cid_num_override = switch_event_get_header(var_event, "origination_caller_id_number");
+                if (zstr(cid_name_override)) {
+                        cid_name_override = switch_event_get_header(var_event, "effective_caller_id_name");
+                }
+                if (zstr(cid_num_override)) {
+                        cid_num_override = switch_event_get_header(var_event, "caller_id_number");
+                }
+                if ((var = switch_event_get_header(var_event, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, "leg_timeout"))) {
+                        timelimit = atoi(var);
+                }
+                
+                intrastate = switch_event_get_header(var_event, "intrastate");
+                intralata = switch_event_get_header(var_event, "intralata");
+                //switch_event_dup(&event, var_event);
+                routes.event = var_event;
+        } else {
+                switch_event_create(&event, SWITCH_EVENT_MESSAGE);
+                routes.event = event;
+        }
+        routes.lookup_number = dest;
+        routes.cid = (char *) cid_num_override;
+        
+        if ((flags & SOF_FORKED_DIAL)) {
+                myflags |= SOF_NOBLOCK;
+        }
+
+        if (!(routes.profile = locate_profile(lcr_profile))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown profile: %s\n", lcr_profile);
+                goto done;
+        }
+
+        if (!zstr(intralata) && !strcasecmp((char *)intralata, "true")) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on intralata rates\n");
+                routes.intralata = SWITCH_FALSE;
+        } else if (!zstr(intrastate) && !strcasecmp((char *)intrastate, "true")) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on intrastate rates\n");
+                routes.intrastate = SWITCH_TRUE;
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on interstate rates\n");
+                routes.intrastate = SWITCH_FALSE;
+        }
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", dest);
+
+        if (lcr_do_lookup(&routes) == SWITCH_STATUS_SUCCESS) {
+                if (channel) {
+                        if (zstr(switch_channel_get_variable(channel, "import"))) {
+                                switch_channel_set_variable(channel, "import", "lcr_carrier,lcr_rate");
+                        } else {
+                                const char *tmp = switch_channel_get_variable(channel, "import");
+                                if (!strstr(tmp, "lcr_carrier,lcr_rate")) {
+                                        switch_channel_set_variable_printf(channel, "import", "%s,lcr_carrier,lcr_rate", tmp);
+                                }
+                        }
+                }
+
+                for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
+                        switch_bool_t pop_limit = SWITCH_FALSE;
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Trying route: %s\n", cur_route->dialstring);
+                        if (mysession && cur_route->limit_realm && cur_route->limit_id) {
+                                if (switch_limit_incr(routes.profile->limit_type, mysession, cur_route->limit_realm, cur_route->limit_id, cur_route->limit_max, 0) == SWITCH_STATUS_SUCCESS) {
+                                        pop_limit = SWITCH_TRUE;
+                                } else {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Limit exceeded for route %s, session: %s\n", cur_route->dialstring, mysession ? "true" : "false");
+                                        continue;
+                                }
+                        }
+                        if (switch_ivr_originate(session, new_session, &cause, cur_route->dialstring, timelimit, NULL,
+                                                                         cid_name_override, cid_num_override, NULL, var_event, myflags, cancel_cause) == SWITCH_STATUS_SUCCESS) {
+                                const char *context;
+                                switch_caller_profile_t *cp;
+
+                                new_channel = switch_core_session_get_channel(*new_session);
+
+                                if ((context = switch_channel_get_variable(new_channel, "lcr_context"))) {
+                                        if ((cp = switch_channel_get_caller_profile(new_channel))) {
+                                                cp->context = switch_core_strdup(cp->pool, context);
+                                        }
+                                }
+                                switch_core_session_rwunlock(*new_session);
+                                break;
+                        }
+                        
+                        /* did not connect, release limit */
+                        if (pop_limit) {
+                                switch_limit_release(routes.profile->limit_type, mysession, cur_route->limit_realm, cur_route->limit_id);
+                        }
+                        if (cause == SWITCH_CAUSE_LOSE_RACE || cause == SWITCH_CAUSE_ORIGINATOR_CANCEL) {
+                                break;
+                        }
+                }
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", caller_profile->destination_number);
+        }
+
+ done:
+        if (event) {
+                switch_event_destroy(&event);
+        }
+        if (mysession) {
+                switch_core_session_rwunlock(mysession);
+        }
+        switch_core_destroy_memory_pool(&pool);
+        switch_safe_free(dest);
+
+        if (cause == SWITCH_CAUSE_NONE) {
+                cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
+        }
+
+        return cause;
+}
+
</ins><span class="cx"> SWITCH_STANDARD_DIALPLAN(lcr_dialplan_hunt)
</span><span class="cx"> {
</span><span class="cx">         switch_caller_extension_t *extension = NULL;
</span><span class="lines">@@ -1115,17 +1383,17 @@
</span><span class="cx">         intralata = switch_channel_get_variable(channel, "intralata");
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "intrastate channel var is [%s]\n", intrastate);
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "intralata channel var is [%s]\n", intralata);
</span><del>-        if (!zstr(intralata) && !strcasecmp((char *) intralata, "true")) {
</del><ins>+        if (!zstr(intralata) && !strcasecmp((char *)intralata, "true")) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Select routes based on intralata rates\n");
</span><span class="cx">                 routes.intralata = SWITCH_FALSE;
</span><del>-        } else if (!zstr(intrastate) && !strcasecmp((char *) intrastate, "true")) {
</del><ins>+        } else if (!zstr(intrastate) && !strcasecmp((char *)intrastate, "true")) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Select routes based on intrastate rates\n");
</span><span class="cx">                 routes.intrastate = SWITCH_TRUE;
</span><span class="cx">         } else {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Select routes based on interstate rates\n");
</span><span class="cx">                 routes.intrastate = SWITCH_FALSE;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (!caller_profile) {
</span><span class="cx">                 caller_profile = switch_channel_get_caller_profile(channel);
</span><span class="cx">         }
</span><span class="lines">@@ -1141,16 +1409,40 @@
</span><span class="cx">
</span><span class="cx">                 switch_channel_set_variable(channel, SWITCH_CONTINUE_ON_FAILURE_VARIABLE, "true");
</span><span class="cx">                 switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
</span><del>-                switch_channel_set_variable(channel, "import", "lcr_carrier,lcr_rate");
</del><ins>+                if (zstr(switch_channel_get_variable(channel, "import"))) {
+                        switch_channel_set_variable(channel, "import", "lcr_carrier,lcr_rate");
+                } else {
+                        const char *tmp = switch_channel_get_variable(channel, "import");
+                        if (!strstr(tmp, "lcr_carrier,lcr_rate")) {
+                                switch_channel_set_variable_printf(channel, "import", "%s,lcr_carrier,lcr_rate", tmp);
+                        }
+                }
</ins><span class="cx">
</span><span class="cx">                 for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
</span><del>-                        switch_caller_extension_add_application(session, extension, "bridge", cur_route->dialstring);
</del><ins>+                        char *app = NULL;
+                        char *argc = NULL;
+                        if (cur_route->limit_realm && cur_route->limit_id) {
+                                app = "limit_execute";
+                                argc = switch_core_sprintf(pool, "%s %s %s %d bridge %s",
+                                                                                        routes.profile->limit_type,
+                                                                                        cur_route->limit_realm,
+                                                                                        cur_route->limit_id,
+                                                                                        cur_route->limit_max,
+                                                                                        cur_route->dialstring);
+                        } else {
+                                app = "bridge";
+                                argc = cur_route->dialstring;
+                        }
+                        switch_caller_extension_add_application(session, extension, app, argc);
</ins><span class="cx">                 }
</span><span class="cx">         } else {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", caller_profile->destination_number);
</span><span class="cx">         }
</span><span class="cx">
</span><del>- end:
</del><ins>+end:
+        if (event) {
+                switch_event_destroy(&event);
+        }
</ins><span class="cx">         if (!session) {
</span><span class="cx">                 switch_core_destroy_memory_pool(&pool);
</span><span class="cx">         }
</span><span class="lines">@@ -1161,9 +1453,9 @@
</span><span class="cx"> {
</span><span class="cx">         size_t i;
</span><span class="cx">
</span><del>-        /*switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "repeating %d of '%s'\n", (int)how_many, what); */
-
-        for (i = 0; i < how_many; i++) {
</del><ins>+        /*switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "repeating %d of '%s'\n", (int)how_many, what);*/
+        
+        for (i=0; i<how_many; i++) {
</ins><span class="cx">                 str_stream->write_function(str_stream, "%s", what);
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="lines">@@ -1204,8 +1496,9 @@
</span><span class="cx">         routes.pool = pool;
</span><span class="cx">
</span><span class="cx">         intra = switch_channel_get_variable(channel, "intrastate");
</span><del>-        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "intrastate channel var is [%s]\n", zstr(intra) ? "undef" : intra);
-        if (zstr(intra) || strcasecmp((char *) intra, "true")) {
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "intrastate channel var is [%s]\n",
+                                        zstr(intra) ? "undef" : intra);
+        if (zstr(intra) || strcasecmp((char *)intra, "true")) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Select routes based on interstate rates\n");
</span><span class="cx">                 routes.intrastate = SWITCH_FALSE;
</span><span class="cx">         } else {
</span><span class="lines">@@ -1224,7 +1517,7 @@
</span><span class="cx">                 if (argc > 1) {
</span><span class="cx">                         lcr_profile = argv[1];
</span><span class="cx">                 }
</span><del>-
</del><ins>+                
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "LCR Lookup on %s using profile %s\n", dest, lcr_profile);
</span><span class="cx">                 routes.lookup_number = dest;
</span><span class="cx">                 if (caller_profile) {
</span><span class="lines">@@ -1234,7 +1527,7 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-
</del><ins>+        
</ins><span class="cx">                 if (!(routes.profile = locate_profile(lcr_profile))) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Unknown profile: %s\n", lcr_profile);
</span><span class="cx">                         goto end;
</span><span class="lines">@@ -1249,33 +1542,46 @@
</span><span class="cx">                                 switch_channel_set_variable(channel, vbuf, cur_route->carrier_name);
</span><span class="cx">                                 switch_snprintf(vbuf, sizeof(vbuf), "lcr_codec_%d", cnt);
</span><span class="cx">                                 switch_channel_set_variable(channel, vbuf, cur_route->codec);
</span><del>-                                cnt++;
-                                switch_snprintf(rbp, rbl, "%s|", cur_route->dialstring);
</del><ins>+                                switch_snprintf(vbuf, sizeof(vbuf), "lcr_limit_%d", cnt);
+                                switch_channel_set_variable_printf(channel, vbuf, "%s %s %d", cur_route->limit_realm, cur_route->limit_id, cur_route->limit_max);
+                                l = switch_snprintf(rbp, rbl, "%s|", cur_route->dialstring);
</ins><span class="cx">                                 last_delim = end_of_p(rbp);
</span><del>-                                l = strlen(cur_route->dialstring) + 1;
</del><span class="cx">                                 rbp += l;
</span><span class="cx">                                 rbl -= l;
</span><ins>+                                cnt++;
</ins><span class="cx">                         }
</span><span class="cx">                         switch_snprintf(vbuf, sizeof(vbuf), "%d", cnt - 1);
</span><span class="cx">                         switch_channel_set_variable(channel, "lcr_route_count", vbuf);
</span><span class="cx">                         *(rbuf + strlen(rbuf) - 1) = '\0';
</span><span class="cx">                         switch_channel_set_variable(channel, "lcr_auto_route", rbuf);
</span><del>-                        switch_channel_set_variable(channel, "import", "lcr_carrier,lcr_rate");
</del><ins>+                        if (zstr(switch_channel_get_variable(channel, "import"))) {
+                                switch_channel_set_variable(channel, "import", "lcr_carrier,lcr_rate");
+                        } else {
+                                const char *tmp = switch_channel_get_variable(channel, "import");
+                                if (!strstr(tmp, "lcr_carrier,lcr_rate")) {
+                                        switch_channel_set_variable_printf(channel, "import", "%s,lcr_carrier,lcr_rate", tmp);
+                                }
+                        }
</ins><span class="cx">                 } else {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", dest);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><del>-
- end:
</del><ins>+        
+end:
+        if (routes.event) {
+                switch_event_destroy(&event);
+        }
</ins><span class="cx">         if (!session) {
</span><span class="cx">                 switch_core_destroy_memory_pool(&pool);
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static void write_data(switch_stream_handle_t *stream, switch_bool_t as_xml, const char *key, const char *data, int indent, int maxlen)
-{
</del><ins>+static void write_data(switch_stream_handle_t *stream, switch_bool_t as_xml, const char *key, const char *data, int indent, int maxlen) {
+        if (!data) {
+                data = "";
+        }
</ins><span class="cx">         if (as_xml) {
</span><del>-                str_repeat(indent * 2, " ", stream);
</del><ins>+                str_repeat(indent*2, " ", stream);
</ins><span class="cx">                 stream->write_function(stream, "<%s>%s</%s>\n", key, data, key);
</span><span class="cx">         } else {
</span><span class="cx">                 stream->write_function(stream, " | %s", data);
</span><span class="lines">@@ -1297,13 +1603,17 @@
</span><span class="cx">         switch_event_t *event;
</span><span class="cx">         switch_status_t lookup_status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">         switch_bool_t as_xml = SWITCH_FALSE;
</span><del>-        int rowcount = 0;
</del><ins>+        char *event_str = NULL;
+        switch_xml_t event_xml = NULL;
+        int rowcount=0;
</ins><span class="cx">
</span><span class="cx">         if (zstr(cmd)) {
</span><span class="cx">                 goto usage;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "data passed to lcr is [%s]\n", cmd);
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG
+                                         , "data passed to lcr is [%s]\n", cmd
+                                         );
</ins><span class="cx">
</span><span class="cx">         if (session) {
</span><span class="cx">                 pool = switch_core_session_get_pool(session);
</span><span class="lines">@@ -1314,7 +1624,7 @@
</span><span class="cx">                 cb_struct.event = event;
</span><span class="cx">         }
</span><span class="cx">         cb_struct.pool = pool;
</span><del>-
</del><ins>+        
</ins><span class="cx">         mydata = switch_core_strdup(pool, cmd);
</span><span class="cx">
</span><span class="cx">         if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
</span><span class="lines">@@ -1325,27 +1635,29 @@
</span><span class="cx">                 }
</span><span class="cx">                 if (argc > 2) {
</span><span class="cx">                         int i;
</span><del>-                        for (i = 2; i < argc; i++) {
</del><ins>+                        for (i=2; i<argc; i++) {
</ins><span class="cx">                                 if (!strcasecmp(argv[i], "intrastate")) {
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Select routes based on intrastate rates\n");
</span><span class="cx">                                         cb_struct.intrastate = SWITCH_TRUE;
</span><span class="cx">                                 } else if (!strcasecmp(argv[i], "as")) {
</span><span class="cx">                                         i++;
</span><span class="cx">                                         if (argv[i] && !strcasecmp(argv[i], "xml")) {
</span><del>-                                                as_xml = SWITCH_TRUE;
-                                        } else {
-                                                goto usage;
-                                        }
</del><ins>+                                                        as_xml = SWITCH_TRUE;
+                                                } else {
+                                                        goto usage;
+                                                }
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Set Caller ID to [%s]\n", argv[i]);
</span><span class="cx">                                         /* the only other option we have right now is caller id */
</span><span class="cx">                                         cb_struct.cid = switch_core_strdup(pool, argv[i]);
</span><span class="cx">                                 }
</span><span class="cx">                         }
</span><del>-                }
</del><ins>+                }
</ins><span class="cx">                 if (zstr(cb_struct.cid)) {
</span><span class="cx">                         cb_struct.cid = "18005551212";
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Using default CID [%s]\n", cb_struct.cid);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING
+                                                         , "Using default CID [%s]\n", cb_struct.cid
+                                                         );
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -1389,6 +1701,11 @@
</span><span class="cx">                                         str_repeat(len, " ", stream);
</span><span class="cx">                                 }
</span><span class="cx">
</span><ins>+                                stream->write_function(stream, " | %s", headers[LCR_HEADERS_LIMIT]);
+                                if ((len = (maximum_lengths.limit - strlen(headers[LCR_HEADERS_LIMIT]))) > 0) {
+                                        str_repeat(len, " ", stream);
+                                }
+                                
</ins><span class="cx">                                 stream->write_function(stream, " | %s", headers[LCR_HEADERS_DIALSTRING]);
</span><span class="cx">                                 if ((len = (maximum_lengths.dialstring - strlen(headers[LCR_HEADERS_DIALSTRING]))) > 0) {
</span><span class="cx">                                         str_repeat(len, " ", stream);
</span><span class="lines">@@ -1405,7 +1722,7 @@
</span><span class="cx">                                 if (as_xml) {
</span><span class="cx">                                         stream->write_function(stream, " <row id=\"%d\">\n", rowcount);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 write_data(stream, as_xml, "prefix", current->digit_str, 2, maximum_lengths.digit_str);
</span><span class="cx">                                 write_data(stream, as_xml, "carrier_name", current->carrier_name, 2, maximum_lengths.carrier_name);
</span><span class="cx">                                 write_data(stream, as_xml, "rate", current->rate_str, 2, maximum_lengths.rate);
</span><span class="lines">@@ -1420,10 +1737,26 @@
</span><span class="cx">                                 } else {
</span><span class="cx">                                         write_data(stream, as_xml, "cid", "", 2, maximum_lengths.cid);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
+                                if (current->limit_realm && current->limit_id) {
+                                        char *str = NULL;
+                                        str = switch_core_sprintf(pool, "%s %s %d", current->limit_realm, current->limit_id, current->limit_max);
+                                        
+                                        write_data(stream, as_xml, "limit", str, 2, maximum_lengths.limit);
+                                } else {
+                                        write_data(stream, as_xml, "limit", "", 2, maximum_lengths.limit);
+                                }
+                                
</ins><span class="cx">                                 write_data(stream, as_xml, "dialstring", current->dialstring, 2, maximum_lengths.dialstring);
</span><del>-
</del><span class="cx">                                 if (as_xml) {
</span><ins>+                                        event_xml = switch_event_xmlize(current->fields, NULL);
+                                        event_str = switch_xml_toxml(event_xml, SWITCH_FALSE);
+                                        stream->write_function(stream, event_str);
+                                        switch_xml_free(event_xml);
+                                        switch_safe_free(event_str);
+                                }
+                                
+                                if (as_xml) {
</ins><span class="cx">                                         stream->write_function(stream, " </row>\n");
</span><span class="cx">                                 } else {
</span><span class="cx">                                         stream->write_function(stream, " |\n");
</span><span class="lines">@@ -1446,14 +1779,14 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>- end:
</del><ins>+end:
</ins><span class="cx">         if (!session) {
</span><span class="cx">                 if (pool) {
</span><span class="cx">                         switch_core_destroy_memory_pool(&pool);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><del>- usage:
</del><ins>+usage:
</ins><span class="cx">         stream->write_function(stream, "USAGE: %s\n", LCR_SYNTAX);
</span><span class="cx">         goto end;
</span><span class="cx"> }
</span><span class="lines">@@ -1482,7 +1815,7 @@
</span><span class="cx">                         for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
</span><span class="cx">                                 switch_hash_this(hi, NULL, NULL, &val);
</span><span class="cx">                                 profile = (profile_t *) val;
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 stream->write_function(stream, "Name:\t\t%s\n", profile->name);
</span><span class="cx">                                 if (zstr(profile->custom_sql)) {
</span><span class="cx">                                         stream->write_function(stream, " ID:\t\t%d\n", profile->id);
</span><span class="lines">@@ -1495,9 +1828,15 @@
</span><span class="cx">                                 stream->write_function(stream, " has intrastate:\t%s\n", profile->profile_has_intrastate ? "true" : "false");
</span><span class="cx">                                 stream->write_function(stream, " has intralata:\t%s\n", profile->profile_has_intralata ? "true" : "false");
</span><span class="cx">                                 stream->write_function(stream, " has npanxx:\t%s\n", profile->profile_has_npanxx ? "true" : "false");
</span><del>-                                stream->write_function(stream, " Reorder rate:\t%s\n", profile->reorder_by_rate ? "enabled" : "disabled");
-                                stream->write_function(stream, " Info in headers:\t%s\n", profile->info_in_headers ? "enabled" : "disabled");
-                                stream->write_function(stream, " Quote IN() List:\t%s\n", profile->quote_in_list ? "enabled" : "disabled");
</del><ins>+                                stream->write_function(stream, " Reorder rate:\t%s\n",
+                                                                                profile->reorder_by_rate ? "enabled" : "disabled");
+                                stream->write_function(stream, " Info in headers:\t%s\n",
+                                                                                profile->info_in_headers ? "enabled" : "disabled");
+                                stream->write_function(stream, " Quote IN() List:\t%s\n",
+                                                                                profile->quote_in_list ? "enabled" : "disabled");
+                                stream->write_function(stream, " Import fields:\t%s\n",
+                                        profile->export_fields_str ? profile->export_fields_str : "(null)");
+                                stream->write_function(stream, " Limit type:\t%s\n", profile->limit_type);
</ins><span class="cx">                                 stream->write_function(stream, "\n");
</span><span class="cx">                         }
</span><span class="cx">                 } else {
</span><span class="lines">@@ -1506,20 +1845,20 @@
</span><span class="cx">         }
</span><span class="cx">         switch_safe_free(mydata);
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><del>- usage:
</del><ins>+usage:
</ins><span class="cx">         switch_safe_free(mydata);
</span><span class="cx">         stream->write_function(stream, "-ERR %s\n", LCR_ADMIN_SYNTAX);
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><del>-
</del><ins>+                
</ins><span class="cx"> }
</span><del>-
</del><ins>+        
</ins><span class="cx"> SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load)
</span><span class="cx"> {
</span><span class="cx">         switch_api_interface_t *dialplan_lcr_api_interface;
</span><span class="cx">         switch_api_interface_t *dialplan_lcr_api_admin_interface;
</span><span class="cx">         switch_application_interface_t *app_interface;
</span><span class="cx">         switch_dialplan_interface_t *dp_interface;
</span><del>-
</del><ins>+        
</ins><span class="cx">         *module_interface = switch_loadable_module_create_module_interface(pool, modname);
</span><span class="cx">
</span><span class="cx">         if (!switch_odbc_available()) {
</span><span class="lines">@@ -1544,6 +1883,10 @@
</span><span class="cx">                                  lcr_app_function, "<number>", SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC);
</span><span class="cx">         SWITCH_ADD_DIALPLAN(dp_interface, "lcr", lcr_dialplan_hunt);
</span><span class="cx">
</span><ins>+        lcr_endpoint_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_ENDPOINT_INTERFACE);
+        lcr_endpoint_interface->interface_name = "lcr";
+        lcr_endpoint_interface->io_routines = &lcr_io_routines;
+        
</ins><span class="cx">         /* indicate that the module should continue to be loaded */
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>