Blame tipc/msg.c

Packit d3f73b
/*
Packit d3f73b
 * msg.c	Messaging (netlink) helper functions.
Packit d3f73b
 *
Packit d3f73b
 *		This program is free software; you can redistribute it and/or
Packit d3f73b
 *		modify it under the terms of the GNU General Public License
Packit d3f73b
 *		as published by the Free Software Foundation; either version
Packit d3f73b
 *		2 of the License, or (at your option) any later version.
Packit d3f73b
 *
Packit d3f73b
 * Authors:	Richard Alpe <richard.alpe@ericsson.com>
Packit d3f73b
 */
Packit d3f73b
Packit d3f73b
#include <stdio.h>
Packit d3f73b
#include <time.h>
Packit d3f73b
#include <errno.h>
Packit d3f73b
Packit d3f73b
#include <linux/tipc_netlink.h>
Packit d3f73b
#include <linux/tipc.h>
Packit d3f73b
#include <linux/genetlink.h>
Packit d3f73b
#include <libmnl/libmnl.h>
Packit d3f73b
Packit d3f73b
#include "msg.h"
Packit d3f73b
Packit d3f73b
int parse_attrs(const struct nlattr *attr, void *data)
Packit d3f73b
{
Packit d3f73b
	const struct nlattr **tb = data;
Packit d3f73b
	int type = mnl_attr_get_type(attr);
Packit d3f73b
Packit d3f73b
	tb[type] = attr;
Packit d3f73b
Packit d3f73b
	return MNL_CB_OK;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int family_id_cb(const struct nlmsghdr *nlh, void *data)
Packit d3f73b
{
Packit d3f73b
	struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
Packit d3f73b
	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
Packit d3f73b
	int *id = data;
Packit d3f73b
Packit d3f73b
	mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, tb);
Packit d3f73b
	if (!tb[CTRL_ATTR_FAMILY_ID])
Packit d3f73b
		return MNL_CB_ERROR;
Packit d3f73b
Packit d3f73b
	*id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
Packit d3f73b
Packit d3f73b
	return MNL_CB_OK;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static struct mnl_socket *msg_send(struct nlmsghdr *nlh)
Packit d3f73b
{
Packit d3f73b
	int ret;
Packit d3f73b
	struct mnl_socket *nl;
Packit d3f73b
Packit d3f73b
	nl = mnl_socket_open(NETLINK_GENERIC);
Packit d3f73b
	if (nl == NULL) {
Packit d3f73b
		perror("mnl_socket_open");
Packit d3f73b
		return NULL;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	ret = mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID);
Packit d3f73b
	if (ret < 0) {
Packit d3f73b
		perror("mnl_socket_bind");
Packit d3f73b
		return NULL;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
Packit d3f73b
	if (ret < 0) {
Packit d3f73b
		perror("mnl_socket_send");
Packit d3f73b
		return NULL;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	return nl;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int msg_recv(struct mnl_socket *nl, mnl_cb_t callback, void *data, int seq)
Packit d3f73b
{
Packit d3f73b
	int ret;
Packit d3f73b
	unsigned int portid;
Packit d3f73b
	char buf[MNL_SOCKET_BUFFER_SIZE];
Packit d3f73b
Packit d3f73b
	portid = mnl_socket_get_portid(nl);
Packit d3f73b
Packit d3f73b
	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
Packit d3f73b
	while (ret > 0) {
Packit d3f73b
		ret = mnl_cb_run(buf, ret, seq, portid, callback, data);
Packit d3f73b
		if (ret <= 0)
Packit d3f73b
			break;
Packit d3f73b
		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
Packit d3f73b
	}
Packit d3f73b
	if (ret == -1)
Packit d3f73b
		perror("error");
Packit d3f73b
Packit d3f73b
	mnl_socket_close(nl);
Packit d3f73b
Packit d3f73b
	return ret;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int msg_query(struct nlmsghdr *nlh, mnl_cb_t callback, void *data)
Packit d3f73b
{
Packit d3f73b
	unsigned int seq;
Packit d3f73b
	struct mnl_socket *nl;
Packit d3f73b
Packit d3f73b
	seq = time(NULL);
Packit d3f73b
	nlh->nlmsg_seq = seq;
Packit d3f73b
Packit d3f73b
	nl = msg_send(nlh);
Packit d3f73b
	if (!nl)
Packit d3f73b
		return -ENOTSUP;
Packit d3f73b
Packit d3f73b
	return msg_recv(nl, callback, data, seq);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
static int get_family(void)
Packit d3f73b
{
Packit d3f73b
	int err;
Packit d3f73b
	int nl_family;
Packit d3f73b
	struct nlmsghdr *nlh;
Packit d3f73b
	struct genlmsghdr *genl;
Packit d3f73b
	char buf[MNL_SOCKET_BUFFER_SIZE];
Packit d3f73b
Packit d3f73b
	nlh = mnl_nlmsg_put_header(buf);
Packit d3f73b
	nlh->nlmsg_type	= GENL_ID_CTRL;
Packit d3f73b
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
Packit d3f73b
Packit d3f73b
	genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
Packit d3f73b
	genl->cmd = CTRL_CMD_GETFAMILY;
Packit d3f73b
	genl->version = 1;
Packit d3f73b
Packit d3f73b
	mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
Packit d3f73b
	mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, TIPC_GENL_V2_NAME);
Packit d3f73b
Packit d3f73b
	if ((err = msg_query(nlh, family_id_cb, &nl_family)))
Packit d3f73b
		return err;
Packit d3f73b
Packit d3f73b
	return nl_family;
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
int msg_doit(struct nlmsghdr *nlh, mnl_cb_t callback, void *data)
Packit d3f73b
{
Packit d3f73b
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
Packit d3f73b
	return msg_query(nlh, callback, data);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
int msg_dumpit(struct nlmsghdr *nlh, mnl_cb_t callback, void *data)
Packit d3f73b
{
Packit d3f73b
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
Packit d3f73b
	return msg_query(nlh, callback, data);
Packit d3f73b
}
Packit d3f73b
Packit d3f73b
struct nlmsghdr *msg_init(char *buf, int cmd)
Packit d3f73b
{
Packit d3f73b
	int family;
Packit d3f73b
	struct nlmsghdr *nlh;
Packit d3f73b
	struct genlmsghdr *genl;
Packit d3f73b
Packit d3f73b
	family = get_family();
Packit d3f73b
	if (family <= 0) {
Packit d3f73b
		fprintf(stderr,
Packit d3f73b
			"Unable to get TIPC nl family id (module loaded?)\n");
Packit d3f73b
		return NULL;
Packit d3f73b
	}
Packit d3f73b
Packit d3f73b
	nlh = mnl_nlmsg_put_header(buf);
Packit d3f73b
	nlh->nlmsg_type	= family;
Packit d3f73b
Packit d3f73b
	genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
Packit d3f73b
	genl->cmd = cmd;
Packit d3f73b
	genl->version = 1;
Packit d3f73b
Packit d3f73b
	return nlh;
Packit d3f73b
}