'Golang: net.ParseIP()
some perfectly valid IPv6 addresses are considered IPv4 by net.ParseIP(), e.g. 0000:0000:0000:0000:0000:ffff:0100:0000 / ::ffff:0100:0. (please see https://play.golang.org/p/57jAJVSIrHF for a simple example)
What would be the most idiomatic way to deal with this situation? The net package does not export most of the functionality, so rewriting net.Parse would result in a lot of code duplication.
Thank you in advance for your help!
best regards,
Stefan
Solution 1:[1]
The IPAddress Go library provides more fine-grained control over such addresses. Disclaimer: I am the project manager.
addrStr := ipaddr.NewIPAddressString("::ffff:0100:0")
addr := addrStr.GetAddress()
converter := ipaddr.DefaultAddressConverter{}
var ipv4Addr *ipaddr.IPv4Address
if converter.IsIPv4Convertible(addr) {
ipv4Addr = converter.ToIPv4(addr)
}
fmt.Printf("original IPv4-mapped IPv6 address is %v, IPv4 address is %v",
addr, ipv4Addr)
Output:
original IPv4-mapped IPv6 address is ::ffff:100:0, IPv4 address is 1.0.0.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sean F |
