Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 1 | /* 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 Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 18 | static void comment_help(void) |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 19 | { |
| 20 | printf( |
| 21 | "COMMENT match options:\n" |
| 22 | "--comment COMMENT Attach a comment to a rule\n\n" |
| 23 | ); |
| 24 | } |
| 25 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 26 | static const struct option comment_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 27 | { "comment", 1, NULL, '1' }, |
| 28 | { } |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static void |
| 32 | parse_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 */ |
| 45 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 46 | comment_parse(int c, char **argv, int invert, unsigned int *flags, |
| 47 | const void *entry, struct xt_entry_match **match) |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 48 | { |
| 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 Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 69 | static void comment_check(unsigned int flags) |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 70 | { |
| 71 | if (!flags) |
| 72 | exit_error(PARAMETER_PROBLEM, |
| 73 | "COMMENT match: You must specify `--comment'"); |
| 74 | } |
| 75 | |
| 76 | /* Prints out the matchinfo. */ |
| 77 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 78 | comment_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 79 | { |
| 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. */ |
| 87 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 88 | comment_save(const void *ip, const struct xt_entry_match *match) |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 89 | { |
| 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 Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 96 | static struct xtables_match comment_match = { |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 97 | .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 Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 102 | .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 KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 110 | static struct xtables_match comment_match6 = { |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 111 | .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 Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 116 | .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 KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | void _init(void) |
| 125 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 126 | xtables_register_match(&comment_match); |
| 127 | xtables_register_match(&comment_match6); |
Yasuyuki KOZAKAI | 9ea637d | 2007-07-24 07:21:17 +0000 | [diff] [blame] | 128 | } |