[SMF 1.1 RC2] Issues in function host_from_ip (Sources/Subs.php)

Started by nwsw, January 22, 2006, 03:51:40 PM

Previous topic - Next topic

nwsw

Function: host_from_ip

This function contains some questionable logic, although none of it actually prevents its overall function. However, I wanted to document the issues, in case it helps someone else.

The following code does not work as intended, and should be removed:


if (strpos($test, 'Non-existent domain') !== false)
    return $ip;


The nslookup program returns the 'Non-existent domain' string via Standard Error. So, the shell_exec program does not capture it, and it will never exist in the $test string.

In my case, I rewrote this section as follows:


$test = @shell_exec('nslookup -timeout=1 ' . @escapeshellarg($ip));

if (preg_match('~Name:\s+([^\s]+)~', $test, $match) == 1)
    $host = $match[1];
elseif (strpos($test, 'Server:') !== false) // Valid output was produced, but no hostname result
    $host = $ip; // allow the failed result to be cached


I also made these additional changes:


  • allowed the failed host and nslookup results to fall through to the end of the function, so that they could be cached
  • removed the "rand(0, 1) == 1" code from this function, as this only serves to randomize the behavior of this function, which I consider to be an undesirable quality

Advertisement: