0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Hypertable::SshSocketHandler Class Reference

Raw socket handler for ssh protocol driver. More...

#include <SshSocketHandler.h>

Inheritance diagram for Hypertable::SshSocketHandler:
Inheritance graph
[legend]
Collaboration diagram for Hypertable::SshSocketHandler:
Collaboration graph
[legend]

Public Member Functions

 SshSocketHandler (const std::string &hostname)
 Constructor. More...
 
virtual ~SshSocketHandler ()
 Destructor. More...
 
virtual bool handle (int sd, int events)
 Socket event handler function. More...
 
virtual void deregister (int sd)
 Deregisters socket. More...
 
virtual int poll_interest (int sd)
 Returns current polling interest. More...
 
void log_callback (ssh_session session, int priority, const char *message)
 Writes log messages to logging output collector. More...
 
int auth_callback (const char *prompt, char *buf, size_t len, int echo, int verify)
 libssh authorization callback. More...
 
void connect_status_callback (float status)
 libssh connection status callback. More...
 
void global_request_callback (ssh_session session, ssh_message message)
 libssh global request callback. More...
 
void set_exit_status (int exit_status)
 libssh exit status callback This function sets m_command_exit_status to exit_status, sets m_command_exit_status_is_set to true, and then signals m_cond. More...
 
bool wait_for_connection (std::chrono::system_clock::time_point deadline)
 Waits for connection establishment. More...
 
bool issue_command (const std::string &command)
 Asynchronously issues a command. More...
 
bool wait_for_command_completion ()
 Waits for command completion. More...
 
void cancel ()
 Cancels outstanding connection establishment or command execution. More...
 
void dump_log (std::ostream &out)
 Writes collected log messages to output stream. More...
 
void set_terminal_output (bool val)
 Tells handler to send collected output subsequent output to terminal If val is true, sends any collected stdout or stderr output to terminal and sets m_terminal_output to true which causes any subsequent output collected to be sent to the terminal. More...
 
const std::string hostname () const
 Returns hostname. More...
 
- Public Member Functions inherited from Hypertable::RawSocketHandler
virtual ~RawSocketHandler ()
 Destructor. More...
 

Static Public Member Functions

static void enable_debug ()
 Enables debug logging output. More...
 
static void set_libssh_verbosity (const std::string &value)
 Sets libssh logging verbosity level. More...
 

Private Member Functions

bool verify_knownhost ()
 Verifies host with public key method. More...
 
void write_to_stdout (const char *output, size_t len)
 Writes output to stdout Writes output to stdout, prefixing each line with '[' hostname ']'. More...
 
void write_to_stderr (const char *output, size_t len)
 Writes output to stderr Writes output to stderr, prefixing each line with '[' hostname ']'. More...
 
bool socket_has_data ()
 Determines if data available on socket for reading Checks socket descriptor m_sd to see if there is any data available for reading. More...
 

Private Attributes

std::mutex m_mutex
 Mutex for serialzing access to members More...
 
std::condition_variable m_cond
 Condition variable signalling connection and command completion. More...
 
Commm_comm
 Pointer to comm layer. More...
 
std::string m_hostname
 Name of host to connect to. More...
 
ssh_session m_ssh_session
 libssh sesison object More...
 
CommAddress m_comm_address
 Address of connection. More...
 
ssh_channel m_channel
 libssh channel object More...
 
int m_state {}
 Current handler state. More...
 
int m_sd {}
 Socket descriptor. More...
 
int m_poll_interest {}
 Current polling interest. More...
 
std::string m_error
 Error message More...
 
ssh_callbacks_struct m_callbacks
 libssh callbacks More...
 
ssh_channel_callbacks_struct m_channel_callbacks
 libssh channel callbacks More...
 
std::string m_command
 Current command being issued. More...
 
int m_command_exit_status {}
 Command exit status. More...
 
bool m_command_exit_status_is_set {}
 Flag indicating that the exit status has been set. More...
 
bool m_terminal_output {}
 Redirect output to terminal. More...
 
bool m_line_prefix_needed_stdout {}
 Line prefix needs to be emitted on next stdout output. More...
 
bool m_line_prefix_needed_stderr {}
 Line prefix needs to be emitted on next stderr output. More...
 
bool m_cancelled {}
 Flag indicating that outstanding operations should be cancelled. More...
 
bool m_channel_is_eof {}
 Flag indicating that current channel is EOF. More...
 
