mirror of
https://github.com/ChronosX88/nesca.git
synced 2024-12-25 01:01:45 +00:00
wqe
This commit is contained in:
parent
460994915c
commit
0232fe20c7
53
IPRandomizer.cpp
Normal file
53
IPRandomizer.cpp
Normal file
@ -0,0 +1,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;
|
||||
}
|
31
IPRandomizer.h
Normal file
31
IPRandomizer.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef RAND_H
|
||||
#define RAND_H
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
struct IPRangeHolder {
|
||||
unsigned int ip1;
|
||||
unsigned int ip2;
|
||||
};
|
||||
|
||||
class IPRandomizer
|
||||
{
|
||||
private:
|
||||
std::vector<IPRangeHolder> ipRangeVec;
|
||||
std::vector<unsigned int> shuffledRange;
|
||||
std::vector<unsigned int> shuffleOffset;
|
||||
int shuffleGap = 20000;
|
||||
private:
|
||||
void shuffleRange();
|
||||
public:
|
||||
IPRandomizer(std::vector<IPRangeHolder> ipRangeVec, int shuffleGap = 20000);
|
||||
IPRandomizer(std::vector<IPRangeHolder> ipRangeVec);
|
||||
~IPRandomizer();
|
||||
|
||||
unsigned int getNext();
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user