Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Method to turn an IPv4 into a mapped 4in6 IPv6 #220

Closed
Fryuni opened this issue Aug 8, 2022 · 3 comments
Closed

Method to turn an IPv4 into a mapped 4in6 IPv6 #220

Fryuni opened this issue Aug 8, 2022 · 3 comments

Comments

@Fryuni
Copy link

Fryuni commented Aug 8, 2022

There is Unmap to turn a 4in6 IPv6 into an IPv4, but there is no way to do the other way around.

This showed up when I was developing a library that has to accept both as the same, but the only way I found to do this conversion was through the standard library with netaddr.FromStdIP(ip.IPAddr().IP.To16()). This is horrible since it does 2 separate allocations to do what could be just an assignment.

I searched for any way to do this and the only thing I found was a mention that this wasn't yet implemented back in 2020.

@bradfitz
Copy link
Contributor

bradfitz commented Aug 8, 2022

The netaddr package is now deprecated. You should use net/netip in the standard library instead, and https://pkg.go.dev/go4.org/netipx for the bits of this netaddr package that didn't make it into net/netip yet.

As for your question, use netaddr.IPv6Raw(ip.As16()) which doesn't allocate.

@bradfitz bradfitz closed this as completed Aug 8, 2022
@Fryuni
Copy link
Author

Fryuni commented Aug 8, 2022

Thanks @bradfitz!

@Fryuni
Copy link
Author

Fryuni commented Aug 8, 2022

I think the new package should be mentioned on the README, I had not seen any link to it till now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants