diff options
| -rw-r--r-- | .clang-format | 33 | ||||
| -rw-r--r-- | include/net/client.hpp | 2 | ||||
| -rw-r--r-- | include/net/conn.hpp | 129 | ||||
| -rw-r--r-- | include/net/packet.hpp | 6 | ||||
| -rw-r--r-- | include/net/server.hpp | 25 | ||||
| -rw-r--r-- | include/net/types.hpp | 32 | ||||
| -rw-r--r-- | include/util/bitset.hpp | 71 | ||||
| -rw-r--r-- | include/version.hpp | 2 | ||||
| -rw-r--r-- | src/kill_proxy.cpp | 20 |
9 files changed, 149 insertions, 171 deletions
diff --git a/.clang-format b/.clang-format index 60d5ee5..b182cc9 100644 --- a/.clang-format +++ b/.clang-format @@ -35,29 +35,7 @@ AlignConsecutiveShortCaseStatements: Enabled: false AcrossEmptyLines: false AcrossComments: false - AlignCaseArrows: false AlignCaseColons: false -AlignConsecutiveTableGenBreakingDAGArgColons: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveTableGenCondOperatorColons: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveTableGenDefinitionColons: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false AlignEscapedNewlines: Right AlignOperands: DontAlign AlignTrailingComments: @@ -67,7 +45,6 @@ AllowAllArgumentsOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowBreakBeforeNoexceptSpecifier: Never AllowShortBlocksOnASingleLine: Empty -AllowShortCaseExpressionOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false AllowShortCompoundRequirementOnASingleLine: true AllowShortEnumsOnASingleLine: true @@ -104,7 +81,6 @@ BraceWrapping: BreakAdjacentStringLiterals: true BreakAfterAttributes: Leave BreakAfterJavaFieldAnnotations: false -BreakAfterReturnType: None BreakArrays: true BreakBeforeBinaryOperators: All BreakBeforeBraces: WebKit @@ -112,10 +88,8 @@ BreakBeforeConceptDeclarations: Always BreakBeforeInlineASMColon: OnlyMultiline BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeComma -BreakFunctionDefinitionParameters: false BreakInheritanceList: BeforeColon BreakStringLiterals: true -BreakTemplateDeclarations: MultiLine ColumnLimit: 0 CommentPragmas: "^ IWYU pragma:" CompactNamespaces: false @@ -171,15 +145,10 @@ IntegerLiteralSeparator: HexMinDigits: 0 JavaScriptQuotes: Leave JavaScriptWrapImports: true -KeepEmptyLines: - AtEndOfFile: false - AtStartOfBlock: true - AtStartOfFile: true LambdaBodyIndentation: Signature LineEnding: DeriveLF MacroBlockBegin: "" MacroBlockEnd: "" -MainIncludeChar: Quote MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBinPackProtocolList: Auto @@ -248,7 +217,6 @@ SpacesInLineCommentPrefix: Maximum: -1 SpacesInParens: Never SpacesInParensOptions: - ExceptDoubleParentheses: false InConditionalStatements: false InCStyleCasts: false InEmptyParentheses: false @@ -261,7 +229,6 @@ StatementMacros: - Q_UNUSED - QT_REQUIRE_VERSION TabWidth: 4 -TableGenBreakInsideDAGArg: DontBreak UseTab: ForIndentation VerilogBreakBetweenInstancePorts: true WhitespaceSensitiveMacros: diff --git a/include/net/client.hpp b/include/net/client.hpp index 8f81638..234ca90 100644 --- a/include/net/client.hpp +++ b/include/net/client.hpp @@ -22,7 +22,7 @@ namespace net { class client { - struct sockaddr_in server_address {}; + struct sockaddr_in server_address { }; public: client(std::string hostname, int port) diff --git a/include/net/conn.hpp b/include/net/conn.hpp index b69dac6..6569973 100644 --- a/include/net/conn.hpp +++ b/include/net/conn.hpp @@ -16,9 +16,9 @@ class conn { sockaddr_in conn_addr; socklen_t conn_addr_size; - using handler_list = std::vector<std::function<bool(std::span<uint8_t>)>>; - std::array<handler_list, 256> handlers; - std::map<uint16_t, handler_list> netmodule_handlers; + using handler_list = std::vector<std::function<bool(std::span<uint8_t>)>>; + std::array<handler_list, 256> handlers; + std::map<uint16_t, handler_list> netmodule_handlers; private: packet::packet_header read_header() @@ -29,14 +29,14 @@ private: recv(packet_size); recv(id); - return { id, packet_size - 3}; + return { id, packet_size - 3 }; } public: conn(int sock_fd, sockaddr_in conn_addr, socklen_t conn_addr_size) - : sock_fd(sock_fd) - , conn_addr(conn_addr) - , conn_addr_size(conn_addr_size) + : sock_fd(sock_fd) + , conn_addr(conn_addr) + , conn_addr_size(conn_addr_size) { } @@ -67,9 +67,9 @@ public: ssize_t recv(std::span<uint8_t> data) { - if (data.size_bytes() == 0) { - return 0; - } + if (data.size_bytes() == 0) { + return 0; + } auto bytes = ::recv(sock_fd, data.data(), data.size_bytes(), 0); if (bytes <= 0) { @@ -98,34 +98,36 @@ public: auto [id, len] = read_header(); assert(id == T::packet_id); - if(len > 0) { + if (len > 0) { std::vector<uint8_t> payload(len); recv(payload); decode_packet(payload, t); } } - template <packet::packet P, typename H> - void reg_handler(H h) { - handlers[P::packet_id].push_back([this, h](std::span<uint8_t> payload) { - P t; - if (payload.size() > 0) { - packet::decode_packet(payload, t); + template <packet::packet P, typename H> + void reg_handler(H h) + { + handlers[P::packet_id].push_back([this, h](std::span<uint8_t> payload) { + P t; + if (payload.size() > 0) { + packet::decode_packet(payload, t); } - return h(t); - }); - } - - template <packet::netmodule P, typename H> - void reg_handler(H h) { - netmodule_handlers[P::module_id].push_back([this, h](std::span<uint8_t> payload) { - P t; - if (payload.size() > 0) { - packet::decode_packet(payload, t); + return h(t); + }); + } + + template <packet::netmodule P, typename H> + void reg_handler(H h) + { + netmodule_handlers[P::module_id].push_back([this, h](std::span<uint8_t> payload) { + P t; + if (payload.size() > 0) { + packet::decode_packet(payload, t); } - return h(t); - }); - } + return h(t); + }); + } template <packet::packet T> void send_packet(T p) @@ -137,52 +139,53 @@ public: if (payload.size() > 0) { send(payload); } - } - - bool handle_netmodule(std::span<uint8_t> payload) { + } + + bool handle_netmodule(std::span<uint8_t> payload) + { uint16_t id; std::memcpy(&id, payload.data(), sizeof(id)); id = to_little(id); - if (netmodule_handlers[id].size() <= 0) { + if (netmodule_handlers[id].size() <= 0) { // currently i won't bother implementing netmodules // FIXME: return false - return true; - } - - for (auto &h : handlers[id]) { - if (h(payload.subspan(2)) == true) { - return false; - } - } - return true; + return true; + } + + for (auto& h : handlers[id]) { + if (h(payload.subspan(2)) == true) { + return false; + } + } + return true; } - bool handle() { - auto [id, len] = read_header(); - if (id == 0) { - return false; - } + bool handle() + { + auto [id, len] = read_header(); + if (id == 0) { + return false; + } - std::vector<uint8_t> payload(len); - recv(payload); + std::vector<uint8_t> payload(len); + recv(payload); - //got netmodule - if (id == 82) { + // got netmodule + if (id == 82) { return handle_netmodule(payload); } - if (handlers[id].size() <= 0) { - return false; - } - - for (auto &h : handlers[id]) { - if (h(payload) == true) { - break; - } - } - return true; - } + if (handlers[id].size() <= 0) { + return false; + } + for (auto& h : handlers[id]) { + if (h(payload) == true) { + break; + } + } + return true; + } }; }
\ No newline at end of file diff --git a/include/net/packet.hpp b/include/net/packet.hpp index f2094c0..117fc15 100644 --- a/include/net/packet.hpp +++ b/include/net/packet.hpp @@ -62,7 +62,7 @@ std::vector<uint8_t> encode_packet(T& t) template <compressed_packet T> std::vector<uint8_t> encode_packet(T& t) { - static constexpr auto chunk_size = 1024; + static constexpr auto chunk_size = 1024; std::array<uint8_t, chunk_size> buffer; std::vector<uint8_t> payload; @@ -82,10 +82,10 @@ std::vector<uint8_t> encode_packet(T& t) strm.next_in = raw_payload.data(); do { strm.avail_out = chunk_size; - strm.next_out = reinterpret_cast<Bytef *>(buffer.data()); + strm.next_out = reinterpret_cast<Bytef*>(buffer.data()); ret = deflate(&strm, flush); assert(ret != Z_STREAM_ERROR); - payload.insert(payload.end(), buffer.begin(), buffer.end()-strm.avail_out); + payload.insert(payload.end(), buffer.begin(), buffer.end() - strm.avail_out); } while (strm.avail_out == 0); assert(strm.avail_in == 0); /* all input will be used */ diff --git a/include/net/server.hpp b/include/net/server.hpp index 14a5d1d..9a27a25 100644 --- a/include/net/server.hpp +++ b/include/net/server.hpp @@ -22,11 +22,10 @@ namespace net { class server { - struct sockaddr_in server_address {}; + struct sockaddr_in server_address { }; int sock_fd; public: - server(std::string hostname, int port) { server_address.sin_family = AF_INET; @@ -34,30 +33,34 @@ public: server_address.sin_port = htons(port); } - ~server() { + ~server() + { close(sock_fd); } - void bind() { + void bind() + { sock_fd = socket(AF_INET, SOCK_STREAM, 0); - if (sock_fd < 0) + if (sock_fd < 0) throw std::runtime_error(strerror(errno)); - + int bindStatus = ::bind(sock_fd, (struct sockaddr*)&server_address, sizeof(server_address)); if (bindStatus < 0) throw std::runtime_error(strerror(errno)); } - void listen(int max_connections) { + void listen(int max_connections) + { ::listen(sock_fd, max_connections); } - - conn accept() { + + conn accept() + { sockaddr_in conn_addr {}; socklen_t conn_addr_size = sizeof(conn_addr); - int conn_fd = ::accept(sock_fd, (struct sockaddr *)&conn_addr, &conn_addr_size); - if (conn_fd < 0) + int conn_fd = ::accept(sock_fd, (struct sockaddr*)&conn_addr, &conn_addr_size); + if (conn_fd < 0) throw std::runtime_error(strerror(errno)); return conn { conn_fd, conn_addr, conn_addr_size }; } diff --git a/include/net/types.hpp b/include/net/types.hpp index 9ebc56c..8f0e383 100644 --- a/include/net/types.hpp +++ b/include/net/types.hpp @@ -25,7 +25,7 @@ template <class T> concept packet = requires(T x) { x.packet_id; }; template <class T> -concept compressed_packet = requires(T x) { packet<T> && x.compressed; }; +concept compressed_packet = requires(T x) { packet<T>&& x.compressed; }; template <class T> concept netmodule = requires(T x) { x.module_id; }; @@ -59,7 +59,7 @@ struct rgb { }; struct death_reason { - enum class reason : int { + enum class reason : int { Player = 0, NPC, Projectile, @@ -71,7 +71,7 @@ struct death_reason { }; bitset<8> reasons; - + int16_t player_index; int16_t npc_index; int16_t projectile_index; @@ -126,7 +126,7 @@ void encode_field(std::vector<uint8_t>& data, std::array<T, sz>& f) } template <typename T> -void encode_field(std::vector<uint8_t>& data, std::vector<T> &f) +void encode_field(std::vector<uint8_t>& data, std::vector<T>& f) { for (auto& e : f) { encode_field(data, e); @@ -134,7 +134,7 @@ void encode_field(std::vector<uint8_t>& data, std::vector<T> &f) } template <typename T> -void encode_field(std::vector<uint8_t>& data, std::span<T> &f) +void encode_field(std::vector<uint8_t>& data, std::span<T>& f) { for (auto& e : f) { encode_field(data, e); @@ -142,14 +142,14 @@ void encode_field(std::vector<uint8_t>& data, std::span<T> &f) } template <unsigned int bits> -void encode_field(std::vector<uint8_t>& data, bitset<bits> &f) +void encode_field(std::vector<uint8_t>& data, bitset<bits>& f) { for (auto& e : f) { encode_field(data, e); } } -void encode_field(std::vector<uint8_t>& data, death_reason &f) +void encode_field(std::vector<uint8_t>& data, death_reason& f) { encode_field(data, f.reasons); if (f.reasons[death_reason::reason::Player]) { @@ -199,14 +199,12 @@ ssize_t decode_field(std::span<uint8_t> data, T& f) return sizeof(T); } - ssize_t decode_field(std::span<uint8_t> data, std::vector<uint8_t>& f) -{ +{ f.insert(f.begin(), data.begin(), data.end()); return f.size(); } - template <typename T, std::size_t sz> ssize_t decode_field(std::span<uint8_t> data, std::array<T, sz>& f) { @@ -220,7 +218,7 @@ ssize_t decode_field(std::span<uint8_t> data, std::array<T, sz>& f) return offset; } -ssize_t decode_field(std::span<uint8_t> data, death_reason &f) +ssize_t decode_field(std::span<uint8_t> data, death_reason& f) { int offset = 0; offset += decode_field(data, f.reasons); @@ -372,7 +370,7 @@ struct send_tile_data { static constexpr uint8_t packet_id = 10; static constexpr packet_flag compressed {}; - //FIXME: implement + // FIXME: implement }; struct spawn_player { @@ -480,12 +478,12 @@ struct player_loadout { struct damage_player { static constexpr uint8_t packet_id = 117; - uint8_t client_id; + uint8_t client_id; death_reason reason; - int16_t damage; - uint8_t hit_direction; - uint8_t ty; - int8_t cooldown_counter; + int16_t damage; + uint8_t hit_direction; + uint8_t ty; + int8_t cooldown_counter; }; template <uint8_t id> diff --git a/include/util/bitset.hpp b/include/util/bitset.hpp index 8798a4a..de5bd49 100644 --- a/include/util/bitset.hpp +++ b/include/util/bitset.hpp @@ -4,45 +4,50 @@ #include <cassert> #include <cstdint> -constexpr int bits_ceil(int bits) { - return bits / 8 + (bits % 8 > 0 ? 1 : 0); +constexpr int bits_ceil(int bits) +{ + return bits / 8 + (bits % 8 > 0 ? 1 : 0); } template <unsigned int flags, auto size = bits_ceil(flags)> struct bitset : std::array<std::uint8_t, size> { - using array_type = std::array<std::uint8_t, size>; + using array_type = std::array<std::uint8_t, size>; private: - struct bit_ref { - bitset<flags> &a; - int referenced_byte; - uint8_t referenced_bit_mask; - - bool operator=(bool b) { - if (b) - a.at(referenced_byte) |= referenced_bit_mask; - else - a.at(referenced_byte) &= ~referenced_bit_mask; - return b; - } - - constexpr operator bool() const { - return (a.at(referenced_byte) & referenced_bit_mask) > 0; - } - }; + struct bit_ref { + bitset<flags>& a; + int referenced_byte; + uint8_t referenced_bit_mask; + + bool operator=(bool b) + { + if (b) + a.at(referenced_byte) |= referenced_bit_mask; + else + a.at(referenced_byte) &= ~referenced_bit_mask; + return b; + } + + constexpr operator bool() const + { + return (a.at(referenced_byte) & referenced_bit_mask) > 0; + } + }; public: - template<class T> - constexpr bool operator [](T idx) const { - unsigned int i = (unsigned int)(idx); - assert(i < flags); - return (array_type::at(i/8) & (1 << (i%8))) > 0; - } - - template<class T> - bit_ref operator[](T idx) { - unsigned int i = (unsigned int)(idx); - assert(i < flags); - return bit_ref(*this, i/8, (1 << (i%8))); - } + template <class T> + constexpr bool operator[](T idx) const + { + unsigned int i = (unsigned int)(idx); + assert(i < flags); + return (array_type::at(i / 8) & (1 << (i % 8))) > 0; + } + + template <class T> + bit_ref operator[](T idx) + { + unsigned int i = (unsigned int)(idx); + assert(i < flags); + return bit_ref(*this, i / 8, (1 << (i % 8))); + } };
\ No newline at end of file diff --git a/include/version.hpp b/include/version.hpp index 0371595..b5f4856 100644 --- a/include/version.hpp +++ b/include/version.hpp @@ -1,3 +1,3 @@ #pragma once -constexpr auto max_buffs = 44;
\ No newline at end of file +constexpr auto max_buffs = 44;
\ No newline at end of file diff --git a/src/kill_proxy.cpp b/src/kill_proxy.cpp index a2a2e27..82723e1 100644 --- a/src/kill_proxy.cpp +++ b/src/kill_proxy.cpp @@ -26,11 +26,13 @@ using net::packet::operator""_ns; template <typename T, T... ints> -void forward_packets(net::conn &dst, net::conn &src, std::integer_sequence<T, ints...> int_seq) { - (src.reg_handler<net::packet::raw<ints>>([&dst](auto &a) { +void forward_packets(net::conn& dst, net::conn& src, std::integer_sequence<T, ints...> int_seq) +{ + (src.reg_handler<net::packet::raw<ints+1>>([&dst](auto& a) { dst.send_packet(a); return true; - }), ...); + }), + ...); } int main(int argc, char* argv[]) @@ -50,25 +52,25 @@ int main(int argc, char* argv[]) return false; }); - proxy_conn.reg_handler<net::packet::damage_player>([&slot, &server_conn](auto &dmg) { + proxy_conn.reg_handler<net::packet::damage_player>([&slot, &server_conn](auto& dmg) { if (dmg.client_id != slot) { dmg.damage *= 100; dmg.reason.reasons.at(0) = 0; dmg.reason.reasons[net::packet::death_reason::reason::Custom] = true; dmg.reason.custom = "killed by server insecurity"; - + server_conn.send_packet(dmg); return true; } return false; }); - forward_packets(server_conn, proxy_conn, std::make_integer_sequence<uint8_t, 255>{}); - forward_packets(proxy_conn, server_conn, std::make_integer_sequence<uint8_t, 255>{}); + forward_packets(server_conn, proxy_conn, std::make_integer_sequence<uint8_t, 255> {}); + forward_packets(proxy_conn, server_conn, std::make_integer_sequence<uint8_t, 255> {}); - auto handle_loop = [] (net::conn &c) { - for(;;) { + auto handle_loop = [](net::conn& c) { + for (;;) { c.handle(); } }; |
