Esempio n. 1
0
 /**
  * Called when new data received
  * @param string New data
  * @return void
  */
 public function stdin($buf)
 {
     $this->buf .= $buf;
     $length = Binary::bytes2int(binarySubstr($this->buf, 0, 2));
     if ($length > strlen($this->buf) + 2) {
         return;
         // not enough data yet
     }
     $packet = binarySubstr($this->buf, 2, $length);
     $this->buf = binarySubstr($this->buf, $length + 2);
     $id = Binary::getWord($packet);
     $bitmap = Binary::getBitmap(Binary::getByte($packet)) . Binary::getBitmap(Binary::getByte($packet));
     $qr = (int) $bitmap[0];
     $this->response['opcode'] = bindec(substr($bitmap, 1, 4));
     $this->response['aa'] = (int) $bitmap[5];
     $tc = (int) $bitmap[6];
     $rd = (int) $bitmap[7];
     $ra = (int) $bitmap[8];
     $z = bindec(substr($bitmap, 9, 3));
     $this->response['qdcount'] = Binary::getWord($packet);
     $this->response['ancount'] = Binary::getWord($packet);
     $this->response['nscount'] = Binary::getWord($packet);
     $this->response['arcount'] = Binary::getWord($packet);
     $this->response = array();
     $hostname = Binary::parseLabels($packet);
     while (strlen($packet) > 0) {
         $qtypeInt = Binary::getWord($packet);
         $qtype = isset(DNSClient::$type[$qtypeInt]) ? DNSClient::$type[$qtypeInt] : 'UNK(' . $qtypeInt . ')';
         $qclassInt = Binary::getWord($packet);
         $qclass = isset(DNSClient::$class[$qclassInt]) ? DNSClient::$class[$qclassInt] : 'UNK(' . $qclassInt . ')';
         if (binarySubstr($packet, 0, 2) === "À\f") {
             $packet = binarySubstr($packet, 2);
             continue;
         }
         $ttl = Binary::getDWord($packet);
         $rdlength = Binary::getWord($packet);
         $rdata = binarySubstr($packet, 0, $rdlength);
         $packet = binarySubstr($packet, $rdlength);
         $record = array('type' => $qtype, 'domain' => $hostname, 'ttl' => $ttl, 'class' => $qclass);
         if ($qtype === 'A') {
             if ($rdata === "") {
                 $record['ip'] = false;
                 $record['ttl'] = 5;
                 $packet = '';
                 break;
             } else {
                 $record['ip'] = inet_ntop($rdata);
             }
         } elseif ($qtype === 'NS') {
             $record['ns'] = Binary::parseLabels($rdata);
         }
         if (!isset($this->response[$qtype])) {
             $this->response[$qtype] = array();
         }
         $this->response[$qtype][] = $record;
         if (binarySubstr($packet, 0, 2) === "À\f") {
             $packet = binarySubstr($packet, 2);
             continue;
         }
     }
     $this->requestFinished();
 }