extern "C" { #include } #include #include #include #include #include #include #include const std::string internetPkgMsg = "internetPkg"; const std::string tunAddressMsg = "tunAddress"; const std::string localRouteMsg = "localRoute"; MessageHeader::MessageHeader(const uint32_t ipSize) { pSize = ipSize; } MessageHeader::MessageHeader(void) { pSize = 0; } std::string MessageHeader::Encode() { std::string buffer, tmp; auto magic = htonl(msgMagic); tmp = std::string(reinterpret_cast(&magic), sizeof(magic)); buffer.append(tmp); auto size = htonl(pSize); tmp = std::string(reinterpret_cast(&size), sizeof(size)); buffer.append(tmp); return buffer; } std::expected MessageHeader::Decode(const std::string rawHeader) { uint32_t tmp; std::memcpy(&tmp, rawHeader.data(), sizeof(uint32_t)); auto magic = ntohl(tmp); if (magic != msgMagic) { return std::unexpected(std::format("Wrong magic code {:x}", magic)); } std::memcpy(&tmp, rawHeader.data() + sizeof(uint32_t), sizeof(uint32_t)); pSize = ntohl(tmp); return {}; } uint32_t MessageHeader::PayloadSize() { return pSize; }