libxt_helper.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1/* Shared library add-on to iptables to add related packet matching support. */
2#include <stdbool.h>
3#include <stdio.h>
4#include <netdb.h>
5#include <string.h>
6#include <stdlib.h>
7#include <getopt.h>
8
9#include <xtables.h>
10#include <linux/netfilter/xt_helper.h>
11
12static void helper_help(void)
13{
14	printf(
15"helper match options:\n"
16"[!] --helper string        Match helper identified by string\n");
17}
18
19static const struct option helper_opts[] = {
20	{.name = "helper", .has_arg = true, .val = '1'},
21	XT_GETOPT_TABLEEND,
22};
23
24static int
25helper_parse(int c, char **argv, int invert, unsigned int *flags,
26             const void *entry, struct xt_entry_match **match)
27{
28	struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data;
29
30	switch (c) {
31	case '1':
32		if (*flags)
33			xtables_error(PARAMETER_PROBLEM,
34					"helper match: Only use --helper ONCE!");
35		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
36		strncpy(info->name, optarg, 29);
37		info->name[29] = '\0';
38		if (invert)
39			info->invert = 1;
40		*flags = 1;
41		break;
42
43	default:
44		return 0;
45	}
46	return 1;
47}
48
49static void helper_check(unsigned int flags)
50{
51	if (!flags)
52		xtables_error(PARAMETER_PROBLEM,
53			   "helper match: You must specify `--helper'");
54}
55
56static void
57helper_print(const void *ip, const struct xt_entry_match *match, int numeric)
58{
59	const struct xt_helper_info *info = (const void *)match->data;
60
61	printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name);
62}
63
64static void helper_save(const void *ip, const struct xt_entry_match *match)
65{
66	const struct xt_helper_info *info = (const void *)match->data;
67
68	printf("%s--helper ",info->invert ? "! " : "");
69	xtables_save_string(info->name);
70}
71
72static struct xtables_match helper_match = {
73	.family		= NFPROTO_UNSPEC,
74	.name		= "helper",
75	.version	= XTABLES_VERSION,
76	.size		= XT_ALIGN(sizeof(struct xt_helper_info)),
77	.help		= helper_help,
78	.parse		= helper_parse,
79	.final_check	= helper_check,
80	.print		= helper_print,
81	.save		= helper_save,
82	.extra_opts	= helper_opts,
83};
84
85void _init(void)
86{
87	xtables_register_match(&helper_match);
88}
89