<?php
// ----------------------------------------------------------------------
// Copyright (C) 2007 by GREGORY LE BRAS
// ----------------------------------------------------------------------
// LICENSE
//
// This file is part of ODCNMS - Open DataCenter Network Management System
//
// ODCNMS 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 2 of the License, or
// (at your option) any later version.
//
// Foobar 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 Foobar; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// ----------------------------------------------------------------------
// Original Author of file: GREGORY LE BRAS - http://www.odcnms.org/
// ----------------------------------------------------------------------
### Function to determine network characteristics
### $host = IP address or hostname of target host (string)
### $mask = Subnet mask of host in dotted decimal (string)
### returns array with
### "cidr" => host and mask in CIDR notation
### "network" => network address
### "broadcast" => broadcast address
###
### Example: find_net("192.168.37.215","255.255.255.224")
### returns:
### "cidr" => 192.168.37.215/27
### "network" => 192.168.37.192
### "broadcast" => 192.168.37.223
###
function find_net($host,$mask) {
$bits=strpos(decbin(ip2long($mask)),"0");
$net["cidr"]="/".$bits;
$net["network"]=long2ip(bindec(decbin(ip2long(gethostbyname($host))) & decbin(ip2long($mask))));
$binhost=str_pad(decbin(ip2long(gethostbyname($host))),32,"0",STR_PAD_LEFT);
$binmask=str_pad(decbin(ip2long($mask)),32,"0",STR_PAD_LEFT);
$broadcast = 0;
for ($i=0; $i<32; $i++) {
if (substr($binhost,$i,1)=="1" || substr($binmask,$i,1)=="0") {
$broadcast.="1";
} else {
$broadcast.="0";
}
}
$net["broadcast"]=long2ip(bindec($broadcast));
return $net;
}
?>