Aggie Student Toolkit — Load Balancer
CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Toggle main menu visibility
Loading...
Searching...
No Matches
Firewall.cpp
Go to the documentation of this file.
1
6
7
#include "
Firewall.h
"
8
9
#include <cctype>
10
11
bool
Firewall::parseIpv4
(
const
std::string& text, std::uint32_t& value) {
12
std::uint32_t result = 0;
13
int
octets = 0;
14
std::size_t position = 0;
15
while
(octets < 4) {
16
if
(position >= text.size() || !std::isdigit(
static_cast<
unsigned
char
>
(text[position]))) {
17
return
false
;
18
}
19
unsigned
int
octet = 0;
20
int
digits = 0;
21
while
(position < text.size() && std::isdigit(
static_cast<
unsigned
char
>
(text[position]))) {
22
octet = octet * 10 +
static_cast<
unsigned
int
>
(text[position] -
'0'
);
23
++position;
24
if
(++digits > 3 || octet > 255) {
25
return
false
;
26
}
27
}
28
result = (result << 8) | octet;
29
++octets;
30
if
(octets < 4) {
31
if
(position >= text.size() || text[position] !=
'.'
) {
32
return
false
;
33
}
34
++position;
35
}
36
}
37
if
(position != text.size()) {
38
return
false
;
39
}
40
value = result;
41
return
true
;
42
}
43
44
bool
Firewall::addRange
(
const
std::string& cidr) {
45
std::string address = cidr;
46
unsigned
int
prefix = 32;
47
const
std::size_t slash = cidr.find(
'/'
);
48
if
(slash != std::string::npos) {
49
address = cidr.substr(0, slash);
50
const
std::string prefixText = cidr.substr(slash + 1);
51
if
(prefixText.empty() || prefixText.size() > 2) {
52
return
false
;
53
}
54
prefix = 0;
55
for
(
char
c : prefixText) {
56
if
(!std::isdigit(
static_cast<
unsigned
char
>
(c))) {
57
return
false
;
58
}
59
prefix = prefix * 10 +
static_cast<
unsigned
int
>
(c -
'0'
);
60
}
61
if
(prefix > 32) {
62
return
false
;
63
}
64
}
65
std::uint32_t network = 0;
66
if
(!
parseIpv4
(address, network)) {
67
return
false
;
68
}
69
const
std::uint32_t mask = prefix == 0 ? 0u : 0xFFFFFFFFu << (32 - prefix);
70
ranges_.push_back(Range{network & mask, mask, cidr});
71
return
true
;
72
}
73
74
bool
Firewall::isBlocked
(
const
std::string& ip)
const
{
75
std::uint32_t value = 0;
76
if
(!
parseIpv4
(ip, value)) {
77
return
true
;
78
}
79
for
(
const
Range& range : ranges_) {
80
if
((value & range.mask) == range.network) {
81
return
true
;
82
}
83
}
84
return
false
;
85
}
86
87
std::vector<std::string>
Firewall::ranges
()
const
{
88
std::vector<std::string> result;
89
result.reserve(ranges_.size());
90
for
(
const
Range& range : ranges_) {
91
result.push_back(range.text);
92
}
93
return
result;
94
}
Firewall.h
Declares the Firewall class, an IPv4 range blocker used before requests are queued.
Firewall::addRange
bool addRange(const std::string &cidr)
Adds a blocked range.
Definition
Firewall.cpp:44
Firewall::isBlocked
bool isBlocked(const std::string &ip) const
Checks whether an address is blocked.
Definition
Firewall.cpp:74
Firewall::parseIpv4
static bool parseIpv4(const std::string &text, std::uint32_t &value)
Parses a dotted-quad IPv4 address.
Definition
Firewall.cpp:11
Firewall::ranges
std::vector< std::string > ranges() const
Definition
Firewall.cpp:87
Firewall.cpp
Generated by
1.18.0