6

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

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
Rahman
  • 3,755
  • 3
  • 26
  • 43

2 Answers2

17

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

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!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • `192.168.0.0` isn't a 'local address', it is another reserved address, for historical reasons (used to be broadcast). – user207421 Jun 30 '14 at 06:09
  • @EJP-basically,that's the address which is used by/for the system when it's not connected to the network! – Am_I_Helpful Jun 30 '14 at 06:11
  • 1
    @shekharsuman _"when it's not connected to the network"_ - citation needed. Also "classfull" addressing became obsolete about two decades ago. – Alnitak Jun 30 '14 at 06:36
  • and in any event, the example given in the question was about a /16, not a "class C" style /24. All that stuff you've written about 192.168.0.0 being used as the address "when it's not connected to the network" is complete bullcrap. – Alnitak Jun 30 '14 at 06:43
  • @Alnitak-Well,Class C addresses start from 192.0.0.0--->It's not bullcrap! Also,"when it's not connected to the network"-it's not bullcrap.Check this--->http://compnetworking.about.com/od/workingwithipaddresses/g/0_0_0_0_ip-address.htm I think your knowledge might be conflicting with it.Please verify and comment as to what's wrong in it!!! – Am_I_Helpful Jun 30 '14 at 06:51
  • @shekharsuman I have nearly 25 years experience in IP networking and operations and four RFCs to my name. Indeed the "class C" range *did* start at 192/8, but that default behaviour of assuming that anything from 192/8 to 223/8 is a Class C with a /24 subnet mask went obsolete about 20 years ago and was replaced with CIDR. The OP specified a /16 so he is explicitly _not_ using "class C" classful addressing. – Alnitak Jun 30 '14 at 06:54
  • Sorry SIR,but at least it belongs to Class `C`---though it has become obsolete can't deny the fact it comes under Class C.Also,I have edited my answer as per your saying,at that time I mistakenly referred to `0.0.0.0` instead of `192.168.0.0`.Thanks for pointing out the answer SIR.THANKS.Also,please comment if there are any more changes to be done! – Am_I_Helpful Jun 30 '14 at 06:56
  • Also,as I can see your `high` qualified bio,THANKS really for pointing out.Any more changes needed sir? – Am_I_Helpful Jun 30 '14 at 06:59
  • See http://tools.ietf.org/html/rfc1518 - class C _is completely irrelevant to the OP's question_, and unless you're studying for a CCNA/CCIE (where bizarrely Cisco still require this knowledge) no-one these days needs to know about it. – Alnitak Jun 30 '14 at 06:59
  • Pardon ME,but those are pretty boring to read for me.I'll,when free.Actually,my field of expertise is being developed under OS department!Again,A BIG THANKS. – Am_I_Helpful Jun 30 '14 at 07:02