Skip to content

Commit

Permalink
Add TXT and RP types
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 19, 2025
1 parent bd32f7a commit 67406d2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/Fields/CharacterString.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function getValue(): string{
*/
public function serializeToPresentationFormat(): string{
$backslashEscapedValue = str_replace(self::BACKSLASH,self::BACKSLASH.self::BACKSLASH,$this->value);
$escapedValue = str_replace(self::QUOTE,self::BACKSLASH.self::QUOTE,$backslashEscapedValue);
//TODO Check only spaces, or also other whitespaces
if(str_contains($this->value,' ')){
$escapedValue = str_replace(self::QUOTE,self::BACKSLASH.self::QUOTE,$backslashEscapedValue);
return self::QUOTE.($escapedValue).self::QUOTE;
}
return $backslashEscapedValue;
return $escapedValue;
}

/**
Expand All @@ -56,7 +56,8 @@ public static function deserializeFromPresentationFormat(string $data): Characte
$backslashUnescapedValue = str_replace(self::BACKSLASH.self::BACKSLASH,self::BACKSLASH,$quoteUnescapedValue);
return new self($backslashUnescapedValue);
}
$backslashUnescapedValue = str_replace(self::BACKSLASH.self::BACKSLASH,self::BACKSLASH,$data);
$quoteUnescapedValue = str_replace(self::BACKSLASH.self::QUOTE,self::QUOTE,$data);
$backslashUnescapedValue = str_replace(self::BACKSLASH.self::BACKSLASH,self::BACKSLASH,$quoteUnescapedValue);
return new self($backslashUnescapedValue);
}

Expand Down
1 change: 0 additions & 1 deletion src/Types/MINFO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace YOCLIB\DNS\Types;

use YOCLIB\DNS\Exceptions\DNSTypeException;
use YOCLIB\DNS\Fields\CharacterString;
use YOCLIB\DNS\Fields\Field;
use YOCLIB\DNS\Fields\FQDN;

Expand Down
27 changes: 27 additions & 0 deletions src/Types/RP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace YOCLIB\DNS\Types;

use YOCLIB\DNS\Exceptions\DNSTypeException;
use YOCLIB\DNS\Fields\Field;
use YOCLIB\DNS\Fields\FQDN;

class RP extends Type{

/**
* @param array|Field[] $fields
* @throws DNSTypeException
*/
public function __construct(array $fields){
parent::__construct($fields);
if(count($fields)!==2){
throw new DNSTypeException('Only two fields allowed.');
}
if(!($fields[0] instanceof FQDN)){
throw new DNSTypeException('First field should be a FQDN.');
}
if(!($fields[1] instanceof FQDN)){
throw new DNSTypeException('Second field should be a FQDN.');
}
}

}
31 changes: 31 additions & 0 deletions src/Types/TXT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace YOCLIB\DNS\Types;

use YOCLIB\DNS\Exceptions\DNSTypeException;
use YOCLIB\DNS\Fields\CharacterString;
use YOCLIB\DNS\Fields\Field;

class TXT extends Type{

/**
* @param array|Field[] $fields
* @throws DNSTypeException
*/
public function __construct(array $fields){
parent::__construct($fields);
if(count($fields)<1){
throw new DNSTypeException('At least one field required.');
}
$totalLength = 0;
foreach($fields as $field){
if(!($field instanceof CharacterString)){
throw new DNSTypeException('Every field should be a character string.');
}
$totalLength += strlen($field->serializeToWireFormat());
}
if($totalLength>65536){
throw new DNSTypeException('Maximum size exceeded.');
}
}

}
27 changes: 27 additions & 0 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
use YOCLIB\DNS\Fields\FQDN;
use YOCLIB\DNS\Fields\IPv4Address;
use YOCLIB\DNS\Fields\IPv6Address;
use YOCLIB\DNS\Fields\UnsignedInteger16;
use YOCLIB\DNS\Fields\UnsignedInteger32;
use YOCLIB\DNS\Fields\UnsignedInteger8;
use YOCLIB\DNS\Types\A;
use YOCLIB\DNS\Types\AAAA;
use YOCLIB\DNS\Types\HINFO;
use YOCLIB\DNS\Types\MD;
use YOCLIB\DNS\Types\MINFO;
use YOCLIB\DNS\Types\MX;
use YOCLIB\DNS\Types\NS;
use YOCLIB\DNS\Types\SOA;
use YOCLIB\DNS\Types\TXT;
use YOCLIB\DNS\Types\WKS;

class TypesTest extends TestCase{
Expand Down Expand Up @@ -80,6 +83,30 @@ public function testTypes(){
self::assertEquals('my\.dotted\.response\.mail\.address.example.com. my\.dotted\.error\.mail\.address.example.com.',$minfoRecord->serializeToPresentationFormat());
self::assertEquals("\x1Fmy.dotted.response.mail.address\x07example\x03com\x00"."\x1Cmy.dotted.error.mail.address\x07example\x03com\x00",$minfoRecord->serializeToWireFormat());

$mxRecord = new MX([
new UnsignedInteger16('10'),
new FQDN('mx','example','com',''),
]);
self::assertEquals('10 mx.example.com.',$mxRecord->serializeToPresentationFormat());
self::assertEquals("\x00\x0A"."\x02mx\x07example\x03com\x00",$mxRecord->serializeToWireFormat());

$txtRecord = new TXT([
new CharacterString('TEXT'),
new CharacterString('"TEXT"'),
new CharacterString('TEXT WITH SPACE'),
new CharacterString('TEXT WITH "QUOTE" AND SPACE'),
new CharacterString('TEXT WITH BACKSPACE (\\) AND SPACE'),
]);
self::assertEquals('TEXT \"TEXT\" "TEXT WITH SPACE" "TEXT WITH \"QUOTE\" AND SPACE" "TEXT WITH BACKSPACE (\\\\) AND SPACE"',$txtRecord->serializeToPresentationFormat());
self::assertEquals("\x04TEXT"."\x06\"TEXT\""."\x0FTEXT WITH SPACE"."\x1BTEXT WITH \"QUOTE\" AND SPACE"."\x21TEXT WITH BACKSPACE (\\) AND SPACE",$txtRecord->serializeToWireFormat());

$rpRecord = new MINFO([
new FQDN('mailbox.domainname','example','com',''),
new FQDN('text.domainname','example','com',''),
]);
self::assertEquals('mailbox\.domainname.example.com. text\.domainname.example.com.',$rpRecord->serializeToPresentationFormat());
self::assertEquals("\x12mailbox.domainname\x07example\x03com\x00"."\x0Ftext.domainname\x07example\x03com\x00",$rpRecord->serializeToWireFormat());

$aaaaRecord = new AAAA([
new IPv6Address('::'),
]);
Expand Down

0 comments on commit 67406d2

Please sign in to comment.