SshOutputCollector m_log_collector
 Output collector for logging. More...
 
SshOutputCollector::Buffer m_log_buffer {}
 Current logging output buffer. More...
 
SshOutputCollector m_stdout_collector
 Output collector for stdout. More...
 
SshOutputCollector::Buffer m_stdout_buffer {}
 Current stdout output buffer. More...
 
SshOutputCollector m_stderr_collector
 Output collector for stderr. More...
 
SshOutputCollector::Buffer m_stderr_buffer {}
 Current stderr output buffer. More...
 

Static Private Attributes

static bool ms_debug_enabled {}
 Flag for enabling debugging output. More...
 
static int ms_libssh_verbosity {SSH_LOG_PROTOCOL}
 Libssh logging verbosity level. More...
 

Detailed Description

Raw socket handler for ssh protocol driver.

Definition at line 60 of file SshSocketHandler.h.

Constructor & Destructor Documentation

SshSocketHandler::SshSocketHandler ( const std::string &  hostname)

Constructor.

Initializes handler by creating a non-blocking TCP socket and asynchonously connecting to hostname.

Parameters
hostnameName of host to connect to

Definition at line 144 of file SshSocketHandler.cc.

SshSocketHandler::~SshSocketHandler ( )
virtual

Destructor.

Definition at line 202 of file SshSocketHandler.cc.

Member Function Documentation

int SshSocketHandler::auth_callback ( const char *  prompt,
char *  buf,
size_t  len,
int  echo,
int  verify 
)

libssh authorization callback.

Currently this function just logs a message and returns -1

Parameters
promptPrompt to be displayed.
bufBuffer to save the password.
lenLength of the buffer.
echoEnable or disable the echo of what you type.
verifyShould the password be verified?
Returns
-1

Definition at line 539 of file SshSocketHandler.cc.

void SshSocketHandler::cancel ( )

Cancels outstanding connection establishment or command execution.

Sets m_cancelled to true, closes m_channel if it has been opened, closes m_ssh_session if it is open, and then signals m_cond.

Definition at line 632 of file SshSocketHandler.cc.

void SshSocketHandler::connect_status_callback ( float  status)

libssh connection status callback.

This method currently does nothing.

Parameters
statusPercentage of connection status, going from 0.0 to 1.0

Definition at line 546 of file SshSocketHandler.cc.

void SshSocketHandler::deregister ( int  sd)
virtual

Deregisters socket.

Closes m_sd

Parameters
sdSocket descriptor to deregister

Implements Hypertable::RawSocketHandler.

Definition at line 504 of file SshSocketHandler.cc.

void SshSocketHandler::dump_log ( std::ostream &  out)

Writes collected log messages to output stream.

This function adds m_log_buffer to m_log_collector and then iterates over m_log_collector writing each line to out. If m_error contains an error message, it is written to out as well.

Parameters
outOutput stream to write log messages to.

if (m_command_exit_status != 0) out << "[" << m_hostname << "] exit status = " << m_command_exit_status << "\n";

Definition at line 700 of file SshSocketHandler.cc.

void SshSocketHandler::enable_debug ( )
static

Enables debug logging output.

Sets ms_debug_enabled to true to cause verbose logging messages to be displayed to stdout.

Definition at line 125 of file SshSocketHandler.cc.

void SshSocketHandler::global_request_callback ( ssh_session  session,
ssh_message  message 
)

libssh global request callback.

This function currenly just logs a message and returns.

Parameters
sessionlibssh session object
messagelibssh message

Definition at line 551 of file SshSocketHandler.cc.

bool SshSocketHandler::handle ( int  sd,
int  events 
)
virtual

Socket event handler function.

Asynchronously handles ssh connection establishment and command execution. If an error is encountered during either connection establishment or command execution, then an error message is stored in m_error and m_cond is signalled. Upon completion of command execution, the exit status is stored in m_command_exit_status.

Parameters
sdSocket descriptor
eventsBitmask of polling events
See also
PollEvent::Flags
Returns
true on success, false if an error was encountered.

been set, then we need to read again to get the exit status

Implements Hypertable::RawSocketHandler.

Definition at line 209 of file SshSocketHandler.cc.

const std::string Hypertable::SshSocketHandler::hostname ( ) const
inline

Returns hostname.

Returns
Hostname

Definition at line 198 of file SshSocketHandler.h.

bool SshSocketHandler::issue_command ( const std::string &  command)

Asynchronously issues a command.

