diff --git a/addr_linux.go b/addr_linux.go index 28746d5..1358bee 100644 --- a/addr_linux.go +++ b/addr_linux.go @@ -11,9 +11,6 @@ import ( "golang.org/x/sys/unix" ) -// IFA_FLAGS is a u32 attribute. -const IFA_FLAGS = 0x8 - // AddrAdd will add an IP address to a link device. // // Equivalent to: `ip addr add $addr dev $link` @@ -125,7 +122,7 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error } else { b := make([]byte, 4) native.PutUint32(b, uint32(addr.Flags)) - flagsData := nl.NewRtAttr(IFA_FLAGS, b) + flagsData := nl.NewRtAttr(unix.IFA_FLAGS, b) req.AddData(flagsData) } } @@ -156,10 +153,10 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error // value should be "forever". To compensate for that, only add the attributes if at least one of the values is // non-zero, which means the caller has explicitly set them if addr.ValidLft > 0 || addr.PreferedLft > 0 { - cachedata := nl.IfaCacheInfo{ - IfaValid: uint32(addr.ValidLft), - IfaPrefered: uint32(addr.PreferedLft), - } + cachedata := nl.IfaCacheInfo{unix.IfaCacheinfo{ + Valid: uint32(addr.ValidLft), + Prefered: uint32(addr.PreferedLft), + }} req.AddData(nl.NewRtAttr(unix.IFA_CACHEINFO, cachedata.Serialize())) } @@ -254,12 +251,12 @@ func parseAddr(m []byte) (addr Addr, family, index int, err error) { addr.Broadcast = attr.Value case unix.IFA_LABEL: addr.Label = string(attr.Value[:len(attr.Value)-1]) - case IFA_FLAGS: + case unix.IFA_FLAGS: addr.Flags = int(native.Uint32(attr.Value[0:4])) - case nl.IFA_CACHEINFO: + case unix.IFA_CACHEINFO: ci := nl.DeserializeIfaCacheInfo(attr.Value) - addr.PreferedLft = int(ci.IfaPrefered) - addr.ValidLft = int(ci.IfaValid) + addr.PreferedLft = int(ci.Prefered) + addr.ValidLft = int(ci.Valid) } } diff --git a/nl/addr_linux.go b/nl/addr_linux.go index 50db3b4..6bea4ed 100644 --- a/nl/addr_linux.go +++ b/nl/addr_linux.go @@ -54,24 +54,18 @@ func (msg *IfAddrmsg) Len() int { // __u32 tstamp; /* updated timestamp, hundredths of seconds */ // }; -const IFA_CACHEINFO = 6 -const SizeofIfaCacheInfo = 0x10 - type IfaCacheInfo struct { - IfaPrefered uint32 - IfaValid uint32 - Cstamp uint32 - Tstamp uint32 + unix.IfaCacheinfo } func (msg *IfaCacheInfo) Len() int { - return SizeofIfaCacheInfo + return unix.SizeofIfaCacheinfo } func DeserializeIfaCacheInfo(b []byte) *IfaCacheInfo { - return (*IfaCacheInfo)(unsafe.Pointer(&b[0:SizeofIfaCacheInfo][0])) + return (*IfaCacheInfo)(unsafe.Pointer(&b[0:unix.SizeofIfaCacheinfo][0])) } func (msg *IfaCacheInfo) Serialize() []byte { - return (*(*[SizeofIfaCacheInfo]byte)(unsafe.Pointer(msg)))[:] + return (*(*[unix.SizeofIfaCacheinfo]byte)(unsafe.Pointer(msg)))[:] } diff --git a/nl/addr_linux_test.go b/nl/addr_linux_test.go index 6a5c03e..b2cbb44 100644 --- a/nl/addr_linux_test.go +++ b/nl/addr_linux_test.go @@ -41,14 +41,14 @@ func TestIfAddrmsgDeserializeSerialize(t *testing.T) { func (msg *IfaCacheInfo) write(b []byte) { native := NativeEndian() - native.PutUint32(b[0:4], uint32(msg.IfaPrefered)) - native.PutUint32(b[4:8], uint32(msg.IfaValid)) + native.PutUint32(b[0:4], uint32(msg.Prefered)) + native.PutUint32(b[4:8], uint32(msg.Valid)) native.PutUint32(b[8:12], uint32(msg.Cstamp)) native.PutUint32(b[12:16], uint32(msg.Tstamp)) } func (msg *IfaCacheInfo) serializeSafe() []byte { - length := SizeofIfaCacheInfo + length := unix.SizeofIfaCacheinfo b := make([]byte, length) msg.write(b) return b @@ -56,12 +56,12 @@ func (msg *IfaCacheInfo) serializeSafe() []byte { func deserializeIfaCacheInfoSafe(b []byte) *IfaCacheInfo { var msg = IfaCacheInfo{} - binary.Read(bytes.NewReader(b[0:SizeofIfaCacheInfo]), NativeEndian(), &msg) + binary.Read(bytes.NewReader(b[0:unix.SizeofIfaCacheinfo]), NativeEndian(), &msg) return &msg } func TestIfaCacheInfoDeserializeSerialize(t *testing.T) { - var orig = make([]byte, SizeofIfaCacheInfo) + var orig = make([]byte, unix.SizeofIfaCacheinfo) rand.Read(orig) safemsg := deserializeIfaCacheInfoSafe(orig) msg := DeserializeIfaCacheInfo(orig)