Skip to content

Commit

Permalink
Add HINFO and MINFO
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 19, 2025
1 parent 142ec2e commit bd32f7a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Fields/FQDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FQDN implements Field{
* @param array|string[] $value
* @throws DNSFieldException
*/
public function __construct(array $value){
public function __construct(string... $value){
foreach($value AS $label){
if(!is_string($label)){
throw new DNSFieldException('Only strings allowed.');
Expand Down Expand Up @@ -57,7 +57,7 @@ public function serializeToWireFormat(): string{
*/
public static function deserializeFromPresentationFormat(string $data): FQDN{
$labels = preg_split('/(?<!\\\\)\\./',$data);
return new self($labels);
return new self(...$labels);
}

/**
Expand All @@ -75,7 +75,7 @@ public static function deserializeFromWireFormat(string $data): FQDN{
$labels[] = substr($data,$i+1,$length);
$i += $length;
}
return new self($labels);
return new self(...$labels);
}

}
27 changes: 27 additions & 0 deletions src/Types/HINFO.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\CharacterString;
use YOCLIB\DNS\Fields\Field;

class HINFO 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 CharacterString)){
throw new DNSTypeException('First field should be a character string.');
}
if(!($fields[1] instanceof CharacterString)){
throw new DNSTypeException('Second field should be a character string.');
}
}

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

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

class MINFO 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.');
}
}

}
4 changes: 2 additions & 2 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FieldsTest extends TestCase{
public function testGetValue(): void{
self::assertEquals([1,2,3,4,5,6],(new Bitmap([1,2,3,4,5,6]))->getValue());
self::assertEquals('This is text',(new CharacterString('This is text'))->getValue());
self::assertEquals(['example','com'],(new FQDN(['example','com']))->getValue());
self::assertEquals(['example','com',''],(new FQDN(['example','com','']))->getValue());
self::assertEquals(['example','com'],(new FQDN('example','com'))->getValue());
self::assertEquals(['example','com',''],(new FQDN('example','com',''))->getValue());
self::assertEquals('1.2.3.4',(new IPv4Address('1.2.3.4'))->getValue());
self::assertEquals('::',(new IPv6Address('::'))->getValue());
self::assertEquals(123,(new UnsignedInteger8(123))->getValue());
Expand Down
25 changes: 21 additions & 4 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
use YOCLIB\DNS\Exceptions\DNSFieldException;
use YOCLIB\DNS\Exceptions\DNSTypeException;
use YOCLIB\DNS\Fields\Bitmap;
use YOCLIB\DNS\Fields\CharacterString;
use YOCLIB\DNS\Fields\FQDN;
use YOCLIB\DNS\Fields\IPv4Address;
use YOCLIB\DNS\Fields\IPv6Address;
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\NS;
use YOCLIB\DNS\Types\SOA;
use YOCLIB\DNS\Types\WKS;
Expand All @@ -32,20 +35,20 @@ public function testTypes(){
self::assertEquals("\x01\x02\x03\x04",$aRecord->serializeToWireFormat());

$nsRecord = new NS([
new FQDN(['ns','example','com','']),
new FQDN('ns','example','com',''),
]);
self::assertEquals('ns.example.com.',$nsRecord->serializeToPresentationFormat());
self::assertEquals("\x02ns\x07example\x03com\x00",$nsRecord->serializeToWireFormat());

$mdRecord = new MD([
new FQDN(['@']),
new FQDN('@'),
]);
self::assertEquals('@',$mdRecord->serializeToPresentationFormat());
self::assertEquals("\x01@\x40",$mdRecord->serializeToWireFormat());

$soaRecord = new SOA([
new FQDN(['ns','example','com','']),
new FQDN(['my.dotted.mail.address','example','com','']),
new FQDN('ns','example','com',''),
new FQDN('my.dotted.mail.address','example','com',''),
new UnsignedInteger32(123),
new UnsignedInteger32(456),
new UnsignedInteger32(789),
Expand All @@ -63,6 +66,20 @@ public function testTypes(){
self::assertEquals('1.2.3.4 6 SMTP',$wksRecord->serializeToPresentationFormat());
self::assertEquals("\x01\x02\x03\x04"."\x06"."\x00\x00\x00\x02",$wksRecord->serializeToWireFormat());

$hinfoRecord = new HINFO([
new CharacterString('ARM64'),
new CharacterString('Linux (2025)'),
]);
self::assertEquals('ARM64 "Linux (2025)"',$hinfoRecord->serializeToPresentationFormat());
self::assertEquals("\x05ARM64"."\x0CLinux (2025)",$hinfoRecord->serializeToWireFormat());

$minfoRecord = new MINFO([
new FQDN('my.dotted.response.mail.address','example','com',''),
new FQDN('my.dotted.error.mail.address','example','com',''),
]);
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());

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

0 comments on commit bd32f7a

Please sign in to comment.