<?php
/*
$Id$
Obsessive Web Statistics
Copyright (C) 2007 Dustin Spicuzza <hide@address.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
By default, most people are NOT going to want DNS resolving. But, some may. This
module can be enabled or run two different ways:
1) Add configuration directive, and host lookups will always be done
$cfg['websites']['websitename']:
['enable_dns_lookup'] true/false.
2) Run dns_analyze.php in the scripts/extra directory
TODO: Write that second script
*/
class OWSDNS implements iPlugin, iAnalysisPlugin{
var $actually_do_it = false;
// this should return a unique ID identifying the plugin, should start with an alpha,
// should use basename instead of just __FILE__ otherwise it could expose path information
public function getPluginId(){
return 'p'. md5(basename(__FILE__) . get_class());
}
// returns an associative array describing the plugin
public function getPluginInformation(){
// automagically increment the revision number :)
$revision = trim(str_replace('Rev:','',str_replace('$','','$Rev: 96 $')));
return array(
'author' => 'Dustin Spicuzza (OWS builtin)',
'pluginName' => 'DNS Resolver',
'version' => "1.0.$revision",
'description' => 'Resolves hosts to hostnames.',
'url' => 'http://obsessive.sourceforge.net/'
);
}
// this function should return a set of arrays that define the dimensions
// and attributes that this plugin defines. You should not specify an attribute
// that another plugin defines. This is not website dependent.
public function define_dimensions(){
return array(
'host' => array(
'hostname' => attribute_defn('varchar',254,16)
)
);
}
/*
Treat this like a constructor. This is called before all phases of
analysis, and is only called once per website. It should be used to
clean up website-specific variables.
*/
public function InitializeAnalysis($website){
// get options
$options = get_website_options($website);
if (array_key_exists('enable_dns_lookup',$options) && $options['enable_dns_lookup'])
$this->actually_do_it = true;
//else if (
return true;
}
/*
This function is called before the round of analysis starts
$ids An array of all the current ID's for every dimension. If you
insert new rows into the dimension table, you MUST use and increment
the ID in the appropriate dimension.
*/
public function preAnalysis($website,&$ids){
return true;
}
/*
This function is called for each line grabbed from the logfile.
$website The current website being worked on
$dimension This parameter defines which dimension is being analyzed
at the moment. The function may be called multiple times
for a row with different dimensions as arguments.
$line This contains an array of data that was retrieved
from the logfile.
This function should return an item that is the 'primary node' of the
dimension (defined as an attribute with the same name as the dimension).
You should return false if you do not define a primary node for that dimension,
or if there is an error.
*/
public function getPrimaryNode($website, $dimension, $line){
return show_error("Invalid dimension passed to plugin\"" . get_class() . "\"");
}
/*
This function is called for each line grabbed from the logfile.
$website The current website being worked on
$dimension This parameter defines which dimension is being analyzed
at the moment. The function may be called multiple times
for a row with different dimensions as arguments.
$pnode This contains the primary node of the dimension.
The plugin should only return attributes that are defined in the
define_dimensions function. This function should return an array
in the form of
array('attribute' => 'value', ...)
which defines values to be uploaded to the SQL database. It should NOT return
the primary node. Please note that the returned values can be cached, so this
function may NOT always be called for each row. You should ALWAYS return
an array with the same keys each time, in the same order.
If you do not define any attributes in the current dimension, or if there is
an error, then return false.
*/
public function getAttributes($website, $dimension, $pnode){
if ($dimension != 'host')
return show_error("Invalid dimension passed to plugin\"" . get_class() . "\" in getAttributes!");
if ($this->actually_do_it){
echo ".";
return array('hostname' => $this->gethostbyaddr_timeout($pnode, "192.168.0.1", 3000));
}
// don't do DNS resolving all the time!
return array('hostname' => '');
}
/*
this function is called after the round of analysis is complete. Called many times.
$ids An array of all the current ID's for every dimension. If you
insert new rows into the dimension table, you MUST use and increment
the ID in the appropriate dimension. $ids[$dimensionname] is how it would
be referenced.
*/
public function postAnalysis($website,&$ids){
return true;
}
// from king dot macro at gmail dot com, (user notes, php.net)
function gethostbyaddr_timeout($ip, $dns, $timeout=1000)
{
// random transaction number (for routers etc to get the reply back)
$data = rand(0, 99);
// trim it to 2 bytes
$data = substr($data, 0, 2);
// request header
$data .= "\1\0\0\1\0\0\0\0\0\0";
// split IP up
$bits = explode(".", $ip);
// error checking
if (count($bits) != 4) return "ERROR";
// there is probably a better way to do this bit...
// loop through each segment
for ($x=3; $x>=0; $x--)
{
// needs a byte to indicate the length of each segment of the request
switch (strlen($bits[$x]))
{
case 1: // 1 byte long segment
$data .= "\1"; break;
case 2: // 2 byte long segment
$data .= "\2"; break;
case 3: // 3 byte long segment
$data .= "\3"; break;
default: // segment is too big, invalid IP
return "INVALID";
}
// and the segment itself
$data .= $bits[$x];
}
// and the final bit of the request
$data .= "\7in-addr\4arpa\0\0\x0C\0\1";
// create UDP socket
$handle = @fsockopen("udp://$dns", 53);
// send our request (and store request size so we can cheat later)
$requestsize=@fwrite($handle, $data);
@socket_set_timeout($handle, $timeout - $timeout%1000, $timeout%1000);
// hope we get a reply
$response = @fread($handle, 1000);
@fclose($handle);
if ($response == "")
return $ip;
// find the response type
$type = @unpack("s", substr($response, $requestsize+2));
if ($type[1] == 0x0C00) // answer
{
// set up our variables
$host="";
$len = 0;
// set our pointer at the beginning of the hostname
// uses the request size from earlier rather than work it out
$position=$requestsize+12;
// reconstruct hostname
do
{
// get segment size
$len = unpack("c", substr($response, $position));
// null terminated string, so length 0 = finished
if ($len[1] == 0)
// return the hostname, without the trailing .
return substr($host, 0, strlen($host) -1);
// add segment to our host
$host .= substr($response, $position+1, $len[1]) . ".";
// move pointer on to the next segment
$position += $len[1] + 1;
}
while ($len != 0);
// error - return the hostname we constructed (without the . on the end)
return $ip;
}
return $ip;
}
}
register_plugin('analysis',new OWSDNS());
?>