Commit 2f67fded authored by samuelwilliams's avatar samuelwilliams
Browse files

Update version constraint.

parents 8940dbd1 741121e1
......@@ -11,7 +11,7 @@
"require": {
"php": "~7.2",
"rlanvin/php-ip": "~2.0",
"ademarre/binary-to-text-php": "@dev"
"ademarre/binary-to-text-php": "dev-master#v2.0.0"
},
"require-dev": {
"phpunit/phpunit": "~8.4",
......
......@@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Badcow\DNS\Rdata;
use Badcow\DNS\Parser\Tokens;
use Badcow\DNS\Validator;
/**
* Class CaaRdata.
......@@ -30,7 +31,7 @@ class CAA implements RdataInterface
const TYPE = 'CAA';
const TYPE_CODE = 257;
const MAX_FLAG = 255;
const TAG_ISSUE = 'issue';
const TAG_ISSUEWILD = 'issuewild';
const TAG_IODEF = 'iodef';
......@@ -74,8 +75,8 @@ class CAA implements RdataInterface
*/
public function setFlag(int $flag): void
{
if ($flag < 0 || $flag > static::MAX_FLAG) {
throw new \InvalidArgumentException('Flag must be an unsigned integer on the range [0-255]');
if (!Validator::isUnsignedInteger($flag, 8)) {
throw new \InvalidArgumentException('Flag must be an unsigned 8-bit integer.');
}
$this->flag = $flag;
......@@ -136,6 +137,9 @@ class CAA implements RdataInterface
);
}
/**
* @return string
*/
public function toWire(): string
{
if (!isset($this->tag) || !isset($this->flag) || !isset($this->value)) {
......@@ -148,6 +152,11 @@ class CAA implements RdataInterface
$this->value;
}
/**
* {@inheritdoc}
*
* @return CAA
*/
public static function fromWire(string $rdata): RdataInterface
{
$caa = new self();
......@@ -159,6 +168,11 @@ class CAA implements RdataInterface
return $caa;
}
/**
* {@inheritdoc}
*
* @return CAA
*/
public static function fromText(string $string): RdataInterface
{
$caa = new self();
......
......@@ -15,10 +15,10 @@ namespace Badcow\DNS\Rdata;
class DecodeException extends \Exception
{
public function __construct(string $type, string $rdata)
public function __construct(string $type, string $rdata, int $code = 0, \Throwable $previous = null)
{
$message = sprintf('Unable to decode %s record rdata from binary data "%s"', $type, $this->binaryToHex($rdata));
parent::__construct($message);
parent::__construct($message, $code, $previous);
}
/**
......
......@@ -274,7 +274,6 @@ class IPSECKEY implements RdataInterface
array_shift($rdata); //Gateway type is inferred from setGateway.
$algorithm = (int) array_shift($rdata);
$ipseckey->setGateway((string) array_shift($rdata));
$publicKey = (0 === $algorithm) ? null : implode('', $rdata);
$ipseckey->setPublicKey($algorithm, $publicKey);
......@@ -285,6 +284,8 @@ class IPSECKEY implements RdataInterface
* {@inheritdoc}
*
* @return IPSECKEY
*
* @throws DecodeException
*/
public static function fromWire(string $rdata): RdataInterface
{
......@@ -296,37 +297,48 @@ class IPSECKEY implements RdataInterface
$gatewayType = $integers['GatewayType'];
$algorithm = $integers['Algorithm'];
$offset += 3;
$gateway = null;
$ipseckey->setGateway(self::extractGateway($gatewayType, $rdata, $offset));
if (self::ALGORITHM_NONE !== $algorithm) {
$publicKey = base64_encode(substr($rdata, $offset));
$ipseckey->setPublicKey($algorithm, $publicKey);
}
return $ipseckey;
}
/**
* @param int $gatewayType
* @param string $rdata
* @param int $offset
*
* @return string
*
* @throws DecodeException
*/
private static function extractGateway(int $gatewayType, string $rdata, int &$offset): string
{
switch ($gatewayType) {
case 0:
case 3:
$gateway = RdataTrait::decodeName($rdata, $offset);
break;
case 1:
$gateway = inet_ntop(substr($rdata, $offset, 4));
$offset += 4;
break;
case 2:
$gateway = inet_ntop(substr($rdata, $offset, 16));
$offset += 16;
$len = (1 === $gatewayType) ? 4 : 16;
$gateway = @inet_ntop(substr($rdata, $offset, $len));
$offset += $len;
break;
default:
throw new \InvalidArgumentException(sprintf('Expected gateway type to be integer on [0,3], got "%d".', $gatewayType));
$invalidArgumentException = new \InvalidArgumentException(sprintf('Expected gateway type to be integer on [0,3], got "%d".', $gatewayType));
throw new DecodeException(static::TYPE, $rdata, 0, $invalidArgumentException);
break;
}
if (false === $gateway) {
throw new \RuntimeException('Could not decode IP address.');
throw new DecodeException(static::TYPE, $rdata);
}
$ipseckey->setGateway($gateway);
if (self::ALGORITHM_NONE !== $algorithm) {
$publicKey = base64_encode(substr($rdata, $offset));
$ipseckey->setPublicKey($algorithm, $publicKey);
}
return $ipseckey;
return $gateway;
}
}
......@@ -36,10 +36,10 @@ class CaaTest extends TestCase
public function testFlagException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Flag must be an unsigned integer on the range [0-255]');
$this->expectExceptionMessage('Flag must be an unsigned 8-bit integer.');
$srv = new CAA();
$srv->setFlag(CAA::MAX_FLAG + 1);
$srv->setFlag(256);
}
/**
......
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