From fb2fefb3c65996abac7defcdc0ea758060ea3111 Mon Sep 17 00:00:00 2001 From: Axel Eppe Date: Fri, 6 Apr 2018 13:41:39 +0100 Subject: [PATCH] channels: add ssh_channel_request_send_break to support RFC 4335 Signed-off-by: Axel Eppe Reviewed-by: Andreas Schneider --- include/libssh/libssh.h | 1 + src/channels.c | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index b00a428a..0f509aea 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -418,6 +418,7 @@ LIBSSH_API int ssh_channel_request_pty_size(ssh_channel channel, const char *ter int cols, int rows); LIBSSH_API int ssh_channel_request_shell(ssh_channel channel); LIBSSH_API int ssh_channel_request_send_signal(ssh_channel channel, const char *signum); +LIBSSH_API int ssh_channel_request_send_break(ssh_channel channel, uint32_t length); LIBSSH_API int ssh_channel_request_sftp(ssh_channel channel); LIBSSH_API int ssh_channel_request_subsystem(ssh_channel channel, const char *subsystem); LIBSSH_API int ssh_channel_request_x11(ssh_channel channel, int single_connection, const char *protocol, diff --git a/src/channels.c b/src/channels.c index 6e65dc62..d43cc82a 100644 --- a/src/channels.c +++ b/src/channels.c @@ -2528,6 +2528,55 @@ error: } +/** + * @brief Send a break signal to the server (as described in RFC 4335). + * + * Sends a break signal to the remote process. + * Note, that remote system may not support breaks. + * In such a case this request will be silently ignored. + * Only SSH-v2 is supported. + * + * @param[in] channel The channel to send the break to. + * + * @param[in] length The break-length in milliseconds to send. + * + * @return SSH_OK on success, SSH_ERROR if an error occurred + * (including attempts to send signal via SSH-v1 session). + */ +int ssh_channel_request_send_break(ssh_channel channel, uint32_t length) { + ssh_buffer buffer = NULL; + int rc = SSH_ERROR; + + if (channel == NULL) { + return SSH_ERROR; + } + +#ifdef WITH_SSH1 + if (channel->version == 1) { + return SSH_ERROR; + } +#endif + + buffer = ssh_buffer_new(); + if (buffer == NULL) { + ssh_set_error_oom(channel->session); + goto error; + } + + rc = ssh_buffer_pack(buffer, "d", length); + if (rc != SSH_OK) { + ssh_set_error_oom(channel->session); + goto error; + } + + rc = channel_request(channel, "break", buffer, 0); + +error: + ssh_buffer_free(buffer); + return rc; +} + + /** * @brief Read data from a channel into a buffer. *