/* Allocate a new netlink message with the default maximum payload size. */ struct nl_msg *nlmsg_alloc(void) { /* Whole page will store nl_msg + nlmsghdr + genlmsghdr + payload */ const int page_sz = getpagesize(); struct nl_msg *nm; struct nlmsghdr *nlh; /* Netlink message */ nm = (struct nl_msg *) malloc(page_sz); if (!nm) goto fail; /* Netlink message header pointer */ nlh = (struct nlmsghdr *) ((char *) nm + sizeof(struct nl_msg)); /* Initialize */ memset(nm, 0, page_sz); nm->nm_size = page_sz; nm->nm_src.nl_family = AF_NETLINK; nm->nm_src.nl_pid = getpid(); nm->nm_dst.nl_family = AF_NETLINK; nm->nm_dst.nl_pid = 0; /* Kernel */ /* Initialize and add to netlink message */ nlh->nlmsg_len = NLMSG_HDRLEN; nm->nm_nlh = nlh; /* Add to reference count and return nl_msg */ nlmsg_get(nm); return nm; fail: return NULL; }
static int request_single_cb(struct nl_msg *msg, void *arg) { struct nl_msg **dest = arg; if (!*dest) { nlmsg_get(msg); *dest = msg; } return NL_SKIP; }