Skip to content

Commit 4031cf8

Browse files
authored
Allow empty neighbor LLAddress (#200)
The kernel sometimes returns empty LLAddress in NeighAttributes. Allow this to be returned as a nil address. Fixes: #199 Signed-off-by: SuperQ <[email protected]>
1 parent eac8438 commit 4031cf8

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

neigh.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ func (a *NeighAttributes) decode(ad *netlink.AttributeDecoder) error {
194194
// Allow IEEE 802 MAC-48, EUI-48, EUI-64, or 20-octet
195195
// IP over InfiniBand link-layer addresses
196196
l := len(ad.Bytes())
197+
if l == 0 {
198+
// Ignore empty addresses.
199+
continue
200+
}
197201
if l != 6 && l != 8 && l != 20 {
198202
return errInvalidNeighMessageAttr
199203
}

neigh_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package rtnetlink
33
import (
44
"bytes"
55
"net"
6-
"reflect"
76
"testing"
87

8+
"github.com/google/go-cmp/cmp"
99
"github.com/jsimonetti/rtnetlink/internal/unix"
1010
)
1111

@@ -130,6 +130,7 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
130130
name string
131131
b []byte
132132
m Message
133+
a NeighAttributes
133134
err error
134135
}{
135136
{
@@ -167,6 +168,27 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
167168
Type: unix.NTF_PROXY,
168169
},
169170
},
171+
{
172+
name: "Empty LLAddr",
173+
b: []byte{
174+
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
175+
0x40, 0x00, 0x00, 0x08, 0x06, 0x00, 0x00, 0x00,
176+
0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00,
177+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
178+
0x00, 0x00, 0xff, 0xff, 0x0a, 0x00, 0x00, 0x01,
179+
0x04, 0x00, 0x02, 0x00, 0x08, 0x00, 0x08, 0x00,
180+
0x00, 0x00, 0x00, 0x00,
181+
},
182+
m: &NeighMessage{
183+
Index: 2,
184+
State: 64,
185+
Type: unix.NTF_PROXY,
186+
Attributes: &NeighAttributes{
187+
Address: net.ParseIP("10.0.0.1"),
188+
LLAddress: nil,
189+
},
190+
},
191+
},
170192
}
171193

172194
for _, tt := range tests {
@@ -181,8 +203,8 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
181203
return
182204
}
183205

184-
if want, got := tt.m, m; !reflect.DeepEqual(want, got) {
185-
t.Fatalf("unexpected Message:\n- want: %#v\n- got: %#v", want, got)
206+
if diff := cmp.Diff(tt.m, m); diff != "" {
207+
t.Fatalf("unexpected Message: %s\n(-want +got):\n%s", tt.name, diff)
186208
}
187209
})
188210
}

0 commit comments

Comments
 (0)