Identification of IPV4 and IPV6
IPV4 vs IPV6:
Problem Scenario: I was involved in developing an windows application which suppose to work on windows 2003, windows xp or earlier version. But at the time of deploying the application to the one machine which is using Windows Vista as OS, my application crashes. It is not getting started. But the same application is working in other Test system having Vista OS.
Solution: After digging into the code I finally found the problem. Before starting, the application it collects few system information’s like Host Name, Host IP address etc and saves into the database. And Client Vista system is using IPV6 format address where as the application is expecting IPV4 address format. Here I came across the need to fetch the IPV4 address if some system is using IPV6 address.
What are IPV4 and IPV6?
IPv6 stands for Internet Protocol version 6 which is the next-generation Internet Layer protocol for packet-switched internetworks and the Internet. IPv4 is currently the dominant Internet Protocol version, and was the first to receive widespread use. In December 1998, the Internet Engineering Task Force (IETF) designated IPv6 as the successor to version 4 by the publication of a Standards Track specification.
Get Into the problem.
In my application it is expecting IPV4 address format, which can be split into four sections by “.” character and each section represents an integer. On the contrary IPv6 address size is 128 bits and the preferred IPv6 address representation is: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx where each x is a hexadecimal digit representing 4 bits. IPv6 addresses range from 0000:0000:0000:0000:0000:0000:0000:0000 to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff. So we need to fetch list of all ip addresses on the host system and fetch the V4 representation of IP address.
Code to fetch the V4 representation of IP address:
Dim IP4Address As String = String.Empty
For Each IPA As IPAddress In Dns.GetHostAddresses(Dns.GetHostName())
If IPA.AddressFamily.ToString() = “InterNetwork” Then
IP4Address = IPA.ToString()
Exit For
End If
Next
The GetHostAddresses() method queries a DNS server for the IP addresses associated with a host name
For V4 comes under address family “InterNetwork”.
Also we can serve the same purpose by using Dns. GetHostByName() method but it advisable to GetHostEntry() or GetHostAddresses() method as GetHostByName() become obsolete. Also we can use “IsIPv6SiteLocal”, “IsIPv6LinkLocal” properties of IPAddress to determine if the current address is V6 format.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Leave a comment