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 <array>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
#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 780 of file TelnetServer.cpp.

781{
782 session->sendLine("\r\n"
783 "𝑲𝒆𝒆𝒑 𝒚𝒐𝒖𝒓 𝒆𝒚𝒆𝒔 𝒐𝒏 𝒕𝒉𝒆 𝒔𝒕𝒂𝒓𝒔 "
784 "𝒂𝒏𝒅 𝒚𝒐𝒖𝒓 𝒇𝒆𝒆𝒕 𝒐𝒏 𝒕𝒉𝒆 𝒈𝒓𝒐𝒖𝒏𝒅 "
785 "\r\n");
787}
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 789 of file TelnetServer.cpp.

790{
791 spdlog::trace("Received message {}", line);
792
793 // Send received message for user terminal
794 session->sendLine(line);
795
796 if (line.empty())
797 {
798 return true;
799 }
800
801 // Process received message
802 switch (constHasher(line.c_str()))
803 {
804 case constHasher("Test Message"):
805 session->sendLine("OK");
806 return true;
807 case constHasher("help"):
809 return true;
810 case constHasher("disable log"):
811 session->sendLine("Default log mode enabled");
812 spdlog::set_level(spdlog::level::info);
813 return true;
814 case constHasher("disable log all"): // Internal use only
815 session->sendLine("Disabling all logs");
816 spdlog::set_level(spdlog::level::off);
817 return true;
818 case constHasher("enable log v"):
819 session->sendLine("Info log mode enabled");
820 spdlog::set_level(spdlog::level::info);
821 return true;
822 case constHasher("enable log vv"):
823 session->sendLine("Debug log mode enabled");
824 spdlog::set_level(spdlog::level::debug);
825 return true;
826 case constHasher("enable log vvv"):
827 session->sendLine("Trace log mode enabled");
828 spdlog::set_level(spdlog::level::trace);
829 return true;
830 case constHasher("ping"):
831 session->sendLine("pong");
832 return true;
833 case constHasher("version"):
834 session->sendLine(PROJECT_FULL_VERSION_STRING);
835 return true;
836 case constHasher("clear"):
837 session->sendLine(TELNET_CLEAR_SCREEN);
838 return true;
839 case constHasher("status"):
840 for (const auto &[service, statusFlag] : vCheckFlag)
841 {
842 std::ostringstream oss;
843 oss << std::left << std::setfill('.') << std::setw(KEY_WIDTH) << service + " " << std::setw(VAL_WIDTH)
844 << std::right << (statusFlag->_M_i ? " OK" : " Not Active");
845 session->sendLine(oss.str());
846 }
847 return true;
848 /* ################################################################################### */
849 /* ############################# MAKE MODIFICATIONS HERE ############################# */
850 /* ################################################################################### */
851
852 /* ################################################################################### */
853 /* ################################ END MODIFICATIONS ################################ */
854 /* ################################################################################### */
855 case constHasher("quit"):
856 session->sendLine("Closing connection");
857 session->sendLine("Goodbye!");
858 session->markTimeout();
859 return true;
860 default:
861 session->sendLine("Unknown command received");
862 return false;
863 }
864}
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)

Definition at line 764 of file TelnetServer.cpp.

765{
766 // Print available commands
767 session->sendLine("");
768 session->sendLine("Available commands:");
769 session->sendLine("");
770 for (const auto &[command, info] : telnetCommands)
771 {
772 std::array<char, BUFSIZ> buffer{'\0'};
773 if (snprintf(buffer.data(), BUFSIZ, "%-25s : %s", command.c_str(), info.c_str()) > 0)
774 {
775 session->sendLine(buffer.data());
776 }
777 }
778}
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 866 of file TelnetServer.cpp.

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

Variable Documentation

◆ ASCII_LF

int ASCII_LF = 0x0A
constexpr

Definition at line 36 of file TelnetServer.cpp.

◆ ASCII_NBSP

int ASCII_NBSP = 0xFF
constexpr

Definition at line 38 of file TelnetServer.cpp.

◆ ASCII_NULL

int ASCII_NULL = 0x00
constexpr

Definition at line 37 of file TelnetServer.cpp.

◆ DEFAULT_BUFLEN

int DEFAULT_BUFLEN = 512
constexpr

Definition at line 21 of file TelnetServer.cpp.

◆ INVALID_SOCKET

int INVALID_SOCKET = -1
constexpr

Definition at line 19 of file TelnetServer.cpp.

◆ KEY_WIDTH

int KEY_WIDTH = 30
constexpr

Definition at line 32 of file TelnetServer.cpp.

◆ MAX_AVAILABLE_SESSION

int MAX_AVAILABLE_SESSION = 5
constexpr

Definition at line 25 of file TelnetServer.cpp.

◆ SLEEP_INTERVAL_MS

int SLEEP_INTERVAL_MS = 50
constexpr

Definition at line 29 of file TelnetServer.cpp.

◆ TELNET_HISTORY_LIMIT

int TELNET_HISTORY_LIMIT = 50
constexpr

Definition at line 27 of file TelnetServer.cpp.

◆ TELNET_TIMEOUT

int TELNET_TIMEOUT = 120
constexpr

Definition at line 23 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 41 of file TelnetServer.cpp.

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

◆ VAL_WIDTH

int VAL_WIDTH = 15
constexpr

Definition at line 33 of file TelnetServer.cpp.