Repo-Init
Loading...
Searching...
No Matches
TelnetServer.cpp File Reference
#include "telnet/TelnetServer.hpp"
#include "Version.h"
#include "utils/ErrorHelpers.hpp"
#include "utils/Hasher.hpp"
#include <spdlog/spdlog.h>
#include <algorithm>
#include <array>
#include <ctime>
#include <format>
#include <iomanip>
#include <sstream>
#include <string>
#include <utility>
#include <sys/time.h>
Include dependency graph for TelnetServer.cpp:

Go to the source code of this file.

Functions

const std::string ANSI_FG_BLACK ("\x1b[30m")
const std::string ANSI_FG_RED ("\x1b[31m")
const std::string ANSI_FG_GREEN ("\x1b[32m")
const std::string ANSI_FG_YELLOW ("\x1b[33m")
const std::string ANSI_FG_BLUE ("\x1b[34m")
const std::string ANSI_FG_MAGENTA ("\x1b[35m")
const std::string ANSI_FG_CYAN ("\x1b[36m")
const std::string ANSI_FG_WHITE ("\x1b[37m")
const std::string ANSI_FG_DEFAULT ("\x1b[39m")
const std::string ANSI_BG_BLACK ("\x1b[40m")
const std::string ANSI_BG_RED ("\x1b[41m")
const std::string ANSI_BG_GREEN ("\x1b[42m")
const std::string ANSI_BG_YELLOW ("\x1b[43m")
const std::string ANSI_BG_BLUE ("\x1b[44m")
const std::string ANSI_BG_MAGENTA ("\x1b[45m")
const std::string ANSI_BG_CYAN ("\x1b[46m")
const std::string ANSI_BG_WHITE ("\x1b[47m")
const std::string ANSI_BG_DEFAULT ("\x1b[49m")
const std::string ANSI_BOLD_ON ("\x1b[1m")
const std::string ANSI_BOLD_OFF ("\x1b[22m")
const std::string ANSI_ITALICS_ON ("\x1b[3m")
const std::string ANSI_ITALCIS_OFF ("\x1b[23m")
const std::string ANSI_UNDERLINE_ON ("\x1b[4m")
const std::string ANSI_UNDERLINE_OFF ("\x1b[24m")
const std::string ANSI_INVERSE_ON ("\x1b[7m")
const std::string ANSI_INVERSE_OFF ("\x1b[27m")
const std::string ANSI_STRIKETHROUGH_ON ("\x1b[9m")
const std::string ANSI_STRIKETHROUGH_OFF ("\x1b[29m")
const std::string ANSI_ERASE_LINE ("\x1b[2K")
const std::string ANSI_ERASE_SCREEN ("\x1b[2J")
const std::string ANSI_ARROW_UP ("\x1b\x5b\x41")
const std::string ANSI_ARROW_DOWN ("\x1b\x5b\x42")
const std::string ANSI_ARROW_RIGHT ("\x1b\x5b\x43")
const std::string ANSI_ARROW_LEFT ("\x1b\x5b\x44")
const std::string ANSI_DOUBLE_HORIZONTAL_TAB ("\t\t")
const std::string ANSI_HORIZONTAL_TAB ("\t")
const std::string TELNET_ERASE_LINE ("\xff\xf8")
const std::string TELNET_CLEAR_SCREEN ("\033[2J")
void TelnetPrintAvailableCommands (const SP_TelnetSession &session)
void TelnetConnectedCallback (const SP_TelnetSession &session)
bool TelnetMessageCallback (const SP_TelnetSession &session, const std::string &line)
std::string TelnetTabCallback (const SP_TelnetSession &session, std::string_view line)

Variables

constexpr int INVALID_SOCKET = -1
constexpr int DEFAULT_BUFLEN = 512
constexpr int TELNET_TIMEOUT = 120
constexpr int MAX_AVAILABLE_SESSION = 5
constexpr int TELNET_HISTORY_LIMIT = 50
constexpr int SLEEP_INTERVAL_MS = 50
constexpr int KEY_WIDTH = 30
constexpr int VAL_WIDTH = 15
constexpr int ASCII_LF = 0x0A
constexpr int ASCII_NULL = 0x00
constexpr int ASCII_NBSP = 0xFF
const std::vector< std::pair< std::string, std::string > > telnetCommands

