1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
<?php
/*
* License Information:
*
* 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.
*
* @package Net_DNS
*/
class Net_DNS_Packet
{
/* class variable definitions {{{ */
/**
* debugging flag
*
* If set to TRUE (non-zero), debugging code will be displayed as the
* packet is parsed.
*
* @var boolean $debug
* @access public
*/
var $debug;
/**
* 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
* @access public
*/
var $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.
*
* @var array $compnames
*/
var $compnames;
/**
* 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
* @access public
*/
var $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
* @access public
*/
var $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.
*
* @var array $question
* @access public
*/
var $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.
*
* @var array $answer
* @access public
*/
var $answer;
/**
* 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.
*
* @var array $authority
* @access public
*/
var $authority;
/**
* 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.
*
* @var array $additional
* @access public
*/
var $additional;
/* }}} */
/* 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
*/
function Net_DNS_Packet($debug = FALSE)
{
$this->debug = $debug;
$this->compnames = array();
}
/* }}} */
/* 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()
*/
function buildQuestion($name, $type = 'A', $class = 'IN')
{
$this->header = new Net_DNS_Header();
$this->header->qdcount = 1;
$this->question[0] = new Net_DNS_Question($name, $type, $class);
$this->answer = NULL;
$this->authority = NULL;
$this->additional = NULL;
/* if ($this->debug) {
* $this->display();
* }
*/
}
/* }}} */
/* 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:
* <ul>
* <li>HEADER
* <li>QUESTION
* <li>ANSWER || PREREQUISITE
* <li>ADDITIONAL || UPDATE
* <li>AUTHORITY
* </ul>
*
* @param string $data A binary string containing a DNS packet
* @return boolean TRUE on success, NULL on parser error
*/
function parse($data)
{
if ($this->debug) {
echo ';; HEADER SECTION' . "\n";
}
$this->header = new Net_DNS_Header($data);
if ($this->debug) {
$this->header->display();
}
/*
* Print and parse the QUESTION section of the packet
*/
if ($this->debug) {
echo "\n";
$section = ($this->header->opcode == 'UPDATE') ? 'ZONE' : 'QUESTION';
echo ";; $section SECTION (" . $this->header->qdcount . ' record' .
($this->header->qdcount == 1 ? '' : 's') . ")\n";
}
$offset = 12;
$this->question = array();
for ($ctr = 0; $ctr < $this->header->qdcount; $ctr++) {
list($qobj, $offset) = $this->parse_question($data, $offset);
if (is_null($qobj)) {
return(NULL);
}
$this->question[count($this->question)] = $qobj;
if ($this->debug) {
echo ";;\n;";
$qobj->display();
}
}
/*
* Print and parse the PREREQUISITE or ANSWER section of the packet
*/
if ($this->debug) {
echo "\n";
$section = ($this->header->opcode == 'UPDATE') ? 'PREREQUISITE' :'ANSWER';
echo ";; $section SECTION (" .
$this->header->ancount . ' record' .
(($this->header->ancount == 1) ? '' : 's') .
")\n";
}
$this->answer = array();
for ($ctr = 0; $ctr < $this->header->ancount; $ctr++) {
list($rrobj, $offset) = $this->parse_rr($data, $offset);
if (is_null($rrobj)) {
return(NULL);
}
array_push($this->answer, $rrobj);
if ($this->debug) {
$rrobj->display();
}
}
/*
* Print and parse the UPDATE or AUTHORITY section of the packet
*/
if ($this->debug) {
echo "\n";
$section = ($this->header->opcode == 'UPDATE') ? 'UPDATE' : 'AUTHORITY';
echo ";; $section SECTION (" .
$this->header->nscount . ' record' .
(($this->header->nscount == 1) ? '' : 's') .
")\n";
}
$this->authority = array();
for ($ctr = 0; $ctr < $this->header->nscount; $ctr++) {
list($rrobj, $offset) = $this->parse_rr($data, $offset);
if (is_null($rrobj)) {
return(NULL);
}
array_push($this->authority, $rrobj);
if ($this->debug) {
$rrobj->display();
}
}
/*
* Print and parse the ADDITIONAL section of the packet
*/
if ($this->debug) {
echo "\n";
echo ';; ADDITIONAL SECTION (' .
$this->header->arcount . ' record' .
(($this->header->arcount == 1) ? '' : 's') .
")\n";
}
$this->additional = array();
for ($ctr = 0; $ctr < $this->header->arcount; $ctr++) {
list($rrobj, $offset) = $this->parse_rr($data, $offset);
if (is_null($rrobj)) {
return(NULL);
}
array_push($this->additional, $rrobj);
if ($this->debug) {
$rrobj->display();
}
}
return(TRUE);
}
/* }}} */
/* 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
*/
function data()
{
$data = $this->header->data();
for ($ctr = 0; $ctr < $this->header->qdcount; $ctr++) {
$data .= $this->question[$ctr]->data($this, strlen($data));
}
for ($ctr = 0; $ctr < $this->header->ancount; $ctr++) {
$data .= $this->answer[$ctr]->data($this, strlen($data));
}
for ($ctr = 0; $ctr < $this->header->nscount; $ctr++) {
$data .= $this->authority[$ctr]->data($this, strlen($data));
}
for ($ctr = 0; $ctr < $this->header->arcount; $ctr++) {
$data .= $this->additional[$ctr]->data($this, strlen($data));
}
return($data);
}
/*}}}*/
/* 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
* future use.
*
* @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
* label.
* @see Net_DNS_Packet::dn_expand()
*/
function dn_comp($name, $offset)
{
$names = array_filter(explode('.', $name));
$compname = '';
while (count($names)) {
$dname = join('.', $names);
if (isset($this->compnames[$dname])) {
$compname .= pack('n', 0xc000 | $this->compnames[$dname]);
break;
}
$this->compnames[$dname] = $offset;
$first = array_shift($names);
$length = strlen($first);
$compname .= pack('Ca*', $length, $first);
$offset += $length + 1;
}
if (! count($names)) {
$compname .= pack('C', 0);
}
return($compname);
}
/*}}}*/
/* 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
* label to decompress.
* @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
*/
function dn_expand($packet, $offset)
{
$packetlen = strlen($packet);
$int16sz = 2;
$name = '';
while (1) {
if ($packetlen < ($offset + 1)) {
return(array(NULL, NULL));
}
$a = unpack("@$offset/Cchar", $packet);
$len = $a['char'];
if ($len == 0) {
$offset++;
break;
} else if (($len & 0xc0) == 0xc0) {
if ($packetlen < ($offset + $int16sz)) {
return(array(NULL, NULL));
}
$ptr = unpack("@$offset/ni", $packet);
$ptr = $ptr['i'];
$ptr = $ptr & 0x3fff;
$name2 = Net_DNS_Packet::dn_expand($packet, $ptr);
if (is_null($name2[0])) {
return(array(NULL, NULL));
}
$name .= $name2[0];
$offset += $int16sz;
break;
} else {
$offset++;
if ($packetlen < ($offset + $len)) {
return(array(NULL, NULL));
}
$elem = substr($packet, $offset, $len);
$name .= $elem . '.';
$offset += $len;
}
}
$name = ereg_replace('\.$', '', $name);
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
* label to extract.
* @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
*/
function label_extract($packet, $offset)
{
$packetlen = strlen($packet);
$name = '';
if ($packetlen < ($offset + 1)) {
return(array(NULL, NULL));
}
$a = unpack("@$offset/Cchar", $packet);
$len = $a['char'];
$offset++;
if ($len + $offset > $packetlen) {
$name = substr($packet, $offset);
$offset = $packetlen;
} else {
$name = substr($packet, $offset, $len);
$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
* question section.
* @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
* needs to be parsed.
*/
function parse_question($data, $offset)
{
list($qname, $offset) = $this->dn_expand($data, $offset);
if (is_null($qname)) {
return(array(NULL, NULL));
}
if (strlen($data) < ($offset + 2 * 2)) {
return(array(NULL, NULL));
}
$q = unpack("@$offset/n2int", $data);
$qtype = $q['int1'];
$qclass = $q['int2'];
$offset += 2 * 2;
$qtype = Net_DNS::typesbyval($qtype);
$qclass = Net_DNS::classesbyval($qclass);
$q = new Net_DNS_Question($qname, $qtype, $qclass);
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
* record section.
* @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
* needs to be parsed.
*/
function parse_rr($data, $offset)
{
list($name, $offset) = $this->dn_expand($data, $offset);
if ($name === NULL) {
//if (! strlen($name)) {
return(array(NULL, NULL));
}
if (strlen($data) < ($offset + 10)) {
return(array(NULL, NULL));
}
$a = unpack("@$offset/n2tc/Nttl/nrdlength", $data);
$type = $a['tc1'];
$class = $a['tc2'];
$ttl = $a['ttl'];
$rdlength = $a['rdlength'];
$type = Net_DNS::typesbyval($type);
$class = Net_DNS::classesbyval($class);
$offset += 10;
if (strlen($data) < ($offset + $rdlength)) {
return(array(NULL, NULL));
}
$rrobj = new Net_DNS_RR(array($name,
$type,
$class,
$ttl,
$rdlength,
$data,
$offset));
$rrobj= $rrobj->getRR();
if (is_null($rrobj)) {
return(array(NULL, NULL));
}
$offset += $rdlength;
return(array($rrobj, $offset));
}
/* }}} */
/* Net_DNS_Packet::display() {{{ */
/**
* Prints out the packet in a human readable formatted string
*/
function display()
{
echo $this->string();
}
/*}}}*/
/* Net_DNS_Packet::string() {{{ */
/**
* Builds a human readable formatted string representing a packet
*/
function string()
{
$retval = '';
if ($this->answerfrom) {
$retval .= ';; Answer received from ' . $this->answerfrom . '(' .
$this->answersize . " bytes)\n;;\n";
}
$retval .= ";; HEADER SECTION\n";
$retval .= $this->header->string();
$retval .= "\n";
$section = ($this->header->opcode == 'UPDATE') ? 'ZONE' : 'QUESTION';
$retval .= ";; $section SECTION (" . $this->header->qdcount .
' record' . ($this->header->qdcount == 1 ? '' : 's') .
")\n";
foreach ($this->question as $qr) {
$retval .= ';; ' . $qr->string() . "\n";
}
$section = ($this->header->opcode == 'UPDATE') ? 'PREREQUISITE' : 'ANSWER';
$retval .= "\n;; $section SECTION (" . $this->header->ancount .
' record' . ($this->header->ancount == 1 ? '' : 's') .
")\n";
if (is_array($this->answer)) {
foreach ($this->answer as $ans) {
$retval .= ';; ' . $ans->string() . "\n";
}
}
$section = ($this->header->opcode == 'UPDATE') ? 'UPDATE' : 'AUTHORITY';
$retval .= "\n;; $section SECTION (" . $this->header->nscount .
' record' . ($this->header->nscount == 1 ? '' : 's') .
")\n";
if (is_array($this->authority)) {
foreach ($this->authority as $auth) {
$retval .= ';; ' . $auth->string() . "\n";
}
}
$retval .= "\n;; ADDITIONAL SECTION (" . $this->header->arcount .
' record' . ($this->header->arcount == 1 ? '' : 's') .
")\n";
if (is_array($this->additional)) {
foreach ($this->additional as $addl) {
$retval .= ';; ' . $addl->string() . "\n";
}
}
$retval .= "\n\n";
return($retval);
}
/*}}}*/
}
/* }}} */
/* VIM settings {{{
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* soft-stop-width: 4
* c indent on
* End:
* vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
* vim<600: sw=4 ts=4
* }}} */
?>
|