Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   netlink    ( 7 )

связь между ядром и пользовательским пространством (AF_NETLINK) (communication between kernel and user space (AF_NETLINK))

  Name  |  Synopsis  |  Description  |  Versions  |  Note  |  Bugs  |    Examples    |  See also  |

Примеры (Examples)

The following example creates a NETLINK_ROUTE netlink socket
       which will listen to the RTMGRP_LINK (network interface
       create/delete/up/down events) and RTMGRP_IPV4_IFADDR (IPv4
       addresses add/delete events) multicast groups.

struct sockaddr_nl sa;

memset(&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;

fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); bind(fd, (struct sockaddr *) &sa, sizeof(sa));

The next example demonstrates how to send a netlink message to the kernel (pid 0). Note that the application must take care of message sequence numbers in order to reliably track acknowledgements.

struct nlmsghdr *nh; /* The nlmsghdr with payload to send */ struct sockaddr_nl sa; struct iovec iov = { nh, nh->nlmsg_len }; struct msghdr msg;

msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; memset(&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; nh->nlmsg_pid = 0; nh->nlmsg_seq = ++sequence_number; /* Request an ack from kernel by setting NLM_F_ACK */ nh->nlmsg_flags |= NLM_F_ACK;

sendmsg(fd, &msg, 0);

And the last example is about reading netlink message.

int len; /* 8192 to avoid message truncation on platforms with page size > 4096 */ struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)]; struct iovec iov = { buf, sizeof(buf) }; struct sockaddr_nl sa; struct msghdr msg; struct nlmsghdr *nh;

msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; len = recvmsg(fd, &msg, 0);

for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); nh = NLMSG_NEXT (nh, len)) { /* The end of multipart message */ if (nh->nlmsg_type == NLMSG_DONE) return;

if (nh->nlmsg_type == NLMSG_ERROR) /* Do some error handling */ ...

/* Continue with parsing payload */ ... }