blob: e29ce166964370a16182d4c30e4be2baedf9672a [file] [log] [blame]
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +00001/* Shared library add-on to iptables to add comment match support.
2 *
3 * ChangeLog
4 * 2003-05-13: Brad Fisher <brad@info-link.net>
5 * Initial comment match
6 * 2004-05-12: Brad Fisher <brad@info-link.net>
7 * Port to patch-o-matic-ng
8 */
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#include <getopt.h>
13
14#include <xtables.h>
15#include <linux/netfilter/xt_comment.h>
16
17/* Function which prints out usage message. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000018static void comment_help(void)
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000019{
20 printf(
21 "COMMENT match options:\n"
22 "--comment COMMENT Attach a comment to a rule\n\n"
23 );
24}
25
Jan Engelhardt181dead2007-10-04 16:27:07 +000026static const struct option comment_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000027 { "comment", 1, NULL, '1' },
28 { }
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000029};
30
31static void
32parse_comment(const char *s, struct xt_comment_info *info)
33{
34 int slen = strlen(s);
35
36 if (slen >= XT_MAX_COMMENT_LEN) {
37 exit_error(PARAMETER_PROBLEM,
38 "COMMENT must be shorter than %i characters", XT_MAX_COMMENT_LEN);
39 }
40 strcpy((char *)info->comment, s);
41}
42
43/* Function which parses command options; returns true if it
44 ate an option */
45static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000046comment_parse(int c, char **argv, int invert, unsigned int *flags,
47 const void *entry, struct xt_entry_match **match)
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000048{
49 struct xt_comment_info *commentinfo = (struct xt_comment_info *)(*match)->data;
50
51 switch (c) {
52 case '1':
53 check_inverse(argv[optind-1], &invert, &optind, 0);
54 if (invert) {
55 exit_error(PARAMETER_PROBLEM,
56 "Sorry, you can't have an inverted comment");
57 }
58 parse_comment(argv[optind-1], commentinfo);
59 *flags = 1;
60 break;
61
62 default:
63 return 0;
64 }
65 return 1;
66}
67
68/* Final check; must have specified --comment. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000069static void comment_check(unsigned int flags)
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000070{
71 if (!flags)
72 exit_error(PARAMETER_PROBLEM,
73 "COMMENT match: You must specify `--comment'");
74}
75
76/* Prints out the matchinfo. */
77static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000078comment_print(const void *ip, const struct xt_entry_match *match, int numeric)
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000079{
80 struct xt_comment_info *commentinfo = (struct xt_comment_info *)match->data;
81
82 commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
83 printf("/* %s */ ", commentinfo->comment);
84}
85
86/* Saves the union ipt_matchinfo in parsable form to stdout. */
87static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000088comment_save(const void *ip, const struct xt_entry_match *match)
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000089{
90 struct xt_comment_info *commentinfo = (struct xt_comment_info *)match->data;
91
92 commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
93 printf("--comment \"%s\" ", commentinfo->comment);
94}
95
Jan Engelhardt181dead2007-10-04 16:27:07 +000096static struct xtables_match comment_match = {
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +000097 .family = AF_INET,
98 .name = "comment",
99 .version = IPTABLES_VERSION,
100 .size = XT_ALIGN(sizeof(struct xt_comment_info)),
101 .userspacesize = XT_ALIGN(sizeof(struct xt_comment_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000102 .help = comment_help,
103 .parse = comment_parse,
104 .final_check = comment_check,
105 .print = comment_print,
106 .save = comment_save,
107 .extra_opts = comment_opts,
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +0000108};
109
Jan Engelhardt181dead2007-10-04 16:27:07 +0000110static struct xtables_match comment_match6 = {
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +0000111 .family = AF_INET6,
112 .name = "comment",
113 .version = IPTABLES_VERSION,
114 .size = XT_ALIGN(sizeof(struct xt_comment_info)),
115 .userspacesize = XT_ALIGN(sizeof(struct xt_comment_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000116 .help = comment_help,
117 .parse = comment_parse,
118 .final_check = comment_check,
119 .print = comment_print,
120 .save = comment_save,
121 .extra_opts = comment_opts,
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +0000122};
123
124void _init(void)
125{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000126 xtables_register_match(&comment_match);
127 xtables_register_match(&comment_match6);
Yasuyuki KOZAKAI9ea637d2007-07-24 07:21:17 +0000128}