Skip to content

Commit

Permalink
IP: Fix formatting issue with IPSpace iteration and BWF.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidWallOfCode committed Sep 22, 2023
1 parent 6def36a commit f0c6a34
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion code/include/swoc/IPRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ inline IPRange::IPRange(string_view const &text) {
inline IPRange::IPRange(IPRangeView const& view) {
if (AF_INET == view._family) {
*this = *view._raw._4;
} else if (AF_INET6 == _family) {
} else if (AF_INET6 == view._family) {
*this = *view._raw._6;
}
}
Expand Down
3 changes: 3 additions & 0 deletions code/include/swoc/bwf_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IP6Range const &r

BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IPRange const &range);

BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IPRangeView const &range);

BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IPNet const &net);

BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, IP4Net const &net);
Expand Down Expand Up @@ -108,4 +110,5 @@ operator<<(ostream &s, swoc::IPRange const &Range) {
swoc::LocalBufferWriter<swoc::IP_STREAM_SIZE> w;
return s << bwformat(w, swoc::bwf::Spec::DEFAULT, Range);
}

} // namespace std
6 changes: 6 additions & 0 deletions code/src/bw_ip_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ bwformat(BufferWriter &w, Spec const &spec, IPRange const &range) {
range.is(AF_INET6) ? bwformat(w, spec, range.ip6()) : w.write("*-*"_tv);
}

BufferWriter &
bwformat(BufferWriter &w, Spec const &spec, IPRangeView const &rv) {
return rv.is(AF_INET) ? bwformat(w, spec, rv.ip4()) :
rv.is(AF_INET6) ? bwformat(w, spec, rv.ip6()) : w.write("*-*"_tv);
}

BufferWriter &
bwformat(BufferWriter &w, Spec const &spec, IP4Net const &net) {
bwformat(w, spec, net.min());
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(ex_flat_space PRIVATE -Wall -Wextra -Werror)
endif()

add_executable(ex_diskstats ex_diskstats.cc ../image/include/swoc/IntrusiveDList.h)
add_executable(ex_diskstats ex_diskstats.cc ../code/include/swoc/IntrusiveDList.h)
target_link_libraries(ex_diskstats PUBLIC libswoc-static)
if (CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(ex_diskstats PRIVATE -Wall -Wextra -Werror)
Expand Down
26 changes: 26 additions & 0 deletions unit_tests/test_ip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ TEST_CASE("IP ranges and networks", "[libswoc][ip][net][range]") {
CHECK(*r5_net == swoc::IPNet{a, m});
++r5_net;
}

}

TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") {
Expand Down Expand Up @@ -1083,6 +1084,31 @@ TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") {
auto [ r, p ] = *space.find(IPAddr{"2001:4997:58:400::1E"});
REQUIRE(r.empty());
}

space.clear();
// Test a mix
unsigned idx = 0;
std::array<TextView, 6> mix_r { "1.1.1.1-1.1.1.111", "2.2.2.2-2.2.2.222", "3.3.3.3-3.255.255.255",
"1:2:3:4:5:6:7:8-1:2:3:4:5:6:7:ffff",
"11:2:3:4:5:6:7:8-11:2:3:4:5:6:7:ffff",
"111:2:3:4:5:6:7:8-111:2:3:4:5:6:7:ffff"
};
for ( auto && r : mix_r) {
space.mark(IPRange(r), idx);
++idx;
}

idx = 0;
std::string s;
for (auto [r,p] : space) {
REQUIRE(!r.empty());
REQUIRE(p == idx);
swoc::LocalBufferWriter<64> dbg;
bwformat(dbg, swoc::bwf::Spec::DEFAULT, r);
bwprint(s, "{}", r);
REQUIRE(s == mix_r[idx]);
++idx;
}
}

TEST_CASE("IPSpace bitset", "[libswoc][ipspace][bitset]") {
Expand Down

0 comments on commit f0c6a34

Please sign in to comment.