/* * WiVRn VR streaming * Copyright (C) 2024 Guillaume Meunier * Copyright (C) 2024 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include "smp.h" #include "wivrn_config.h" #include "wivrn_packets.h" #if WIRESHARK_VERSION_MAJOR > 4 || (WIRESHARK_VERSION_MAJOR == 4 && WIRESHARK_VERSION_MINOR >= 4) static inline uint32_t get_uint32(tvbuff_t * tvb, const int offset, const unsigned encoding) { return tvb_get_uint32(tvb, offset, encoding); } static inline uint16_t get_uint16(tvbuff_t * tvb, const int offset, const unsigned encoding) { return tvb_get_uint16(tvb, offset, encoding); } static inline uint16_t get_uint8(tvbuff_t * tvb, const int offset) { return tvb_get_uint8(tvb, offset); } #else static inline uint32_t get_uint32(tvbuff_t * tvb, const int offset, const unsigned encoding) { return tvb_get_guint32(tvb, offset, encoding); } static inline uint16_t get_uint16(tvbuff_t * tvb, const int offset, const unsigned encoding) { return tvb_get_guint16(tvb, offset, encoding); } static inline uint16_t get_uint8(tvbuff_t * tvb, const int offset) { return tvb_get_guint8(tvb, offset); } #endif using namespace 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 size_t pos3 = sv3.find_first_of(", "); constexpr std::string_view sv4 = sv3.substr(pos2, pos3 == std::string_view::npos ? pos3 : pos3 - 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 <> [[maybe_unused]] 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 <> [[maybe_unused]] ftenum field_type = FT_DOUBLE; } // namespace details 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) { std::string s = abbrev.value; auto member = s.substr(s.find_last_of('.') + 1); if (std::is_same_v && member.starts_with("angle")) { float value = tvb_get_ieee_float(tvb, start, ENC_LITTLE_ENDIAN) * 180 / M_PI; proto_tree_add_float_format_value(tree, field_handle, tvb, start, sizeof(T), value, "%f deg", value); } else 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([[maybe_unused]] proto_tree * tree, [[maybe_unused]] 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([[maybe_unused]] 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 = get_uint16(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 = get_uint16(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 = get_uint16(tvb, start, ENC_LITTLE_ENDIAN); start += sizeof(uint16_t); if (count & 0x8000) { count = (count & 0x7fff) | (get_uint16(tvb, start, ENC_LITTLE_ENDIAN) << 15); 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 = get_uint16(tvb, start, ENC_LITTLE_ENDIAN); start += sizeof(uint16_t); size_t size = sizeof(uint16_t); if (count & 0x8000) { count = (count & 0x7fff) | (get_uint16(tvb, start, ENC_LITTLE_ENDIAN) << 15); start += sizeof(uint16_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) { bool has_value = get_uint8(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 = get_uint8(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 = get_uint8(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 = get_uint8(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; using T = std::chrono::seconds::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_seconds, .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 = get_uint16(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 = get_uint16(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; } }; 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 string_size = get_uint16(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 = get_uint16(tvb, start, ENC_LITTLE_ENDIAN); start += sizeof(uint16_t); start += string_size; return sizeof(uint16_t) + string_size; } }; 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); int offset = 0; while (true) { 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 remaining = tvb_reported_length_remaining(tvb, offset); if (tcp) { if (remaining < sizeof(uint32_t)) return remaining - sizeof(uint32_t); remaining -= sizeof(uint32_t); uint32_t size = get_uint32(tvb, offset, ENC_LITTLE_ENDIAN); if (remaining < size) return remaining - size; offset += sizeof(uint32_t); } if (pinfo->destport == wivrn::default_port) tree_traits<"wivrn.from_headset", from_headset::packets>::dissect(subtree, tvb, offset); else tree_traits<"wivrn.to_headset", to_headset::packets>::dissect(subtree, tvb, offset); if (not tcp) return offset; if (tvb_reported_length(tvb) <= offset) return offset; } } 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", wivrn::default_port, handle_udp); dissector_add_uint("tcp.port", 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); }