Skip to content

Commit 80eda9c

Browse files
committed
RR refactoring, add CERT record
1 parent 66ae4c3 commit 80eda9c

18 files changed

+260
-64
lines changed

src/Protocol/Decoding/ResourceDataDecoder.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
final class ResourceDataDecoder
1414
{
1515
const DEFAULT_DECODERS = [
16-
ResourceTypes::A => [ResourceData\A::class, 'decode'],
17-
ResourceTypes::AAAA => [ResourceData\AAAA::class, 'decode'],
18-
ResourceTypes::CNAME => [ResourceData\CNAME::class, 'decode'],
19-
ResourceTypes::DNAME => [ResourceData\DNAME::class, 'decode'],
20-
ResourceTypes::MX => [ResourceData\MX::class, 'decode'],
21-
ResourceTypes::NAPTR => [ResourceData\NAPTR::class, 'decode'],
22-
ResourceTypes::NS => [ResourceData\NS::class, 'decode'],
23-
ResourceTypes::PTR => [ResourceData\PTR::class, 'decode'],
24-
ResourceTypes::RP => [ResourceData\RP::class, 'decode'],
25-
ResourceTypes::SOA => [ResourceData\SOA::class, 'decode'],
26-
ResourceTypes::SRV => [ResourceData\SRV::class, 'decode'],
27-
ResourceTypes::TXT => [ResourceData\TXT::class, 'decode'],
16+
ResourceTypes::A => [ResourceData\A::class, 'protocolDecode'],
17+
ResourceTypes::AAAA => [ResourceData\AAAA::class, 'protocolDecode'],
18+
ResourceTypes::CNAME => [ResourceData\CNAME::class, 'protocolDecode'],
19+
ResourceTypes::DNAME => [ResourceData\DNAME::class, 'protocolDecode'],
20+
ResourceTypes::MX => [ResourceData\MX::class, 'protocolDecode'],
21+
ResourceTypes::NAPTR => [ResourceData\NAPTR::class, 'protocolDecode'],
22+
ResourceTypes::NS => [ResourceData\NS::class, 'protocolDecode'],
23+
ResourceTypes::PTR => [ResourceData\PTR::class, 'protocolDecode'],
24+
ResourceTypes::RP => [ResourceData\RP::class, 'protocolDecode'],
25+
ResourceTypes::SOA => [ResourceData\SOA::class, 'protocolDecode'],
26+
ResourceTypes::SRV => [ResourceData\SRV::class, 'protocolDecode'],
27+
ResourceTypes::TXT => [ResourceData\TXT::class, 'protocolDecode'],
2828
];
2929

3030
private static $callbackType;

src/Protocol/Encoding/ResourceDataEncoder.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
final class ResourceDataEncoder
1414
{
1515
const DEFAULT_ENCODERS = [
16-
ResourceTypes::A => [ResourceData\A::class, 'encode'],
17-
ResourceTypes::AAAA => [ResourceData\AAAA::class, 'encode'],
18-
ResourceTypes::CNAME => [ResourceData\CNAME::class, 'encode'],
19-
ResourceTypes::CNAME => [ResourceData\DNAME::class, 'encode'],
20-
ResourceTypes::MX => [ResourceData\MX::class, 'encode'],
21-
ResourceTypes::NAPTR => [ResourceData\NAPTR::class, 'encode'],
22-
ResourceTypes::NS => [ResourceData\NS::class, 'encode'],
23-
ResourceTypes::PTR => [ResourceData\PTR::class, 'encode'],
24-
ResourceTypes::RP => [ResourceData\RP::class, 'encode'],
25-
ResourceTypes::SOA => [ResourceData\SOA::class, 'encode'],
26-
ResourceTypes::SRV => [ResourceData\SRV::class, 'encode'],
27-
ResourceTypes::TXT => [ResourceData\TXT::class, 'encode'],
16+
ResourceTypes::A => [ResourceData\A::class, 'protocolEncode'],
17+
ResourceTypes::AAAA => [ResourceData\AAAA::class, 'protocolEncode'],
18+
ResourceTypes::CNAME => [ResourceData\CNAME::class, 'protocolEncode'],
19+
ResourceTypes::CNAME => [ResourceData\DNAME::class, 'protocolEncode'],
20+
ResourceTypes::MX => [ResourceData\MX::class, 'protocolEncode'],
21+
ResourceTypes::NAPTR => [ResourceData\NAPTR::class, 'protocolEncode'],
22+
ResourceTypes::NS => [ResourceData\NS::class, 'protocolEncode'],
23+
ResourceTypes::PTR => [ResourceData\PTR::class, 'protocolEncode'],
24+
ResourceTypes::RP => [ResourceData\RP::class, 'protocolEncode'],
25+
ResourceTypes::SOA => [ResourceData\SOA::class, 'protocolEncode'],
26+
ResourceTypes::SRV => [ResourceData\SRV::class, 'protocolEncode'],
27+
ResourceTypes::TXT => [ResourceData\TXT::class, 'protocolEncode'],
2828
];
2929

3030
private static $callbackType;

src/Records/ResourceData.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
namespace DaveRandom\LibDNS\Records;
44

5-
interface ResourceData { }
5+
interface ResourceData
6+
{
7+
function __toString(): string;
8+
}

src/Records/ResourceData/A.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function getAddress(): IPv4Address
2121
return $this->address;
2222
}
2323

24-
public static function decode(DecodingContext $ctx): A
24+
public function __toString(): string
2525
{
26-
return new A(\DaveRandom\LibDNS\decode_ipv4address($ctx));
26+
return self::zoneFileEncode($this);
2727
}
2828

29-
public static function encode(EncodingContext $ctx, A $record)
29+
public static function zoneFileEncode(self $record): string
30+
{
31+
return (string)$record->address;
32+
}
33+
34+
public static function protocolDecode(DecodingContext $ctx): self
35+
{
36+
return new self(\DaveRandom\LibDNS\decode_ipv4address($ctx));
37+
}
38+
39+
public static function protocolEncode(EncodingContext $ctx, self $record)
3040
{
3141
\DaveRandom\LibDNS\encode_ipv4address($ctx, $record->address);
3242
}

src/Records/ResourceData/AAAA.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function getAddress(): IPv6Address
2121
return $this->address;
2222
}
2323

