Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: arp unmarshal #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions protocol/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type ARP struct {
IPSrc net.IP
HWDst net.HardwareAddr
IPDst net.IP
// The actual test shows that ARP reply packets sent by the H3C switch include an additional 14 bytes.
// This is done by the switch to meet the minimum frame length requirement of 64 bytes.
Padding []byte
}

func NewARP(opt int) (*ARP, error) {
Expand All @@ -43,6 +46,8 @@ func NewARP(opt int) (*ARP, error) {
func (a *ARP) Len() (n uint16) {
n = 8
n += uint16(a.HWLength*2 + a.ProtoLength*2)
// Including the padding bytes, an inaccurate length can cause a panic in PacketIn2PropPacket.UnmarshalBinary.
n += uint16(len(a.Padding))
return
}

Expand Down Expand Up @@ -91,5 +96,10 @@ func (a *ARP) UnmarshalBinary(data []byte) error {
n += int(a.HWLength)
a.IPDst = make([]byte, a.ProtoLength)
copy(a.IPDst, data[n:n+int(a.ProtoLength)])
n += int(a.ProtoLength)
if len(data[n:]) > 0 {
a.Padding = make([]byte, len(data[n:]))
copy(a.Padding, data[n:])
}
return nil
}
18 changes: 18 additions & 0 deletions protocol/arp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package protocol

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ARPUnmarshalBinary(t *testing.T) {
data, err := hex.DecodeString("00010800060400027057bf301a03c0a8ac01525400ec4b98c0a8accc0000000000000000000000000000")
assert.Nil(t, err)
arp := new(ARP)
err = arp.UnmarshalBinary(data)
assert.Nil(t, err)
assert.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, arp.Padding)
assert.Equal(t, 42, arp.Len())
}