function bistro_canonical_host_id_uncached()

in web_ui/src/applications/bistro/utils.php [117:147]


function bistro_canonical_host_id_uncached($host) {
  $dns_recs = dns_get_record($host);
  if ($dns_recs) {
    // Find the lexicographically largest (in binary) IP of each type.
    //
    // ASIDE: Could it be a better / more legible strategy to get the
    // lexicographically largest 'host' entry from the DNS records?
    $ipv6 = null;
    $ipv4 = null;
    foreach ($dns_recs as $dns_rec) {
      if (idx($dns_rec, 'type') === 'A' && isset($dns_rec['ip'])) {
        $binary_ip = inet_pton($dns_rec['ip']);
        if ($ipv4 === null || $binary_ip > $ipv4) {
          $ipv4 = $binary_ip;
        }
      } else if (idx($dns_rec, 'type') === 'AAAA' && isset($dns_rec['ipv6'])) {
        $binary_ip = inet_pton($dns_rec['ipv6']);
        if ($ipv6 === null || $binary_ip > $ipv6) {
          $ipv6 = $binary_ip;
        }
      }
    }
    if ($ipv6 !== null) {  // IPv6 is canonical if it's available
      return 'ipv6:'.$ipv6;
    }
    if ($ipv4 !== null) {
      return 'ipv4:'.$ipv4;
    }
  }
  return 'unresolved:'.$host;
}