Module: check_mk
Branch: master
Commit: 8866d7a8b68b1a38cb00077b9f77f76ea1d76cbb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8866d7a8b68b1a…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Fri Jan 12 10:34:06 2018 +0100
Windows agent: don't put ipspec pointers in vector
Change-Id: I299a44b57e0fdaab3434d729936f120fc59a29fb
---
agents/windows/ListenSocket.cc | 13 +++++----
agents/windows/build_version | 2 +-
agents/windows/check_mk_agent.cc | 30 ++++++++++---------
agents/windows/sections/SectionCheckMK.cc | 13 ++-------
agents/windows/types.cc | 48 ++++++++++++++++++++-----------
agents/windows/types.h | 10 ++++---
6 files changed, 63 insertions(+), 53 deletions(-)
diff --git a/agents/windows/ListenSocket.cc b/agents/windows/ListenSocket.cc
index 43a810b..0ff2678 100644
--- a/agents/windows/ListenSocket.cc
+++ b/agents/windows/ListenSocket.cc
@@ -68,9 +68,9 @@ bool ListenSocket::check_only_from(sockaddr *ip) {
for (only_from_t::const_iterator it_from = _source_whitelist.begin();
it_from != _source_whitelist.end(); ++it_from) {
- ipspec *only_from = *it_from;
+ const ipspec &only_from = *it_from;
- if (only_from->ipv6 != (ip->sa_family == AF_INET6)) {
+ if (only_from.ipv6 != (ip->sa_family == AF_INET6)) {
// test ipv6 address only against ipv6 filter and ipv4 address
// against ipv4 filter.
// the only_from list already contains v4->v6 converted addresses
@@ -82,8 +82,8 @@ bool ListenSocket::check_only_from(sockaddr *ip) {
sockaddr_in6 *addrv6 = (sockaddr_in6 *)ip;
for (int i = 0; i < 8 && match; ++i) {
match =
- only_from->ip.v6.address[i] ==
- (addrv6->sin6_addr.u.Word[i] &
only_from->ip.v6.netmask[i]);
+ only_from.ip.v6.address[i] ==
+ (addrv6->sin6_addr.u.Word[i] & only_from.ip.v6.netmask[i]);
}
if (match) {
return true;
@@ -91,8 +91,9 @@ bool ListenSocket::check_only_from(sockaddr *ip) {
} else {
uint32_t significant_bits =
((sockaddr_in *)ip)->sin_addr.S_un.S_addr &
- only_from->ip.v4.netmask;
- if (significant_bits == only_from->ip.v4.address) {
+ only_from.ip.v4.netmask;
+
+ if (significant_bits == only_from.ip.v4.address) {
return true;
}
}
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 31410c0..e92e7e9 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3064
+3066
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index 4977b5d..7c65e75 100644
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -928,27 +928,29 @@ void do_unpack_plugins(const char *plugin_filename, const
Environment &env) {
void postProcessOnlyFrom() {
if (*s_config->support_ipv6) {
// find all ipv4 specs, later insert a the same spec as a v6 adress.
- std::vector<ipspec *> v4specs;
- for (ipspec *spec : *s_config->only_from) {
- if (!spec->ipv6) {
+ only_from_t v4specs;
+ v4specs.reserve(s_config->only_from->size());
+ for (const auto &spec : *s_config->only_from) {
+ if (!spec.ipv6) {
v4specs.push_back(spec);
}
}
- for (ipspec *spec : v4specs) {
+ s_config->only_from->reserve(s_config->only_from->size() +
v4specs.size());
+ for (const auto &spec : v4specs) {
// also add a v4->v6 coverted filter
- ipspec *result = new ipspec();
+ ipspec result{0};
// first 96 bits are fixed: 0:0:0:0:0:ffff
- result->bits = 96 + spec->bits;
- result->ipv6 = true;
- memset(result->ip.v6.address, 0, sizeof(uint16_t) * 5);
- result->ip.v6.address[5] = 0xFFFFu;
- result->ip.v6.address[6] =
- static_cast<uint16_t>(spec->ip.v4.address & 0xFFFFu);
- result->ip.v6.address[7] =
- static_cast<uint16_t>(spec->ip.v4.address >> 16);
- netmaskFromPrefixIPv6(result->bits, result->ip.v6.netmask,
+ result.bits = 96 + spec.bits;
+ result.ipv6 = true;
+ memset(result.ip.v6.address, 0, sizeof(uint16_t) * 5);
+ result.ip.v6.address[5] = 0xFFFFu;
+ result.ip.v6.address[6] =
+ static_cast<uint16_t>(spec.ip.v4.address & 0xFFFFu);
+ result.ip.v6.address[7] =
+ static_cast<uint16_t>(spec.ip.v4.address >> 16);
+ netmaskFromPrefixIPv6(result.bits, result.ip.v6.netmask,
s_winapi);
s_config->only_from.add(result);
}
diff --git a/agents/windows/sections/SectionCheckMK.cc
b/agents/windows/sections/SectionCheckMK.cc
index 9b16aed..433bbc4 100644
--- a/agents/windows/sections/SectionCheckMK.cc
+++ b/agents/windows/sections/SectionCheckMK.cc
@@ -100,17 +100,8 @@ bool SectionCheckMK::produceOutputInner(std::ostream &out) {
if (_only_from->size() == 0) {
out << " 0.0.0.0/0\n";
} else {
- for (const ipspec *is : *_only_from) {
- if (is->ipv6) {
- out << " "
- << join(is->ip.v6.address, is->ip.v6.address + 7,
":")
- << "/" << is->bits;
- } else {
- out << " " << (is->ip.v4.address & 0xff)
<< "."
- << (is->ip.v4.address >> 8 & 0xff) <<
"."
- << (is->ip.v4.address >> 16 & 0xff) <<
"."
- << (is->ip.v4.address >> 24 & 0xff) <<
"/" << is->bits;
- }
+ for (const auto &is : *_only_from) {
+ out << " " << is;
}
}
return true;
diff --git a/agents/windows/types.cc b/agents/windows/types.cc
index fa1b36b..2e79e70 100644
--- a/agents/windows/types.cc
+++ b/agents/windows/types.cc
@@ -23,38 +23,38 @@ std::string from_string<std::string>(const WinApiAdaptor &,
}
template <>
-ipspec *from_string<ipspec *>(const WinApiAdaptor &winapi,
- const std::string &value) {
- ipspec *result = new ipspec();
+ipspec from_string<ipspec>(const WinApiAdaptor &winapi,
+ const std::string &value) {
+ ipspec result{0};
char *slash_pos = strchr(value.c_str(), '/');
if (slash_pos != NULL) {
// ipv4/ipv6 agnostic
- result->bits = strtol(slash_pos + 1, NULL, 10);
+ result.bits = strtol(slash_pos + 1, NULL, 10);
} else {
- result->bits = 0;
+ result.bits = 0;
}
- result->ipv6 = strchr(value.c_str(), ':') != NULL;
+ result.ipv6 = strchr(value.c_str(), ':') != NULL;
- if (result->ipv6) {
- if (result->bits == 0) {
- result->bits = 128;
+ if (result.ipv6) {
+ if (result.bits == 0) {
+ result.bits = 128;
}
- stringToIPv6(value.c_str(), result->ip.v6.address, winapi);
- netmaskFromPrefixIPv6(result->bits, result->ip.v6.netmask, winapi);
+ stringToIPv6(value.c_str(), result.ip.v6.address, winapi);
+ netmaskFromPrefixIPv6(result.bits, result.ip.v6.netmask, winapi);
// TODO verify that host part is 0
} else {
- if (result->bits == 0) {
- result->bits = 32;
+ if (result.bits == 0) {
+ result.bits = 32;
}
- stringToIPv4(value.c_str(), result->ip.v4.address);
- netmaskFromPrefixIPv4(result->bits, result->ip.v4.netmask);
+ stringToIPv4(value.c_str(), result.ip.v4.address);
+ netmaskFromPrefixIPv4(result.bits, result.ip.v4.netmask);
- if ((result->ip.v4.address & result->ip.v4.netmask) !=
- result->ip.v4.address) {
+ if ((result.ip.v4.address & result.ip.v4.netmask) !=
+ result.ip.v4.address) {
fprintf(stderr, "Invalid only_hosts entry: host part not 0: %s",
value.c_str());
exit(1);
@@ -62,3 +62,17 @@ ipspec *from_string<ipspec *>(const WinApiAdaptor &winapi,
}
return result;
}
+
+std::ostream &operator<<(std::ostream &os, const ipspec &ips) {
+ if (ips.ipv6) {
+ os << join(ips.ip.v6.address, ips.ip.v6.address + 7, ":")
<< "/"
+ << ips.bits;
+ } else {
+ os << (ips.ip.v4.address & 0xff) << "."
+ << (ips.ip.v4.address >> 8 & 0xff) << "."
+ << (ips.ip.v4.address >> 16 & 0xff) << "."
+ << (ips.ip.v4.address >> 24 & 0xff) << "/"
<< ips.bits;
+ }
+
+ return os;
+}
diff --git a/agents/windows/types.h b/agents/windows/types.h
index 77e894e..3869de1 100644
--- a/agents/windows/types.h
+++ b/agents/windows/types.h
@@ -85,8 +85,12 @@ struct ipspec {
};
template <>
-ipspec *from_string<ipspec *>(const WinApiAdaptor &winapi,
- const std::string &value);
+ipspec from_string<ipspec>(const WinApiAdaptor &winapi,
+ const std::string &value);
+
+std::ostream &operator<<(std::ostream &os, const ipspec &ips);
+
+using only_from_t = std::vector<ipspec>;
class StateParseError : public std::invalid_argument {
public:
@@ -94,8 +98,6 @@ public:
: std::invalid_argument(what) {}
};
-using only_from_t = std::vector<ipspec *>;
-
class OnScopeExit {
public:
OnScopeExit(const std::function<void()> &cleaner) : _cleaner(cleaner) {}