Function Documentation

◆ ANSI_ARROW_DOWN()

const std::string ANSI_ARROW_DOWN ( "\x1b\x5b\x42" )
Here is the caller graph for this function:

◆ ANSI_ARROW_LEFT()

const std::string ANSI_ARROW_LEFT ( "\x1b\x5b\x44" )
Here is the caller graph for this function:

◆ ANSI_ARROW_RIGHT()

const std::string ANSI_ARROW_RIGHT ( "\x1b\x5b\x43" )
Here is the caller graph for this function:

◆ ANSI_ARROW_UP()

const std::string ANSI_ARROW_UP ( "\x1b\x5b\x41" )
Here is the caller graph for this function:

◆ ANSI_BG_BLACK()

const std::string ANSI_BG_BLACK ( )

◆ ANSI_BG_BLUE()

const std::string ANSI_BG_BLUE ( )

◆ ANSI_BG_CYAN()

const std::string ANSI_BG_CYAN ( )

◆ ANSI_BG_DEFAULT()

const std::string ANSI_BG_DEFAULT ( )

◆ ANSI_BG_GREEN()

const std::string ANSI_BG_GREEN ( )

◆ ANSI_BG_MAGENTA()

const std::string ANSI_BG_MAGENTA ( )

◆ ANSI_BG_RED()

const std::string ANSI_BG_RED ( )

◆ ANSI_BG_WHITE()

const std::string ANSI_BG_WHITE ( )

◆ ANSI_BG_YELLOW()

const std::string ANSI_BG_YELLOW ( )

◆ ANSI_BOLD_OFF()

const std::string ANSI_BOLD_OFF ( )

◆ ANSI_BOLD_ON()

const std::string ANSI_BOLD_ON ( )

◆ ANSI_DOUBLE_HORIZONTAL_TAB()

const std::string ANSI_DOUBLE_HORIZONTAL_TAB ( "\t\t" )

◆ ANSI_ERASE_LINE()

const std::string ANSI_ERASE_LINE ( )
Here is the caller graph for this function:

◆ ANSI_ERASE_SCREEN()

const std::string ANSI_ERASE_SCREEN ( )

◆ ANSI_FG_BLACK()

const std::string ANSI_FG_BLACK ( )

◆ ANSI_FG_BLUE()

const std::string ANSI_FG_BLUE ( )

◆ ANSI_FG_CYAN()

const std::string ANSI_FG_CYAN ( )

◆ ANSI_FG_DEFAULT()

const std::string ANSI_FG_DEFAULT ( )

◆ ANSI_FG_GREEN()

const std::string ANSI_FG_GREEN ( )

◆ ANSI_FG_MAGENTA()

const std::string ANSI_FG_MAGENTA ( )

◆ ANSI_FG_RED()

const std::string ANSI_FG_RED ( )

◆ ANSI_FG_WHITE()

const std::string ANSI_FG_WHITE ( )

◆ ANSI_FG_YELLOW()

const std::string ANSI_FG_YELLOW ( )

◆ ANSI_HORIZONTAL_TAB()

const std::string ANSI_HORIZONTAL_TAB ( "\t" )

◆ ANSI_INVERSE_OFF()

const std::string ANSI_INVERSE_OFF ( )

◆ ANSI_INVERSE_ON()

const std::string ANSI_INVERSE_ON ( )

◆ ANSI_ITALCIS_OFF()

const std::string ANSI_ITALCIS_OFF ( )

◆ ANSI_ITALICS_ON()

const std::string ANSI_ITALICS_ON ( )

◆ ANSI_STRIKETHROUGH_OFF()

const std::string ANSI_STRIKETHROUGH_OFF ( )

◆ ANSI_STRIKETHROUGH_ON()

const std::string ANSI_STRIKETHROUGH_ON ( )

◆ ANSI_UNDERLINE_OFF()

const std::string ANSI_UNDERLINE_OFF ( )

◆ ANSI_UNDERLINE_ON()

const std::string ANSI_UNDERLINE_ON ( )

◆ TELNET_CLEAR_SCREEN()

const std::string TELNET_CLEAR_SCREEN ( )
Here is the caller graph for this function:

◆ TELNET_ERASE_LINE()

const std::string TELNET_ERASE_LINE ( "\xff\xf8" )

◆ TelnetConnectedCallback()

