Skip to content

Commit

Permalink
dhcpd: imp docs & naming
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Sep 7, 2021
1 parent d87c646 commit ec7c3b7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
14 changes: 3 additions & 11 deletions internal/dhcpd/broadcast_bsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 3 additions & 11 deletions internal/dhcpd/broadcast_others_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions internal/dhcpd/dhcpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
}
}
6 changes: 3 additions & 3 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/dhcpd/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down

0 comments on commit ec7c3b7

Please sign in to comment.