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 Jul 30, 2021
1 parent 9a9574a commit 26724df
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
7 changes: 3 additions & 4 deletions internal/aghnet/net_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
if err != nil {
return false, err
}

defer func() { err = errors.WithDeferred(err, f.Close()) }()

var r io.Reader
Expand All @@ -40,12 +39,12 @@ func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
return false, err
}

return rcconfStaticConfig(r, ifaceName)
return rcConfStaticConfig(r, ifaceName)
}

// rcconfStaticConfig checks if the interface is configured by /etc/rc.conf to
// rcConfStaticConfig checks if the interface is configured by /etc/rc.conf to
// have a static IP.
func rcconfStaticConfig(r io.Reader, ifaceName string) (has bool, err error) {
func rcConfStaticConfig(r io.Reader, ifaceName string) (has bool, err error) {
s := bufio.NewScanner(r)
for ifaceLinePref := fmt.Sprintf("ifconfig_%s", ifaceName); s.Scan(); {
line := strings.TrimSpace(s.Text())
Expand Down
4 changes: 2 additions & 2 deletions internal/aghnet/net_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestRcconfStaticConfig(t *testing.T) {
func TestRcConfStaticConfig(t *testing.T) {
const ifaceName = `em0`
const nl = "\n"

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestRcconfStaticConfig(t *testing.T) {
for _, tc := range testCases {
r := strings.NewReader(tc.rcconfData)
t.Run(tc.name, func(t *testing.T) {
has, err := rcconfStaticConfig(r, ifaceName)
has, err := rcConfStaticConfig(r, ifaceName)
require.NoError(t, err)

assert.Equal(t, tc.wantHas, has)
Expand Down
4 changes: 2 additions & 2 deletions internal/dhcpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type V4ServerConf struct {

GatewayIP net.IP `yaml:"gateway_ip" json:"gateway_ip"`
SubnetMask net.IP `yaml:"subnet_mask" json:"subnet_mask"`
// broadcastIP is the broadcasting address specific for the configured
// interface.
// broadcastIP is the broadcasting address pre-calculated from the
// configured gateway IP and subnet mask.
broadcastIP net.IP

// The first & the last IP address for dynamic leases
Expand Down
14 changes: 11 additions & 3 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,15 +927,23 @@ func (s *v4Server) packetHandler(conn net.PacketConn, peer net.Addr, req *dhcpv4
resp.Options.Update(dhcpv4.OptMessageType(dhcpv4.MessageTypeNak))
}

upeer, ok := peer.(*net.UDPAddr)
// peer is expected to be of type *net.UDPConn as the server4.NewServer
// initializes it.
udpPeer, ok := peer.(*net.UDPAddr)
if !ok {
log.Error("dhcpv4: peer is of unexpected type %T", peer)

return
}

if upeer.IP.Equal(net.IPv4bcast) {
upeer.IP = s.conf.broadcastIP
// Despite the fact that server4.NewIPv4UDPConn explicitly sets socket
// options to allow broadcasting, it's also binds the connection to the
// specified interface. On FreeBSD conn.WriteTo causes errors while
// writing to the addresses that belong to another interface. So use
// the broadcast address specific for the binded interface in case the
// server4.Serve method sets it to net.IPv4Bcast.
if udpPeer.IP.Equal(net.IPv4bcast) {
udpPeer.IP = s.conf.broadcastIP
}

log.Debug("dhcpv4: sending: %s", resp.Summary())
Expand Down

0 comments on commit 26724df

Please sign in to comment.