add protocol version check

This commit is contained in:
MihailRis 2025-10-09 02:13:03 +03:00
parent d861595f78
commit e83b3b3ce1
2 changed files with 35 additions and 2 deletions

View File

@ -17,6 +17,27 @@ ClientConnection::~ClientConnection() {
} }
} }
bool ClientConnection::initiate(network::ReadableConnection* connection) {
if (connection->available() < 8) {
return false;
}
char buffer[8] {};
char expected[8] {};
std::memcpy(expected, VCDBG_MAGIC, sizeof(VCDBG_MAGIC));
expected[6] = VCDBG_VERSION >> 8;
expected[7] = VCDBG_VERSION & 0xFF;
connection->recv(buffer, sizeof(VCDBG_MAGIC));
connection->send(expected, sizeof(VCDBG_MAGIC));
if (std::memcmp(expected, buffer, sizeof(VCDBG_MAGIC)) == 0) {
initiated = true;
return false;
} else {
connection->close(true);
return true;
}
}
std::string ClientConnection::read() { std::string ClientConnection::read() {
auto connection = dynamic_cast<network::ReadableConnection*>( auto connection = dynamic_cast<network::ReadableConnection*>(
network.getConnection(this->connection, true) network.getConnection(this->connection, true)
@ -24,6 +45,11 @@ std::string ClientConnection::read() {
if (connection == nullptr) { if (connection == nullptr) {
return ""; return "";
} }
if (!initiated) {
if (initiate(connection)) {
return "";
}
}
if (messageLength == 0) { if (messageLength == 0) {
if (connection->available() >= sizeof(int32_t)) { if (connection->available() >= sizeof(int32_t)) {
int32_t length = 0; int32_t length = 0;
@ -166,7 +192,7 @@ bool DebuggingServer::update() {
bool DebuggingServer::performCommand( bool DebuggingServer::performCommand(
const std::string& type, const dv::value& map const std::string& type, const dv::value& map
) { ) {
if (type == "connect" && !connectionEstablished) { if (!connectionEstablished && type == "connect") {
map.at("disconnect-action").get(disconnectAction); map.at("disconnect-action").get(disconnectAction);
connectionEstablished = true; connectionEstablished = true;
logger.info() << "client connection established"; logger.info() << "client connection established";
@ -275,8 +301,9 @@ void DebuggingServer::sendValue(
} }
void DebuggingServer::setClient(u64id_t client) { void DebuggingServer::setClient(u64id_t client) {
this->connection = connection =
std::make_unique<ClientConnection>(engine.getNetwork(), client); std::make_unique<ClientConnection>(engine.getNetwork(), client);
connectionEstablished = false;
} }
std::vector<DebuggingEvent> DebuggingServer::pullEvents() { std::vector<DebuggingEvent> DebuggingServer::pullEvents() {

View File

@ -21,6 +21,9 @@ namespace dv {
class Engine; class Engine;
namespace devtools { namespace devtools {
inline constexpr const char VCDBG_MAGIC[8] = "vc-dbg\0";
inline constexpr int VCDBG_VERSION = 1;
class ClientConnection { class ClientConnection {
public: public:
ClientConnection(network::Network& network, u64id_t connection) ClientConnection(network::Network& network, u64id_t connection)
@ -37,6 +40,9 @@ namespace devtools {
network::Network& network; network::Network& network;
size_t messageLength = 0; size_t messageLength = 0;
u64id_t connection; u64id_t connection;
bool initiated = false;
bool initiate(network::ReadableConnection* connection);
}; };
enum class DebuggingEventType { enum class DebuggingEventType {