diff --git a/internal/aghnet/dhcp_unix.go b/internal/aghnet/dhcp_unix.go index a5287b1aa41..16c3c87aff0 100644 --- a/internal/aghnet/dhcp_unix.go +++ b/internal/aghnet/dhcp_unix.go @@ -42,7 +42,7 @@ func checkOtherDHCP(ifaceName string) (ok4, ok6 bool, err4, err6 error) { func ifaceIPv4Subnet(iface *net.Interface) (subnet netip.Prefix, err error) { var addrs []net.Addr if addrs, err = iface.Addrs(); err != nil { - return subnet, err + return netip.Prefix{}, err } for _, a := range addrs { @@ -60,9 +60,7 @@ func ifaceIPv4Subnet(iface *net.Interface) (subnet netip.Prefix, err error) { } if ip = ip.To4(); ip != nil { - subnet = netip.PrefixFrom(netip.AddrFrom4(*(*[4]byte)(ip)), maskLen) - - return subnet, nil + return netip.PrefixFrom(netip.AddrFrom4(*(*[4]byte)(ip)), maskLen), nil } } diff --git a/internal/aghnet/hostscontainer.go b/internal/aghnet/hostscontainer.go index 5d7509455bf..9b9124d9506 100644 --- a/internal/aghnet/hostscontainer.go +++ b/internal/aghnet/hostscontainer.go @@ -106,9 +106,13 @@ type HostsContainer struct { done chan struct{} // updates is the channel for receiving updated hosts. + // + // TODO(e.burkov): Use map[netip.Addr]struct{} instead. updates chan *netutil.IPMap // last is the set of hosts that was cached within last detected change. + // + // TODO(e.burkov): Use map[netip.Addr]struct{} instead. last *netutil.IPMap // fsys is the working file system to read hosts files from. diff --git a/internal/aghnet/net.go b/internal/aghnet/net.go index 5c0f6d1b9e1..bdcc6e4f3ca 100644 --- a/internal/aghnet/net.go +++ b/internal/aghnet/net.go @@ -58,18 +58,18 @@ func GatewayIP(ifaceName string) (ip netip.Addr) { if err != nil { log.Debug("%s", err) - return ip + return netip.Addr{} } else if code != 0 { log.Debug("fetching gateway ip: unexpected exit code: %d", code) - return ip + return netip.Addr{} } fields := bytes.Fields(out) // The meaningful "ip route" command output should contain the word // "default" at first field and default gateway IP address at third field. if len(fields) < 3 || string(fields[0]) != "default" { - return ip + return netip.Addr{} } ip, err = netip.ParseAddr(string(fields[2])) diff --git a/internal/aghnet/net_linux.go b/internal/aghnet/net_linux.go index 86e41705059..fc83d56a3d7 100644 --- a/internal/aghnet/net_linux.go +++ b/internal/aghnet/net_linux.go @@ -187,7 +187,7 @@ func dhcpcdConfIface(ifaceName string, subnet netip.Prefix, gateway netip.Addr) "\n", ) - if gateway.IsValid() { + if gateway != (netip.Addr{}) { stringutil.WriteToBuilder(b, "static routers=", gateway.String(), "\n") } diff --git a/internal/aghnet/net_test.go b/internal/aghnet/net_test.go index 67e4f9125ec..730f1b4ee48 100644 --- a/internal/aghnet/net_test.go +++ b/internal/aghnet/net_test.go @@ -99,17 +99,12 @@ func TestGatewayIP(t *testing.T) { name string }{{ shell: theOnlyCmd(cmd, 0, `default via 1.2.3.4 onlink`, nil), - want: netip.AddrFrom4([4]byte{1, 2, 3, 4}), + want: netip.MustParseAddr("1.2.3.4"), name: "success_v4", }, { shell: theOnlyCmd(cmd, 0, `default via ::ffff onlink`, nil), - want: netip.AddrFrom16([16]byte{ - 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0x0, 0x0, - 0x0, 0x0, 0xFF, 0xFF, - }), - name: "success_v6", + want: netip.MustParseAddr("::ffff"), + name: "success_v6", }, { shell: theOnlyCmd(cmd, 0, `non-default via 1.2.3.4 onlink`, nil), want: netip.Addr{}, @@ -151,15 +146,10 @@ func TestInterfaceByIP(t *testing.T) { } func TestBroadcastFromIPNet(t *testing.T) { - known4 := netip.AddrFrom4([4]byte{192, 168, 0, 1}) - fullBroadcast4 := netip.AddrFrom4([4]byte{255, 255, 255, 255}) - - known6 := netip.AddrFrom16([16]byte{ - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16, - }) + known4 := netip.MustParseAddr("192.168.0.1") + fullBroadcast4 := netip.MustParseAddr("255.255.255.255") + + known6 := netip.MustParseAddr("102:304:506:708:90a:b0c:d0e:f10") testCases := []struct { pref netip.Prefix @@ -171,7 +161,7 @@ func TestBroadcastFromIPNet(t *testing.T) { name: "full", }, { pref: netip.PrefixFrom(known4, 20), - want: netip.AddrFrom4([4]byte{192, 168, 15, 255}), + want: netip.MustParseAddr("192.168.15.255"), name: "full", }, { pref: netip.PrefixFrom(known6, netutil.IPv6BitLen), diff --git a/internal/dhcpd/dhcpd_unix_test.go b/internal/dhcpd/dhcpd_unix_test.go index 032a41b8a3c..f13dbf9824a 100644 --- a/internal/dhcpd/dhcpd_unix_test.go +++ b/internal/dhcpd/dhcpd_unix_test.go @@ -34,10 +34,10 @@ func TestDB(t *testing.T) { s.srv4, err = v4Create(&V4ServerConf{ Enabled: true, - RangeStart: netip.AddrFrom4([4]byte{192, 168, 10, 100}), - RangeEnd: netip.AddrFrom4([4]byte{192, 168, 10, 200}), - GatewayIP: netip.AddrFrom4([4]byte{192, 168, 10, 1}), - SubnetMask: netip.AddrFrom4([4]byte{255, 255, 255, 0}), + RangeStart: netip.MustParseAddr("192.168.10.100"), + RangeEnd: netip.MustParseAddr("192.168.10.200"), + GatewayIP: netip.MustParseAddr("192.168.10.1"), + SubnetMask: netip.MustParseAddr("255.255.255.0"), notify: testNotify, }) require.NoError(t, err) @@ -114,35 +114,35 @@ func TestNormalizeLeases(t *testing.T) { func TestV4Server_badRange(t *testing.T) { testCases := []struct { name string - wantErrMsg string gatewayIP netip.Addr subnetMask netip.Addr + wantErrMsg string }{{ - name: "gateway_in_range", + name: "gateway_in_range", + gatewayIP: netip.MustParseAddr("192.168.10.120"), + subnetMask: netip.MustParseAddr("255.255.255.0"), wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " + "192.168.10.20-192.168.10.200", - gatewayIP: netip.AddrFrom4([4]byte{192, 168, 10, 120}), - subnetMask: netip.AddrFrom4([4]byte{255, 255, 255, 0}), }, { - name: "outside_range_start", + name: "outside_range_start", + gatewayIP: netip.MustParseAddr("192.168.10.1"), + subnetMask: netip.MustParseAddr("255.255.255.240"), wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " + "192.168.10.1/28", - gatewayIP: netip.AddrFrom4([4]byte{192, 168, 10, 1}), - subnetMask: netip.AddrFrom4([4]byte{255, 255, 255, 240}), }, { - name: "outside_range_end", + name: "outside_range_end", + gatewayIP: netip.MustParseAddr("192.168.10.1"), + subnetMask: netip.MustParseAddr("255.255.255.224"), wantErrMsg: "dhcpv4: range end 192.168.10.200 is outside network " + "192.168.10.1/27", - gatewayIP: netip.AddrFrom4([4]byte{192, 168, 10, 1}), - subnetMask: netip.AddrFrom4([4]byte{255, 255, 255, 224}), }} for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { conf := V4ServerConf{ Enabled: true, - RangeStart: netip.AddrFrom4([4]byte{192, 168, 10, 20}), - RangeEnd: netip.AddrFrom4([4]byte{192, 168, 10, 200}), + RangeStart: netip.MustParseAddr("192.168.10.20"), + RangeEnd: netip.MustParseAddr("192.168.10.200"), GatewayIP: tc.gatewayIP, SubnetMask: tc.subnetMask, notify: testNotify, diff --git a/internal/dhcpd/v4_unix_test.go b/internal/dhcpd/v4_unix_test.go index 2d8b67c3d1c..c8e4dd1f1eb 100644 --- a/internal/dhcpd/v4_unix_test.go +++ b/internal/dhcpd/v4_unix_test.go @@ -23,11 +23,11 @@ import ( ) var ( - DefaultRangeStart = netip.AddrFrom4([4]byte{192, 168, 10, 100}) - DefaultRangeEnd = netip.AddrFrom4([4]byte{192, 168, 10, 200}) - DefaultGatewayIP = netip.AddrFrom4([4]byte{192, 168, 10, 1}) - DefaultSelfIP = netip.AddrFrom4([4]byte{192, 168, 10, 2}) - DefaultSubnetMask = netip.AddrFrom4([4]byte{255, 255, 255, 0}) + DefaultRangeStart = netip.MustParseAddr("192.168.10.100") + DefaultRangeEnd = netip.MustParseAddr("192.168.10.200") + DefaultGatewayIP = netip.MustParseAddr("192.168.10.1") + DefaultSelfIP = netip.MustParseAddr("192.168.10.2") + DefaultSubnetMask = netip.MustParseAddr("255.255.255.0") ) // defaultV4ServerConf returns the default configuration for *v4Server to use in @@ -327,7 +327,7 @@ func TestV4_AddReplace(t *testing.T) { } func TestV4Server_handle_optionsPriority(t *testing.T) { - defaultIP := netip.AddrFrom4([4]byte{192, 168, 1, 1}) + defaultIP := netip.MustParseAddr("192.168.1.1") knownIP := net.IP{1, 2, 3, 4} // prepareSrv creates a *v4Server and sets the opt6IPs in the initial @@ -507,7 +507,7 @@ func TestV4StaticLease_Get(t *testing.T) { s, ok := sIface.(*v4Server) require.True(t, ok) - dnsAddr := netip.AddrFrom4([4]byte{192, 168, 10, 1}) + dnsAddr := netip.MustParseAddr("192.168.10.1") s.conf.dnsIPAddrs = []netip.Addr{dnsAddr} s.implicitOpts.Update(dhcpv4.OptDNS(dnsAddr.AsSlice())) @@ -599,7 +599,7 @@ func TestV4DynamicLease_Get(t *testing.T) { s, err := v4Create(conf) require.NoError(t, err) - s.conf.dnsIPAddrs = []netip.Addr{netip.AddrFrom4([4]byte{192, 168, 10, 1})} + s.conf.dnsIPAddrs = []netip.Addr{netip.MustParseAddr("192.168.10.1")} s.implicitOpts.Update(dhcpv4.OptDNS(s.conf.dnsIPAddrs[0].AsSlice())) var req, resp *dhcpv4.DHCPv4 diff --git a/internal/dnsforward/access.go b/internal/dnsforward/access.go index 23ec11374da..ddf3d93fa5a 100644 --- a/internal/dnsforward/access.go +++ b/internal/dnsforward/access.go @@ -19,6 +19,7 @@ import ( // accessCtx controls IP and client blocking that takes place before all other // processing. An accessCtx is safe for concurrent use. type accessCtx struct { + // TODO(e.burkov): Use map[netip.Addr]struct{} instead. allowedIPs *netutil.IPMap blockedIPs *netutil.IPMap diff --git a/internal/dnsforward/dnsforward.go b/internal/dnsforward/dnsforward.go index 3806ef5b9fe..2455fff5982 100644 --- a/internal/dnsforward/dnsforward.go +++ b/internal/dnsforward/dnsforward.go @@ -81,6 +81,7 @@ type Server struct { tableHostToIP hostToIPTable tableHostToIPLock sync.Mutex + // TODO(e.burkov): Use map[netip.Addr]struct{} instead. tableIPToHost *netutil.IPMap tableIPToHostLock sync.Mutex diff --git a/internal/home/clients.go b/internal/home/clients.go index 631d27ba987..ee535ea9128 100644 --- a/internal/home/clients.go +++ b/internal/home/clients.go @@ -119,6 +119,8 @@ type clientsContainer struct { idIndex map[string]*Client // ID -> client // ipToRC is the IP address to *RuntimeClient map. + // + // TODO(e.burkov): Use map[netip.Addr]struct{} instead. ipToRC *netutil.IPMap lock sync.Mutex diff --git a/internal/home/clients_test.go b/internal/home/clients_test.go index 8649be5c62d..00148c26d49 100644 --- a/internal/home/clients_test.go +++ b/internal/home/clients_test.go @@ -288,10 +288,10 @@ func TestClientsAddExisting(t *testing.T) { DBFilePath: "leases.db", Conf4: dhcpd.V4ServerConf{ Enabled: true, - GatewayIP: netip.AddrFrom4([4]byte{1, 2, 3, 1}), - SubnetMask: netip.AddrFrom4([4]byte{255, 255, 255, 0}), - RangeStart: netip.AddrFrom4([4]byte{1, 2, 3, 2}), - RangeEnd: netip.AddrFrom4([4]byte{1, 2, 3, 10}), + GatewayIP: netip.MustParseAddr("1.2.3.1"), + SubnetMask: netip.MustParseAddr("255.255.255.0"), + RangeStart: netip.MustParseAddr("1.2.3.2"), + RangeEnd: netip.MustParseAddr("1.2.3.10"), }, } diff --git a/internal/home/options_test.go b/internal/home/options_test.go index 1b669104314..32b4243a172 100644 --- a/internal/home/options_test.go +++ b/internal/home/options_test.go @@ -56,7 +56,7 @@ func TestParseWorkDir(t *testing.T) { } func TestParseBindHost(t *testing.T) { - wantAddr := netip.AddrFrom4([4]byte{1, 2, 3, 4}) + wantAddr := netip.MustParseAddr("1.2.3.4") assert.Zero(t, testParseOK(t).bindHost, "empty is not host") assert.Equal(t, wantAddr, testParseOK(t, "-h", "1.2.3.4").bindHost, "-h is host") @@ -151,7 +151,7 @@ func TestOptsToArgs(t *testing.T) { opts: options{workDir: "path"}, }, { name: "bind_host", - opts: options{bindHost: netip.AddrFrom4([4]byte{1, 2, 3, 4})}, + opts: options{bindHost: netip.MustParseAddr("1.2.3.4")}, args: []string{"-h", "1.2.3.4"}, }, { name: "bind_port",