Skip to content

Commit

Permalink
- added support for the ATMA resource record
Browse files Browse the repository at this point in the history
- added initial support for simple local caching. this support for file based, and shared memory based cache.
- fixed some typo's in the OPT and TKEY class
- added a test for the ATMA RR
  • Loading branch information
mike.pultz committed Apr 19, 2011
1 parent abf75ea commit b1b195a
Show file tree
Hide file tree
Showing 14 changed files with 556 additions and 30 deletions.
54 changes: 48 additions & 6 deletions Net/DNS2.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ class Net_DNS2
/*
* the current version of this library
*/
const VERSION = "1.0.2";
const VERSION = '1.0.2';

/*
* the default path to a resolv.conf file
*/
const RESOLV_CONF = '/etc/resolv.conf';

/*
* use TCP only (true/false)
Expand Down Expand Up @@ -116,6 +121,16 @@ class Net_DNS2
*/
public $search_list = array();

/*
* use the local shared memory cache (true/false)
*/
public $use_cache = false;

/*
* file name to use for shared memory segment or file cache
*/
public $cache_file = '/tmp/net_dns2.cache';

/*
* local sockets
*/
Expand All @@ -136,6 +151,11 @@ class Net_DNS2
*/
protected $_auth_signature = null;

/*
* the shared memory segment id for the local cache
*/
protected $_cache = null;

/*
* the last erro message returned by the sockets class
*/
Expand Down Expand Up @@ -172,6 +192,21 @@ public function __construct(array $options = null)
}
}
}

//
// if we're set to use the local shared memory cache, then
// make sure it's been initialized
//
if ($this->use_cache == true) {

if (extension_loaded("shmop")) {

$this->_cache = new Net_DNS2_Cache_Shm($this->cache_file, 10000);
} else {

$this->_cache = new Net_DNS2_Cache_File($this->cache_file);
}
}
}

