mirror of
https://github.com/jsimonetti/rtnetlink.git
synced 2026-03-28 07:21:07 +01:00
Change address message to ignore empty ip's (#74)
This will not marshal the following attributes when empty: - Local (was already ignored) - Broadcast - Anycast - Multicast Fixes: #73 Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
This commit is contained in:
parent
7a753270ee
commit
31febcbb33
15
address.go
15
address.go
@ -213,14 +213,19 @@ func (a *AddressAttributes) decode(ad *netlink.AttributeDecoder) error {
|
||||
func (a *AddressAttributes) encode(ae *netlink.AttributeEncoder) error {
|
||||
ae.Uint16(unix.IFA_UNSPEC, 0)
|
||||
ae.Bytes(unix.IFA_ADDRESS, a.Address)
|
||||
ae.Bytes(unix.IFA_BROADCAST, a.Broadcast)
|
||||
ae.Bytes(unix.IFA_ANYCAST, a.Anycast)
|
||||
ae.Bytes(unix.IFA_MULTICAST, a.Multicast)
|
||||
ae.Uint32(unix.IFA_FLAGS, a.Flags)
|
||||
|
||||
if a.Local != nil {
|
||||
ae.Bytes(unix.IFA_LOCAL, a.Local)
|
||||
}
|
||||
if a.Broadcast != nil {
|
||||
ae.Bytes(unix.IFA_BROADCAST, a.Broadcast)
|
||||
}
|
||||
if a.Anycast != nil {
|
||||
ae.Bytes(unix.IFA_ANYCAST, a.Anycast)
|
||||
}
|
||||
if a.Multicast != nil {
|
||||
ae.Bytes(unix.IFA_MULTICAST, a.Multicast)
|
||||
}
|
||||
ae.Uint32(unix.IFA_FLAGS, a.Flags)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -26,9 +26,8 @@ func TestAddressMessageMarshalBinary(t *testing.T) {
|
||||
b: []byte{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00,
|
||||
0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x07, 0x00,
|
||||
0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -43,9 +42,8 @@ func TestAddressMessageMarshalBinary(t *testing.T) {
|
||||
b: []byte{
|
||||
0x02, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
|
||||
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00,
|
||||
0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x07, 0x00,
|
||||
0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x04, 0x00, 0x01, 0x00, 0x08, 0x00, 0x08, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -63,10 +61,25 @@ func TestAddressMessageMarshalBinary(t *testing.T) {
|
||||
0x0a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x04, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x04, 0x00, 0x05, 0x00, 0x04, 0x00, 0x07, 0x00,
|
||||
0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no broadcast ",
|
||||
m: &AddressMessage{
|
||||
Attributes: AddressAttributes{
|
||||
Address: []byte{0, 0, 0, 0, 0, 0},
|
||||
Label: "lo",
|
||||
},
|
||||
},
|
||||
b: []byte{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x0a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@ -81,7 +94,7 @@ func TestAddressMessageMarshalBinary(t *testing.T) {
|
||||
}
|
||||
|
||||
if want, got := tt.b, b; !bytes.Equal(want, got) {
|
||||
t.Fatalf("unexpected Message bytes:\n- want: [%# x]\n- got: [%# x]", want, got)
|
||||
t.Fatalf("unexpected Message bytes:\n- want: [%s]\n- got: [%s]", bPrint(want), bPrint(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -221,3 +222,7 @@ func mustMarshal(m encoding.BinaryMarshaler) []byte {
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func bPrint(b []byte) string {
|
||||
return strings.ReplaceAll(fmt.Sprintf("%# x", b), " ", ", ")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user