Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions netkat/packet_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ bool PacketSetManager::Contains(PacketSetHandle packet_set,
return Contains(node.default_branch, packet);
}

std::string PacketSetManager::ToDot(PacketSetHandle packet_set) const {
std::string PacketSetManager::ToDotRaw(PacketSetHandle packet_set) const {
std::string result = "digraph {\n";
std::queue<PacketSetHandle> work_list;
work_list.push(packet_set);
Expand Down Expand Up @@ -165,10 +165,68 @@ std::string PacketSetManager::ToDot(PacketSetHandle packet_set) const {
bool new_branch = visited.insert(fallthrough).second;
if (new_branch) work_list.push(fallthrough);
}
absl::StrAppend(&result, "}\n");
absl::StrAppend(&result, "}");
return result;
}

std::string PacketSetManager::ToDotUrl(PacketSetHandle packet_set) const {
// "digraph {\n"
std::string url =
"https://dreampuf.github.io/GraphvizOnline/?engine=dot#digraph%20%7B%0A";
std::queue<PacketSetHandle> work_list;
work_list.push(packet_set);
// " 4294967294 [label=\"T\" shape=box]\n"
absl::flat_hash_set<PacketSetHandle> visited = {packet_set};
absl::StrAppend(&url, "%20%20");
absl::StrAppendFormat(&url, "%d", SentinelNodeIndex::kFullSet);
absl::StrAppend(&url, "%20%5Blabel%3D%22T%22%20shape%3Dbox%5D%0A");
// " 4294967295 [label=\"F\" shape=box]\n"
absl::StrAppend(&url, "%20%20");
absl::StrAppendFormat(&url, "%d", SentinelNodeIndex::kEmptySet);
absl::StrAppend(&url, "%20%5Blabel%3D%22F%22%20shape%3Dbox%5D%0A");

while (!work_list.empty()) {
PacketSetHandle packet_set = work_list.front();
work_list.pop();
if (IsFullSet(packet_set) || IsEmptySet(packet_set)) continue;

const DecisionNode& node = GetNodeOrDie(packet_set);
// " <node_index> [label=\"<field_name>\"]\n"
absl::StrAppend(&url, "%20%20");
absl::StrAppendFormat(&url, "%d", packet_set.node_index_);
absl::StrAppend(&url, "%20%5Blabel%3D%22");
absl::StrAppend(&url, field_manager_.GetFieldName(node.field));
absl::StrAppend(&url, "%22%5D%0A");

for (const auto& [value, branch] : node.branch_by_field_value) {
// " <node_index> -> <node_index> [label=\"<value>\"]\n"
absl::StrAppend(&url, "%20%20");
absl::StrAppendFormat(&url, "%d", packet_set.node_index_);
absl::StrAppend(&url, "%20-%3E%20");
absl::StrAppendFormat(&url, "%d", branch.node_index_);
absl::StrAppend(&url, "%20%5Blabel%3D%22");
absl::StrAppend(&url, value);
absl::StrAppend(&url, "%22%5D%0A");
if (IsFullSet(branch) || IsEmptySet(branch)) continue;
bool new_branch = visited.insert(branch).second;
if (new_branch) work_list.push(branch);
}
PacketSetHandle fallthrough = node.default_branch;
// " <node_index> -> <node_index> [style=dashed]\n"
absl::StrAppend(&url, "%20%20");
absl::StrAppendFormat(&url, "%d", packet_set.node_index_);
absl::StrAppend(&url, "%20-%3E%20");
absl::StrAppendFormat(&url, "%d", fallthrough.node_index_);
absl::StrAppend(&url, "%20%5Bstyle%3Ddashed%5D%0A");
if (IsFullSet(fallthrough) || IsEmptySet(fallthrough)) continue;
bool new_branch = visited.insert(fallthrough).second;
if (new_branch) work_list.push(fallthrough);
}
// "}"
absl::StrAppend(&url, "%7D");
return url;
}

PacketSetHandle PacketSetManager::Compile(const PredicateProto& pred) {
switch (pred.predicate_case()) {
case PredicateProto::kBoolConstant:
Expand Down
7 changes: 5 additions & 2 deletions netkat/packet_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ class PacketSetManager {
// `packet`, or false otherwise.
bool Contains(PacketSetHandle packet_set, const Packet& packet) const;

// Returns a dot string representation of the given `packet_set`.
std::string ToDot(PacketSetHandle packet_set) const;
// Returns a raw dot string representation of the given `packet_set`.
std::string ToDotRaw(PacketSetHandle packet_set) const;
// Returns a URL to a Graphviz visualized representation of the given
// `packet_set`.
std::string ToDotUrl(PacketSetHandle packet_set) const;

// Compiles the given `PredicateProto` into a `PacketSetHandle` that
// represents the set of packets satisfying the predicate.
Expand Down
6 changes: 6 additions & 0 deletions netkat/packet_set_test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ digraph {
5 -> 4294967294 [label="5"]
5 -> 4294967295 [style=dashed]
}
-- URL -------------------------------------------------------------------------
https://dreampuf.github.io/GraphvizOnline/?engine=dot#digraph%20%7B%0A%20%204294967294%20%5Blabel%3D%22T%22%20shape%3Dbox%5D%0A%20%204294967295%20%5Blabel%3D%22F%22%20shape%3Dbox%5D%0A%20%2014%20%5Blabel%3D%22a%22%5D%0A%20%2014%20-%3E%2013%20%5Blabel%3D%223%22%5D%0A%20%2014%20-%3E%206%20%5Bstyle%3Ddashed%5D%0A%20%2013%20%5Blabel%3D%22b%22%5D%0A%20%2013%20-%3E%204294967294%20%5Blabel%3D%224%22%5D%0A%20%2013%20-%3E%204294967295%20%5Blabel%3D%225%22%5D%0A%20%2013%20-%3E%205%20%5Bstyle%3Ddashed%5D%0A%20%206%20%5Blabel%3D%22b%22%5D%0A%20%206%20-%3E%204294967295%20%5Blabel%3D%225%22%5D%0A%20%206%20-%3E%205%20%5Bstyle%3Ddashed%5D%0A%20%205%20%5Blabel%3D%22c%22%5D%0A%20%205%20-%3E%204294967294%20%5Blabel%3D%225%22%5D%0A%20%205%20-%3E%204294967295%20%5Bstyle%3Ddashed%5D%0A%7D
================================================================================
Test case: q := (b=3 && c=4) || (a=5 && c!=5). Example from Katch paper Fig 3.
================================================================================
Expand Down Expand Up @@ -66,6 +68,8 @@ digraph {
16 -> 4294967294 [label="4"]
16 -> 4294967295 [style=dashed]
}
-- URL -------------------------------------------------------------------------
https://dreampuf.github.io/GraphvizOnline/?engine=dot#digraph%20%7B%0A%20%204294967294%20%5Blabel%3D%22T%22%20shape%3Dbox%5D%0A%20%204294967295%20%5Blabel%3D%22F%22%20shape%3Dbox%5D%0A%20%2024%20%5Blabel%3D%22a%22%5D%0A%20%2024%20-%3E%209%20%5Blabel%3D%225%22%5D%0A%20%2024%20-%3E%2017%20%5Bstyle%3Ddashed%5D%0A%20%209%20%5Blabel%3D%22c%22%5D%0A%20%209%20-%3E%204294967295%20%5Blabel%3D%225%22%5D%0A%20%209%20-%3E%204294967294%20%5Bstyle%3Ddashed%5D%0A%20%2017%20%5Blabel%3D%22b%22%5D%0A%20%2017%20-%3E%2016%20%5Blabel%3D%223%22%5D%0A%20%2017%20-%3E%204294967295%20%5Bstyle%3Ddashed%5D%0A%20%2016%20%5Blabel%3D%22c%22%5D%0A%20%2016%20-%3E%204294967294%20%5Blabel%3D%224%22%5D%0A%20%2016%20-%3E%204294967295%20%5Bstyle%3Ddashed%5D%0A%7D
================================================================================
Test case: p + q. Example from Katch paper Fig 3.
================================================================================
Expand Down Expand Up @@ -127,3 +131,5 @@ digraph {
9 -> 4294967295 [label="5"]
9 -> 4294967294 [style=dashed]
}
-- URL -------------------------------------------------------------------------
https://dreampuf.github.io/GraphvizOnline/?engine=dot#digraph%20%7B%0A%20%204294967294%20%5Blabel%3D%22T%22%20shape%3Dbox%5D%0A%20%204294967295%20%5Blabel%3D%22F%22%20shape%3Dbox%5D%0A%20%2034%20%5Blabel%3D%22a%22%5D%0A%20%2034%20-%3E%2032%20%5Blabel%3D%223%22%5D%0A%20%2034%20-%3E%2033%20%5Blabel%3D%225%22%5D%0A%20%2034%20-%3E%2031%20%5Bstyle%3Ddashed%5D%0A%20%2032%20%5Blabel%3D%22b%22%5D%0A%20%2032%20-%3E%2030%20%5Blabel%3D%223%22%5D%0A%20%2032%20-%3E%204294967294%20%5Blabel%3D%224%22%5D%0A%20%2032%20-%3E%204294967295%20%5Blabel%3D%225%22%5D%0A%20%2032%20-%3E%205%20%5Bstyle%3Ddashed%5D%0A%20%2033%20%5Blabel%3D%22b%22%5D%0A%20%2033%20-%3E%209%20%5Blabel%3D%225%22%5D%0A%20%2033%20-%3E%204294967294%20%5Bstyle%3Ddashed%5D%0A%20%2031%20%5Blabel%3D%22b%22%5D%0A%20%2031%20-%3E%2030%20%5Blabel%3D%223%22%5D%0A%20%2031%20-%3E%204294967295%20%5Blabel%3D%225%22%5D%0A%20%2031%20-%3E%205%20%5Bstyle%3Ddashed%5D%0A%20%2030%20%5Blabel%3D%22c%22%5D%0A%20%2030%20-%3E%204294967294%20%5Blabel%3D%224%22%5D%0A%20%2030%20-%3E%204294967294%20%5Blabel%3D%225%22%5D%0A%20%2030%20-%3E%204294967295%20%5Bstyle%3Ddashed%5D%0A%20%205%20%5Blabel%3D%22c%22%5D%0A%20%205%20-%3E%204294967294%20%5Blabel%3D%225%22%5D%0A%20%205%20-%3E%204294967295%20%5Bstyle%3Ddashed%5D%0A%20%209%20%5Blabel%3D%22c%22%5D%0A%20%209%20-%3E%204294967295%20%5Blabel%3D%225%22%5D%0A%20%209%20-%3E%204294967294%20%5Bstyle%3Ddashed%5D%0A%7D
8 changes: 6 additions & 2 deletions netkat/packet_set_test_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ namespace {
constexpr char kBanner[] =
"=========================================================================="
"======\n";
constexpr char kDotHeader[] =
constexpr char kDotRawHeader[] =
"-- DOT -------------------------------------------------------------------"
"------\n";
constexpr char kDotUrlHeader[] =
"-- URL -------------------------------------------------------------------"
"------\n";
constexpr char kStringHeader[] =
"-- STRING ----------------------------------------------------------------"
"------\n";
Expand Down Expand Up @@ -79,7 +82,8 @@ void main() {
std::cout << kBanner << "Test case: " << test_case.description << std::endl
<< kBanner;
std::cout << kStringHeader << manager.ToString(packet_set);
std::cout << kDotHeader << manager.ToDot(packet_set);
std::cout << kDotRawHeader << manager.ToDotRaw(packet_set) << std::endl;
std::cout << kDotUrlHeader << manager.ToDotUrl(packet_set) << std::endl;
}
}

Expand Down