Raw socket handler for ssh protocol driver. More...
#include <SshSocketHandler.h>
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... | |
Comm * | m_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... | |
Raw socket handler for ssh protocol driver.
Definition at line 60 of file SshSocketHandler.h.
SshSocketHandler::SshSocketHandler | ( | const std::string & | hostname | ) |
Constructor.
Initializes handler by creating a non-blocking TCP socket and asynchonously connecting to hostname
.
hostname | Name of host to connect to |
Definition at line 144 of file SshSocketHandler.cc.
|
virtual |
Destructor.
Definition at line 202 of file SshSocketHandler.cc.
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
prompt | Prompt to be displayed. |
buf | Buffer to save the password. |
len | Length of the buffer. |
echo | Enable or disable the echo of what you type. |
verify | Should the password be verified? |
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.
status | Percentage of connection status, going from 0.0 to 1.0 |
Definition at line 546 of file SshSocketHandler.cc.
|
virtual |
Deregisters socket.
Closes m_sd
sd | Socket 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.
out | Output 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.
|
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.
session | libssh session object |
message | libssh message |
Definition at line 551 of file SshSocketHandler.cc.
|
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.
sd | Socket descriptor |
events | Bitmask of polling events |
been set, then we need to read again to get the exit status
Implements Hypertable::RawSocketHandler.
Definition at line 209 of file SshSocketHandler.cc.
|
inline |
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.
command | Command to issue |
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.
session | libssh session object |
priority | Message priority |
message | Log message |
Definition at line 510 of file SshSocketHandler.cc.
|
inlinevirtual |
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.
exit_status | Exit status of issued command |
Definition at line 557 of file SshSocketHandler.cc.
|
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.
val | If true, enable terminal output, otherwise disable it |
Definition at line 648 of file SshSocketHandler.cc.
|
private |
Determines if data available on socket for reading Checks socket descriptor m_sd to see if there is any data available for reading.
Definition at line 833 of file SshSocketHandler.cc.
|
private |
Verifies host with public key method.
Verifies host and writes known host file. m_error is populated with an error message on failure.
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.
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.
deadline | Maximum wait time |
deadline
was reached before connection could be established Definition at line 563 of file SshSocketHandler.cc.
|
private |
Writes output to stderr Writes output to stderr, prefixing each line with '[' hostname ']'.
output | Pointer to output data |
len | Length of data pointed to by output |
Definition at line 804 of file SshSocketHandler.cc.
|
private |
Writes output to stdout Writes output to stdout, prefixing each line with '[' hostname ']'.
output | Pointer to output data |
len | Length of data pointed to by output |
Definition at line 775 of file SshSocketHandler.cc.
|
private |
libssh callbacks
Definition at line 266 of file SshSocketHandler.h.
|
private |
Flag indicating that outstanding operations should be cancelled.
Definition at line 290 of file SshSocketHandler.h.
|
private |
libssh channel object
Definition at line 251 of file SshSocketHandler.h.
|
private |
libssh channel callbacks
Definition at line 269 of file SshSocketHandler.h.
|
private |
Flag indicating that current channel is EOF.
Definition at line 293 of file SshSocketHandler.h.
|
private |
Pointer to comm layer.
Definition at line 239 of file SshSocketHandler.h.
|
private |
Address of connection.
Definition at line 248 of file SshSocketHandler.h.
|
private |
Current command being issued.
Definition at line 272 of file SshSocketHandler.h.
|
private |
Command exit status.
Definition at line 275 of file SshSocketHandler.h.
|
private |
Flag indicating that the exit status has been set.
Definition at line 278 of file SshSocketHandler.h.
|
private |
Condition variable signalling connection and command completion.
Definition at line 236 of file SshSocketHandler.h.
|
private |
Error message
Definition at line 263 of file SshSocketHandler.h.
|
private |
Name of host to connect to.
Definition at line 242 of file SshSocketHandler.h.
|
private |
Line prefix needs to be emitted on next stderr output.
Definition at line 287 of file SshSocketHandler.h.
|
private |
Line prefix needs to be emitted on next stdout output.
Definition at line 284 of file SshSocketHandler.h.
|
private |
Current logging output buffer.
Definition at line 299 of file SshSocketHandler.h.
|
private |
Output collector for logging.
Definition at line 296 of file SshSocketHandler.h.
|
private |
Mutex for serialzing access to members
Definition at line 233 of file SshSocketHandler.h.
|
private |
Current polling interest.
Definition at line 260 of file SshSocketHandler.h.
|
private |
Socket descriptor.
Definition at line 257 of file SshSocketHandler.h.
|
private |
libssh sesison object
Definition at line 245 of file SshSocketHandler.h.
|
private |
Current handler state.
Definition at line 254 of file SshSocketHandler.h.
|
private |
Current stderr output buffer.
Definition at line 311 of file SshSocketHandler.h.
|
private |
Output collector for stderr.
Definition at line 308 of file SshSocketHandler.h.
|
private |
Current stdout output buffer.
Definition at line 305 of file SshSocketHandler.h.
|
private |
Output collector for stdout.
Definition at line 302 of file SshSocketHandler.h.
|
private |
Redirect output to terminal.
Definition at line 281 of file SshSocketHandler.h.
|
staticprivate |
Flag for enabling debugging output.
Definition at line 227 of file SshSocketHandler.h.
|
staticprivate |
Libssh logging verbosity level.
Definition at line 230 of file SshSocketHandler.h.