-4

I'm writing a code, which will output the first and last IP address of every subnet from a given IP.

for example if the IP address is 192.168.1.8/28 so if we calculate there are total 16 subnets. So, how do I find the start and last IP address of any subnet in it? (like 12th or 13th)

NOTE: (I'm looking for the logic not the actual code)

  • 4
    I'm voting to close this question as off-topic because it's either homework, a course item, or a programming challenge question that OP has not shown any proof of solving on his own first. – WGS Oct 05 '15 at 03:51
  • How do arrive at the idea that `192.168.1.8/28` means that there are 16 subnets? There are 16 host addresses in that network, and, I suppose if you had a `/32` on each address, you may want to call that 16 subnets of the `192.168.1.0/28` network, but that is not entirely accurate. – Ron Maupin Oct 05 '15 at 04:01
  • @TheLaughingMan first of all its not any homework type or programming challenge question. And it's not a off type question its genuine question one may have to make his program more useful. I didn't get you why would you like to close this question. I have tried to solve it but I didn't get any simple solution rather than usual regular type solution. So that's why I asked this question. – Rushikesh Gaidhani Oct 05 '15 at 10:37
  • @RonMaupin I am considering IP address as 32bit. So maximum CIDR notation you will have is /32. Now back to the example that I have mentioned 192.168.1.8/28 is of Class C type IP address so valid CIDR notation is greater than /24 . I have given /28 so 4 bits are used as subnet bits which can give 16 subnet resulting in 16 hosts in a particular subnet. – Rushikesh Gaidhani Oct 05 '15 at 10:48
  • Classful addressing is dead; RFCs 1518 (1993), 1519 (1993), and 1817 (1995) killed it. It is completely obsolete, and everyone needs to get over it. As the millenials say, "That is soooo last-century, Dude!" You are declaring the network with the `/28`, so a subnet would be a longer mask than that. You should modify the question to clarify that you mean the network to be `/24`; something like `192.168.1.0/24` with subnet sizes of `/28`, or you could say, "`192.168.1.8/28` is an address in one of 16 contiguous subnets," but don't assume classful addressing. – Ron Maupin Oct 05 '15 at 13:41

1 Answers1

0

Based on your comments, I think you get the idea that IPv4 addresses and masks are each 32-bit unsigned-integers. With just these two values, you can get anything you need about an IP address. You need the actual 32-bit unsigned-integer mask to do IP math.

  • To get a mask from the mask length, you simply set that number of high-order bits on in a 32-bit unsigned-integer.
  • To get a network from an address, you simply AND the address with the mask.
  • In most cases, for IPv4, the first usable address in a network is network + 1. This is a little more complicated for /31 and /32 networks because the first usable address is the actual network address in those corner cases.
  • The broadcast address for a network is the network address plus the NOT, or inverse, of the mask.
  • In most cases, for IPv4, the last usable address in a network is the broadcast address minus 1. Again, /31 and /32 networks are corner cases because the last usable address is the broadcast address for the network.

This information should be enough to get you started writing the code.

Ron Maupin
  • 6,180
  • 4
  • 29
  • 36