i am trying to route all calls to twilio through kamailio proxy. with my config file, call gets connected and automatically drops after about 30 seconds. This is because ACK sent to twilio for 200 OK was not correct. Twilio expects ACK with ruri same as contact in 200 OK response, but kamailio sent was different. How to fix this error?
200 OK response, ACK forwarded and kamailio config file are postes in paste bin , please check below link.(its not easy to paste code here)
now i am trying to change sip ruri by storing contact from htable for 200 and forward ack . But its not working, i cant store contact from 200 to htable and use in ack.Please see the code from config file ,by which i am tying.
if(status=="200")
{
$var(x) = $ct;
$var(c) = $(var(x){nameaddr.uri});
$sht(b=>ru)=$var(c);
exit;
}
if ( is_method("ACK") && $si=="64.2.142.90") {
$du = "sip:xxxxxxx.sip.twilio.com";
$ru=$sht(b=>ru);
forward();
exit;
}
For a proper analysis, the incoming ACK to Kamailio would be needed along with the one sent out by Kamailio.
I can try to guess what happens there. Looking at 200ok, there are 3 Record-Route headers, that means there are 3 SIP proxies routing this call. I assume Kamailio is on 107.21.211.20:5060 (this IP is not in Via stack of 200ok reply, making me think that the 200ok you pasted is the one from Kamailio sent back towards caller).
Then, the 2nd (54.69.159.69:5060) and 3rd (64.2.142.90) Record-Route headers in 200ok use lr=on parameter, which is quite specific for SER-like proxy (SER, older Kamailio (including OpenSER) or other variants).
I conclude that between caller and your Kamailio are two other such proxies. Very likely, the 2nd (54.69.159.69:5060) has NAT traversal routing logic. But it doesn't do it right. It looks at Contact header in 200ok and sees an private IP, concluding the callee is behind NAT and replacing Contact with source IP and port of 200ok, which are the ones of your Kamailio.
To confirm this, you would need to see the 200ok received by caller -- see the Contact header there. If this is confirmed, the solutions are:
1) get in contact with the owner of the 2nd proxy and tell him to do proper NAT traversal processing. If it is a proxy after another proxy, it should not do anything on that side of the SIP traffic. If it using Kamailio there, it must switch from using fix_nated_contact() to using set_contact_alias()/handle_contact_alias() for dealing with natted contacts and r-uri towards natted destinations (example is in default config file for latest kamailio versions like 4.2.x or 4.1.x)
2) if 1) is not a solution, then you have to fix it in your kamailio, that can be done by using set_contact_alias()/handle_contact_alias(). It appears that your kamailio is first hop next to twilio UA behind nat, so you should do it anyhow.
Note that solution 2) might not work if twilio UA is very restrictive on incoming r-uri -- the 2nd proxy processing will lose the ip of twilio UA. If it is the case, then you have to do additional tricks to store the original contact from 200ok sent by twilio. In can be done either via extra parameter in contact uri or using htable and store on kamailio memory, then use it when you receive the ACK (same will be needed for re-INVITE or BYE).