2014-09-19 02:04:48 +00:00
|
|
|
package nl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RtMsg struct {
|
|
|
|
syscall.RtMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRtMsg() *RtMsg {
|
|
|
|
return &RtMsg{
|
|
|
|
RtMsg: syscall.RtMsg{
|
|
|
|
Table: syscall.RT_TABLE_MAIN,
|
|
|
|
Scope: syscall.RT_SCOPE_UNIVERSE,
|
|
|
|
Protocol: syscall.RTPROT_BOOT,
|
|
|
|
Type: syscall.RTN_UNICAST,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:29:45 +00:00
|
|
|
func NewRtDelMsg() *RtMsg {
|
|
|
|
return &RtMsg{
|
|
|
|
RtMsg: syscall.RtMsg{
|
2015-07-28 06:58:44 +00:00
|
|
|
Table: syscall.RT_TABLE_MAIN,
|
|
|
|
Scope: syscall.RT_SCOPE_NOWHERE,
|
2015-07-28 02:29:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
func (msg *RtMsg) Len() int {
|
|
|
|
return syscall.SizeofRtMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeserializeRtMsg(b []byte) *RtMsg {
|
|
|
|
return (*RtMsg)(unsafe.Pointer(&b[0:syscall.SizeofRtMsg][0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *RtMsg) Serialize() []byte {
|
|
|
|
return (*(*[syscall.SizeofRtMsg]byte)(unsafe.Pointer(msg)))[:]
|
|
|
|
}
|
2016-06-15 05:02:24 +00:00
|
|
|
|
|
|
|
type RtNexthop struct {
|
|
|
|
syscall.RtNexthop
|
2017-02-19 03:22:09 +00:00
|
|
|
Children []NetlinkRequestData
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeserializeRtNexthop(b []byte) *RtNexthop {
|
|
|
|
return (*RtNexthop)(unsafe.Pointer(&b[0:syscall.SizeofRtNexthop][0]))
|
|
|
|
}
|
|
|
|
|
2017-02-19 03:22:09 +00:00
|
|
|
func (msg *RtNexthop) Len() int {
|
|
|
|
if len(msg.Children) == 0 {
|
|
|
|
return syscall.SizeofRtNexthop
|
|
|
|
}
|
|
|
|
|
|
|
|
l := 0
|
|
|
|
for _, child := range msg.Children {
|
|
|
|
l += rtaAlignOf(child.Len())
|
|
|
|
}
|
|
|
|
l += syscall.SizeofRtNexthop
|
|
|
|
return rtaAlignOf(l)
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:02:24 +00:00
|
|
|
func (msg *RtNexthop) Serialize() []byte {
|
2017-02-19 03:22:09 +00:00
|
|
|
length := msg.Len()
|
|
|
|
msg.RtNexthop.Len = uint16(length)
|
|
|
|
buf := make([]byte, length)
|
|
|
|
copy(buf, (*(*[syscall.SizeofRtNexthop]byte)(unsafe.Pointer(msg)))[:])
|
|
|
|
next := rtaAlignOf(syscall.SizeofRtNexthop)
|
|
|
|
if len(msg.Children) > 0 {
|
|
|
|
for _, child := range msg.Children {
|
|
|
|
childBuf := child.Serialize()
|
|
|
|
copy(buf[next:], childBuf)
|
|
|
|
next += rtaAlignOf(len(childBuf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
2016-06-15 05:02:24 +00:00
|
|
|
}
|