added

parents
Pipeline #55 canceled with stages
Net_DNS2_RFC3597 - RFC3597 support for Net_DNS2
Copyright (c) 2019 CentralNic Group plc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Mike Pultz nor the names of his contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
<?php
/**
* RFC3597 support for Net_DNS2
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597 {
/**
* setup method, replaces the arrays in Net_DNS2_Lookups with dynamic registries
*/
static function setup() {
Net_DNS2_Lookups::$rr_types_by_id = new Net_DNS2_RFC3597_DynamicRegistry_RRTypeByID(Net_DNS2_Lookups::$rr_types_by_id);
Net_DNS2_Lookups::$rr_types_by_name = new Net_DNS2_RFC3597_DynamicRegistry_RRIDByType(Net_DNS2_Lookups::$rr_types_by_name);
Net_DNS2_Lookups::$rr_types_class_to_id = new Net_DNS2_RFC3597_DynamicRegistry_RRIDByClass(Net_DNS2_Lookups::$rr_types_class_to_id);
Net_DNS2_Lookups::$rr_types_id_to_class = new Net_DNS2_RFC3597_DynamicRegistry_RRClassByID(Net_DNS2_Lookups::$rr_types_id_to_class);
}
/**
* name (and create) a new class, which extends Net_DNS2_RFC3597_RR, for the given unknown RR type
* @param int $id
* @return string
*/
static function createRRType(int $id) : string {
$class = sprintf('Net_DNS2_RR_TYPE%u', $id);
// yes I know eval() is eval
eval(sprintf('class %s extends Net_DNS2_RFC3597_RR {}', $class));
return $class;
}
/**
* autoload function
* @param string $name
*/
static public function autoload($name) {
if (__CLASS__ == substr($name, 0, strlen(__CLASS__))) @include_once(str_replace('_', '/', $name).'.php');
}
}
<?php
/**
* base class for dynamic registry objects
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
abstract class Net_DNS2_RFC3597_DynamicRegistry extends ArrayIterator {
/**
* @see ArrayIterator::offsetGet()
* @param mixed $index
* @return mixed
*/
function offsetGet($index) {
if (parent::offsetExists($index)) {
return parent::offsetGet($index);
} else {
$value = $this->createValueFor($index);
$this->offsetSet($index, $value);
return $value;
}
}
/**
* given an index (for a non-existent entry in the registry), compute what the value *should* be
* @param mixed $index
* @return mixed
*/
protected abstract function createValueFor($index);
}
<?php
/**
* dynamic registry for RR classes indexed by numeric ID
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597_DynamicRegistry_RRClassByID extends Net_DNS2_RFC3597_DynamicRegistry {
/**
* @see Net_DNS2_RFC3597_DynamicRegistry::offsetGet()
* @param mixed $index
* @return mixed
*/
function offsetExists($index) {
if (parent::offsetExists($index)) {
return true;
} else {
return ($index >= 0 && $index <= 65535);
}
}
/**
* given an index (for a non-existent entry in the registry), compute what the value *should* be
* @param mixed index
* @return mixed
*/
protected function createValueFor($index) {
return Net_DNS2_RFC3597::createRRType($index);
}
}
<?php
/**
* dynamic registry for RR IDs indexed by class
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597_DynamicRegistry_RRIDByClass extends Net_DNS2_RFC3597_DynamicRegistry {
/**
* @see Net_DNS2_RFC3597_DynamicRegistry::offsetGet()
* @param mixed $index
* @return mixed
*/
function offsetExists($index) {
if (parent::offsetExists($index)) {
return true;
} else {
return (1 == preg_match('/^Net_DNS2_RR_TYPE(\d+)$/', $index));
}
}
/**
* given an index (for a non-existent entry in the registry), compute what the value *should* be
* @param mixed index
* @return mixed
*/
protected function createValueFor($index) {
if (1 == preg_match('/^Net_DNS2_RR_TYPE(\d+)$/', $index, $matches)) {
return intval($matches[1]);
} else {
return NULL;
}
}
}
<?php
/**
* dynamic registry for RR IDs indexed by type
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597_DynamicRegistry_RRIDByType extends Net_DNS2_RFC3597_DynamicRegistry {
/**
* @see Net_DNS2_RFC3597_DynamicRegistry::offsetGet()
* @param mixed $index
* @return mixed
*/
function offsetExists($index) {
if (parent::offsetExists($index)) {
return true;
} else {
return (1 == preg_match('/^TYPE(\d+)$/', $index));
}
}
/**
* given an index (for a non-existent entry in the registry), compute what the value *should* be
* @param mixed index
* @return mixed
*/
protected function createValueFor($index) {
if (1 == preg_match('/^TYPE(\d+)$/', $index, $matches)) {
return intval($matches[1]);
} else {
return NULL;
}
}
}
<?php
/**
* dynamic registry for RR types indexed by numeric ID
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597_DynamicRegistry_RRTypeByID extends Net_DNS2_RFC3597_DynamicRegistry {
/**
* @see Net_DNS2_RFC3597_DynamicRegistry::offsetGet()
* @param mixed $index
* @return mixed
*/
function offsetExists($index) {
if (parent::offsetExists($index)) {
return true;
} else {
return ($index >= 0 && $index <= 65535);
}
}
/**
* given an index (for a non-existent entry in the registry), compute what the value *should* be
* @param mixed $index
* @return mixed
*/
protected function createValueFor($index) {
return sprintf('TYPE%u', $index);
}
}
<?php
/**
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597_Exception extends Net_DNS2_Exception {
}
<?php
/**
* base class for RFC4597 RR objects
* @category Networking
* @author Gavin Brown <gavin.brown@centralnic.com>
* @package Net_DNS2_RFC3597
*/
class Net_DNS2_RFC3597_RR extends Net_DNS2_RR {
/**
* the binary data for this RR - by definition we don't know what might be in this data, so applications will need to parse it themselves
*/
public $data;
/**
* method to return a RR as a string; not to
* be confused with the __toString() magic method.
* @return string
*/
protected function rrToString() : string {
return sprintf("\# %u %s", strlen($this->data), bin2hex($this->data));
}
/**
* parses a RR from a standard DNS config line
* @param array $rdata a string split line of values for the rdata
* @return bool
*/
protected function rrFromString(array $rdata) : bool {
// first element must be a literal a backslash immediately followed by a hash sign
if ('\#' != array_shift($rdata)) return false;
// discard the next value which is an unsigned decimal integer specifying the RDATA length in octets
array_shift($rdata);
// discard anything not hex
$data = preg_replace('/[^0-9a-f]/', '', strtolower(implode('', $rdata)));
// must be an even number of hex digits
if (0 != strlen($data) % 2) return false;
// convert the hex to binary
$this->data = hex2bin($data);
return true;
}
/**
* sets a Net_DNS2_RR from a Net_DNS2_Packet object
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
* @return bool
*/
protected function rrSet(Net_DNS2_Packet &$packet) : bool {
// just copy the binary data
$this->data = $this->rdata;
return true;
}
/**
* returns a binary packet DNS RR object
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for compressed names
* @return mixed either returns a binary packed string or null on failure
*/
protected function rrGet(Net_DNS2_Packet &$packet) {
$packet->offset += strlen($this->data);
return $this->data;
}
}
# Net\_DNS2\_RFC3597 - RFC3597 support for Net_DNS2
This package implements support for unsupported RR types to Net\_DNS2, as described in RFC3597.
Normally, Net\_DNS2 will throw an exception if it parses a response packet and sees a record that it doesn't have a corresponding Net\_DNS2\_RR\_* class for. This can cause problems with things like zone file parsing and zone transfers.
Net\_DNS2_RFC3597 resolves this problem by replacing the RR type registries in Net\_DNS2\_Lookups with dynamically objects, which will define a new class for each unsupported RR type that Net\_DNS2 sees.
Net\_DNS2_RFC3597 also allows you to query for unsupported RR types.
## Installation
```
composer require centralnic/net_dns2_rfc3597
```
## Requirements
* Net\_DNS2
## Using Net\_DNS2
```php
<?php
require 'vendor/autoload.php';
Net_DNS2_RFC3597::setup();
$resolver = new Net_DNS2_Resolver;
$resolver->query('example.com.', 'TYPE1234');
```
## Copyright
Copyright (c) 2019 CentralNic Group plc. All rights reserved.
{
"name": "centralnic/net_dns2_rfc3597",
"type": "library",
"description": "RFC3597 support for Net_DNS2",
"keywords": ["dns","network", "pear"],
"license": "BSD-2-Clause",
"authors": [
{
"name": "CentralNic Group plc",
"email": "info@centralnic.com",
"homepage": "https://www.centralnic.com/",
"role": "Developer"
}
],
"require": {
"pear/netdns2": "*"
},
"autoload": {
"psr-0": { "Net_DNS2_RFC3597": "" }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment