diff --git a/internal/dhcpd/broadcast_bsd_test.go b/internal/dhcpd/broadcast_bsd_test.go index 1097e0d4f7e..49221b047a2 100644 --- a/internal/dhcpd/broadcast_bsd_test.go +++ b/internal/dhcpd/broadcast_bsd_test.go @@ -14,14 +14,6 @@ import ( "github.com/stretchr/testify/require" ) -func copyUDPAddr(a *net.UDPAddr) (copy *net.UDPAddr) { - return &net.UDPAddr{ - IP: netutil.CloneIP(a.IP), - Port: a.Port, - Zone: a.Zone, - } -} - func TestV4Server_Send_broadcast(t *testing.T) { b := &bytes.Buffer{} var peer *net.UDPAddr @@ -31,7 +23,7 @@ func TestV4Server_Send_broadcast(t *testing.T) { udpPeer, ok := addr.(*net.UDPAddr) require.True(t, ok) - peer = copyUDPAddr(udpPeer) + peer = cloneUDPAddr(udpPeer) n, err = b.Write(p) require.NoError(t, err) @@ -42,7 +34,7 @@ func TestV4Server_Send_broadcast(t *testing.T) { defaultPeer := &net.UDPAddr{ IP: net.IP{1, 2, 3, 4}, - // Neither 'Client Port' nor 'Server Port'. + // Use neither client nor server port. Port: 1234, } s := &v4Server{ @@ -80,7 +72,7 @@ func TestV4Server_Send_broadcast(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - s.send(copyUDPAddr(defaultPeer), conn, tc.req, tc.resp) + s.send(cloneUDPAddr(defaultPeer), conn, tc.req, tc.resp) assert.EqualValues(t, tc.resp.ToBytes(), b.Bytes()) assert.Equal(t, &net.UDPAddr{ IP: s.conf.broadcastIP, diff --git a/internal/dhcpd/broadcast_others_test.go b/internal/dhcpd/broadcast_others_test.go index 2a84134cbd2..246be382f35 100644 --- a/internal/dhcpd/broadcast_others_test.go +++ b/internal/dhcpd/broadcast_others_test.go @@ -14,14 +14,6 @@ import ( "github.com/stretchr/testify/require" ) -func copyUDPAddr(a *net.UDPAddr) (copy *net.UDPAddr) { - return &net.UDPAddr{ - IP: netutil.CloneIP(a.IP), - Port: a.Port, - Zone: a.Zone, - } -} - func TestV4Server_Send_broadcast(t *testing.T) { b := &bytes.Buffer{} var peers []*net.UDPAddr @@ -31,7 +23,7 @@ func TestV4Server_Send_broadcast(t *testing.T) { udpPeer, ok := addr.(*net.UDPAddr) require.True(t, ok) - peers = append(peers, copyUDPAddr(udpPeer)) + peers = append(peers, cloneUDPAddr(udpPeer)) n, err = b.Write(p) require.NoError(t, err) @@ -42,7 +34,7 @@ func TestV4Server_Send_broadcast(t *testing.T) { defaultPeer := &net.UDPAddr{ IP: net.IP{1, 2, 3, 4}, - // Neither 'Client Port' nor 'Server Port'. + // Use neither client nor server port. Port: 1234, } s := &v4Server{ @@ -80,7 +72,7 @@ func TestV4Server_Send_broadcast(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - s.send(copyUDPAddr(defaultPeer), conn, tc.req, tc.resp) + s.send(cloneUDPAddr(defaultPeer), conn, tc.req, tc.resp) // The same response is written twice. respData := tc.resp.ToBytes() diff --git a/internal/dhcpd/dhcpd_test.go b/internal/dhcpd/dhcpd_test.go index faae9e8d427..718783699dd 100644 --- a/internal/dhcpd/dhcpd_test.go +++ b/internal/dhcpd/dhcpd_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/AdguardTeam/AdGuardHome/internal/aghtest" + "github.com/AdguardTeam/golibs/netutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -136,3 +137,12 @@ func TestNormalizeLeases(t *testing.T) { assert.Equal(t, leases[1].HWAddr, staticLeases[1].HWAddr) assert.Equal(t, leases[2].HWAddr, dynLeases[1].HWAddr) } + +// cloneUDPAddr returns a deep copy of a. +func cloneUDPAddr(a *net.UDPAddr) (copy *net.UDPAddr) { + return &net.UDPAddr{ + IP: netutil.CloneIP(a.IP), + Port: a.Port, + Zone: a.Zone, + } +} diff --git a/internal/dhcpd/v4.go b/internal/dhcpd/v4.go index 7f1ba2a614a..3e9a954c079 100644 --- a/internal/dhcpd/v4.go +++ b/internal/dhcpd/v4.go @@ -938,8 +938,8 @@ func (s *v4Server) packetHandler(conn net.PacketConn, peer net.Addr, req *dhcpv4 func (s *v4Server) send(peer net.Addr, conn net.PacketConn, req, resp *dhcpv4.DHCPv4) { var unicast bool if giaddr, unspec := req.GatewayIPAddr, netutil.IPv4Zero(); !giaddr.Equal(unspec) { - // Send any return messages to the 'DHCP server' port on the - // BOOTP relay agent whose address appears in 'giaddr'. + // Send any return messages to the server port on the BOOTP + // relay agent whose address appears in giaddr. peer = &net.UDPAddr{ IP: giaddr, Port: dhcpv4.ServerPort, @@ -949,7 +949,7 @@ func (s *v4Server) send(peer net.Addr, conn net.PacketConn, req, resp *dhcpv4.DH // Broadcast any DHCPNAK messages to 0xffffffff. } else if ciaddr := req.ClientIPAddr; !ciaddr.Equal(unspec) { // Unicast DHCPOFFER and DHCPACK messages to the address in - // 'ciaddr'. + // ciaddr. peer = &net.UDPAddr{ IP: ciaddr, Port: dhcpv4.ClientPort, diff --git a/internal/dhcpd/v4_test.go b/internal/dhcpd/v4_test.go index c6ab3768132..bbc1dcea6a0 100644 --- a/internal/dhcpd/v4_test.go +++ b/internal/dhcpd/v4_test.go @@ -392,7 +392,7 @@ func TestV4Server_Send_unicast(t *testing.T) { udpPeer, ok := addr.(*net.UDPAddr) require.True(t, ok) - peer = copyUDPAddr(udpPeer) + peer = cloneUDPAddr(udpPeer) n, err = b.Write(p) require.NoError(t, err) @@ -403,7 +403,7 @@ func TestV4Server_Send_unicast(t *testing.T) { defaultPeer := &net.UDPAddr{ IP: net.IP{1, 2, 3, 4}, - // Use neither 'Client Port' nor 'Server Port'. + // Use neither client nor server port. Port: 1234, } defaultResp := &dhcpv4.DHCPv4{