Skip to content

Commit

Permalink
Further comment fixes, better variable/function naming
Browse files Browse the repository at this point in the history
  • Loading branch information
vista- authored and ggidofalvy-tc committed May 23, 2024
1 parent eecb06e commit 4fa4462
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions dhcpv4/nclient4/conn_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions dhcpv4/nclient4/ethernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const (
etherIPv4Proto uint16 = 0x0800
ethHdrMinimum int = 14
ethHdrBaseLen int = 14

vlanTagLen int = 4
vlanMax uint16 = 0x0FFF
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions dhcpv4/nclient4/ethernet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4fa4462

Please sign in to comment.