forked from pantyusha/nesca
-
Notifications
You must be signed in to change notification settings - Fork 7
/
IPRandomizer.cpp
53 lines (41 loc) · 1.33 KB
/
IPRandomizer.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
#include "IPRandomizer.h"
IPRandomizer::IPRandomizer(std::vector<IPRangeHolder> ipRangeVec, int shuffleGap)
{
this->ipRangeVec = ipRangeVec;
this->shuffleGap = shuffleGap;
for (int i = 0; i < ipRangeVec.size(); ++i) {
this->shuffleOffset.push_back(0);
}
}
IPRandomizer::~IPRandomizer()
{
this->ipRangeVec.clear();
this->shuffleOffset.clear();
}
void IPRandomizer::shuffleRange() {
for (int i = 0; i < this->ipRangeVec.size(); ++i) {
IPRangeHolder ipRangeHolder = this->ipRangeVec[i];
if (ipRangeHolder.ip1 + this->shuffleOffset[i] >= ipRangeHolder.ip2) {
continue;
}
unsigned int rangeSize = ipRangeHolder.ip2 - (ipRangeHolder.ip1 + this->shuffleOffset[i] - 1);
int offset = (rangeSize < this->shuffleGap ? rangeSize : this->shuffleGap);
for (unsigned int j = this->shuffleOffset[i]; j < this->shuffleOffset[i] + offset; ++j) {
this->shuffledRange.push_back(ipRangeHolder.ip1 + j);
}
this->shuffleOffset[i] += offset;
}
std::random_shuffle(this->shuffledRange.begin(), this->shuffledRange.end());
}
unsigned int IPRandomizer::getNext() {
if (this->shuffledRange.empty()) {
shuffleRange();
//If still empty then ip-range chunk is depleted.
if (this->shuffledRange.empty()) {
return 0;
};
};
unsigned int ip = this->shuffledRange[0];
this->shuffledRange.erase(this->shuffledRange.begin());
return ip;
}