If an error is encountered while attempting to issue command and error message is stored in m_error and false is returned.

Parameters
commandCommand to issue
Returns
true if command was successfully issued, false otherwise.

Definition at line 574 of file SshSocketHandler.cc.

void SshSocketHandler::log_callback ( ssh_session  session,
int  priority,
const char *  message 
)

Writes log messages to logging output collector.

If priority is greater than 1, this function writes message to the logging output buffer m_log_buffer. If m_log_buffer fills up, it gets added to m_log_collector and a new one is allocted.

Parameters
sessionlibssh session object
priorityMessage priority
messageLog message

Definition at line 510 of file SshSocketHandler.cc.

virtual int Hypertable::SshSocketHandler::poll_interest ( int  sd)
inlinevirtual

Returns current polling interest.

Returns
Current polling interest

Implements Hypertable::RawSocketHandler.

Definition at line 91 of file SshSocketHandler.h.

void SshSocketHandler::set_exit_status ( int  exit_status)

libssh exit status callback This function sets m_command_exit_status to exit_status, sets m_command_exit_status_is_set to true, and then signals m_cond.

Parameters
exit_statusExit status of issued command

Definition at line 557 of file SshSocketHandler.cc.

void SshSocketHandler::set_libssh_verbosity ( const std::string &  value)
static

Sets libssh logging verbosity level.

Parses value and sets ms_libssh_verbosity accordingly. The valid values for value are: none, warning, protocol, packet, functions. If value does not match (case insensitively) any of the value values, then an error message is written to the console and exit is called with status code 1.

Definition at line 127 of file SshSocketHandler.cc.

void SshSocketHandler::set_terminal_output ( bool  val)

Tells handler to send collected output subsequent output to terminal If val is true, sends any collected stdout or stderr output to terminal and sets m_terminal_output to true which causes any subsequent output collected to be sent to the terminal.

Otherwise, m_terminal_output is set to false which prevents subsequent output from being sent to the terminal.

Parameters
valIf true, enable terminal output, otherwise disable it

Definition at line 648 of file SshSocketHandler.cc.

bool SshSocketHandler::socket_has_data ( )
private

Determines if data available on socket for reading Checks socket descriptor m_sd to see if there is any data available for reading.

Returns
true if data is available, false otherwise

Definition at line 833 of file SshSocketHandler.cc.

bool SshSocketHandler::verify_knownhost ( )
private

Verifies host with public key method.

Verifies host and writes known host file. m_error is populated with an error message on failure.

Returns
true on success, false on error.

Definition at line 718 of file SshSocketHandler.cc.

bool SshSocketHandler::wait_for_command_completion ( )

Waits for command completion.

This function blocks on m_cond until the command previously issued by issue_command() has completed or stopped due to an error or m_cancelled was set to true.

Returns
true if command successfully completed, false if m_error is contains an error message or m_command_exit_status was set to a non-zero value or m_cancelled is true and m_state is not STATE_CONNECTED.

Definition at line 623 of file SshSocketHandler.cc.

bool SshSocketHandler::wait_for_connection ( std::chrono::system_clock::time_point  deadline)

Waits for connection establishment.

Blocks on m_cond until in connected state or m_error is non-empty or m_cancelled is true. If connection is not established by deadline then m_error is set to "timeout" and false is returned.

Parameters
deadlineMaximum wait time
Returns
true upon successful connection, false if connection attempt failed (m_error is non-empty) or deadline was reached before connection could be established

Definition at line 563 of file SshSocketHandler.cc.

void SshSocketHandler::write_to_stderr ( const char *  output,
size_t  len 
)
private

Writes output to stderr Writes output to stderr, prefixing each line with '[' hostname ']'.

Parameters
outputPointer to output data
lenLength of data pointed to by output

Definition at line 804 of file SshSocketHandler.cc.

void SshSocketHandler::write_to_stdout ( const char *  output,
size_t  len 
)
private

Writes output to stdout Writes output to stdout, prefixing each line with '[' hostname ']'.

Parameters
outputPointer to output data
lenLength of data pointed to by output

Definition at line 775 of file SshSocketHandler.cc.

Member Data Documentation

ssh_callbacks_struct Hypertable::SshSocketHandler::m_callbacks
private

libssh callbacks

Definition at line 266 of file SshSocketHandler.h.

bool Hypertable::SshSocketHandler::m_cancelled {}
private

Flag indicating that outstanding operations should be cancelled.

Definition at line 290 of file SshSocketHandler.h.

