Friday, August 3, 2007

Syntax Highlight?

The last long rambling post was partially to test out layout. So far, fail.

The pseudo edit in html and flip back to compose is impressive, but it's not playing nice with other elements. Though it does seem to take the html it doesn't mangle fairly well.

If you're going to post code, you want some kind of highlighter. Many blogs leverage GeSHi - Generic Syntax Highlighter. Can that be done here?

If you loose indents on C++ it's no bit deal, it will run fine and most C code is written to be cryptic anyway. But something like python, that puppy is dead on arrival without indents.

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.

What's it all about?

I'm a computer programmer, for both fun and profit. I write code for work. I play with computers and write code for amusement. I produce a whole lot of code. I'm also a firm supported of Open Source and like to share when possible.

Open Source is many things to many folks. But to a developer, it's special. Above all else, it's offers a chance to learn how other people solve problems. Some solutions will strike the viewer as elegant, some cumbersome, regardless there is always something to be learned.

Anything I post here is to be considered public domain. If anyone finds some bit of information useful, I'd love to here it. If someone feels they have a better way, such wisdom is always welcome.

I can't say what I'll be posting. I play with lots of different toys. I'll try to have more code than rhetoric.