Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame^] | 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 <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_helper.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "helper match v%s options:\n" |
| 17 | "[!] --helper value Match helper value\n" |
| 18 | "\n", |
| 19 | NETFILTER_VERSION); |
| 20 | } |
| 21 | |
| 22 | static struct option opts[] = { |
| 23 | { "helper", 1, 0, '1' }, |
| 24 | {0} |
| 25 | }; |
| 26 | |
| 27 | /* Initialize the match. */ |
| 28 | static void |
| 29 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 30 | { |
| 31 | /* Can't cache this. */ |
| 32 | *nfcache |= NFC_UNKNOWN; |
| 33 | } |
| 34 | |
| 35 | /* Function which parses command options; returns true if it |
| 36 | ate an option */ |
| 37 | static int |
| 38 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 39 | const struct ipt_entry *entry, |
| 40 | unsigned int *nfcache, |
| 41 | struct ipt_entry_match **match) |
| 42 | { |
| 43 | struct ipt_helper_info *info = (struct ipt_helper_info *)(*match)->data; |
| 44 | |
| 45 | switch (c) { |
| 46 | case '1': |
| 47 | if (check_inverse(optarg, &invert)) |
| 48 | optind++; |
| 49 | strncpy(info->name, optarg, 29); |
| 50 | if (invert) |
| 51 | info->invert = 1; |
| 52 | *flags = 1; |
| 53 | break; |
| 54 | |
| 55 | default: |
| 56 | return 0; |
| 57 | } |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | /* Final check; must have specified --helper. */ |
| 62 | static void |
| 63 | final_check(unsigned int flags) |
| 64 | { |
| 65 | if (!flags) |
| 66 | exit_error(PARAMETER_PROBLEM, |
| 67 | "helper match: You must specify `--helper'"); |
| 68 | } |
| 69 | |
| 70 | /* Prints out the info. */ |
| 71 | static void |
| 72 | print(const struct ipt_ip *ip, |
| 73 | const struct ipt_entry_match *match, |
| 74 | int numeric) |
| 75 | { |
| 76 | struct ipt_helper_info *info = (struct ipt_helper_info *)match->data; |
| 77 | |
| 78 | printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name); |
| 79 | } |
| 80 | |
| 81 | /* Saves the union ipt_info in parsable form to stdout. */ |
| 82 | static void |
| 83 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 84 | { |
| 85 | struct ipt_helper_info *info = (struct ipt_helper_info *)match->data; |
| 86 | |
| 87 | printf("%s--helper \"%s\" ",info->invert ? "! " : "", info->name); |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | struct iptables_match helper |
| 92 | = { NULL, |
| 93 | "helper", |
| 94 | NETFILTER_VERSION, |
| 95 | IPT_ALIGN(sizeof(struct ipt_helper_info)), |
| 96 | IPT_ALIGN(sizeof(struct ipt_helper_info)), |
| 97 | &help, |
| 98 | &init, |
| 99 | &parse, |
| 100 | &final_check, |
| 101 | &print, |
| 102 | &save, |
| 103 | opts |
| 104 | }; |
| 105 | |
| 106 | void _init(void) |
| 107 | { |
| 108 | register_match(&helper); |
| 109 | } |