Skip to content

Commit 14f8b7f

Browse files
committed
Remove dependency on apparentlymart/go-cidr
Fixes #651 Replace the go-cidr library with Go's standard netip package. The dependency was only used in pkg/tap/ip_pool.go for counting IPs and getting the nth host address in a subnet. Changes: - Convert IPPool to use netip.Prefix and netip.Addr instead of net.IPNet and net.IP - Replace cidr.Host() iteration with netip.Addr.Next() traversal - Remove cidr.AddressCount() by iterating directly through the prefix - Update callers in pkg/virtualnetwork and pkg/services/dhcp Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: Matus Skvarla <mskvarla@redhat.com>
1 parent 828275d commit 14f8b7f

10 files changed

Lines changed: 30 additions & 338 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.25.5
44

55
require (
66
github.com/Microsoft/go-winio v0.6.2
7-
github.com/apparentlymart/go-cidr v1.1.1
87
github.com/containers/winquit v1.1.0
98
github.com/coreos/stream-metadata-go v0.4.11
109
github.com/dustin/go-humanize v1.0.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
22
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
3-
github.com/apparentlymart/go-cidr v1.1.1 h1:oEEk8CE0HP0YpHxsegk/TaOtR2FLHdWv4p3eM4ceUwg=
4-
github.com/apparentlymart/go-cidr v1.1.1/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
53
github.com/armon/go-proxyproto v0.0.0-20210323213023-7e956b284f0a/go.mod h1:QmP9hvJ91BbJmGVGSbutW19IC0Q9phDCLGaomwTJbgU=
64
github.com/containers/winquit v1.1.0 h1:jArun04BNDQvt2W0Y78kh9TazN2EIEMG5Im6/JY7+pE=
75
github.com/containers/winquit v1.1.0/go.mod h1:PsPeZlnbkmGGIToMPHF1zhWjBUkd8aHjMOr/vFcPxw8=

pkg/services/dhcp/dhcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func handler(configuration *types.Configuration, ipPool *tap.IPPool) server4.Han
4444
return
4545
}
4646

47-
reply.YourIPAddr = ip
47+
reply.YourIPAddr = net.IP(ip.AsSlice())
4848
reply.UpdateOption(dhcpv4.OptServerIdentifier(net.ParseIP(configuration.GatewayIP)))
4949
reply.UpdateOption(dhcpv4.OptIPAddressLeaseTime(time.Hour))
5050

pkg/tap/ip_pool.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@ package tap
33
import (
44
"errors"
55
"maps"
6-
"math"
7-
"net"
6+
"net/netip"
87
"sync"
9-
10-
"github.com/apparentlymart/go-cidr/cidr"
118
)
129

1310
type IPPool struct {
14-
base *net.IPNet
15-
count uint64
11+
base netip.Prefix
1612
leases map[string]string
1713
lock sync.Mutex
1814
}
1915

20-
func NewIPPool(base *net.IPNet) *IPPool {
16+
func NewIPPool(base netip.Prefix) *IPPool {
2117
return &IPPool{
2218
base: base,
23-
count: cidr.AddressCount(base),
2419
leases: make(map[string]string),
2520
}
2621
}
@@ -34,37 +29,35 @@ func (p *IPPool) Leases() map[string]string {
3429
}
3530

3631
func (p *IPPool) Mask() int {
37-
ones, _ := p.base.Mask.Size()
38-
return ones
32+
return p.base.Bits()
3933
}
4034

41-
func (p *IPPool) GetOrAssign(mac string) (net.IP, error) {
35+
func (p *IPPool) GetOrAssign(mac string) (netip.Addr, error) {
4236
p.lock.Lock()
4337
defer p.lock.Unlock()
4438

4539
for ip, candidate := range p.leases {
4640
if candidate == mac {
47-
return net.ParseIP(ip), nil
41+
return netip.ParseAddr(ip)
4842
}
4943
}
5044

51-
if p.count > math.MaxInt {
52-
return nil, errors.New("IP pool exceeds maximum number of IP addresses")
53-
}
54-
for i := 1; i < int(p.count); i++ {
55-
candidate, err := cidr.Host(p.base, i)
56-
if err != nil {
57-
continue
58-
}
45+
// Start from the first usable IP (network address + 1)
46+
candidate := p.base.Masked().Addr().Next()
47+
48+
// Iterate through all IPs in the subnet
49+
for candidate.IsValid() && p.base.Contains(candidate) {
5950
if _, ok := p.leases[candidate.String()]; !ok {
6051
p.leases[candidate.String()] = mac
6152
return candidate, nil
6253
}
54+
candidate = candidate.Next()
6355
}
64-
return nil, errors.New("cannot find available IP")
56+
57+
return netip.Addr{}, errors.New("cannot find available IP")
6558
}
6659

67-
func (p *IPPool) Reserve(ip net.IP, mac string) {
60+
func (p *IPPool) Reserve(ip netip.Addr, mac string) {
6861
p.lock.Lock()
6962
defer p.lock.Unlock()
7063

pkg/tap/ip_pool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package tap
22

33
import (
4-
"net"
4+
"net/netip"
55
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
99

1010
func TestIPPool(t *testing.T) {
11-
_, network, _ := net.ParseCIDR("10.0.0.0/8")
11+
network := netip.MustParsePrefix("10.0.0.0/8")
1212
pool := NewIPPool(network)
1313

1414
ip1, err := pool.GetOrAssign("mac1")

pkg/virtualnetwork/virtualnetwork.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"math"
77
"net"
88
"net/http"
9+
"net/netip"
910
"os"
1011

1112
"github.com/containers/gvisor-tap-vsock/pkg/notification"
@@ -34,17 +35,25 @@ func (n *VirtualNetwork) SetNotificationSender(notificationSender *notification.
3435
}
3536

3637
func New(configuration *types.Configuration) (*VirtualNetwork, error) {
37-
_, subnet, err := net.ParseCIDR(configuration.Subnet)
38+
subnet, err := netip.ParsePrefix(configuration.Subnet)
3839
if err != nil {
3940
return nil, fmt.Errorf("cannot parse subnet cidr: %w", err)
4041
}
4142

4243
var endpoint stack.LinkEndpoint
4344

4445
ipPool := tap.NewIPPool(subnet)
45-
ipPool.Reserve(net.ParseIP(configuration.GatewayIP), configuration.GatewayMacAddress)
46+
gatewayIP, err := netip.ParseAddr(configuration.GatewayIP)
47+
if err != nil {
48+
return nil, fmt.Errorf("cannot parse gateway IP: %w", err)
49+
}
50+
ipPool.Reserve(gatewayIP, configuration.GatewayMacAddress)
4651
for ip, mac := range configuration.DHCPStaticLeases {
47-
ipPool.Reserve(net.ParseIP(ip), mac)
52+
addr, err := netip.ParseAddr(ip)
53+
if err != nil {
54+
return nil, fmt.Errorf("cannot parse static lease IP %s: %w", ip, err)
55+
}
56+
ipPool.Reserve(addr, mac)
4857
}
4958

5059
mtu := configuration.MTU

vendor/github.com/apparentlymart/go-cidr/LICENSE

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)