Branch data Line data Source code
1 : : // $Id: Anon.h 416 2004-09-17 03:52:28Z vern $
2 : :
3 : : // The prefix-preserving IP address anonymization code is largely
4 : : // based on (and sometimes directly copied from) Eddie Kohler's
5 : : // ipsumdump-1.20 code, per:
6 : : //
7 : : // http://www.icir.org/kohler/ipsumdump/
8 : : //
9 : : // ipsumdump, in turn, takes some of its code from tcpdpriv:
10 : : //
11 : : // http://ita.ee.lbl.gov/html/contrib/tcpdpriv.html
12 : :
13 : : #ifndef anon_h
14 : : #define anon_h
15 : :
16 : : #include <vector>
17 : : #include <set>
18 : : #include <map>
19 : : using namespace std;
20 : :
21 : : #include "net_util.h"
22 : :
23 : : // TODO: Anon.h may not be the right place to put these functions ...
24 : :
25 : : enum ip_addr_anonymization_class_t {
26 : : ORIG_ADDR, // client address
27 : : RESP_ADDR, // server address
28 : : OTHER_ADDR,
29 : : NUM_ADDR_ANONYMIZATION_CLASSES,
30 : : };
31 : :
32 : : enum ip_addr_anonymization_method_t {
33 : : KEEP_ORIG_ADDR,
34 : : SEQUENTIALLY_NUMBERED,
35 : : RANDOM_MD5,
36 : : PREFIX_PRESERVING_A50,
37 : : PREFIX_PRESERVING_MD5,
38 : : NUM_ADDR_ANONYMIZATION_METHODS,
39 : : };
40 : :
41 : : typedef uint32 ipaddr32_t;
42 : :
43 : : // NOTE: all addresses in parameters of *public* functions are in
44 : : // network order.
45 : :
46 : 0 : class AnonymizeIPAddr {
47 : : public:
48 [ # # ][ # # ]: 0 : virtual ~AnonymizeIPAddr() { mapping.clear(); }
[ # # ]
49 : :
50 : : ipaddr32_t Anonymize(ipaddr32_t addr);
51 : :
52 : : // Keep the specified prefix unchanged.
53 : 0 : virtual int PreservePrefix(ipaddr32_t /* input */, int /* num_bits */)
54 : : {
55 : 0 : internal_error("prefix preserving is not supported for the anonymizer");
56 : : return 0;
57 : : }
58 : :
59 : : virtual ipaddr32_t anonymize(ipaddr32_t addr) = 0;
60 : :
61 : : int PreserveNet(ipaddr32_t input);
62 : :
63 : : protected:
64 : : map<ipaddr32_t, ipaddr32_t> mapping;
65 : : };
66 : :
67 [ # # ][ # # ]: 0 : class AnonymizeIPAddr_Seq : public AnonymizeIPAddr {
68 : : public:
69 : 0 : AnonymizeIPAddr_Seq() { seq = 1; }
70 : : ipaddr32_t anonymize(ipaddr32_t addr);
71 : :
72 : : protected:
73 : : ipaddr32_t seq;
74 : : };
75 : :
76 [ # # ][ # # ]: 0 : class AnonymizeIPAddr_RandomMD5 : public AnonymizeIPAddr {
77 : : public:
78 : : ipaddr32_t anonymize(ipaddr32_t addr);
79 : : };
80 : :
81 [ # # ][ # # ]: 0 : class AnonymizeIPAddr_PrefixMD5 : public AnonymizeIPAddr {
82 : : public:
83 : : ipaddr32_t anonymize(ipaddr32_t addr);
84 : :
85 : : protected:
86 : : struct anon_prefix {
87 : : int len;
88 : : ipaddr32_t prefix;
89 : : } prefix;
90 : : };
91 : :
92 : : class AnonymizeIPAddr_A50 : public AnonymizeIPAddr {
93 : : public:
94 : 0 : AnonymizeIPAddr_A50() { init(); }
95 : : ~AnonymizeIPAddr_A50();
96 : :
97 : : ipaddr32_t anonymize(ipaddr32_t addr);
98 : : int PreservePrefix(ipaddr32_t input, int num_bits);
99 : :
100 : : protected:
101 : : struct Node {
102 : : ipaddr32_t input;
103 : : ipaddr32_t output;
104 : : Node* child[2];
105 : : };
106 : :
107 : : int method;
108 : : int before_anonymization;
109 : : int new_mapping;
110 : :
111 : : // The root of prefix preserving mapping tree.
112 : : Node* root;
113 : :
114 : : // A node pool for new_node.
115 : : Node* next_free_node;
116 : : std::vector<Node*> blocks;
117 : :
118 : : // for 0.0.0.0 and 255.255.255.255.
119 : : Node special_nodes[2];
120 : :
121 : : void init();
122 : :
123 : : Node* new_node();
124 : : Node* new_node_block();
125 : : void free_node(Node*);
126 : :
127 : : ipaddr32_t make_output(ipaddr32_t, int) const;
128 : : Node* make_peer(ipaddr32_t, Node*);
129 : : Node* find_node(ipaddr32_t);
130 : : };
131 : :
132 : : // The global IP anonymizers.
133 : : extern AnonymizeIPAddr* ip_anonymizer[NUM_ADDR_ANONYMIZATION_METHODS];
134 : :
135 : : void init_ip_addr_anonymizers();
136 : : ipaddr32_t anonymize_ip(ipaddr32_t ip, enum ip_addr_anonymization_class_t cl);
137 : :
138 : : #define LOG_ANONYMIZATION_MAPPING
139 : : void log_anonymization_mapping(ipaddr32_t input, ipaddr32_t output);
140 : :
141 : : #endif
|