Location: PHPKode > projects > IPTables log analyzer > iptablelog/admin/modules/resolve.php
<? #require_once("$file_base/utils/Db.php"); ?>
<center>
<table cellspacing="1" cellpadding="2" width="500" bgcolor="#FFFFFF">
<tr>
<td rowspan=4 bgcolor="#DDDDDD" width=200>
<center><b>Resolve hosts</b><br>
<div class="DatabaseStats">
<? 
if (isset($_REQUEST["resolve"])) {
    if ($_REQUEST["limit"]) {
        $limit = " LIMIT " . $_REQUEST["limit"];
    } else {
        $limit = "";
    }
    if ($_REQUEST["method"] == "host") {
        include "$file_base/utils/resolver_host.php";
    } else {
        include "$file_base/utils/resolver_native.php";
    }
    $resolved = 0;
    $tried = 0;
    $start_time = time();
    if ($_REQUEST["preference"] == "dest") {
        $query = "SELECT distinct(u.ip_daddr) AS ip FROM ulog u LEFT JOIN host_cache hc ON u.ip_daddr = hc.ip WHERE hc.host IS NULL ORDER BY u.local_time DESC $limit";
    } else {
        $query = "SELECT distinct(u.ip_saddr) AS ip FROM ulog u LEFT JOIN host_cache hc ON u.ip_saddr = hc.ip WHERE hc.host IS NULL ORDER BY u.local_time DESC $limit";
    }
    $result = mysql_query ($query) or db_handleError("Unable to query log table");
    #while($line = mysql_fetch_object($result)){
    while($line = db_nextobject($result)){
        $tried++;
        $host = resolve_host(long2ip($line->ip));
        if ($host) {
            if (substr($host,-1) != '.') { $host .= '.'; }
	    $query = "INSERT INTO host_cache SET ip=" . $line->ip . ", host='$host'";
             mysql_query ($query) or db_handleError("Could not update host cache in database.");
	    $resolved++;
        }
    }
    if ($tried < $_REQUEST["limit"]) {
        $limit = " LIMIT " . ($_REQUEST["limit"] - $tried);
        if ($_REQUEST["preference"] == "dest") {
            $query = "SELECT distinct(u.ip_saddr) AS ip FROM ulog u LEFT JOIN host_cache hc ON u.ip_saddr = hc.ip WHERE hc.host IS NULL ORDER BY u.local_time DESC $limit";
        } else {
            $query = "SELECT distinct(u.ip_daddr) AS ip FROM ulog u LEFT JOIN host_cache hc ON u.ip_daddr = hc.ip WHERE hc.host IS NULL ORDER BY u.local_time DESC $limit";
        }
        $result = mysql_query ($query) or db_handleError("Unable to query log table");
        while($line = db_nextobject($result)){
            $tried++;
            $host = resolve_host(long2ip($line->ip));
            if ($host) {
                $query = "INSERT INTO host_cache SET ip=" . $line->ip . ", host='$host'";
                mysql_query ($query) or db_handleError("Could not update host cache in database.");
                $resolved++;
            }
        }
    }
    $elapsed = time() - $start_time;
    echo "Resolved $resolved hosts in $tried attempts<br>($elapsed secs)";
} ?>
</div>
</center></td>
<form action="index.php" method="post">
<input type="hidden" name="resolve" value="1">
<td colspan=2>Limit number of lookups to 
<select name="limit">
<option value=10>10</option>
<option value=50>50</option>
<option value=100>100</option>
<option value=250>250</option>
<option value=0 selected>No Limit</option>
</select>
</td>
</tr>
<tr><td colspan=2>Preference to <select name="preference">
<option value="src" selected>Source Addresses</option>
<option value="dest">Destination Addresses</option>
</select>
</td></tr>
<tr>
<td colspan=2>
<? if ($host_resolution_avail) { ?>
Use <select name="method">
<option value="native" selected>gethostbyaddr</option>
<option value="host">host command (Linux)</option>
</select> method.
<? } ?>
</td>
</tr>
<tr><td colspan=2> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; 
 <input type="submit" value="Resolve"></td></tr>
 </form>
<tr>
<td rowspan=3 bgcolor="#DDDDDD" width=200>
<center><b>Manual host entry</b><br>
<div class="DatabaseStats">
<?
if (isset($_REQUEST["resolve_manual"])) {
	$query = "SELECT INET_NTOA(ip) as ip_s, ip, host, static FROM host_cache WHERE ip = INET_ATON('" . $_REQUEST['manual_ip'] . "')";
        $result = mysql_query ($query) or db_handleError("Unable to query log table");
        if($line = db_nextobject($result)) {
		if ($line->static == 'Y') {
			echo $line->ip_s . ": changing static entry from<br>" . $line->host . " to " . $_REQUEST['manual_host'] . ".";
		} else {
			echo $line->ip_s . ": changing entry from<br>" . $line->host . " to " . $_REQUEST['manual_host'] . ".";
		}
		$query = "UPDATE host_cache SET host = '" . $_REQUEST['manual_host'] . "' WHERE ip=" . $line->ip;
	} else {
		$query = "INSERT INTO host_cache (ip,host,static) VALUES (INET_ATON('" . $_REQUEST['manual_ip'] . "'),'" .
			$_REQUEST['manual_host'] . "','Y')";
		echo $_REQUEST['manual_ip'] . ": adding entry for<br>" . $_REQUEST['manual_host'] . ".";
	}
	#echo "<br>" . $query;
        $result = mysql_query ($query) or db_handleError("Unable to query log table");
}
?>
</div>
</center></td>
<form action="index.php" method="post">
<input type="hidden" name="resolve_manual" value="1">
<td align=right width=75> IP: </td><td><input type=text size=20 name="manual_ip">
</td>
</tr><tr>
<td align=right width=75> Hostname: </td><td><input type=text size=20 name="manual_host">
</td>
</tr>
</tr>
<tr><td> &nbsp; </td>
<td> <input type="submit" value="Add"> &nbsp; &nbsp; 
<input type="button" onClick="window.open('show_cache.php','show','status=0,toolbar=0,menubar=0,width=700,height=500,scrollbars=1');" value="Show Cache"></td></tr>
 </form>
</table>
</center>
Return current item: IPTables log analyzer