Skip to content
API Documentation
BastionGuard™
BlacklistIpExtractor.cpp File Reference

Go to the source code of this file.

Functions

static bool is_valid_ipv4 (const std::string &s)
static bool is_valid_ipv4_cidr (const std::string &s)
static bool is_valid_ipv6 (const std::string &s)
static bool extract_host_from_url (const std::string &line, std::string &host)

Function Documentation

◆ extract_host_from_url()

bool extract_host_from_url ( const std::string & line,
std::string & host )
static

Definition at line 71 of file BlacklistIpExtractor.cpp.

71 {
72 static const std::regex url_re(
73 R"(^(?:https?|ftp)://([^/:?#]+))",
74 std::regex::icase
75 );
76
77 std::smatch m;
78 if (!std::regex_search(line, m, url_re))
79 return false;
80
81 host = m[1].str();
82 return true;
83}

References extract_host_from_url().

Referenced by action_check_url(), extract_host_from_url(), extract_host_from_url(), and BlacklistIpExtractor::extractAndWrite().

◆ is_valid_ipv4()

bool is_valid_ipv4 ( const std::string & s)
static

Definition at line 31 of file BlacklistIpExtractor.cpp.

31 {
32 int parts = 0, acc = 0;
33 bool has_digit = false;
34
35 for (char c : s) {
36 if (c >= '0' && c <= '9') {
37 has_digit = true;
38 acc = acc * 10 + (c - '0');
39 if (acc > 255) return false;
40 } else if (c == '.') {
41 if (!has_digit) return false;
42 parts++;
43 acc = 0;
44 has_digit = false;
45 } else return false;
46 }
47
48 return has_digit && parts == 3;
49}

References is_valid_ipv4().

Referenced by BlacklistIpExtractor::extractAndWrite(), is_valid_ipv4(), and is_valid_ipv4_cidr().

◆ is_valid_ipv4_cidr()

bool is_valid_ipv4_cidr ( const std::string & s)
static

Definition at line 51 of file BlacklistIpExtractor.cpp.

51 {
52 auto pos = s.find('/');
53 if (pos == std::string::npos) return false;
54 if (!is_valid_ipv4(s.substr(0, pos))) return false;
55
56 try {
57 int m = std::stoi(s.substr(pos + 1));
58 return m >= 0 && m <= 32;
59 } catch (...) {
60 return false;
61 }
62}
static bool is_valid_ipv4(const std::string &s)

References is_valid_ipv4(), and is_valid_ipv4_cidr().

Referenced by BlacklistIpExtractor::extractAndWrite(), and is_valid_ipv4_cidr().

◆ is_valid_ipv6()

bool is_valid_ipv6 ( const std::string & s)
static

Definition at line 64 of file BlacklistIpExtractor.cpp.

64 {
65 static const std::regex ipv6(
66 R"(^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$)"
67 );
68 return std::regex_match(s, ipv6);
69}

References is_valid_ipv6().

Referenced by BlacklistIpExtractor::extractAndWrite(), and is_valid_ipv6().