Blame extensions/libxt_comment.c

Packit Service d1fe03
/* Shared library add-on to iptables to add comment match support.
Packit Service d1fe03
 *
Packit Service d1fe03
 * ChangeLog
Packit Service d1fe03
 *     2003-05-13: Brad Fisher <brad@info-link.net>
Packit Service d1fe03
 *         Initial comment match
Packit Service d1fe03
 *     2004-05-12: Brad Fisher <brad@info-link.net>
Packit Service d1fe03
 *         Port to patch-o-matic-ng
Packit Service d1fe03
 */
Packit Service d1fe03
#include <stdio.h>
Packit Service d1fe03
#include <xtables.h>
Packit Service d1fe03
#include <linux/netfilter/xt_comment.h>
Packit Service d1fe03
Packit Service d1fe03
enum {
Packit Service d1fe03
	O_COMMENT = 0,
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
static void comment_help(void)
Packit Service d1fe03
{
Packit Service d1fe03
	printf(
Packit Service d1fe03
		"comment match options:\n"
Packit Service d1fe03
		"--comment COMMENT             Attach a comment to a rule\n");
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static const struct xt_option_entry comment_opts[] = {
Packit Service d1fe03
	{.name = "comment", .id = O_COMMENT, .type = XTTYPE_STRING,
Packit Service d1fe03
	 .flags = XTOPT_MAND | XTOPT_PUT,
Packit Service d1fe03
	 XTOPT_POINTER(struct xt_comment_info, comment)},
Packit Service d1fe03
	XTOPT_TABLEEND,
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
static void
Packit Service d1fe03
comment_print(const void *ip, const struct xt_entry_match *match, int numeric)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_comment_info *commentinfo = (void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
Packit Service d1fe03
	printf(" /* %s */", commentinfo->comment);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
/* Saves the union ipt_matchinfo in parsable form to stdout. */
Packit Service d1fe03
static void
Packit Service d1fe03
comment_save(const void *ip, const struct xt_entry_match *match)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_comment_info *commentinfo = (void *)match->data;
Packit Service d1fe03
Packit Service d1fe03
	commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
Packit Service d1fe03
	printf(" --comment");
Packit Service d1fe03
	xtables_save_string(commentinfo->comment);
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static int comment_xlate(struct xt_xlate *xl,
Packit Service d1fe03
			 const struct xt_xlate_mt_params *params)
Packit Service d1fe03
{
Packit Service d1fe03
	struct xt_comment_info *commentinfo = (void *)params->match->data;
Packit Service d1fe03
	char comment[XT_MAX_COMMENT_LEN + sizeof("\\\"\\\"")];
Packit Service d1fe03
Packit Service d1fe03
	commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
Packit Service d1fe03
	if (params->escape_quotes)
Packit Service d1fe03
		snprintf(comment, sizeof(comment), "\\\"%s\\\"",
Packit Service d1fe03
			 commentinfo->comment);
Packit Service d1fe03
	else
Packit Service d1fe03
		snprintf(comment, sizeof(comment), "\"%s\"",
Packit Service d1fe03
			 commentinfo->comment);
Packit Service d1fe03
Packit Service d1fe03
	xt_xlate_add_comment(xl, comment);
Packit Service d1fe03
Packit Service d1fe03
	return 1;
Packit Service d1fe03
}
Packit Service d1fe03
Packit Service d1fe03
static struct xtables_match comment_match = {
Packit Service d1fe03
	.family		= NFPROTO_UNSPEC,
Packit Service d1fe03
	.name		= "comment",
Packit Service d1fe03
	.version	= XTABLES_VERSION,
Packit Service d1fe03
	.size		= XT_ALIGN(sizeof(struct xt_comment_info)),
Packit Service d1fe03
	.userspacesize	= XT_ALIGN(sizeof(struct xt_comment_info)),
Packit Service d1fe03
	.help		= comment_help,
Packit Service d1fe03
	.print 		= comment_print,
Packit Service d1fe03
	.save 		= comment_save,
Packit Service d1fe03
	.x6_parse	= xtables_option_parse,
Packit Service d1fe03
	.x6_options	= comment_opts,
Packit Service d1fe03
	.xlate		= comment_xlate,
Packit Service d1fe03
};
Packit Service d1fe03
Packit Service d1fe03
void _init(void)
Packit Service d1fe03
{
Packit Service d1fe03
	xtables_register_match(&comment_match);
Packit Service d1fe03
}