-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPValidator.cpp
187 lines (159 loc) · 5.1 KB
/
IPValidator.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "IPValidator.h"
#include "ThreadPool.h"
#include <sstream>
#include <exception>
#include <regex>
using namespace std;
const ui32 IPValidator::m_dataQueueMaxSize = 65'000;
const regex IPValidator::m_ipv4Validator("(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])");
const regex IPValidator::m_ipv6Validator("((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}");
//A common threadpool object
ThreadPool pool(10);
IPValidator::IPValidator()
: m_pIPDataQueue(new Queue())
, m_pUniqueIPV4Addresses(new UnorderedSet())
, m_pUniqueIPV6Addresses(new UnorderedSet())
{
promise<bool> p;
m_bConsumerThreadRunningStatus = p.get_future();
m_consumerThread = move(thread(&IPValidator::startValidation, this, move(p)));
}
IPValidator::~IPValidator()
{
pool.waitForTasks();
auto status = m_bConsumerThreadRunningStatus.wait_for(0ms);
while (status != future_status::ready)
{
this_thread::sleep_for(1ms);
continue;
}
if (m_consumerThread.joinable())
m_consumerThread.join();
}
/*static*/ bool IPValidator::isValidIPV4Address(const std::string& ip) noexcept
{
if (ip.empty())
return false;
return regex_match(ip, m_ipv4Validator);
}
/*static*/ bool IPValidator::isValidIPV6Address(const std::string& ip) noexcept
{
if (ip.empty())
return false;
return regex_match(ip, m_ipv6Validator);
}
/*static*/ ui32 IPValidator::getHashKey(const std::string& ip, const bool bIsIPV6)
{
if (ip.empty())
return 0;
auto localIp(ip);
ui32 hashKey = bIsIPV6 ? 0x0 : 0;
size_t endPos = 0;
auto separator = bIsIPV6 ? ':' : '.';
ui32 segmentIdx = bIsIPV6 ? 0x1 : 1;
try
{
while (endPos != string::npos && (bIsIPV6 ? (segmentIdx <= 0x7) : (segmentIdx <= 3)))
{
auto segment = localIp.substr(0, localIp.find_first_of(separator) - 1);
localIp = localIp.substr(endPos + 1);
endPos = localIp.find_first_of(separator);
istringstream is(segment);
ui32 val = 0;
if (bIsIPV6)
is >> hex >> val;
else
is >> val;
if ((bIsIPV6 ? (val < 0x0) : (val < 0)))
throw exception();
hashKey += (val / segmentIdx) + (val % segmentIdx);
++segmentIdx;
}
}
catch (...)
{
throw current_exception;
}
return hashKey;
}
bool IPValidator::isUniqueIPV4Address(const ui32& hashKey, const bool bIPV4) noexcept
{
if (!bIPV4 || hashKey == 0)
return false;
shared_lock readLock(m_MtxIPV4);
if (m_pUniqueIPV4Addresses->find(hashKey) == m_pUniqueIPV4Addresses->end())
return true;
return false;
}
bool IPValidator::isUniqueIPV6Address(const ui32& hashKey, const bool bIPV6) noexcept
{
if (!bIPV6 || hashKey == 0)
return false;
scoped_lock readLock(m_MtxIPV6);
if (m_pUniqueIPV6Addresses->find(hashKey) == m_pUniqueIPV6Addresses->end())
return true;
return false;
}
void IPValidator::pushData(const std::string& ip)
{
if (ip.empty())
return;
{
shared_lock readLock(m_dataQueueMtx);
while (m_pIPDataQueue->size() == m_dataQueueMaxSize)
continue;
}
unique_lock writeLock(m_dataQueueMtx);
m_pIPDataQueue->emplace(ip);
}
void IPValidator::processData()
{
unique_lock writeLock(m_dataQueueMtx);
if (!m_pIPDataQueue->empty())
{
auto ipAddr = m_pIPDataQueue->front();
m_pIPDataQueue->pop();
writeLock.unlock();
auto bIsValidIPV4 = isValidIPV4Address(ipAddr);
auto bIsValidIPV6 = isValidIPV6Address(ipAddr);
if (bIsValidIPV4)
{
auto hashKey = getHashKey(ipAddr, false);
auto bIsUnique = isUniqueIPV4Address(hashKey, true);
{
unique_lock writeLock(m_MtxIPV4);
if (bIsUnique)
m_pUniqueIPV4Addresses->emplace(hashKey);
else
m_pUniqueIPV4Addresses->erase(hashKey);
}
++m_totalIPV4AddrCnt;
}
else if (bIsValidIPV6)
{
auto hashKey = getHashKey(ipAddr, true);
auto bIsUnique = isUniqueIPV6Address(hashKey, true);
{
unique_lock writeLock(m_MtxIPV6);
if (bIsUnique)
m_pUniqueIPV6Addresses->emplace(hashKey);
else
m_pUniqueIPV6Addresses->erase(hashKey);
}
++m_totalIPV6AddrCnt;
}
else
{
++m_invalidIPAddrCnt;
}
}
}
void IPValidator::startValidation(std::promise<bool> bFinished)
{
while (!m_bIsReadingDataDone.load())
pool.submit([this] { processData(); });
while (!m_pIPDataQueue->empty())
pool.submit([this] { processData(); });
pool.waitForTasks();
m_bIsProcessingDataDone = true;
}