From a0fc672816105a3245aef7ac6cf36210bdb4d728 Mon Sep 17 00:00:00 2001 From: Niek van der Maas Date: Tue, 1 Oct 2019 00:52:28 +0200 Subject: [PATCH] Properly handle telnet negotiation (#1927) * Get rid of _telnetFirst (assumption that first line is always telnet renegotiation) Discussed in: #1920 (comment) --- code/espurna/telnet.ino | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index 063575e2..3ab088d0 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -10,6 +10,9 @@ Parts of the code have been borrowed from Thomas Sarlandie's NetServer #if TELNET_SUPPORT +#define TELNET_IAC 0xFF +#define TELNET_XEOF 0xEC + #if TELNET_SERVER == TELNET_SERVER_WIFISERVER #include WiFiServer _telnetServer = WiFiServer(TELNET_PORT); @@ -20,8 +23,6 @@ Parts of the code have been borrowed from Thomas Sarlandie's NetServer std::unique_ptr _telnetClients[TELNET_MAX_CLIENTS]; #endif -bool _telnetFirst = true; - bool _telnetAuth = TELNET_AUTHENTICATION; bool _telnetClientsAuth[TELNET_MAX_CLIENTS]; @@ -81,21 +82,15 @@ bool _telnetWrite(unsigned char clientId, const char * message) { } void _telnetData(unsigned char clientId, void *data, size_t len) { - // Skip first message since it's always garbage - if (_telnetFirst) { - _telnetFirst = false; - return; - } - // Capture close connection char * p = (char *) data; - // C-d is sent as two bytes (sometimes repeating) - if (len >= 2) { - if ((p[0] == 0xFF) && (p[1] == 0xEC)) { + if ((len >= 2) && (p[0] == TELNET_IAC)) { + // C-d is sent as two bytes (sometimes repeating) + if (p[1] == TELNET_XEOF) { _telnetDisconnect(clientId); - return; } + return; // Ignore telnet negotiation } if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) { @@ -153,7 +148,6 @@ void _telnetNotifyConnected(unsigned char i) { } #endif - _telnetFirst = true; wifiReconnectCheck(); }