Blame libnetlabel/netlabel_msg.c

Packit 51d0f7
/** @file
Packit 51d0f7
 * NetLabel Message Functions
Packit 51d0f7
 *
Packit 51d0f7
 * Author: Paul Moore <paul@paul-moore.com>
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
/*
Packit 51d0f7
 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
Packit 51d0f7
 *
Packit 51d0f7
 * This program is free software: you can redistribute it and/or modify
Packit 51d0f7
 * it under the terms of version 2 of the GNU General Public License as
Packit 51d0f7
 * published by the Free Software Foundation.
Packit 51d0f7
 *
Packit 51d0f7
 * This program is distributed in the hope that it will be useful,
Packit 51d0f7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 51d0f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 51d0f7
 * GNU General Public License for more details.
Packit 51d0f7
 *
Packit 51d0f7
 * You should have received a copy of the GNU General Public License
Packit 51d0f7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
#include <stdlib.h>
Packit 51d0f7
#include <errno.h>
Packit 51d0f7
#include <sys/types.h>
Packit 51d0f7
#include <sys/socket.h>
Packit 51d0f7
#include <linux/types.h>
Packit 51d0f7
Packit 51d0f7
#include <libnetlabel.h>
Packit 51d0f7
Packit 51d0f7
#include "netlabel_internal.h"
Packit 51d0f7
Packit 51d0f7
/*
Packit 51d0f7
 * Allocation Functions
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Free a NetLabel message
Packit 51d0f7
 * @param msg the NetLabel message
Packit 51d0f7
 *
Packit 51d0f7
 * Free the memory associated with a NetLabel message.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
void nlbl_msg_free(nlbl_msg *msg)
Packit 51d0f7
{
Packit 51d0f7
	if (msg == NULL)
Packit 51d0f7
		return;
Packit 51d0f7
	nlmsg_free(msg);
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Create a new NetLabel message
Packit 51d0f7
 *
Packit 51d0f7
 * Creates a new NetLabel message and allocates space for both the Netlink and
Packit 51d0f7
 * Generic Netlink headers.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
nlbl_msg *nlbl_msg_new(void)
Packit 51d0f7
{
Packit 51d0f7
	nlbl_msg *msg;
Packit 51d0f7
	void *msg_buf;
Packit 51d0f7
Packit 51d0f7
	msg = nlmsg_alloc();
Packit 51d0f7
	if (msg == NULL)
Packit 51d0f7
		goto msg_new_failure;
Packit 51d0f7
Packit 51d0f7
	msg_buf = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, 0, 0, 0, 0, 0);
Packit 51d0f7
	if (msg_buf == NULL)
Packit 51d0f7
		goto msg_new_failure;
Packit 51d0f7
Packit 51d0f7
	return msg;
Packit 51d0f7
Packit 51d0f7
msg_new_failure:
Packit 51d0f7
	nlbl_msg_free(msg);
Packit 51d0f7
	return NULL;
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/*
Packit 51d0f7
 * Netlink Header Functions
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Get the Netlink header from the NetLabel message
Packit 51d0f7
 * @param msg the NetLabel message
Packit 51d0f7
 *
Packit 51d0f7
 * Returns the Netlink header on success, or NULL on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
struct nlmsghdr *nlbl_msg_nlhdr(nlbl_msg *msg)
Packit 51d0f7
{
Packit 51d0f7
	if (msg == NULL)
Packit 51d0f7
		return NULL;
Packit 51d0f7
	return nlmsg_hdr(msg);
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Get the Generic Netlink header from the NetLabel message
Packit 51d0f7
 * @param msg the NetLabel message
Packit 51d0f7
 *
Packit 51d0f7
 * Returns the Generic Netlink header on success, or NULL on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
struct genlmsghdr *nlbl_msg_genlhdr(nlbl_msg *msg)
Packit 51d0f7
{
Packit 51d0f7
	struct nlmsghdr *nl_hdr;
Packit 51d0f7
Packit 51d0f7
	/* NOTE: nlbl_msg_nlhdr() can handle being passed a NULL msg */
Packit 51d0f7
	nl_hdr = nlbl_msg_nlhdr(msg);
Packit 51d0f7
	if (nl_hdr == NULL)
Packit 51d0f7
		return NULL;
Packit 51d0f7
Packit 51d0f7
	return genlmsg_hdr(nl_hdr);
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/*
Packit 51d0f7
 * Netlink Message Functions
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Return the nlmsgerr struct in the Netlink message
Packit 51d0f7
 * @param msg the NetLabel message
Packit 51d0f7
 *
Packit 51d0f7
 * Returns a pointer to the nlmsgerr struct in a NLMSG_ERROR Netlink message
Packit 51d0f7
 * or NULL on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
struct nlmsgerr *nlbl_msg_err(nlbl_msg *msg)
Packit 51d0f7
{
Packit 51d0f7
	struct nlmsghdr *nl_hdr;
Packit 51d0f7
Packit 51d0f7
	/* NOTE: nlbl_msg_nlhdr() can handle being passed a NULL msg */
Packit 51d0f7
	nl_hdr = nlbl_msg_nlhdr(msg);
Packit 51d0f7
	if (nl_hdr == NULL || nl_hdr->nlmsg_type != NLMSG_ERROR)
Packit 51d0f7
		return NULL;
Packit 51d0f7
	return nlmsg_data(nl_hdr);
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/*
Packit 51d0f7
 * Netlink Attribute Functions
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Get the head of the Netlink attributes from a NetLabel msg
Packit 51d0f7
 * @param msg the NetLabel message
Packit 51d0f7
 *
Packit 51d0f7
 * Returns the head of the Netlink attributes from a NetLabel message on
Packit 51d0f7
 * success, or NULL on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
struct nlattr *nlbl_attr_head(nlbl_msg *msg)
Packit 51d0f7
{
Packit 51d0f7
	struct genlmsghdr *genl_hdr;
Packit 51d0f7
Packit 51d0f7
	/* NOTE: nlbl_msg_genlhdr() can handle being passed a NULL msg */
Packit 51d0f7
	genl_hdr = nlbl_msg_genlhdr(msg);
Packit 51d0f7
	if (genl_hdr == NULL)
Packit 51d0f7
		return NULL;
Packit 51d0f7
Packit 51d0f7
	return genlmsg_attrdata(genl_hdr, 0);
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Find an attribute in a NetLabel message
Packit 51d0f7
 * @param msg the NetLabel message
Packit 51d0f7
 * @param nla_type the attribute type
Packit 51d0f7
 *
Packit 51d0f7
 * Search @msg looking for the @nla_type attribute.  Return the attribute on
Packit 51d0f7
 * success, or NULL on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
struct nlattr *nlbl_attr_find(nlbl_msg *msg, int nla_type)
Packit 51d0f7
{
Packit 51d0f7
	struct genlmsghdr *genl_hdr;
Packit 51d0f7
	struct nlattr *nla_head;
Packit 51d0f7
Packit 51d0f7
	/* NOTE: nlbl_msg_genlhdr() can handle being passed a NULL msg */
Packit 51d0f7
	genl_hdr = nlbl_msg_genlhdr(msg);
Packit 51d0f7
	if (genl_hdr == NULL)
Packit 51d0f7
		return NULL;
Packit 51d0f7
Packit 51d0f7
	nla_head = genlmsg_attrdata(genl_hdr, 0);
Packit 51d0f7
	if (nla_head == NULL)
Packit 51d0f7
		return NULL;
Packit 51d0f7
Packit 51d0f7
	return nla_find(nla_head, genlmsg_attrlen(genl_hdr, 0), nla_type);
Packit 51d0f7
}