Skip to content

Commit 6d3efd0

Browse files
author
jiasheng.yu
committed
fix: arp unmarshal
1 parent 009740f commit 6d3efd0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

protocol/arp.go

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type ARP struct {
2121
IPSrc net.IP
2222
HWDst net.HardwareAddr
2323
IPDst net.IP
24+
// The actual test shows that ARP reply packets sent by the H3C switch include an additional 14 bytes.
25+
// This is done by the switch to meet the minimum frame length requirement of 64 bytes.
26+
Padding []byte
2427
}
2528

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

@@ -91,5 +96,10 @@ func (a *ARP) UnmarshalBinary(data []byte) error {
9196
n += int(a.HWLength)
9297
a.IPDst = make([]byte, a.ProtoLength)
9398
copy(a.IPDst, data[n:n+int(a.ProtoLength)])
99+
n += int(a.ProtoLength)
100+
if len(data[n:]) > 0 {
101+
a.Padding = make([]byte, len(data[n:]))
102+
copy(a.Padding, data[n:])
103+
}
94104
return nil
95105
}

protocol/arp_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package protocol
2+
3+
import (
4+
"encoding/hex"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_ARPUnmarshalBinary(t *testing.T) {
11+
data, err := hex.DecodeString("00010800060400027057bf301a03c0a8ac01525400ec4b98c0a8accc0000000000000000000000000000")
12+
assert.Nil(t, err)
13+
arp := new(ARP)
14+
err = arp.UnmarshalBinary(data)
15+
assert.Nil(t, err)
16+
assert.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, arp.Padding)
17+
assert.Equal(t, 42, arp.Len())
18+
}

0 commit comments

Comments
 (0)