1 /* Shared library add-on to iptables to add comment match support.
2 *
3 * ChangeLog
4 * 2003-05-13: Brad Fisher <[email protected]>
5 * Initial comment match
6 * 2004-05-12: Brad Fisher <[email protected]>
7 * Port to patch-o-matic-ng
8 */
9 #include <stdio.h>
10 #include <xtables.h>
11 #include <linux/netfilter/xt_comment.h>
12
13 enum {
14 O_COMMENT = 0,
15 };
16
comment_help(void)17 static void comment_help(void)
18 {
19 printf(
20 "comment match options:\n"
21 "--comment COMMENT Attach a comment to a rule\n");
22 }
23
24 static const struct xt_option_entry comment_opts[] = {
25 {.name = "comment", .id = O_COMMENT, .type = XTTYPE_STRING,
26 .flags = XTOPT_MAND | XTOPT_PUT,
27 XTOPT_POINTER(struct xt_comment_info, comment)},
28 XTOPT_TABLEEND,
29 };
30
31 static void
comment_print(const void * ip,const struct xt_entry_match * match,int numeric)32 comment_print(const void *ip, const struct xt_entry_match *match, int numeric)
33 {
34 struct xt_comment_info *commentinfo = (void *)match->data;
35
36 commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
37 printf(" /* %s */", commentinfo->comment);
38 }
39
40 /* Saves the union ipt_matchinfo in parsable form to stdout. */
41 static void
comment_save(const void * ip,const struct xt_entry_match * match)42 comment_save(const void *ip, const struct xt_entry_match *match)
43 {
44 struct xt_comment_info *commentinfo = (void *)match->data;
45
46 commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
47 printf(" --comment");
48 xtables_save_string(commentinfo->comment);
49 }
50
comment_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)51 static int comment_xlate(struct xt_xlate *xl,
52 const struct xt_xlate_mt_params *params)
53 {
54 struct xt_comment_info *commentinfo = (void *)params->match->data;
55 char comment[XT_MAX_COMMENT_LEN + sizeof("\\\"\\\"")];
56
57 commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
58 snprintf(comment, sizeof(comment), "\"%s\"", commentinfo->comment);
59
60 xt_xlate_add_comment(xl, comment);
61
62 return 1;
63 }
64
65 static struct xtables_match comment_match = {
66 .family = NFPROTO_UNSPEC,
67 .name = "comment",
68 .version = XTABLES_VERSION,
69 .size = XT_ALIGN(sizeof(struct xt_comment_info)),
70 .userspacesize = XT_ALIGN(sizeof(struct xt_comment_info)),
71 .help = comment_help,
72 .print = comment_print,
73 .save = comment_save,
74 .x6_parse = xtables_option_parse,
75 .x6_options = comment_opts,
76 .xlate = comment_xlate,
77 };
78
_init(void)79 void _init(void)
80 {
81 xtables_register_match(&comment_match);
82 }
83