Friday, August 3, 2007

Network Calc

Networking 101, computer networks can be parsed off into subnet. This leads to this cryptic kind of syntax 192.168.0.1/25 which can also be expressed as 192.168.0.1/255.255.255.128. This is a typical class C network two subnet setup.

But, what the hell does that mean. I have two subnets, but what are the ranges? And what's the logic? After exhaustive searching and a myriad of online subnet calculators, I found not a single one that would list off the subnets for me. I'm sure a veteran routing guru wouldn't see the point to such a listing as they do the bit mapping in their skulls. But, well, I'm not a networking guy and I wanted to know.

I'm going to ignore the first three octets and just look at my last little 0-255. Well, typically, if I'm /24 then 0 is a gateway, 255 is broadcast, and 1-254 are mine to play with. The whole /n thing is kind of annoying if you don't have notes; here's what it means.


/24 = 255.255.255.0 = 11111111.11111111.11111111.00000000
/25 = 255.255.255.128 = 11111111.11111111.11111111.10000000
/26 = 255.255.255.192 = 11111111.11111111.11111111.11000000


Didn't catch it? It's /NumberOfBits from the top. And that number must be greater that 23 to be class C, because in C we only really care about the last octet. Octet meaning a cluster of eight bits. Yeah, jargon is fun.

So, in /24 0 and 255 are reserved for gateway and broadcast. And in /26 you have 4 subnets, each with their own gateway and broadcast. You also have four clusters addresses to use. For someone as math challenged as myself, figuring out what those ranges are is no trivial. There should be a list...

Couldn't find one.

Doing /26 by hand isn't so bad. It's actually kind of educational.


1 00000001-00111110 001-062
2 01000001-01111110 065-126
3 10000001-10111110 129-190
4 11000001-11111110 193-254


At last, that makes sense! Well, to me at least. Note the first to bits are our mask and that the range starts one from the top and one from the end. Well, two bits is easy enough. But what about four? Nope, not me, not going to do it. But I'll write a script to do it.

#!/usr/bin/python

def EnumSubs(subnetBits):
rangeList = []
for bit in range(0, pow(2, subnetBits) ):
top = ( bit << (8 - subnetBits) ) + 1 bottom = ( top | ( 255 >> subnetBits ) ) - 1
rangeList.append("%d:%d-%d" % (( bit + 1 ), top, bottom))
return ", ".join(rangeList)


for i in range(24,29): # for i in range(24,31):
subnetBits = i - 24
oct4 = ( 255 >> subnetBits ) ^ 255
print "255.255.255.%s or /%d\n %s" \
% (oct4, i, EnumSubs(subnetBits))



Output:

255.255.255.0 or /24
1:1-254
255.255.255.128 or /25
1:1-126, 2:129-254
255.255.255.192 or /26
1:1-62, 2:65-126, 3:129-190, 4:193-254
255.255.255.224 or /27
1:1-30, 2:33-62, 3:65-94, 4:97-126, 5:129-158, 6:161-190, 7:193-222, 8:225-254
255.255.255.240 or /28
1:1-14, 2:17-30, 3:33-46, 4:49-62, 5:65-78, 6:81-94, 7:97-110, 8:113-126, 9:129-142, 10:145-158, 11:161-174, 12:177-190, 13:193-206, 14:209-222, 15:225-238, 16:241-254


I don't know if that clears up subnets for anyone but me, but I had fun.

1 comment:

Anonymous said...

Interesting to know.