Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

VB6-SubNet representation

$
0
0
Applying a mask to an IP address is relatively straight forward in "C". You simply do a bit wise "AND" between the two. Because Visual Basic does not do bit arithmetic very well, it is a little more complicated.

Both IPv4 and IPv6 drop leading zeros, so they are an inconsistent length. IPv4 uses four bytes (32 bits), displayed as decimal numbers separated by dots (eg. 192.168.1.2), whereas IPv6 uses 16 bytes (128 bits) displayed as hexadecimal numbers separated by colons (eg. 2001:0:53aa:64c:406:8e9d:934b:458d). IPv6 is even more difficult to work with, as a group of zero elements can be replaced with a double colon (eg. 2620:0:ccc::2). The easiest way to work with them is to convert them to byte arrays, but since they are almost always transmitted and displayed as text, I found the best way is to convert them both to hexadecimal strings and padd the leading zeros.

Classless Inter-Domain Routing (CIDR) representation is the same(eg. 192.168.1.2/32 & 2001:0:53aa:64c:406:8e9d:934b:458d/128) for both. The part behind the "/" is the number of significant bits. For example, 192.168.1.0/24 is the same as using 192.168.1.0 with a netmask of 255.255.255.0.

The attached program allows you to enter an IPv4 or IPv6 network using CIDR. The program then converts the network into a fixed length starting address and a fixed length netmask. For example, 2620:0:ccc::2/120 is converted to: 262000000ccc00000000000000000000/FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
Notice that the last 2 in the address has been dropped. That is because the last byte (8 bits) is insignificant, so the starting address is actually 2620:0:ccc::0.

Once you have defined the network, you can enter an address in the bottom text box to see if it belongs to the defined subnet.

J.A. Coutts
Attached Files

Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>