From 2b6b08dd9558b52a4f41ec99e2f69a5af903ea23 Mon Sep 17 00:00:00 2001 From: vista Date: Sat, 23 Mar 2024 13:05:53 +0100 Subject: [PATCH] Move UDP-related functions and variables into separate file, add/fix some comments Signed-off-by: vista --- dhcpv4/nclient4/conn_unix.go | 8 ++++---- dhcpv4/nclient4/ethernet.go | 11 +++++++++-- dhcpv4/nclient4/{conn.go => udp.go} | 10 +++------- 3 files changed, 16 insertions(+), 13 deletions(-) rename dhcpv4/nclient4/{conn.go => udp.go} (86%) diff --git a/dhcpv4/nclient4/conn_unix.go b/dhcpv4/nclient4/conn_unix.go index 7f422ad6..1f648324 100644 --- a/dhcpv4/nclient4/conn_unix.go +++ b/dhcpv4/nclient4/conn_unix.go @@ -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 { @@ -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 { diff --git a/dhcpv4/nclient4/ethernet.go b/dhcpv4/nclient4/ethernet.go index a03f8a19..aa73a4f9 100644 --- a/dhcpv4/nclient4/ethernet.go +++ b/dhcpv4/nclient4/ethernet.go @@ -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 diff --git a/dhcpv4/nclient4/conn.go b/dhcpv4/nclient4/udp.go similarity index 86% rename from dhcpv4/nclient4/conn.go rename to dhcpv4/nclient4/udp.go index eb1a6a00..5dcddde4 100644 --- a/dhcpv4/nclient4/conn.go +++ b/dhcpv4/nclient4/udp.go @@ -1,3 +1,6 @@ +//go:build go1.12 +// +build go1.12 + package nclient4 import ( @@ -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")