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