-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPortAnonymizer.cpp
73 lines (58 loc) · 1.52 KB
/
PortAnonymizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "PortAnonymizer.hpp"
#include <string.h>
#include <errno.h>
#include <cstdio>
#include <cstdlib>
#include <inttypes.h>
#include <algorithm>
#include <arpa/inet.h>
using namespace std;
using namespace capsan;
static const int RANDOM_MAX = 2147483647;
/**
* @return an int in range [0, upper], inclusive.
*/
static long random_int(long upper)
{
long r;
++upper;
long last_valid_random_val = RANDOM_MAX - (RANDOM_MAX % upper) - 1;
// Remove modulo bias.
do { r = random(); } while ( r > last_valid_random_val );
return r % upper;
}
PortAnonymizer::PortAnonymizer(unsigned seed)
{
char state[256];
char* prev_state = initstate(seed, state, sizeof(state));
for ( uint16_t i = 0; i < NUM_PORTS; ++i )
port_map[i] = i;
// Sattolo's shuffling algorithm.
for ( uint16_t i = NUM_PORTS - 1; i > 0; --i )
swap(port_map[i], port_map[random_int(i - 1)]);
for ( uint16_t i = 0; i < NUM_PORTS; ++i )
reverse_map[port_map[i]] = i;
setstate(prev_state);
}
uint16_t PortAnonymizer::Anonymize(uint16_t port) const
{
return htons(port_map[ntohs(port)]);
}
uint16_t PortAnonymizer::DeAnonymize(uint16_t port) const
{
return htons(reverse_map[ntohs(port)]);
}
void PortAnonymizer::WriteMappings(const char* filename) const
{
FILE* f = fopen(filename, "w");
if ( ! f )
{
char buf[128];
strerror_r(errno, buf, sizeof(buf));
fprintf(stderr, "Failed to open %s: %s\n", filename, buf);
exit(1);
}
for ( uint16_t i = 0; i < NUM_PORTS; ++i )
fprintf(f, "%" PRIu16 ": %" PRIu16 "\n", i, reverse_map[i]);
fclose(f);
}