Skip to content

Commit 345c753

Browse files
committed
added smartAAAALookup() for IPv6 smart lookup
1 parent e129b47 commit 345c753

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

src/PurplePixie/PhpDns/DNSQuery.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,77 @@ public function smartALookup(string $hostname, int $depth = 0): string
652652
return $this->smartALookup($newtarget, $depth + 1);
653653
}
654654

655+
/**
656+
* @param string $hostname
657+
* @param int $depth
658+
* @return string
659+
* @throws Exceptions\InvalidQueryTypeName
660+
*/
661+
public function smartAAAALookup(string $hostname, int $depth = 0): string
662+
{
663+
$this->debug('SmartAAAALookup for ' . $hostname . ' depth ' . $depth);
664+
665+
// avoid recursive lookups
666+
if ($depth > 5) {
667+
return '';
668+
}
669+
670+
// The SmartALookup function will resolve CNAMES using the additional properties if possible
671+
$answer = $this->query($hostname, DNSTypes::NAME_AAAA);
672+
673+
// failed totally
674+
if ($answer === false) {
675+
return '';
676+
}
677+
678+
// no records at all returned
679+
if (count($answer) === 0) {
680+
return '';
681+
}
682+
683+
foreach ($answer as $record) {
684+
// found it
685+
if ($record->getTypeid() == DNSTypes::ID_AAAA) {
686+
$best_answer = $record;
687+
break;
688+
}
689+
690+
// alias
691+
if ($record->getTypeid() == DNSTypes::ID_CNAME) {
692+
$best_answer = $record;
693+
// and keep going
694+
}
695+
}
696+
697+
if (!isset($best_answer)) {
698+
return '';
699+
}
700+
701+
if ($best_answer->getTypeid() == DNSTypes::ID_AAAA) {
702+
return $best_answer->getData();
703+
} // got an IP ok
704+
705+
if ($best_answer->getTypeid() != DNSTypes::ID_CNAME) {
706+
return '';
707+
} // shouldn't ever happen
708+
709+
$newtarget = $best_answer->getData(); // this is what we now need to resolve
710+
711+
// First is it in the additional section
712+
foreach ($this->lastadditional as $result) {
713+
if (
714+
$result->getDomain() == $hostname
715+
&& $result->getTypeid() == DNSTypes::ID_AAAA
716+
) {
717+
return $result->getData();
718+
}
719+
}
720+
721+
// Not in the results
722+
723+
return $this->smartALookup($newtarget, $depth + 1);
724+
}
725+
655726
public function getLastnameservers(): DNSAnswer
656727
{
657728
return $this->lastnameservers;

0 commit comments

Comments
 (0)