예제 #1
0
// SendCallback sends the netlink message. The specified callback function
// will be called for each message that is received in response.
func (m *Message) SendCallback(fn CallbackFunc, arg interface{}) error {
	s, err := newSocket()
	if err != nil {
		return err
	}
	defer s.free()

	if errno := C.genl_connect(s.nls); errno != 0 {
		return &Error{errno, "failed to connect to netlink"}
	}
	defer C.nl_close(s.nls)

	cbArg := &callbackArg{fn: fn, arg: arg}
	cbID := registerCallback(cbArg)
	defer unregisterCallback(cbArg)

	if errno := C.nl_socket_modify_cb(s.nls, C.NL_CB_VALID, C.NL_CB_CUSTOM, (C.nl_recvmsg_msg_cb_t)(unsafe.Pointer(C.callbackGateway)), unsafe.Pointer(cbID)); errno != 0 {
		return &Error{errno, "failed to modify callback"}
	}
	// nl_send_auto_complete returns number of bytes sent or a negative
	// errno on failure.
	if errno := C.nl_send_auto_complete(s.nls, m.nlm); errno < 0 {
		return &Error{errno, "failed to send netlink message"}
	}
	if errno := C.nl_recvmsgs_default(s.nls); errno != 0 {
		return &Error{errno, "failed to receive messages"}
	}
	return nil
}
예제 #2
0
// Family returns the family identifier for the specified family name.
func Family(name string) (int, error) {
	s, err := newSocket()
	if err != nil {
		return -1, err
	}
	defer s.free()

	if errno := C.genl_connect(s.nls); errno != 0 {
		return -1, &Error{errno, "failed to connect to netlink"}
	}
	defer C.nl_close((*C.struct_nl_sock)(s.nls))

	cn := C.CString(name)
	defer C.free(unsafe.Pointer(cn))
	family := C.genl_ctrl_resolve(s.nls, cn)
	if family < 0 {
		return -1, errors.New("failed to resolve family name")
	}
	return int(family), nil
}