Bart De Schuymer | a501b78 | 2002-07-20 16:15:39 +0000 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <string.h> |
| 5 | #include <getopt.h> |
| 6 | #include "../include/ebtables_u.h" |
| 7 | #include <linux/netfilter_bridge/ebt_mark_m.h> |
| 8 | |
| 9 | #define MARK '1' |
| 10 | |
| 11 | static struct option opts[] = |
| 12 | { |
| 13 | { "mark" , required_argument, 0, MARK }, |
| 14 | { 0 } |
| 15 | }; |
| 16 | |
| 17 | static void print_help() |
| 18 | { |
| 19 | printf( |
| 20 | "mark option:\n" |
| 21 | "--mark [!] value[/mask]: Match nfmask value with optional mask\n"); |
| 22 | } |
| 23 | |
| 24 | static void init(struct ebt_entry_match *match) |
| 25 | { |
| 26 | struct ebt_mark_m_info *markinfo = (struct ebt_mark_m_info *)match->data; |
| 27 | |
| 28 | markinfo->mark = 0; |
| 29 | markinfo->mask = 0; |
| 30 | markinfo->invert = 0; |
| 31 | } |
| 32 | |
| 33 | #define OPT_MARK 0x01 |
| 34 | static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, |
| 35 | unsigned int *flags, struct ebt_entry_match **match) |
| 36 | { |
| 37 | struct ebt_mark_m_info *markinfo = (struct ebt_mark_m_info *) |
| 38 | (*match)->data; |
| 39 | char *end; |
| 40 | |
| 41 | switch (c) { |
| 42 | case MARK: |
| 43 | check_option(flags, MARK); |
| 44 | if (check_inverse(optarg)) |
| 45 | markinfo->invert = 1; |
| 46 | if (optind > argc) |
| 47 | print_error("No mark specified"); |
| 48 | markinfo->mark = strtoul(argv[optind - 1], &end, 0); |
| 49 | if (*end == '/') |
| 50 | markinfo->mask = strtoul(end+1, &end, 0); |
| 51 | else |
| 52 | markinfo->mask = 0xffffffff; |
| 53 | if ( *end != '\0' || end == argv[optind - 1]) |
| 54 | print_error("Bad mark value '%s'", argv[optind - 1]); |
| 55 | break; |
| 56 | default: |
| 57 | return 0; |
| 58 | } |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | static void final_check(const struct ebt_u_entry *entry, |
| 63 | const struct ebt_entry_match *match, const char *name, |
| 64 | unsigned int hook_mask, unsigned int time) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | static void print(const struct ebt_u_entry *entry, |
| 69 | const struct ebt_entry_match *match) |
| 70 | { |
| 71 | struct ebt_mark_m_info *markinfo = |
| 72 | (struct ebt_mark_m_info *)match->data; |
| 73 | |
| 74 | printf("--mark "); |
| 75 | if (markinfo->invert) |
| 76 | printf("! "); |
| 77 | if(markinfo->mask != 0xffffffff) |
| 78 | printf("0x%lx/0x%lx ", markinfo->mark, markinfo->mask); |
| 79 | else |
| 80 | printf("0x%lx ", markinfo->mark); |
| 81 | } |
| 82 | |
| 83 | static int compare(const struct ebt_entry_match *m1, |
| 84 | const struct ebt_entry_match *m2) |
| 85 | { |
| 86 | struct ebt_mark_m_info *markinfo1 = (struct ebt_mark_m_info *)m1->data; |
| 87 | struct ebt_mark_m_info *markinfo2 = (struct ebt_mark_m_info *)m2->data; |
| 88 | |
| 89 | if (markinfo1->invert != markinfo2->invert) |
| 90 | return 0; |
| 91 | if (markinfo1->mark != markinfo2->mark) |
| 92 | return 0; |
| 93 | if (markinfo1->mask != markinfo2->mask) |
| 94 | return 0; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | static struct ebt_u_match mark_match = |
| 99 | { |
| 100 | EBT_MARK_MATCH, |
| 101 | sizeof(struct ebt_mark_m_info), |
| 102 | print_help, |
| 103 | init, |
| 104 | parse, |
| 105 | final_check, |
| 106 | print, |
| 107 | compare, |
| 108 | opts, |
| 109 | }; |
| 110 | |
| 111 | static void _init(void) __attribute((constructor)); |
| 112 | static void _init(void) |
| 113 | { |
| 114 | register_match(&mark_match); |
| 115 | } |