Re: Connector - how to start?

Evgeniy Polyakov
2006-04-20T15:25:36Z

On Thu, Apr 20, 2006 at 05:12:33PM +0200, Libor Vanek (libor.vanek@gmail.com) wrote:

> - When sending message from kernel, cn_netlink_send returns an error
> in case when there is no reciever - BUT user space "send" doesn't
> return an error when there is no reciever in kernel :-(

Userspace sends it into kernel socket. Groups were created for
userspace's sockets, so there is always a listener in kernelspace.

> - How (if) can I do ACK (acknoledge received and processed message)?

I put seq and ack fields into connector header, and described a simple
protocol for it's usage, but it does require some work by driver author,
it is impossible to generate automatic ack messages in netlink
(well, it is possible to generate nlmsgerr message sending but generally
it is not what people want with protocols designed over connector).

> Thanks for your help,
> Libor Vanek

> /*
> * cn_send.c
> */

...

> void my_send_test(void)
> {
> struct cn_msg *msg;
> char data[32];
> int ret;
>
> msg = kmalloc(sizeof(*msg) + sizeof(data), GFP_ATOMIC);

Not a good style of allocating data from atomic pool without checks for
return value.
Btw, you do not need to allocate it dynamically,
char msg[sizeof(struct cn_msg) + 32] will work too.

...

> /*
> * cn_user_recv.c
> */

...

> l_local.nl_family = AF_NETLINK;
> l_local.nl_groups = 0x3;
> l_local.nl_pid = getpid();
>
> if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
> perror("bind");
> close(s);
> return -1;
> }
> int on = l_local.nl_groups;
> if (setsockopt(s, 270, 1, &on, sizeof(on))) {
> perror("setsockopt");
> close(s);
> return -1;
> }

In this example you will receive messages for groups 1, 2 (bind time
gropus 0x3 is equal to (1<<(1-1)) | (1<<(2-1))) and group 3 (you
have subscribed to that gropu explicitly).