/**
Expand Down Expand Up @@ -294,13 +329,20 @@ public function setServers($nameservers)
* @access protected
*
*/
protected function checkServers()
protected function checkServers($default = null)
{
if (empty($this->nameservers)) {
throw new Net_DNS2_Exception(
'empty name servers list; you must provide a list of name servers,'.
' or the path to a resolv.conf file.'
);

if (isset($default)) {

$this->setServers($default);
} else {

throw new Net_DNS2_Exception(
'empty name servers list; you must provide a list of name '.
'servers, or the path to a resolv.conf file.'
);
}
}

return true;
Expand Down
87 changes: 87 additions & 0 deletions Net/DNS2/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

class Net_DNS2_Cache
{
protected $_cache_id = false;
protected $_cache_file = "";
protected $_cache_data = array();
protected $_cache_size = 10000;

public function has($key)
{
return isset($this->_cache_data[$key]);
}
public function get($key)
{
if (isset($this->_cache_data[$key])) {
echo "CACHE HIT!\n";
return $this->_cache_data[$key]['object'];
} else {

return false;
}
}
public function put($key, $data)
{
$this->_cache_data[$key] = array('cache_date' => time(), 'object' => $data);
}
protected function clean()
{
if (count($this->_cache_data) > 0) {

//
// go through each entry and adjust their TTL, and remove entries that
// have expired
//
$now = time();

foreach($this->_cache_data as $key => $data) {

$diff = $now - $data['cache_date'];

$this->_cache_data[$key]['cache_date'] = $now;

foreach($data['object']->answer as $index => $rr) {

if ($rr->ttl <= $diff) {

unset($this->_cache_data[$key]);
break 2;
} else {

$this->_cache_data[$key]['object']->answer[$index]->ttl -= $diff;
}
}
foreach($data['object']->authority as $index => $rr) {

if ($rr->ttl <= $diff) {

unset($this->_cache_data[$key]);
break 2;
} else {

$this->_cache_data[$key]['object']->authority[$index]->ttl -= $diff;
}
}
foreach($data['object']->additional as $index => $rr) {

if ($rr->ttl <= $diff) {

unset($this->_cache_data[$key]);
break 2;
} else {

$this->_cache_data[$key]['object']->additional[$index]->ttl -= $diff;
}
}
}

//
// how also check to see if we have too many entries, and remove the
// oldest entries first
//
}
}
};

?>
80 changes: 80 additions & 0 deletions Net/DNS2/Cache/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

class Net_DNS2_Cache_File extends Net_DNS2_Cache
{
public function __construct($cache_file)
{
$this->_cache_file = $cache_file;
if (file_exists($this->_cache_file) == true) {

//
// open the file for reading
//
$fp = fopen($this->_cache_file, "r");
if ($fp !== false) {

//
// lock the file just in case
//
flock($fp, LOCK_EX);

//
// read the file contents
//
$this->_cache_data = unserialize(fread($fp, filesize($this->_cache_file)));

//
// unlock
//
flock($fp, LOCK_UN);

//
// close the file
//
fclose($fp);

} else {
// throw an exception
}

$this->clean();
}
}
public function __destruct()
{
if (count($this->_cache_data) > 0) {

//
// open the file for writing
//
$fp = fopen($this->_cache_file, "w");
if ($fp !== false) {

//
// lock the file just in case
//
flock($fp, LOCK_EX);

//
// write the file contents
//
fwrite($fp, serialize($this->_cache_data));

//
// unlock
//
flock($fp, LOCK_UN);

//
// close the file
//
fclose($fp);

} else {
// throw an exception
}
}
}
};

?>
65 changes: 65 additions & 0 deletions Net/DNS2/Cache/Shm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

class Net_DNS2_Cache_Shm extends Net_DNS2_Cache
{
public function __construct($cache_file, $size)
{
//
// make sure the file exists first
//
if (!file_exists($cache_file)) {
touch($cache_file);
}

//
// convert the filename to a IPC key
//
$this->_cache_file = ftok($cache_file, 't');
echo "using: " . $this->_cache_file . "\n";
//
// open the shared memory segment
//
$this->_cache_id = shmop_open($this->_cache_file, 'c', 0644, $size);
if ($this->_cache_id !== false) {

//
// this returns the size allocated, and not the size used, but it's
// still a good check to make sure there's space allocated.
//
$size = shmop_size($this->_cache_id);
if ($size > 0) {

//
// read the data from teh shared memory segment
//
$data = @shmop_read($this->_cache_id, 0, $size);
if ( ($data !== false) && (strlen($data) > 0) ) {

//
// unserialize and store the data
//
$this->_cache_data = unserialize($data);

//
// call clean to clean up old entries
//
$this->clean();
}
}
} else {

// throw an exception
}
}
public function __destruct()
{
if (count($this->_cache_data) > 0) {

shmop_write($this->_cache_id, serialize($this->_cache_data), 0);
}

shmop_close($this->_cache_id);
}
};

?>
3 changes: 2 additions & 1 deletion Net/DNS2/Lookups.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Net_DNS2_Lookups
'EID' => 31, //
'NIMLOC' => 32, //
'SRV' => 33, // RFC 2782
'ATMA' => 34, // Not implemented
'ATMA' => 34, // Windows only
'NAPTR' => 35, // RFC 2915
'KX' => 36, // RFC 2230
'CERT' => 37, // RFC 4398
Expand Down Expand Up @@ -282,6 +282,7 @@ class Net_DNS2_Lookups
31 => 'Net_DNS2_RR_EID',
32 => 'Net_DNS2_RR_NIMLOC',
33 => 'Net_DNS2_RR_SRV',
34 => 'Net_DNS2_RR_ATMA',
35 => 'Net_DNS2_RR_NAPTR',
36 => 'Net_DNS2_RR_KX',
37 => 'Net_DNS2_RR_CERT',
Expand Down
2 changes: 1 addition & 1 deletion Net/DNS2/RR.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static function fromString($line)
//
// lookup the class to use
//
$o = null;
$o = null;
$class_name = Net_DNS2_Lookups::$rr_types_id_to_class[
Net_DNS2_Lookups::$rr_types_by_name[$type]
];
Expand Down
Loading

0 comments on commit b1b195a

Please sign in to comment.