24-
public static function decode(DecodingContext $ctx): AAAA
24+
public function __toString(): string
2525
{
26-
return new AAAA(\DaveRandom\LibDNS\decode_ipv6address($ctx));
26+
return self::zoneFileEncode($this);
2727
}
2828

29-
public static function encode(EncodingContext $ctx, AAAA $record)
29+
public static function zoneFileEncode(self $record): string
30+
{
31+
return (string)$record->address;
32+
}
33+
34+
public static function protocolDecode(DecodingContext $ctx): self
35+
{
36+
return new self(\DaveRandom\LibDNS\decode_ipv6address($ctx));
37+
}
38+
39+
public static function protocolEncode(EncodingContext $ctx, self $record)
3040
{
3141
\DaveRandom\LibDNS\encode_ipv6address($ctx, $record->address);
3242
}

src/Records/ResourceData/CERT.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DaveRandom\LibDNS\Records\ResourceData;
4+
5+
use DaveRandom\LibDNS\Protocol\DecodingContext;
6+
use DaveRandom\LibDNS\Protocol\EncodingContext;
7+
use DaveRandom\LibDNS\Records\ResourceData;
8+
9+
final class CERT implements ResourceData
10+
{
11+
private $type;
12+
private $keyTag;
13+
private $algorithm;
14+
private $certificate;
15+
16+
public function __construct(int $type, int $keyTag, int $algorithm, string $certificate)
17+
{
18+
$this->type = \DaveRandom\LibDNS\validate_uint16('Certificate type', $type);
19+
$this->keyTag = \DaveRandom\LibDNS\validate_uint16('Key tag', $keyTag);
20+
$this->algorithm = \DaveRandom\LibDNS\validate_byte('Certificate algorithm', $algorithm);
21+
$this->certificate = $certificate;
22+
}
23+
24+
public function getType(): int
25+
{
26+
return $this->type;
27+
}
28+
29+
public function getKeyTag(): int
30+
{
31+
return $this->keyTag;
32+
}
33+
34+
public function getAlgorithm(): int
35+
{
36+
return $this->algorithm;
37+
}
38+
39+
public function getCertificate(): string
40+
{
41+
return $this->certificate;
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return self::zoneFileEncode($this);
47+
}
48+
49+
public static function zoneFileEncode(self $record): string
50+
{
51+
return "{$record->type} {$record->keyTag}. {$record->algorithm} " . \base64_encode($record->certificate);
52+
}
53+
54+
public static function protocolDecode(DecodingContext $ctx, int $length): self
55+
{
56+
$certLength = $length - 5;
57+
$parts = $ctx->unpack("ntype/ntag/Calgo/Z{$certLength}cert", $length);
58+
59+
return new self($parts['type'], $parts['tag'], $parts['algo'], $parts['cert']);
60+
}
61+
62+
public static function protocolEncode(EncodingContext $ctx, self $record)
63+
{
64+
$ctx->appendData(\pack('n2CZ*', $record->type, $record->keyTag, $record->algorithm, $record->certificate));
65+
}
66+
}

