blob: 8c59e2a66ccbe62dc634559da46c9b39a8b30688 [file] [log] [blame]
Martin Josefsson110610b2002-01-11 18:41:02 +00001/* Shared library add-on to iptables to add related packet matching support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7
8#include <iptables.h>
9#include <linux/netfilter_ipv4/ipt_helper.h>
10
11/* Function which prints out usage message. */
12static void
13help(void)
14{
15 printf(
16"helper match v%s options:\n"
17"[!] --helper value Match helper value\n"
18"\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000019IPTABLES_VERSION);
Martin Josefsson110610b2002-01-11 18:41:02 +000020}
21
22static struct option opts[] = {
23 { "helper", 1, 0, '1' },
24 {0}
25};
26
27/* Initialize the match. */
28static void
29init(struct ipt_entry_match *m, unsigned int *nfcache)
30{
31 /* Can't cache this. */
32 *nfcache |= NFC_UNKNOWN;
33}
34
35/* Function which parses command options; returns true if it
36 ate an option */
37static int
38parse(int c, char **argv, int invert, unsigned int *flags,
39 const struct ipt_entry *entry,
40 unsigned int *nfcache,
41 struct ipt_entry_match **match)
42{
43 struct ipt_helper_info *info = (struct ipt_helper_info *)(*match)->data;
44
45 switch (c) {
46 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +000047 check_inverse(optarg, &invert, &invert, 0);
Martin Josefsson110610b2002-01-11 18:41:02 +000048 strncpy(info->name, optarg, 29);
49 if (invert)
50 info->invert = 1;
51 *flags = 1;
52 break;
53
54 default:
55 return 0;
56 }
57 return 1;
58}
59
60/* Final check; must have specified --helper. */
61static void
62final_check(unsigned int flags)
63{
64 if (!flags)
65 exit_error(PARAMETER_PROBLEM,
66 "helper match: You must specify `--helper'");
67}
68
69/* Prints out the info. */
70static void
71print(const struct ipt_ip *ip,
72 const struct ipt_entry_match *match,
73 int numeric)
74{
75 struct ipt_helper_info *info = (struct ipt_helper_info *)match->data;
76
77 printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name);
78}
79
80/* Saves the union ipt_info in parsable form to stdout. */
81static void
82save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
83{
84 struct ipt_helper_info *info = (struct ipt_helper_info *)match->data;
85
86 printf("%s--helper \"%s\" ",info->invert ? "! " : "", info->name);
87}
88
89static
90struct iptables_match helper
91= { NULL,
92 "helper",
Harald Welte80fe35d2002-05-29 13:08:15 +000093 IPTABLES_VERSION,
Martin Josefsson110610b2002-01-11 18:41:02 +000094 IPT_ALIGN(sizeof(struct ipt_helper_info)),
95 IPT_ALIGN(sizeof(struct ipt_helper_info)),
96 &help,
97 &init,
98 &parse,
99 &final_check,
100 &print,
101 &save,
102 opts
103};
104
105void _init(void)
106{
107 register_match(&helper);
108}