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

netaddr: add initial well-known IP addresses #25

Merged
merged 1 commit into from
Apr 30, 2020
Merged

Conversation

mdlayher
Copy link
Member

Signed-off-by: Matt Layher [email protected]

These are the only two "constants" I need at the moment. The standard library exposes a few more.

It'd be nice if these could be immutable but I think this is probably reasonable.

@bradfitz
Copy link
Contributor

It'd be ideal if we could do this without init work. And something the linker could discard.

@mdlayher
Copy link
Member Author

Yeah. I am curious if there is something more clever that could be done, but I also know that being able to check equality via == was an explicit goal with this type.

Here's a thought that comes to mind:

type WellKnown int

const (
    Unspecified WellKnown = iota
    IPv6LinkLocalAllNodes
)

func (ip IP) Is(known WellKnown) bool {}

// Usage:
ip, _ := netaddr.ParseIP("::")
fmt.Println(ip.Is(netaddr.Unspecified))
// true

@bradfitz
Copy link
Contributor

No, I was thinking just laying out the value in memory to help the linker (see golang/go#19533 and golang/go#38450 (comment)). Not a new type or API.

Something like:

var ip6Unspecified = IP{v6Addr{}}
func IPv6Unspecified() IP { return ip6Unspecified }

Then:

  • linker can discard it all (confirm that the unexported var can be discard to and doesn't make an init func?)
  • it doesn't allocate at runtuime
  • no new types
  • people can't modify exported package vars

@mdlayher
Copy link
Member Author

I see! I'm good with that.

@mdlayher
Copy link
Member Author

Updated.

@mdlayher
Copy link
Member Author

Any more thoughts? Thanks!

@mdlayher mdlayher merged commit 5aaf209 into master Apr 30, 2020
@mdlayher mdlayher deleted the mdl-wellknown branch April 30, 2020 17:50
@mdlayher
Copy link
Member Author

I'll take that as my "+2", but feel free to chime in or open issues/PRs if something's amiss! Thanks.

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

Successfully merging this pull request may close these issues.

2 participants