0

Possible Duplicate:
Getting list IPs from CIDR notation in PHP

Hi there,

I need to generate the list of IP addresses from a CIDR notation.

For example, user has entered 200.41.132.11/28. In return, I need to show the list of IP addresses matching this CIDR notation.

Do you know a PHP function for this or have an algorithm?

Thanks for your help.

Mat.

Community
  • 1
  • 1
TamTam
  • 849
  • 3
  • 13
  • 25

1 Answers1

0

Maybe I'm wrong but try something like this (not tested):

function getIPfromCIDR($CIDRn) {
  $a = explode("/", $CIDRn);
  $g = explode(".", $a[0]);
  $b = '';  $res = '';
  foreach ($g as $gv) $b .= str_pad(decbin($gv), 8, '0', STR_PAD_LEFT);
  $bm = str_split(str_pad(substr($b, 0, $a[1]), 32, '0', STR_PAD_RIGHT), 8);
  foreach($bm as &$bg) $bg = bindec($bg);
  return join('.', $bm); 
  }

$addr = "200.41.132.11/28";
print_r(getIPFromCIDR($addr));

Output should be

200.41.132.0

To get bit mask (network mask) only

function bitMask($bits) {
  $res = str_split(str_pad(str_pad('', $bits, '1'), 32, '0', STR_PAD_RIGHT), 8);
  foreach($res as &$bg) $bg = bindec($bg); 
  return join('.', $res); 
  }

echo bitMask(28);

Output

255.255.255.240

Note: It works for IPv4 only!

Wh1T3h4Ck5
  • 8,399
  • 9
  • 59
  • 79