Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting CIDR notation to IP range in java [duplicate]

How can I convert cidr to ip range in java ? I have used apache SubnetUtils but its ignoring first and last value of the range.

e.g for CIDR 192.168.1.0/16 output should be 192.168.0.0 to 192.168.255.255. but I am getting 192.168.0.1 to 192.168.255.254.

Please help

like image 718
Rahman Avatar asked Jun 30 '14 05:06

Rahman


2 Answers

The SubnetUtils class by default excludes the zeroth and last address from the range of usable "host" addresses because they are (or were, in the case of the zeroth address) used as the network broadcast address.

You need to invoke the .setInclusiveHostCount(true) method on your SubnetUtils instance to tell it to include those addresses within the returned range.

like image 176
Alnitak Avatar answered Sep 28 '22 02:09

Alnitak


Actually,first and last value will be ignored beacuse of the specialitty which it carries.Theses are not general purpose IP-Addresses.The Class C IP Addresses range will be this only.You need to read more about "Subnetting and Addressing in Class C Type IP-Addresses".

192.168.0.0--->By convention, network routers and other gateways use 192.168.0.0 to reference a private network generically!Being private, 192.168.0.0 and all other addresses within this network cannot be used on the Internet.It's not like a general IP-Address.It is the host's IP Address(by default),you can't change it!

192.168.255.255--->Broadcasting Address in the network,again not a general IP Address as it is used to broadcast the data to all other computers on the network!

like image 40
Am_I_Helpful Avatar answered Sep 28 '22 02:09

Am_I_Helpful