Skip to content

Commit

Permalink
Fix FQDN
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 19, 2025
1 parent 08c7f2c commit 8544592
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
66 changes: 38 additions & 28 deletions src/Fields/FQDN.php
Original file line number Diff line number Diff line change
@@ -1,71 +1,81 @@
<?php
namespace YOCLIB\DNS\Fields;

use YOCLIB\DNS\Exceptions\DNSFieldException;

class FQDN implements Field{

private string $value;
private array $value;

/**
* @param mixed $value
* @param array|string[] $value
* @throws DNSFieldException
*/
public function __construct(mixed $value){
public function __construct(array $value){
foreach($value AS $label){
if(!is_string($label)){
throw new DNSFieldException('Only strings allowed.');
}
}
$this->value = $value;
}

/**
* @return mixed
* @return array|string[]
*/
public function getValue(): mixed{
public function getValue(): array{
return $this->value;
}

/**
* @return string
*/
public function serializeToPresentationFormat(): string{
$labels = [];
for($i=0;$i<strlen($this->value);$i++){
$length = ord($this->value[$i]);
if($length===0x40){
break;
}
$labels[] = substr($this->value,$i+1,$length);
$i += $length;
}
return implode('.',$labels);
return implode('.',array_map(static function($label){
return str_replace('.','\.',$label);
},$this->value));
}

/**
* @return string
*/
public function serializeToWireFormat(): string{
return $this->value;
$output = '';
foreach($this->value AS $label){
$output .= chr(strlen($label)).$label;
}
if($this->value[count($this->value)-1]!==''){
$output .= chr(0x40);
}
return $output;
}

/**
* @param string $data
* @return FQDN
* @throws DNSFieldException
*/
public static function deserializeFromPresentationFormat(string $data): FQDN{
$labels = explode('.',$data);
$binary = '';
foreach($labels AS $label){
$binary .= chr(strlen($label)) . $label;
}
if($labels[count($labels)-1]!==''){
$binary .= chr(0x40);
}
//TODO Add check
return new self($binary);
$labels = preg_split('/(?<!\\\\)\\./',$data);
return new self($labels);
}

/**
* @param string $data
* @return FQDN
* @throws DNSFieldException
*/
public static function deserializeFromWireFormat(string $data): FQDN{
//TODO Add check
return new self($data);
$labels = [];
for($i=0;$i<strlen($data);$i++){
$length = ord($data[$i]);
if($length===0x40){
break;
}
$labels[] = substr($data,$i+1,$length);
$i += $length;
}
return new self($labels);
}

}
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
18 changes: 15 additions & 3 deletions tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@

use PHPUnit\Framework\TestCase;

use YOCLIB\DNS\Exceptions\DNSFieldException;
use YOCLIB\DNS\Exceptions\DNSTypeException;
use YOCLIB\DNS\Fields\FQDN;
use YOCLIB\DNS\Fields\IPv4Address;
use YOCLIB\DNS\Types\A;
use YOCLIB\DNS\Types\NS;

class TypesTest extends TestCase{

/**
* @return void
* @throws DNSTypeException
* @throws DNSFieldException|DNSTypeException
*/
public function testTypes(){
self::assertEquals('1.2.3.4',(new A([new IPv4Address('1.2.3.4')]))->serializeToPresentationFormat());
self::assertEquals("\x01\x02\x03\x04",(new A([new IPv4Address('1.2.3.4')]))->serializeToWireFormat());
$aRecord = new A([new IPv4Address('1.2.3.4')]);
self::assertEquals('1.2.3.4',$aRecord->serializeToPresentationFormat());
self::assertEquals("\x01\x02\x03\x04",$aRecord->serializeToWireFormat());

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

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

}

0 comments on commit 8544592

Please sign in to comment.