Emmanuel Roger | 3071913 | 2000-10-04 15:19:31 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add string matching support. |
| 2 | * |
| 3 | * Copyright (C) 2000 Emmanuel Roger <winfield@freegates.be> |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <netdb.h> |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <getopt.h> |
| 10 | |
| 11 | #include <iptables.h> |
| 12 | #include <linux/netfilter_ipv4/ipt_string.h> |
| 13 | |
| 14 | /* Function which prints out usage message. */ |
| 15 | static void |
| 16 | help(void) |
| 17 | { |
| 18 | printf( |
| 19 | "STRING match v%s options:\n" |
| 20 | "--string [!] string Match a string in a packet\n", |
| 21 | NETFILTER_VERSION); |
| 22 | |
| 23 | fputc('\n', stdout); |
| 24 | } |
| 25 | |
| 26 | static struct option opts[] = { |
| 27 | { "string", 1, 0, '1' }, |
| 28 | {0} |
| 29 | }; |
| 30 | |
| 31 | /* Initialize the match. */ |
| 32 | static void |
| 33 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 34 | { |
| 35 | *nfcache |= NFC_UNKNOWN; |
| 36 | } |
| 37 | |
| 38 | static void |
| 39 | parse_string(const unsigned char *s, struct ipt_string_info *info) |
| 40 | { |
| 41 | if (strlen(s) <= 255) strcpy(info->string, s); |
| 42 | else exit_error(PARAMETER_PROBLEM, "STRING too long `%s'", s); |
| 43 | } |
| 44 | |
| 45 | /* Function which parses command options; returns true if it |
| 46 | ate an option */ |
| 47 | static int |
| 48 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 49 | const struct ipt_entry *entry, |
| 50 | unsigned int *nfcache, |
| 51 | struct ipt_entry_match **match) |
| 52 | { |
| 53 | struct ipt_string_info *stringinfo = (struct ipt_string_info *)(*match)->data; |
| 54 | |
| 55 | switch (c) { |
| 56 | case '1': |
| 57 | if (check_inverse(optarg, &invert)) |
| 58 | optind++; |
| 59 | parse_string(argv[optind-1], stringinfo); |
| 60 | if (invert) |
| 61 | stringinfo->invert = 1; |
| 62 | *flags = 1; |
| 63 | break; |
| 64 | |
| 65 | default: |
| 66 | return 0; |
| 67 | } |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | print_string(char string[], int invert, int numeric) |
| 73 | { |
| 74 | |
| 75 | if (invert) |
| 76 | fputc('!', stdout); |
| 77 | printf("%s ",string); |
| 78 | } |
| 79 | |
| 80 | /* Final check; must have specified --string. */ |
| 81 | static void |
| 82 | final_check(unsigned int flags) |
| 83 | { |
| 84 | if (!flags) |
| 85 | exit_error(PARAMETER_PROBLEM, |
| 86 | "STRING match: You must specify `--string'"); |
| 87 | } |
| 88 | |
| 89 | /* Prints out the matchinfo. */ |
| 90 | static void |
| 91 | print(const struct ipt_ip *ip, |
| 92 | const struct ipt_entry_match *match, |
| 93 | int numeric) |
| 94 | { |
| 95 | printf("STRING match "); |
| 96 | print_string(((struct ipt_string_info *)match->data)->string, |
| 97 | ((struct ipt_string_info *)match->data)->invert, numeric); |
| 98 | } |
| 99 | |
| 100 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 101 | static void |
| 102 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 103 | { |
| 104 | printf("--tos "); |
| 105 | print_string(((struct ipt_string_info *)match->data)->string, |
| 106 | ((struct ipt_string_info *)match->data)->invert, 0); |
| 107 | } |
| 108 | |
| 109 | struct iptables_match string |
| 110 | = { NULL, |
| 111 | "string", |
| 112 | NETFILTER_VERSION, |
| 113 | IPT_ALIGN(sizeof(struct ipt_string_info)), |
| 114 | IPT_ALIGN(sizeof(struct ipt_string_info)), |
| 115 | &help, |
| 116 | &init, |
| 117 | &parse, |
| 118 | &final_check, |
| 119 | &print, |
| 120 | &save, |
| 121 | opts |
| 122 | }; |
| 123 | |
| 124 | void _init(void) |
| 125 | { |
| 126 | register_match(&string); |
| 127 | } |