0
votes

I am trying to 'represent' an IPv6 address(those IPv6 addresses with embedded IPv4 address) to its equivalent IPv4 address in Visual Basic 2012. The following is the code snippet that I use for the same:

Dim ip As IPAddress = IPAddress.Parse(strIP)
Dim ipv4 As IPAddress
Dim ipStr As String = ip.ToString()
IPAddress.TryParse(ipStr.Substring(ipStr.LastIndexOf(":") + 1), ipv4)
Console.Writeline(ipv4.MapToIPv4().ToString)

But with the loopback interface( 127.0.0.1), the ipv6 representation seems to be ::1 as opposed to 0:0:0:0:0:ffff:7f00:1? Is this address the only "deviation" ? are there other addresses which need "some special" treatment?

Thanks!

2

2 Answers

1
votes

You can't "convert" an IPv6 address to IPv4 so I'm not really sure what you are trying to achieve.

Are you referring to the IPAddress.IsIPv4MappedToIPv6 Property? This is not the same thing. Note this property was introduced in .Net 4.5 so will not work in VB2012

You are correct in saying that the loopback adress for IPv6 is ::1

Not sure if this helps but you can parse a string into a .Net IPAddress and check the IPAddress.AddressFamily Property to see if it is set to InternNetworkV6 if it is the IPAddress is v6.

e.g.

Dim ip1 As IPAddress = IPAddress.Parse("::1")
Dim ip2 As IPAddress = IPAddress.Parse("127.0.0.1")
Debug.WriteLine("Is ip1 a v6 address:" + (ip1.AddressFamily = Sockets.AddressFamily.InterNetworkV6).ToString)
Debug.WriteLine("Is ip2 a v6 address:" + (ip2.AddressFamily = Sockets.AddressFamily.InterNetworkV6).ToString)

Output:

Is ip1 a v6 address:True
Is ip2 a v6 address:False
0
votes

My understanding is that .NET (v3.5 to 4.5.2, in my testing) will return IPv6 addresses with two colons, like this -

Dim host As String = Dns.GetHostName()
Dim ip As IPHostEntry = Dns.GetHostEntry(host)
Console.WriteLine(ip.AddressList(0).ToString())
Console.ReadKey()

which outputs fe80::2d36:51be:16dc:257b%36 in my situation, a wireless connection through a gateway to an ADSL modem. My interest in your problem is that I would like to return the IPv4 address, which I know to be 203.214.36.136 (until I reset the modem). I can see that by logging into my ISP, or by using code like this -

Using client = New WebClient()
    Dim ret As String = client.DownloadString("http://icanhazip.com/").Trim()
    Console.WriteLine(ret)
    Console.ReadKey()
    client.Dispose()
 End Using

The web page used will return an IPv4 IP address (which I can verify is correct by both logging into my ADSL modem, and into my ISP's webpage). I'm not sure what code on the page redirects to return the IP address (I copied this from a SO answer also dealing with IP addressing).

Trying your code for which uses .MaptoIPv4 (of course I used VS2012 or 2013, with .NET v4.5) I also used a null value for ipv4 (your variable), but the string value returned from my first code block above, for your variable strIP.

It failed.

I'm still trying to nut out just how these methods should be used.

[edit] Actually, I'm wrong in assuming the first IP address in the host AddressList is the one I want. There are 3, 2 of them IPv6, and one IPv4 which is the gateway to my modem.

So I haven't contributed much to the solution. Sorry.