1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-22 05:21:51 +03:00

channels: Reformat

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2025-04-24 15:10:27 +02:00
committed by Andreas Schneider
parent babd891e82
commit c799a18d89

View File

@ -166,7 +166,8 @@ uint32_t ssh_channel_new_id(ssh_session session)
* *
* Constructs the channel object. * Constructs the channel object.
*/ */
SSH_PACKET_CALLBACK(ssh_packet_channel_open_conf){ SSH_PACKET_CALLBACK(ssh_packet_channel_open_conf)
{
uint32_t channelid = 0; uint32_t channelid = 0;
ssh_channel channel; ssh_channel channel;
int rc; int rc;
@ -180,7 +181,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_open_conf){
goto error; goto error;
channel = ssh_channel_from_local(session, channelid); channel = ssh_channel_from_local(session, channelid);
if (channel == NULL) { if (channel == NULL) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session,
SSH_FATAL,
"Unknown channel id %" PRIu32, "Unknown channel id %" PRIu32,
(uint32_t)channelid); (uint32_t)channelid);
/* TODO: Set error marking in channel object */ /* TODO: Set error marking in channel object */
@ -188,7 +190,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_open_conf){
return SSH_PACKET_USED; return SSH_PACKET_USED;
} }
rc = ssh_buffer_unpack(packet, "ddd", rc = ssh_buffer_unpack(packet,
"ddd",
&channel->remote_channel, &channel->remote_channel,
&channel->remote_window, &channel->remote_window,
&channel->remote_maxpacket); &channel->remote_maxpacket);
@ -196,7 +199,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_open_conf){
goto error; goto error;
SSH_LOG(SSH_LOG_DEBUG, SSH_LOG(SSH_LOG_DEBUG,
"Received a CHANNEL_OPEN_CONFIRMATION for channel %" PRIu32 ":%" PRIu32, "Received a CHANNEL_OPEN_CONFIRMATION for channel %" PRIu32
":%" PRIu32,
channel->local_channel, channel->local_channel,
channel->remote_channel); channel->remote_channel);
@ -235,8 +239,8 @@ error:
* *
* @brief Handle a SSH_CHANNEL_OPEN_FAILURE and set the state of the channel. * @brief Handle a SSH_CHANNEL_OPEN_FAILURE and set the state of the channel.
*/ */
SSH_PACKET_CALLBACK(ssh_packet_channel_open_fail){ SSH_PACKET_CALLBACK(ssh_packet_channel_open_fail)
{
ssh_channel channel; ssh_channel channel;
char *error = NULL; char *error = NULL;
uint32_t code; uint32_t code;
@ -265,8 +269,10 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_open_fail){
goto error; goto error;
} }
ssh_set_error(session, SSH_REQUEST_DENIED, ssh_set_error(session,
"Channel opening failure: channel %" PRIu32 " error (%" PRIu32 ") %s", SSH_REQUEST_DENIED,
"Channel opening failure: channel %" PRIu32 " error (%" PRIu32
") %s",
channel->local_channel, channel->local_channel,
code, code,
error); error);
@ -403,11 +409,13 @@ end:
} }
/* return channel with corresponding local id, or NULL if not found */ /* return channel with corresponding local id, or NULL if not found */
ssh_channel ssh_channel_from_local(ssh_session session, uint32_t id) { ssh_channel ssh_channel_from_local(ssh_session session, uint32_t id)
{
struct ssh_iterator *it; struct ssh_iterator *it;
ssh_channel channel; ssh_channel channel;
for (it = ssh_list_get_iterator(session->channels); it != NULL ; it=it->next) { for (it = ssh_list_get_iterator(session->channels); it != NULL;
it = it->next) {
channel = ssh_iterator_value(ssh_channel, it); channel = ssh_iterator_value(ssh_channel, it);
if (channel == NULL) { if (channel == NULL) {
continue; continue;
@ -507,14 +515,16 @@ static ssh_channel channel_from_msg(ssh_session session, ssh_buffer packet)
rc = ssh_buffer_unpack(packet, "d", &chan); rc = ssh_buffer_unpack(packet, "d", &chan);
if (rc != SSH_OK) { if (rc != SSH_OK) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session,
SSH_FATAL,
"Getting channel from message: short read"); "Getting channel from message: short read");
return NULL; return NULL;
} }
channel = ssh_channel_from_local(session, chan); channel = ssh_channel_from_local(session, chan);
if (channel == NULL) { if (channel == NULL) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session,
SSH_FATAL,
"Server specified invalid channel %" PRIu32, "Server specified invalid channel %" PRIu32,
(uint32_t)chan); (uint32_t)chan);
} }
@ -522,7 +532,8 @@ static ssh_channel channel_from_msg(ssh_session session, ssh_buffer packet)
return channel; return channel;
} }
SSH_PACKET_CALLBACK(channel_rcv_change_window) { SSH_PACKET_CALLBACK(channel_rcv_change_window)
{
ssh_channel channel; ssh_channel channel;
uint32_t bytes; uint32_t bytes;
int rc; int rc;
@ -545,7 +556,8 @@ SSH_PACKET_CALLBACK(channel_rcv_change_window) {
} }
SSH_LOG(SSH_LOG_DEBUG, SSH_LOG(SSH_LOG_DEBUG,
"Adding %" PRIu32 " bytes to channel (%" PRIu32 ":%" PRIu32 ") (from %" PRIu32 " bytes)", "Adding %" PRIu32 " bytes to channel (%" PRIu32 ":%" PRIu32
") (from %" PRIu32 " bytes)",
bytes, bytes,
channel->local_channel, channel->local_channel,
channel->remote_channel, channel->remote_channel,
@ -556,7 +568,8 @@ SSH_PACKET_CALLBACK(channel_rcv_change_window) {
channel->remote_window += bytes; channel->remote_window += bytes;
/* Writing to the channel is non-blocking until the receive window is empty. /* Writing to the channel is non-blocking until the receive window is empty.
When the receive window becomes non-zero again, call channel_write_wontblock_function. */ * When the receive window becomes non-zero again, call
* channel_write_wontblock_function. */
if (was_empty && bytes > 0) { if (was_empty && bytes > 0) {
ssh_callbacks_execute_list(channel->callbacks, ssh_callbacks_execute_list(channel->callbacks,
ssh_channel_callbacks, ssh_channel_callbacks,
@ -694,7 +707,8 @@ SSH_PACKET_CALLBACK(channel_rcv_data)
return SSH_PACKET_USED; return SSH_PACKET_USED;
} }
SSH_PACKET_CALLBACK(channel_rcv_eof) { SSH_PACKET_CALLBACK(channel_rcv_eof)
{
ssh_channel channel; ssh_channel channel;
(void)user; (void)user;
(void)type; (void)type;
@ -1538,19 +1552,23 @@ static int channel_write_common(ssh_channel channel,
if (len > INT_MAX) { if (len > INT_MAX) {
SSH_LOG(SSH_LOG_TRACE, SSH_LOG(SSH_LOG_TRACE,
"Length (%" PRIu32 ") is bigger than INT_MAX", len); "Length (%" PRIu32 ") is bigger than INT_MAX",
len);
return SSH_ERROR; return SSH_ERROR;
} }
if (channel->local_eof) { if (channel->local_eof) {
ssh_set_error(session, SSH_REQUEST_DENIED, ssh_set_error(session,
"Can't write to channel %" PRIu32 ":%" PRIu32 " after EOF was sent", SSH_REQUEST_DENIED,
"Can't write to channel %" PRIu32 ":%" PRIu32
" after EOF was sent",
channel->local_channel, channel->local_channel,
channel->remote_channel); channel->remote_channel);
return -1; return -1;
} }
if (channel->state != SSH_CHANNEL_STATE_OPEN || channel->delayed_close != 0) { if (channel->state != SSH_CHANNEL_STATE_OPEN ||
channel->delayed_close != 0) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Remote channel is closed"); ssh_set_error(session, SSH_REQUEST_DENIED, "Remote channel is closed");
return -1; return -1;
@ -1561,24 +1579,29 @@ static int channel_write_common(ssh_channel channel,
} }
if (ssh_waitsession_unblocked(session) == 0) { if (ssh_waitsession_unblocked(session) == 0) {
rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_DEFAULT, rc = ssh_handle_packets_termination(session,
ssh_waitsession_unblocked, session); SSH_TIMEOUT_DEFAULT,
ssh_waitsession_unblocked,
session);
if (rc == SSH_ERROR || !ssh_waitsession_unblocked(session)) if (rc == SSH_ERROR || !ssh_waitsession_unblocked(session))
goto out; goto out;
} }
while (len > 0) { while (len > 0) {
if (channel->remote_window < len) { if (channel->remote_window < len) {
SSH_LOG(SSH_LOG_DEBUG, SSH_LOG(SSH_LOG_DEBUG,
"Remote window is %" PRIu32 " bytes. going to write %" PRIu32 " bytes", "Remote window is %" PRIu32
" bytes. going to write %" PRIu32 " bytes",
channel->remote_window, channel->remote_window,
len); len);
/* When the window is zero, wait for it to grow */ /* When the window is zero, wait for it to grow */
if (channel->remote_window == 0) { if (channel->remote_window == 0) {
/* nothing can be written */ /* nothing can be written */
SSH_LOG(SSH_LOG_DEBUG, SSH_LOG(SSH_LOG_DEBUG, "Wait for a growing window message...");
"Wait for a growing window message..."); rc = ssh_handle_packets_termination(
rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_DEFAULT, session,
ssh_channel_waitwindow_termination,channel); SSH_TIMEOUT_DEFAULT,
ssh_channel_waitwindow_termination,
channel);
if (rc == SSH_ERROR || if (rc == SSH_ERROR ||
!ssh_channel_waitwindow_termination(channel) || !ssh_channel_waitwindow_termination(channel) ||
session->session_state == SSH_SESSION_STATE_ERROR || session->session_state == SSH_SESSION_STATE_ERROR ||
@ -1600,7 +1623,8 @@ static int channel_write_common(ssh_channel channel,
rc = ssh_buffer_pack(session->out_buffer, rc = ssh_buffer_pack(session->out_buffer,
"bd", "bd",
is_stderr ? SSH2_MSG_CHANNEL_EXTENDED_DATA : SSH2_MSG_CHANNEL_DATA, is_stderr ? SSH2_MSG_CHANNEL_EXTENDED_DATA
: SSH2_MSG_CHANNEL_DATA,
channel->remote_channel); channel->remote_channel);
if (rc != SSH_OK) { if (rc != SSH_OK) {
ssh_set_error_oom(session); ssh_set_error_oom(session);
@ -1778,7 +1802,8 @@ void ssh_channel_set_blocking(ssh_channel channel, int blocking)
* *
* @brief handle a SSH_CHANNEL_SUCCESS packet and set the channel state. * @brief handle a SSH_CHANNEL_SUCCESS packet and set the channel state.
*/ */
SSH_PACKET_CALLBACK(ssh_packet_channel_success){ SSH_PACKET_CALLBACK(ssh_packet_channel_success)
{
ssh_channel channel; ssh_channel channel;
(void)type; (void)type;
(void)user; (void)user;
@ -1794,7 +1819,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_success){
channel->local_channel, channel->local_channel,
channel->remote_channel); channel->remote_channel);
if (channel->request_state != SSH_CHANNEL_REQ_STATE_PENDING) { if (channel->request_state != SSH_CHANNEL_REQ_STATE_PENDING) {
SSH_LOG(SSH_LOG_RARE, "SSH_CHANNEL_SUCCESS received in incorrect state %d", SSH_LOG(SSH_LOG_RARE,
"SSH_CHANNEL_SUCCESS received in incorrect state %d",
channel->request_state); channel->request_state);
} else { } else {
channel->request_state = SSH_CHANNEL_REQ_STATE_ACCEPTED; channel->request_state = SSH_CHANNEL_REQ_STATE_ACCEPTED;
@ -1814,7 +1840,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_success){
* *
* @brief Handle a SSH_CHANNEL_FAILURE packet and set the channel state. * @brief Handle a SSH_CHANNEL_FAILURE packet and set the channel state.
*/ */
SSH_PACKET_CALLBACK(ssh_packet_channel_failure){ SSH_PACKET_CALLBACK(ssh_packet_channel_failure)
{
ssh_channel channel; ssh_channel channel;
(void)type; (void)type;
(void)user; (void)user;
@ -1831,7 +1858,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_failure){
channel->local_channel, channel->local_channel,
channel->remote_channel); channel->remote_channel);
if (channel->request_state != SSH_CHANNEL_REQ_STATE_PENDING) { if (channel->request_state != SSH_CHANNEL_REQ_STATE_PENDING) {
SSH_LOG(SSH_LOG_RARE, "SSH_CHANNEL_FAILURE received in incorrect state %d", SSH_LOG(SSH_LOG_RARE,
"SSH_CHANNEL_FAILURE received in incorrect state %d",
channel->request_state); channel->request_state);
} else { } else {
channel->request_state = SSH_CHANNEL_REQ_STATE_DENIED; channel->request_state = SSH_CHANNEL_REQ_STATE_DENIED;
@ -3148,7 +3176,8 @@ int ssh_channel_read_timeout(ssh_channel channel,
} }
/* /*
* If the channel is closed or in an error state, reading from it is an error * If the channel is closed or in an error state, reading from it is an
* error
*/ */
if (session->session_state == SSH_SESSION_STATE_ERROR) { if (session->session_state == SSH_SESSION_STATE_ERROR) {
return SSH_ERROR; return SSH_ERROR;
@ -3158,9 +3187,7 @@ int ssh_channel_read_timeout(ssh_channel channel,
return 0; return 0;
} }
if (channel->state == SSH_CHANNEL_STATE_CLOSED) { if (channel->state == SSH_CHANNEL_STATE_CLOSED) {
ssh_set_error(session, ssh_set_error(session, SSH_FATAL, "Remote channel is closed.");
SSH_FATAL,
"Remote channel is closed.");
return SSH_ERROR; return SSH_ERROR;
} }
len = ssh_buffer_get_len(stdbuf); len = ssh_buffer_get_len(stdbuf);
@ -3530,12 +3557,15 @@ channel_protocol_select(ssh_channel *rchans, ssh_channel *wchans,
for (i = 0; rchans[i] != NULL; i++) { for (i = 0; rchans[i] != NULL; i++) {
chan = rchans[i]; chan = rchans[i];
while (ssh_channel_is_open(chan) && ssh_socket_data_available(chan->session->socket)) { while (ssh_channel_is_open(chan) &&
ssh_socket_data_available(chan->session->socket)) {
ssh_handle_packets(chan->session, SSH_TIMEOUT_NONBLOCKING); ssh_handle_packets(chan->session, SSH_TIMEOUT_NONBLOCKING);
} }
if ((chan->stdout_buffer && ssh_buffer_get_len(chan->stdout_buffer) > 0) || if ((chan->stdout_buffer &&
(chan->stderr_buffer && ssh_buffer_get_len(chan->stderr_buffer) > 0) || ssh_buffer_get_len(chan->stdout_buffer) > 0) ||
(chan->stderr_buffer &&
ssh_buffer_get_len(chan->stderr_buffer) > 0) ||
chan->remote_eof) { chan->remote_eof) {
rout[j] = chan; rout[j] = chan;
j++; j++;
@ -3559,7 +3589,8 @@ channel_protocol_select(ssh_channel *rchans, ssh_channel *wchans,
for (i = 0; echans[i] != NULL; i++) { for (i = 0; echans[i] != NULL; i++) {
chan = echans[i]; chan = echans[i];
if (!ssh_socket_is_open(chan->session->socket) || ssh_channel_is_closed(chan)) { if (!ssh_socket_is_open(chan->session->socket) ||
ssh_channel_is_closed(chan)) {
eout[j] = chan; eout[j] = chan;
j++; j++;
} }
@ -3632,7 +3663,8 @@ int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans,
exceptchans = &dummy; exceptchans = &dummy;
} }
if (readchans[0] == NULL && writechans[0] == NULL && exceptchans[0] == NULL) { if (readchans[0] == NULL && writechans[0] == NULL &&
exceptchans[0] == NULL) {
/* No channel to poll?? Go away! */ /* No channel to poll?? Go away! */
return 0; return 0;
} }
@ -3661,8 +3693,12 @@ int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans,
* infrastructure to poll on all sessions. * infrastructure to poll on all sessions.
*/ */
do { do {
channel_protocol_select(readchans, writechans, exceptchans, channel_protocol_select(readchans,
rchans, wchans, echans); writechans,
exceptchans,
rchans,
wchans,
echans);
if (rchans[0] != NULL || wchans[0] != NULL || echans[0] != NULL) { if (rchans[0] != NULL || wchans[0] != NULL || echans[0] != NULL) {
/* At least one channel has an event */ /* At least one channel has an event */
break; break;
@ -3708,13 +3744,19 @@ int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans,
} while (1); } while (1);
if (readchans != &dummy) { if (readchans != &dummy) {
memcpy(readchans, rchans, (count_ptrs(rchans) + 1) * sizeof(ssh_channel)); memcpy(readchans,
rchans,
(count_ptrs(rchans) + 1) * sizeof(ssh_channel));
} }
if (writechans != &dummy) { if (writechans != &dummy) {
memcpy(writechans, wchans, (count_ptrs(wchans) + 1) * sizeof(ssh_channel)); memcpy(writechans,
wchans,
(count_ptrs(wchans) + 1) * sizeof(ssh_channel));
} }
if (exceptchans != &dummy) { if (exceptchans != &dummy) {
memcpy(exceptchans, echans, (count_ptrs(echans) + 1) * sizeof(ssh_channel)); memcpy(exceptchans,
echans,
(count_ptrs(echans) + 1) * sizeof(ssh_channel));
} }
SAFE_FREE(rchans); SAFE_FREE(rchans);
SAFE_FREE(wchans); SAFE_FREE(wchans);
@ -3883,10 +3925,7 @@ int ssh_channel_open_x11(ssh_channel channel,
goto error; goto error;
} }
rc = ssh_buffer_pack(payload, rc = ssh_buffer_pack(payload, "sd", orig_addr, orig_port);
"sd",
orig_addr,
orig_port);
if (rc != SSH_OK) { if (rc != SSH_OK) {
ssh_set_error_oom(session); ssh_set_error_oom(session);
goto error; goto error;