jgonian / commons-ip-math

https://github.com/jgonian/commons-ip-math
MIT License
74 stars 19 forks source link

Add conversions from IPv4 to IPv4-Mapped IPv6 addresses. #13

Open jgonian opened 8 years ago

jgonian commented 8 years ago

As mentioned in #9, extending the library in order to support conversions of an IPv4 address to an IPv4-Mapped IPv6 address and vice-versa could be useful in the case where we want to use IPv6 for dealing with both IPv4 and IPv6. Conversion should be according to section 2.5.5 of RFC 4291.

As an example:

// IP addresses
Ipv4 ipv4Address = Ipv4.parse("192.168.0.0");
Ipv6 ipv6Address = Ipv6.of(ipv4Address)            // ::ffff:c0a8:0
Ipv6 ipv6Address = ipv4Address.asIpv4MappedIpv6(); // ::ffff:c0a8:0
boolean ... = ipv6Address.isIpv4MappedIpv6()       // true

// IP ranges
Ipv4Range ipv4Range = Ipv4Range.parse("192.168.0.0/32")
Ipv6Range ipv6Range = ipv4range.asIpv4MappedIpv6();  // ::ffff:c0a8:0/120
...