Skip to content

Commit

Permalink
ethernet{,_test}: remove suffix from {Destination,Source}HardwareAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Jul 31, 2015
1 parent 1df24b9 commit f5805a0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
22 changes: 12 additions & 10 deletions ethernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ const (
// such as source and destination hardware addresses, zero or more optional
// 802.1Q VLAN tags, an EtherType, and payload data.
type Frame struct {
// DestinationHardwareAddr specifies the destination hardware address for
// this Frame. If this address is set to Broadcast, the Frame will be sent
// to every device on a given LAN segment.
DestinationHardwareAddr net.HardwareAddr
// Destination specifies the destination hardware address for this Frame.
//
// If this address is set to Broadcast, the Frame will be sent to every
// device on a given LAN segment.
Destination net.HardwareAddr

// SourceHardwareAddr specifies the source hardware address for this Frame.
// Source specifies the source hardware address for this Frame.
//
// Typically, this is the hardware address of the network interface used to
// send this Frame.
SourceHardwareAddr net.HardwareAddr
Source net.HardwareAddr

// VLAN specifies one or more optional 802.1Q VLAN tags, which may or may
// not be present in a Frame. It is important to note that the operating
Expand Down Expand Up @@ -89,8 +91,8 @@ func (f *Frame) MarshalBinary() ([]byte, error) {

b := make([]byte, 6+6+(4*len(f.VLAN))+2+pl)

copy(b[0:6], f.DestinationHardwareAddr)
copy(b[6:12], f.SourceHardwareAddr)
copy(b[0:6], f.Destination)
copy(b[6:12], f.Source)

// Marshal each VLAN tag into bytes, inserting a VLAN EtherType value
// before each, so devices know that one or more VLANs are present.
Expand Down Expand Up @@ -129,11 +131,11 @@ func (f *Frame) UnmarshalBinary(b []byte) error {

dst := make(net.HardwareAddr, 6)
copy(dst, b[0:6])
f.DestinationHardwareAddr = dst
f.Destination = dst

src := make(net.HardwareAddr, 6)
copy(src, b[6:12])
f.SourceHardwareAddr = src
f.Source = src

// Track offset in packet for writing data
n := 14
Expand Down
46 changes: 23 additions & 23 deletions ethernet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func TestFrameMarshalBinary(t *testing.T) {
{
desc: "IPv4, no VLANs",
f: &Frame{
DestinationHardwareAddr: net.HardwareAddr{0, 1, 0, 1, 0, 1},
SourceHardwareAddr: net.HardwareAddr{1, 0, 1, 0, 1, 0},
EtherType: EtherTypeIPv4,
Payload: bytes.Repeat([]byte{0}, 50),
Destination: net.HardwareAddr{0, 1, 0, 1, 0, 1},
Source: net.HardwareAddr{1, 0, 1, 0, 1, 0},
EtherType: EtherTypeIPv4,
Payload: bytes.Repeat([]byte{0}, 50),
},
b: append([]byte{
0, 1, 0, 1, 0, 1,
Expand All @@ -50,8 +50,8 @@ func TestFrameMarshalBinary(t *testing.T) {
{
desc: "IPv6, 1 VLAN: PRI 1, ID 101",
f: &Frame{
DestinationHardwareAddr: net.HardwareAddr{1, 0, 1, 0, 1, 0},
SourceHardwareAddr: net.HardwareAddr{0, 1, 0, 1, 0, 1},
Destination: net.HardwareAddr{1, 0, 1, 0, 1, 0},
Source: net.HardwareAddr{0, 1, 0, 1, 0, 1},
VLAN: []*VLAN{{
Priority: 1,
ID: 101,
Expand All @@ -70,8 +70,8 @@ func TestFrameMarshalBinary(t *testing.T) {
{
desc: "ARP, 2 VLANs: (PRI 0, DROP, ID 100) (PRI 1, ID 101)",
f: &Frame{
DestinationHardwareAddr: Broadcast,
SourceHardwareAddr: net.HardwareAddr{0, 1, 0, 1, 0, 1},
Destination: Broadcast,
Source: net.HardwareAddr{0, 1, 0, 1, 0, 1},
VLAN: []*VLAN{
{
DropEligible: true,
Expand Down Expand Up @@ -161,9 +161,9 @@ func TestFrameUnmarshalBinary(t *testing.T) {
desc: "0 VLANs detected, but 1 may have been present",
b: bytes.Repeat([]byte{0}, 56),
f: &Frame{
DestinationHardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
SourceHardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
Payload: bytes.Repeat([]byte{0}, 42),
Destination: net.HardwareAddr{0, 0, 0, 0, 0, 0},
Source: net.HardwareAddr{0, 0, 0, 0, 0, 0},
Payload: bytes.Repeat([]byte{0}, 42),
},
},
{
Expand All @@ -174,10 +174,10 @@ func TestFrameUnmarshalBinary(t *testing.T) {
0x08, 0x00,
}, bytes.Repeat([]byte{0}, 50)...),
f: &Frame{
DestinationHardwareAddr: net.HardwareAddr{0, 1, 0, 1, 0, 1},
SourceHardwareAddr: net.HardwareAddr{1, 0, 1, 0, 1, 0},
EtherType: EtherTypeIPv4,
Payload: bytes.Repeat([]byte{0}, 50),
Destination: net.HardwareAddr{0, 1, 0, 1, 0, 1},
Source: net.HardwareAddr{1, 0, 1, 0, 1, 0},
EtherType: EtherTypeIPv4,
Payload: bytes.Repeat([]byte{0}, 50),
},
},
{
Expand All @@ -190,8 +190,8 @@ func TestFrameUnmarshalBinary(t *testing.T) {
0x86, 0xDD,
}, bytes.Repeat([]byte{0}, 50)...),
f: &Frame{
DestinationHardwareAddr: net.HardwareAddr{1, 0, 1, 0, 1, 0},
SourceHardwareAddr: net.HardwareAddr{0, 1, 0, 1, 0, 1},
Destination: net.HardwareAddr{1, 0, 1, 0, 1, 0},
Source: net.HardwareAddr{0, 1, 0, 1, 0, 1},
VLAN: []*VLAN{{
Priority: 1,
ID: 101,
Expand All @@ -212,8 +212,8 @@ func TestFrameUnmarshalBinary(t *testing.T) {
0x08, 0x06,
}, bytes.Repeat([]byte{0}, 50)...),
f: &Frame{
DestinationHardwareAddr: Broadcast,
SourceHardwareAddr: net.HardwareAddr{0, 1, 0, 1, 0, 1},
Destination: Broadcast,
Source: net.HardwareAddr{0, 1, 0, 1, 0, 1},
VLAN: []*VLAN{
{
DropEligible: true,
Expand Down Expand Up @@ -299,8 +299,8 @@ func BenchmarkFrameMarshalBinaryJumboPayload(b *testing.B) {
}

func benchmarkFrameMarshalBinary(b *testing.B, f *Frame) {
f.DestinationHardwareAddr = net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
f.SourceHardwareAddr = net.HardwareAddr{0xad, 0xbe, 0xef, 0xde, 0xad, 0xde}
f.Destination = net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
f.Source = net.HardwareAddr{0xad, 0xbe, 0xef, 0xde, 0xad, 0xde}

b.ResetTimer()
b.ReportAllocs()
Expand Down Expand Up @@ -362,8 +362,8 @@ func BenchmarkFrameUnmarshalBinaryJumboPayload(b *testing.B) {
}

func benchmarkFrameUnmarshalBinary(b *testing.B, f *Frame) {
f.DestinationHardwareAddr = net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
f.SourceHardwareAddr = net.HardwareAddr{0xad, 0xbe, 0xef, 0xde, 0xad, 0xde}
f.Destination = net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
f.Source = net.HardwareAddr{0xad, 0xbe, 0xef, 0xde, 0xad, 0xde}

fb, err := f.MarshalBinary()
if err != nil {
Expand Down

0 comments on commit f5805a0

Please sign in to comment.