src/Records/ResourceData/CNAME.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function getCanonicalName(): DomainName
2121
return $this->canonicalName;
2222
}
2323

24-
public static function decode(DecodingContext $ctx): CNAME
24+
public function __toString(): string
2525
{
26-
return new CNAME(\DaveRandom\LibDNS\decode_domain_name($ctx));
26+
return self::zoneFileEncode($this);
2727
}
2828

29-
public static function encode(EncodingContext $ctx, CNAME $record)
29+
public static function zoneFileEncode(self $record): string
30+
{
31+
return "{$record->canonicalName}.";
32+
}
33+
34+
public static function protocolDecode(DecodingContext $ctx): self
35+
{
36+
return new self(\DaveRandom\LibDNS\decode_domain_name($ctx));
37+
}
38+
39+
public static function protocolEncode(EncodingContext $ctx, self $record)
3040
{
3141
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->canonicalName);
3242
}

src/Records/ResourceData/DNAME.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function getCanonicalName(): DomainName
2121
return $this->canonicalName;
2222
}
2323

24-
public static function decode(DecodingContext $ctx): DNAME
24+
public function __toString(): string
2525
{
26-
return new DNAME(\DaveRandom\LibDNS\decode_domain_name($ctx));
26+
return self::zoneFileEncode($this);
2727
}
2828

29-
public static function encode(EncodingContext $ctx, DNAME $record)
29+
public static function zoneFileEncode(self $record): string
30+
{
31+
return "{$record->canonicalName}.";
32+
}
33+
34+
public static function protocolDecode(DecodingContext $ctx): self
35+
{
36+
return new self(\DaveRandom\LibDNS\decode_domain_name($ctx));
37+
}
38+
39+
public static function protocolEncode(EncodingContext $ctx, self $record)
3040
{
3141
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->getCanonicalName());
3242
}

src/Records/ResourceData/MX.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ public function getExchange(): DomainName
2828
return $this->exchange;
2929
}
3030

31-
public static function decode(DecodingContext $ctx): MX
31+
public function __toString(): string
32+
{
33+
return self::zoneFileEncode($this);
34+
}
35+
36+
public static function zoneFileEncode(self $record): string
37+
{
38+
return "{$record->preference} {$record->exchange}.";
39+
}
40+
41+
public static function protocolDecode(DecodingContext $ctx): self
3242
{
3343
$preference = $ctx->unpack('n', 2)[1];
3444
$exchange = \DaveRandom\LibDNS\decode_domain_name($ctx);
3545

36-
return new MX($preference, $exchange);
46+
return new self($preference, $exchange);
3747
}
3848

39-
public static function encode(EncodingContext $ctx, MX $record)
49+
public static function protocolEncode(EncodingContext $ctx, self $record)
4050
{
4151
$ctx->appendData(\pack('n', $record->getPreference()));
4252
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->getExchange());

src/Records/ResourceData/NAPTR.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,29 @@ public function getReplacement(): DomainName
5656
return $this->replacement;
5757
}
5858

