Skip to content

Commit 2089cb5

Browse files
committed
WIF - Classifiers - Introduce IP prefix classifier
TG-36
1 parent 64b30ce commit 2089cb5

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @file
3+
* @author Richard Plny <[email protected]>
4+
* @brief IP prefix classifier interface
5+
*
6+
* SPDX-License-Identifier: BSD-3-Clause
7+
*/
8+
9+
#pragma once
10+
11+
#include "wif/classifiers/classifier.hpp"
12+
#include "wif/utils/ipPrefix.hpp"
13+
14+
#include <algorithm>
15+
#include <memory>
16+
#include <vector>
17+
18+
namespace WIF {
19+
20+
/**
21+
* @brief Classifier performing blocklist detection
22+
*/
23+
class IpPrefixClassifier : public Classifier {
24+
public:
25+
/**
26+
* @brief Construct an IP Prefix Classifier object with empty blocklist
27+
*/
28+
IpPrefixClassifier() = default;
29+
30+
/**
31+
* @brief Construct a new IP Prefix Classifier object
32+
* @param blocklist std::vector of blocklisted IP prefixes
33+
*/
34+
IpPrefixClassifier(const std::vector<IpPrefix>& blocklist);
35+
36+
/**
37+
* @brief Getter for current IP prefix blocklist
38+
* @return const std::vector<IpPrefix>& current blocklist
39+
*/
40+
const std::vector<IpPrefix>& getBlocklist() const noexcept { return m_blocklist; }
41+
42+
/**
43+
* @brief Update blocklist
44+
* Old blocklist is destructed and replaced by new one
45+
* @param blocklist std::vector of new IP prefixes on blocklist
46+
*/
47+
void updateBlocklist(const std::vector<IpPrefix>& blocklist);
48+
49+
/**
50+
* @brief Classify single flowFeature object
51+
* ClfResult contains non-zero double value if flowFeatures contained a field with IP from
52+
* blocklist, otherwise the double value is set to zero
53+
*
54+
* @param flowFeatures flow features to classify
55+
* @return ClfResult result of the classification
56+
*/
57+
ClfResult classify(const FlowFeatures& flowFeatures) override;
58+
59+
/**
60+
* @brief Classify a burst of flow features
61+
*
62+
* @param burstOfFlowsFeatures the burst of flow features to classify
63+
* @return std::vector<ClfResult> std::vector<ClfResult> the results of the classification
64+
*/
65+
std::vector<ClfResult> classify(const std::vector<FlowFeatures>& burstOfFlowsFeatures) override;
66+
67+
private:
68+
bool findIpAddress(const IpAddress& ipAddress);
69+
70+
std::vector<IpPrefix> m_blocklist;
71+
};
72+
73+
} // namespace WIF

src/wif/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(LIBWIF_SOURCES
22
classifiers/classifier.cpp
3+
classifiers/ipPrefixClassifier.cpp
34
classifiers/regexClassifier.cpp
45
classifiers/scikitMlClassifier.cpp
56
combinators/averageCombinator.cpp
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @file
3+
* @author Richard Plny <[email protected]>
4+
* @brief IP prefix classifier implementation
5+
*
6+
* SPDX-License-Identifier: BSD-3-Clause
7+
*/
8+
9+
#include "wif/classifiers/ipPrefixClassifier.hpp"
10+
11+
namespace WIF {
12+
13+
IpPrefixClassifier::IpPrefixClassifier(const std::vector<IpPrefix>& blocklist)
14+
{
15+
updateBlocklist(blocklist);
16+
}
17+
18+
void IpPrefixClassifier::updateBlocklist(const std::vector<IpPrefix>& blocklist)
19+
{
20+
m_blocklist = blocklist;
21+
std::sort(m_blocklist.begin(), m_blocklist.end());
22+
}
23+
24+
ClfResult IpPrefixClassifier::classify(const FlowFeatures& flowFeatures)
25+
{
26+
for (FeatureID featureID : getSourceFeatureIDs()) {
27+
auto ipAddress = flowFeatures.get<IpAddress>(featureID);
28+
if (findIpAddress(ipAddress)) {
29+
return ClfResult(1.0);
30+
}
31+
}
32+
return ClfResult(0.0);
33+
}
34+
35+
std::vector<ClfResult>
36+
IpPrefixClassifier::classify(const std::vector<FlowFeatures>& burstOfFlowsFeatures)
37+
{
38+
std::vector<ClfResult> burstResults;
39+
burstResults.reserve(burstOfFlowsFeatures.size());
40+
for (const auto& flowFeatures : burstOfFlowsFeatures) {
41+
burstResults.emplace_back(classify(flowFeatures));
42+
}
43+
return burstResults;
44+
}
45+
46+
bool IpPrefixClassifier::findIpAddress(const IpAddress& ipAddress)
47+
{
48+
return std::binary_search(m_blocklist.begin(), m_blocklist.end(), ipAddress);
49+
}
50+
51+
} // namespace WIF

0 commit comments

Comments
 (0)