Skip to content

Commit

Permalink
Use IfaCacheinfo and IFA_* consts from golang.org/x/sys/unix
Browse files Browse the repository at this point in the history
Use the IfaCacheinfo type and the IFA_* consts from
golang.org/x/sys/unix instead of locally duplicating them.
  • Loading branch information
tklauser committed Jan 21, 2020
1 parent 937bf44 commit 2638f07
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
21 changes: 9 additions & 12 deletions addr_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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()))
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
14 changes: 4 additions & 10 deletions nl/addr_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))[:]
}
6 changes: 3 additions & 3 deletions nl/addr_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ func (msg *IfaCacheInfo) write(b []byte) {
}

func (msg *IfaCacheInfo) serializeSafe() []byte {
length := SizeofIfaCacheInfo
length := unix.SizeofIfaCacheinfo
b := make([]byte, length)
msg.write(b)
return b
}

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)
Expand Down

0 comments on commit 2638f07

Please sign in to comment.