Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php cidr prefix to netmask

Tags:

php

ipv4

cidr

I'm looking to convert a cidr prefix (e.g. /28) to a netmask (e.g. 255.255.255.240) and have not located a function for this, does one exist in php? If not, how would I go about doing this?

like image 496
phpnoobipv4 Avatar asked Apr 19 '11 02:04

phpnoobipv4


People also ask

What is the mask 255.255 255.0 in CIDR notation?

The CIDR number comes from the number of ones in the subnet mask when converted to binary. The subnet mask 255.255. 255.0 is 11111111.11111111.

What is the subnet mask corresponding to a CIDR value of 16?

Class B IP Addresses 255.255 , with a default subnet mask of 255.255. 0.0 (or /16 in CIDR).


1 Answers

I wrote a class that contains a method that does this.

function CIDRtoMask($int) {
    return long2ip(-1 << (32 - (int)$int));
}

I posted the entire class as a gist.

like image 110
jonavon Avatar answered Sep 20 '22 01:09

jonavon