<!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 &lt;anthm@freeswitch.org&gt;
</del><ins>+ * Copyright (C) 2005-2009, Anthony Minessale II &lt;anthm@freeswitch.org&gt;
</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 &quot;lcr &lt;digits&gt; [&lt;lcr profile&gt;] [caller_id] [intrastate] [as xml]&quot;
</span><span class="cx"> #define LCR_ADMIN_SYNTAX &quot;lcr_admin show profiles&quot;
</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">         &quot;Digit Match&quot;,
</span><span class="cx">         &quot;Carrier&quot;,
</span><span class="cx">         &quot;Rate&quot;,
</span><span class="cx">         &quot;Dialstring&quot;,
</span><span class="cx">         &quot;Codec&quot;,
</span><del>-        &quot;CID Regexp&quot;
</del><ins>+        &quot;CID Regexp&quot;,
+        &quot;Limit&quot;,
</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] == '/') &amp;&amp; src[len - 1] == '/') {
</del><ins>+        
+        if ((src[0] == '/') &amp;&amp; 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, &quot;expanded src: %s, dst: %s\n&quot;, src, dst);
</span><span class="cx">         }
</span><del>-
</del><ins>+  
</ins><span class="cx">         if ((proceed = switch_regex_perform(number, src, &amp;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, &quot;Memory Error!\n&quot;);
</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-&gt;digit_len - cur_route-&gt;tstrip) + 1);
</span><span class="cx">         lstrip = cur_route-&gt;lstrip;
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (cur_route-&gt;tstrip &gt; 0) {
</span><span class="cx">                 if (strlen(destination_number) &gt; 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-&gt;lstrip &gt; 0) {
</span><span class="cx">                 if (strlen(destination_number) &gt; 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 = &quot;&quot;;
</span><span class="cx">         if (!zstr(cur_route-&gt;codec)) {
</span><span class="cx">                 codec = switch_core_sprintf(pool, &quot;,absolute_codec_string=%s&quot;, cur_route-&gt;codec);
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         cid = &quot;&quot;;
</span><span class="cx">         if (!zstr(cur_route-&gt;cid)) {
</span><del>-                cid = switch_core_sprintf(pool, &quot;,origination_caller_id_number=%s&quot;, do_cid(pool, cur_route-&gt;cid, caller_id, session));
</del><ins>+                cid = switch_core_sprintf(pool, &quot;,origination_caller_id_number=%s&quot;, 
+                                                                  do_cid(pool, cur_route-&gt;cid, caller_id, session));
</ins><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         header = &quot;&quot;;
</span><span class="cx">         if (profile-&gt;info_in_headers) {
</span><del>-                header = switch_core_sprintf(pool, &quot;,sip_h_X-LCR-INFO=lcr_rate=%s;lcr_carrier=%s&quot;, cur_route-&gt;rate_str, cur_route-&gt;carrier_name);
</del><ins>+                header = switch_core_sprintf(pool, &quot;,sip_h_X-LCR-INFO=lcr_rate=%s;lcr_carrier=%s&quot;,
+                                                                          cur_route-&gt;rate_str, 
+                                                                          cur_route-&gt;carrier_name);
</ins><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (zstr(cur_route-&gt;user_rate_str)) {
</span><span class="cx">                 user_rate = &quot;&quot;;
</span><span class="cx">         } else {
</span><span class="cx">                 user_rate = switch_core_sprintf(pool, &quot;,lcr_user_rate=%s&quot;, cur_route-&gt;user_rate_str);
</span><span class="cx">         }
</span><del>-
-        data =
-                switch_core_sprintf(pool, &quot;[lcr_carrier=%s,lcr_rate=%s%s%s%s%s]%s%s%s%s%s&quot;, cur_route-&gt;carrier_name, cur_route-&gt;rate_str, user_rate, codec, cid,
-                                                        header, cur_route-&gt;gw_prefix, cur_route-&gt;prefix, destination_number, cur_route-&gt;suffix, cur_route-&gt;gw_suffix);
-
</del><ins>+        
+        export_fields = &quot;&quot;;
+        if (profile-&gt;export_fields_cnt &gt; 0) {
+                int i = 0;
+                char *val = NULL;
+                for (i = 0; i &lt; profile-&gt;export_fields_cnt; i++) {
+                        val = switch_event_get_header(cur_route-&gt;fields, profile-&gt;export_fields[i]);
+                        if (val) {
+                                export_fields = switch_core_sprintf(pool, &quot;%s,%s=%s&quot;, 
+                                                                                                        export_fields,
+                                                                                                        profile-&gt;export_fields[i], 
+                                                                                                        val);
+                        }
+                }
+        }
+        
+        data = switch_core_sprintf(pool, &quot;[lcr_carrier=%s,lcr_rate=%s%s%s%s%s%s]%s%s%s%s%s&quot;
+                                                                , cur_route-&gt;carrier_name, cur_route-&gt;rate_str
+                                                                , user_rate, codec, cid, header, export_fields
+                                                                , cur_route-&gt;gw_prefix, cur_route-&gt;prefix
+                                                                , destination_number, cur_route-&gt;suffix, cur_route-&gt;gw_suffix);
+                        
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;Returning Dialstring %s\n&quot;, 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, &quot;Error invalid profile %s\n&quot;, 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-&gt;rate = 8;
</span><span class="cx">         maxes-&gt;codec = (headers[LCR_HEADERS_CODEC] == NULL ? 0 : strlen(headers[LCR_HEADERS_CODEC]));
</span><span class="cx">         maxes-&gt;cid = (headers[LCR_HEADERS_CID] == NULL ? 0 : strlen(headers[LCR_HEADERS_CID]));
</span><ins>+        maxes-&gt;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-&gt;next) {
</span><span class="cx">                 size_t this_len;
</span><span class="cx"> 
</span><del>-                if (current-&gt;carrier_name != NULL) {
</del><ins>+                if (current-&gt;carrier_name) {
</ins><span class="cx">                         this_len = strlen(current-&gt;carrier_name);
</span><span class="cx">                         if (this_len &gt; maxes-&gt;carrier_name) {
</span><span class="cx">                                 maxes-&gt;carrier_name = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current-&gt;dialstring != NULL) {
</del><ins>+                if (current-&gt;dialstring) {
</ins><span class="cx">                         this_len = strlen(current-&gt;dialstring);
</span><span class="cx">                         if (this_len &gt; maxes-&gt;dialstring) {
</span><span class="cx">                                 maxes-&gt;dialstring = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current-&gt;digit_str != NULL) {
</del><ins>+                if (current-&gt;digit_str) {
</ins><span class="cx">                         if (current-&gt;digit_len &gt; maxes-&gt;digit_str) {
</span><span class="cx">                                 maxes-&gt;digit_str = current-&gt;digit_len;
</span><del>-                        }
</del><ins>+                        } 
</ins><span class="cx">                 }
</span><del>-                if (current-&gt;rate_str != NULL) {
</del><ins>+                if (current-&gt;rate_str) {
</ins><span class="cx">                         this_len = strlen(current-&gt;rate_str);
</span><span class="cx">                         if (this_len &gt; maxes-&gt;rate) {
</span><span class="cx">                                 maxes-&gt;rate = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current-&gt;codec != NULL) {
-                        this_len = strlen(current-&gt;codec);
</del><ins>+                if (current-&gt;codec) {
+                        this_len= strlen(current-&gt;codec);
</ins><span class="cx">                         if (this_len &gt; maxes-&gt;codec) {
</span><span class="cx">                                 maxes-&gt;codec = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                if (current-&gt;cid != NULL) {
</del><ins>+                if (current-&gt;cid) {
</ins><span class="cx">                         this_len = strlen(current-&gt;cid);
</span><span class="cx">                         if (this_len &gt; maxes-&gt;cid) {
</span><span class="cx">                                 maxes-&gt;cid = this_len;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+
+                if (current-&gt;limit_realm &amp;&amp; current-&gt;limit_id) {
+                        this_len = strlen(current-&gt;limit_realm) + strlen(current-&gt;limit_id) + 5;
+                        if (this_len &gt; maxes-&gt;limit) {
+                                maxes-&gt;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(&amp;dbh, SCDB_TYPE_ODBC, &amp;options) != SWITCH_STATUS_SUCCESS)
-                        dbh = NULL;
</del><ins>+                if (switch_cache_db_get_db_handle(&amp;dbh, SCDB_TYPE_ODBC, &amp;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 = &quot;random()&quot;;
</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 &gt; 0; n--) {
</span><span class="cx">                 digits_copy[n] = '\0';
</span><del>-                dig_stream.write_function(&amp;dig_stream, &quot;%s%s%s%s&quot;, (n == digit_len ? &quot;&quot; : &quot;, &quot;), (quote ? &quot;'&quot; : &quot;&quot;), digits_copy, (quote ? &quot;'&quot; : &quot;&quot;));
</del><ins>+                dig_stream.write_function(&amp;dig_stream, &quot;%s%s%s%s&quot;, 
+                                                                        (n==digit_len ? &quot;&quot; : &quot;, &quot;), 
+                                                                        (quote ? &quot;'&quot; : &quot;&quot;),
+                                                                        digits_copy,
+                                                                        (quote ? &quot;'&quot; : &quot;&quot;));
</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-&gt;profile-&gt;custom_sql_has_percent == SWITCH_TRUE) {
</span><span class="cx">                 tmpSQL = switch_string_replace(custom_sql, &quot;%q&quot;, 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-&gt;profile-&gt;custom_sql_has_vars == SWITCH_TRUE) {
</span><span class="cx">                 if (cb_struct-&gt;session) {
</span><span class="cx">                         channel = switch_core_session_get_channel(cb_struct-&gt;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-&gt;event) {
</span><span class="cx">                         /* use event system to expand vars */
</span><span class="cx">                         newSQL = switch_event_expand_headers(cb_struct-&gt;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-&gt;session), SWITCH_LOG_CRIT,
</span><del>-                                                          &quot;mod_lcr called without a valid session while using a custom_sql that has channel variables.\n&quot;);
</del><ins>+                                                                &quot;mod_lcr called without a valid session while using a custom_sql that has channel variables.\n&quot;);
</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 &amp;&amp; (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-&gt;pool;
</span><ins>+        
+        additional = switch_core_alloc(pool, sizeof(lcr_obj_t));
+        additional-&gt;fields = switch_core_alloc(pool, sizeof(switch_event_t));
</ins><span class="cx"> 
</span><del>-
-        if (argc &lt; LCR_QUERY_COLS_REQUIRED) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
-                                                  &quot;Unexpected number of columns returned for SQL.  Returned column count: %d. &quot;
-                                                  &quot;If using a custom sql for this profile, verify it is correct.  Otherwise file a bug report.\n&quot;, argc);
-                return SWITCH_STATUS_GENERR;
</del><ins>+        for (i = 0; i &lt; argc ; i++) {
+                if (CF(&quot;lcr_digits&quot;)) {
+                        additional-&gt;digit_len = strlen(argv[i]);
+                        additional-&gt;digit_str = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_prefix&quot;)) {
+                        additional-&gt;prefix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_suffix&quot;)) {
+                        additional-&gt;suffix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_carrier_name&quot;)) {
+                        additional-&gt;carrier_name = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_rate_field&quot;)) {
+                        additional-&gt;rate = (float)atof(switch_str_nil(argv[i]));
+                        additional-&gt;rate_str = switch_core_sprintf(pool, &quot;%0.5f&quot;, additional-&gt;rate);
+                } else if (CF(&quot;lcr_gw_prefix&quot;)) {
+                        additional-&gt;gw_prefix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_gw_suffix&quot;)) {
+                        additional-&gt;gw_suffix = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_lead_strip&quot;)) {
+                        additional-&gt;lstrip = atoi(switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_trail_strip&quot;)) {
+                        additional-&gt;tstrip = atoi(switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_codec&quot;)) {
+                        additional-&gt;codec = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_cid&quot;)) {
+                        additional-&gt;cid = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_user_rate&quot;)) {
+                        additional-&gt;user_rate = (float)atof(switch_str_nil(argv[i]));
+                        additional-&gt;user_rate_str = switch_core_sprintf(pool, &quot;%0.5f&quot;, additional-&gt;user_rate);
+                } else if (CF(&quot;lcr_limit_realm&quot;)) {
+                        additional-&gt;limit_realm = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_limit_id&quot;)) {
+                        additional-&gt;limit_id = switch_core_strdup(pool, switch_str_nil(argv[i]));
+                } else if (CF(&quot;lcr_limit_max&quot;)) {
+                        additional-&gt;limit_max = (float)atof(switch_str_nil(argv[i]));
+                }
+                
+                /* add all fields to the fields event */
+                switch_event_add_header_string(additional-&gt;fields, SWITCH_STACK_BOTTOM, columnNames[i], argv[i]);
</ins><span class="cx">         }
</span><del>-
-        if (zstr(argv[LCR_GW_PREFIX_PLACE]) &amp;&amp; zstr(argv[LCR_GW_SUFFIX_PLACE])) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                  &quot;There's no way to dial this Gateway: Carrier: \&quot;%s\&quot; Prefix: \&quot;%s\&quot;, Suffix \&quot;%s\&quot;\n&quot;,
-                                                  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-&gt;matches++;
</span><del>-        additional = switch_core_alloc(pool, sizeof(lcr_obj_t));
-
-        additional-&gt;digit_len = strlen(argv[LCR_DIGITS_PLACE]);
-        additional-&gt;digit_str = switch_core_strdup(pool, switch_str_nil(argv[LCR_DIGITS_PLACE]));
-        additional-&gt;suffix = switch_core_strdup(pool, switch_str_nil(argv[LCR_SUFFIX_PLACE]));
-        additional-&gt;prefix = switch_core_strdup(pool, switch_str_nil(argv[LCR_PREFIX_PLACE]));
-        additional-&gt;carrier_name = switch_core_strdup(pool, switch_str_nil(argv[LCR_CARRIER_PLACE]));
-        additional-&gt;rate = (float) atof(switch_str_nil(argv[LCR_RATE_PLACE]));
-        additional-&gt;rate_str = switch_core_sprintf(pool, &quot;%0.5f&quot;, additional-&gt;rate);
-        additional-&gt;gw_prefix = switch_core_strdup(pool, switch_str_nil(argv[LCR_GW_PREFIX_PLACE]));
-        additional-&gt;gw_suffix = switch_core_strdup(pool, switch_str_nil(argv[LCR_GW_SUFFIX_PLACE]));
-        additional-&gt;lstrip = atoi(switch_str_nil(argv[LCR_LSTRIP_PLACE]));
-        additional-&gt;tstrip = atoi(switch_str_nil(argv[LCR_TSTRIP_PLACE]));
-        if (argc &gt; LCR_CODEC_PLACE) {
-                additional-&gt;codec = switch_core_strdup(pool, switch_str_nil(argv[LCR_CODEC_PLACE]));
-        }
-        if (argc &gt; LCR_CID_PLACE) {
-                additional-&gt;cid = switch_core_strdup(pool, switch_str_nil(argv[LCR_CID_PLACE]));
-        }
-        if (argc &gt; LCR_USER_RATE_PLACE) {
-                additional-&gt;user_rate = (float) atof(switch_str_nil(argv[LCR_USER_RATE_PLACE]));
-                additional-&gt;user_rate_str = switch_core_sprintf(pool, &quot;%0.5f&quot;, additional-&gt;user_rate);
-        }
</del><ins>+        
</ins><span class="cx">         additional-&gt;dialstring = get_bridge_data(pool, cbt-&gt;lookup_number, cbt-&gt;cid, additional, cbt-&gt;profile, cbt-&gt;session);
</span><span class="cx"> 
</span><span class="cx">         if (cbt-&gt;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-&gt;head; current; current = current-&gt;next) {
</span><del>-
</del><ins>+        
</ins><span class="cx">                 key = switch_core_sprintf(pool, &quot;%s:%s&quot;, additional-&gt;gw_prefix, additional-&gt;gw_suffix);
</span><span class="cx">                 if (switch_core_hash_find(cbt-&gt;dedup_hash, key)) {
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Ignoring Duplicate route for termination point (%s)\n&quot;, key);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
+                                                                &quot;Ignoring Duplicate route for termination point (%s)\n&quot;,
+                                                                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-&gt;rate &gt; additional-&gt;rate) {
</span><span class="cx">                                 /* insert myself here */
</span><span class="cx">                                 if (current-&gt;prev != NULL) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Adding %s before %s\n&quot;, additional-&gt;carrier_name, current-&gt;carrier_name);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Adding %s before %s\n&quot;, 
+                                                                                additional-&gt;carrier_name, current-&gt;carrier_name);
</ins><span class="cx">                                         current-&gt;prev-&gt;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, &quot;Inserting %s to list head before %s\n&quot;,
-                                                                          additional-&gt;carrier_name, current-&gt;carrier_name);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Inserting %s to list head before %s\n&quot;, 
+                                                                                additional-&gt;carrier_name, current-&gt;carrier_name);
</ins><span class="cx">                                         cbt-&gt;head = additional;
</span><span class="cx">                                 }
</span><span class="cx">                                 additional-&gt;next = current;
</span><span class="lines">@@ -650,7 +689,7 @@
</span><span class="cx">                                 break;
</span><span class="cx">                         } else if (current-&gt;next == NULL) {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;adding %s to end of list after %s\n&quot;,
</span><del>-                                                                  additional-&gt;carrier_name, current-&gt;carrier_name);
</del><ins>+                                                                        additional-&gt;carrier_name, current-&gt;carrier_name);
</ins><span class="cx">                                 current-&gt;next = additional;
</span><span class="cx">                                 additional-&gt;prev = current;
</span><span class="cx">                                 if (switch_core_hash_insert(cbt-&gt;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], &quot;state&quot;)) {
</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, &quot;Type: %s, Count: %d\n&quot;, argv[0], count);
-
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Type: %s, Count: %d\n&quot;, 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-&gt;lookup_number || (strlen(cb_struct-&gt;lookup_number) != 11 &amp;&amp; *cb_struct-&gt;lookup_number != '1')) {
</del><ins>+                e164 format without the +
+                NANP only (so 11 digits starting with 1)
+        */
+        if (!cb_struct-&gt;lookup_number || !(strlen(cb_struct-&gt;lookup_number) == 11 &amp;&amp; *cb_struct-&gt;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-&gt;cid || (strlen(cb_struct-&gt;cid) != 11 &amp;&amp; *cb_struct-&gt;cid != '1')) {
</del><ins>+        if (!cb_struct-&gt;cid || !(strlen(cb_struct-&gt;cid) == 11 &amp;&amp; *cb_struct-&gt;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-&gt;lookup_number, cb_struct-&gt;cid, 4)) {
-           cb_struct-&gt;intrastate = SWITCH_TRUE;
-           return SWITCH_STATUS_SUCCESS;
-           }
-         */
-
</del><ins>+        if (!strncmp(cb_struct-&gt;lookup_number, cb_struct-&gt;cid, 4)) {
+                cb_struct-&gt;intrastate = SWITCH_TRUE;
+                return SWITCH_STATUS_SUCCESS;
+        }
+        */
+        
</ins><span class="cx">         sql = switch_core_sprintf(cb_struct-&gt;pool,
</span><del>-                                                          &quot;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)&quot;
-                                                          &quot; UNION &quot;
-                                                          &quot;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)&quot;,
-                                                          cb_struct-&gt;lookup_number + 1, cb_struct-&gt;lookup_number + 4,
-                                                          cb_struct-&gt;cid + 1, cb_struct-&gt;cid + 4,
-                                                          cb_struct-&gt;lookup_number + 1, cb_struct-&gt;lookup_number + 4, cb_struct-&gt;cid + 1, cb_struct-&gt;cid + 4);
</del><ins>+                                                                &quot;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)&quot;
+                                                                &quot; UNION &quot;
+                                                                &quot;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)&quot;,
+                                                                cb_struct-&gt;lookup_number+1, cb_struct-&gt;lookup_number+4,
+                                                                cb_struct-&gt;cid+1, cb_struct-&gt;cid+4,
+                                                                cb_struct-&gt;lookup_number+1, cb_struct-&gt;lookup_number+4,
+                                                                cb_struct-&gt;cid+1, cb_struct-&gt;cid+4);
</ins><span class="cx"> 
</span><del>-        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct-&gt;session), SWITCH_LOG_DEBUG, &quot;SQL: %s\n&quot;, sql);
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct-&gt;session), SWITCH_LOG_DEBUG, &quot;SQL: %s\n&quot;, 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-&gt;lookup_number != NULL);
</span><span class="cx"> 
</span><span class="cx">         digits_copy = string_digitsonly(cb_struct-&gt;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(&amp;cb_struct-&gt;dedup_hash, cb_struct-&gt;pool) != SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct-&gt;session), SWITCH_LOG_ERROR, &quot;Error initializing the dedup hash\n&quot;);
</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-&gt;pool, digits_copy, cb_struct-&gt;profile-&gt;quote_in_list);
</span><del>-
</del><ins>+        
</ins><span class="cx">         if (profile-&gt;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-&gt;intralata == SWITCH_TRUE &amp;&amp; profile-&gt;profile_has_intralata == SWITCH_TRUE) {
</span><span class="cx">                 rate_field = switch_core_strdup(cb_struct-&gt;pool, &quot;intralata_rate&quot;);
</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-&gt;session), SWITCH_LOG_DEBUG, &quot;intra routing [state:%d lata:%d] so rate field is [%s]\n&quot;,
</span><span class="cx">                                           cb_struct-&gt;intrastate, cb_struct-&gt;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-&gt;session) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(cb_struct-&gt;session), SWITCH_LOG_DEBUG, &quot;we have a session\n&quot;);
</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-&gt;custom_sql, cb_struct, digits_copy);
</span><span class="cx">         if (!safe_sql) {
</span><del>-                switch_event_safe_destroy(&amp;cb_struct-&gt;event);
</del><span class="cx">                 switch_core_hash_destroy(&amp;cb_struct-&gt;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-&gt;session), SWITCH_LOG_DEBUG, &quot;SQL: %s\n&quot;, (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-&gt;session), SWITCH_LOG_DEBUG, &quot;SQL: %s\n&quot;, (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(&amp;cb_struct-&gt;event);
</del><span class="cx">         switch_core_hash_destroy(&amp;cb_struct-&gt;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(&amp;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, &quot;Unknown profile: %s\n&quot;, lcr_profile);
</span><span class="cx">                 return SWITCH_FALSE;
</span><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         routes.lookup_number = &quot;15555551212&quot;;
</span><span class="cx">         routes.cid = &quot;18005551212&quot;;
</span><del>-        return (lcr_do_lookup(&amp;routes) == SWITCH_STATUS_SUCCESS) ? SWITCH_TRUE : SWITCH_FALSE;
</del><ins>+        return (lcr_do_lookup(&amp;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, &quot;dsn is \&quot;%s\&quot;, user is \&quot;%s\&quot;\n&quot;, globals.odbc_dsn, globals.odbc_user);
</del><ins>+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
+                                                  , &quot;dsn is \&quot;%s\&quot;, user is \&quot;%s\&quot;\n&quot;
+                                                  , 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, &quot;Cannot Open ODBC Database!\n&quot;);
</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, &quot;Database RANDOM function set to %s\n&quot;, 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, &quot;param&quot;); param; param = param-&gt;next) {
</span><span class="lines">@@ -914,12 +958,12 @@
</span><span class="cx"> 
</span><span class="cx">                                 var = (char *) switch_xml_attr_soft(param, &quot;name&quot;);
</span><span class="cx">                                 val = (char *) switch_xml_attr_soft(param, &quot;value&quot;);
</span><del>-
-                                if (!strcasecmp(var, &quot;order_by&quot;) &amp;&amp; !zstr(val)) {
</del><ins>+                                
+                                if (!strcasecmp(var, &quot;order_by&quot;)  &amp;&amp; !zstr(val)) {
</ins><span class="cx">                                         thisorder = &amp;order_by;
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;param val is %s\n&quot;, val);
</span><span class="cx">                                         if ((argc = switch_separate_string(val, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
</span><del>-                                                for (x = 0; x &lt; argc; x++) {
</del><ins>+                                                for (x=0; x&lt;argc; x++) {
</ins><span class="cx">                                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;arg #%d/%d is %s\n&quot;, x, argc, argv[x]);
</span><span class="cx">                                                         if (!zstr(argv[x])) {
</span><span class="cx">                                                                 if (!strcasecmp(argv[x], &quot;quality&quot;)) {
</span><span class="lines">@@ -954,78 +998,85 @@
</span><span class="cx">                                         info_in_headers = val;
</span><span class="cx">                                 } else if (!strcasecmp(var, &quot;quote_in_list&quot;) &amp;&amp; !zstr(val)) {
</span><span class="cx">                                         quote_in_list = val;
</span><ins>+                                } else if (!strcasecmp(var, &quot;export_fields&quot;) &amp;&amp; !zstr(val)) {
+                                        export_fields = val;
+                                } else if (!strcasecmp(var, &quot;limit_type&quot;) &amp;&amp; !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, &quot;No name specified.\n&quot;);
</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-&gt;name = switch_core_strdup(globals.pool, name);
</span><del>-
-                                if (!zstr((char *) order_by.data)) {
-                                        profile-&gt;order_by = switch_core_strdup(globals.pool, (char *) order_by.data);
</del><ins>+                                
+                                if (!zstr((char *)order_by.data)) {
+                                        profile-&gt;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-&gt;order_by = &quot;, ${lcr_rate_field}&quot;;
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><span class="cx">                                 if (!zstr(id_s)) {
</span><del>-                                        profile-&gt;id = (uint16_t) atoi(id_s);
</del><ins>+                                        profile-&gt;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(&amp;sql_stream,
-                                                                                          &quot;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 &quot;);
</del><ins>+                                        sql_stream.write_function(&amp;sql_stream, 
+                                                                                          &quot;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 &quot;
+                                                                                          );
</ins><span class="cx">                                         if (db_check(&quot;SELECT codec from carrier_gateway limit 1&quot;) == SWITCH_TRUE) {
</span><del>-                                                sql_stream.write_function(&amp;sql_stream, &quot;, cg.codec &quot;);
</del><ins>+                                                sql_stream.write_function(&amp;sql_stream, &quot;, cg.codec AS lcr_codec &quot;);
</ins><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;codec field defined.\n&quot;);
</span><span class="cx">                                         } else {
</span><del>-                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                                  &quot;codec field not defined, please update your lcr carrier_gateway database schema.\n&quot;);
</del><ins>+                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;codec field not defined, please update your lcr carrier_gateway database schema.\n&quot;);
</ins><span class="cx">                                         }
</span><span class="cx">                                         if (db_check(&quot;SELECT cid from lcr limit 1&quot;) == SWITCH_TRUE) {
</span><del>-                                                sql_stream.write_function(&amp;sql_stream, &quot;, l.cid &quot;);
</del><ins>+                                                sql_stream.write_function(&amp;sql_stream, &quot;, l.cid AS lcr_cid &quot;);
</ins><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;cid field defined.\n&quot;);
</span><span class="cx">                                         } else {
</span><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;cid field not defined, please update your lcr database schema.\n&quot;);
</span><span class="cx">                                         }
</span><del>-                                        sql_stream.write_function(&amp;sql_stream,
-                                                                                          &quot;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 (&quot;);
</del><ins>+                                        sql_stream.write_function(&amp;sql_stream, &quot;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 (&quot;);
</ins><span class="cx">                                         sql_stream.write_function(&amp;sql_stream, &quot;${lcr_query_expanded_digits}&quot;);
</span><span class="cx">                                         sql_stream.write_function(&amp;sql_stream, &quot;) AND CURRENT_TIMESTAMP BETWEEN date_start AND date_end &quot;);
</span><span class="cx">                                         if (profile-&gt;id &gt; 0) {
</span><span class="cx">                                                 sql_stream.write_function(&amp;sql_stream, &quot;AND lcr_profile=%d &quot;, profile-&gt;id);
</span><span class="cx">                                         }
</span><del>-                                        sql_stream.write_function(&amp;sql_stream, &quot;ORDER BY digits DESC%s&quot;, profile-&gt;order_by);
</del><ins>+                                        sql_stream.write_function(&amp;sql_stream, &quot;ORDER BY digits DESC%s&quot;, 
+                                                                                                                        profile-&gt;order_by);
</ins><span class="cx">                                         if (db_random) {
</span><span class="cx">                                                 sql_stream.write_function(&amp;sql_stream, &quot;, %s&quot;, db_random);
</span><span class="cx">                                         }
</span><span class="cx">                                         sql_stream.write_function(&amp;sql_stream, &quot;;&quot;);
</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-&gt;profile_has_intralata = db_check(&quot;SELECT intralata_rate FROM lcr LIMIT 1&quot;);
</span><span class="cx">                                 if (profile-&gt;profile_has_intralata != SWITCH_TRUE) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                          &quot;no \&quot;intralata_rate\&quot; field found in the \&quot;lcr\&quot; table, routing by intralata rates will be disabled until the field is added and mod_lcr is reloaded\n&quot;);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, 
+                                                                          &quot;no \&quot;intralata_rate\&quot; field found in the \&quot;lcr\&quot; table, routing by intralata rates will be disabled until the field is added and mod_lcr is reloaded\n&quot;
+                                                                          );
</ins><span class="cx">                                 }
</span><span class="cx">                                 profile-&gt;profile_has_intrastate = db_check(&quot;SELECT intrastate_rate FROM lcr LIMIT 1&quot;);
</span><span class="cx">                                 if (profile-&gt;profile_has_intrastate != SWITCH_TRUE) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                          &quot;no \&quot;intrastate_rate\&quot; field found in the \&quot;lcr\&quot; table, routing by intrastate rates will be disabled until the field is added and mod_lcr is reloaded\n&quot;);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, 
+                                                                          &quot;no \&quot;intrastate_rate\&quot; field found in the \&quot;lcr\&quot; table, routing by intrastate rates will be disabled until the field is added and mod_lcr is reloaded\n&quot;
+                                                                          );
</ins><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 profile-&gt;profile_has_npanxx = db_check(&quot;SELECT npa, nxx, state FROM npa_nxx_company_ocn LIMIT 1&quot;);
</span><span class="cx">                                 if (profile-&gt;profile_has_npanxx != SWITCH_TRUE) {
</span><del>-                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
-                                                                          &quot;no \&quot;npa_nxx_company_ocn\&quot; table found in the \&quot;lcr\&quot; database, automatic intrastate detection will be disabled until the table is added and mod_lcr is reloaded\n&quot;);
</del><ins>+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, 
+                                                                          &quot;no \&quot;npa_nxx_company_ocn\&quot; table found in the \&quot;lcr\&quot; database, automatic intrastate detection will be disabled until the table is added and mod_lcr is reloaded\n&quot;
+                                                                          );
</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, &quot;%&quot;)) {
</span><span class="cx">                                         profile-&gt;custom_sql_has_percent = SWITCH_TRUE;
</span><span class="cx">                                 }
</span><del>-                                profile-&gt;custom_sql = switch_core_strdup(globals.pool, (char *) custom_sql);
-
</del><ins>+                                profile-&gt;custom_sql = switch_core_strdup(globals.pool, (char *)custom_sql);
+                                
</ins><span class="cx">                                 if (!zstr(reorder_by_rate)) {
</span><span class="cx">                                         profile-&gt;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-&gt;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-&gt;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-&gt;export_fields_str = switch_core_strdup(globals.pool, export_fields);
+                                        profile-&gt;export_fields_cnt = argc2;
+                                        argvdup = switch_core_alloc(globals.pool, sizeof(argv2));
+                                        memcpy(argvdup, argv2, sizeof(argv2));
+                                        profile-&gt;export_fields = argvdup;
+                                }
+                                
+                                if (!zstr(limit_type)) {
+                                        if (!strcasecmp(limit_type, &quot;hash&quot;)) {
+                                                profile-&gt;limit_type = &quot;hash&quot;;
+                                        } else {
+                                                profile-&gt;limit_type = &quot;sql&quot;;
+                                        }
+                                } else {
+                                        profile-&gt;limit_type = &quot;sql&quot;;
+                                }
+                                
</ins><span class="cx">                                 switch_core_hash_insert(globals.profile_hash, profile-&gt;name, profile);
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;Loaded lcr profile %s.\n&quot;, profile-&gt;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, &quot;Removing INVALID Profile %s.\n&quot;, profile-&gt;name);
</span><span class="cx">                                         switch_core_hash_delete(globals.profile_hash, profile-&gt;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, &quot;No lcr profiles defined.\n&quot;);
</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, &quot;Setting system defined default profile.&quot;);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-  done:
</del><ins>+done:
</ins><span class="cx">         switch_cache_db_release_db_handle(&amp;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-&gt;destination_number);
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Entering lcr endpoint for %s\n&quot;, dest);
+        
+        if (!dest) {
+                goto done;
+        }
+        
+        switch_core_new_memory_pool(&amp;pool);
+        routes.pool = pool;
+
+        if (var_event &amp;&amp; (skip = switch_event_get_header(var_event, &quot;lcr_recurse_variables&quot;)) &amp;&amp; switch_false(skip)) {
+                if ((var = switch_event_get_header(var_event, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, &quot;leg_timeout&quot;))) {
+                        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, &quot;leg_timeout&quot;))) {
+                        timelimit = atoi(var);
+                }
+                routes.session = session;
+                caller_profile = switch_channel_get_caller_profile(channel);
+                intrastate = switch_channel_get_variable(channel, &quot;intrastate&quot;);
+                intralata = switch_channel_get_variable(channel, &quot;intralata&quot;);
+                cid_name_override = switch_channel_get_variable(channel, &quot;origination_caller_id_name&quot;);
+                cid_num_override = switch_channel_get_variable(channel, &quot;origination_caller_id_number&quot;);
+                if (zstr(cid_name_override)) {
+                        cid_name_override = switch_channel_get_variable(channel, &quot;effective_caller_id_name&quot;);
+                }
+                if (zstr(cid_num_override)) {
+                        cid_num_override = switch_channel_get_variable(channel, &quot;effective_caller_id_number&quot;);
+                }
+                if ((var = switch_channel_get_variable(channel, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, &quot;leg_timeout&quot;))) {
+                        timelimit = atoi(var);
+                }
+        } else if (var_event) {
+                char *session_uuid = switch_event_get_header(var_event, &quot;ent_originate_aleg_uuid&quot;);
+                if (session_uuid) {
+                        mysession = switch_core_session_locate(session_uuid);
+                }
+                cid_name_override = switch_event_get_header(var_event, &quot;origination_caller_id_name&quot;);
+                cid_num_override = switch_event_get_header(var_event, &quot;origination_caller_id_number&quot;);
+                if (zstr(cid_name_override)) {
+                        cid_name_override = switch_event_get_header(var_event, &quot;effective_caller_id_name&quot;);
+                }
+                if (zstr(cid_num_override)) {
+                        cid_num_override = switch_event_get_header(var_event, &quot;caller_id_number&quot;);
+                }
+                if ((var = switch_event_get_header(var_event, SWITCH_CALL_TIMEOUT_VARIABLE)) || (var = switch_event_get_header(var_event, &quot;leg_timeout&quot;))) {
+                        timelimit = atoi(var);
+                }
+                
+                intrastate = switch_event_get_header(var_event, &quot;intrastate&quot;);
+                intralata = switch_event_get_header(var_event, &quot;intralata&quot;);
+                //switch_event_dup(&amp;event, var_event);
+                routes.event = var_event;
+        } else {
+                switch_event_create(&amp;event, SWITCH_EVENT_MESSAGE);
+                routes.event = event;
+        }
+        routes.lookup_number = dest;
+        routes.cid = (char *) cid_num_override;
+        
+        if ((flags &amp; SOF_FORKED_DIAL)) {
+                myflags |= SOF_NOBLOCK;
+        }
+
+        if (!(routes.profile = locate_profile(lcr_profile))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Unknown profile: %s\n&quot;, lcr_profile);
+                goto done;
+        }
+
+        if (!zstr(intralata) &amp;&amp; !strcasecmp((char *)intralata, &quot;true&quot;)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Select routes based on intralata rates\n&quot;);
+                routes.intralata = SWITCH_FALSE;
+        } else if (!zstr(intrastate) &amp;&amp; !strcasecmp((char *)intrastate, &quot;true&quot;)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Select routes based on intrastate rates\n&quot;);
+                routes.intrastate = SWITCH_TRUE;
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Select routes based on interstate rates\n&quot;);
+                routes.intrastate = SWITCH_FALSE;
+        }
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;LCR Lookup on %s\n&quot;, dest);
+
+        if (lcr_do_lookup(&amp;routes) == SWITCH_STATUS_SUCCESS) {
+                if (channel) {
+                        if (zstr(switch_channel_get_variable(channel, &quot;import&quot;))) {
+                                switch_channel_set_variable(channel, &quot;import&quot;, &quot;lcr_carrier,lcr_rate&quot;);
+                        } else {
+                                const char *tmp = switch_channel_get_variable(channel, &quot;import&quot;);
+                                if (!strstr(tmp, &quot;lcr_carrier,lcr_rate&quot;)) {
+                                        switch_channel_set_variable_printf(channel, &quot;import&quot;, &quot;%s,lcr_carrier,lcr_rate&quot;, tmp);
+                                }
+                        }
+                }
+
+                for (cur_route = routes.head; cur_route; cur_route = cur_route-&gt;next) {
+                        switch_bool_t pop_limit = SWITCH_FALSE;
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Trying route: %s\n&quot;, cur_route-&gt;dialstring);
+                        if (mysession &amp;&amp; cur_route-&gt;limit_realm &amp;&amp; cur_route-&gt;limit_id) {
+                                if (switch_limit_incr(routes.profile-&gt;limit_type, mysession, cur_route-&gt;limit_realm, cur_route-&gt;limit_id, cur_route-&gt;limit_max, 0) == SWITCH_STATUS_SUCCESS) {
+                                        pop_limit = SWITCH_TRUE;
+                                } else {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Limit exceeded for route %s, session: %s\n&quot;, cur_route-&gt;dialstring, mysession ? &quot;true&quot; : &quot;false&quot;);
+                                        continue;
+                                }
+                        }
+                        if (switch_ivr_originate(session, new_session, &amp;cause, cur_route-&gt;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, &quot;lcr_context&quot;))) {
+                                        if ((cp = switch_channel_get_caller_profile(new_channel))) {
+                                                cp-&gt;context = switch_core_strdup(cp-&gt;pool, context);
+                                        }
+                                }
+                                switch_core_session_rwunlock(*new_session);
+                                break;
+                        }
+                        
+                        /* did not connect, release limit */
+                        if (pop_limit) {
+                                switch_limit_release(routes.profile-&gt;limit_type, mysession, cur_route-&gt;limit_realm, cur_route-&gt;limit_id);
+                        }
+                        if (cause == SWITCH_CAUSE_LOSE_RACE || cause == SWITCH_CAUSE_ORIGINATOR_CANCEL) {
+                                break;
+                        }
+                }
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;LCR lookup failed for %s\n&quot;, caller_profile-&gt;destination_number);
+        }
+
+  done:
+        if (event) {
+                switch_event_destroy(&amp;event);
+        }
+        if (mysession) {
+                switch_core_session_rwunlock(mysession);
+        }
+        switch_core_destroy_memory_pool(&amp;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, &quot;intralata&quot;);
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;intrastate channel var is [%s]\n&quot;, intrastate);
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;intralata channel var is [%s]\n&quot;, intralata);
</span><del>-        if (!zstr(intralata) &amp;&amp; !strcasecmp((char *) intralata, &quot;true&quot;)) {
</del><ins>+        if (!zstr(intralata) &amp;&amp; !strcasecmp((char *)intralata, &quot;true&quot;)) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;Select routes based on intralata rates\n&quot;);
</span><span class="cx">                 routes.intralata = SWITCH_FALSE;
</span><del>-        } else if (!zstr(intrastate) &amp;&amp; !strcasecmp((char *) intrastate, &quot;true&quot;)) {
</del><ins>+        } else if (!zstr(intrastate) &amp;&amp; !strcasecmp((char *)intrastate, &quot;true&quot;)) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;Select routes based on intrastate rates\n&quot;);
</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, &quot;Select routes based on interstate rates\n&quot;);
</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, &quot;true&quot;);
</span><span class="cx">                 switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, &quot;true&quot;);
</span><del>-                switch_channel_set_variable(channel, &quot;import&quot;, &quot;lcr_carrier,lcr_rate&quot;);
</del><ins>+                if (zstr(switch_channel_get_variable(channel, &quot;import&quot;))) {
+                        switch_channel_set_variable(channel, &quot;import&quot;, &quot;lcr_carrier,lcr_rate&quot;);
+                } else {
+                        const char *tmp = switch_channel_get_variable(channel, &quot;import&quot;);
+                        if (!strstr(tmp, &quot;lcr_carrier,lcr_rate&quot;)) {
+                                switch_channel_set_variable_printf(channel, &quot;import&quot;, &quot;%s,lcr_carrier,lcr_rate&quot;, tmp);
+                        }
+                }
</ins><span class="cx"> 
</span><span class="cx">                 for (cur_route = routes.head; cur_route; cur_route = cur_route-&gt;next) {
</span><del>-                        switch_caller_extension_add_application(session, extension, &quot;bridge&quot;, cur_route-&gt;dialstring);
</del><ins>+                        char *app = NULL;
+                        char *argc = NULL;
+                        if (cur_route-&gt;limit_realm &amp;&amp; cur_route-&gt;limit_id) {
+                                app = &quot;limit_execute&quot;;
+                                argc = switch_core_sprintf(pool, &quot;%s %s %s %d bridge %s&quot;,
+                                                                                        routes.profile-&gt;limit_type,
+                                                                                        cur_route-&gt;limit_realm,
+                                                                                        cur_route-&gt;limit_id,
+                                                                                        cur_route-&gt;limit_max,
+                                                                                        cur_route-&gt;dialstring);
+                        } else {
+                                app = &quot;bridge&quot;;
+                                argc = cur_route-&gt;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, &quot;LCR lookup failed for %s\n&quot;, caller_profile-&gt;destination_number);
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-  end:
</del><ins>+end:
+        if (event) {
+                switch_event_destroy(&amp;event);
+        }
</ins><span class="cx">         if (!session) {
</span><span class="cx">                 switch_core_destroy_memory_pool(&amp;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, &quot;repeating %d of '%s'\n&quot;, (int)how_many, what); */
-
-        for (i = 0; i &lt; how_many; i++) {
</del><ins>+        /*switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;repeating %d of '%s'\n&quot;, (int)how_many, what);*/
+        
+        for (i=0; i&lt;how_many; i++) {
</ins><span class="cx">                 str_stream-&gt;write_function(str_stream, &quot;%s&quot;, 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, &quot;intrastate&quot;);
</span><del>-        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;intrastate channel var is [%s]\n&quot;, zstr(intra) ? &quot;undef&quot; : intra);
-        if (zstr(intra) || strcasecmp((char *) intra, &quot;true&quot;)) {
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;intrastate channel var is [%s]\n&quot;, 
+                                        zstr(intra) ? &quot;undef&quot; : intra);
+        if (zstr(intra) || strcasecmp((char *)intra, &quot;true&quot;)) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;Select routes based on interstate rates\n&quot;);
</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 &gt; 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, &quot;LCR Lookup on %s using profile %s\n&quot;, 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, &quot;Unknown profile: %s\n&quot;, 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-&gt;carrier_name);
</span><span class="cx">                                 switch_snprintf(vbuf, sizeof(vbuf), &quot;lcr_codec_%d&quot;, cnt);
</span><span class="cx">                                 switch_channel_set_variable(channel, vbuf, cur_route-&gt;codec);
</span><del>-                                cnt++;
-                                switch_snprintf(rbp, rbl, &quot;%s|&quot;, cur_route-&gt;dialstring);
</del><ins>+                                switch_snprintf(vbuf, sizeof(vbuf), &quot;lcr_limit_%d&quot;, cnt);
+                                switch_channel_set_variable_printf(channel, vbuf, &quot;%s %s %d&quot;, cur_route-&gt;limit_realm, cur_route-&gt;limit_id, cur_route-&gt;limit_max);
+                                l = switch_snprintf(rbp, rbl, &quot;%s|&quot;, cur_route-&gt;dialstring);
</ins><span class="cx">                                 last_delim = end_of_p(rbp);
</span><del>-                                l = strlen(cur_route-&gt;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), &quot;%d&quot;, cnt - 1);
</span><span class="cx">                         switch_channel_set_variable(channel, &quot;lcr_route_count&quot;, vbuf);
</span><span class="cx">                         *(rbuf + strlen(rbuf) - 1) = '\0';
</span><span class="cx">                         switch_channel_set_variable(channel, &quot;lcr_auto_route&quot;, rbuf);
</span><del>-                        switch_channel_set_variable(channel, &quot;import&quot;, &quot;lcr_carrier,lcr_rate&quot;);
</del><ins>+                        if (zstr(switch_channel_get_variable(channel, &quot;import&quot;))) {
+                                switch_channel_set_variable(channel, &quot;import&quot;, &quot;lcr_carrier,lcr_rate&quot;);
+                        } else {
+                                const char *tmp = switch_channel_get_variable(channel, &quot;import&quot;);
+                                if (!strstr(tmp, &quot;lcr_carrier,lcr_rate&quot;)) {
+                                        switch_channel_set_variable_printf(channel, &quot;import&quot;, &quot;%s,lcr_carrier,lcr_rate&quot;, tmp);
+                                }
+                        }
</ins><span class="cx">                 } else {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, &quot;LCR lookup failed for %s\n&quot;, dest);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><del>-
-  end:
</del><ins>+        
+end:
+        if (routes.event) {
+                switch_event_destroy(&amp;event);
+        }
</ins><span class="cx">         if (!session) {
</span><span class="cx">                 switch_core_destroy_memory_pool(&amp;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 = &quot;&quot;;
+        }
</ins><span class="cx">         if (as_xml) {
</span><del>-                str_repeat(indent * 2, &quot; &quot;, stream);
</del><ins>+                str_repeat(indent*2, &quot; &quot;, stream);
</ins><span class="cx">                 stream-&gt;write_function(stream, &quot;&lt;%s&gt;%s&lt;/%s&gt;\n&quot;, key, data, key);
</span><span class="cx">         } else {
</span><span class="cx">                 stream-&gt;write_function(stream, &quot; | %s&quot;, 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, &quot;data passed to lcr is [%s]\n&quot;, cmd);
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG
+                                          , &quot;data passed to lcr is [%s]\n&quot;, 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 &gt; 2) {
</span><span class="cx">                         int i;
</span><del>-                        for (i = 2; i &lt; argc; i++) {
</del><ins>+                        for (i=2; i&lt;argc; i++) {
</ins><span class="cx">                                 if (!strcasecmp(argv[i], &quot;intrastate&quot;)) {
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;Select routes based on intrastate rates\n&quot;);
</span><span class="cx">                                         cb_struct.intrastate = SWITCH_TRUE;
</span><span class="cx">                                 } else if (!strcasecmp(argv[i], &quot;as&quot;)) {
</span><span class="cx">                                         i++;
</span><span class="cx">                                         if (argv[i] &amp;&amp; !strcasecmp(argv[i], &quot;xml&quot;)) {
</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, &quot;Set Caller ID to [%s]\n&quot;, 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 = &quot;18005551212&quot;;
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, &quot;Using default CID [%s]\n&quot;, cb_struct.cid);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING
+                                                          , &quot;Using default CID [%s]\n&quot;, 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, &quot; &quot;, stream);
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><ins>+                                stream-&gt;write_function(stream, &quot; | %s&quot;, headers[LCR_HEADERS_LIMIT]);
+                                if ((len = (maximum_lengths.limit - strlen(headers[LCR_HEADERS_LIMIT]))) &gt; 0) {
+                                        str_repeat(len, &quot; &quot;, stream);
+                                }
+                                
</ins><span class="cx">                                 stream-&gt;write_function(stream, &quot; | %s&quot;, headers[LCR_HEADERS_DIALSTRING]);
</span><span class="cx">                                 if ((len = (maximum_lengths.dialstring - strlen(headers[LCR_HEADERS_DIALSTRING]))) &gt; 0) {
</span><span class="cx">                                         str_repeat(len, &quot; &quot;, stream);
</span><span class="lines">@@ -1405,7 +1722,7 @@
</span><span class="cx">                                 if (as_xml) {
</span><span class="cx">                                         stream-&gt;write_function(stream, &quot;  &lt;row id=\&quot;%d\&quot;&gt;\n&quot;, rowcount);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 write_data(stream, as_xml, &quot;prefix&quot;, current-&gt;digit_str, 2, maximum_lengths.digit_str);
</span><span class="cx">                                 write_data(stream, as_xml, &quot;carrier_name&quot;, current-&gt;carrier_name, 2, maximum_lengths.carrier_name);
</span><span class="cx">                                 write_data(stream, as_xml, &quot;rate&quot;, current-&gt;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, &quot;cid&quot;, &quot;&quot;, 2, maximum_lengths.cid);
</span><span class="cx">                                 }
</span><del>-
</del><ins>+                                
+                                if (current-&gt;limit_realm &amp;&amp; current-&gt;limit_id) {
+                                        char *str = NULL;
+                                        str = switch_core_sprintf(pool, &quot;%s %s %d&quot;, current-&gt;limit_realm, current-&gt;limit_id, current-&gt;limit_max);
+                                        
+                                        write_data(stream, as_xml, &quot;limit&quot;, str, 2, maximum_lengths.limit);
+                                } else {
+                                        write_data(stream, as_xml, &quot;limit&quot;, &quot;&quot;, 2, maximum_lengths.limit);
+                                }
+                                
</ins><span class="cx">                                 write_data(stream, as_xml, &quot;dialstring&quot;, current-&gt;dialstring, 2, maximum_lengths.dialstring);
</span><del>-
</del><span class="cx">                                 if (as_xml) {
</span><ins>+                                        event_xml = switch_event_xmlize(current-&gt;fields, NULL);
+                                        event_str = switch_xml_toxml(event_xml, SWITCH_FALSE);
+                                        stream-&gt;write_function(stream, event_str);
+                                        switch_xml_free(event_xml);
+                                        switch_safe_free(event_str);
+                                }
+                                
+                                if (as_xml) {
</ins><span class="cx">                                         stream-&gt;write_function(stream, &quot;  &lt;/row&gt;\n&quot;);
</span><span class="cx">                                 } else {
</span><span class="cx">                                         stream-&gt;write_function(stream, &quot; |\n&quot;);
</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(&amp;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-&gt;write_function(stream, &quot;USAGE: %s\n&quot;, 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, &amp;val);
</span><span class="cx">                                 profile = (profile_t *) val;
</span><del>-
</del><ins>+                                
</ins><span class="cx">                                 stream-&gt;write_function(stream, &quot;Name:\t\t%s\n&quot;, profile-&gt;name);
</span><span class="cx">                                 if (zstr(profile-&gt;custom_sql)) {
</span><span class="cx">                                         stream-&gt;write_function(stream, &quot; ID:\t\t%d\n&quot;, profile-&gt;id);
</span><span class="lines">@@ -1495,9 +1828,15 @@
</span><span class="cx">                                 stream-&gt;write_function(stream, &quot; has intrastate:\t%s\n&quot;, profile-&gt;profile_has_intrastate ? &quot;true&quot; : &quot;false&quot;);
</span><span class="cx">                                 stream-&gt;write_function(stream, &quot; has intralata:\t%s\n&quot;, profile-&gt;profile_has_intralata ? &quot;true&quot; : &quot;false&quot;);
</span><span class="cx">                                 stream-&gt;write_function(stream, &quot; has npanxx:\t%s\n&quot;, profile-&gt;profile_has_npanxx ? &quot;true&quot; : &quot;false&quot;);
</span><del>-                                stream-&gt;write_function(stream, &quot; Reorder rate:\t%s\n&quot;, profile-&gt;reorder_by_rate ? &quot;enabled&quot; : &quot;disabled&quot;);
-                                stream-&gt;write_function(stream, &quot; Info in headers:\t%s\n&quot;, profile-&gt;info_in_headers ? &quot;enabled&quot; : &quot;disabled&quot;);
-                                stream-&gt;write_function(stream, &quot; Quote IN() List:\t%s\n&quot;, profile-&gt;quote_in_list ? &quot;enabled&quot; : &quot;disabled&quot;);
</del><ins>+                                stream-&gt;write_function(stream, &quot; Reorder rate:\t%s\n&quot;, 
+                                                                                profile-&gt;reorder_by_rate ? &quot;enabled&quot; : &quot;disabled&quot;);
+                                stream-&gt;write_function(stream, &quot; Info in headers:\t%s\n&quot;, 
+                                                                                profile-&gt;info_in_headers ? &quot;enabled&quot; : &quot;disabled&quot;);
+                                stream-&gt;write_function(stream, &quot; Quote IN() List:\t%s\n&quot;, 
+                                                                                profile-&gt;quote_in_list ? &quot;enabled&quot; : &quot;disabled&quot;);
+                                stream-&gt;write_function(stream, &quot; Import fields:\t%s\n&quot;, 
+                                        profile-&gt;export_fields_str ? profile-&gt;export_fields_str : &quot;(null)&quot;);
+                                stream-&gt;write_function(stream, &quot; Limit type:\t%s\n&quot;, profile-&gt;limit_type);
</ins><span class="cx">                                 stream-&gt;write_function(stream, &quot;\n&quot;);
</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-&gt;write_function(stream, &quot;-ERR %s\n&quot;, 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, &quot;&lt;number&gt;&quot;, SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC);
</span><span class="cx">         SWITCH_ADD_DIALPLAN(dp_interface, &quot;lcr&quot;, 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-&gt;interface_name = &quot;lcr&quot;;
+        lcr_endpoint_interface-&gt;io_routines = &amp;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>