Skip to content

Commit

Permalink
Small fix for trailing dot on domains
Browse files Browse the repository at this point in the history
  • Loading branch information
yswery committed Nov 22, 2014
1 parent 9ea9466 commit a982aed
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/RecursiveProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace yswery\DNS;

use \Exception;

class JsonStorageProvider extends AbstractStorageProvider {

private $dns_records;
private $DS_TTL;

public function __construct($record_file, $default_ttl = 300)
{
$handle = @fopen($record_file, "r");
if(!$handle) {
throw new Exception('Unable to open dns record file.');
}

$dns_json = fread($handle, filesize($record_file));
fclose($handle);

$dns_records = json_decode($dns_json, true);
if(!$dns_records) {
throw new Exception('Unable to parse dns record file.');
}

if(!is_int($default_ttl)) {
throw new Exception('Default TTL must be an integer.');
}
$this->DS_TTL = $default_ttl;

$this->dns_records = $dns_records;
}

public function get_answer($question)
{
$answer = array();
$domain = trim($question[0]['qname'], '.');
$type = RecordTypeEnum::get_name($question[0]['qtype']);

if(isset($this->dns_records[$domain]) &&isset($this->dns_records[$domain][$type])) {
if(is_array($this->dns_records[$domain][$type])) {
foreach($this->dns_records[$domain][$type] as $ip) {
$answer[] = array('name' => $question[0]['qname'], 'class' => $question[0]['qclass'], 'ttl' => $this->DS_TTL, 'data' => array('type' => $question[0]['qtype'], 'value' => $ip));
}
} else {
$answer[] = array('name' => $question[0]['qname'], 'class' => $question[0]['qclass'], 'ttl' => $this->DS_TTL, 'data' => array('type' => $question[0]['qtype'], 'value' => $this->dns_records[$domain][$type]));
}
}

return $answer;
}

}
8 changes: 7 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,24 @@ private function ds_encode_type($type, $val = NULL, $offset = NULL)
$enc = str_repeat("\0", 16);
return $enc;
case RecordTypeEnum::TYPE_NS:
$val = rtrim($val,'.').'.';
return $this->ds_encode_label($val, $offset);
case RecordTypeEnum::TYPE_CNAME:
$val = rtrim($val,'.').'.';
return $this->ds_encode_label($val, $offset);
case RecordTypeEnum::TYPE_SOA:
$res = '';
$val['mname'] = rtrim($val['mname'],'.').'.';
$val['rname'] = rtrim($val['rname'],'.').'.';
$res .= $this->ds_encode_label($val['mname'], $offset);
$res .= $this->ds_encode_label($val['rname'], $offset +strlen($res));
$res .= pack('NNNNN', $val['serial'], $val['refresh'], $val['retry'], $val['expire'], $val['minimum']);
$res .= pack('NNNNN', $val['serial'], $val['refresh'], $val['retry'], $val['expire'], $val['minimum-ttl']);
return $res;
case RecordTypeEnum::TYPE_PTR:
$val = rtrim($val,'.').'.';
return $this->ds_encode_label($val, $offset);
case RecordTypeEnum::TYPE_MX:
$val = rtrim($val,'.').'.';
return pack('n', 10) . $this->ds_encode_label($val, $offset +2);
case RecordTypeEnum::TYPE_TXT:
if(strlen($val) > 255)
Expand Down

0 comments on commit a982aed

Please sign in to comment.