From 00903d4481ad7f0958b58e2ab3e85c03516aa6fa Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Wed, 26 Jun 2013 00:05:51 +0100 Subject: [PATCH] Burn all the things! Look, it was rubbish, OK. I'm sorry. If you are feeling nostalgic or are simply insane, feel free to use the 1.0 branch. --- examples/AQuery.php | 42 -- examples/BasicServer.php | 460 --------------------- examples/SOAQuery.php | 41 -- src/LibDNS/DataType.php | 40 -- src/LibDNS/DataTypes/Anything.php | 29 -- src/LibDNS/DataTypes/BitMap.php | 81 ---- src/LibDNS/DataTypes/Char.php | 40 -- src/LibDNS/DataTypes/CharacterString.php | 43 -- src/LibDNS/DataTypes/DomainName.php | 87 ---- src/LibDNS/DataTypes/IPv4Address.php | 64 --- src/LibDNS/DataTypes/IPv6Address.php | 98 ----- src/LibDNS/DataTypes/Long.php | 71 ---- src/LibDNS/DataTypes/Short.php | 40 -- src/LibDNS/DataTypes/Vector.php | 24 -- src/LibDNS/DataTypes/Vectors/AFSDB.php | 62 --- src/LibDNS/DataTypes/Vectors/HINFO.php | 54 --- src/LibDNS/DataTypes/Vectors/ISDN.php | 54 --- src/LibDNS/DataTypes/Vectors/MINFO.php | 62 --- src/LibDNS/DataTypes/Vectors/MX.php | 63 --- src/LibDNS/DataTypes/Vectors/RP.php | 62 --- src/LibDNS/DataTypes/Vectors/RT.php | 63 --- src/LibDNS/DataTypes/Vectors/SOA.php | 147 ------- src/LibDNS/DataTypes/Vectors/WKS.php | 69 ---- src/LibDNS/Message.php | 297 ------------- src/LibDNS/Messages/Request.php | 13 - src/LibDNS/Messages/Response.php | 13 - src/LibDNS/Packet.php | 80 ---- src/LibDNS/PacketBuilder/PacketBuilder.php | 80 ---- src/LibDNS/PacketBuilder/WriteBlock.php | 61 --- src/LibDNS/Record.php | 79 ---- src/LibDNS/Records/Query.php | 30 -- src/LibDNS/Records/Resource.php | 103 ----- 32 files changed, 2552 deletions(-) delete mode 100644 examples/AQuery.php delete mode 100644 examples/BasicServer.php delete mode 100644 examples/SOAQuery.php delete mode 100644 src/LibDNS/DataType.php delete mode 100644 src/LibDNS/DataTypes/Anything.php delete mode 100644 src/LibDNS/DataTypes/BitMap.php delete mode 100644 src/LibDNS/DataTypes/Char.php delete mode 100644 src/LibDNS/DataTypes/CharacterString.php delete mode 100644 src/LibDNS/DataTypes/DomainName.php delete mode 100644 src/LibDNS/DataTypes/IPv4Address.php delete mode 100644 src/LibDNS/DataTypes/IPv6Address.php delete mode 100644 src/LibDNS/DataTypes/Long.php delete mode 100644 src/LibDNS/DataTypes/Short.php delete mode 100644 src/LibDNS/DataTypes/Vector.php delete mode 100644 src/LibDNS/DataTypes/Vectors/AFSDB.php delete mode 100644 src/LibDNS/DataTypes/Vectors/HINFO.php delete mode 100644 src/LibDNS/DataTypes/Vectors/ISDN.php delete mode 100644 src/LibDNS/DataTypes/Vectors/MINFO.php delete mode 100644 src/LibDNS/DataTypes/Vectors/MX.php delete mode 100644 src/LibDNS/DataTypes/Vectors/RP.php delete mode 100644 src/LibDNS/DataTypes/Vectors/RT.php delete mode 100644 src/LibDNS/DataTypes/Vectors/SOA.php delete mode 100644 src/LibDNS/DataTypes/Vectors/WKS.php delete mode 100644 src/LibDNS/Message.php delete mode 100644 src/LibDNS/Messages/Request.php delete mode 100644 src/LibDNS/Messages/Response.php delete mode 100644 src/LibDNS/Packet.php delete mode 100644 src/LibDNS/PacketBuilder/PacketBuilder.php delete mode 100644 src/LibDNS/PacketBuilder/WriteBlock.php delete mode 100644 src/LibDNS/Record.php delete mode 100644 src/LibDNS/Records/Query.php delete mode 100644 src/LibDNS/Records/Resource.php diff --git a/examples/AQuery.php b/examples/AQuery.php deleted file mode 100644 index 381eb88..0000000 --- a/examples/AQuery.php +++ /dev/null @@ -1,42 +0,0 @@ -addQuestionRecord($questionRecord); - $request->isRecursionDesired(TRUE); - - // Create the client socket - $socket = stream_socket_client("udp://$serverIP:53"); - - // Send the request - stream_socket_sendto($socket, $request->writeToPacket()); - - // Wait for the response - $r = array($socket); - $w = $e = array(); - stream_select($r, $w, $e, NULL); - - // Load the response - $packet = new Packet(fread($socket, 512)); - $response = new Response; - $response->loadFromPacket($packet); - - // Display the results - foreach ($response->getAnswerRecords() as $record) { - echo $record->getData()."\n"; - } diff --git a/examples/BasicServer.php b/examples/BasicServer.php deleted file mode 100644 index 06508bd..0000000 --- a/examples/BasicServer.php +++ /dev/null @@ -1,460 +0,0 @@ -lookupQuestionFactory = $lookupQuestionFactory; - $this->packetFactory = $packetFactory; - $this->requestFactory = $requestFactory; - $this->responseFactory = $responseFactory; - $this->resourceFactory = $resourceFactory; - $this->remoteAddr = $remoteAddr; - $this->relayServerAddr = $relayServerAddr; - $this->relayServerPort = $relayServerPort; - } - - private function createSocket() - { - $uri = "udp://$this->relayServerAddr:$this->relayServerPort"; - if (!$this->socket = stream_socket_client($uri, $errNo, $errStr)) { - throw new \RuntimeException("Creating server socket failed: $errNo: $errStr"); - } - } - - private function getNextId() - { - $result = $this->idCounter++; - - if ($this->idCounter >= 65536) { - $this->idCounter = 0; - } - - return $result; - } - - public function resolve(Query $question) - { - $lookupQuestion = $this->lookupQuestionFactory->create($question); - - if ($question->getType() === Resource::TYPE_A) { - $lookupQuestion->setAnswer($this->resourceFactory->create($question->getName(), Resource::TYPE_A, Resource::CLASS_IN, 1440, $this->remoteAddr)); - } else { - $id = $this->getNextId(); - $this->pendingRelayQuestions[$id] = $lookupQuestion; - - $request = $this->requestFactory->create($id); - $request->addQuestionRecord($question); - $request->isRecursionDesired(true); - - if (!isset($this->socket)) { - $this->createSocket(); - } - stream_socket_sendto($this->socket, $request->writeToPacket()); - } - - return $lookupQuestion; - } - - public function getSocket() - { - if (!isset($this->socket)) { - $this->createSocket(); - } - - return $this->socket; - } - - public function processPendingResponses() - { - do { - $data = stream_socket_recvfrom($this->socket, 512); - $packet = $this->packetFactory->create($data); - - $response = $this->responseFactory->create(); - $response->loadFromPacket($packet); - $id = $response->getID(); - - $answers = $response->getAnswerRecords(); - $answer = count($answers) > 0 ? $answers[0] : null; - - $this->pendingRelayQuestions[$id]->setAnswer($answer); - unset($this->pendingRelayQuestions[$id]); - - $r = [$this->socket]; - $w = $e = null; - } while (stream_select($r, $w, $e, 0)); - } - } - - class LookupRequest - { - private $resolver; - private $responseFactory; - - private $request; - private $clientAddress; - - private $timeout; - - private $lookupQuestions = []; - - public function __construct(Resolver $resolver, ResponseFactory $responseFactory, Request $request, $clientAddress, $timeout) - { - $this->resolver = $resolver; - $this->responseFactory = $responseFactory; - $this->request = $request; - $this->clientAddress = $clientAddress; - - $this->timeout = microtime(true) + $timeout; - } - - public function resolve() - { - foreach ($this->request->getQuestionRecords() as $question) { - $this->lookupQuestions[] = $this->resolver->resolve($question); - } - } - - public function isResolved() - { - $result = true; - - foreach ($this->lookupQuestions as $lookupQuestion) { - if (!$lookupQuestion->isResolved()) { - $result = false; - break; - } - } - - return $result; - } - - public function isTimedOut() - { - return microtime(true) >= $this->timeout; - } - - public function getClientAddress() - { - return $this->clientAddress; - } - - public function getResponse() - { - $response = $this->responseFactory->create($this->request->getID()); - - foreach ($this->lookupQuestions as $lookupQuestion) { - $question = $lookupQuestion->getQuestion(); - $answer = $lookupQuestion->getAnswer(); - - $response->addQuestionRecord($question); - if ($answer) { - $response->addAnswerRecord($answer); - } - } - - return $response; - } - } - - class LookupRequestFactory - { - private $resolver; - private $responseFactory; - private $defaultTimeout; - - public function __construct(Resolver $resolver, ResponseFactory $responseFactory, $defaultTimeout = 2) - { - $this->resolver = $resolver; - $this->responseFactory = $responseFactory; - $this->defaultTimeout = $defaultTimeout; - } - - public function create(Request $request, $clientAddress, $timeout = null) - { - return new LookupRequest($this->resolver, $this->responseFactory, $request, $clientAddress, $timeout ?: $this->defaultTimeout); - } - } - - class LookupQuestion - { - private $question; - private $answer; - - private $haveAnswer = false; - - public function __construct(Query $question) - { - $this->question = $question; - } - - public function setAnswer(Resource $answer = null) - { - $this->answer = $answer; - $this->haveAnswer = true; - } - - public function getAnswer() - { - return $this->answer; - } - - public function getQuestion() - { - return $this->question; - } - - public function isResolved() - { - return $this->haveAnswer; - } - } - - class LookupQuestionFactory - { - public function create(Query $question) - { - return new LookupQuestion($question); - } - } - - class ResourceFactory - { - public function create($name, $type, $class, $ttl, DataType $data) - { - return new Resource($name, $type, $class, $ttl, $data); - } - } - - class RequestFactory - { - public function create($id = null) - { - return new Request($id); - } - } - - class ResponseFactory - { - public function create($id = null) - { - return new Response($id); - } - } - - class PacketFactory - { - public function create($data) - { - return new Packet($data); - } - } - - class Server - { - private $packetFactory; - private $requestFactory; - private $lookupRequestFactory; - - private $address; - private $port; - - private $socket; - - public function __construct(PacketFactory $packetFactory, RequestFactory $requestFactory, LookupRequestFactory $lookupRequestFactory, $address, $port = 53) - { - $this->packetFactory = $packetFactory; - $this->requestFactory = $requestFactory; - $this->lookupRequestFactory = $lookupRequestFactory; - - $this->address = $address; - $this->port = $port; - } - - public function listen() - { - $this->socket = stream_socket_server("udp://$this->address:$this->port", $errNo, $errStr, STREAM_SERVER_BIND); - } - - public function getSocket() - { - return $this->socket; - } - - public function getPendingRequest() - { - $data = stream_socket_recvfrom($this->socket, 512, 0, $clientAddress); - $packet = $this->packetFactory->create($data); - - $request = $this->requestFactory->create(); - $request->loadFromPacket($packet); - - return $this->lookupRequestFactory->create($request, $clientAddress); - } - - public function sendResponse(LookupRequest $lookupRequest) - { - $packet = $lookupRequest->getResponse()->writeToPacket(); - file_put_contents('packet.bin', $packet); - stream_socket_sendto($this->socket, $lookupRequest->getResponse()->writeToPacket(), 0, $lookupRequest->getClientAddress()); - } - } - - class LookupRequestCollection implements \Iterator - { - private $requests = []; - private $position = 0; - - public function add(LookupRequest $request) - { - $this->requests[] = $request; - } - - public function remove(LookupRequest $request) - { - if (false !== $key = array_search($request, $this->requests, true)) { - array_splice($this->requests, $key, 1); - } - } - - public function rewind() { - $this->position = 0; - } - - public function current() { - return $this->requests[$this->position]; - } - - public function key() { - return $this->position; - } - - public function next() { - $this->position++; - } - - public function valid() { - return isset($this->requests[$this->position]); - } - } - - $packetFactory = new PacketFactory; - $requestFactory = new RequestFactory; - $responseFactory = new ResponseFactory; - - $resolver = new Resolver( - new LookupQuestionFactory, - $packetFactory, - $requestFactory, - $responseFactory, - new ResourceFactory, - new IPv4Address($remoteIP), - $relayServerIP - ); - - $server = new Server( - $packetFactory, - $requestFactory, - new LookupRequestFactory($resolver, $responseFactory), - $localIP - ); - - $pendingRequests = new LookupRequestCollection; - - - function __autoload($className) { - include str_replace('\\', '/', preg_replace('#^\\\\?DaveRandom#i', '../src', $className)).'.php'; - } - - - $server->listen(); - $interval = (int) (1000000 / $heartbeat); - - echo "Server running\n"; - - while (TRUE) { - - $r = [$server->getSocket(), $resolver->getSocket()]; - $w = $e = []; - stream_select($r, $w, $e, 0, $interval); - - if (in_array($server->getSocket(), $r)) { - $lookupRequest = $server->getPendingRequest(); - echo "Received request from client " . $lookupRequest->getClientAddress() . "\n"; - - $pendingRequests->add($lookupRequest); - $lookupRequest->resolve(); - } - - if (in_array($resolver->getSocket(), $r)) { - echo "Received response from relay server\n";; - $resolver->processPendingResponses(); - } - - $respond = []; - foreach ($pendingRequests as $lookupRequest) { - if ($lookupRequest->isResolved()) { - echo "Request from " . $lookupRequest->getClientAddress() . " is resolved\n"; - $respond[] = $lookupRequest; - } else if ($lookupRequest->isTimedOut()) { - echo "Request from " . $lookupRequest->getClientAddress() . " has timed out\n"; - $respond[] = $lookupRequest; - } - } - - foreach ($respond as $lookupRequest) { - echo "Sending response to " . $lookupRequest->getClientAddress() . "\n"; - $server->sendResponse($lookupRequest); - $pendingRequests->remove($lookupRequest); - } - - } diff --git a/examples/SOAQuery.php b/examples/SOAQuery.php deleted file mode 100644 index 12d96ca..0000000 --- a/examples/SOAQuery.php +++ /dev/null @@ -1,41 +0,0 @@ -addQuestionRecord($questionRecord); - $request->isRecursionDesired(TRUE); - - // Create the client socket - $socket = stream_socket_client("udp://$serverIP:53"); - - // Send the request - stream_socket_sendto($socket, $request->writeToPacket()); - - // Wait for the response - $r = array($socket); - $w = $e = array(); - stream_select($r, $w, $e, NULL); - - // Load the response - $packet = new Packet(fread($socket, 512)); - $response = new Response; - $response->loadFromPacket($packet); - - // Display the results - $record = $response->getAnswerRecords()[0]; - echo $record->getName().': '.$record->getData()."\n"; diff --git a/src/LibDNS/DataType.php b/src/LibDNS/DataType.php deleted file mode 100644 index 1ad0a9e..0000000 --- a/src/LibDNS/DataType.php +++ /dev/null @@ -1,40 +0,0 @@ -getFormattedData(); - } - - public function loadFromPacket(Packet $packet, $dataLength = NULL) { - if (FALSE === $data = $packet->read($dataLength)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct($data); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock($withLengthWord) - ->write($this->getRawData()); - } - - public function __construct($data = NULL) { - if ($data !== NULL) { - $this->setData($data); - } - } - - abstract public function getRawData(); - - abstract public function getFormattedData(); - - abstract public function setData($data); - - } diff --git a/src/LibDNS/DataTypes/Anything.php b/src/LibDNS/DataTypes/Anything.php deleted file mode 100644 index 6a9bff9..0000000 --- a/src/LibDNS/DataTypes/Anything.php +++ /dev/null @@ -1,29 +0,0 @@ -data; - } - - public function getFormattedData() { - return $this->data; - } - - public function setData($data) { - if (!is_scalar($data) && $data !== NULL && !(is_object($data) && method_exists($data, '__toString'))) { - throw new \InvalidArgumentException('Invalid data type'); - } else if (strlen($data) > 65535) { - throw new \InvalidArgumentException('Maximum length of 65535 bytes exceded'); - } - $this->data = (string) $data; - } - - } diff --git a/src/LibDNS/DataTypes/BitMap.php b/src/LibDNS/DataTypes/BitMap.php deleted file mode 100644 index 5481f84..0000000 --- a/src/LibDNS/DataTypes/BitMap.php +++ /dev/null @@ -1,81 +0,0 @@ -createStringFromMap($this->map); - } - - public function getFormattedData() { - $data = $this->createStringFromMap($this->map); - return call_user_func_array( - 'sprintf', - array_merge( - array(str_repeat('%08b', strlen($data))), - array_values(unpack('C*', $data)) - ) - ); - } - - public function setData($data) { - $map = array(); - if (is_array($data)) { - foreach ($data as $bit) { - $map[] = (int)(bool)$bit; - } - } else if (is_string($data)) { - foreach (unpack('C*', $data) as $char) { - for ($i = 7; $i >=0; $i--) { - $map[] = ($char >> $i) & 0x01; - } - } - } else if (is_int($data)) { - for ($i = round(log(PHP_INT_MAX, 2)); $i >= 0; $i--) { - $map[] = ($data >> $i) & 0x01; - } - } else { - throw new \InvalidArgumentException('Invalid data type'); - } - return $map; - } - - public function getMap() { - return $this->map; - } - - public function valueAtPosition($position, $newValue = NULL) { - if ($newValue = NULL) { - $result = isset($this->map) ? $this->map[$position] : 0; - } else { - $this->map[$position] = (int)(bool) $newValue; - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Char.php b/src/LibDNS/DataTypes/Char.php deleted file mode 100644 index 8e65289..0000000 --- a/src/LibDNS/DataTypes/Char.php +++ /dev/null @@ -1,40 +0,0 @@ -read(1)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct(ord($data)); - return $this; - } - - public function writeToPacket(Packet $packet, $withLengthWord = FALSE) { - $packet->prepareWriteBlock($withLengthWord)->write(chr($this->char)); - } - - public function getFormattedData() { - return $this->char; - } - - public function setData() { - if (is_scalar($char) || $char === NULL) { - $char = (int) $char; - if ($char < 0 || $char > 255) { - throw new \InvalidArgumentException('Value outside acceptable range for an unsigned char'); - } - $this->char = $char; - } else { - throw new \InvalidArgumentException('Invalid data type'); - } - } - - } diff --git a/src/LibDNS/DataTypes/CharacterString.php b/src/LibDNS/DataTypes/CharacterString.php deleted file mode 100644 index cc06ee7..0000000 --- a/src/LibDNS/DataTypes/CharacterString.php +++ /dev/null @@ -1,43 +0,0 @@ -read(1))) || (false === $data = $packet->read($length))) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct($data); - return $this; - } - - public function getRawData() { - return chr($this->length).$this->string; - } - - public function getFormattedData() { - return $this->string; - } - - public function setData($string = NULL) { - if (!is_scalar($string) && $string !== NULL && !(is_object($string) && method_exists($string, '__toString'))) { - throw new \InvalidArgumentException('Invalid data type'); - } else if (($length = strlen($string)) > 255) { - throw new \InvalidArgumentException('Maximum length of 255 characters exceded'); - } - $this->length = $length; - $this->string = (string) $string; - } - - public function getLength() { - return $this->length; - } - - } diff --git a/src/LibDNS/DataTypes/DomainName.php b/src/LibDNS/DataTypes/DomainName.php deleted file mode 100644 index a1517dd..0000000 --- a/src/LibDNS/DataTypes/DomainName.php +++ /dev/null @@ -1,87 +0,0 @@ -getLength()) { - if (FALSE === $length = $packet->readAbsolute($position++, 1)) { - throw new \InvalidArgumentException('Malformed packet'); - } - if (!$length = ord($length)) { - $complete = TRUE; - break; - } - if (($length & 0xC0) === 0xC0) { - if (FALSE === $pointer = $packet->readAbsolute($position - 1, 2)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $offset = current(unpack('n', $pointer)) & 0x3FFF; - $name = array_merge($name, $this->recordNameToArray($packet, $offset)); - $position++; - $complete = TRUE; - break; - } else { - if (FALSE === $name[] = $packet->readAbsolute($position, $length)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $position += $length; - } - } - if (!$complete) { - throw new \UnexpectedValueException('Malformed domain name data at the specified offset'); - } - return $name; - } - - public function loadFromPacket(Packet $packet, $dataLength = NULL) { - $position = $packet->tell(); - $data = $this->recordNameToArray($packet, $position); - $packet->seek($position); - $this->__construct($data); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock($withLengthWord) - ->writeDomainName($this); - } - - public function getRawData() { - $result = ''; - foreach ($this->tokens as $label) { - $result .= pack('C', strlen($label)).$label; - } - $result .= "\x00"; - return $result; - } - - public function getFormattedData() { - return implode('.', $this->tokens); - } - - public function setData($name) { - if (is_array($name)) { - $this->tokens = $name; - } else if (is_string($name)) { - $this->tokens = explode('.', trim($name)); - } else { - throw new \InvalidArgumentException('Invalid data type'); - } - } - - public function getTokens() { - return $this->tokens; - } - - } diff --git a/src/LibDNS/DataTypes/IPv4Address.php b/src/LibDNS/DataTypes/IPv4Address.php deleted file mode 100644 index c2b28af..0000000 --- a/src/LibDNS/DataTypes/IPv4Address.php +++ /dev/null @@ -1,64 +0,0 @@ -> 24) & 0xFF, - ($long >> 16) & 0xFF, - ($long >> 8) & 0xFF, - $long & 0xFF - ); - } - private function validateOctetValue($octet) { - return $octet >= 0 && $octet <= 255; - } - - public function loadFromPacket(Packet $packet, $dataLength = 4) { - if ($dataLength !== 4 || FALSE === $data = $packet->read(4)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct(array_values(unpack('C*', $data))); - return $this; - } - - public function getRawData() { - return call_user_func_array('pack', array_merge(array('C*'), $this->octets)); - } - - public function getFormattedData() { - return implode('.', $this->octets); - } - - public function setData($address) { - if (is_int($address)) { - $octets = $this->longToOctets($address); - } else if (is_array($address)) { - $octets = $address; - } else if (is_string($address)) { - if (!filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - throw new \InvalidArgumentException('Invalid address data'); - } - $octets = explode('.', $address); - } else { - throw new \InvalidArgumentException('Invalid address data'); - } - $octets = array_filter(array_map('intval', $octets), array($this, 'validateOctetValue')); - if (count($octets) !== 4) { - throw new \InvalidArgumentException('Invalid address data'); - } - $this->octets = $octets; - } - - public function getOctets() { - return $this->octets; - } - - } diff --git a/src/LibDNS/DataTypes/IPv6Address.php b/src/LibDNS/DataTypes/IPv6Address.php deleted file mode 100644 index 62ddc03..0000000 --- a/src/LibDNS/DataTypes/IPv6Address.php +++ /dev/null @@ -1,98 +0,0 @@ - &$block) { - if (((int) $block) === 0) { - $zeroCounter++; - } else if ($zeroCounter) { - if ($zeroCounter > 1 && $zeroCounter > $zeroMax) { - $zeroMax = $zeroCounter; - $zeroPos = $i - $zeroCounter; - } - $zeroCounter = 0; - } - $block = dechex($block); - } - if ($zeroCounter > 1 && $zeroCounter > $zeroMax) { - $zeroMax = $zeroCounter; - $zeroPos = $i - $zeroCounter + 1; - } - if ($zeroPos > -1) { - $replace = array(''); - if ($zeroPos === 0 || $zeroPos + $zeroMax === 8) { - $replace[] = ''; - } - array_splice($blocks, $zeroPos, $zeroMax, $replace); - } - return implode(':', $blocks); - } - - private function validateBlockArray($blocks) { - $result = array_filter(array_map('intval', $blocks), array($this, 'validateBlockValue')); - return count($result) === 8 ? $result : FALSE; - } - private function validateBlockValue($block) { - return $block >= 0 && $block <= 65535; - } - - public function loadFromPacket(Packet $packet, $dataLength = 16) { - if ($dataLength !== 16 || FALSE === $data = $packet->read(16)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct(array_values(unpack('n*', $data))); - return $this; - } - - public function getRawData() { - return call_user_func_array('pack', array_merge(array('n*'), $this->blocks)); - } - - public function getFormattedData() { - return $this->blockArrayToAddressString($this->blocks); - } - - public function setData($address) { - if (is_string($address)) { - if (!filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - throw new \InvalidArgumentException('Invalid address data'); - } - $address = $this->addressStringToBlockArray($address); - } - if (!is_array($address) || !$blocks = $this->validateBlockArray($address)) { - throw new \InvalidArgumentException('Invalid address data'); - } - $this->blocks = $blocks; - } - - public function getBlocks() { - return $this->blocks; - } - - } diff --git a/src/LibDNS/DataTypes/Long.php b/src/LibDNS/DataTypes/Long.php deleted file mode 100644 index 06b1159..0000000 --- a/src/LibDNS/DataTypes/Long.php +++ /dev/null @@ -1,71 +0,0 @@ -= 0 && $long <= 0xFFFFFFFF; - } - } - private function longToOctets($long) { - return array( - ($long >> 24) & 0xFF, - ($long >> 16) & 0xFF, - ($long >> 8) & 0xFF, - $long & 0xFF - ); - } - private function octetsToLong($octets) { - $long = 0; - $long |= ($octets[0] << 24); - $long |= ($octets[1] << 16); - $long |= ($octets[2] << 8); - $long |= $octets[3]; - return $long; - } - - public function loadFromPacket(Packet $packet, $dataLength = 4) { - if ($dataLength !== 4 || FALSE === $data = $packet->read(4)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct(current(unpack('N', $data))); - return $this; - } - - public function getRawData() { - return call_user_func_array('pack', array_merge(array('C*'), $this->octets)); - } - - public function getFormattedData() { - return $this->octetsToLong($this->octets); - } - - public function setData($long) { - if (!is_int($long)) { - if (is_scalar($long) || $long === NULL) { - // 32bit-safe way of casting to int - $long = current(unpack('N', pack('N', (float) $long))); - } else { - throw new \InvalidArgumentException('Invalid data type'); - } - } - if (!$this->validateLongRange($long)) { - throw new \InvalidArgumentException('Value outside acceptable range for an unsigned long integer'); - } - $this->octets = $this->longToOctets($long); - } - - } diff --git a/src/LibDNS/DataTypes/Short.php b/src/LibDNS/DataTypes/Short.php deleted file mode 100644 index 70932e4..0000000 --- a/src/LibDNS/DataTypes/Short.php +++ /dev/null @@ -1,40 +0,0 @@ -read(2)) { - throw new \InvalidArgumentException('Malformed packet'); - } - $this->__construct(current(unpack('n', $data))); - return $this; - } - - public function getRawData() { - return pack('n', $this->short); - } - - public function getFormattedData() { - return $this->short; - } - - public function setData($short) { - if (is_scalar($short) || $short === NULL) { - $short = (int) $short; - if ($short < 0 || $short > 65535) { - throw new \InvalidArgumentException('Value outside acceptable range for an unsigned short integer'); - } - $this->short = $short; - } else { - throw new \InvalidArgumentException('Invalid data type'); - } - } - - } diff --git a/src/LibDNS/DataTypes/Vector.php b/src/LibDNS/DataTypes/Vector.php deleted file mode 100644 index 2f62941..0000000 --- a/src/LibDNS/DataTypes/Vector.php +++ /dev/null @@ -1,24 +0,0 @@ -constructRawData(); - } - - public function getFormattedData() { - return $this->constructFormattedData(); - } - - public function setData($data) {} - - abstract protected function constructRawData(); - - abstract protected function constructFormattedData(); - - } diff --git a/src/LibDNS/DataTypes/Vectors/AFSDB.php b/src/LibDNS/DataTypes/Vectors/AFSDB.php deleted file mode 100644 index a777fad..0000000 --- a/src/LibDNS/DataTypes/Vectors/AFSDB.php +++ /dev/null @@ -1,62 +0,0 @@ -loadFromPacket($packet); - $hostname = (new DomainName)->loadFromPacket($packet); - $this->__construct($subtype, $hostname); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock(TRUE) - ->write($this->subtypeData->getRawData()) - ->writeDomainName($this->hostnameData); - } - - public function __construct($subtype = NULL, $hostname = NULL) { - $this->subtypeData = $subtype instanceof Short ? $subtype : new Short($subtype); - $this->hostnameData = $hostname instanceof DomainName ? $hostname : new DomainName($hostname); - } - - protected function constructRawData() { - return $this->subtypeData->getRawData().$this->hostnameData->getRawData(); - } - - protected function constructFormattedData() { - return 'Subtype: '.$this->subtypeData->getFormattedData().' Host: '.$this->hostnameData->getFormattedData(); - } - - public function subType($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->subtypeData; - } else { - $this->subtypeData = $newValue instanceof Short ? $newValue : new Short($newValue); - $result = $this; - } - return $result; - } - - public function hostName($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->hostnameData; - } else { - $this->hostnameData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/HINFO.php b/src/LibDNS/DataTypes/Vectors/HINFO.php deleted file mode 100644 index b1815e9..0000000 --- a/src/LibDNS/DataTypes/Vectors/HINFO.php +++ /dev/null @@ -1,54 +0,0 @@ -loadFromPacket($packet); - $os = (new CharacterString)->loadFromPacket($packet); - $this->__construct($cpu, $os); - return $this; - } - - public function __construct($cpu = NULL, $os = NULL) { - $this->cpuData = $cpu instanceof CharacterString ? $cpu : new CharacterString($cpu); - $this->osData = $os instanceof CharacterString ? $os : new CharacterString($os); - } - - protected function constructRawData() { - return $this->cpuData->getRawData().$this->osData->getRawData(); - } - - protected function constructFormattedData() { - return $this->cpuData->getFormattedData().'/'.$this->osData->getFormattedData(); - } - - public function cpu($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->cpuData; - } else { - $this->cpuData = $newValue instanceof CharacterString ? $newValue : new CharacterString($newValue); - $result = $this; - } - return $result; - } - - public function os($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->osData; - } else { - $this->osData = $newValue instanceof CharacterString ? $newValue : new CharacterString($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/ISDN.php b/src/LibDNS/DataTypes/Vectors/ISDN.php deleted file mode 100644 index 5b0827c..0000000 --- a/src/LibDNS/DataTypes/Vectors/ISDN.php +++ /dev/null @@ -1,54 +0,0 @@ -loadFromPacket($packet); - $subAddress = (new CharacterString)->loadFromPacket($packet); - $this->__construct($isdnAddress, $subAddress); - return $this; - } - - public function __construct($isdnAddress = NULL, $subAddress = NULL) { - $this->isdnAddressData = $isdnAddress instanceof CharacterString ? $isdnAddress : new CharacterString($isdnAddress); - $this->subAddressData = $subAddress instanceof CharacterString ? $subAddress : new CharacterString($subAddress); - } - - protected function constructRawData() { - return $this->isdnAddressData->getRawData().$this->subAddressData->getRawData(); - } - - protected function constructFormattedData() { - return $this->isdnAddressData->getFormattedData().'/'.$this->subAddressData->getFormattedData(); - } - - public function isdnAddress($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->isdnAddressData; - } else { - $this->isdnAddressData = $newValue instanceof CharacterString ? $newValue : new CharacterString($newValue); - $result = $this; - } - return $result; - } - - public function subAddress($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->subAddressData; - } else { - $this->subAddressData = $newValue instanceof CharacterString ? $newValue : new CharacterString($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/MINFO.php b/src/LibDNS/DataTypes/Vectors/MINFO.php deleted file mode 100644 index 633e573..0000000 --- a/src/LibDNS/DataTypes/Vectors/MINFO.php +++ /dev/null @@ -1,62 +0,0 @@ -loadFromPacket($packet); - $eMail = (new DomainName)->loadFromPacket($packet); - $this->__construct($rMail, $eMail); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock(TRUE) - ->writeDomainName($this->rMailData) - ->writeDomainName($this->eMailData); - } - - public function __construct($rMail = NULL, $eMail = NULL) { - $this->rMailData = $rMail instanceof DomainName ? $rMail : new DomainName($rMail); - $this->eMailData = $eMail instanceof DomainName ? $eMail : new DomainName($eMail); - } - - protected function constructRawData() { - return $this->rMailData->getRawData().$this->eMailData->getRawData(); - } - - protected function constructFormattedData() { - return 'Responsible: '.$this->rMailData->getFormattedData().' Error: '.$this->eMailData->getFormattedData(); - } - - public function responsibleMailAddress($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->rMailData; - } else { - $this->rMailData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - public function errorMailAddress($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->eMailData; - } else { - $this->eMailData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/MX.php b/src/LibDNS/DataTypes/Vectors/MX.php deleted file mode 100644 index 6c8e289..0000000 --- a/src/LibDNS/DataTypes/Vectors/MX.php +++ /dev/null @@ -1,63 +0,0 @@ -loadFromPacket($packet); - $exchange = (new DomainName)->loadFromPacket($packet); - $this->__construct($preference, $exchange); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock(TRUE) - ->write($this->preferenceData->getRawData()) - ->writeDomainName($this->exchangeData); - } - - public function __construct($preference = NULL, $exchange = NULL) { - $this->preferenceData = $preference instanceof Short ? $preference : new Short($preference); - $this->exchangeData = $exchange instanceof DomainName ? $exchange : new DomainName($exchange); - } - - protected function constructRawData() { - return $this->preferenceData->getRawData().$this->exchangeData->getRawData(); - } - - protected function constructFormattedData() { - return $this->preferenceData->getFormattedData().' '.$this->exchangeData->getFormattedData(); - } - - public function preference($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->preferenceData; - } else { - $this->preferenceData = $newValue instanceof Short ? $newValue : new Short($newValue); - $result = $this; - } - return $result; - } - - public function exchange($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->exchangeData; - } else { - $this->exchangeData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/RP.php b/src/LibDNS/DataTypes/Vectors/RP.php deleted file mode 100644 index bfd0c5e..0000000 --- a/src/LibDNS/DataTypes/Vectors/RP.php +++ /dev/null @@ -1,62 +0,0 @@ -loadFromPacket($packet); - $txt = (new DomainName)->loadFromPacket($packet); - $this->__construct($mail, $txt); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock(TRUE) - ->writeDomainName($this->mailData) - ->writeDomainName($this->txtData); - } - - public function __construct($mail = NULL, $txt = NULL) { - $this->mailData = $mail instanceof DomainName ? $mail : new DomainName($mail); - $this->txtData = $txt instanceof DomainName ? $txt : new DomainName($txt); - } - - protected function constructRawData() { - return $this->mailData->getRawData().$this->txtData->getRawData(); - } - - protected function constructFormattedData() { - return 'Mail: '.$this->mailData->getFormattedData().' TXT: '.$this->txtData->getFormattedData(); - } - - public function mailAddress($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->mailData; - } else { - $this->mailData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - public function txtLocation($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->txtData; - } else { - $this->txtData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/RT.php b/src/LibDNS/DataTypes/Vectors/RT.php deleted file mode 100644 index 97682b8..0000000 --- a/src/LibDNS/DataTypes/Vectors/RT.php +++ /dev/null @@ -1,63 +0,0 @@ -loadFromPacket($packet); - $intermediateHost = (new DomainName)->loadFromPacket($packet); - $this->__construct($preference, $intermediateHost); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock(TRUE) - ->write($this->preferenceData->getRawData()) - ->writeDomainName($this->intermediateHostData); - } - - public function __construct($preference = NULL, $intermediateHost = NULL) { - $this->preferenceData = $preference instanceof Short ? $preference : new Short($preference); - $this->intermediateHostData = $intermediateHost instanceof DomainName ? $intermediateHost : new DomainName($intermediateHost); - } - - protected function constructRawData() { - return $this->preferenceData->getRawData().$this->intermediateHostData->getRawData(); - } - - protected function constructFormattedData() { - return $this->preferenceData->getFormattedData().' '.$this->intermediateHostData->getFormattedData(); - } - - public function preference($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->preferenceData; - } else { - $this->preferenceData = $newValue instanceof Short ? $newValue : new Short($newValue); - $result = $this; - } - return $result; - } - - public function intermediateHost($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->intermediateHostData; - } else { - $this->intermediateHostData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/SOA.php b/src/LibDNS/DataTypes/Vectors/SOA.php deleted file mode 100644 index df6601d..0000000 --- a/src/LibDNS/DataTypes/Vectors/SOA.php +++ /dev/null @@ -1,147 +0,0 @@ -loadFromPacket($packet); - $rMail = (new DomainName)->loadFromPacket($packet); - $serial = (new Long)->loadFromPacket($packet); - $refresh = (new Long)->loadFromPacket($packet); - $retry = (new Long)->loadFromPacket($packet); - $expire = (new Long)->loadFromPacket($packet); - $minimum = (new Long)->loadFromPacket($packet); - $this->__construct($mName, $rMail, $serial, $refresh, $retry, $expire, $minimum); - return $this; - } - - public function __construct($mName = NULL, $rMail = NULL, $serial = NULL, $refresh = NULL, $retry = NULL, $expire = NULL, $minimum = NULL) { - $this->mNameData = $mName instanceof DomainName ? $mName : new DomainName($mName); - $this->rMailData = $rMail instanceof DomainName ? $rMail : new DomainName($rMail); - $this->serialData = $serial instanceof Long ? $serial : new Long($serial); - $this->refreshData = $refresh instanceof Long ? $refresh : new Long($refresh); - $this->retryData = $retry instanceof Long ? $retry : new Long($retry); - $this->expireData = $expire instanceof Long ? $expire : new Long($expire); - $this->minimumData = $minimum instanceof Long ? $minimum : new Long($minimum); - } - - public function writeToPacket(PacketBuilder $packetBuilder, $withLengthWord = FALSE) { - $packetBuilder - ->addWriteBlock(TRUE) - ->writeDomainName($this->mNameData) - ->writeDomainName($this->rMailData) - ->write($this->serialData->getRawData()) - ->write($this->refreshData->getRawData()) - ->write($this->retryData->getRawData()) - ->write($this->expireData->getRawData()) - ->write($this->minimumData->getRawData()); - } - - protected function constructRawData() { - return $this->mNameData->getRawData() - . $this->rMailData->getRawData() - . $this->serialData->getRawData() - . $this->refreshData->getRawData() - . $this->retryData->getRawData() - . $this->expireData->getRawData() - . $this->minimumData->getRawData(); - } - - protected function constructFormattedData() { - return "{\n" - . " Primary Name Server = " . $this->mNameData . "\n" - . " Responsible Mail Address = " . $this->rMailData . "\n" - . " Serial = " . $this->serialData . "\n" - . " Refresh = " . $this->refreshData . "\n" - . " Retry = " . $this->retryData . "\n" - . " Expire = " . $this->expireData . "\n" - . " Minimum = " . $this->minimumData . "\n" - . "}"; - } - - public function primaryNameServer($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->mNameData; - } else { - $this->mNameData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - public function responsibleMailAddress($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->rMailData; - } else { - $this->rMailData = $newValue instanceof DomainName ? $newValue : new DomainName($newValue); - $result = $this; - } - return $result; - } - - public function serial($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->serialData; - } else { - $this->serialData = $newValue instanceof Long ? $newValue : new Long($newValue); - $result = $this; - } - return $result; - } - - public function refresh($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->refreshData; - } else { - $this->refreshData = $newValue instanceof Long ? $newValue : new Long($newValue); - $result = $this; - } - return $result; - } - - public function retry($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->retryData; - } else { - $this->retryData = $newValue instanceof Long ? $newValue : new Long($newValue); - $result = $this; - } - return $result; - } - - public function expire($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->expireData; - } else { - $this->expireData = $newValue instanceof Long ? $newValue : new Long($newValue); - $result = $this; - } - return $result; - } - - public function minimum($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->minimumData; - } else { - $this->minimumData = $newValue instanceof Long ? $newValue : new Long($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/DataTypes/Vectors/WKS.php b/src/LibDNS/DataTypes/Vectors/WKS.php deleted file mode 100644 index 8f5b887..0000000 --- a/src/LibDNS/DataTypes/Vectors/WKS.php +++ /dev/null @@ -1,69 +0,0 @@ -loadFromPacket($packet); - $protocol = (new Char)->loadFromPacket($packet); - $services = (new BitMap)->loadFromPacket($packet, $dataLength - 5); - $this->__construct($address, $protocol, $services); - return $this; - } - - public function __construct($address = NULL, $protocol = NULL, $services = NULL) { - $this->addressData = $address instanceof IPv4Address ? $address : new IPv4Address($address); - $this->protocolData = $protocol instanceof Char ? $protocol : new Char($protocol); - $this->servicesData = $services instanceof BitMap ? $services : new BitMap($services); - } - - protected function constructRawData() { - return $this->addressData->getRawData().$this->protocolData->getRawData().$this->servicesData->getRawData(); - } - - protected function constructFormattedData() { - return $this->addressData->getFormattedData().' ('.$this->protocolData->getFormattedData().'): '.$this->servicesData->getFormattedData(); - } - - public function address($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->addressData; - } else { - $this->addressData = $newValue instanceof IPv4Address ? $newValue : new IPv4Address($newValue); - $result = $this; - } - return $result; - } - - public function protocol($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->protocolData; - } else { - $this->protocolData = $newValue instanceof Char ? $newValue : new Char($newValue); - $result = $this; - } - return $result; - } - - public function services($newValue = NULL) { - if ($newValue === NULL) { - $result = $this->servicesData; - } else { - $this->servicesData = $newValue instanceof BitMap ? $newValue : new BitMap($newValue); - $result = $this; - } - return $result; - } - - } diff --git a/src/LibDNS/Message.php b/src/LibDNS/Message.php deleted file mode 100644 index f50b061..0000000 --- a/src/LibDNS/Message.php +++ /dev/null @@ -1,297 +0,0 @@ - 'No error', - 1 => 'Format error', - 2 => 'Server error', - 3 => 'Name error', - 4 => 'Not implemented', - 5 => 'Refused' - ); - - protected $id = 0; - protected $type; - protected $opCode; - protected $authoritative = FALSE; - protected $truncated = FALSE; - protected $recursionDesired = FALSE; - protected $recursionAvailable = FALSE; - protected $responseCode = 0; - - protected $questionRecords = array(); - protected $answerRecords = array(); - protected $nameServerRecords = array(); - protected $additionalRecords = array(); - - public function __construct($id = NULL) { - if ($id !== NULL) { - $this->id = (int) $id; - } - } - - public function loadFromPacket(Packet $packet) { - if (FALSE === $header = $packet->read(12)) { - throw new \InvalidArgumentException('Malformed packet'); - } - list($id, $meta, $qdCount, $anCount, $nsCount, $arCount) = array_values(unpack('n*', $header)); - - $this->id = $id; - - $this->type = ($meta >> 15) & 0x0001; - $this->opCode = ($meta >> 11) & 0x000F; - $this->authoritative = (bool) (($meta >> 10) & 0x0001); - $this->truncated = (bool) (($meta >> 9) & 0x0001); - $this->recursionDesired = (bool) (($meta >> 8) & 0x0001); - $this->recursionAvailable = (bool) (($meta >> 7) & 0x0001); - $this->responseCode = $meta & 0x000F; - - for ($i = 0; $i < $qdCount; $i++) { - $this->questionRecords[] = (new Query)->loadFromPacket($packet); - } - - for ($i = 0; $i < $anCount; $i++) { - $this->answerRecords[] = (new Resource)->loadFromPacket($packet); - } - - for ($i = 0; $i < $nsCount; $i++) { - $this->nameServerRecords[] = (new Resource)->loadFromPacket($packet); - } - - for ($i = 0; $i < $arCount; $i++) { - $this->additionalRecords[] = (new Resource)->loadFromPacket($packet); - } - - return $this; - } - public function writeToPacket(Packet $packet = NULL) { - $packetBuilder = new PacketBuilder($this->compression); - $this->truncated = FALSE; - - $headerBlock = $packetBuilder->addWriteBlock(); - $headerBlock->write("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); - - try { - foreach ($this->questionRecords as $record) { - $record->writeToPacket($packetBuilder); - } - foreach ($this->answerRecords as $record) { - $record->writeToPacket($packetBuilder); - } - foreach ($this->nameServerRecords as $record) { - $record->writeToPacket($packetBuilder); - } - foreach ($this->additionalRecords as $record) { - $record->writeToPacket($packetBuilder); - } - } catch (\Exception $e) { - $this->truncated = TRUE; - } - - $meta = $this->type << 15; - $meta |= $this->opCode << 11; - $meta |= ((int) $this->authoritative) << 10; - $meta |= ((int) $this->truncated) << 9; - $meta |= ((int) $this->recursionDesired) << 8; - $meta |= $this->responseCode; - - $qdCount = count($this->questionRecords); - $anCount = count($this->answerRecords); - $nsCount = count($this->nameServerRecords); - $arCount = count($this->additionalRecords); - $headerBlock->overwrite(pack('n*', $this->id, $meta, $qdCount, $anCount, $nsCount, $arCount)); - - return $packetBuilder->write($packet); - } - - public function isTruncated() { - return $this->truncated; - } - - public function getQuestionRecords() { - return $this->questionRecords; - } - public function addQuestionRecord(Records\Query $record) { - foreach ($this->questionRecords as $key => $val) { - if ($val === $record) { - return $record; - } - } - $this->questionRecords[] = $record; - return $record; - } - public function removeQuestionRecord(Records\Query $record) { - foreach ($this->questionRecords as $key => $val) { - if ($val === $record) { - array_splice($this->questionRecords, $key, 1); - return $record; - } - } - } - - public function getAnswerRecords() { - return $this->answerRecords; - } - public function addAnswerRecord(Records\Resource $record) { - foreach ($this->answerRecords as $key => $val) { - if ($val === $record) { - return $record; - } - } - $this->answerRecords[] = $record; - return $record; - } - public function removeAnswerRecord(Records\Resource $record) { - foreach ($this->answerRecords as $key => $val) { - if ($val === $record) { - array_splice($this->answerRecords, $key, 1); - return $record; - } - } - } - - public function getNameServerRecords() { - return $this->nameServerRecords; - } - public function addNameServerRecord(Records\Resource $record) { - foreach ($this->nameServerRecords as $key => $val) { - if ($val === $record) { - return $record; - } - } - $this->nameServerRecords[] = $record; - return $record; - } - public function removeNameServerRecord(Records\Resource $record) { - foreach ($this->nameServerRecords as $key => $val) { - if ($val === $record) { - array_splice($this->nameServerRecords, $key, 1); - return $record; - } - } - } - - public function getAdditionalRecords() { - return $this->additionalRecords; - } - public function addAdditionalRecord(Records\Resource $record) { - foreach ($this->additionalRecords as $key => $val) { - if ($val === $record) { - return $record; - } - } - $this->additionalRecords[] = $record; - return $record; - } - public function removeAdditionalRecord(Records\Resource $record) { - foreach ($this->additionalRecords as $key => $val) { - if ($val === $record) { - array_splice($this->additionalRecords, $key, 1); - return $record; - } - } - } - - public function enableCompression($newValue) { - $this->compression = (bool) $newValue; - return $this; - } - - public function getID() { - return $this->id; - } - public function setID($newValue) { - if (!is_scalar($newValue) && $newValue !== NULL) { - throw new \InvalidArgumentException('Invalid data type'); - } - $newValue = (int) $newValue; - if ($newValue < 0 || $newValue > 65535) { - throw new \InvalidArgumentException('Value outside acceptable range for an unsigned short integer'); - } - $this->id = $newValue; - return $this; - } - - public function getType() { - return $this->type; - } - - public function isAuthoritative($newValue = NULL) { - $oldValue = $this->authoritative; - if ($newValue !== NULL) { - $this->authoritative = (bool) $newValue; - } - return $oldValue; - } - - public function isRecursionDesired($newValue = NULL) { - $oldValue = $this->recursionDesired; - if ($newValue !== NULL) { - $this->recursionDesired = (bool) $newValue; - } - return $oldValue; - } - public function isRecursionAvailable($newValue = NULL) { - $oldValue = $this->recursionAvailable; - if ($newValue !== NULL) { - $this->recursionAvailable = (bool) $newValue; - } - return $oldValue; - } - - public function getResponseCode() { - return $this->responseCode; - } - public function setResponseCode($newValue = NULL) { - if (!is_scalar($newValue) && $newValue !== NULL) { - throw new \InvalidArgumentException('Invalid data type'); - } - $newValue = (int) $newValue; - if ($newValue < 0 || $newValue > 5) { - throw new \InvalidArgumentException('Value is not a recognised response code'); - } - $this->responseCode = $newValue; - return $this; - } - public function getErrorMessage() { - return isset($this->responseMessages[$this->responseCode]) ? $this->responseMessages[$this->responseCode] : 'Unknown error'; - } - - public function getOpCode() { - return $this->opCode; - } - public function setOpCode($newValue) { - if (!is_scalar($newValue) && $newValue !== NULL) { - throw new \InvalidArgumentException('Invalid data type'); - } - $newValue = (int) $newValue; - if ($newValue < 1 || $newValue > 3) { - throw new \InvalidArgumentException('Value is not a recognised opcode'); - } - $this->opCode = $newValue; - return $this; - } - - } diff --git a/src/LibDNS/Messages/Request.php b/src/LibDNS/Messages/Request.php deleted file mode 100644 index 606836c..0000000 --- a/src/LibDNS/Messages/Request.php +++ /dev/null @@ -1,13 +0,0 @@ -loadMessage($data); - } else if ($data !== NULL) { - $this->loadString((string) $data); - } - } - public function __toString() { - return $this->getData(); - } - - public function getData() { - return $this->data; - } - public function getLength() { - return $this->length; - } - - public function loadMessage(Message $message) { - $message->writeToPacket($this); - return $this; - } - public function loadString($string) { - $this->write($string); - return $this; - } - - public function tell() { - return $this->pointer; - } - public function seek($position) { - $position = (int) $position; - if ($position >= $this->length) { - return FALSE; - } - if ($position < 0) { - $position += $this->length; - } - $this->pointer = $position; - return TRUE; - } - - public function read($length) { - if ($this->pointer + $length > $this->length) { - return FALSE; - } - $chunk = substr($this->data, $this->pointer, $length); - $this->pointer += $length; - return $chunk; - } - public function readAbsolute($start, $length) { - if ($start < 0) { - $start += $this->length; - } - if ($length < 0) { - $length += $this->length - $start; - } - if ($start >= $this->length || $start + $length > $this->length) { - return FALSE; - } - return substr($this->data, $start, $length); - } - - public function write($data) { - $this->data .= $data; - $this->length = strlen($this->data); - return strlen($data); - } - - } diff --git a/src/LibDNS/PacketBuilder/PacketBuilder.php b/src/LibDNS/PacketBuilder/PacketBuilder.php deleted file mode 100644 index a599085..0000000 --- a/src/LibDNS/PacketBuilder/PacketBuilder.php +++ /dev/null @@ -1,80 +0,0 @@ -enableCompression = $enableCompression; - } - - public function getLength() { - return $this->length; - } - public function getData() { - $data = ''; - while ($block = array_shift($this->writeBlocks)) { - $data .= $block->getData(); - } - return $data; - } - - public function addWriteBlock($hasLengthWord = FALSE) { - return $this->writeBlocks[] = new WriteBlock($this, (bool) $hasLengthWord); - } - public function removeWriteBlock(WriteBlock $block) { - for ($i = count($this->writeBlocks) - 1; $i >= 0; $i--) { - if ($block === $this->writeBlocks[$i]) { - array_splice($this->writeBlocks, $i, 1); - break; - } - } - } - - public function updateLength($length) { - $this->length += $length; - } - - public function write(Packet $packet = NULL) { - if ($packet === NULL) { - $packet = new Packet; - } - $packet->write($this->getData()); - return $packet; - } - - public function storeDomainName($tokens) { - $literals = array(); - $pointer = "\x00"; - $data = ''; - while ($tokens) { - if ($this->enableCompression) { - $name = implode('.', $tokens); - if (isset($this->domainNamePointers[$name])) { - $pointer = $this->domainNamePointers[$name]; - break; - } - } - $literals[] = array_shift($tokens); - } - $position = $this->length; - while ($literals) { - $this->domainNamePointers[implode('.', array_merge($literals, $tokens))] = pack('n', $position | 0xC000); - $token = array_shift($literals); - $tokenLength = strlen($token); - $data .= chr($tokenLength) . $token; - $position += $tokenLength + 1; - } - return $data . $pointer; - } - - } diff --git a/src/LibDNS/PacketBuilder/WriteBlock.php b/src/LibDNS/PacketBuilder/WriteBlock.php deleted file mode 100644 index 6cc0d48..0000000 --- a/src/LibDNS/PacketBuilder/WriteBlock.php +++ /dev/null @@ -1,61 +0,0 @@ -packetBuilder->updateLength($length); - if ($this->packetBuilder->getLength() > 512) { - $this->packetBuilder->removeWriteBlock($this); - throw new \Exception('Maximum packet length exceded'); - } - } - - public function __construct(PacketBuilder $packetBuilder, $hasLengthWord) { - $this->packetBuilder = $packetBuilder; - $this->hasLengthWord = $hasLengthWord; - if ($hasLengthWord) { - $this->packetBuilder->updateLength(2); - } - } - - public function getLength() { - return $this->length; - } - public function getData() { - $data = ''; - if ($this->hasLengthWord) { - $data .= pack('n', strlen($this->data)); - } - $data .= $this->data; - return $data; - } - - public function write($data) { - $this->data .= $data; - $this->processWriteLength(strlen($data)); - return $this; - } - public function overwrite($data) { - $lengthDiff = strlen($data) - strlen($this->data); - $this->data = $data; - if ($lengthDiff) { - $this->processWriteLength($lengthDiff); - } - return $this; - } - - public function writeDomainName(DomainName $name) { - return $this->write($this->packetBuilder->storeDomainName($name->getTokens())); - } - - } diff --git a/src/LibDNS/Record.php b/src/LibDNS/Record.php deleted file mode 100644 index 9cb9d16..0000000 --- a/src/LibDNS/Record.php +++ /dev/null @@ -1,79 +0,0 @@ -setName($name); - } - $this->setType($type); - $this->setClass($class); - } - - public function getName() { - return $this->name; - } - public function setName($name) { - if ($name instanceof DomainName) { - $this->name = $name; - } else { - $this->name = new DomainName($name); - } - } - - public function getType() { - return $this->type; - } - public function setType($type) { - $this->type = (int) $type; - } - - public function getClass() { - return $this->class; - } - public function setClass($class) { - $this->class = (int) $class; - } - - } diff --git a/src/LibDNS/Records/Query.php b/src/LibDNS/Records/Query.php deleted file mode 100644 index e018b66..0000000 --- a/src/LibDNS/Records/Query.php +++ /dev/null @@ -1,30 +0,0 @@ -loadFromPacket($packet); - if (FALSE === $meta = $packet->read(4)) { - throw new \InvalidArgumentException('Malformed packet'); - } - list($type, $class) = array_values(unpack('ntype/nclass', $meta)); - $this->__construct($name, $type, $class); - return $this; - } - - public function writeToPacket(PacketBuilder $packetBuilder) { - $packetBuilder - ->addWriteBlock() - ->writeDomainName($this->name) - ->write(pack('nn', $this->type, $this->class)); - } - - } diff --git a/src/LibDNS/Records/Resource.php b/src/LibDNS/Records/Resource.php deleted file mode 100644 index 6758102..0000000 --- a/src/LibDNS/Records/Resource.php +++ /dev/null @@ -1,103 +0,0 @@ - 'IPv4Address', // A - 2 => 'DomainName', // NS - 3 => 'DomainName', // MD - 4 => 'DomainName', // MF - 5 => 'DomainName', // CNAME - 6 => 'Vectors\\SOA', // SOA - 7 => 'DomainName', // MB - 8 => 'DomainName', // MG - 9 => 'DomainName', // MR - 10 => 'Anything', // NULL - 11 => 'Vectors\\WKS', // WKS - 12 => 'DomainName', // PTR - 13 => 'Vectors\\HINFO', // HINFO - 14 => 'Vectors\\MINFO', // MINFO - 15 => 'Vectors\\MX', // MX - 16 => 'CharacterString', // TXT - 17 => 'Vectors\\RP', // RP - 18 => 'Vectors\\AFSDB', // AFSDB - 19 => 'CharacterString', // X25 - 20 => 'Vectors\\ISDN', // ISDN - 21 => 'Vectors\\RT', // RT - 28 => 'IPv6Address' // AAAA - ); - - private function parseResourceHeader(Packet $packet) { - if (FALSE === $header = $packet->read(10)) { - throw new \InvalidArgumentException('Malformed packet'); - } - return array_values(unpack('ntype/nclass/Nttl/nlength', $header)); - } - private function createDataObject($packet, $dataLength, $type) { - $class = '\\DaveRandom\\LibDNS\\DataTypes\\'.$this->dataTypes[$type]; - $obj = new $class; - $obj->loadFromPacket($packet, $dataLength); - return $obj; - } - - public function loadFromPacket(Packet $packet) { - $name = new DomainName; - $name->loadFromPacket($packet); - list($type, $class, $ttl, $dataLength) = $this->parseResourceHeader($packet); - $data = $this->createDataObject($packet, $dataLength, $type); - $this->__construct($name, $type, $class, $ttl, $data); - return $this; - } - - public function __construct($name = NULL, $type = self::TYPE_A, $class = self::CLASS_IN, $ttl = NULL, DataType $data = NULL) { - parent::__construct($name, $type, $class); - if ($ttl !== NULL) { - $this->setTTL($ttl); - } - if ($data !== NULL) { - $this->setData($data); - } - } - - public function writeToPacket(PacketBuilder $packetBuilder) { - if ($this->ttl === NULL || $this->data === NULL) { - throw new \Exception('Data incomplete'); - } - try { - $headBlock = $packetBuilder - ->addWriteBlock() - ->writeDomainName($this->name) - ->write(pack('nnN', $this->type, $this->class, $this->ttl)); - $this->data->writeToPacket($packetBuilder, TRUE); - } catch (\Exception $e) { - $packetBuilder->removeWriteBlock($headBlock); - throw $e; - } - } - - public function getTTL() { - return $this->ttl; - } - public function setTTL($ttl) { - $this->ttl = (int) $ttl; - } - - public function getData() { - return $this->data; - } - public function setData(DataType $data) { - $this->data = $data; - } - - }