-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
deps: remove iplib in favor of net/netip #2466
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
ac27614
1d879a8
04d3d5f
4f42493
c35f046
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,13 @@ import ( | |
| "fmt" | ||
| "maps" | ||
| "net" | ||
| "net/netip" | ||
| "regexp" | ||
| "runtime" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/BurntSushi/toml" | ||
| "github.com/c-robinson/iplib" | ||
| "github.com/knqyf263/go-cpe/naming" | ||
| "golang.org/x/xerrors" | ||
|
|
||
|
|
@@ -193,27 +193,29 @@ func enumerateHosts(host string) ([]string, error) { | |
| return []string{host}, nil | ||
| } | ||
|
|
||
| ipAddr, ipNet, err := net.ParseCIDR(host) | ||
| prefix, err := netip.ParsePrefix(host) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("Failed to parse CIDR. err: %w", err) | ||
| } | ||
| maskLen, _ := ipNet.Mask.Size() | ||
|
|
||
| addrs := []string{} | ||
| if net.ParseIP(ipAddr.String()).To4() != nil { | ||
| n := iplib.NewNet4(ipAddr, int(maskLen)) | ||
| for _, addr := range n.Enumerate(int(n.Count()), 0) { | ||
| addrs = append(addrs, addr.String()) | ||
| } | ||
| } else if net.ParseIP(ipAddr.String()).To16() != nil { | ||
| n := iplib.NewNet6(ipAddr, int(maskLen), 0) | ||
| if !n.Count().IsInt64() { | ||
| return nil, xerrors.Errorf("Failed to enumerate IP address. err: mask bitsize too big") | ||
| } | ||
| for _, addr := range n.Enumerate(int(n.Count().Int64()), 0) { | ||
| addrs = append(addrs, addr.String()) | ||
| } | ||
| hostBits := prefix.Addr().BitLen() - prefix.Bits() | ||
| if hostBits > 63 { | ||
| return nil, xerrors.Errorf("Failed to enumerate IP address. err: mask bitsize too big") | ||
| } | ||
|
|
||
| count := uint64(1) << hostBits | ||
| addrs := make([]string, 0, count) | ||
| addr := prefix.Masked().Addr() | ||
| for range count { | ||
| addrs = append(addrs, addr.String()) | ||
| addr = addr.Next() | ||
| } | ||
|
Comment on lines
+201
to
+215
|
||
|
|
||
| // IPv4 with prefix < 31: strip network (first) and broadcast (last) addresses | ||
| if prefix.Addr().Is4() && prefix.Bits() < 31 && len(addrs) > 2 { | ||
| addrs = addrs[1 : len(addrs)-1] | ||
| } | ||
|
|
||
| return addrs, nil | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
countis auint64, butmake([]string, 0, count)requires anintcapacity/length (per Go spec). As written this won’t compile (or will require an implicit conversion that Go doesn’t do). Convertcounttointafter checking it fits inmath.MaxInt(and adjust the loop accordingly).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c35f046. Changed
countfromuint64tointsomake([]string, 0, count)receives the correct type directly. Additionally, the hard upper bound was tightened from 2^63 to 2^16 host bits, which guaranteescountalways fits comfortably in aninton any platform.