59-
public static function decode(DecodingContext $ctx): NAPTR
59+
public function __toString(): string
60+
{
61+
return self::zoneFileEncode($this);
62+
}
63+
64+
public static function zoneFileEncode(self $record): string
65+
{
66+
return "{$record->order} {$record->preference} {$record->flags}"
67+
. " {$record->services} {$record->regex} {$record->replacement}.";
68+
}
69+
70+
public static function protocolDecode(DecodingContext $ctx): self
6071
{
6172
$parts = $ctx->unpack('norder/npreference', 4);
6273
$flags = \DaveRandom\LibDNS\decode_character_string($ctx);
6374
$services = \DaveRandom\LibDNS\decode_character_string($ctx);
6475
$regex = \DaveRandom\LibDNS\decode_character_string($ctx);
6576
$replacement = \DaveRandom\LibDNS\decode_domain_name($ctx);
6677

67-
return new NAPTR($parts['order'], $parts['preference'], $flags, $services, $regex, $replacement);
78+
return new self($parts['order'], $parts['preference'], $flags, $services, $regex, $replacement);
6879
}
6980

70-
public static function encode(EncodingContext $ctx, NAPTR $record)
81+
public static function protocolEncode(EncodingContext $ctx, self $record)
7182
{
7283
$ctx->appendData(\pack('n2', $record->order, $record->preference));
7384
\DaveRandom\LibDNS\encode_character_string($ctx, $record->flags);

src/Records/ResourceData/NS.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function getAuthoritativeServerName(): DomainName
2121
return $this->authoritativeServerName;
2222
}
2323

24-
public static function decode(DecodingContext $ctx): NS
24+
public function __toString(): string
2525
{
26-
return new NS(\DaveRandom\LibDNS\decode_domain_name($ctx));
26+
return self::zoneFileEncode($this);
2727
}
2828

29-
public static function encode(EncodingContext $ctx, NS $record)
29+
public static function zoneFileEncode(self $record): string
30+
{
31+
return "{$record->authoritativeServerName}.";
32+
}
33+
34+
public static function protocolDecode(DecodingContext $ctx): self
35+
{
36+
return new self(\DaveRandom\LibDNS\decode_domain_name($ctx));
37+
}
38+
39+
public static function protocolEncode(EncodingContext $ctx, self $record)
3040
{
3141
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->getAuthoritativeServerName());
3242
}

src/Records/ResourceData/PTR.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ public function getName(): DomainName
2121
return $this->name;
2222
}
2323

24-
public static function decode(DecodingContext $ctx): PTR
24+
public function __toString(): string
2525
{
26-
return new PTR(\DaveRandom\LibDNS\decode_domain_name($ctx));
26+
return self::zoneFileEncode($this);
2727
}
2828

29-
public static function encode(EncodingContext $ctx, PTR $record)
29+
public static function zoneFileEncode(self $record): string
30+
{
31+
return "{$record->name}.";
32+
}
33+
34+
public static function protocolDecode(DecodingContext $ctx): self
35+
{
36+
return new self(\DaveRandom\LibDNS\decode_domain_name($ctx));
37+
}
38+
39+
public static function protocolEncode(EncodingContext $ctx, self $record)
3040
{
3141
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->getName());
3242
}

src/Records/ResourceData/RP.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ public function getTxtName(): DomainName
2828
return $this->txtName;
2929
}
3030

31-
public static function decode(DecodingContext $ctx): RP
31+
public function __toString(): string
32+
{
33+
return self::zoneFileEncode($this);
34+
}
35+
36+
public static function zoneFileEncode(self $record): string
37+
{
38+
return "{$record->mailboxName}. {$record->txtName}.";
39+
}
40+
41+
public static function protocolDecode(DecodingContext $ctx): self
3242
{
3343
$mailboxName = \DaveRandom\LibDNS\decode_domain_name($ctx);
3444
$txtName = \DaveRandom\LibDNS\decode_domain_name($ctx);
3545

36-
return new RP($mailboxName, $txtName);
46+
return new self($mailboxName, $txtName);
3747
}
3848

39-
public static function encode(EncodingContext $ctx, RP $record)
49+
public static function protocolEncode(EncodingContext $ctx, self $record)
4050
{
4151
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->mailboxName);
4252
\DaveRandom\LibDNS\encode_domain_name($ctx, $record->txtName);

0 commit comments

Comments
 (0)