Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add telnet service

Geoa45ae065d4e6
diff --git a/src/Services/TelnetService.cpp b/src/Services/TelnetService.cpp
new file mode 100644
index 00000000..fd5b22f8
--- /dev/null
+++ b/src/Services/TelnetService.cpp
@@ -0,0 +1,150 @@
+#include "TelnetService.h"
+
+bool TelnetService::connectTo(const std::string& host, uint16_t port, uint32_t recvTimeoutMs) {
+ close(); // just in case
+
+ // DNS resolution
+ struct addrinfo hints {};
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ struct addrinfo* res = nullptr;
+ const std::string portStr = std::to_string(port);
+ int gai = getaddrinfo(host.c_str(), portStr.c_str(), &hints, &res);
+ if (gai != 0 || !res) {
+ _lastError = "DNS failed";
+ return false;
+ }
+
+ // Socket creation
+ _sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+ if (_sock < 0) {
+ _lastError = "socket failed";
+ freeaddrinfo(res);
+ return false;
+ }
+
+ // Timeout for recv
+ struct timeval tv;
+ tv.tv_sec = static_cast<time_t>(recvTimeoutMs / 1000);
+ tv.tv_usec = static_cast<suseconds_t>((recvTimeoutMs % 1000) * 1000);
+ setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+
+ // Connect
+ if (::connect(_sock, res->ai_addr, res->ai_addrlen) != 0) {
+ _lastError = "connect failed";
+ ::close(_sock);
+ _sock = -1;
+ freeaddrinfo(res);
+ return false;
+ }
+
+ freeaddrinfo(res);
+ _rx.reserve(512);
+ return true;
+}
+
+void TelnetService::close() {
+ if (_sock >= 0) {
+ ::shutdown(_sock, SHUT_RDWR);
+ ::close(_sock);
+ _sock = -1;
+ }
+ _rx.clear();
+}
+
+int TelnetService::sendAll(int s, const void* data, size_t len) {
+ const uint8_t* p = static_cast<const uint8_t*>(data);
+ size_t sentTotal = 0;
+ while (sentTotal < len) {
+ size_t n = ::send(s, p + sentTotal, len - sentTotal, 0);
+ if (n <= 0) return -1;
+ sentTotal += n;
+ }
+ return static_cast<int>(sentTotal);
+}
+
+bool TelnetService::writeChar(char c) {
+ if (_sock < 0) return false;
+ if (c == '\n' || c == '\r') {
+ static const char crlf[2] = {'\r','\n'};
+ return sendAll(_sock, crlf, 2) == 2;
+ }
+ return sendAll(_sock, &c, 1) == 1;
+}
+
+int TelnetService::writeRaw(const char* data, size_t len) {
+ if (_sock < 0 || !data || !len) return -1;
+ return sendAll(_sock, data, len);
+}
+
+bool TelnetService::writeLine(const std::string& line) {
+ if (_sock < 0) return false;
+ static const char crlf[2] = {'\r','\n'};
+ return sendAll(_sock, line.c_str(), line.size()) == (int)line.size()
+ && sendAll(_sock, crlf, 2) == 2;
+}
+
+void TelnetService::sendIAC(uint8_t cmd, uint8_t opt) {
+ if (_sock < 0) return;
+ const uint8_t pkt[3] = { IAC, cmd, opt };
+ sendAll(_sock, pkt, sizeof(pkt));
+}
+
+void TelnetService::doTelnetNegotiation(const uint8_t* buf, int len, std::string& out) {
+ for (int i = 0; i < len; ++i) {
+ uint8_t b = buf[i];
+ if (b != IAC) { out.push_back(static_cast<char>(b)); continue; }
+
+ if (i + 1 >= len) break;
+ uint8_t cmd = buf[++i];
+
+ if (cmd == IAC) { out.push_back(static_cast<char>(IAC)); continue; } // 0xFF
+
+ if (cmd == WILL || cmd == WONT || cmd == DO || cmd == DONT) {
+ if (i + 1 >= len) break;
+ uint8_t opt = buf[++i];
+
+ if (cmd == WILL) {
+ // Server requests to enable <opt>
+ if (opt == OPT_ECHO || opt == OPT_SGA || opt == OPT_BINARY) sendIAC(DO, opt);
+ else sendIAC(DONT, opt);
+ } else if (cmd == DO) {
+ // Server asks us to enable <opt> -> refuse (WONT)
+ sendIAC(WONT, opt);
+ }
+ // WONT / DONT
+ continue;
+ }
+
+ if (cmd == SB) {
+ // Subnegotiation until IAC SE
+ while (i + 1 < len) {
+ uint8_t x = buf[++i];
+ if (x == IAC && i + 1 < len && buf[i+1] == SE) { i++; break; }
+ }
+ continue;
+ }
+
+ // Other IAC commands: ignored for now
+ }
+}
+
+void TelnetService::poll() {
+ if (_sock < 0) return;
+
+ uint8_t buf[512];
+ int n = ::recv(_sock, reinterpret_cast<char*>(buf), sizeof(buf), MSG_DONTWAIT);
+ if (n <= 0) return;
+
+ std::string out;
+ out.reserve(static_cast<size_t>(n));
+ doTelnetNegotiation(buf, n, out);
+
+ if (!out.empty()) _rx.append(out);
+}
+
+std::string TelnetService::readOutputNonBlocking() {
+ std::string out;
+ out.swap(_rx);
+ return out;
+}
diff --git a/src/Services/TelnetService.h b/src/Services/TelnetService.h
new file mode 100644
index 00000000..0855b62a
--- /dev/null
+++ b/src/Services/TelnetService.h
@@ -0,0 +1,62 @@
+#pragma once
+#include <cstdint>
+#include <string>
+
+extern "C" {
+ #include <lwip/sockets.h>
+ #include <lwip/inet.h>
+ #include <lwip/netdb.h>
+}
+
+class TelnetService {
+public:
+ // Connect to a Telnet server (synchronous)
+ bool connectTo(const std::string& host, uint16_t port, uint32_t recvTimeoutMs = 3000);
+
+ void close();
+ bool isConnected() const { return _sock >= 0; }
+
+ // Send a character
+ bool writeChar(char c);
+
+ // Send raw data
+ int writeRaw(const char* data, size_t len);
+
+ // Send a line
+ bool writeLine(const std::string& line);
+
+ // Read the socket
+ void poll();
+
+ // Retrieve and clear the text buffer accumulated by poll()
+ std::string readOutputNonBlocking();
+
+ // Get the last error message if connection failed
+ const std::string& lastError() const { return _lastError; }
+
+private:
+ // Telnet constants
+ static constexpr uint8_t IAC = 255;
+ static constexpr uint8_t DONT = 254;
+ static constexpr uint8_t DO = 253;
+ static constexpr uint8_t WONT = 252;
+ static constexpr uint8_t WILL = 251;
+ static constexpr uint8_t SB = 250;
+ static constexpr uint8_t SE = 240;
+
+ // Options
+ static constexpr uint8_t OPT_BINARY = 0;
+ static constexpr uint8_t OPT_ECHO = 1;
+ static constexpr uint8_t OPT_SGA = 3;
+
+ int sendAll(int s, const void* data, size_t len);
+
+ void doTelnetNegotiation(const uint8_t* buf, int len, std::string& out);
+
+ void sendIAC(uint8_t cmd, uint8_t opt);
+
+private:
+ int _sock = -1;
+ std::string _rx;
+ std::string _lastError;
+};