mirror of
https://github.com/jsimonetti/rtnetlink.git
synced 2026-04-14 08:41:08 +02:00
neigh: replace constants for x/sys/unix (#34)
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
This commit is contained in:
parent
b391ea1b16
commit
9c928e1cf7
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.12
|
||||
|
||||
require (
|
||||
github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225
|
||||
golang.org/x/sys v0.0.0-20190410170021-cc4d4f50624c
|
||||
golang.org/x/sys v0.0.0-20190410235845-0ad05ae3009d
|
||||
)
|
||||
|
||||
10
go.sum
10
go.sum
@ -1,11 +1,17 @@
|
||||
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225 h1:CxV4h424xakl+HjTCCwbXmDK8Qu/w+rRl2+cw1HDZyM=
|
||||
github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313 h1:pczuHS43Cp2ktBEEmLwScxgjWsBSzdaQiKzUyf3DTTc=
|
||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190410170021-cc4d4f50624c h1:OUGWoQpM/o3TxM7Fp3CEqRpaYCbg4H1hOVPnZoUtr2U=
|
||||
golang.org/x/sys v0.0.0-20190410170021-cc4d4f50624c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190410235845-0ad05ae3009d h1:+9jagSGtlJZAaZGdRvJikXNpc5lh2/rq9eyMN/5kmwA=
|
||||
golang.org/x/sys v0.0.0-20190410235845-0ad05ae3009d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
||||
53
neigh.go
53
neigh.go
@ -41,21 +41,9 @@ type NeighMessage struct {
|
||||
Attributes *NeighAttributes
|
||||
}
|
||||
|
||||
const (
|
||||
NTF_USE = 0x01
|
||||
NTF_SELF = 0x02
|
||||
NTF_MASTER = 0x04
|
||||
NTF_PROXY = 0x08
|
||||
NTF_EXT_LEARNED = 0x10
|
||||
NTF_OFFLOADED = 0x20
|
||||
NTF_ROUTER = 0x80
|
||||
)
|
||||
|
||||
const neighMsgLen = 12
|
||||
|
||||
// MarshalBinary marshals a NeighMessage into a byte slice.
|
||||
func (m *NeighMessage) MarshalBinary() ([]byte, error) {
|
||||
b := make([]byte, neighMsgLen)
|
||||
b := make([]byte, unix.SizeofNdMsg)
|
||||
|
||||
nlenc.PutUint16(b[0:2], m.Family)
|
||||
// bytes 3 and 4 are padding
|
||||
@ -78,7 +66,7 @@ func (m *NeighMessage) MarshalBinary() ([]byte, error) {
|
||||
// UnmarshalBinary unmarshals the contents of a byte slice into a NeighMessage.
|
||||
func (m *NeighMessage) UnmarshalBinary(b []byte) error {
|
||||
l := len(b)
|
||||
if l < neighMsgLen {
|
||||
if l < unix.SizeofNdMsg {
|
||||
return errInvalidNeighMessage
|
||||
}
|
||||
|
||||
@ -88,9 +76,9 @@ func (m *NeighMessage) UnmarshalBinary(b []byte) error {
|
||||
m.Flags = b[10]
|
||||
m.Type = b[11]
|
||||
|
||||
if l > neighMsgLen {
|
||||
if l > unix.SizeofNdMsg {
|
||||
m.Attributes = &NeighAttributes{}
|
||||
err := m.Attributes.UnmarshalBinary(b[neighMsgLen:])
|
||||
err := m.Attributes.UnmarshalBinary(b[unix.SizeofNdMsg:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -180,21 +168,6 @@ type NeighAttributes struct {
|
||||
IfIndex uint32
|
||||
}
|
||||
|
||||
const (
|
||||
NDA_UNSPEC uint16 = iota
|
||||
NDA_DST
|
||||
NDA_LLADDR
|
||||
NDA_CACHEINFO
|
||||
NDA_PROBES
|
||||
NDA_VLAN
|
||||
NDA_PORT
|
||||
NDA_VNI
|
||||
NDA_IFINDEX
|
||||
NDA_MASTER
|
||||
NDA_LINK_NETNSID
|
||||
NDA_SRC_VNI
|
||||
)
|
||||
|
||||
// NeighAttributes unmarshals the contents of a byte slice into a NeighMessage.
|
||||
func (a *NeighAttributes) UnmarshalBinary(b []byte) error {
|
||||
attrs, err := netlink.UnmarshalAttributes(b)
|
||||
@ -204,25 +177,25 @@ func (a *NeighAttributes) UnmarshalBinary(b []byte) error {
|
||||
|
||||
for _, attr := range attrs {
|
||||
switch attr.Type {
|
||||
case NDA_UNSPEC:
|
||||
case unix.NDA_UNSPEC:
|
||||
//unused attribute
|
||||
case NDA_DST:
|
||||
case unix.NDA_DST:
|
||||
if len(attr.Data) != 4 && len(attr.Data) != 16 {
|
||||
return errInvalidNeighMessageAttr
|
||||
}
|
||||
a.Address = attr.Data
|
||||
case NDA_LLADDR:
|
||||
case unix.NDA_LLADDR:
|
||||
if len(attr.Data) != 6 {
|
||||
return errInvalidNeighMessageAttr
|
||||
}
|
||||
a.LLAddress = attr.Data
|
||||
case NDA_CACHEINFO:
|
||||
case unix.NDA_CACHEINFO:
|
||||
a.CacheInfo = &NeighCacheInfo{}
|
||||
err := a.CacheInfo.UnmarshalBinary(attr.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case NDA_IFINDEX:
|
||||
case unix.NDA_IFINDEX:
|
||||
if len(attr.Data) != 4 {
|
||||
return errInvalidNeighMessageAttr
|
||||
}
|
||||
@ -237,19 +210,19 @@ func (a *NeighAttributes) UnmarshalBinary(b []byte) error {
|
||||
func (a *NeighAttributes) MarshalBinary() ([]byte, error) {
|
||||
attrs := []netlink.Attribute{
|
||||
{
|
||||
Type: NDA_UNSPEC,
|
||||
Type: unix.NDA_UNSPEC,
|
||||
Data: nlenc.Uint16Bytes(0),
|
||||
},
|
||||
{
|
||||
Type: NDA_DST,
|
||||
Type: unix.NDA_DST,
|
||||
Data: a.Address,
|
||||
},
|
||||
{
|
||||
Type: NDA_LLADDR,
|
||||
Type: unix.NDA_LLADDR,
|
||||
Data: a.LLAddress,
|
||||
},
|
||||
{
|
||||
Type: NDA_IFINDEX,
|
||||
Type: unix.NDA_IFINDEX,
|
||||
Data: nlenc.Uint32Bytes(a.IfIndex),
|
||||
},
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@ import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Tests will only pass on little endian machines
|
||||
@ -29,7 +31,7 @@ func TestNeighMessageMarshalBinary(t *testing.T) {
|
||||
m: &NeighMessage{
|
||||
Index: 2,
|
||||
State: 64,
|
||||
Type: NTF_PROXY,
|
||||
Type: unix.NTF_PROXY,
|
||||
},
|
||||
b: []byte{
|
||||
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
|
||||
@ -41,7 +43,7 @@ func TestNeighMessageMarshalBinary(t *testing.T) {
|
||||
m: &NeighMessage{
|
||||
Index: 2,
|
||||
State: 64,
|
||||
Type: NTF_PROXY,
|
||||
Type: unix.NTF_PROXY,
|
||||
Attributes: &NeighAttributes{
|
||||
Address: net.ParseIP("10.0.0.0"),
|
||||
LLAddress: []byte{0x33, 0x33, 0x00, 0x00, 0x00, 0x16},
|
||||
@ -119,7 +121,7 @@ func TestNeighMessageUnmarshalBinary(t *testing.T) {
|
||||
m: &NeighMessage{
|
||||
Index: 2,
|
||||
State: 64,
|
||||
Type: NTF_PROXY,
|
||||
Type: unix.NTF_PROXY,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user