The Asterisk server is running on an AWS EC2 instance (and is not behind a NAT). It is communicating with a Kamailio Server which is in the same local network as the Asterisk.
The issue which I am observing is that whenever Asterisk is sending any SIP request like OPTIONS or INVITE to Kamailio, it is setting its public IP in the contact header instead of the local IP. Due to this, for any REINVITE in the call, Kamailio will try to send the packets to the Public IP of Asterisk which is unnecessary. The relevant part of my sip.conf is provided below:
On what basis does Asterisk decide whether to use Public IP or Local IP in the contact header?
In this case it makes logical sense for Asterisk to use local IP in the contact header right - since the kamailio machine is in the same local network?
How can I fix this so that the contact header has the local IP? Also in general how can I configure/ override the contact header in Asterisk?
Asterisk 1.8 hasn’t been supported for 5 and half years for general bugs and 4 and half years for security issues! It is possible the the logic has changed. Also chan_sip is deprecated in favour of chan_pjsip, and it is possible that varies.
Although I’m not sure whether it actually makes a difference, localnet should be 10.x.x.0/255.255.255.0.
Cases where nat=no is appropriate are very rare. I believe it disables the use of the rport flag. It does not affect whether public or private address is used.
Without knowing the network parts of the addresses of Asterisk and the proxy, it isn’t possible to say whether or not Asterisk should treat this a as NATted. However, I believe that the network of the interface used to send the request is unioned with any networks listed in localnet, and if the address in the request URI doesn’t fall in one of those networks, the public address is used.
I’m pretty sure the request URI, rather than the first hop address, is used, as Contact is end to end. However, NAT has been bolted onto SIP and I’m not sure that there is any policy contemporary with Asterisk 1.8 on how proxies should handle Contact addresses when they are located at a NAT boundary. However, being certain about how any version of the code handles this will require trawling through significant amounts of code. 1.8.32.3 is the latest 1.8 version and is actually over five years old.
I’d suggest the place to override it is in the proxy. I’m pretty sure that 1.8 has no mechanism to override this. I’m not sufficiently familiar with chan_pjsip to answer for that.