Skip to content

Commit

Permalink
all: imp code, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 13, 2022
1 parent 59549c4 commit 25c1afc
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 56 deletions.
6 changes: 2 additions & 4 deletions internal/aghnet/dhcp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/aghnet/hostscontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions internal/aghnet/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
2 changes: 1 addition & 1 deletion internal/aghnet/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
26 changes: 8 additions & 18 deletions internal/aghnet/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down
32 changes: 16 additions & 16 deletions internal/dhcpd/dhcpd_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions internal/dhcpd/v4_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/dnsforward/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/home/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}

Expand Down
4 changes: 2 additions & 2 deletions internal/home/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 25c1afc

Please sign in to comment.