<p></p>
<h3>Description</h3>
<p><code>dns_int_match_ip</code> is broken on 5.3.0 and above</p>
<h3>Troubleshooting</h3>
<h4>Reproduction</h4>
<p>Try this dialplan:</p>
<pre><code>if (dns_int_match_ip('dns.google', '8.8.8.8')) {
   xlog("L_INFO", "OK dns-ip match with dns_int_match_ip");
} else {
   xlog("L_ERR", "FAIL dns-ip match with dns_int_match_ip");
}

if (dns_int_match_ip('100.100.100.100', '8.8.8.8')) {
   xlog("L_ERR", "FAIL dns_int_match_ip on 2 ip's");
} else {
   xlog("L_INFO", "OK dns_int_match_ip on 2 ip's");
}
</code></pre>
<p>On 5.2 releases, both checks succeed. On 5.3 and up, both fail.</p>
<h4>Debugging Data</h4>
<p>I've traced down the problem to <code>str2ip</code>.</p>
<p>In 5.2, this is a <code>static inline</code> function defined in the header file <code>resolve.h</code>. Therefore, the returned <code>ip</code> structure is allocated <em>once per compilation unit</em> - ie. <code>ipops_mod.c</code> and <code>dns_cache.c</code> both have their own versions of the variable.</p>
<p>Since <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/kamailio/kamailio/commit/fb75e90549a2ed901d15842c6d621ad7d0850106/hovercard" href="https://github.com/kamailio/kamailio/commit/fb75e90549a2ed901d15842c6d621ad7d0850106"><tt>fb75e90</tt></a> (5.3 and up) this function is defined in it's own compilation unit (<code>resolve.c</code>) and therefore shared by all callers.</p>
<p>See: <a href="https://stackoverflow.com/questions/185624/static-variables-in-an-inlined-function" rel="nofollow">https://stackoverflow.com/questions/185624/static-variables-in-an-inlined-function</a></p>
<h4>Log Messages</h4>
<p>n/a</p>
<h4>SIP Traffic</h4>
<p>n/a</p>
<h3>Possible Solutions</h3>
<ul>
<li>Have <code>ki_dns_sys_match_ip</code> make a local copy of the return value of <code>str2ip</code></li>
<li>Move <code>str2ip</code> back to a header file</li>
<li>Refactor <code>str2ip</code> to take a pointer to an output struct</li>
</ul>
<h3>Additional Information</h3>
<p>In 5.3, this is what happens:</p>
<pre><code># - ki_dns_int_match_ip will call str2ip on 8.8.8.8, filling the static variable
# - resolvehost will call dns_a_get_he, which calls str2ip on dns.google.com, zeroing out the static variable
# - (dns_cache_do_request will call str2ip on dns.google.com, zeroing out the static variable again)
# - resolvehost queries DNS
# - the resolved IP's will be compared to the zero'd out struct
dns_int_match_ip('dns.google', '8.8.8.8') == false

# - ki_dns_int_match_ip will call str2ip on 8.8.8.8, filling the static variable
# - resolvehost will call dns_a_get_he, which calls str2ip on 100.100.100.100, overwriting the static variable
# - the static variable will be compared to itself
dns_int_match_ip('100.100.100.100', '8.8.8.8') == true
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/2645">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABO7UZPBBTYRMBIJEZXXWVDTALX4LANCNFSM4YBPV3SA">unsubscribe</a>.<img src="https://github.com/notifications/beacon/ABO7UZPB6AFTVS7SGVYGBVDTALX4LA5CNFSM4YBPV3SKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4MEEKR7Q.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/2645",
"url": "https://github.com/kamailio/kamailio/issues/2645",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>