diff --git a/libssh/include/libssh/server.h b/libssh/include/libssh/server.h index 6ed8002a..9fab8e3d 100644 --- a/libssh/include/libssh/server.h +++ b/libssh/include/libssh/server.h @@ -379,6 +379,8 @@ LIBSSH_API int ssh_channel_write_stderr(ssh_channel channel, const void *data, uint32_t len); +LIBSSH_API int ssh_send_keepalive(ssh_session session); + /* deprecated functions */ SSH_DEPRECATED LIBSSH_API int ssh_accept(ssh_session session); SSH_DEPRECATED LIBSSH_API int channel_write_stderr(ssh_channel channel, diff --git a/libssh/src/server.c b/libssh/src/server.c index db8f8152..642abb30 100644 --- a/libssh/src/server.c +++ b/libssh/src/server.c @@ -1200,6 +1200,41 @@ int ssh_execute_message_callbacks(ssh_session session){ return SSH_OK; } +int ssh_send_keepalive(ssh_session session) +{ + /* TODO check the reply and all that */ + struct ssh_string_struct *req; + int reply = 1; + int rc = SSH_ERROR; + + enter_function(); + req = ssh_string_from_char("keepalive@openssh.com"); + if (req == NULL) { + ssh_set_error_oom(session); + goto out; + } + + if (buffer_add_u8(session->out_buffer, SSH2_MSG_GLOBAL_REQUEST) < 0 || + buffer_add_ssh_string(session->out_buffer, req) < 0 || + buffer_add_u8(session->out_buffer, reply == 0 ? 0 : 1) < 0) { + ssh_set_error_oom(session); + goto out; + } + + if (packet_send(session) == SSH_ERROR) + goto out; + + ssh_handle_packets(session, 0); + + ssh_log(session, SSH_LOG_PACKET, "Sent a keepalive"); + rc = SSH_OK; + +out: + ssh_string_free(req); + leave_function(); + return rc; +} + /** @} */ /* vim: set ts=4 sw=4 et cindent: */