void TelnetConnectedCallback ( const SP_TelnetSession & session)

Telnet session connection start callback

Parameters
[in]sessionHandle to session

Definition at line 783 of file TelnetServer.cpp.

784{
785 session->sendLine("\r\n"
786 "𝑲𝒆𝒆𝒑 𝒚𝒐𝒖𝒓 𝒆𝒚𝒆𝒔 𝒐𝒏 𝒕𝒉𝒆 𝒔𝒕𝒂𝒓𝒔 "
787 "𝒂𝒏𝒅 𝒚𝒐𝒖𝒓 𝒇𝒆𝒆𝒕 𝒐𝒏 𝒕𝒉𝒆 𝒈𝒓𝒐𝒖𝒏𝒅 "
788 "\r\n");
790}
void TelnetPrintAvailableCommands(const SP_TelnetSession &session)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TelnetMessageCallback()

bool TelnetMessageCallback ( const SP_TelnetSession & session,
const std::string & line )

Telnet session message received callback

Parameters
[in]sessionHandle to session
[in]lineReceived message

Definition at line 792 of file TelnetServer.cpp.

793{
794 spdlog::trace("Received message {}", line);
795
796 // Send received message for user terminal
797 session->sendLine(line);
798
799 if (line.empty())
800 {
801 return true;
802 }
803
804 // Process received message
805 switch (constHasher(line.c_str()))
806 {
807 case constHasher("Test Message"):
808 session->sendLine("OK");
809 return true;
810 case constHasher("help"):
812 return true;
813 case constHasher("disable log"):
814 session->sendLine("Default log mode enabled");
815#ifdef NDEBUG
816 spdlog::set_level(spdlog::level::warn);
817#else
818 spdlog::set_level(spdlog::level::info);
819#endif
820 return true;
821 case constHasher("disable log all"): // Internal use only
822 session->sendLine("Disabling all logs");
823 spdlog::set_level(spdlog::level::off);
824 return true;
825 case constHasher("enable log v"):
826 session->sendLine("Info log mode enabled");
827 spdlog::set_level(spdlog::level::info);
828 return true;
829 case constHasher("enable log vv"):
830 session->sendLine("Debug log mode enabled");
831 spdlog::set_level(spdlog::level::debug);
832 return true;
833 case constHasher("enable log vvv"):
834 session->sendLine("Trace log mode enabled");
835 spdlog::set_level(spdlog::level::trace);
836 return true;
837 case constHasher("ping"):
838 session->sendLine("pong");
839 return true;
840 case constHasher("version"):
841 session->sendLine(PROJECT_FULL_VERSION_STRING);
842 return true;
843 case constHasher("clear"):
844 session->sendLine(TELNET_CLEAR_SCREEN);
845 return true;
846 case constHasher("status"):
847 for (const auto &[service, statusFlag] : vCheckFlag)
848 {
849 session->sendLine(std::format("{:.<{}}{:.>{}} ", service + " ", KEY_WIDTH,
850 (statusFlag->test() ? " OK" : " Not Active"), VAL_WIDTH));
851 }
852 return true;
853 /* ################################################################################### */
854 /* ############################# MAKE MODIFICATIONS HERE ############################# */
855 /* ################################################################################### */
856
857 /* ################################################################################### */
858 /* ################################ END MODIFICATIONS ################################ */
859 /* ################################################################################### */
860 case constHasher("quit"):
861 session->sendLine("Closing connection");
862 session->sendLine("Goodbye!");
863 session->markTimeout();
864 return true;
865 default:
866 session->sendLine("Unknown command received");
867 return false;
868 }
869}
std::vector< std::pair< std::string, std::shared_ptr< std::atomic_flag > > > vCheckFlag
Global variable to check if the servers are running.
constexpr size_t constHasher(const char *s)
Definition Hasher.hpp:13
constexpr int VAL_WIDTH
constexpr int KEY_WIDTH
const std::string TELNET_CLEAR_SCREEN("\033[2J")
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TelnetPrintAvailableCommands()

void TelnetPrintAvailableCommands ( const SP_TelnetSession & session)

Print available commands to the session

Parameters
[in]sessionHandle to session

Definition at line 767 of file TelnetServer.cpp.

