Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: net/netip: function to split ip range to into cidr networks #53370

Closed
coopersong opened this issue Jun 14, 2022 · 7 comments
Closed

Comments

@coopersong
Copy link

coopersong commented Jun 14, 2022

Author background

  • Would you consider yourself a novice, intermediate, or experienced Go programmer?
    • I consider myself an intermediate Go programmer.
  • What other languages do you have experience with?
    • I have experience with C/C++, Python before Go.

Related proposals

  • Has this idea, or one like it, been proposed before?
    • No.
    • If so, how does this proposal differ?
  • Does this affect error handling?
    • No.
    • If so, how does this differ from previous error handling proposals?
  • Is this about generics?
    • No.
    • If so, how does this relate to the accepted design and other generics proposals?

Proposal

  • What is the proposed change?
    • Add common function of convert IP range to CIDR(s) in net/ip.go. BTW, Python already has such function in netaddr.iprange_to_cidrs(startIP, endIP).
  • Who does this proposal help, and why?
    • Network programmer will benefit from this change. Because they can convert IP range to CIDR(s) easily just by call net.ConvertIPRangeToCIDRs.
  • Please describe as precisely as possible the change to the language.
    • This proposal will provide more support for IP and CIDR. We do not have the power to convert IP range to CIDR(s) in Go before. With this propasal, for example, [192.168.1.0, 192.168.1.255] can be converted to [192.168.1.0/24], [192.168.1.0, 192.168.1.254] can be converted to [192.168.1.0/25 192.168.1.128/26 192.168.1.192/27 192.168.1.224/28 192.168.1.240/29 192.168.1.248/30 192.168.1.252/31 192.168.1.254/32]. As more and more network related projects are choosing Go as their developing language, I think we do need this change and benefit them.
  • What would change in the language spec?
    • No change in language spec.
  • Please also describe the change informally, as in a class teaching Go.
    • With this change, we can convert IP range to CIDR(s) by call net.ConvertIPRangeToCIDRs(beginIP, endIP) like this.
  • Is this change backward compatible?
    • Yes, this change is backward compatible.
    • Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit.
      Show example code before and after the change.
    • Before
    • After
  • Orthogonality: how does this change interact or overlap with existing features?
    • This change will call existing function under net package to parse IP address as I want to provide simple strings as params in order to be convenient for use.
  • Is the goal of this change a performance improvement?
    • No.
    • If so, what quantifiable improvement should we expect?
    • How would we measure it?

Costs

  • Would this change make Go easier or harder to learn, and why?
    • This change will make Go easier to use for network programmer. Because this change provide a common function for convert IP range to CIDR(s).
  • What is the cost of this proposal? (Every language change has a cost).
    • The size of net/ip.go will increase.
  • How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
    • No tool would be affected.
  • What is the compile time cost?
    • Not sure.
  • What is the run time cost?
    • Not sure.
  • Can you describe a possible implementation?
    • Enum every possible CIDR that the IP range can contain, choose the CIDR with max IP addresses as the result. This is implemented by DFS(Deep First Search) algorithm. There is an existing implementation by me. You can refer to github.com/coopersong/irtc. Now I want to move this part to net/ip.go.
  • Do you have a prototype? (This is not required.)
    • No.
@gopherbot gopherbot added this to the Proposal milestone Jun 14, 2022
@coopersong coopersong changed the title proposal: Go 2: proposal: Go 2: Provide Convert IP range to CIDR(s) function in net/ip.go Jun 14, 2022
@seankhliao seankhliao changed the title proposal: Go 2: Provide Convert IP range to CIDR(s) function in net/ip.go proposal: net/netip: function to split ip range to into cidr networks Jun 14, 2022
@seankhliao
Copy link
Member

seankhliao commented Jun 14, 2022

not a go2 proposal

looks like https://pkg.go.dev/inet.af/netaddr#IPSet.Prefixes

@seankhliao
Copy link
Member

cc @josharian @bradfitz

@josharian
Copy link
Contributor

As I commented at #53236 (comment):

When bringing netip into std, there was an explicit decision to leave out the IP address calculation support in netaddr. I think if we want to start adding it, we should consider adding the full suite rather than nickel and diming it.

Can you use netaddr's IPSet?

@coopersong
Copy link
Author

OK, you mean there is no need to add such function into net/ip.go? And if we want to use this function, we can just import the third-party package netaddr? right?

@josharian
Copy link
Contributor

@coopersong yes, that’s the idea.

@coopersong
Copy link
Author

OK, got it. Thanks~

@ianlancetaylor
Copy link
Member

Sounds like there is nothing to change, so closing.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Jun 15, 2022
@golang golang locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants