func (rr *NetlinkRouteRequest) toWireFormat() []byte {
b := make([]byte, rr.Header.Len)
- b[0] = byte(rr.Header.Len)
- b[1] = byte(rr.Header.Len >> 8)
- b[2] = byte(rr.Header.Len >> 16)
- b[3] = byte(rr.Header.Len >> 24)
- b[4] = byte(rr.Header.Type)
- b[5] = byte(rr.Header.Type >> 8)
- b[6] = byte(rr.Header.Flags)
- b[7] = byte(rr.Header.Flags >> 8)
- b[8] = byte(rr.Header.Seq)
- b[9] = byte(rr.Header.Seq >> 8)
- b[10] = byte(rr.Header.Seq >> 16)
- b[11] = byte(rr.Header.Seq >> 24)
- b[12] = byte(rr.Header.Pid)
- b[13] = byte(rr.Header.Pid >> 8)
- b[14] = byte(rr.Header.Pid >> 16)
- b[15] = byte(rr.Header.Pid >> 24)
+ *(*uint32)(unsafe.Pointer(&b[0:4][0])) = rr.Header.Len
+ *(*uint16)(unsafe.Pointer(&b[4:6][0])) = rr.Header.Type
+ *(*uint16)(unsafe.Pointer(&b[6:8][0])) = rr.Header.Flags
+ *(*uint32)(unsafe.Pointer(&b[8:12][0])) = rr.Header.Seq
+ *(*uint32)(unsafe.Pointer(&b[12:16][0])) = rr.Header.Pid
b[16] = byte(rr.Data.Family)
return b
}
// NetlinkRIB returns routing information base, as known as RIB,
// which consists of network facility information, states and
// parameters.
-func NetlinkRIB(proto, family int) ([]byte, int) {
+func NetlinkRIB(proto, family int) ([]byte, error) {
var (
- s int
- e int
lsanl SockaddrNetlink
- seq int
tab []byte
)
- s, e = Socket(AF_NETLINK, SOCK_RAW, 0)
- if e != 0 {
+ s, e := Socket(AF_NETLINK, SOCK_RAW, 0)
+ if e != nil {
return nil, e
}
defer Close(s)
lsanl.Family = AF_NETLINK
e = Bind(s, &lsanl)
- if e != 0 {
+ if e != nil {
return nil, e
}
- seq++
+ seq := 1
wb := newNetlinkRouteRequest(proto, seq, family)
e = Sendto(s, wb, 0, &lsanl)
- if e != 0 {
+ if e != nil {
return nil, e
}
rb = make([]byte, Getpagesize())
nr, _, e = Recvfrom(s, rb, 0)
- if e != 0 {
+ if e != nil {
return nil, e
}
if nr < NLMSG_HDRLEN {
msgs, _ := ParseNetlinkMessage(rb)
for _, m := range msgs {
- if lsa, e = Getsockname(s); e != 0 {
+ if lsa, e = Getsockname(s); e != nil {
return nil, e
}
switch v := lsa.(type) {
}
done:
- return tab, 0
+ return tab, nil
}
// NetlinkMessage represents the netlink message.
// ParseNetlinkMessage parses buf as netlink messages and returns
// the slice containing the NetlinkMessage structs.
-func ParseNetlinkMessage(buf []byte) ([]NetlinkMessage, int) {
+func ParseNetlinkMessage(buf []byte) ([]NetlinkMessage, error) {
var (
h *NlMsghdr
dbuf []byte
dlen int
- e int
+ e error
msgs []NetlinkMessage
)
for len(buf) >= NLMSG_HDRLEN {
h, dbuf, dlen, e = netlinkMessageHeaderAndData(buf)
- if e != 0 {
+ if e != nil {
break
}
m := NetlinkMessage{}
return msgs, e
}
-func netlinkMessageHeaderAndData(buf []byte) (*NlMsghdr, []byte, int, int) {
+func netlinkMessageHeaderAndData(buf []byte) (*NlMsghdr, []byte, int, error) {
h := (*NlMsghdr)(unsafe.Pointer(&buf[0]))
if int(h.Len) < NLMSG_HDRLEN || int(h.Len) > len(buf) {
return nil, nil, 0, EINVAL
}
- return h, buf[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), 0
+ return h, buf[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
}
// NetlinkRouteAttr represents the netlink route attribute.
// ParseNetlinkRouteAttr parses msg's payload as netlink route
// attributes and returns the slice containing the NetlinkRouteAttr
// structs.
-func ParseNetlinkRouteAttr(msg *NetlinkMessage) ([]NetlinkRouteAttr, int) {
+func ParseNetlinkRouteAttr(msg *NetlinkMessage) ([]NetlinkRouteAttr, error) {
var (
buf []byte
a *RtAttr
alen int
vbuf []byte
- e int
+ e error
attrs []NetlinkRouteAttr
)
for len(buf) >= SizeofRtAttr {
a, vbuf, alen, e = netlinkRouteAttrAndValue(buf)
- if e != 0 {
+ if e != nil {
break
}
ra := NetlinkRouteAttr{}
buf = buf[alen:]
}
- return attrs, 0
+ return attrs, nil
}
-func netlinkRouteAttrAndValue(buf []byte) (*RtAttr, []byte, int, int) {
+func netlinkRouteAttrAndValue(buf []byte) (*RtAttr, []byte, int, error) {
h := (*RtAttr)(unsafe.Pointer(&buf[0]))
if int(h.Len) < SizeofRtAttr || int(h.Len) > len(buf) {
return nil, nil, 0, EINVAL
}
- return h, buf[SizeofRtAttr:], rtaAlignOf(int(h.Len)), 0
+ return h, buf[SizeofRtAttr:], rtaAlignOf(int(h.Len)), nil
}