~ubuntu-branches/ubuntu/jaunty/xtables-addons/jaunty

« back to all changes in this revision

Viewing changes to extensions/libxt_TEE.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2008-10-27 22:31:20 UTC
  • Revision ID: james.westby@ubuntu.com-20081027223120-njqjsraqjpp7s98t
Tags: upstream-1.5.7
Import upstream version 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      "TEE" target extension for iptables
 
3
 *      Copyright © Sebastian Claßen <sebastian.classen [at] freenet.ag>, 2007
 
4
 *      Jan Engelhardt <jengelh [at] medozas de>, 2007 - 2008
 
5
 *
 
6
 *      This program is free software; you can redistribute it and/or
 
7
 *      modify it under the terms of the GNU General Public License; either
 
8
 *      version 2 of the License, or any later version, as published by the
 
9
 *      Free Software Foundation.
 
10
 */
 
11
#include <sys/socket.h>
 
12
#include <getopt.h>
 
13
#include <stdbool.h>
 
14
#include <stdio.h>
 
15
#include <stdlib.h>
 
16
#include <string.h>
 
17
 
 
18
#include <arpa/inet.h>
 
19
#include <net/if.h>
 
20
#include <netinet/in.h>
 
21
 
 
22
#include <xtables.h>
 
23
#include <linux/netfilter.h>
 
24
#include <linux/netfilter/x_tables.h>
 
25
#include "xt_TEE.h"
 
26
 
 
27
enum {
 
28
        FLAG_GATEWAY = 1 << 0,
 
29
};
 
30
 
 
31
static const struct option tee_tg_opts[] = {
 
32
        {.name = "gateway", .has_arg = true, .val = 'g'},
 
33
        {},
 
34
};
 
35
 
 
36
static void tee_tg_help(void)
 
37
{
 
38
        printf(
 
39
"TEE target options:\n"
 
40
"  --gateway IPADDR    Route packet via the gateway given by address\n"
 
41
"\n");
 
42
}
 
43
 
 
44
static int tee_tg_parse(int c, char **argv, int invert, unsigned int *flags,
 
45
                        const void *entry, struct xt_entry_target **target)
 
46
{
 
47
        struct xt_tee_tginfo *info = (void *)(*target)->data;
 
48
        const struct in_addr *ia;
 
49
 
 
50
        switch (c) {
 
51
        case 'g':
 
52
                if (*flags & FLAG_GATEWAY)
 
53
                        exit_error(PARAMETER_PROBLEM,
 
54
                                   "Cannot specify --gw more than once");
 
55
 
 
56
                if (check_inverse(optarg, &invert, NULL, 0))
 
57
                        exit_error(PARAMETER_PROBLEM,
 
58
                                   "Unexpected \"!\" after --gateway");
 
59
 
 
60
                ia = numeric_to_ipaddr(optarg);
 
61
                if (ia == NULL)
 
62
                        exit_error(PARAMETER_PROBLEM,
 
63
                                   "Invalid IP address %s", optarg);
 
64
 
 
65
                memcpy(&info->gw, ia, sizeof(*ia));
 
66
                *flags |= FLAG_GATEWAY;
 
67
                return true;
 
68
        }
 
69
 
 
70
        return false;
 
71
}
 
72
 
 
73
static void tee_tg_check(unsigned int flags)
 
74
{
 
75
        if (flags == 0)
 
76
                exit_error(PARAMETER_PROBLEM, "TEE target: "
 
77
                           "--gateway parameter required");
 
78
}
 
79
 
 
80
static void tee_tg_print(const void *ip, const struct xt_entry_target *target,
 
81
                         int numeric)
 
82
{
 
83
        const struct xt_tee_tginfo *info = (const void *)target->data;
 
84
 
 
85
        if (numeric)
 
86
                printf("TEE gw:%s ", ipaddr_to_anyname(&info->gw.in));
 
87
        else
 
88
                printf("TEE gw:%s ", ipaddr_to_numeric(&info->gw.in));
 
89
}
 
90
 
 
91
static void tee_tg_save(const void *ip, const struct xt_entry_target *target)
 
92
{
 
93
        const struct xt_tee_tginfo *info = (const void *)target->data;
 
94
 
 
95
        printf("--gateway %s ", ipaddr_to_numeric(&info->gw.in));
 
96
}
 
97
 
 
98
static struct xtables_target tee_tg_reg = {
 
99
        .name          = "TEE",
 
100
        .version       = XTABLES_VERSION,
 
101
        .size          = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
 
102
        .userspacesize = XT_ALIGN(sizeof(struct xt_tee_tginfo)),
 
103
        .help          = tee_tg_help,
 
104
        .parse         = tee_tg_parse,
 
105
        .final_check   = tee_tg_check,
 
106
        .print         = tee_tg_print,
 
107
        .save          = tee_tg_save,
 
108
        .extra_opts    = tee_tg_opts,
 
109
};
 
110
 
 
111
static void _init(void)
 
112
{
 
113
        xtables_register_target(&tee_tg_reg);
 
114
}