iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_helper.c
1 /* 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. */
12 static void
13 help(void)
14 {
15         printf(
16 "helper match v%s options:\n"
17 "[!] --helper string        Match helper identified by string\n"
18 "\n",
19 IPTABLES_VERSION);
20 }
21
22 static struct option opts[] = {
23         { "helper", 1, 0, '1' },
24         {0}
25 };
26
27 /* Initialize the match. */
28 static void
29 init(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 */
37 static int
38 parse(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':
47                 check_inverse(optarg, &invert, &invert, 0);
48                 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. */
61 static void
62 final_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. */
70 static void
71 print(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. */
81 static void
82 save(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
89 static
90 struct iptables_match helper
91 = { NULL,
92     "helper",
93     IPTABLES_VERSION,
94     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
105 void _init(void)
106 {
107         register_match(&helper);
108 }