I am attempting to put together code which will allow us to redirect a call to a different tn if the route failed.
 
Example:
User dials 11235551234
Kamailio uses LCR to route to 192.168.0.100 (404 error)
Change $rU to 11235550000 in failure_route
 
The only way I can get $rU to show up in the SIP message is to call append_branch() which is not a solution because I now end up with two SIP messages one that is correct and one that is incorrect.
 
Here is the failure_route I am using.  Any input would be appreciated.
 
failure_route[ECRC] {
 if (t_is_canceled()) {
  xlog("L_INFO", "Call has been canceled in 'ECRC'.");
  exit;
 }
 
 xlog("L_INFO", "SIP Response Code: $T_reply_code");
 
 # We should fail over if we received one of these response codes.
 if (!t_check_status("[45][0-9][0-9]")) {
  xlog("L_INFO", "Not a failover error.");
  exit;
 }
 
 # Translate the dialed number into the ECRC DID.
 dp_translate("1", "$rU/$rU");
 
 # Load the gateways for normal dialing.
 xlog("L_INFO", "Trying to find LCR route for $rU");
 if (! load_gws("1", "$rU")) {
  xlog("Unable to load routing");
  t_reply("503", "Unable to load routing");
  exit;
 }
 
 xlog("L_INFO", "Trying gateway '$avp(i:709)'");
 if (!next_gw()) {
  xlog("L_INFO", "No LCR route for $rU");
  t_reply("404", "No available gateways");
  exit;
 }
 
 $rd=$dd;
 $rp=$dp;
 $du=$ru;
 xlog("L_INFO", "ru: $ru");
 xlog("L_INFO", "du: $du");
 #at this point $ru and $du contain the correct uri.
 append_branch();

 # Send out the request.
 xlog("L_INFO", "Now we're sending the request out.");
 if (!t_relay()) {
  xlog("L_INFO" "Relay failed!");
  t_reply("502", "Bad Gateway");
  exit;
 }
}
 
Now I have two SIP messages one containing the correct phone number:
        INVITE sip:11235550000@xxx.xxx.xxx.xxx SIP/2.0
        Record-Route: <sip:xxx.xxx.xxx.xxx;lr=on>
        Via: SIP/2.0/UDP xxx.xxx.xxx.xxx;branch=z9hG4bK2e2f.4145be65.2
        Via: SIP/2.0/UDP xxx.xxx.xxx.xxx:5060;branch=z9hG4bK28aa7440;rport=5060
        Max-Forwards: 69
        From: "Steven Wheeler" <sip:6124243265@xxx.xxx.xxx.xxx>;tag=as30b3d6f5
        To: <sip:11235551234@xxx.xxx.xxx.xxx:5060>
        ...
And one containing the wrong phone number:
        INVITE sip:11235551234@xxx.xxx.xxx.xxx SIP/2.0
        Record-Route: <sip:xxx.xxx.xxx.xxx;lr=on>
        Via: SIP/2.0/UDP xxx.xxx.xxx.xxx;branch=z9hG4bK2e2f.4145be65.3
        Via: SIP/2.0/UDP xxx.xxx.xxx.xxx:5060;branch=z9hG4bK28aa7440;rport=5060
        Max-Forwards: 69
        From: "Steven Wheeler" <sip:6124243265@xxx.xxx.xxx.xxx>;tag=as30b3d6f5
        To: <sip:11235551234@xxx.xxx.xxx.xxx:5060>
        ...
 
Steven Wheeler
swheeler@usinternet.com
952-253-3252