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 <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 771 of file TelnetServer.cpp.

772{
773 session->sendLine("\r\n"
774 "𝑲𝒆𝒆𝒑 𝒚𝒐𝒖𝒓 𝒆𝒚𝒆𝒔 𝒐𝒏 𝒕𝒉𝒆 𝒔𝒕𝒂𝒓𝒔 "
775 "𝒂𝒏𝒅 𝒚𝒐𝒖𝒓 𝒇𝒆𝒆𝒕 𝒐𝒏 𝒕𝒉𝒆 𝒈𝒓𝒐𝒖𝒏𝒅 "
776 "\r\n");
778}
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 780 of file TelnetServer.cpp.

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

756{
757 // Print available commands
758 session->sendLine("");
759 session->sendLine("Available commands:");
760 session->sendLine("");
761 for (const auto &[command, info] : telnetCommands)
762 {
763 std::array<char, BUFSIZ> buffer{'\0'};
764 if (snprintf(buffer.data(), BUFSIZ, "%-25s : %s", command.c_str(), info.c_str()) > 0)
765 {
766 session->sendLine(buffer.data());
767 }
768 }
769}
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 857 of file TelnetServer.cpp.

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

Variable Documentation

◆ ASCII_LF

int ASCII_LF = 0x0A
constexpr

Definition at line 37 of file TelnetServer.cpp.

◆ ASCII_NBSP

int ASCII_NBSP = 0xFF
constexpr

Definition at line 39 of file TelnetServer.cpp.

◆ ASCII_NULL

int ASCII_NULL = 0x00
constexpr

Definition at line 38 of file TelnetServer.cpp.

◆ DEFAULT_BUFLEN

int DEFAULT_BUFLEN = 512
constexpr

Definition at line 22 of file TelnetServer.cpp.

◆ INVALID_SOCKET

int INVALID_SOCKET = -1
constexpr

Definition at line 20 of file TelnetServer.cpp.

◆ KEY_WIDTH

int KEY_WIDTH = 30
constexpr

Definition at line 33 of file TelnetServer.cpp.

◆ MAX_AVAILABLE_SESSION

int MAX_AVAILABLE_SESSION = 5
constexpr

Definition at line 26 of file TelnetServer.cpp.

◆ SLEEP_INTERVAL_MS

int SLEEP_INTERVAL_MS = 50
constexpr

Definition at line 30 of file TelnetServer.cpp.

◆ TELNET_HISTORY_LIMIT

int TELNET_HISTORY_LIMIT = 50
constexpr

Definition at line 28 of file TelnetServer.cpp.

◆ TELNET_TIMEOUT

int TELNET_TIMEOUT = 120
constexpr

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

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

◆ VAL_WIDTH

int VAL_WIDTH = 15
constexpr

Definition at line 34 of file TelnetServer.cpp.