768{
769 // Print available commands
770 session->sendLine("");
771 session->sendLine("Available commands:");
772 session->sendLine("");
773 for (const auto &[command, info] : telnetCommands)
774 {
775 std::array<char, BUFSIZ> buffer{'\0'};
776 if (std::format_to_n(buffer.data(), BUFSIZ, "{:<25} : {}", command, info).size > 0)
777 {
778 session->sendLine(buffer.data());
779 }
780 }
781}
const std::vector< std::pair< std::string, std::string > > telnetCommands
Here is the caller graph for this function:

◆ TelnetTabCallback()

std::string TelnetTabCallback ( const SP_TelnetSession & session,
std::string_view line )

Telnet session TAB received callback

Parameters
[in]sessionHandle to session
[in]lineReceived message
Returns
std::string Command to complete

Definition at line 871 of file TelnetServer.cpp.

872{
873 std::string retval;
874
875 size_t ctr = 0;
876 std::ostringstream sStream;
877 for (const auto &[command, info] : telnetCommands)
878 {
879 if (command.starts_with(line))
880 {
881 ++ctr;
882 retval = command;
883 sStream << command << std::setw(KEY_WIDTH);
884 }
885 }
886 // Send suggestions if found any. If there is only one command retval will invoke completion
887 if (ctr != 1 && (!sStream.str().empty()))
888 {
889 session->sendLine(sStream.str());
890 retval = "";
891 }
892
893 return retval;
894}
Here is the caller graph for this function:

Variable Documentation

◆ ASCII_LF

int ASCII_LF = 0x0A
constexpr

Definition at line 38 of file TelnetServer.cpp.

◆ ASCII_NBSP

int ASCII_NBSP = 0xFF
constexpr

Definition at line 40 of file TelnetServer.cpp.

◆ ASCII_NULL

int ASCII_NULL = 0x00
constexpr

Definition at line 39 of file TelnetServer.cpp.

◆ DEFAULT_BUFLEN

int DEFAULT_BUFLEN = 512
constexpr

Definition at line 23 of file TelnetServer.cpp.

◆ INVALID_SOCKET

int INVALID_SOCKET = -1
constexpr

Definition at line 21 of file TelnetServer.cpp.

◆ KEY_WIDTH

int KEY_WIDTH = 30
constexpr

Definition at line 34 of file TelnetServer.cpp.

◆ MAX_AVAILABLE_SESSION

int MAX_AVAILABLE_SESSION = 5
constexpr

Definition at line 27 of file TelnetServer.cpp.

◆ SLEEP_INTERVAL_MS

int SLEEP_INTERVAL_MS = 50
constexpr

Definition at line 31 of file TelnetServer.cpp.

◆ TELNET_HISTORY_LIMIT

int TELNET_HISTORY_LIMIT = 50
constexpr

Definition at line 29 of file TelnetServer.cpp.

◆ TELNET_TIMEOUT

int TELNET_TIMEOUT = 120
constexpr

Definition at line 25 of file TelnetServer.cpp.

◆ telnetCommands

const std::vector<std::pair<std::string, std::string> > telnetCommands
Initial value:
= {
{"clear", "Clears the terminal screen"},
{"disable log", "Resets logger level"},
{"enable log", R"(Enable specified logger level. Level can be "v" (info), "vv" (debug) and "vvv" (trace))"},
{"help", "Prints available commands"},
{"ping", "Pings the server"},
{"status", "Checks the internal status"},
{"version", "Displays the current version"},
{"quit", "Ends the connection"}}

Definition at line 43 of file TelnetServer.cpp.

43 {
44 {"clear", "Clears the terminal screen"},
45 {"disable log", "Resets logger level"},
46 {"enable log", R"(Enable specified logger level. Level can be "v" (info), "vv" (debug) and "vvv" (trace))"},
47 {"help", "Prints available commands"},
48 {"ping", "Pings the server"},
49 {"status", "Checks the internal status"},
50 {"version", "Displays the current version"},
51 /* ################################################################################### */
52 /* ############################# MAKE MODIFICATIONS HERE ############################# */
53 /* ################################################################################### */
54
55 /* ################################################################################### */
56 /* ################################ END MODIFICATIONS ################################ */
57 /* ################################################################################### */
58 {"quit", "Ends the connection"}};

◆ VAL_WIDTH

int VAL_WIDTH = 15
constexpr

Definition at line 35 of file TelnetServer.cpp.