File: libxt_LOGMARK.c

package info (click to toggle)
xtables-addons 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,296 kB
  • ctags: 1,415
  • sloc: sh: 11,559; ansic: 10,329; perl: 186; makefile: 121
file content (117 lines) | stat: -rw-r--r-- 3,352 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 *	"LOGMARK" target extension for iptables
 *	Copyright © Jan Engelhardt, 2008
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License; either
 *	version 2 of the License, or any later version, as published by the
 *	Free Software Foundation.
 */
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <xtables.h>
#include "xt_LOGMARK.h"
#include "compat_user.h"

enum {
	F_LEVEL  = 1 << 0,
	F_PREFIX = 1 << 1,
};

static const struct option logmark_tg_opts[] = {
	{.name = "log-level",   .has_arg = true,  .val = 'l'},
	{.name = "log-prefix",  .has_arg = true,  .val = 'p'},
	{NULL},
};

static void logmark_tg_help(void)
{
	printf(
"LOGMARK target options:\n"
"  --log-level level      Level of logging (numeric, 0-8)\n"
"  --log-prefix prefix    Prefix log messages with this string\n"
);
}

static void logmark_tg_init(struct xt_entry_target *target)
{
	struct xt_logmark_tginfo *info = (void *)target->data;

	info->level   = 4;
	*info->prefix = '\0';
}

static int
logmark_tg_parse(int c, char **argv, int invert, unsigned int *flags,
                 const void *entry, struct xt_entry_target **target)
{
	struct xt_logmark_tginfo *info = (void *)(*target)->data;
	unsigned int x;

	switch (c) {
	case 'l': /* --log-level */
		xtables_param_act(XTF_ONLY_ONCE, "LOGMARK", "--log-level", *flags & F_LEVEL);
		xtables_param_act(XTF_NO_INVERT, "LOGMARK", "--log-level", invert);
		if (!xtables_strtoui(optarg, NULL, &x, 0, 8))
			xtables_param_act(XTF_BAD_VALUE, "LOGMARK", "--log-level", optarg);
		info->level = x;
		*flags |= F_LEVEL;
		return true;

	case 'p': /* --log-prefix */
		xtables_param_act(XTF_ONLY_ONCE, "LOGMARK", "--log-prefix", *flags & F_PREFIX);
		xtables_param_act(XTF_NO_INVERT, "LOGMARK", "--log-prefix", invert);
		if (strlen(optarg) > sizeof(info->prefix))
			xtables_error(PARAMETER_PROBLEM, "LOGMARK: Maximum "
			           "prefix length is %zu",
			           sizeof(info->prefix));
		if (strchr(optarg, '\n'))
			xtables_error(PARAMETER_PROBLEM, "LOGMARK: Newlines not "
			           "allowed in log prefix");
		strncpy(info->prefix, optarg, sizeof(info->prefix));
		*flags |= F_PREFIX;
		return true;
	}
	return false;
}

static void
logmark_tg_save(const void *ip, const struct xt_entry_target *target)
{
	const struct xt_logmark_tginfo *info = (void *)target->data;

	if (info->level != 4)
		printf(" --log-level %u ", info->level);
	if (*info->prefix != '\0')
		printf(" --log-prefix \"%s\" ", info->prefix);
}

static void
logmark_tg_print(const void *ip, const struct xt_entry_target *target,
                 int numeric)
{
	printf(" -j LOGMARK");
	logmark_tg_save(ip, target);
}

static struct xtables_target logmark_tg_reg = {
	.version       = XTABLES_VERSION,
	.name          = "LOGMARK",
	.revision      = 0,
	.family        = NFPROTO_UNSPEC,
	.size          = XT_ALIGN(sizeof(struct xt_logmark_tginfo)),
	.userspacesize = XT_ALIGN(sizeof(struct xt_logmark_tginfo)),
	.help          = logmark_tg_help,
	.init          = logmark_tg_init,
	.parse         = logmark_tg_parse,
	.print         = logmark_tg_print,
	.save          = logmark_tg_save,
	.extra_opts    = logmark_tg_opts,
};

static __attribute__((constructor)) void logmark_tg_ldr(void)
{
	xtables_register_target(&logmark_tg_reg);
}