예제 #1
0
// Bytes returns the byte slice representation of a netlink message.
func (m *Message) Bytes() ([]byte, error) {
	if m.nlm == nil {
		return nil, errors.New("no netlink message")
	}
	nlh := C.nlmsg_hdr(m.nlm)
	nlb := make([]byte, nlh.nlmsg_len)
	copy(nlb, (*[1 << 20]byte)(unsafe.Pointer(nlh))[:nlh.nlmsg_len])
	return nlb, nil
}
예제 #2
0
// NewMessageFromBytes returns a netlink message that is initialised from the
// given byte slice.
func NewMessageFromBytes(nlb []byte) (*Message, error) {
	nlm := C.nlmsg_alloc_size(C.size_t(len(nlb)))
	if nlm == nil {
		return nil, errors.New("failed to create netlink message")
	}
	nlh := C.nlmsg_hdr(nlm)
	copy((*[1 << 20]byte)(unsafe.Pointer(nlh))[:len(nlb)], nlb)
	return &Message{nlm: nlm}, nil
}
예제 #3
0
func parseMessage(nlm *C.struct_nl_msg, maxAttrID uint16) (map[uint16]*attribute, error) {
	if nlm == nil {
		return nil, errors.New("no netlink message")
	}

	nlas, err := newNLAttrs(uint(maxAttrID) + 1)
	if err != nil {
		return nil, err
	}
	defer nlas.free()

	var nlaPolicy *C.struct_nla_policy
	if errno := C.genlmsg_parse(C.nlmsg_hdr(nlm), 0, nlas.attrs, C.int(maxAttrID), nlaPolicy); errno != 0 {
		return nil, &Error{errno, "failed to parse netlink message"}
	}

	return nlas.attributes(), nil
}