Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IfaCacheinfo and IFA_* consts from golang.org/x/sys/unix #514

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.12

require (
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444
golang.org/x/sys v0.0.0-20200121082415-34d275377bf9
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444 h1:/d2cWp6PSamH4jDPFLyO150psQdqvtoNX8Zjg3AQ31g=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200121082415-34d275377bf9 h1:N19i1HjUnR7TF7rMt8O4p3dLvqvmYyzB6ifMFmrbY50=
golang.org/x/sys v0.0.0-20200121082415-34d275377bf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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)))[:]
}
10 changes: 5 additions & 5 deletions nl/addr_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ 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
}

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