Repo-Init
 
Loading...
Searching...
No Matches
TelnetServer.hpp File Reference
#include "telnet/TelnetStats.hpp"
#include <array>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
Include dependency graph for TelnetServer.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  TelnetSession
 
class  TelnetServer
 

Typedefs

using Socket = int
 
using SP_TelnetSession = std::shared_ptr<TelnetSession>
 
using VEC_SP_TelnetSession = std::vector<SP_TelnetSession>
 
using FPTR_ConnectedCallback = std::function<void(SP_TelnetSession)>
 
using FPTR_NewLineCallback = std::function<bool(SP_TelnetSession, std::string)>
 
using FPTR_TabCallback = std::function<std::string(SP_TelnetSession, std::string)>
 

Functions

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)
 

Typedef Documentation

◆ FPTR_ConnectedCallback

using FPTR_ConnectedCallback = std::function<void(SP_TelnetSession)>

Definition at line 126 of file TelnetServer.hpp.

◆ FPTR_NewLineCallback

using FPTR_NewLineCallback = std::function<bool(SP_TelnetSession, std::string)>

Definition at line 127 of file TelnetServer.hpp.

◆ FPTR_TabCallback

using FPTR_TabCallback = std::function<std::string(SP_TelnetSession, std::string)>

Definition at line 128 of file TelnetServer.hpp.

◆ Socket

using Socket = int

Definition at line 51 of file TelnetServer.hpp.

◆ SP_TelnetSession

using SP_TelnetSession = std::shared_ptr<TelnetSession>

Definition at line 123 of file TelnetServer.hpp.

◆ VEC_SP_TelnetSession

using VEC_SP_TelnetSession = std::vector<SP_TelnetSession>

Definition at line 124 of file TelnetServer.hpp.

Function Documentation

◆ TelnetConnectedCallback()

void TelnetConnectedCallback ( const SP_TelnetSession & session)

Telnet session connection start callback

Parameters
[in]sessionHandle to session

Definition at line 770 of file TelnetServer.cpp.

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

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

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

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