Example #1
0
 public function get($hostname, $cb)
 {
     if (!$this->preloading->hasCompleted()) {
         $pool = $this;
         $this->preloading->addListener(function ($job) use($hostname, $cb, $noncache, $pool) {
             $pool->get($hostname, $cb, $noncache);
         });
         return;
     }
     $conn = $this->getConnectionByKey($hostname);
     if (!$conn) {
         call_user_func($cb, false);
         return false;
     }
     $conn->onResponse->push($cb);
     $conn->setFree(false);
     $e = explode(':', $hostname, 3);
     $hostname = $e[0];
     $qtype = isset($e[1]) ? $e[1] : 'A';
     $qclass = isset($e[2]) ? $e[2] : 'IN';
     $QD = array();
     $qtypeInt = array_search($qtype, DNSClient::$type, true);
     $qclassInt = array_search($qclass, DNSClient::$class, true);
     if ($qtypeInt === false || $qclassInt === false) {
         call_user_func($cb, false);
         return;
     }
     $q = Binary::labels($hostname) . Binary::word($qtypeInt) . Binary::word($qclassInt);
     $QD[] = $q;
     $packet = Binary::word(++$conn->seq) . Binary::bitmap2bytes('0' . '0000' . '0' . '0' . '1' . '0' . '000' . '0000', 2) . Binary::word(sizeof($QD)) . Binary::word(0) . Binary::word(0) . Binary::word(0) . implode('', $QD);
     $conn->write(Binary::word(strlen($packet)) . $packet);
 }