<% // IP address functions // Author: Cory R. King (coryking@azalea.com) // http://www.coryking.com // boy, wouldn't it be nice if PHP had datatypes so I can force some of this crap to be unsigned? // wouldn't want to upset the script kiddies though.... function GetNumberOfUsable($ip_address) { // Inputs: // $ip_address - A full IP address with wither a subnet or CIDR style // address (ex: 24.0.226.0/24 or 24.0.226.0/255.255.255.0) // Returns: // number of avalible hosts $range = GetNetworkRange($ip_address); return $range - 2; // broadcase & network } function GetNetworkAddress($ip_address) { // Inputs: // $ip_address - A full IP address with wither a subnet or CIDR style // address (ex: 24.0.226.0/24 or 24.0.226.0/255.255.255.0) // Returns: // A human readable IP address that contains the network address for the given IP $hex_addr = ConvertIPtoHex($ip_address); $subnet = Getnetmask($ip_address); $network = $hex_addr & $subnet; return ConvertHextoIP($network); } function GetBroadcastAddress($ip_address) { // Inputs: // $ip_address - A full IP address with wither a subnet or CIDR style // address (ex: 24.0.226.0/24 or 24.0.226.0/255.255.255.0) // Returns: // A human readable IP address that contains the broadcast address for the given IP $hex_addr = ConvertIPtoHex($ip_address); $subnet = Getnetmask($ip_address); $range = GetNetworkRange($ip_address); $broadcast = ($hex_addr & $subnet) + $range; return ConvertHextoIP($broadcast); } function GetNetworkRange($ip_address) { // Inputs: // $ip_address - A full IP address with wither a subnet or CIDR style // address (ex: 24.0.226.0/24 or 24.0.226.0/255.255.255.0) // Returns: // this function is the opposite of the netmask function // it returns the range of usable addresses in hex/dec format $mask = GetNetmask($ip_address); return (-1 - $mask); } function GetNetmask($ip_address) { // Inputs: // $ip_address - A full IP address with wither a subnet or CIDR style // address (ex: 24.0.226.0/24 or 24.0.226.0/255.255.255.0) // Returns: // returns a hex/dec representation of the net mask for the given IP // first we need to split the ip up $ip_parts = preg_split("/[\.\/]/", $ip_address); // how big are we, did this guy give us a subnet mask, or // a cidr style address? if(Count($ip_parts)==8) { // we have a subnet $netmask_hex = $ip_parts[7]; $netmask_hex += $ip_parts[6] << 8; $netmask_hex += $ip_parts[5] << 16; $netmask_hex += $ip_parts[4] << 24; } if(Count($ip_parts)==5) { // we have a CIDR style adddress, first get the address $netmask_hex = 0xFFFFFFFF >> ($ip_parts[4] - 1); } if(Count($ip_parts)==4) { // just an IP $netmask_hex = -1; } return $netmask_hex; } function ConvertIPtoHex($ip_address) { // Inputs: // $ip_address - A full IP address with wither a subnet or CIDR style // address (ex: 24.0.226.0/24 or 24.0.226.0/255.255.255.0) // Returns: // a decimal/hex representation of the given IP (strips out the subnet stuff) // first we need to split the ip up $ip_parts = preg_split("/[\.\/]/", $ip_address); // now we get the number... $ip_hex = $ip_parts[3]; $ip_hex += $ip_parts[2] << 8; $ip_hex += $ip_parts[1] << 16; $ip_hex += $ip_parts[0] << 24; return $ip_hex; } function ConvertHextoIP($hex_address) { // takes a hex address and converts it into a human readable IP address // I'm going to shift, then mask as it appears that php has some kind // of bug when i do it the other way. // I think it's because of a of a sign thing (they probably use a 4 byte signed int) // damn typeless languages... damn script kiddies.. "oooo data types. // I don't know no data types, thats not in the sams teach your self php in 24 hours book". // Course, if you're one of those script kiddies // then this probably doesn't make much sense either... $ip_hex[0] = $hex_address & 0xFF; $ip_hex[1] = ($hex_address >> 8) & 0xFF; $ip_hex[2] = ($hex_address >> 16) & 0xFF; $ip_hex[3] = ($hex_address >> 24) & 0xFF; $ip_text = "$ip_hex[3].$ip_hex[2].$ip_hex[1].$ip_hex[0]"; return $ip_text; } %>