From b633d08c722f9c5f6bf30989de68adf3fb562a4e Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 27 Feb 2024 20:12:55 +0100 Subject: [PATCH] Add Wireshark dissector for WiVRn packets --- CMakeLists.txt | 21 +- tools/CMakeLists.txt | 3 + tools/wireshark/CMakeLists.txt | 16 + tools/wireshark/dissector.cpp | 727 +++++++++++++++++++++++++++++++++ 4 files changed, 764 insertions(+), 3 deletions(-) create mode 100644 tools/CMakeLists.txt create mode 100644 tools/wireshark/CMakeLists.txt create mode 100644 tools/wireshark/dissector.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ff36abe..1e1b1d13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ auto_option(WIVRN_USE_SYSTEMD "Use libsystemd, required to show pretty hostname" option(WIVRN_USE_ANDROID_VALIDATION_LAYER "Download the android version of the Vulkan validation layer" OFF) option(WIVRN_BUILD_CLIENT "Build WiVRn client" OFF) option(WIVRN_BUILD_SERVER "Build WiVRn server" ON) +option(WIVRN_BUILD_DISSECTOR "Build Wireshark dissector" OFF) auto_option(WIVRN_USE_NVENC "Enable nvenc (Nvidia) hardware encoder" AUTO) auto_option(WIVRN_USE_VAAPI "Enable vaapi (AMD/Intel) hardware encoder" AUTO) @@ -44,9 +45,17 @@ find_package(Git) if (WIVRN_USE_SYSTEM_BOOST STREQUAL "AUTO") - find_package(Boost 1.75.0) + if(WIVRN_BUILD_DISSECTOR) + find_package(Boost 1.84.0) + else() + find_package(Boost 1.75.0) + endif() elseif(WIVRN_USE_SYSTEM_BOOST STREQUAL "ON") - find_package(Boost 1.75.0 REQUIRED) + if(WIVRN_BUILD_DISSECTOR) + find_package(Boost 1.84.0 REQUIRED) + else() + find_package(Boost 1.75.0 REQUIRED) + endif() endif() if (WIVRN_BUILD_SERVER) @@ -119,8 +128,12 @@ if(ANDROID) android_ndk_import_module_native_app_glue() endif() +if (WIVRN_BUILD_DISSECTOR) + find_package(Wireshark REQUIRED) +endif() + # Common dependencies -FetchContent_Declare(boostpfr URL https://github.com/boostorg/pfr/archive/refs/tags/2.0.3.tar.gz) +FetchContent_Declare(boostpfr URL https://github.com/boostorg/pfr/archive/refs/tags/2.2.0.tar.gz) # Client dependencies FetchContent_Declare(fastgltf URL https://github.com/spnda/fastgltf/archive/refs/tags/v0.6.1.tar.gz) FetchContent_Declare(glm URL https://github.com/g-truc/glm/archive/refs/tags/0.9.9.8.tar.gz) @@ -161,3 +174,5 @@ if (WIVRN_BUILD_SERVER) message("Optional features:") message("\tsystemd: ${WIVRN_USE_SYSTEMD}") endif() + +add_subdirectory(tools) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 00000000..3cef64b4 --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,3 @@ +if(WIVRN_BUILD_DISSECTOR) + add_subdirectory(wireshark) +endif() diff --git a/tools/wireshark/CMakeLists.txt b/tools/wireshark/CMakeLists.txt new file mode 100644 index 00000000..ef1ba6cb --- /dev/null +++ b/tools/wireshark/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(wivrn-dissector SHARED + dissector.cpp + ) + +set_target_properties(wivrn-dissector PROPERTIES + PREFIX "" + OUTPUT_NAME "wivrn" + DEFINE_SYMBOL "" + ) + +target_link_libraries(wivrn-dissector PRIVATE epan wivrn-common) +set_target_properties(wivrn-dissector PROPERTIES CXX_VISIBILITY_PRESET hidden) + +install(TARGETS wivrn-dissector + LIBRARY DESTINATION "${Wireshark_PLUGIN_LIBDIR}/epan" NAMELINK_SKIP +) diff --git a/tools/wireshark/dissector.cpp b/tools/wireshark/dissector.cpp new file mode 100644 index 00000000..58157999 --- /dev/null +++ b/tools/wireshark/dissector.cpp @@ -0,0 +1,727 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "wivrn_packets.h" +#include +#include +#include + +using namespace xrt::drivers::wivrn; + +namespace +{ +int proto; + +namespace details +{ +template +struct is_stdarray : public std::false_type +{}; + +template +struct is_stdarray> : public std::true_type +{}; + +template +inline constexpr bool is_stdarray_v = is_stdarray::value; + +template +inline constexpr bool is_struct_v = std::is_aggregate_v && !is_stdarray_v; + +template struct fixed_string +{ + char value[N]; + + consteval fixed_string() = default; + + consteval fixed_string(const char (&str)[N]) + { + std::copy_n(str, N, value); + } + + consteval fixed_string(std::string_view str) + { + std::copy_n(str.data(), N - 1, value); + value[N - 1] = '\0'; + } + + constexpr size_t size() const + { + return N; + } +}; + +template +consteval auto join(const fixed_string& str1, const fixed_string& str2) -> fixed_string +{ + fixed_string value; + std::copy_n(str1.value, N1, &value.value[0]); + value.value[N1 - 1] = '.'; + std::copy_n(str2.value, N2, &value.value[N1]); + return value; +} + +template +consteval auto join(const fixed_string& str1, std::string_view str2) +{ + fixed_string value; + std::copy_n(str1.value, N1, &value.value[0]); + value.value[N1 - 1] = '.'; + std::copy_n(str2.data(), str2.size(), &value.value[N1]); + return value; +} + +std::string name_from_abbrev(const std::string& name) +{ + auto pos = name.find_last_of('.'); + if (pos == std::string::npos) + return name; + else + return name.substr(pos + 1); +} + +template +constexpr auto type_name() +{ + constexpr std::string_view sv = __PRETTY_FUNCTION__; + constexpr std::string_view sv2 = sv.substr(sv.find("T = ") + 4); + constexpr std::string_view sv3 = sv2.substr(0, sv2.size() - 1); + + constexpr size_t pos = sv3.rfind("::"); + constexpr size_t pos2 = pos == std::string_view::npos ? 0 : (pos + 2); + + constexpr std::string_view sv4 = sv3.substr(pos2); + + return fixed_string(sv4); +} + +template ftenum field_type = FT_NONE; + +template<> ftenum field_type = FT_BOOLEAN; +template<> ftenum field_type = FT_UINT8; +template<> ftenum field_type = FT_UINT16; +template<> ftenum field_type = FT_UINT32; +template<> ftenum field_type = FT_UINT64; +template<> ftenum field_type = FT_INT8; +template<> ftenum field_type = FT_INT16; +template<> ftenum field_type = FT_INT32; +template<> ftenum field_type = FT_INT64; +template<> ftenum field_type = FT_FLOAT; +template<> ftenum field_type = FT_DOUBLE; + +} + +template +struct tree_traits; + +std::unordered_map subtree_handles; +std::vector fields; + + +template +struct tree_traits>> +{ + static inline int field_handle = -1; + static void info() + { + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = details::field_type, + .display = BASE_DEC, + .strings = nullptr, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + proto_tree_add_item(tree, field_handle, tvb, start, sizeof(T), ENC_LITTLE_ENDIAN); + start += sizeof(T); + } + + static size_t size(tvbuff_t *tvb, int& start) + { + start += sizeof(T); + return sizeof(T); + } +}; + +template +struct tree_traits>> +{ + static inline int field_handle = -1; + + using U = std::underlying_type_t; + constexpr static auto entries = magic_enum::enum_entries(); + + static void info() + { + value_string * strings = new value_string[entries.size() + 1]; + for(size_t i = 0; i < entries.size(); i++) + { + strings[i].value = (uint32_t)entries[i].first; + strings[i].strptr = strndup(entries[i].second.data(), entries[i].second.size()); + } + strings[entries.size()] = {0, nullptr}; + + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = details::field_type, + .display = BASE_DEC, + .strings = strings, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + proto_tree_add_item(tree, field_handle, tvb, start, sizeof(U), ENC_LITTLE_ENDIAN); + start += sizeof(U); + } + + static size_t size(tvbuff_t *tvb, int& start) + { + start += sizeof(U); + return sizeof(U); + } +}; + +template +struct tree_traits>> +{ + static inline int field_handle = -1; + + template + static void info_aux2() + { + constexpr static auto names = boost::pfr::names_as_array(); + tree_traits(abbrev, names[I]), boost::pfr::tuple_element_t>::info(); + } + + template + static void info_aux1(std::index_sequence) + { + (info_aux2(), ...); + } + + static void info() + { + subtree_handles.emplace(abbrev.value, -1); + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = FT_NONE, + .display = BASE_NONE, + .strings = nullptr, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + + info_aux1(std::make_index_sequence>()); + } + + template + static void dissect_aux2(proto_tree *tree, tvbuff_t *tvb, int& start) + { + constexpr static auto names = boost::pfr::names_as_array(); + tree_traits(abbrev, names[I]), boost::pfr::tuple_element_t>::dissect(tree, tvb, start); + } + + template + static void dissect_aux1(proto_tree *tree, tvbuff_t *tvb, int& start, std::index_sequence) + { + (dissect_aux2(tree, tvb, start), ...); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + int start2 = start; + size_t size2 = size(tvb, start2); + + proto_item * ti = proto_tree_add_item(tree, field_handle, tvb, start, size2, ENC_NA); + proto_tree * subtree = proto_item_add_subtree(ti, subtree_handles.at(abbrev.value)); + + dissect_aux1(subtree, tvb, start, std::make_index_sequence>()); + } + + template + static size_t size_aux2(tvbuff_t *tvb, int& start) + { + constexpr static auto names = boost::pfr::names_as_array(); + return tree_traits(abbrev, names[I]), boost::pfr::tuple_element_t>::size(tvb, start); + } + + template + static size_t size_aux1(tvbuff_t *tvb, int& start, std::index_sequence) + { + return (size_aux2(tvb, start) + ... + 0); + } + + static size_t size(tvbuff_t *tvb, int& start) + { + return size_aux1(tvb, start, std::make_index_sequence>()); + } +}; + +template +struct tree_traits +{ + static inline int field_handle = -1; + + static void info() + { + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = FT_STRING, +#if WIRESHARK_VERSION_MAJOR >= 4 && WIRESHARK_VERSION_MINOR >= 2 + .display = BASE_STR_WSP, +#else + .display = BASE_NONE, +#endif + .strings = nullptr, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + size_t string_size = tvb_get_guint16(tvb, start, ENC_LITTLE_ENDIAN); + start += sizeof(uint16_t); + + proto_tree_add_item(tree, field_handle, tvb, start, string_size, ENC_STRING); + + start += string_size; + } + + static size_t size(tvbuff_t *tvb, int& start) + { + size_t string_size = tvb_get_guint16(tvb, start, ENC_LITTLE_ENDIAN); + start += sizeof(uint16_t); + start += string_size; + + return sizeof(uint16_t) + string_size; + } +}; + +template +struct tree_traits> +{ + static inline int field_handle = -1; + + static void info() + { + subtree_handles.emplace(abbrev.value, -1); + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = FT_NONE, + .display = BASE_NONE, + .strings = nullptr, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + + tree_traits()), T>::info(); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + int start2 = start; + size_t size2 = size(tvb, start2); + + proto_item * ti = proto_tree_add_item(tree, field_handle, tvb, start, size2, ENC_NA); + proto_tree * subtree = proto_item_add_subtree(ti, subtree_handles.at(abbrev.value)); + + size_t count = tvb_get_guint16(tvb, start, ENC_LITTLE_ENDIAN); + start += sizeof(uint16_t); + + for(size_t i = 0; i < count; i++) + { + tree_traits()), T>::dissect(subtree, tvb, start); + } + } + + static size_t size(tvbuff_t *tvb, int& start) + { + size_t count = tvb_get_guint16(tvb, start, ENC_LITTLE_ENDIAN); + start += sizeof(uint16_t); + size_t size = sizeof(uint16_t); + + for(size_t i = 0; i < count; i++) + { + size += tree_traits()), T>::size(tvb, start); + } + + return size; + } +}; + +template +struct tree_traits> +{ + static void info() + { + tree_traits::info(); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + int start2 = start; + size_t size2 = size(tvb, start2); + + bool has_value = tvb_get_guint8(tvb, start); + + if (has_value) + { + start += sizeof(uint8_t); + tree_traits::dissect(tree, tvb, start); + } + else + { + // auto pi = proto_tree_add_format_text(tree, tvb, start, sizeof(uint8_t)); + // proto_item_set_text(pi, "none"); + start += sizeof(uint8_t); + } + } + + static size_t size(tvbuff_t *tvb, int& start) + { + bool has_value = tvb_get_guint8(tvb, start); + start += sizeof(uint8_t); + size_t size = sizeof(uint8_t); + + if (has_value) + { + size += tree_traits::size(tvb, start); + } + + return size; + } +}; + +template +struct tree_traits> +{ + static inline int field_handle = -1; + + static void info() + { + subtree_handles.emplace(abbrev.value, -1); + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = FT_NONE, + .display = BASE_NONE, + .strings = nullptr, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + + tree_traits()), T>::info(); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + int start2 = start; + size_t size2 = size(tvb, start2); + + proto_item * ti = proto_tree_add_item(tree, field_handle, tvb, start, size2, ENC_NA); + proto_tree * subtree = proto_item_add_subtree(ti, subtree_handles.at(abbrev.value)); + + for(size_t i = 0; i < N; i++) + { + tree_traits()), T>::dissect(subtree, tvb, start); + } + } + + static size_t size(tvbuff_t *tvb, int& start) + { + size_t size = 0; + + for(size_t i = 0; i < N; i++) + { + size += tree_traits()), T>::size(tvb, start); + } + + return size; + } +}; + +template +struct tree_traits> +{ + template + using i_th_type = std::tuple_element_t>; + + template + static void info_aux2() + { + tree_traits>()), i_th_type>::info(); + } + + template + static void info_aux1(std::index_sequence) + { + (info_aux2(), ...); + } + + static void info() + { + info_aux1(std::make_index_sequence()); + } + + template + static void dissect_aux(proto_tree *tree, tvbuff_t *tvb, int& start, size_t type_index, std::index_sequence) + { + ((I == type_index ? (void)(tree_traits>()), i_th_type>::dissect(tree, tvb, start)) : (void)0), ...); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + uint8_t type_index = tvb_get_guint8(tvb, start); + start += 1; + + dissect_aux(tree, tvb, start, type_index, std::make_index_sequence()); + } + + template + static size_t size_aux(tvbuff_t *tvb, int& start, size_t type_index, std::index_sequence) + { + size_t value; + ((I == type_index ? (void)(value = tree_traits>()), i_th_type>::size(tvb, start)) : (void)0), ...); + return value; + } + + static size_t size(tvbuff_t *tvb, int& start) + { + uint8_t type_index = tvb_get_guint8(tvb, start); + start += sizeof(uint8_t); + + return sizeof(uint8_t) + size_aux(tvb, start, type_index, std::make_index_sequence()); + } +}; + +template +struct tree_traits +{ + static inline int field_handle = -1; + using T = std::chrono::nanoseconds::rep; + + static void info() + { + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = details::field_type, + .display = BASE_DEC | BASE_UNIT_STRING, + .strings = &units_nanoseconds, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + proto_tree_add_item(tree, field_handle, tvb, start, sizeof(T), ENC_LITTLE_ENDIAN); + start += sizeof(T); + } + + static size_t size(tvbuff_t *tvb, int& start) + { + start += sizeof(T); + return sizeof(T); + } +}; + +template +struct tree_traits> +{ + static inline int field_handle = -1; + + static void info() + { + hf_register_info hf = { + .p_id = &field_handle, + .hfinfo = { + .name = strdup(details::name_from_abbrev(abbrev.value).c_str()), + .abbrev = abbrev.value, + .type = FT_BYTES, + .display = BASE_NONE, + .strings = nullptr, + .bitmask = 0, + .blurb = "", + } + }; + HFILL_INIT(hf); + + fields.push_back(hf); + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + size_t span_size = tvb_get_guint16(tvb, start, ENC_LITTLE_ENDIAN); + start += sizeof(uint16_t); + + proto_tree_add_item(tree, field_handle, tvb, start, span_size, ENC_NA); + + start += span_size; + } + + static size_t size(tvbuff_t *tvb, int& start) + { + size_t span_size = tvb_get_guint16(tvb, start, ENC_LITTLE_ENDIAN); + start += sizeof(uint16_t); + start += span_size; + + return sizeof(uint16_t) + span_size; + } +}; + +template +struct tree_traits +{ + static void info() + { + } + + static void dissect(proto_tree *tree, tvbuff_t *tvb, int& start) + { + } + + static size_t size(tvbuff_t *tvb, int& start) + { + return 0; + } +}; + + +int dissect_wivrn(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree _U_, void * data _U_, bool tcp) +{ + col_set_str(pinfo->cinfo, COL_PROTOCOL, "WiVRn"); + /* Clear out stuff in the info column */ + col_clear(pinfo->cinfo, COL_INFO); + + proto_item * ti = proto_tree_add_item(tree, proto, tvb, 0, -1, ENC_NA); + proto_tree * subtree = proto_item_add_subtree(ti, subtree_handles.at("")); + int start = 0; + + if (tcp) + start += sizeof(uint16_t); + + if (pinfo->destport == xrt::drivers::wivrn::default_port) + tree_traits<"wivrn.from_headset", from_headset::packets>::dissect(subtree, tvb, start); + else + tree_traits<"wivrn.to_headset", to_headset::packets>::dissect(subtree, tvb, start); + + + return tvb_captured_length(tvb); +} + +int dissect_wivrn_udp(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree _U_, void * data _U_) +{ + return dissect_wivrn(tvb, pinfo, tree, data, false); +} + +int dissect_wivrn_tcp(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree _U_, void * data _U_) +{ + return dissect_wivrn(tvb, pinfo, tree, data, true); +} + +void proto_register_wivrn() +{ + subtree_handles.emplace("", -1); + tree_traits<"wivrn.from_headset", from_headset::packets>::info(); + tree_traits<"wivrn.to_headset", to_headset::packets>::info(); + + proto = proto_register_protocol( + "WiVRn protocol", + "WiVRn", + "wivrn"); + + proto_register_field_array(proto, fields.data(), fields.size()); + + std::vector tree; + for(auto& [i,j]: subtree_handles) + { + tree.push_back(&j); + } + + proto_register_subtree_array(tree.data(), tree.size()); +} + +void proto_reg_handoff_wivrn() +{ + static dissector_handle_t handle_tcp = create_dissector_handle(dissect_wivrn_tcp, proto); + static dissector_handle_t handle_udp = create_dissector_handle(dissect_wivrn_udp, proto); + dissector_add_uint("udp.port", xrt::drivers::wivrn::default_port, handle_udp); + dissector_add_uint("tcp.port", xrt::drivers::wivrn::default_port, handle_tcp); +} + +} // namespace +extern "C" WS_DLL_PUBLIC_DEF const char plugin_version[] = "0.11"; +extern "C" WS_DLL_PUBLIC_DEF const int plugin_want_major = WIRESHARK_VERSION_MAJOR; +extern "C" WS_DLL_PUBLIC_DEF const int plugin_want_minor = WIRESHARK_VERSION_MINOR; + +extern "C" WS_DLL_PUBLIC_DEF void plugin_register(void) +{ + static proto_plugin plug; + + plug.register_protoinfo = proto_register_wivrn; + plug.register_handoff = proto_reg_handoff_wivrn; + proto_register_plugin(&plug); +}