Matthew G. Marsh | d9c66ba | 2000-12-18 06:22:44 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables for FTOS |
| 2 | * |
| 3 | * (C) 2000 by Matthew G. Marsh <mgm@paktronix.com> |
| 4 | * |
| 5 | * This program is distributed under the terms of GNU GPL v2, 1991 |
| 6 | * |
| 7 | * libipt_FTOS.c borrowed heavily from libipt_TOS.c 11/09/2000 |
| 8 | * |
| 9 | */ |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <getopt.h> |
| 14 | |
| 15 | #include <iptables.h> |
| 16 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 17 | #include <linux/netfilter_ipv4/ipt_FTOS.h> |
| 18 | |
| 19 | struct finfo { |
| 20 | struct ipt_entry_target t; |
| 21 | u_int8_t ftos; |
| 22 | }; |
| 23 | |
| 24 | static void init(struct ipt_entry_target *t, unsigned int *nfcache) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | static void help(void) |
| 29 | { |
| 30 | printf( |
| 31 | "FTOS target options\n" |
| 32 | " --set-ftos value Set TOS field in packet header to value\n" |
| 33 | " This value can be in decimal (ex: 32)\n" |
| 34 | " or in hex (ex: 0x20)\n" |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | static struct option opts[] = { |
| 39 | { "set-ftos", 1, 0, 'F' }, |
| 40 | { 0 } |
| 41 | }; |
| 42 | |
| 43 | static void |
| 44 | parse_ftos(const unsigned char *s, struct ipt_FTOS_info *finfo) |
| 45 | { |
| 46 | int ftos = string_to_number(s, 0, 255); |
| 47 | finfo->ftos = (u_int8_t )ftos; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | static int |
| 52 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 53 | const struct ipt_entry *entry, |
| 54 | struct ipt_entry_target **target) |
| 55 | { |
| 56 | struct ipt_FTOS_info *finfo |
| 57 | = (struct ipt_FTOS_info *)(*target)->data; |
| 58 | |
| 59 | switch (c) { |
| 60 | case 'F': |
| 61 | if (*flags) |
| 62 | exit_error(PARAMETER_PROBLEM, |
| 63 | "FTOS target: Only use --set-ftos ONCE!"); |
| 64 | parse_ftos(optarg, finfo); |
| 65 | *flags = 1; |
| 66 | break; |
| 67 | |
| 68 | default: |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | final_check(unsigned int flags) |
| 77 | { |
| 78 | if (!flags) |
| 79 | exit_error(PARAMETER_PROBLEM, |
| 80 | "FTOS target: Parameter --set-ftos is required"); |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | print_ftos(u_int8_t ftos, int numeric) |
| 85 | { |
| 86 | printf("0x%02x ", ftos); |
| 87 | } |
| 88 | |
| 89 | /* Prints out the targinfo. */ |
| 90 | static void |
| 91 | print(const struct ipt_ip *ip, |
| 92 | const struct ipt_entry_target *target, |
| 93 | int numeric) |
| 94 | { |
| 95 | const struct ipt_FTOS_info *finfo = |
| 96 | (const struct ipt_FTOS_info *)target->data; |
| 97 | printf("TOS set "); |
| 98 | print_ftos(finfo->ftos, numeric); |
| 99 | } |
| 100 | |
| 101 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 102 | static void |
| 103 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 104 | { |
| 105 | const struct ipt_FTOS_info *finfo = |
| 106 | (const struct ipt_FTOS_info *)target->data; |
| 107 | |
| 108 | printf("--set-ftos 0x%02x ", finfo->ftos); |
| 109 | } |
| 110 | |
| 111 | struct iptables_target ftos |
| 112 | = { NULL, |
| 113 | "FTOS", |
| 114 | NETFILTER_VERSION, |
| 115 | IPT_ALIGN(sizeof(struct ipt_FTOS_info)), |
| 116 | IPT_ALIGN(sizeof(struct ipt_FTOS_info)), |
| 117 | &help, |
| 118 | &init, |
| 119 | &parse, |
| 120 | &final_check, |
| 121 | &print, |
| 122 | &save, |
| 123 | opts |
| 124 | }; |
| 125 | |
| 126 | void _init(void) |
| 127 | { |
| 128 | register_target(&ftos); |
| 129 | } |