ssh_channel Hypertable::SshSocketHandler::m_channel
private

libssh channel object

Definition at line 251 of file SshSocketHandler.h.

ssh_channel_callbacks_struct Hypertable::SshSocketHandler::m_channel_callbacks
private

libssh channel callbacks

Definition at line 269 of file SshSocketHandler.h.

bool Hypertable::SshSocketHandler::m_channel_is_eof {}
private

Flag indicating that current channel is EOF.

Definition at line 293 of file SshSocketHandler.h.

Comm* Hypertable::SshSocketHandler::m_comm
private

Pointer to comm layer.

Definition at line 239 of file SshSocketHandler.h.

CommAddress Hypertable::SshSocketHandler::m_comm_address
private

Address of connection.

Definition at line 248 of file SshSocketHandler.h.

std::string Hypertable::SshSocketHandler::m_command
private

Current command being issued.

Definition at line 272 of file SshSocketHandler.h.

int Hypertable::SshSocketHandler::m_command_exit_status {}
private

Command exit status.

Definition at line 275 of file SshSocketHandler.h.

bool Hypertable::SshSocketHandler::m_command_exit_status_is_set {}
private

Flag indicating that the exit status has been set.

Definition at line 278 of file SshSocketHandler.h.

std::condition_variable Hypertable::SshSocketHandler::m_cond
private

Condition variable signalling connection and command completion.

Definition at line 236 of file SshSocketHandler.h.

std::string Hypertable::SshSocketHandler::m_error
private

Error message

Definition at line 263 of file SshSocketHandler.h.

std::string Hypertable::SshSocketHandler::m_hostname
private

Name of host to connect to.

Definition at line 242 of file SshSocketHandler.h.

bool Hypertable::SshSocketHandler::m_line_prefix_needed_stderr {}
private

Line prefix needs to be emitted on next stderr output.

Definition at line 287 of file SshSocketHandler.h.

bool Hypertable::SshSocketHandler::m_line_prefix_needed_stdout {}
private

Line prefix needs to be emitted on next stdout output.

Definition at line 284 of file SshSocketHandler.h.

SshOutputCollector::Buffer Hypertable::SshSocketHandler::m_log_buffer {}
private

Current logging output buffer.

Definition at line 299 of file SshSocketHandler.h.

SshOutputCollector Hypertable::SshSocketHandler::m_log_collector
private

Output collector for logging.

Definition at line 296 of file SshSocketHandler.h.

std::mutex Hypertable::SshSocketHandler::m_mutex
private

Mutex for serialzing access to members

Definition at line 233 of file SshSocketHandler.h.

int Hypertable::SshSocketHandler::m_poll_interest {}
private

Current polling interest.

Definition at line 260 of file SshSocketHandler.h.

int Hypertable::SshSocketHandler::m_sd {}
private

Socket descriptor.

Definition at line 257 of file SshSocketHandler.h.

ssh_session Hypertable::SshSocketHandler::m_ssh_session
private

libssh sesison object

Definition at line 245 of file SshSocketHandler.h.

int Hypertable::SshSocketHandler::m_state {}
private

Current handler state.

Definition at line 254 of file SshSocketHandler.h.

SshOutputCollector::Buffer Hypertable::SshSocketHandler::m_stderr_buffer {}
private

Current stderr output buffer.

Definition at line 311 of file SshSocketHandler.h.

SshOutputCollector Hypertable::SshSocketHandler::m_stderr_collector
private

Output collector for stderr.

Definition at line 308 of file SshSocketHandler.h.

SshOutputCollector::Buffer Hypertable::SshSocketHandler::m_stdout_buffer {}
private

Current stdout output buffer.

Definition at line 305 of file SshSocketHandler.h.

SshOutputCollector Hypertable::SshSocketHandler::m_stdout_collector
private

Output collector for stdout.

Definition at line 302 of file SshSocketHandler.h.

bool Hypertable::SshSocketHandler::m_terminal_output {}
private

Redirect output to terminal.

Definition at line 281 of file SshSocketHandler.h.

bool SshSocketHandler::ms_debug_enabled {}
staticprivate

Flag for enabling debugging output.

Definition at line 227 of file SshSocketHandler.h.

int SshSocketHandler::ms_libssh_verbosity {SSH_LOG_PROTOCOL}
staticprivate

Libssh logging verbosity level.

Definition at line 230 of file SshSocketHandler.h.


The documentation for this class was generated from the following files: