Source for file Packet.php
Documentation is available at Packet.php
* Net_DNS: A resolver library for PHP
* Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/* Net_DNS_Packet object definition {{{ */
* A object represation of a DNS packet (RFC1035)
* This object is used to manage a DNS packet. It contains methods for
* DNS packet compression as defined in RFC1035, as well as parsing a DNS
* packet response from a DNS server, or building a DNS packet from the
* instance variables contained in the class.
/* class variable definitions {{{ */
* If set to true (non-zero), debugging code will be displayed as the
* A packet Header object.
* An object of type Net_DNS_Header which contains the header
* information of the packet.
* @var object Net_DNS_Header $header
* A hash of compressed labels
* A list of all labels which have been compressed in the DNS packet
* and the location offset of the label within the packet.
* The origin of the packet, if the packet is a server response.
* This contains a string containing the IP address of the name server
* from which the answer was given.
* @var string $answerfrom
* The size of the answer packet, if the packet is a server response.
* This contains a integer containing the size of the DNS packet the
* server responded with if this packet was received by a DNS server
* using the query() method.
* @var string $answersize
* An array of Net_DNS_Question objects
* Contains all of the questions within the packet. Each question is
* stored as an object of type Net_DNS_Question.
* An array of Net_DNS_RR ANSWER objects
* Contains all of the answer RRs within the packet. Each answer is
* stored as an object of type Net_DNS_RR.
* An array of Net_DNS_RR AUTHORITY objects
* Contains all of the authority RRs within the packet. Each authority is
* stored as an object of type Net_DNS_RR.
* An array of Net_DNS_RR ADDITIONAL objects
* Contains all of the additional RRs within the packet. Each additional is
* stored as an object of type Net_DNS_RR.
/* class constructor - Net_DNS_Packet($debug = false) {{{ */
* unfortunately (or fortunately), we can't follow the same
* silly method for determining if name is a hostname or a packet
* stream in PHP, since there is no ref() function. So we're going
* to define a new method called parse to deal with this
* circumstance and another method called buildQuestion to build a question.
* I like it better that way anyway.
* Initalizes a Net_DNS_Packet object
* @param boolean $debug Turns debugging on or off
/* Net_DNS_Packet::buildQuestion($name, $type = "A", $class = "IN") {{{ */
* Adds a DNS question to the DNS packet
* @param string $name The name of the record to query
* @param string $type The type of record to query
* @param string $class The class of record to query
* @see Net_DNS::typesbyname(), Net_DNS::classesbyname()
/* Do not print question packet
/* Net_DNS_Packet::parse($data) {{{ */
* Parses a DNS packet returned by a DNS server
* Parses a complete DNS packet and builds an object hierarchy
* containing all of the parts of the packet:
* <li>ANSWER || PREREQUISITE
* <li>ADDITIONAL || UPDATE
* @param string $data A binary string containing a DNS packet
* @return boolean true on success, null on parser error
echo ';; HEADER SECTION' . "\n";
* Print and parse the QUESTION section of the packet
$section = ($this->header->opcode == 'UPDATE') ? 'ZONE' : 'QUESTION';
echo " ;; $section SECTION (" . $this->header->qdcount . ' record' .
* Print and parse the PREREQUISITE or ANSWER section of the packet
$section = ($this->header->opcode == 'UPDATE') ? 'PREREQUISITE' : 'ANSWER';
echo " ;; $section SECTION (" .
list ($rrobj, $offset) = $this->parse_rr($data, $offset);
* Print and parse the UPDATE or AUTHORITY section of the packet
$section = ($this->header->opcode == 'UPDATE') ? 'UPDATE' : 'AUTHORITY';
echo " ;; $section SECTION (" .
list ($rrobj, $offset) = $this->parse_rr($data, $offset);
* Print and parse the ADDITIONAL section of the packet
echo ';; ADDITIONAL SECTION (' .
list ($rrobj, $offset) = $this->parse_rr($data, $offset);
/* Net_DNS_Packet::data() {{{*/
* Build a packet from a Packet object hierarchy
* Builds a valid DNS packet suitable for sending to a DNS server or
* resolver client containing all of the data in the packet hierarchy.
* @return string A binary string containing a DNS Packet
/* Net_DNS_Packet::dn_comp($name, $offset) {{{*/
* DNS packet compression method
* Returns a domain name compressed for a particular packet object, to
* be stored beginning at the given offset within the packet data. The
* name will be added to a running list of compressed domain names for
* @param string $name The name of the label to compress
* @param integer $offset The location offset in the packet to where
* the label will be stored.
* @return string $compname A binary string containing the compressed
* @see Net_DNS_Packet::dn_expand()
$dname = join('.', $names);
$compname .= pack('Ca*', $length, $first);
$compname .= pack('C', 0 );
/* Net_DNS_Packet::dn_expand($packet, $offset) {{{ */
* DNS packet decompression method
* Expands the domain name stored at a particular location in a DNS
* packet. The first argument is a variable containing the packet
* data. The second argument is the offset within the packet where
* the (possibly) compressed domain name is stored.
* @param string $packet The packet data
* @param integer $offset The location offset in the packet of the
* @return array Returns a list of type array($name, $offset) where
* $name is the name of the label which was decompressed
* and $offset is the offset of the next field in the
* packet. Returns array(null, null) on error
if ($packetlen < ($offset + 1 )) {
return array (null , null );
$a = unpack(" @$offset/Cchar" , $packet);
} else if (($len & 0xc0 ) == 0xc0 ) {
if ($packetlen < ($offset + $int16sz)) {
return array (null , null );
$ptr = unpack(" @$offset/ni" , $packet);
return array (null , null );
if ($packetlen < ($offset + $len)) {
return array (null , null );
$elem = substr($packet, $offset, $len);
return array ($name, $offset);
/* Net_DNS_Packet::label_extract($packet, $offset) {{{ */
* DNS packet decompression method
* Extracts the label stored at a particular location in a DNS
* packet. The first argument is a variable containing the packet
* data. The second argument is the offset within the packet where
* the (possibly) compressed domain name is stored.
* @param string $packet The packet data
* @param integer $offset The location offset in the packet of the
* @return array Returns a list of type array($name, $offset) where
* $name is the name of the label which was decompressed
* and $offset is the offset of the next field in the
* packet. Returns array(null, null) on error
if ($packetlen < ($offset + 1 )) {
return array (null , null );
$a = unpack(" @$offset/Cchar" , $packet);
if ($len + $offset > $packetlen) {
$name = substr($packet, $offset);
$name = substr($packet, $offset, $len);
return array ($name, $offset);
/* Net_DNS_Packet::parse_question($data, $offset) {{{ */
* Parses the question section of a packet
* Examines a DNS packet at the specified offset and parses the data
* of the QUESTION section.
* @param string $data The packet data returned from the server
* @param integer $offset The location offset of the start of the
* @return array An array of type array($q, $offset) where $q
* is a Net_DNS_Question object and $offset is the
* location of the next section of the packet which
list ($qname, $offset) = $this->dn_expand($data, $offset);
return array (null , null );
if (strlen($data) < ($offset + 2 * 2 )) {
return array (null , null );
$q = unpack(" @$offset/n2int" , $data);
return array ($q, $offset);
/* Net_DNS_Packet::parse_rr($data, $offset) {{{ */
* Parses a resource record section of a packet
* Examines a DNS packet at the specified offset and parses the data
* of a section which contains RRs (ANSWER, AUTHORITY, ADDITIONAL).
* @param string $data The packet data returned from the server
* @param integer $offset The location offset of the start of the resource
* @return array An array of type array($rr, $offset) where $rr
* is a Net_DNS_RR object and $offset is the
* location of the next section of the packet which
list ($name, $offset) = $this->dn_expand($data, $offset);
return array (null , null );
if (strlen($data) < ($offset + 10 )) {
return array (null , null );
$a = unpack(" @$offset/n2tc/Nttl/nrdlength" , $data);
$rdlength = $a['rdlength'];
if (strlen($data) < ($offset + $rdlength)) {
return array (null , null );
return array (null , null );
return array ($rrobj, $offset);
/* Net_DNS_Packet::display() {{{ */
* Prints out the packet in a human readable formatted string
/* Net_DNS_Packet::string() {{{ */
* Builds a human readable formatted string representing a packet
$retval .= ';; Answer received from ' . $this->answerfrom . '(' .
$retval .= ";; HEADER SECTION\n";
$section = ($this->header->opcode == 'UPDATE') ? 'ZONE' : 'QUESTION';
$retval .= ';; ' . $qr->string () . "\n";
$section = ($this->header->opcode == 'UPDATE') ? 'PREREQUISITE' : 'ANSWER';
foreach ($this->answer as $ans) {
$retval .= ';; ' . $ans->string () . "\n";
$section = ($this->header->opcode == 'UPDATE') ? 'UPDATE' : 'AUTHORITY';
$retval .= ';; ' . $auth->string () . "\n";
$retval .= "\n;; ADDITIONAL SECTION (" . $this->header->arcount .
$retval .= ';; ' . $addl->string () . "\n";
* vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
Documentation generated on Mon, 11 Mar 2019 14:35:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|