-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: add conversions between IP ranges and prefixes #65909
Comments
|
why would you want to iterate over 2001::/112? and how long can you wait for this iteration to go on? |
@thediveo @earthboundkid keep in mind that max len Prefixes(start, stop) is no sequence or iteration over an IP prefix, its just a conversion from a range to a slice of netip.Prefix with max. 254 items:
|
what's the difference with the previously declined #53236 ? |
In 2022 netip was brand new in stdlib with few users and the attention for this proposal was non-existent. In the meantime, some projects have switched from net/IP to net/netip, but often still have to include netipx or extnetip for this trivial functionality. And for 1.22 the window was opened for two other much less important compare methods, which do not bring any essential functionality but only convenience, in contrast this proposal is essential for a performant calculation between IP prefixes and IP ranges, which you need as a networker every day. IP filter lists are often not based on prefixes but on ranges, but ranges cannot be implemented performantly in routers, for this you need prefixes (longest-common-prefix-match), so you have to be able to convert ranges into prefixes quickly and vice versa. |
Data would help a lot here. E.g. what percent of projects that import net/netip also import netipx or extnetip? Of those that do, what APIs from netipx/extnetip do they use? |
@josharian the pure statistics speak against my suggestion, But please keep in mind that the limit of supported functionality of Maybe @danderson or @bradfitz as authors of netip and netipx can comment on this. Additionally, please keep in mind that IP libraries in other languages support exactly these conversions, because this is tricky for the normal user. |
Proposal Details
I suggest the following additional method/functions for the
net/netip
package:func (p Prefix) Range() (first, last Addr)
Range returns the inclusive range of IP addresses that p covers.
func PrefixFromRange(first, last Addr) (prefix Prefix, ok bool)
PrefixFromRange returns the IP Prefix from first to last and ok=true, if it can be presented exactly as such.
func Prefixes(first, last Addr) []Prefix
Prefixes returns the set of Prefix entries that covers the IP range from first to last.
func AppendPrefixes(dst []Prefix, first, last Addr) []Prefix
AppendPrefixes is an append version of Prefixes.
Reason:
An effective calculation of the last IP address of a
netip.Prefix
is only possible with the privateuint128
representation in the netip package. The same applies to the inverse conversion of an IP-range into a set of prefixes.I explicitly do not propose another type IPRange as used in
netipx
, but only the minimum necessary to build third party packages with IP-ranges based onnet/netip
.If these functions were in the stdlib, many packages could drop the dependency on
github.com/go4org/netipx
orjackfan.us.kg/gaissmai/extnetip
.see also: #61642 #53236
The text was updated successfully, but these errors were encountered: