k3s/vendor/github.com/vishvananda/netlink/nl/route_linux.go

82 lines
1.5 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
package nl
import (
"unsafe"
2019-09-27 21:51:53 +00:00
"golang.org/x/sys/unix"
2019-01-12 04:58:27 +00:00
)
type RtMsg struct {
2019-09-27 21:51:53 +00:00
unix.RtMsg
2019-01-12 04:58:27 +00:00
}
func NewRtMsg() *RtMsg {
return &RtMsg{
2019-09-27 21:51:53 +00:00
RtMsg: unix.RtMsg{
Table: unix.RT_TABLE_MAIN,
Scope: unix.RT_SCOPE_UNIVERSE,
Protocol: unix.RTPROT_BOOT,
Type: unix.RTN_UNICAST,
2019-01-12 04:58:27 +00:00
},
}
}
func NewRtDelMsg() *RtMsg {
return &RtMsg{
2019-09-27 21:51:53 +00:00
RtMsg: unix.RtMsg{
Table: unix.RT_TABLE_MAIN,
Scope: unix.RT_SCOPE_NOWHERE,
2019-01-12 04:58:27 +00:00
},
}
}
func (msg *RtMsg) Len() int {
2019-09-27 21:51:53 +00:00
return unix.SizeofRtMsg
2019-01-12 04:58:27 +00:00
}
func DeserializeRtMsg(b []byte) *RtMsg {
2019-09-27 21:51:53 +00:00
return (*RtMsg)(unsafe.Pointer(&b[0:unix.SizeofRtMsg][0]))
2019-01-12 04:58:27 +00:00
}
func (msg *RtMsg) Serialize() []byte {
2019-09-27 21:51:53 +00:00
return (*(*[unix.SizeofRtMsg]byte)(unsafe.Pointer(msg)))[:]
2019-01-12 04:58:27 +00:00
}
type RtNexthop struct {
2019-09-27 21:51:53 +00:00
unix.RtNexthop
2019-01-12 04:58:27 +00:00
Children []NetlinkRequestData
}
func DeserializeRtNexthop(b []byte) *RtNexthop {
2019-09-27 21:51:53 +00:00
return (*RtNexthop)(unsafe.Pointer(&b[0:unix.SizeofRtNexthop][0]))
2019-01-12 04:58:27 +00:00
}
func (msg *RtNexthop) Len() int {
if len(msg.Children) == 0 {
2019-09-27 21:51:53 +00:00
return unix.SizeofRtNexthop
2019-01-12 04:58:27 +00:00
}
l := 0
for _, child := range msg.Children {
l += rtaAlignOf(child.Len())
}
2019-09-27 21:51:53 +00:00
l += unix.SizeofRtNexthop
2019-01-12 04:58:27 +00:00
return rtaAlignOf(l)
}
func (msg *RtNexthop) Serialize() []byte {
length := msg.Len()
msg.RtNexthop.Len = uint16(length)
buf := make([]byte, length)
2019-09-27 21:51:53 +00:00
copy(buf, (*(*[unix.SizeofRtNexthop]byte)(unsafe.Pointer(msg)))[:])
next := rtaAlignOf(unix.SizeofRtNexthop)
2019-01-12 04:58:27 +00:00
if len(msg.Children) > 0 {
for _, child := range msg.Children {
childBuf := child.Serialize()
copy(buf[next:], childBuf)
next += rtaAlignOf(len(childBuf))
}
}
return buf
}