2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5
* DNS Library for handling lookups and updates.
9
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
10
* All rights reserved.
12
* Redistribution and use in source and binary forms, with or without
13
* modification, are permitted provided that the following conditions
16
* * Redistributions of source code must retain the above copyright
17
* notice, this list of conditions and the following disclaimer.
19
* * Redistributions in binary form must reproduce the above copyright
20
* notice, this list of conditions and the following disclaimer in
21
* the documentation and/or other materials provided with the
24
* * Neither the name of Mike Pultz nor the names of his contributors
25
* may be used to endorse or promote products derived from this
26
* software without specific prior written permission.
28
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
37
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39
* POSSIBILITY OF SUCH DAMAGE.
41
* @category Networking
43
* @author Mike Pultz <mike@mikepultz.com>
44
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
45
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
46
* @version SVN: $Id: RR.php 188 2013-03-31 01:25:46Z mike.pultz $
47
* @link http://pear.php.net/package/Net_DNS2
48
* @since File available since Release 0.6.0
54
* This is the base class for DNS Resource Records
56
* Each resource record type (defined in RR/*.php) extends this class for
59
* This class handles parsing and constructing the common parts of the DNS
60
* resource records, while the RR specific functionality is handled in each
63
* DNS resource record format - RFC1035 section 4.1.3
65
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
66
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
71
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
73
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
75
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
78
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
80
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
83
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
85
* @category Networking
87
* @author Mike Pultz <mike@mikepultz.com>
88
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
89
* @link http://pear.php.net/package/Net_DNS2
92
abstract class Net_DNS2_RR
95
* The name of the resource record
100
* The resource record type
105
* The resouce record class
110
* The time to live for this resource record
115
* The length of the rdata field
120
* The resource record specific data as a packed binary string
125
* abstract definition - method to return a RR as a string; not to
126
* be confused with the __toString() magic method.
132
abstract protected function rrToString();
135
* abstract definition - parses a RR from a standard DNS config line
137
* @param array $rdata a string split line of values for the rdata
143
abstract protected function rrFromString(array $rdata);
146
* abstract definition - sets a Net_DNS2_RR from a Net_DNS2_Packet object
148
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
154
abstract protected function rrSet(Net_DNS2_Packet &$packet);
157
* abstract definition - returns a binary packet DNS RR object
159
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
162
* @return mixed either returns a binary packed string or
167
abstract protected function rrGet(Net_DNS2_Packet &$packet);
170
* Constructor - builds a new Net_DNS2_RR object
172
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet or null to create
174
* @param array $rr an array with RR parse values or null to
175
* create an empty object
177
* @throws Net_DNS2_Exception
181
public function __construct(Net_DNS2_Packet &$packet = null, array $rr = null)
183
if ( (!is_null($packet)) && (!is_null($rr)) ) {
185
if ($this->set($packet, $rr) == false) {
187
throw new Net_DNS2_Exception(
188
'failed to generate resource record',
189
Net_DNS2_Lookups::E_RR_INVALID
194
$class = Net_DNS2_Lookups::$rr_types_class_to_id[get_class($this)];
197
$this->type = Net_DNS2_Lookups::$rr_types_by_id[$class];
206
* magic __toString() method to return the Net_DNS2_RR object object as a string
212
public function __toString()
214
return $this->name . '. ' . $this->ttl . ' ' . $this->class .
215
' ' . $this->type . ' ' . $this->rrToString();
219
* return a formatted string; if a string has spaces in it, then return
220
* it with double quotes around it, otherwise, return it as it was passed in.
222
* @param string $string the string to format
228
protected function formatString($string)
230
return '"' . str_replace('"', '\"', trim($string, '"')) . '"';
234
* builds an array of strings from an array of chunks of text split by spaces
236
* @param array $chunks an array of chunks of text split by spaces
242
protected function buildString(array $chunks)
248
foreach ($chunks as $r) {
251
if (strlen($r) == 0) {
256
&& ($r[strlen($r) - 1] == '"')
257
&& ($r[strlen($r) - 2] != '\\')
264
} else if ($r[0] == '"') {
269
} else if ( ($r[strlen($r) - 1] == '"')
270
&& ($r[strlen($r) - 2] != '\\')
273
$data[$c] .= ' ' . $r;
280
$data[$c] .= ' ' . $r;
287
foreach ($data as $index => $string) {
289
$data[$index] = str_replace('\"', '"', trim($string, '"'));
296
* builds a new Net_DNS2_RR object
298
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet or null to create
300
* @param array $rr an array with RR parse values or null to
301
* create an empty object
304
* @throws Net_DNS2_Exception
308
public function set(Net_DNS2_Packet &$packet, array $rr)
310
$this->name = $rr['name'];
311
$this->type = Net_DNS2_Lookups::$rr_types_by_id[$rr['type']];
314
// for RR OPT (41), the class value includes the requestors UDP payload size,
315
// and not a class value
317
if ($this->type == 'OPT') {
318
$this->class = $rr['class'];
320
$this->class = Net_DNS2_Lookups::$classes_by_id[$rr['class']];
323
$this->ttl = $rr['ttl'];
324
$this->rdlength = $rr['rdlength'];
325
$this->rdata = substr($packet->rdata, $packet->offset, $rr['rdlength']);
327
return $this->rrSet($packet);
331
* returns a binary packed DNS RR object
333
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet used for
337
* @throws Net_DNS2_Exception
341
public function get(Net_DNS2_Packet &$packet)
349
$data = $packet->compress($this->name, $packet->offset);
352
// pack the main values
354
if ($this->type == 'OPT') {
357
// pre-build the TTL value
362
// the class value is different for OPT types
366
Net_DNS2_Lookups::$rr_types_by_name[$this->type],
374
Net_DNS2_Lookups::$rr_types_by_name[$this->type],
375
Net_DNS2_Lookups::$classes_by_name[$this->class],
381
// increase the offset, and allow for the rdlength
383
$packet->offset += 10;
386
// get the RR specific details
388
if ($this->rdlength != -1) {
390
$rdata = $this->rrGet($packet);
396
$data .= pack('n', strlen($rdata)) . $rdata;
402
* parses a binary packet, and returns the appropriate Net_DNS2_RR object,
403
* based on the RR type of the binary content.
405
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet used for
406
* decompressing names
408
* @return mixed returns a new Net_DNS2_RR_* object for
410
* @throws Net_DNS2_Exception
414
public static function parse(Net_DNS2_Packet &$packet)
421
$object['name'] = $packet->expand($packet, $packet->offset);
422
if (is_null($object['name'])) {
424
throw new Net_DNS2_Exception(
425
'failed to parse resource record: failed to expand name.',
426
Net_DNS2_Lookups::E_PARSE_ERROR
429
if ($packet->rdlength < ($packet->offset + 10)) {
431
throw new Net_DNS2_Exception(
432
'failed to parse resource record: packet too small.',
433
Net_DNS2_Lookups::E_PARSE_ERROR
438
// unpack the RR details
440
$object['type'] = ord($packet->rdata[$packet->offset++]) << 8 |
441
ord($packet->rdata[$packet->offset++]);
442
$object['class'] = ord($packet->rdata[$packet->offset++]) << 8 |
443
ord($packet->rdata[$packet->offset++]);
445
$object['ttl'] = ord($packet->rdata[$packet->offset++]) << 24 |
446
ord($packet->rdata[$packet->offset++]) << 16 |
447
ord($packet->rdata[$packet->offset++]) << 8 |
448
ord($packet->rdata[$packet->offset++]);
450
$object['rdlength'] = ord($packet->rdata[$packet->offset++]) << 8 |
451
ord($packet->rdata[$packet->offset++]);
453
if ($packet->rdlength < ($packet->offset + $object['rdlength'])) {
458
// lookup the class to use
461
$class = Net_DNS2_Lookups::$rr_types_id_to_class[$object['type']];
465
$o = new $class($packet, $object);
468
$packet->offset += $object['rdlength'];
472
throw new Net_DNS2_Exception(
473
'un-implemented resource record type: ' . $object['type'],
474
Net_DNS2_Lookups::E_RR_INVALID
482
* cleans up some RR data
484
* @param string $data the text string to clean
486
* @return string returns the cleaned string
491
public function cleanString($data)
493
return strtolower(rtrim($data, '.'));
497
* parses a standard RR format lines, as defined by rfc1035 (kinda)
499
* In our implementation, the domain *must* be specified- format must be
501
* <name> [<ttl>] [<class>] <type> <rdata>
503
* <name> [<class>] [<ttl>] <type> <rdata>
505
* name, title, class and type are parsed by this function, rdata is passed
506
* to the RR specific classes for parsing.
508
* @param string $line a standard DNS config line
510
* @return mixed returns a new Net_DNS2_RR_* object for the given RR
511
* @throws Net_DNS2_Exception
515
public static function fromString($line)
517
if (strlen($line) == 0) {
518
throw new Net_DNS2_Exception(
519
'empty config line provided.',
520
Net_DNS2_Lookups::E_PARSE_ERROR
530
// split the line by spaces
532
$values = preg_split('/[\s]+/', $line);
533
if (count($values) < 3) {
535
throw new Net_DNS2_Exception(
536
'failed to parse config: minimum of name, type and rdata required.',
537
Net_DNS2_Lookups::E_PARSE_ERROR
542
// assume the first value is the name
544
$name = trim(strtolower(array_shift($values)), '.');
547
// The next value is either a TTL, Class or Type
549
foreach ($values as $value) {
552
case is_numeric($value):
554
$ttl = array_shift($values);
561
$ttl = array_shift($values);
564
case isset(Net_DNS2_Lookups::$classes_by_name[strtoupper($value)]):
566
$class = strtoupper(array_shift($values));
569
case isset(Net_DNS2_Lookups::$rr_types_by_name[strtoupper($value)]):
571
$type = strtoupper(array_shift($values));
576
throw new Net_DNS2_Exception(
577
'invalid config line provided: unknown file: ' . $value,
578
Net_DNS2_Lookups::E_PARSE_ERROR
584
// lookup the class to use
587
$class_name = Net_DNS2_Lookups::$rr_types_id_to_class[
588
Net_DNS2_Lookups::$rr_types_by_name[$type]
591
if (isset($class_name)) {
593
$o = new $class_name;
597
// set the parsed values
606
if ($o->rrFromString($values) === false) {
608
throw new Net_DNS2_Exception(
609
'failed to parse rdata for config: ' . $line,
610
Net_DNS2_Lookups::E_PARSE_ERROR
616
throw new Net_DNS2_Exception(
617
'failed to create new RR record for type: ' . $type,
618
Net_DNS2_Lookups::E_RR_INVALID
624
throw new Net_DNS2_Exception(
625
'un-implemented resource record type: '. $type,
626
Net_DNS2_Lookups::E_RR_INVALID
638
* c-hanging-comment-ender-p: nil