IP range vs IP check
Auteur: Paul Gregg - 29 april 2009 - 14:19 - Gekeurd door: Koen - Hits: 4147 - Aantal punten: (0 stemmen)
Bekijk of een ip adres voorkomt in een bepaalde range
|
Code: |
<?php
// decbin32
// In order to simplify working with IP addresses (in binary) and their
// netmasks, it is easier to ensure that the binary strings are padded
// with zeros out to 32 characters - IP addresses are 32 bit numbers
Function decbin32 ($dec) {
return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
}
// ip_in_range
// This function takes 2 arguments, an IP address and a "range" in several
// different formats.
// Network ranges can be specified as:
// 1. Wildcard format: 1.2.3.*
// 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
// 3. Start-End IP format: 1.2.3.0-1.2.3.255
// The function will return true if the supplied IP is within the range.
// Note little validation is done on the range inputs - it expects you to
// use one of the above 3 formats.
Function ip_in_range($ip, $range) {
if (strpos($range, '/') !== false) {
// $range is in IP/NETMASK format
list($range, $netmask) = explode('/', $range, 2);
if (strpos($netmask, '.') !== false) {
// $netmask is a 255.255.0.0 format
$netmask = str_replace('*', '0', $netmask);
$netmask_dec = ip2long($netmask);
return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
} else {
// $netmask is a CIDR size block
// fix the range argument
$x = explode('.', $range);
while(count($x)<4) $x[] = '0';
list($a,$b,$c,$d) = $x;
$range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
$range_dec = ip2long($range);
$ip_dec = ip2long($ip);
# Strategy 1 - Using substr to chop up the range and pad it with 1s to the right
$broadcast_dec = bindec(substr(decbin32($range_dec), 0, $netmask)
. str_pad('', 32-$netmask, '1'));
# Strategy 2 - Use math to OR the range with the wildcard to create the Broadcast address
$wildcard_dec = pow(2, (32-$netmask)) - 1;
$broadcast_dec = $range_dec | $wildcard_dec;
return (($ip_dec & $broadcast_dec) == $ip_dec);
}
} else {
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
if (strpos($range, '*') !==false) { // a.b.*.* format
// Just convert to A-B format by setting * to 0 for A and 255 for B
$lower = str_replace('*', '0', $range);
$upper = str_replace('*', '255', $range);
$range = "$lower-$upper";
}
if (strpos($range, '-')!==false) { // A-B format
list($lower, $upper) = explode('-', $range, 2);
$lower_dec = ip2long($lower);
$upper_dec = ip2long($upper);
$ip_dec = ip2long($ip);
return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
}
echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
return false;
}
$ip_dec = ip2long($ip);
printf("ip_dec = %s\n", decbin32($ip_dec));
printf("netmask_dec = %s\n", decbin32($netmask_dec));
printf("ANDed = %s\n", decbin32(($ip_dec & $netmask_dec)));
return (($ip_dec & $netmask_dec) == $ip_dec);
}
?>
<?php // decbin32 // In order to simplify working with IP addresses (in binary) and their // netmasks, it is easier to ensure that the binary strings are padded // with zeros out to 32 characters - IP addresses are 32 bit numbers Function decbin32 ($dec) { } // ip_in_range // This function takes 2 arguments, an IP address and a "range" in several // different formats. // Network ranges can be specified as: // 1. Wildcard format: 1.2.3.* // 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0 // 3. Start-End IP format: 1.2.3.0-1.2.3.255 // The function will return true if the supplied IP is within the range. // Note little validation is done on the range inputs - it expects you to // use one of the above 3 formats. Function ip_in_range($ip, $range) { if (strpos($range, '/') !== false) { // $range is in IP/NETMASK format if (strpos($netmask, '.') !== false) { // $netmask is a 255.255.0.0 format return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) ); } else { // $netmask is a CIDR size block // fix the range argument while(count($x)<4) $x[] = '0'; # Strategy 1 - Using substr to chop up the range and pad it with 1s to the right $broadcast_dec = bindec(substr(decbin32 ($range_dec), 0, $netmask) # Strategy 2 - Use math to OR the range with the wildcard to create the Broadcast address $wildcard_dec = pow(2, (32-$netmask)) - 1; $broadcast_dec = $range_dec | $wildcard_dec; return (($ip_dec & $broadcast_dec) == $ip_dec); } } else { // range might be 255.255.*.* or 1.2.3.0-1.2.3.255 if (strpos($range, '*') !==false) { // a.b.*.* format // Just convert to A-B format by setting * to 0 for A and 255 for B $range = "$lower-$upper"; } if (strpos($range, '-')!==false) { // A-B format return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) ); } echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format'; return false; } printf("ip_dec = %s\n", decbin32 ($ip_dec)); printf("netmask_dec = %s\n", decbin32 ($netmask_dec)); printf("ANDed = %s\n", decbin32 (($ip_dec & $netmask_dec))); return (($ip_dec & $netmask_dec) == $ip_dec); } ?>
Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|