{"id":128,"date":"2017-03-28T14:58:16","date_gmt":"2017-03-28T14:58:16","guid":{"rendered":"https:\/\/patryk.rzski.com\/?p=128"},"modified":"2017-03-28T19:05:53","modified_gmt":"2017-03-28T19:05:53","slug":"improving-the-voip-foreign-number-solution","status":"publish","type":"post","link":"https:\/\/patryk.rzski.com\/?p=128","title":{"rendered":"Improving the VoIP foreign number solution"},"content":{"rendered":"<p>It has been a while and I&#8217;ve noticed a few issues in my solution (more info <a href=\"https:\/\/patryk.rzski.com\/?p=88\">here<\/a>) allowing &#8220;assigning&#8221; a foreign number to your smart phone. Since solving them was a nice little achievement, I&#8217;ll share the description here. It was quite an investigation, too!<\/p>\n<p>The first problem was that the connection keeps timing out from time to time. Not always, but I couldn&#8217;t determine a pattern (ah, I love these situations!). In such cases there&#8217;s no other choice but to get your hands dirty&#8230; and in this case it means some deep packet inspection. Asterisk makes this task fairly easy, so tcpdump was not required. Using <em>sip set debug ip\/peer on\/off<\/em> allows finding how my asterisk and the SIP client on my mobile are talking. That&#8217;s possible even if you don&#8217;t have a good understanding of the session initiation protocol &#8211; simply googling it&#8217;s workflow is enough to see how it should look. Then all you have to do is comparing the expected flow with what you actually get.<\/p>\n<p>Because of my intermittent issue, I had two sets of SIP debug data &#8211; when it works, and when it doesn&#8217;t. Comparing the two showed me that when the timeout happens, it is actually the phone that stops responding to SIP INVITEs. Having tried a few other SIP clients on the phone, I had a strong feeling this was not going to be a client issue. So maybe the network? Bingo! As soon as I switched off from my wifi and landed on 4G, the connection worked flawlessly and never failed. As this is weird, I went on to research why that migth happen and found some content explaining how most home routers have a faulty implementation of application layer gateway for SIP. Indeed, it was SIP ALG messing my SIP traffic in failed attempts to &#8220;secure my traffic&#8221; by inspecting source\/destination addresses in the SIP packets. In my case, it would filter out my SIP traffic as soon as the router &#8220;forgets&#8221; the addresses and mappings, which in my case was 30 seconds from registration (TCP handshake from the mobile to my VPS).<\/p>\n<p>Considering the minimum frequency at which SIP clients can re-register is once every 60 seconds and that my router would time out after about 30s, I was only left with half of the register time working. That&#8217;s way below my expectations. I can&#8217;t reconfigure my home router (thanks to the ISP), so what do? There&#8217;s no way of setting the registry time lower in the client itself without dirty hacks. I ended up doign the following: switch from SIP to IAX2 (Asterisk&#8217;s preferred protocol for VoIP), and then I wrote a small patch to my ebuild for Asterisk on Gentoo to define the re-register frequency as 25s, using the following:<\/p>\n<p><code><br \/>\n$ grep sed \/usr\/portage\/net-misc\/asterisk\/asterisk-11.25.1.ebuild<br \/>\nsed -i 's:EXPIRE.*60:EXPIRE 25:' \"${S}\"\/channels\/iax2.h && ewarn patched IAX2 registry timeout<br \/>\n<\/code><\/p>\n<p>After re-compiling (making such tweaks is very easy thanks to Gentoo) and restarting Asterisk, even though my client kept asking for 60 second sessions, Asterisk would &#8220;demand&#8221; a new session (from which all it gets is updating the actual IP address of the smart phone) every 25 seconds anyway. And the timeout problem is almost gone!<\/p>\n<p>Almost, because Asterisk has a security setting called &#8220;nat&#8221;. It has to be set to &#8220;no&#8221; regardless if it is SIP or IAX2, which means that when the agents register, Asterisk doesn&#8217;t inspect the IP addresses in the header &#8211; and those do fail since NAT would translate the IP address of my phone from local LAN to the public IP address of my router. Since I control access to my Asterisk using multiple factors and layers (iptables and user\/pass), I consider it safe for disabling.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It has been a while and I&#8217;ve noticed a few issues in my solution (more info here) allowing &#8220;assigning&#8221; a foreign number to your smart phone. Since solving them was a nice little achievement, I&#8217;ll share the description here. It was quite an investigation, too! The first problem was that the connection keeps timing out [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts\/128"}],"collection":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":3,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/patryk.rzski.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}