Skip to content

Commit

Permalink
Move UDP-related functions and variables into separate file, add/fix …
Browse files Browse the repository at this point in the history
…some comments

Signed-off-by: vista <[email protected]>
  • Loading branch information
vista- committed May 23, 2024
1 parent 20b1aea commit 2b6b08d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions dhcpv4/nclient4/conn_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ var rawConnectionConfig = &raw.Config{

// NewRawUDPConn returns a UDP connection bound to the interface and port
// given based on a raw packet socket. All packets are broadcasted.
//
// The interface can be completely unconfigured.
func NewRawUDPConn(iface string, port int, vlans ...uint16) (net.PacketConn, error) {
ifc, err := net.InterfaceByName(iface)
if err != nil {
Expand Down Expand Up @@ -68,8 +66,10 @@ func NewBroadcastUDPConn(rawPacketConn net.PacketConn, boundAddr *net.UDPAddr, v

// ReadFrom implements net.PacketConn.ReadFrom.
//
// ReadFrom reads raw IP packets and will try to match them against
// upc.boundAddr. Any matching packets are returned via the given buffer.
// ReadFrom reads raw Ethernet packets, parses the VLAN stack (if configured)
// and will try to match the IP+UDP destinations against upc.boundAddr.
//
// Any matching packets are returned via the given buffer.
func (upc *BroadcastRawUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
ethHdrLen := ethHdrMinimum
if len(upc.VLANs) > 0 {
Expand Down
11 changes: 9 additions & 2 deletions dhcpv4/nclient4/ethernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ const (
vlanTPID uint16 = 0x8100
)

var (
// BroadcastMac is the broadcast MAC address.
//
// Any UDP packet sent to this address is broadcast on the subnet.
BroadcastMac = net.HardwareAddr([]byte{255, 255, 255, 255, 255, 255})
)

// processVLANStack receives a buffer starting at the first TPID/EtherType field, and walks through
// the VLAN stack until either an unexpected VLAN is found, or if an IPv4 EtherType is found.
// The data from the provided buffer is consumed until the end of the Ethernet header
// The data from the provided buffer is consumed until the end of the Ethernet header.
//
// processVLANStack returns true if the VLAN stack in the packet corresponds to the VLAN configuration, false otherwise
// processVLANStack returns true if the VLAN stack in the packet corresponds to the VLAN configuration, false otherwise.
func processVLANStack(buf *uio.Lexer, vlans []uint16) bool {
var currentVLAN uint16
var vlanStackIsCorrect bool = true
Expand Down
10 changes: 3 additions & 7 deletions dhcpv4/nclient4/conn.go → dhcpv4/nclient4/udp.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build go1.12
// +build go1.12

package nclient4

import (
Expand All @@ -7,13 +10,6 @@ import (
"github.com/u-root/uio/uio"
)

var (
// BroadcastMac is the broadcast MAC address.
//
// Any UDP packet sent to this address is broadcast on the subnet.
BroadcastMac = net.HardwareAddr([]byte{255, 255, 255, 255, 255, 255})
)

var (
// ErrUDPAddrIsRequired is an error used when a passed argument is not of type "*net.UDPAddr".
ErrUDPAddrIsRequired = errors.New("must supply UDPAddr")
Expand Down

0 comments on commit 2b6b08d

Please sign in to comment.