diff --git a/dhcpv4/nclient4/conn_unix.go b/dhcpv4/nclient4/conn_unix.go index 1f648324..c35bfe49 100644 --- a/dhcpv4/nclient4/conn_unix.go +++ b/dhcpv4/nclient4/conn_unix.go @@ -21,6 +21,8 @@ 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 { @@ -34,6 +36,7 @@ func NewRawUDPConn(iface string, port int, vlans ...uint16) (net.PacketConn, err etherType = etherIPv4Proto } + // Create a bidirectional raw socket on ifc with etherType as the filter rawConn, err := raw.ListenPacket(ifc, etherType, rawConnectionConfig) if err != nil { return nil, err @@ -66,12 +69,12 @@ func NewBroadcastUDPConn(rawPacketConn net.PacketConn, boundAddr *net.UDPAddr, v // ReadFrom implements net.PacketConn.ReadFrom. // -// ReadFrom reads raw Ethernet packets, parses the VLAN stack (if configured) -// and will try to match the IP+UDP destinations against upc.boundAddr. +// ReadFrom reads raw Ethernet frames, parses and matches the VLAN stack (if configured), +// and will try to match the remaining IP packet against upc.boundAddr. // // Any matching packets are returned via the given buffer. func (upc *BroadcastRawUDPConn) ReadFrom(b []byte) (int, net.Addr, error) { - ethHdrLen := ethHdrMinimum + ethHdrLen := ethHdrBaseLen if len(upc.VLANs) > 0 { ethHdrLen += len(upc.VLANs) * vlanTagLen } diff --git a/dhcpv4/nclient4/ethernet.go b/dhcpv4/nclient4/ethernet.go index aa73a4f9..b21cbfd1 100644 --- a/dhcpv4/nclient4/ethernet.go +++ b/dhcpv4/nclient4/ethernet.go @@ -12,7 +12,7 @@ import ( const ( etherIPv4Proto uint16 = 0x0800 - ethHdrMinimum int = 14 + ethHdrBaseLen int = 14 vlanTagLen int = 4 vlanMax uint16 = 0x0FFF @@ -118,7 +118,7 @@ func createVLANTag(vlan uint16) []byte { // addEthernetHdr returns the supplied packet (in bytes) with an // added Ethernet header with the specified EtherType. func addEthernetHdr(b []byte, dstMac, srcMac net.HardwareAddr, etherProto uint16, vlans []uint16) []byte { - ethHdrLen := ethHdrMinimum + ethHdrLen := ethHdrBaseLen if len(vlans) > 0 { ethHdrLen += len(vlans) * vlanTagLen } diff --git a/dhcpv4/nclient4/ethernet_test.go b/dhcpv4/nclient4/ethernet_test.go index 6ed586e1..322f30db 100644 --- a/dhcpv4/nclient4/ethernet_test.go +++ b/dhcpv4/nclient4/ethernet_test.go @@ -82,7 +82,7 @@ func TestProcessVLANStack(t *testing.T) { } func TestCreateVLANTag(t *testing.T) { - // Gopacket builds VLAN tags the other way around: first VLAN ID/TCI, then TPID, due to their different layered approach + // gopacket builds VLAN tags the other way around: first VLAN ID/TCI, then TPID, due to their different layered approach // Since a VLAN tag is only 4 bytes, and the value is well-known, it makes sense to just construct the packet by hand. want := []byte{0x81, 0x00, 0x01, 0x23} @@ -135,7 +135,7 @@ func TestGetEthernetPayload(t *testing.T) { } } -func TestAddEthernetHdrTwo(t *testing.T) { +func TestAddEthernetHdr(t *testing.T) { for _, tt := range []struct { name string testLayers []gopacket.SerializableLayer