cleanups: better binary packet gen, size_t fixes and PACKET_* removal

I'll introduce a new internal function set named

 _libssh2_store_u32
 _libssh2_store_u64
 _libssh2_store_str

That can be used all through the library to build binary outgoing
packets.  Using these instead of the current approach removes
hundreds of lines from the library while at the same time greatly
enhances readability. I've not yet fully converted everything to
use these functions.

I've converted LOTS of 'unsigned long' to 'size_t' where
data/string lengths are dealt with internally. This is The Right
Thing and it will help us make the transition to our
size_t-polished API later on as well.

I'm removing the PACKET_* error codes. They were originally
introduced as a set of separate error codes from the transport
layer, but having its own set of errors turned out to be very
awkward and they were then converted into a set of #defines that
simply maps them to the global libssh2 error codes instead. Now,
I'l take the next logical step and simply replace the PACKET_*
defines with the actual LIBSSH2_ERROR_* defines. It will increase
readability and decrease confusion.

I also separated packet stuff into its own packet.h header file.
This commit is contained in:
Daniel Stenberg 2010-04-17 13:18:15 +02:00
parent 81e63b3657
commit c3bcdd88a4
21 changed files with 599 additions and 862 deletions

View File

@ -4,4 +4,4 @@ CSOURCES = channel.c comp.c crypt.c hostkey.c kex.c mac.c misc.c \
global.c global.c
HHEADERS = libssh2_priv.h openssl.h libgcrypt.h transport.h channel.h \ HHEADERS = libssh2_priv.h openssl.h libgcrypt.h transport.h channel.h \
comp.h mac.h misc.h comp.h mac.h misc.h packet.h

View File

@ -284,8 +284,8 @@ agent_transact_pageant(LIBSSH2_AGENT *agent, agent_transaction_ctx_t transctx)
return -1; return -1;
} }
p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
_libssh2_htonu32(p, transctx->request_len); _libssh2_store_str(p, transctx->request, transctx->request_len);
memcpy(p + 4, transctx->request, transctx->request_len);
cds.dwData = PAGEANT_COPYDATA_ID; cds.dwData = PAGEANT_COPYDATA_ID;
cds.cbData = 1 + strlen(mapname); cds.cbData = 1 + strlen(mapname);
cds.lpData = mapname; cds.lpData = mapname;
@ -361,18 +361,14 @@ agent_sign(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len,
*s++ = SSH2_AGENTC_SIGN_REQUEST; *s++ = SSH2_AGENTC_SIGN_REQUEST;
/* key blob */ /* key blob */
_libssh2_htonu32(s, identity->external.blob_len); _libssh2_store_str(&s, (const char *)identity->external.blob,
s += 4; identity->external.blob_len);
memcpy(s, identity->external.blob, identity->external.blob_len);
s += identity->external.blob_len;
/* data */ /* data */
_libssh2_htonu32(s, data_len); _libssh2_store_str(&s, (const char *)data, data_len);
s += 4;
memcpy(s, data, data_len);
s += data_len;
/* flags */ /* flags */
_libssh2_htonu32(s, 0); _libssh2_store_u32(&s, 0);
s += 4;
transctx->request_len = s - transctx->request; transctx->request_len = s - transctx->request;
transctx->state = agent_NB_state_request_created; transctx->state = agent_NB_state_request_created;
} }

View File

@ -49,6 +49,7 @@
#include "channel.h" #include "channel.h"
#include "transport.h" #include "transport.h"
#include "packet.h"
/* /*
* _libssh2_channel_nextid * _libssh2_channel_nextid
@ -192,20 +193,10 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
goto channel_error; goto channel_error;
} }
*(s++) = SSH_MSG_CHANNEL_OPEN; *(s++) = SSH_MSG_CHANNEL_OPEN;
_libssh2_htonu32(s, channel_type_len); _libssh2_store_str(&s, channel_type, channel_type_len);
s += 4; _libssh2_store_u32(&s, session->open_local_channel);
_libssh2_store_u32(&s, window_size);
memcpy(s, channel_type, channel_type_len); _libssh2_store_u32(&s, packet_size);
s += channel_type_len;
_libssh2_htonu32(s, session->open_local_channel);
s += 4;
_libssh2_htonu32(s, window_size);
s += 4;
_libssh2_htonu32(s, packet_size);
s += 4;
if (message && message_len) { if (message && message_len) {
memcpy(s, message, message_len); memcpy(s, message, message_len);
@ -218,7 +209,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
if (session->open_state == libssh2_NB_state_created) { if (session->open_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->open_packet, rc = _libssh2_transport_write(session, session->open_packet,
session->open_packet_len); session->open_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending channel-open request"); "Would block sending channel-open request");
return NULL; return NULL;
@ -238,7 +229,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
session->open_packet + 5 + session->open_packet + 5 +
channel_type_len, 4, channel_type_len, 4,
&session->open_packet_requirev_state); &session->open_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
@ -372,19 +363,10 @@ channel_direct_tcpip(LIBSSH2_SESSION * session, const char *host,
"Unable to allocate memory for direct-tcpip connection"); "Unable to allocate memory for direct-tcpip connection");
return NULL; return NULL;
} }
_libssh2_htonu32(s, session->direct_host_len); _libssh2_store_str(&s, host, session->direct_host_len);
s += 4; _libssh2_store_u32(&s, port);
memcpy(s, host, session->direct_host_len); _libssh2_store_str(&s, shost, session->direct_shost_len);
s += session->direct_host_len; _libssh2_store_u32(&s, sport);
_libssh2_htonu32(s, port);
s += 4;
_libssh2_htonu32(s, session->direct_shost_len);
s += 4;
memcpy(s, shost, session->direct_shost_len);
s += session->direct_shost_len;
_libssh2_htonu32(s, sport);
s += 4;
} }
channel = channel =
@ -439,7 +421,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
unsigned char *s, *data; unsigned char *s, *data;
static const unsigned char reply_codes[3] = static const unsigned char reply_codes[3] =
{ SSH_MSG_REQUEST_SUCCESS, SSH_MSG_REQUEST_FAILURE, 0 }; { SSH_MSG_REQUEST_SUCCESS, SSH_MSG_REQUEST_FAILURE, 0 };
unsigned long data_len; size_t data_len;
int rc; int rc;
if (session->fwdLstn_state == libssh2_NB_state_idle) { if (session->fwdLstn_state == libssh2_NB_state_idle) {
@ -467,18 +449,12 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
} }
*(s++) = SSH_MSG_GLOBAL_REQUEST; *(s++) = SSH_MSG_GLOBAL_REQUEST;
_libssh2_htonu32(s, sizeof("tcpip-forward") - 1); _libssh2_store_str(&s, "tcpip-forward", sizeof("tcpip-forward") - 1);
s += 4;
memcpy(s, "tcpip-forward", sizeof("tcpip-forward") - 1);
s += sizeof("tcpip-forward") - 1;
*(s++) = 0x01; /* want_reply */ *(s++) = 0x01; /* want_reply */
_libssh2_htonu32(s, session->fwdLstn_host_len); _libssh2_store_str(&s, host ? host : "0.0.0.0",
s += 4; session->fwdLstn_host_len);
memcpy(s, host ? host : "0.0.0.0", session->fwdLstn_host_len); _libssh2_store_u32(&s, port);
s += session->fwdLstn_host_len;
_libssh2_htonu32(s, port);
s += 4;
session->fwdLstn_state = libssh2_NB_state_created; session->fwdLstn_state = libssh2_NB_state_created;
} }
@ -486,7 +462,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
if (session->fwdLstn_state == libssh2_NB_state_created) { if (session->fwdLstn_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->fwdLstn_packet, rc = _libssh2_transport_write(session, session->fwdLstn_packet,
session->fwdLstn_packet_len); session->fwdLstn_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending global-request packet for " "Would block sending global-request packet for "
"forward listen request"); "forward listen request");
@ -510,7 +486,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host,
rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len,
0, NULL, 0, 0, NULL, 0,
&session->fwdLstn_packet_requirev_state); &session->fwdLstn_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
return NULL; return NULL;
} else if (rc) { } else if (rc) {
@ -605,17 +581,17 @@ libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host,
* Stop listening on a remote port and free the listener * Stop listening on a remote port and free the listener
* Toss out any pending (un-accept()ed) connections * Toss out any pending (un-accept()ed) connections
* *
* Return 0 on success, PACKET_EAGAIN if would block, -1 on error * Return 0 on success, LIBSSH2_ERROR_EAGAIN if would block, -1 on error
*/ */
static int channel_forward_cancel(LIBSSH2_LISTENER *listener) static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
{ {
LIBSSH2_SESSION *session = listener->session; LIBSSH2_SESSION *session = listener->session;
LIBSSH2_CHANNEL *queued; LIBSSH2_CHANNEL *queued;
unsigned char *packet, *s; unsigned char *packet, *s;
unsigned long host_len = strlen(listener->host); size_t host_len = strlen(listener->host);
/* 14 = packet_type(1) + request_len(4) + want_replay(1) + host_len(4) + /* 14 = packet_type(1) + request_len(4) + want_replay(1) + host_len(4) +
port(4) */ port(4) */
unsigned long packet_len = size_t packet_len =
host_len + 14 + sizeof("cancel-tcpip-forward") - 1; host_len + 14 + sizeof("cancel-tcpip-forward") - 1;
int rc; int rc;
@ -632,18 +608,12 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
} }
*(s++) = SSH_MSG_GLOBAL_REQUEST; *(s++) = SSH_MSG_GLOBAL_REQUEST;
_libssh2_htonu32(s, sizeof("cancel-tcpip-forward") - 1); _libssh2_store_str(&s, "cancel-tcpip-forward",
s += 4; sizeof("cancel-tcpip-forward") - 1);
memcpy(s, "cancel-tcpip-forward", sizeof("cancel-tcpip-forward") - 1);
s += sizeof("cancel-tcpip-forward") - 1;
*(s++) = 0x00; /* want_reply */ *(s++) = 0x00; /* want_reply */
_libssh2_htonu32(s, host_len); _libssh2_store_str(&s, listener->host, host_len);
s += 4; _libssh2_store_u32(&s, listener->port);
memcpy(s, listener->host, host_len);
s += host_len;
_libssh2_htonu32(s, listener->port);
s += 4;
listener->chanFwdCncl_state = libssh2_NB_state_created; listener->chanFwdCncl_state = libssh2_NB_state_created;
} else { } else {
@ -652,7 +622,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
if (listener->chanFwdCncl_state == libssh2_NB_state_created) { if (listener->chanFwdCncl_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, packet, packet_len); rc = _libssh2_transport_write(session, packet, packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
listener->chanFwdCncl_data = packet; listener->chanFwdCncl_data = packet;
return rc; return rc;
} }
@ -674,7 +644,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
LIBSSH2_CHANNEL *next = _libssh2_list_next(&queued->node); LIBSSH2_CHANNEL *next = _libssh2_list_next(&queued->node);
rc = libssh2_channel_free(queued); rc = libssh2_channel_free(queued);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
queued = next; queued = next;
@ -697,7 +667,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener)
* Stop listening on a remote port and free the listener * Stop listening on a remote port and free the listener
* Toss out any pending (un-accept()ed) connections * Toss out any pending (un-accept()ed) connections
* *
* Return 0 on success, PACKET_EAGAIN if would block, -1 on error * Return 0 on success, LIBSSH2_ERROR_EAGAIN if would block, -1 on error
*/ */
LIBSSH2_API int LIBSSH2_API int
libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener) libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener)
@ -735,7 +705,7 @@ channel_forward_accept(LIBSSH2_LISTENER *listener)
return channel; return channel;
} }
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for packet"); "Would block waiting for packet");
} }
@ -773,7 +743,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
unsigned char *s, *data; unsigned char *s, *data;
static const unsigned char reply_codes[3] = static const unsigned char reply_codes[3] =
{ SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 };
unsigned long data_len; size_t data_len;
int rc; int rc;
if (channel->setenv_state == libssh2_NB_state_idle) { if (channel->setenv_state == libssh2_NB_state_idle) {
@ -799,24 +769,11 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
_libssh2_htonu32(s, channel->remote.id); _libssh2_store_u32(&s, channel->remote.id);
s += 4; _libssh2_store_str(&s, "env", sizeof("env") - 1);
_libssh2_htonu32(s, sizeof("env") - 1);
s += 4;
memcpy(s, "env", sizeof("env") - 1);
s += sizeof("env") - 1;
*(s++) = 0x01; *(s++) = 0x01;
_libssh2_store_str(&s, varname, varname_len);
_libssh2_htonu32(s, varname_len); _libssh2_store_str(&s, value, value_len);
s += 4;
memcpy(s, varname, varname_len);
s += varname_len;
_libssh2_htonu32(s, value_len);
s += 4;
memcpy(s, value, value_len);
s += value_len;
channel->setenv_state = libssh2_NB_state_created; channel->setenv_state = libssh2_NB_state_created;
} }
@ -824,7 +781,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
if (channel->setenv_state == libssh2_NB_state_created) { if (channel->setenv_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, channel->setenv_packet, rc = _libssh2_transport_write(session, channel->setenv_packet,
channel->setenv_packet_len); channel->setenv_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, channel->setenv_packet); LIBSSH2_FREE(session, channel->setenv_packet);
@ -847,7 +804,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
1, channel->setenv_local_channel, 4, 1, channel->setenv_local_channel, 4,
&channel-> &channel->
setenv_packet_requirev_state); setenv_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
if (rc) { if (rc) {
@ -900,7 +857,7 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
unsigned char *s, *data; unsigned char *s, *data;
static const unsigned char reply_codes[3] = static const unsigned char reply_codes[3] =
{ SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 };
unsigned long data_len; size_t data_len;
int rc; int rc;
if (channel->reqPTY_state == libssh2_NB_state_idle) { if (channel->reqPTY_state == libssh2_NB_state_idle) {
@ -925,37 +882,17 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
_libssh2_htonu32(s, channel->remote.id); _libssh2_store_u32(&s, channel->remote.id);
s += 4; _libssh2_store_str(&s, (char *)"pty-req", sizeof("pty-req") - 1);
_libssh2_htonu32(s, sizeof("pty-req") - 1);
s += 4;
memcpy(s, "pty-req", sizeof("pty-req") - 1);
s += sizeof("pty-req") - 1;
*(s++) = 0x01; *(s++) = 0x01;
_libssh2_htonu32(s, term_len); _libssh2_store_str(&s, term, term_len);
s += 4; _libssh2_store_u32(&s, width);
if (term) { _libssh2_store_u32(&s, height);
memcpy(s, term, term_len); _libssh2_store_u32(&s, width_px);
s += term_len; _libssh2_store_u32(&s, height_px);
} _libssh2_store_str(&s, modes, modes_len);
_libssh2_htonu32(s, width);
s += 4;
_libssh2_htonu32(s, height);
s += 4;
_libssh2_htonu32(s, width_px);
s += 4;
_libssh2_htonu32(s, height_px);
s += 4;
_libssh2_htonu32(s, modes_len);
s += 4;
if (modes) {
memcpy(s, modes, modes_len);
s += modes_len;
}
channel->reqPTY_state = libssh2_NB_state_created; channel->reqPTY_state = libssh2_NB_state_created;
} }
@ -963,7 +900,7 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
if (channel->reqPTY_state == libssh2_NB_state_created) { if (channel->reqPTY_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, channel->reqPTY_packet, rc = _libssh2_transport_write(session, channel->reqPTY_packet,
channel->reqPTY_packet_len); channel->reqPTY_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, channel->reqPTY_packet); LIBSSH2_FREE(session, channel->reqPTY_packet);
@ -984,7 +921,7 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len,
1, channel->reqPTY_local_channel, 4, 1, channel->reqPTY_local_channel, 4,
&channel->reqPTY_packet_requirev_state); &channel->reqPTY_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
channel->reqPTY_state = libssh2_NB_state_idle; channel->reqPTY_state = libssh2_NB_state_idle;
@ -1045,28 +982,19 @@ channel_request_pty_size(LIBSSH2_CHANNEL * channel, int width,
s = channel->reqPTY_packet = s = channel->reqPTY_packet =
LIBSSH2_ALLOC(session, channel->reqPTY_packet_len); LIBSSH2_ALLOC(session, channel->reqPTY_packet_len);
if (!channel->reqPTY_packet) { if (!channel->reqPTY_packet)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for pty-request"); "Unable to allocate memory for pty-request");
}
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
_libssh2_htonu32(s, channel->remote.id); _libssh2_store_u32(&s, channel->remote.id);
s += 4; _libssh2_store_str(&s, (char *)"window-change",
_libssh2_htonu32(s, sizeof("window-change") - 1); sizeof("window-change") - 1);
s += 4;
memcpy(s, "window-change", sizeof("window-change") - 1);
s += sizeof("window-change") - 1;
*(s++) = 0x00; /* Don't reply */ *(s++) = 0x00; /* Don't reply */
_libssh2_htonu32(s, width); _libssh2_store_u32(&s, width);
s += 4; _libssh2_store_u32(&s, height);
_libssh2_htonu32(s, height); _libssh2_store_u32(&s, width_px);
s += 4; _libssh2_store_u32(&s, height_px);
_libssh2_htonu32(s, width_px);
s += 4;
_libssh2_htonu32(s, height_px);
s += 4;
channel->reqPTY_state = libssh2_NB_state_created; channel->reqPTY_state = libssh2_NB_state_created;
} }
@ -1074,7 +1002,7 @@ channel_request_pty_size(LIBSSH2_CHANNEL * channel, int width,
if (channel->reqPTY_state == libssh2_NB_state_created) { if (channel->reqPTY_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, channel->reqPTY_packet, rc = _libssh2_transport_write(session, channel->reqPTY_packet,
channel->reqPTY_packet_len); channel->reqPTY_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, channel->reqPTY_packet); LIBSSH2_FREE(session, channel->reqPTY_packet);
@ -1122,10 +1050,10 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
unsigned char *s, *data; unsigned char *s, *data;
static const unsigned char reply_codes[3] = static const unsigned char reply_codes[3] =
{ SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 };
unsigned long data_len; size_t data_len;
unsigned long proto_len = size_t proto_len =
auth_proto ? strlen(auth_proto) : (sizeof("MIT-MAGIC-COOKIE-1") - 1); auth_proto ? strlen(auth_proto) : (sizeof("MIT-MAGIC-COOKIE-1") - 1);
unsigned long cookie_len = size_t cookie_len =
auth_cookie ? strlen(auth_cookie) : LIBSSH2_X11_RANDOM_COOKIE_LEN; auth_cookie ? strlen(auth_cookie) : LIBSSH2_X11_RANDOM_COOKIE_LEN;
int rc; int rc;
@ -1155,23 +1083,16 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
_libssh2_htonu32(s, channel->remote.id); _libssh2_store_u32(&s, channel->remote.id);
s += 4; _libssh2_store_str(&s, "x11-req", sizeof("x11-req") - 1);
_libssh2_htonu32(s, sizeof("x11-req") - 1);
s += 4;
memcpy(s, "x11-req", sizeof("x11-req") - 1);
s += sizeof("x11-req") - 1;
*(s++) = 0x01; /* want_reply */ *(s++) = 0x01; /* want_reply */
*(s++) = single_connection ? 0x01 : 0x00; *(s++) = single_connection ? 0x01 : 0x00;
_libssh2_htonu32(s, proto_len); _libssh2_store_str(&s, auth_proto?auth_proto:"MIT-MAGIC-COOKIE-1",
s += 4; proto_len);
memcpy(s, auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", proto_len);
s += proto_len;
_libssh2_htonu32(s, cookie_len); _libssh2_store_u32(&s, cookie_len);
s += 4;
if (auth_cookie) { if (auth_cookie) {
memcpy(s, auth_cookie, cookie_len); memcpy(s, auth_cookie, cookie_len);
} else { } else {
@ -1189,16 +1110,14 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
} }
s += cookie_len; s += cookie_len;
_libssh2_htonu32(s, screen_number); _libssh2_store_u32(&s, screen_number);
s += 4;
channel->reqX11_state = libssh2_NB_state_created; channel->reqX11_state = libssh2_NB_state_created;
} }
if (channel->reqX11_state == libssh2_NB_state_created) { if (channel->reqX11_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, channel->reqX11_packet, rc = _libssh2_transport_write(session, channel->reqX11_packet,
channel->reqX11_packet_len); channel->reqX11_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
if (rc) { if (rc) {
@ -1220,7 +1139,7 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len,
1, channel->reqX11_local_channel, 4, 1, channel->reqX11_local_channel, 4,
&channel->reqX11_packet_requirev_state); &channel->reqX11_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
channel->reqX11_state = libssh2_NB_state_idle; channel->reqX11_state = libssh2_NB_state_idle;
@ -1271,7 +1190,7 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
unsigned char *s, *data; unsigned char *s, *data;
static const unsigned char reply_codes[3] = static const unsigned char reply_codes[3] =
{ SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 };
unsigned long data_len; size_t data_len;
int rc; int rc;
if (channel->process_state == libssh2_NB_state_idle) { if (channel->process_state == libssh2_NB_state_idle) {
@ -1299,21 +1218,12 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
} }
*(s++) = SSH_MSG_CHANNEL_REQUEST; *(s++) = SSH_MSG_CHANNEL_REQUEST;
_libssh2_htonu32(s, channel->remote.id); _libssh2_store_u32(&s, channel->remote.id);
s += 4; _libssh2_store_str(&s, request, request_len);
_libssh2_htonu32(s, request_len);
s += 4;
memcpy(s, request, request_len);
s += request_len;
*(s++) = 0x01; *(s++) = 0x01;
if (message) { if (message)
_libssh2_htonu32(s, message_len); _libssh2_store_str(&s, message, message_len);
s += 4;
memcpy(s, message, message_len);
s += message_len;
}
channel->process_state = libssh2_NB_state_created; channel->process_state = libssh2_NB_state_created;
} }
@ -1321,7 +1231,7 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
if (channel->process_state == libssh2_NB_state_created) { if (channel->process_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, channel->process_packet, rc = _libssh2_transport_write(session, channel->process_packet,
channel->process_packet_len); channel->process_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc) { else if (rc) {
@ -1343,7 +1253,7 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len,
1, channel->process_local_channel, 4, 1, channel->process_local_channel, 4,
&channel->process_packet_requirev_state); &channel->process_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
channel->process_state = libssh2_NB_state_idle; channel->process_state = libssh2_NB_state_idle;
@ -1458,7 +1368,7 @@ _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid)
rc = _libssh2_channel_receive_window_adjust(channel, rc = _libssh2_channel_receive_window_adjust(channel,
channel->flush_refund_bytes, channel->flush_refund_bytes,
0, NULL); 0, NULL);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
} }
@ -1545,7 +1455,7 @@ _libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL * channel,
} }
rc = _libssh2_transport_write(channel->session, channel->adjust_adjust, 9); rc = _libssh2_transport_write(channel->session, channel->adjust_adjust, 9);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc) { else if (rc) {
@ -1636,7 +1546,7 @@ _libssh2_channel_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode)
int rc = int rc =
_libssh2_channel_flush(channel, _libssh2_channel_flush(channel,
LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA); LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA);
if(PACKET_EAGAIN == rc) if(LIBSSH2_ERROR_EAGAIN == rc)
return rc; return rc;
} }
} }
@ -1685,7 +1595,7 @@ libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel,
* *
* It is important to not return 0 until the currently read channel is * It is important to not return 0 until the currently read channel is
* complete. If we read stuff from the wire but it was no payload data to fill * complete. If we read stuff from the wire but it was no payload data to fill
* in the buffer with, we MUST make sure to return PACKET_EAGAIN. * in the buffer with, we MUST make sure to return LIBSSH2_ERROR_EAGAIN.
*/ */
ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
char *buf, size_t buflen) char *buf, size_t buflen)
@ -1714,7 +1624,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
while (rc > 0) while (rc > 0)
rc = _libssh2_transport_read(session); rc = _libssh2_transport_read(session);
if ((rc < 0) && (rc != PACKET_EAGAIN)) if ((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN))
return _libssh2_error(session, rc, "transport read"); return _libssh2_error(session, rc, "transport read");
/* /*
@ -1813,7 +1723,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
return 0; return 0;
else else
/* if the transport layer said EAGAIN then we say so as well */ /* if the transport layer said EAGAIN then we say so as well */
return (rc == PACKET_EAGAIN)?rc:0; return (rc == LIBSSH2_ERROR_EAGAIN)?rc:0;
} }
else else
/* make sure we remain in the created state to focus on emptying the /* make sure we remain in the created state to focus on emptying the
@ -1830,7 +1740,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
this special state here */ this special state here */
rc = _libssh2_channel_receive_window_adjust(channel, rc = _libssh2_channel_receive_window_adjust(channel,
(LIBSSH2_CHANNEL_WINDOW_DEFAULT*60), 0, NULL); (LIBSSH2_CHANNEL_WINDOW_DEFAULT*60), 0, NULL);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
_libssh2_debug(session, LIBSSH2_TRACE_CONN, _libssh2_debug(session, LIBSSH2_TRACE_CONN,
@ -1852,7 +1762,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
* When this is done non-blocking, it is important to not return 0 until the * When this is done non-blocking, it is important to not return 0 until the
* currently read channel is complete. If we read stuff from the wire but it * currently read channel is complete. If we read stuff from the wire but it
* was no payload data to fill in the buffer with, we MUST make sure to return * was no payload data to fill in the buffer with, we MUST make sure to return
* PACKET_EAGAIN. * LIBSSH2_ERROR_EAGAIN.
*/ */
LIBSSH2_API ssize_t LIBSSH2_API ssize_t
libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, int stream_id, char *buf, libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, int stream_id, char *buf,
@ -1996,12 +1906,9 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
*(channel->write_s++) = *(channel->write_s++) =
stream_id ? SSH_MSG_CHANNEL_EXTENDED_DATA : stream_id ? SSH_MSG_CHANNEL_EXTENDED_DATA :
SSH_MSG_CHANNEL_DATA; SSH_MSG_CHANNEL_DATA;
_libssh2_htonu32(channel->write_s, channel->remote.id); _libssh2_store_u32(&channel->write_s, channel->remote.id);
channel->write_s += 4; if (stream_id)
if (stream_id) { _libssh2_store_u32(&channel->write_s, stream_id);
_libssh2_htonu32(channel->write_s, stream_id);
channel->write_s += 4;
}
/* Don't exceed the remote end's limits */ /* Don't exceed the remote end's limits */
/* REMEMBER local means local as the SOURCE of the data */ /* REMEMBER local means local as the SOURCE of the data */
@ -2021,10 +1928,7 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
channel->remote.id, stream_id); channel->remote.id, stream_id);
channel->write_bufwrite = channel->local.packet_size; channel->write_bufwrite = channel->local.packet_size;
} }
_libssh2_htonu32(channel->write_s, channel->write_bufwrite); _libssh2_store_str(&channel->write_s, buf, channel->write_bufwrite);
channel->write_s += 4;
memcpy(channel->write_s, buf, channel->write_bufwrite);
channel->write_s += channel->write_bufwrite;
_libssh2_debug(session, LIBSSH2_TRACE_CONN, _libssh2_debug(session, LIBSSH2_TRACE_CONN,
"Sending %d bytes on channel %lu/%lu, stream_id=%d", "Sending %d bytes on channel %lu/%lu, stream_id=%d",
@ -2038,7 +1942,7 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
rc = _libssh2_transport_write(session, channel->write_packet, rc = _libssh2_transport_write(session, channel->write_packet,
channel->write_s - channel->write_s -
channel->write_packet); channel->write_packet);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
if(wrote) { if(wrote) {
/* some pieces of data was sent before the EAGAIN so we /* some pieces of data was sent before the EAGAIN so we
return that amount! As we ignore EAGAIN, we must drain return that amount! As we ignore EAGAIN, we must drain
@ -2110,7 +2014,7 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel)
packet[0] = SSH_MSG_CHANNEL_EOF; packet[0] = SSH_MSG_CHANNEL_EOF;
_libssh2_htonu32(packet + 1, channel->remote.id); _libssh2_htonu32(packet + 1, channel->remote.id);
rc = _libssh2_transport_write(session, packet, 5); rc = _libssh2_transport_write(session, packet, 5);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc) { else if (rc) {
@ -2186,7 +2090,7 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel)
break; break;
} }
rc = _libssh2_transport_read(session); rc = _libssh2_transport_read(session);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc < 0) { else if (rc < 0) {
@ -2246,7 +2150,7 @@ channel_close(LIBSSH2_CHANNEL * channel)
if (channel->close_state == libssh2_NB_state_created) { if (channel->close_state == libssh2_NB_state_created) {
retcode = _libssh2_transport_write(session, channel->close_packet, 5); retcode = _libssh2_transport_write(session, channel->close_packet, 5);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
return retcode; return retcode;
} else if (retcode) { } else if (retcode) {
channel->close_state = libssh2_NB_state_idle; channel->close_state = libssh2_NB_state_idle;
@ -2366,7 +2270,7 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel)
LIBSSH2_SESSION *session = channel->session; LIBSSH2_SESSION *session = channel->session;
unsigned char channel_id[4]; unsigned char channel_id[4];
unsigned char *data; unsigned char *data;
unsigned long data_len; size_t data_len;
int rc; int rc;
assert(session); assert(session);
@ -2384,7 +2288,7 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel)
&& (session->socket_state == LIBSSH2_SOCKET_CONNECTED)) { && (session->socket_state == LIBSSH2_SOCKET_CONNECTED)) {
rc = channel_close(channel); rc = channel_close(channel);
if(rc == PACKET_EAGAIN) if(rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
else if (rc < 0) { else if (rc < 0) {
channel->free_state = libssh2_NB_state_idle; channel->free_state = libssh2_NB_state_idle;
@ -2476,7 +2380,7 @@ libssh2_channel_window_read_ex(LIBSSH2_CHANNEL * channel,
} }
if (read_avail) { if (read_avail) {
unsigned long bytes_queued = 0; size_t bytes_queued = 0;
LIBSSH2_PACKET *packet = LIBSSH2_PACKET *packet =
_libssh2_list_first(&channel->session->packets); _libssh2_list_first(&channel->session->packets);

View File

@ -60,7 +60,7 @@ static int hostkey_method_ssh_rsa_dtor(LIBSSH2_SESSION * session,
static int static int
hostkey_method_ssh_rsa_init(LIBSSH2_SESSION * session, hostkey_method_ssh_rsa_init(LIBSSH2_SESSION * session,
const unsigned char *hostkey_data, const unsigned char *hostkey_data,
unsigned long hostkey_data_len, size_t hostkey_data_len,
void **abstract) void **abstract)
{ {
libssh2_rsa_ctx *rsactx; libssh2_rsa_ctx *rsactx;
@ -139,9 +139,9 @@ hostkey_method_ssh_rsa_initPEM(LIBSSH2_SESSION * session,
static int static int
hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION * session, hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION * session,
const unsigned char *sig, const unsigned char *sig,
unsigned long sig_len, size_t sig_len,
const unsigned char *m, const unsigned char *m,
unsigned long m_len, void **abstract) size_t m_len, void **abstract)
{ {
libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract); libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract);
(void) session; (void) session;
@ -160,8 +160,8 @@ hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION * session,
static int static int
hostkey_method_ssh_rsa_signv(LIBSSH2_SESSION * session, hostkey_method_ssh_rsa_signv(LIBSSH2_SESSION * session,
unsigned char **signature, unsigned char **signature,
unsigned long *signature_len, size_t *signature_len,
unsigned long veccount, int veccount,
const struct iovec datavec[], const struct iovec datavec[],
void **abstract) void **abstract)
{ {
@ -232,7 +232,7 @@ static int hostkey_method_ssh_dss_dtor(LIBSSH2_SESSION * session,
static int static int
hostkey_method_ssh_dss_init(LIBSSH2_SESSION * session, hostkey_method_ssh_dss_init(LIBSSH2_SESSION * session,
const unsigned char *hostkey_data, const unsigned char *hostkey_data,
unsigned long hostkey_data_len, size_t hostkey_data_len,
void **abstract) void **abstract)
{ {
libssh2_dsa_ctx *dsactx; libssh2_dsa_ctx *dsactx;
@ -314,9 +314,9 @@ hostkey_method_ssh_dss_initPEM(LIBSSH2_SESSION * session,
static int static int
hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session, hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session,
const unsigned char *sig, const unsigned char *sig,
unsigned long sig_len, size_t sig_len,
const unsigned char *m, const unsigned char *m,
unsigned long m_len, void **abstract) size_t m_len, void **abstract)
{ {
libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *) (*abstract); libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *) (*abstract);
@ -338,8 +338,8 @@ hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session,
static int static int
hostkey_method_ssh_dss_signv(LIBSSH2_SESSION * session, hostkey_method_ssh_dss_signv(LIBSSH2_SESSION * session,
unsigned char **signature, unsigned char **signature,
unsigned long *signature_len, size_t *signature_len,
unsigned long veccount, int veccount,
const struct iovec datavec[], const struct iovec datavec[],
void **abstract) void **abstract)
{ {

View File

@ -79,7 +79,7 @@ libssh2_keepalive_send (LIBSSH2_SESSION *session,
rc = _libssh2_transport_write(session, keepalive_data, len); rc = _libssh2_transport_write(session, keepalive_data, len);
/* Silently ignore PACKET_EAGAIN here: if the write buffer is /* Silently ignore PACKET_EAGAIN here: if the write buffer is
already full, sending another keepalive is not useful. */ already full, sending another keepalive is not useful. */
if (rc && rc != PACKET_EAGAIN) { if (rc && rc != LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send keepalive message"); "Unable to send keepalive message");
return rc; return rc;

View File

@ -142,7 +142,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (exchange_state->state == libssh2_NB_state_created) { if (exchange_state->state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, exchange_state->e_packet, rc = _libssh2_transport_write(session, exchange_state->e_packet,
exchange_state->e_packet_len); exchange_state->e_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
ret = _libssh2_error(session, rc, ret = _libssh2_error(session, rc,
@ -163,7 +163,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
"Waiting for badly guessed KEX packet (to be ignored)"); "Waiting for badly guessed KEX packet (to be ignored)");
burn_type = burn_type =
_libssh2_packet_burn(session, &exchange_state->burn_state); _libssh2_packet_burn(session, &exchange_state->burn_state);
if (burn_type == PACKET_EAGAIN) { if (burn_type == LIBSSH2_ERROR_EAGAIN) {
return burn_type; return burn_type;
} else if (burn_type <= 0) { } else if (burn_type <= 0) {
/* Failed to receive a packet */ /* Failed to receive a packet */
@ -186,7 +186,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
&exchange_state->s_packet, &exchange_state->s_packet,
&exchange_state->s_packet_len, 0, NULL, &exchange_state->s_packet_len, 0, NULL,
0, &exchange_state->req_state); 0, &exchange_state->req_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
if (rc) { if (rc) {
@ -411,7 +411,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
if (exchange_state->state == libssh2_NB_state_sent2) { if (exchange_state->state == libssh2_NB_state_sent2) {
rc = _libssh2_transport_write(session, &exchange_state->c, 1); rc = _libssh2_transport_write(session, &exchange_state->c, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
ret = _libssh2_error(session, rc, "Unable to send NEWKEYS message"); ret = _libssh2_error(session, rc, "Unable to send NEWKEYS message");
@ -426,7 +426,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session,
&exchange_state->tmp, &exchange_state->tmp,
&exchange_state->tmp_len, 0, NULL, 0, &exchange_state->tmp_len, 0, NULL, 0,
&exchange_state->req_state); &exchange_state->req_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
ret = _libssh2_error(session, rc, "Timed out waiting for NEWKEYS"); ret = _libssh2_error(session, rc, "Timed out waiting for NEWKEYS");
@ -682,7 +682,7 @@ kex_method_diffie_hellman_group1_sha1_key_exchange(LIBSSH2_SESSION *session,
ret = diffie_hellman_sha1(session, key_state->g, key_state->p, 128, ret = diffie_hellman_sha1(session, key_state->g, key_state->p, 128,
SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
NULL, 0, &key_state->exchange_state); NULL, 0, &key_state->exchange_state);
if (ret == PACKET_EAGAIN) { if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} }
@ -758,7 +758,7 @@ kex_method_diffie_hellman_group14_sha1_key_exchange(LIBSSH2_SESSION *session,
ret = diffie_hellman_sha1(session, key_state->g, key_state->p, ret = diffie_hellman_sha1(session, key_state->g, key_state->p,
256, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY, 256, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY,
NULL, 0, &key_state->exchange_state); NULL, 0, &key_state->exchange_state);
if (ret == PACKET_EAGAIN) { if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} }
@ -812,7 +812,7 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
if (key_state->state == libssh2_NB_state_created) { if (key_state->state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, key_state->request, rc = _libssh2_transport_write(session, key_state->request,
key_state->request_len); key_state->request_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
ret = _libssh2_error(session, rc, ret = _libssh2_error(session, rc,
@ -827,7 +827,7 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
rc = _libssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP, rc = _libssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP,
&key_state->data, &key_state->data_len, &key_state->data, &key_state->data_len,
0, NULL, 0, &key_state->req_state); 0, NULL, 0, &key_state->req_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
ret = _libssh2_error(session, rc, ret = _libssh2_error(session, rc,
@ -856,7 +856,7 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange
key_state->data + 1, key_state->data + 1,
key_state->data_len - 1, key_state->data_len - 1,
&key_state->exchange_state); &key_state->exchange_state);
if (ret == PACKET_EAGAIN) { if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} }
@ -1107,7 +1107,7 @@ static int kexinit(LIBSSH2_SESSION * session)
} }
rc = _libssh2_transport_write(session, data, data_len); rc = _libssh2_transport_write(session, data, data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
session->kexinit_data = data; session->kexinit_data = data;
session->kexinit_data_len = data_len; session->kexinit_data_len = data_len;
return rc; return rc;
@ -1675,7 +1675,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
if (key_state->state == libssh2_NB_state_sent) { if (key_state->state == libssh2_NB_state_sent) {
retcode = kexinit(session); retcode = kexinit(session);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
return retcode; return retcode;
} else if (retcode) { } else if (retcode) {
@ -1696,7 +1696,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
&key_state->data, &key_state->data,
&key_state->data_len, 0, NULL, 0, &key_state->data_len, 0, NULL, 0,
&key_state->req_state); &key_state->req_state);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
return retcode; return retcode;
} }
@ -1732,7 +1732,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
if (key_state->state == libssh2_NB_state_sent2) { if (key_state->state == libssh2_NB_state_sent2) {
retcode = session->kex->exchange_keys(session, retcode = session->kex->exchange_keys(session,
&key_state->key_state_low); &key_state->key_state_low);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; session->state &= ~LIBSSH2_STATE_KEX_ACTIVE;
return retcode; return retcode;
} else if (retcode) { } else if (retcode) {

View File

@ -344,8 +344,8 @@ int
_libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session, _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
libssh2_dsa_ctx * rsactx, libssh2_dsa_ctx * rsactx,
const unsigned char *hash, const unsigned char *hash,
unsigned long hash_len, size_t hash_len,
unsigned char **signature, unsigned long *signature_len) unsigned char **signature, size_t *signature_len)
{ {
gcry_sexp_t sig_sexp; gcry_sexp_t sig_sexp;
gcry_sexp_t data; gcry_sexp_t data;

View File

@ -83,6 +83,21 @@
# endif # endif
#endif #endif
/* Needed for struct iovec on some platforms */
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include "libssh2.h" #include "libssh2.h"
#include "libssh2_publickey.h" #include "libssh2_publickey.h"
#include "libssh2_sftp.h" #include "libssh2_sftp.h"
@ -116,20 +131,6 @@ static inline int writev(int sock, struct iovec *iov, int nvecs)
#endif /* WIN32 */ #endif /* WIN32 */
/* Needed for struct iovec on some platforms */
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef LIBSSH2_LIBGCRYPT #ifdef LIBSSH2_LIBGCRYPT
#include "libgcrypt.h" #include "libgcrypt.h"
@ -235,9 +236,9 @@ typedef struct kmdhgGPsha1kex_state_t
unsigned char *tmp; unsigned char *tmp;
unsigned char h_sig_comp[SHA_DIGEST_LENGTH]; unsigned char h_sig_comp[SHA_DIGEST_LENGTH];
unsigned char c; unsigned char c;
unsigned long e_packet_len; size_t e_packet_len;
unsigned long s_packet_len; size_t s_packet_len;
unsigned long tmp_len; size_t tmp_len;
_libssh2_bn_ctx *ctx; _libssh2_bn_ctx *ctx;
_libssh2_bn *x; _libssh2_bn *x;
_libssh2_bn *e; _libssh2_bn *e;
@ -247,9 +248,9 @@ typedef struct kmdhgGPsha1kex_state_t
unsigned char *f_value; unsigned char *f_value;
unsigned char *k_value; unsigned char *k_value;
unsigned char *h_sig; unsigned char *h_sig;
unsigned long f_value_len; size_t f_value_len;
unsigned long k_value_len; size_t k_value_len;
unsigned long h_sig_len; size_t h_sig_len;
libssh2_sha1_ctx exchange_hash; libssh2_sha1_ctx exchange_hash;
packet_require_state_t req_state; packet_require_state_t req_state;
libssh2_nonblocking_states burn_state; libssh2_nonblocking_states burn_state;
@ -264,8 +265,8 @@ typedef struct key_exchange_state_low_t
_libssh2_bn *g; /* SSH2 defined value (2) */ _libssh2_bn *g; /* SSH2 defined value (2) */
unsigned char request[13]; unsigned char request[13];
unsigned char *data; unsigned char *data;
unsigned long request_len; size_t request_len;
unsigned long data_len; size_t data_len;
} key_exchange_state_low_t; } key_exchange_state_low_t;
typedef struct key_exchange_state_t typedef struct key_exchange_state_t
@ -274,9 +275,9 @@ typedef struct key_exchange_state_t
packet_require_state_t req_state; packet_require_state_t req_state;
key_exchange_state_low_t key_state_low; key_exchange_state_low_t key_state_low;
unsigned char *data; unsigned char *data;
unsigned long data_len; size_t data_len;
unsigned char *oldlocal; unsigned char *oldlocal;
unsigned long oldlocal_len; size_t oldlocal_len;
} key_exchange_state_t; } key_exchange_state_t;
#define FwdNotReq "Forward not requested" #define FwdNotReq "Forward not requested"
@ -320,7 +321,7 @@ struct _LIBSSH2_PACKET
/* Unencrypted Payload (no type byte, no padding, just the facts ma'am) */ /* Unencrypted Payload (no type byte, no padding, just the facts ma'am) */
unsigned char *data; unsigned char *data;
unsigned long data_len; size_t data_len;
/* Where to start reading data from, /* Where to start reading data from,
* used for channel data that's been partially consumed */ * used for channel data that's been partially consumed */
@ -536,7 +537,7 @@ struct _LIBSSH2_PUBLICKEY
unsigned char *listFetch_s; unsigned char *listFetch_s;
unsigned char listFetch_buffer[12]; unsigned char listFetch_buffer[12];
unsigned char *listFetch_data; unsigned char *listFetch_data;
unsigned long listFetch_data_len; size_t listFetch_data_len;
}; };
#define SFTP_HANDLE_MAXLEN 256 /* according to spec! */ #define SFTP_HANDLE_MAXLEN 256 /* according to spec! */
@ -754,9 +755,9 @@ struct _LIBSSH2_SESSION
/* State variables used in libssh2_session_startup() */ /* State variables used in libssh2_session_startup() */
libssh2_nonblocking_states startup_state; libssh2_nonblocking_states startup_state;
unsigned char *startup_data; unsigned char *startup_data;
unsigned long startup_data_len; size_t startup_data_len;
unsigned char startup_service[sizeof("ssh-userauth") + 5 - 1]; unsigned char startup_service[sizeof("ssh-userauth") + 5 - 1];
unsigned long startup_service_length; size_t startup_service_length;
packet_require_state_t startup_req_state; packet_require_state_t startup_req_state;
key_exchange_state_t startup_key_state; key_exchange_state_t startup_key_state;
@ -766,7 +767,7 @@ struct _LIBSSH2_SESSION
/* State variables used in libssh2_session_disconnect_ex() */ /* State variables used in libssh2_session_disconnect_ex() */
libssh2_nonblocking_states disconnect_state; libssh2_nonblocking_states disconnect_state;
unsigned char *disconnect_data; unsigned char *disconnect_data;
unsigned long disconnect_data_len; size_t disconnect_data_len;
/* State variables used in libssh2_packet_read() */ /* State variables used in libssh2_packet_read() */
libssh2_nonblocking_states readPack_state; libssh2_nonblocking_states readPack_state;
@ -775,14 +776,14 @@ struct _LIBSSH2_SESSION
/* State variables used in libssh2_userauth_list() */ /* State variables used in libssh2_userauth_list() */
libssh2_nonblocking_states userauth_list_state; libssh2_nonblocking_states userauth_list_state;
unsigned char *userauth_list_data; unsigned char *userauth_list_data;
unsigned long userauth_list_data_len; size_t userauth_list_data_len;
packet_requirev_state_t userauth_list_packet_requirev_state; packet_requirev_state_t userauth_list_packet_requirev_state;
/* State variables used in libssh2_userauth_password_ex() */ /* State variables used in libssh2_userauth_password_ex() */
libssh2_nonblocking_states userauth_pswd_state; libssh2_nonblocking_states userauth_pswd_state;
unsigned char *userauth_pswd_data; unsigned char *userauth_pswd_data;
unsigned char userauth_pswd_data0; unsigned char userauth_pswd_data0;
unsigned long userauth_pswd_data_len; size_t userauth_pswd_data_len;
char *userauth_pswd_newpw; char *userauth_pswd_newpw;
int userauth_pswd_newpw_len; int userauth_pswd_newpw_len;
packet_requirev_state_t userauth_pswd_packet_requirev_state; packet_requirev_state_t userauth_pswd_packet_requirev_state;
@ -790,22 +791,22 @@ struct _LIBSSH2_SESSION
/* State variables used in libssh2_userauth_hostbased_fromfile_ex() */ /* State variables used in libssh2_userauth_hostbased_fromfile_ex() */
libssh2_nonblocking_states userauth_host_state; libssh2_nonblocking_states userauth_host_state;
unsigned char *userauth_host_data; unsigned char *userauth_host_data;
unsigned long userauth_host_data_len; size_t userauth_host_data_len;
unsigned char *userauth_host_packet; unsigned char *userauth_host_packet;
unsigned long userauth_host_packet_len; size_t userauth_host_packet_len;
unsigned char *userauth_host_method; unsigned char *userauth_host_method;
unsigned long userauth_host_method_len; size_t userauth_host_method_len;
unsigned char *userauth_host_s; unsigned char *userauth_host_s;
packet_requirev_state_t userauth_host_packet_requirev_state; packet_requirev_state_t userauth_host_packet_requirev_state;
/* State variables used in libssh2_userauth_publickey_fromfile_ex() */ /* State variables used in libssh2_userauth_publickey_fromfile_ex() */
libssh2_nonblocking_states userauth_pblc_state; libssh2_nonblocking_states userauth_pblc_state;
unsigned char *userauth_pblc_data; unsigned char *userauth_pblc_data;
unsigned long userauth_pblc_data_len; size_t userauth_pblc_data_len;
unsigned char *userauth_pblc_packet; unsigned char *userauth_pblc_packet;
unsigned long userauth_pblc_packet_len; size_t userauth_pblc_packet_len;
unsigned char *userauth_pblc_method; unsigned char *userauth_pblc_method;
unsigned long userauth_pblc_method_len; size_t userauth_pblc_method_len;
unsigned char *userauth_pblc_s; unsigned char *userauth_pblc_s;
unsigned char *userauth_pblc_b; unsigned char *userauth_pblc_b;
packet_requirev_state_t userauth_pblc_packet_requirev_state; packet_requirev_state_t userauth_pblc_packet_requirev_state;
@ -813,9 +814,9 @@ struct _LIBSSH2_SESSION
/* State variables used in libssh2_userauth_keyboard_interactive_ex() */ /* State variables used in libssh2_userauth_keyboard_interactive_ex() */
libssh2_nonblocking_states userauth_kybd_state; libssh2_nonblocking_states userauth_kybd_state;
unsigned char *userauth_kybd_data; unsigned char *userauth_kybd_data;
unsigned long userauth_kybd_data_len; size_t userauth_kybd_data_len;
unsigned char *userauth_kybd_packet; unsigned char *userauth_kybd_packet;
unsigned long userauth_kybd_packet_len; size_t userauth_kybd_packet_len;
unsigned int userauth_kybd_auth_name_len; unsigned int userauth_kybd_auth_name_len;
char *userauth_kybd_auth_name; char *userauth_kybd_auth_name;
unsigned userauth_kybd_auth_instruction_len; unsigned userauth_kybd_auth_instruction_len;
@ -831,17 +832,17 @@ struct _LIBSSH2_SESSION
packet_requirev_state_t open_packet_requirev_state; packet_requirev_state_t open_packet_requirev_state;
LIBSSH2_CHANNEL *open_channel; LIBSSH2_CHANNEL *open_channel;
unsigned char *open_packet; unsigned char *open_packet;
unsigned long open_packet_len; size_t open_packet_len;
unsigned char *open_data; unsigned char *open_data;
unsigned long open_data_len; size_t open_data_len;
unsigned long open_local_channel; uint32_t open_local_channel;
/* State variables used in libssh2_channel_direct_tcpip_ex() */ /* State variables used in libssh2_channel_direct_tcpip_ex() */
libssh2_nonblocking_states direct_state; libssh2_nonblocking_states direct_state;
unsigned char *direct_message; unsigned char *direct_message;
unsigned long direct_host_len; size_t direct_host_len;
unsigned long direct_shost_len; size_t direct_shost_len;
unsigned long direct_message_len; size_t direct_message_len;
/* State variables used in libssh2_channel_forward_listen_ex() */ /* State variables used in libssh2_channel_forward_listen_ex() */
libssh2_nonblocking_states fwdLstn_state; libssh2_nonblocking_states fwdLstn_state;
@ -855,7 +856,7 @@ struct _LIBSSH2_SESSION
LIBSSH2_PUBLICKEY *pkeyInit_pkey; LIBSSH2_PUBLICKEY *pkeyInit_pkey;
LIBSSH2_CHANNEL *pkeyInit_channel; LIBSSH2_CHANNEL *pkeyInit_channel;
unsigned char *pkeyInit_data; unsigned char *pkeyInit_data;
unsigned long pkeyInit_data_len; size_t pkeyInit_data_len;
/* State variables used in libssh2_packet_add() */ /* State variables used in libssh2_packet_add() */
libssh2_nonblocking_states packAdd_state; libssh2_nonblocking_states packAdd_state;
@ -955,18 +956,18 @@ struct _LIBSSH2_HOSTKEY_METHOD
unsigned long hash_len; unsigned long hash_len;
int (*init) (LIBSSH2_SESSION * session, const unsigned char *hostkey_data, int (*init) (LIBSSH2_SESSION * session, const unsigned char *hostkey_data,
unsigned long hostkey_data_len, void **abstract); size_t hostkey_data_len, void **abstract);
int (*initPEM) (LIBSSH2_SESSION * session, const char *privkeyfile, int (*initPEM) (LIBSSH2_SESSION * session, const char *privkeyfile,
unsigned const char *passphrase, void **abstract); unsigned const char *passphrase, void **abstract);
int (*sig_verify) (LIBSSH2_SESSION * session, const unsigned char *sig, int (*sig_verify) (LIBSSH2_SESSION * session, const unsigned char *sig,
unsigned long sig_len, const unsigned char *m, size_t sig_len, const unsigned char *m,
unsigned long m_len, void **abstract); size_t m_len, void **abstract);
int (*signv) (LIBSSH2_SESSION * session, unsigned char **signature, int (*signv) (LIBSSH2_SESSION * session, unsigned char **signature,
unsigned long *signature_len, unsigned long veccount, size_t *signature_len, int veccount,
const struct iovec datavec[], void **abstract); const struct iovec datavec[], void **abstract);
int (*encrypt) (LIBSSH2_SESSION * session, unsigned char **dst, int (*encrypt) (LIBSSH2_SESSION * session, unsigned char **dst,
unsigned long *dst_len, const unsigned char *src, size_t *dst_len, const unsigned char *src,
unsigned long src_len, void **abstract); size_t src_len, void **abstract);
int (*dtor) (LIBSSH2_SESSION * session, void **abstract); int (*dtor) (LIBSSH2_SESSION * session, void **abstract);
}; };
@ -1108,10 +1109,6 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
void _libssh2_session_shutdown(LIBSSH2_SESSION * session); void _libssh2_session_shutdown(LIBSSH2_SESSION * session);
unsigned int _libssh2_ntohu32(const unsigned char *buf);
libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf);
void _libssh2_htonu32(unsigned char *buf, unsigned int val);
#ifdef WIN32 #ifdef WIN32
ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer, size_t length, int flags); ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer, size_t length, int flags);
ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer, size_t length, int flags); ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer, size_t length, int flags);
@ -1126,53 +1123,6 @@ ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer, size_t length
int _libssh2_wait_socket(LIBSSH2_SESSION *session); int _libssh2_wait_socket(LIBSSH2_SESSION *session);
/* These started out as private return codes for the transport layer, but was
converted to using the library-wide return codes to easy propagation of the
error reasons all over etc without risking mixups. The PACKET_* names are
left only to reduce the impact of changing the code all over.*/
#define PACKET_TIMEOUT LIBSSH2_ERROR_TIMEOUT
#define PACKET_BADUSE LIBSSH2_ERROR_BAD_USE
#define PACKET_COMPRESS LIBSSH2_ERROR_COMPRESS
#define PACKET_TOOBIG LIBSSH2_ERROR_OUT_OF_BOUNDARY
#define PACKET_ENOMEM LIBSSH2_ERROR_ALLOC
#define PACKET_EAGAIN LIBSSH2_ERROR_EAGAIN
#define PACKET_FAIL LIBSSH2_ERROR_SOCKET_NONE
#define PACKET_NONE LIBSSH2_ERROR_NONE
int _libssh2_packet_read(LIBSSH2_SESSION * session);
int _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type,
unsigned char **data, unsigned long *data_len,
unsigned long match_ofs,
const unsigned char *match_buf,
unsigned long match_len);
int _libssh2_packet_askv(LIBSSH2_SESSION * session,
const unsigned char *packet_types,
unsigned char **data, unsigned long *data_len,
unsigned long match_ofs,
const unsigned char *match_buf,
unsigned long match_len);
int _libssh2_packet_require(LIBSSH2_SESSION * session,
unsigned char packet_type, unsigned char **data,
unsigned long *data_len, unsigned long match_ofs,
const unsigned char *match_buf,
unsigned long match_len,
packet_require_state_t * state);
int _libssh2_packet_requirev(LIBSSH2_SESSION * session,
const unsigned char *packet_types,
unsigned char **data, unsigned long *data_len,
unsigned long match_ofs,
const unsigned char *match_buf,
unsigned long match_len,
packet_requirev_state_t * state);
int _libssh2_packet_burn(LIBSSH2_SESSION * session,
libssh2_nonblocking_states * state);
int _libssh2_packet_write(LIBSSH2_SESSION * session, unsigned char *data,
unsigned long data_len);
int _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
size_t datalen, int macstate);
int libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange, int libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
key_exchange_state_t * state); key_exchange_state_t * state);
unsigned long _libssh2_channel_nextid(LIBSSH2_SESSION * session); unsigned long _libssh2_channel_nextid(LIBSSH2_SESSION * session);

View File

@ -145,7 +145,7 @@ _libssh2_ntohu64(const unsigned char *buf)
/* _libssh2_htonu32 /* _libssh2_htonu32
*/ */
void void
_libssh2_htonu32(unsigned char *buf, unsigned int value) _libssh2_htonu32(unsigned char *buf, uint32_t value)
{ {
buf[0] = (value >> 24) & 0xFF; buf[0] = (value >> 24) & 0xFF;
buf[1] = (value >> 16) & 0xFF; buf[1] = (value >> 16) & 0xFF;
@ -153,6 +153,25 @@ _libssh2_htonu32(unsigned char *buf, unsigned int value)
buf[3] = value & 0xFF; buf[3] = value & 0xFF;
} }
/* _libssh2_store_u32
*/
void _libssh2_store_u32(unsigned char **buf, uint32_t value)
{
_libssh2_htonu32(*buf, value);
*buf += sizeof(uint32_t);
}
/* _libssh2_store_str
*/
void _libssh2_store_str(unsigned char **buf, const char *str, size_t len)
{
_libssh2_store_u32(buf, (uint32_t)len);
if(len) {
memcpy(*buf, str, len);
*buf += len;
}
}
/* Base64 Conversion */ /* Base64 Conversion */
static const char base64_table[] = static const char base64_table[] =

View File

@ -71,4 +71,11 @@ void _libssh2_list_remove(struct list_node *entry);
size_t _libssh2_base64_encode(struct _LIBSSH2_SESSION *session, size_t _libssh2_base64_encode(struct _LIBSSH2_SESSION *session,
const char *inp, size_t insize, char **outptr); const char *inp, size_t insize, char **outptr);
unsigned int _libssh2_ntohu32(const unsigned char *buf);
libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf);
void _libssh2_htonu32(unsigned char *buf, uint32_t val);
void _libssh2_store_u32(unsigned char **buf, uint32_t value);
void _libssh2_store_str(unsigned char **buf, const char *str, size_t len);
#endif /* _LIBSSH2_MISC_H */ #endif /* _LIBSSH2_MISC_H */

View File

@ -385,8 +385,8 @@ int
_libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session, _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
libssh2_rsa_ctx * rsactx, libssh2_rsa_ctx * rsactx,
const unsigned char *hash, const unsigned char *hash,
unsigned long hash_len, size_t hash_len,
unsigned char **signature, unsigned long *signature_len) unsigned char **signature, size_t *signature_len)
{ {
int ret; int ret;
unsigned char *sig; unsigned char *sig;

View File

@ -162,9 +162,9 @@ int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx * rsa,
int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session, int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
libssh2_rsa_ctx * rsactx, libssh2_rsa_ctx * rsactx,
const unsigned char *hash, const unsigned char *hash,
unsigned long hash_len, size_t hash_len,
unsigned char **signature, unsigned char **signature,
unsigned long *signature_len); size_t *signature_len);
#define _libssh2_rsa_free(rsactx) RSA_free(rsactx) #define _libssh2_rsa_free(rsactx) RSA_free(rsactx)

View File

@ -62,6 +62,7 @@
#include "transport.h" #include "transport.h"
#include "channel.h" #include "channel.h"
#include "packet.h"
/* /*
* libssh2_packet_queue_listener * libssh2_packet_queue_listener
@ -209,7 +210,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
if (listen_state->state == libssh2_NB_state_created) { if (listen_state->state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, listen_state->packet, rc = _libssh2_transport_write(session, listen_state->packet,
17); 17);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
else if (rc) { else if (rc) {
listen_state->state = libssh2_NB_state_idle; listen_state->state = libssh2_NB_state_idle;
@ -249,7 +250,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
rc = _libssh2_transport_write(session, listen_state->packet, rc = _libssh2_transport_write(session, listen_state->packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
listen_state->state = libssh2_NB_state_idle; listen_state->state = libssh2_NB_state_idle;
@ -363,7 +364,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
if (x11open_state->state == libssh2_NB_state_created) { if (x11open_state->state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, x11open_state->packet, 17); rc = _libssh2_transport_write(session, x11open_state->packet, 17);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
x11open_state->state = libssh2_NB_state_idle; x11open_state->state = libssh2_NB_state_idle;
@ -403,7 +404,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
_libssh2_htonu32(p, 0); _libssh2_htonu32(p, 0);
rc = _libssh2_transport_write(session, x11open_state->packet, packet_len); rc = _libssh2_transport_write(session, x11open_state->packet, packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
x11open_state->state = libssh2_NB_state_idle; x11open_state->state = libssh2_NB_state_idle;
@ -608,7 +609,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
session->packAdd_state = libssh2_NB_state_jump5; session->packAdd_state = libssh2_NB_state_jump5;
data[0] = SSH_MSG_REQUEST_FAILURE; data[0] = SSH_MSG_REQUEST_FAILURE;
rc = _libssh2_transport_write(session, data, 1); rc = _libssh2_transport_write(session, data, 1);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->packAdd_state = libssh2_NB_state_idle; session->packAdd_state = libssh2_NB_state_idle;
@ -665,7 +666,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
packAdd_channel, packAdd_channel,
datalen - 13, datalen - 13,
0, NULL); 0, NULL);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
session->packAdd_state = libssh2_NB_state_idle; session->packAdd_state = libssh2_NB_state_idle;
@ -785,7 +786,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
session->packAdd_state = libssh2_NB_state_jump4; session->packAdd_state = libssh2_NB_state_jump4;
data[0] = SSH_MSG_CHANNEL_FAILURE; data[0] = SSH_MSG_CHANNEL_FAILURE;
rc = _libssh2_transport_write(session, data, 5); rc = _libssh2_transport_write(session, data, 5);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->packAdd_state = libssh2_NB_state_idle; session->packAdd_state = libssh2_NB_state_idle;
@ -827,7 +828,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
session->packAdd_state = libssh2_NB_state_jump2; session->packAdd_state = libssh2_NB_state_jump2;
rc = packet_queue_listener(session, data, datalen, rc = packet_queue_listener(session, data, datalen,
&session->packAdd_Qlstn_state); &session->packAdd_Qlstn_state);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
@ -842,7 +843,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
session->packAdd_state = libssh2_NB_state_jump3; session->packAdd_state = libssh2_NB_state_jump3;
rc = packet_x11_open(session, data, datalen, rc = packet_x11_open(session, data, datalen,
&session->packAdd_x11open_state); &session->packAdd_x11open_state);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
@ -941,7 +942,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
* send NEWKEYS yet, otherwise remote will drop us like a rock * send NEWKEYS yet, otherwise remote will drop us like a rock
*/ */
rc = libssh2_kex_exchange(session, 1, &session->startup_key_state); rc = libssh2_kex_exchange(session, 1, &session->startup_key_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
} }
@ -958,9 +959,9 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
*/ */
int int
_libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type, _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type,
unsigned char **data, unsigned long *data_len, unsigned char **data, size_t *data_len,
unsigned long match_ofs, const unsigned char *match_buf, int match_ofs, const unsigned char *match_buf,
unsigned long match_len) size_t match_len)
{ {
LIBSSH2_PACKET *packet = _libssh2_list_first(&session->packets); LIBSSH2_PACKET *packet = _libssh2_list_first(&session->packets);
@ -997,10 +998,10 @@ _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type,
int int
_libssh2_packet_askv(LIBSSH2_SESSION * session, _libssh2_packet_askv(LIBSSH2_SESSION * session,
const unsigned char *packet_types, const unsigned char *packet_types,
unsigned char **data, unsigned long *data_len, unsigned char **data, size_t *data_len,
unsigned long match_ofs, int match_ofs,
const unsigned char *match_buf, const unsigned char *match_buf,
unsigned long match_len) size_t match_len)
{ {
int i, packet_types_len = strlen((char *) packet_types); int i, packet_types_len = strlen((char *) packet_types);
@ -1026,10 +1027,10 @@ _libssh2_packet_askv(LIBSSH2_SESSION * session,
*/ */
int int
_libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type,
unsigned char **data, unsigned long *data_len, unsigned char **data, size_t *data_len,
unsigned long match_ofs, int match_ofs,
const unsigned char *match_buf, const unsigned char *match_buf,
unsigned long match_len, size_t match_len,
packet_require_state_t *state) packet_require_state_t *state)
{ {
if (state->start == 0) { if (state->start == 0) {
@ -1045,7 +1046,7 @@ _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type,
while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) { while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
int ret = _libssh2_transport_read(session); int ret = _libssh2_transport_read(session);
if (ret == PACKET_EAGAIN) if (ret == LIBSSH2_ERROR_EAGAIN)
return ret; return ret;
else if (ret < 0) { else if (ret < 0) {
state->start = 0; state->start = 0;
@ -1063,7 +1064,7 @@ _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type,
if (left <= 0) { if (left <= 0) {
state->start = 0; state->start = 0;
return PACKET_TIMEOUT; return LIBSSH2_ERROR_TIMEOUT;
} }
return -1; /* no packet available yet */ return -1; /* no packet available yet */
} }
@ -1085,7 +1086,7 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session,
libssh2_nonblocking_states * state) libssh2_nonblocking_states * state)
{ {
unsigned char *data; unsigned char *data;
unsigned long data_len; size_t data_len;
unsigned char all_packets[255]; unsigned char all_packets[255];
int i; int i;
int ret; int ret;
@ -1110,7 +1111,7 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session,
while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) { while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
ret = _libssh2_transport_read(session); ret = _libssh2_transport_read(session);
if (ret == PACKET_EAGAIN) { if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} else if (ret < 0) { } else if (ret < 0) {
*state = libssh2_NB_state_idle; *state = libssh2_NB_state_idle;
@ -1143,12 +1144,11 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session,
*/ */
int int
_libssh2_packet_requirev(LIBSSH2_SESSION * session, _libssh2_packet_requirev(LIBSSH2_SESSION *session,
const unsigned char *packet_types, const unsigned char *packet_types,
unsigned char **data, unsigned long *data_len, unsigned char **data, size_t *data_len,
unsigned long match_ofs, int match_ofs,
const unsigned char *match_buf, const unsigned char *match_buf, size_t match_len,
unsigned long match_len,
packet_requirev_state_t * state) packet_requirev_state_t * state)
{ {
if (_libssh2_packet_askv(session, packet_types, data, data_len, match_ofs, if (_libssh2_packet_askv(session, packet_types, data, data_len, match_ofs,
@ -1164,7 +1164,7 @@ _libssh2_packet_requirev(LIBSSH2_SESSION * session,
while (session->socket_state != LIBSSH2_SOCKET_DISCONNECTED) { while (session->socket_state != LIBSSH2_SOCKET_DISCONNECTED) {
int ret = _libssh2_transport_read(session); int ret = _libssh2_transport_read(session);
if ((ret < 0) && (ret != PACKET_EAGAIN)) { if ((ret < 0) && (ret != LIBSSH2_ERROR_EAGAIN)) {
state->start = 0; state->start = 0;
return ret; return ret;
} }
@ -1174,9 +1174,9 @@ _libssh2_packet_requirev(LIBSSH2_SESSION * session,
if (left <= 0) { if (left <= 0) {
state->start = 0; state->start = 0;
return PACKET_TIMEOUT; return LIBSSH2_ERROR_TIMEOUT;
} }
else if (ret == PACKET_EAGAIN) { else if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} }
} }

76
src/packet.h Normal file
View File

@ -0,0 +1,76 @@
#ifndef LIBSSH2_PACKET_H
#define LIBSSH2_PACKET_H
/*
* Copyright (C) 2010 by Daniel Stenberg
* Author: Daniel Stenberg <daniel@haxx.se>
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the copyright holder nor the names
* of any other contributors may be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
*/
int _libssh2_packet_read(LIBSSH2_SESSION * session);
int _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type,
unsigned char **data, size_t *data_len,
int match_ofs,
const unsigned char *match_buf,
size_t match_len);
int _libssh2_packet_askv(LIBSSH2_SESSION * session,
const unsigned char *packet_types,
unsigned char **data, size_t *data_len,
int match_ofs,
const unsigned char *match_buf,
size_t match_len);
int _libssh2_packet_require(LIBSSH2_SESSION * session,
unsigned char packet_type, unsigned char **data,
size_t *data_len, int match_ofs,
const unsigned char *match_buf,
size_t match_len,
packet_require_state_t * state);
int _libssh2_packet_requirev(LIBSSH2_SESSION *session,
const unsigned char *packet_types,
unsigned char **data, size_t *data_len,
int match_ofs,
const unsigned char *match_buf,
size_t match_len,
packet_requirev_state_t * state);
int _libssh2_packet_burn(LIBSSH2_SESSION * session,
libssh2_nonblocking_states * state);
int _libssh2_packet_write(LIBSSH2_SESSION * session, unsigned char *data,
unsigned long data_len);
int _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
size_t datalen, int macstate);
#endif /* LIBSSH2_PACKET_H */

View File

@ -128,7 +128,7 @@ publickey_status_error(const LIBSSH2_PUBLICKEY *pkey,
*/ */
static int static int
publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey, publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
unsigned char **data, unsigned long *data_len) unsigned char **data, size_t *data_len)
{ {
LIBSSH2_CHANNEL *channel = pkey->channel; LIBSSH2_CHANNEL *channel = pkey->channel;
LIBSSH2_SESSION *session = channel->session; LIBSSH2_SESSION *session = channel->session;
@ -137,7 +137,7 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
if (pkey->receive_state == libssh2_NB_state_idle) { if (pkey->receive_state == libssh2_NB_state_idle) {
rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4); rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc != 4) { } else if (rc != 4) {
return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
@ -159,7 +159,7 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
if (pkey->receive_state == libssh2_NB_state_sent) { if (pkey->receive_state == libssh2_NB_state_sent) {
rc = _libssh2_channel_read(channel, 0, (char *) pkey->receive_packet, rc = _libssh2_channel_read(channel, 0, (char *) pkey->receive_packet,
pkey->receive_packet_len); pkey->receive_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc != (int)pkey->receive_packet_len) { } else if (rc != (int)pkey->receive_packet_len) {
LIBSSH2_FREE(session, pkey->receive_packet); LIBSSH2_FREE(session, pkey->receive_packet);
@ -185,9 +185,9 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey,
* Data will be incremented by 4 + response_len on success only * Data will be incremented by 4 + response_len on success only
*/ */
static int static int
publickey_response_id(unsigned char **pdata, int data_len) publickey_response_id(unsigned char **pdata, size_t data_len)
{ {
unsigned long response_len; size_t response_len;
unsigned char *data = *pdata; unsigned char *data = *pdata;
const LIBSSH2_PUBLICKEY_CODE_LIST *codes = publickey_response_codes; const LIBSSH2_PUBLICKEY_CODE_LIST *codes = publickey_response_codes;
@ -198,7 +198,7 @@ publickey_response_id(unsigned char **pdata, int data_len)
response_len = _libssh2_ntohu32(data); response_len = _libssh2_ntohu32(data);
data += 4; data += 4;
data_len -= 4; data_len -= 4;
if (data_len < (int)response_len) { if (data_len < response_len) {
/* Malformed response */ /* Malformed response */
return -1; return -1;
} }
@ -224,13 +224,13 @@ publickey_response_success(LIBSSH2_PUBLICKEY * pkey)
{ {
LIBSSH2_SESSION *session = pkey->channel->session; LIBSSH2_SESSION *session = pkey->channel->session;
unsigned char *data, *s; unsigned char *data, *s;
unsigned long data_len; size_t data_len;
int response; int response;
int rc; int rc;
while (1) { while (1) {
rc = publickey_packet_receive(pkey, &data, &data_len); rc = publickey_packet_receive(pkey, &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
@ -332,7 +332,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
"subsystem", "subsystem",
sizeof("subsystem") - 1, sizeof("subsystem") - 1,
"publickey", strlen("publickey")); "publickey", strlen("publickey"));
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting publickey subsystem"); "Would block starting publickey subsystem");
return NULL; return NULL;
@ -348,7 +348,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
if (session->pkeyInit_state == libssh2_NB_state_sent1) { if (session->pkeyInit_state == libssh2_NB_state_sent1) {
rc = libssh2_channel_handle_extended_data2(session->pkeyInit_channel, rc = libssh2_channel_handle_extended_data2(session->pkeyInit_channel,
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block starting publickey subsystem"); "Would block starting publickey subsystem");
return NULL; return NULL;
@ -385,7 +385,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
if (session->pkeyInit_state == libssh2_NB_state_sent2) { if (session->pkeyInit_state == libssh2_NB_state_sent2) {
rc = _libssh2_channel_write(session->pkeyInit_channel, 0, rc = _libssh2_channel_write(session->pkeyInit_channel, 0,
(char *) buffer, (s - buffer)); (char *) buffer, (s - buffer));
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending publickey version packet"); "Would block sending publickey version packet");
return NULL; return NULL;
@ -403,7 +403,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
rc = publickey_packet_receive(session->pkeyInit_pkey, rc = publickey_packet_receive(session->pkeyInit_pkey,
&session->pkeyInit_data, &session->pkeyInit_data,
&session->pkeyInit_data_len); &session->pkeyInit_data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from " "Would block waiting for response from "
"publickey subsystem"); "publickey subsystem");
@ -489,7 +489,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session)
session->pkeyInit_state = libssh2_NB_state_sent4; session->pkeyInit_state = libssh2_NB_state_sent4;
if (session->pkeyInit_channel) { if (session->pkeyInit_channel) {
rc = libssh2_channel_close(session->pkeyInit_channel); rc = libssh2_channel_close(session->pkeyInit_channel);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block closing channel"); "Would block closing channel");
return NULL; return NULL;
@ -620,7 +620,7 @@ libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY * pkey, const unsigned char *name,
if (pkey->add_state == libssh2_NB_state_created) { if (pkey->add_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) pkey->add_packet, rc = _libssh2_channel_write(channel, 0, (char *) pkey->add_packet,
(pkey->add_s - pkey->add_packet)); (pkey->add_s - pkey->add_packet));
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if ((pkey->add_s - pkey->add_packet) != rc) { } else if ((pkey->add_s - pkey->add_packet) != rc) {
LIBSSH2_FREE(session, pkey->add_packet); LIBSSH2_FREE(session, pkey->add_packet);
@ -635,7 +635,7 @@ libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY * pkey, const unsigned char *name,
} }
rc = publickey_response_success(pkey); rc = publickey_response_success(pkey);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
@ -694,7 +694,7 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey,
if (pkey->remove_state == libssh2_NB_state_created) { if (pkey->remove_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) pkey->remove_packet, rc = _libssh2_channel_write(channel, 0, (char *) pkey->remove_packet,
(pkey->remove_s - pkey->remove_packet)); (pkey->remove_s - pkey->remove_packet));
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if ((pkey->remove_s - pkey->remove_packet) != rc) { } else if ((pkey->remove_s - pkey->remove_packet) != rc) {
LIBSSH2_FREE(session, pkey->remove_packet); LIBSSH2_FREE(session, pkey->remove_packet);
@ -710,7 +710,7 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey,
} }
rc = publickey_response_success(pkey); rc = publickey_response_success(pkey);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
@ -756,7 +756,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
(char *) pkey->listFetch_buffer, (char *) pkey->listFetch_buffer,
(pkey->listFetch_s - (pkey->listFetch_s -
pkey->listFetch_buffer)); pkey->listFetch_buffer));
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if ((pkey->listFetch_s - pkey->listFetch_buffer) != rc) { } else if ((pkey->listFetch_s - pkey->listFetch_buffer) != rc) {
pkey->listFetch_state = libssh2_NB_state_idle; pkey->listFetch_state = libssh2_NB_state_idle;
@ -770,7 +770,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys,
while (1) { while (1) {
rc = publickey_packet_receive(pkey, &pkey->listFetch_data, rc = publickey_packet_receive(pkey, &pkey->listFetch_data,
&pkey->listFetch_data_len); &pkey->listFetch_data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
_libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
@ -997,7 +997,7 @@ libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY * pkey)
} }
rc = libssh2_channel_free(pkey->channel); rc = libssh2_channel_free(pkey->channel);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
LIBSSH2_FREE(session, pkey); LIBSSH2_FREE(session, pkey);

View File

@ -338,7 +338,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
sizeof("exec") - 1, sizeof("exec") - 1,
(char *) session->scpRecv_command, (char *) session->scpRecv_command,
session->scpRecv_command_len); session->scpRecv_command_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting SCP startup"); "Would block requesting SCP startup");
return NULL; return NULL;
@ -360,7 +360,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
if (session->scpRecv_state == libssh2_NB_state_sent1) { if (session->scpRecv_state == libssh2_NB_state_sent1) {
rc = _libssh2_channel_write(session->scpRecv_channel, 0, rc = _libssh2_channel_write(session->scpRecv_channel, 0,
(char *) session->scpRecv_response, 1); (char *) session->scpRecv_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending initial wakeup"); "Would block sending initial wakeup");
return NULL; return NULL;
@ -385,7 +385,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
(char *) session-> (char *) session->
scpRecv_response + scpRecv_response +
session->scpRecv_response_len, 1); session->scpRecv_response_len, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for SCP response"); "Would block waiting for SCP response");
return NULL; return NULL;
@ -424,7 +424,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
/* /*
* Since we have alread started reading this packet, * Since we have alread started reading this packet,
* it is already in the systems so it can't return * it is already in the systems so it can't return
* PACKET_EAGAIN * LIBSSH2_ERROR_EAGAIN
*/ */
_libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL,
"Unknown error" ); "Unknown error" );
@ -557,7 +557,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
rc = _libssh2_channel_write(session->scpRecv_channel, 0, rc = _libssh2_channel_write(session->scpRecv_channel, 0,
(char *) session-> (char *) session->
scpRecv_response, 1); scpRecv_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting to send SCP ACK"); "Would block waiting to send SCP ACK");
return NULL; return NULL;
@ -594,7 +594,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
(char *) session-> (char *) session->
scpRecv_response + scpRecv_response +
session->scpRecv_response_len, 1); session->scpRecv_response_len, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for SCP response"); "Would block waiting for SCP response");
return NULL; return NULL;
@ -714,7 +714,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
rc = _libssh2_channel_write(session->scpRecv_channel, 0, rc = _libssh2_channel_write(session->scpRecv_channel, 0,
(char *) session-> (char *) session->
scpRecv_response, 1); scpRecv_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending SCP ACK"); "Would block sending SCP ACK");
return NULL; return NULL;
@ -747,7 +747,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb)
return session->scpRecv_channel; return session->scpRecv_channel;
scp_recv_error: scp_recv_error:
while (libssh2_channel_free(session->scpRecv_channel) == PACKET_EAGAIN); while (libssh2_channel_free(session->scpRecv_channel) == LIBSSH2_ERROR_EAGAIN);
session->scpRecv_channel = NULL; session->scpRecv_channel = NULL;
session->scpRecv_state = libssh2_NB_state_idle; session->scpRecv_state = libssh2_NB_state_idle;
return NULL; return NULL;
@ -841,7 +841,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
sizeof("exec") - 1, sizeof("exec") - 1,
(char *) session->scpSend_command, (char *) session->scpSend_command,
session->scpSend_command_len); session->scpSend_command_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting SCP startup"); "Would block requesting SCP startup");
return NULL; return NULL;
@ -865,7 +865,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
/* Wait for ACK */ /* Wait for ACK */
rc = _libssh2_channel_read(session->scpSend_channel, 0, rc = _libssh2_channel_read(session->scpSend_channel, 0,
(char *) session->scpSend_response, 1); (char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from remote"); "Would block waiting for response from remote");
return NULL; return NULL;
@ -894,7 +894,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
rc = _libssh2_channel_write(session->scpSend_channel, 0, rc = _libssh2_channel_write(session->scpSend_channel, 0,
(char *) session->scpSend_response, (char *) session->scpSend_response,
session->scpSend_response_len); session->scpSend_response_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending time data for SCP file"); "Would block sending time data for SCP file");
return NULL; return NULL;
@ -911,7 +911,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
/* Wait for ACK */ /* Wait for ACK */
rc = _libssh2_channel_read(session->scpSend_channel, 0, rc = _libssh2_channel_read(session->scpSend_channel, 0,
(char *) session->scpSend_response, 1); (char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response"); "Would block waiting for response");
return NULL; return NULL;
@ -952,7 +952,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
rc = _libssh2_channel_write(session->scpSend_channel, 0, rc = _libssh2_channel_write(session->scpSend_channel, 0,
(char *) session->scpSend_response, (char *) session->scpSend_response,
session->scpSend_response_len); session->scpSend_response_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block send core file data for SCP file"); "Would block send core file data for SCP file");
return NULL; return NULL;
@ -969,7 +969,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
/* Wait for ACK */ /* Wait for ACK */
rc = _libssh2_channel_read(session->scpSend_channel, 0, rc = _libssh2_channel_read(session->scpSend_channel, 0,
(char *) session->scpSend_response, 1); (char *) session->scpSend_response, 1);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response"); "Would block waiting for response");
return NULL; return NULL;
@ -1001,7 +1001,8 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
if (rc <= 0) { if (rc <= 0) {
/* /*
* Since we have alread started reading this packet, it is * Since we have alread started reading this packet, it is
* already in the systems so it can't return PACKET_EAGAIN * already in the systems so it can't return
* LIBSSH2_ERROR_EAGAIN
*/ */
LIBSSH2_FREE(session, session->scpSend_err_msg); LIBSSH2_FREE(session, session->scpSend_err_msg);
session->scpSend_err_msg = NULL; session->scpSend_err_msg = NULL;
@ -1018,7 +1019,8 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode,
return session->scpSend_channel; return session->scpSend_channel;
scp_send_error: scp_send_error:
while (libssh2_channel_free(session->scpSend_channel) == PACKET_EAGAIN); while (libssh2_channel_free(session->scpSend_channel) ==
LIBSSH2_ERROR_EAGAIN);
session->scpSend_channel = NULL; session->scpSend_channel = NULL;
session->scpSend_state = libssh2_NB_state_idle; session->scpSend_state = libssh2_NB_state_idle;
return NULL; return NULL;

View File

@ -86,7 +86,7 @@ LIBSSH2_REALLOC_FUNC(libssh2_default_realloc)
* *
* Wait for a hello from the remote host * Wait for a hello from the remote host
* Allocate a buffer and store the banner in session->remote.banner * Allocate a buffer and store the banner in session->remote.banner
* Returns: 0 on success, PACKET_EAGAIN if read would block, negative on failure * Returns: 0 on success, LIBSSH2_ERROR_EAGAIN if read would block, negative on failure
*/ */
static int static int
banner_receive(LIBSSH2_SESSION * session) banner_receive(LIBSSH2_SESSION * session)
@ -124,7 +124,7 @@ banner_receive(LIBSSH2_SESSION * session)
session->socket_block_directions = session->socket_block_directions =
LIBSSH2_SESSION_BLOCK_INBOUND; LIBSSH2_SESSION_BLOCK_INBOUND;
session->banner_TxRx_total_send = banner_len; session->banner_TxRx_total_send = banner_len;
return PACKET_EAGAIN; return LIBSSH2_ERROR_EAGAIN;
} }
/* Some kinda error */ /* Some kinda error */
@ -135,7 +135,7 @@ banner_receive(LIBSSH2_SESSION * session)
if (ret == 0) { if (ret == 0) {
session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; session->socket_state = LIBSSH2_SOCKET_DISCONNECTED;
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
if (c == '\0') { if (c == '\0') {
@ -178,7 +178,7 @@ banner_receive(LIBSSH2_SESSION * session)
* *
* Send the default banner, or the one set via libssh2_setopt_string * Send the default banner, or the one set via libssh2_setopt_string
* *
* Returns PACKET_EAGAIN if it would block - and if it does so, you should * Returns LIBSSH2_ERROR_EAGAIN if it would block - and if it does so, you should
* call this function again as soon as it is likely that more data can be * call this function again as soon as it is likely that more data can be
* sent, and this function should then be called with the same argument set * sent, and this function should then be called with the same argument set
* (same data pointer and same data_len) until zero or failure is returned. * (same data pointer and same data_len) until zero or failure is returned.
@ -239,11 +239,11 @@ banner_send(LIBSSH2_SESSION * session)
session->socket_block_directions = session->socket_block_directions =
LIBSSH2_SESSION_BLOCK_OUTBOUND; LIBSSH2_SESSION_BLOCK_OUTBOUND;
session->banner_TxRx_total_send += ret; session->banner_TxRx_total_send += ret;
return PACKET_EAGAIN; return LIBSSH2_ERROR_EAGAIN;
} }
session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_state = libssh2_NB_state_idle;
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
/* Set the state back to idle */ /* Set the state back to idle */
@ -738,7 +738,7 @@ session_free(LIBSSH2_SESSION *session)
while ((ch = _libssh2_list_first(&session->channels))) { while ((ch = _libssh2_list_first(&session->channels))) {
rc = libssh2_channel_free(ch); rc = libssh2_channel_free(ch);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
#if 0 #if 0
/* Daniel's note: I'm leaving this code here right now since it /* Daniel's note: I'm leaving this code here right now since it
@ -766,7 +766,7 @@ session_free(LIBSSH2_SESSION *session)
if (session->state == libssh2_NB_state_sent) { if (session->state == libssh2_NB_state_sent) {
while ((l = _libssh2_list_first(&session->listeners))) { while ((l = _libssh2_list_first(&session->listeners))) {
rc = libssh2_channel_forward_cancel(l); rc = libssh2_channel_forward_cancel(l);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return rc; return rc;
} }
@ -1010,29 +1010,16 @@ session_disconnect(LIBSSH2_SESSION *session, int reason,
} }
*(s++) = SSH_MSG_DISCONNECT; *(s++) = SSH_MSG_DISCONNECT;
_libssh2_htonu32(s, reason); _libssh2_store_u32(&s, reason);
s += 4; _libssh2_store_str(&s, description, descr_len);
_libssh2_store_str(&s, lang, lang_len);
_libssh2_htonu32(s, descr_len);
s += 4;
if (description) {
memcpy(s, description, descr_len);
s += descr_len;
}
_libssh2_htonu32(s, lang_len);
s += 4;
if (lang) {
memcpy(s, lang, lang_len);
s += lang_len;
}
session->disconnect_state = libssh2_NB_state_created; session->disconnect_state = libssh2_NB_state_created;
} }
rc = _libssh2_transport_write(session, session->disconnect_data, rc = _libssh2_transport_write(session, session->disconnect_data,
session->disconnect_data_len); session->disconnect_data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }

View File

@ -88,12 +88,12 @@
static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle); static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle);
/* libssh2_htonu64 /* _libssh2_store_u64
*/ */
static void static void _libssh2_store_u64(unsigned char **ptr, libssh2_uint64_t value)
_libssh2_htonu64(unsigned char *buf, libssh2_uint64_t value)
{ {
unsigned long msl = (unsigned long)(value >> 32); unsigned long msl = (unsigned long)(value >> 32);
unsigned char *buf = *ptr;
buf[0] = (unsigned char)((msl >> 24) & 0xFF); buf[0] = (unsigned char)((msl >> 24) & 0xFF);
buf[1] = (unsigned char)((msl >> 16) & 0xFF); buf[1] = (unsigned char)((msl >> 16) & 0xFF);
@ -104,6 +104,8 @@ _libssh2_htonu64(unsigned char *buf, libssh2_uint64_t value)
buf[5] = (unsigned char)((value >> 16) & 0xFF); buf[5] = (unsigned char)((value >> 16) & 0xFF);
buf[6] = (unsigned char)((value >> 8) & 0xFF); buf[6] = (unsigned char)((value >> 8) & 0xFF);
buf[7] = (unsigned char)( value & 0xFF); buf[7] = (unsigned char)( value & 0xFF);
*ptr += 8;
} }
/* /*
@ -167,7 +169,7 @@ sftp_packet_read(LIBSSH2_SFTP *sftp)
} }
else { else {
rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4); rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (4 != rc) { else if (4 != rc) {
@ -202,7 +204,7 @@ sftp_packet_read(LIBSSH2_SFTP *sftp)
(char *) packet + packet_received, (char *) packet + packet_received,
packet_len - packet_received); packet_len - packet_received);
if (bytes_received == PACKET_EAGAIN) { if (bytes_received == LIBSSH2_ERROR_EAGAIN) {
/* /*
* We received EAGAIN, save what we have and * We received EAGAIN, save what we have and
* return to EAGAIN to the caller * return to EAGAIN to the caller
@ -300,7 +302,7 @@ sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type,
while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) { while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
ret = sftp_packet_read(sftp); ret = sftp_packet_read(sftp);
if (ret == PACKET_EAGAIN) { if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} else if (ret <= 0) { } else if (ret <= 0) {
return -1; return -1;
@ -350,7 +352,7 @@ sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
} }
ret = sftp_packet_read(sftp); ret = sftp_packet_read(sftp);
if ((ret < 0) && (ret != PACKET_EAGAIN)) { if ((ret < 0) && (ret != LIBSSH2_ERROR_EAGAIN)) {
sftp->requirev_start = 0; sftp->requirev_start = 0;
return -1; return -1;
} else if (ret <= 0) { } else if (ret <= 0) {
@ -360,9 +362,9 @@ sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
if (left <= 0) { if (left <= 0) {
sftp->requirev_start = 0; sftp->requirev_start = 0;
return PACKET_TIMEOUT; return LIBSSH2_ERROR_TIMEOUT;
} }
else if (ret == PACKET_EAGAIN) { else if (ret == LIBSSH2_ERROR_EAGAIN) {
return ret; return ret;
} }
} }
@ -415,31 +417,24 @@ sftp_attr2bin(unsigned char *p, const LIBSSH2_SFTP_ATTRIBUTES * attrs)
return 4; return 4;
} }
_libssh2_htonu32(s, attrs->flags & flag_mask); _libssh2_store_u32(&s, attrs->flags & flag_mask);
s += 4;
if (attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) { if (attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
_libssh2_htonu64(s, attrs->filesize); _libssh2_store_u64(&s, attrs->filesize);
s += 8;
} }
if (attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) { if (attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
_libssh2_htonu32(s, attrs->uid); _libssh2_store_u32(&s, attrs->uid);
s += 4; _libssh2_store_u32(&s, attrs->gid);
_libssh2_htonu32(s, attrs->gid);
s += 4;
} }
if (attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { if (attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
_libssh2_htonu32(s, attrs->permissions); _libssh2_store_u32(&s, attrs->permissions);
s += 4;
} }
if (attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) { if (attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
_libssh2_htonu32(s, attrs->atime); _libssh2_store_u32(&s, attrs->atime);
s += 4; _libssh2_store_u32(&s, attrs->mtime);
_libssh2_htonu32(s, attrs->mtime);
s += 4;
} }
return (s - p); return (s - p);
@ -573,7 +568,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
"subsystem", "subsystem",
sizeof("subsystem") - 1, "sftp", sizeof("subsystem") - 1, "sftp",
strlen("sftp")); strlen("sftp"));
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block to request SFTP subsystem"); "Would block to request SFTP subsystem");
return NULL; return NULL;
@ -589,7 +584,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
if (session->sftpInit_state == libssh2_NB_state_sent1) { if (session->sftpInit_state == libssh2_NB_state_sent1) {
rc = _libssh2_channel_extended_data(session->sftpInit_channel, rc = _libssh2_channel_extended_data(session->sftpInit_channel,
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting handle extended data"); "Would block requesting handle extended data");
return NULL; return NULL;
@ -625,7 +620,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
(char *)session->sftpInit_buffer + (char *)session->sftpInit_buffer +
session->sftpInit_sent, session->sftpInit_sent,
9 - session->sftpInit_sent); 9 - session->sftpInit_sent);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending SSH_FXP_INIT"); "Would block sending SSH_FXP_INIT");
return NULL; return NULL;
@ -649,7 +644,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION, rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION,
0, &data, &data_len); 0, &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for response from SFTP subsystem"); "Would block waiting for response from SFTP subsystem");
return NULL; return NULL;
@ -710,7 +705,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
return sftp_handle; return sftp_handle;
sftp_init_error: sftp_init_error:
while (_libssh2_channel_free(session->sftpInit_channel) == PACKET_EAGAIN); while (_libssh2_channel_free(session->sftpInit_channel) == LIBSSH2_ERROR_EAGAIN);
session->sftpInit_channel = NULL; session->sftpInit_channel = NULL;
if (session->sftpInit_sftp) { if (session->sftpInit_sftp) {
LIBSSH2_FREE(session, session->sftpInit_sftp); LIBSSH2_FREE(session, session->sftpInit_sftp);
@ -852,21 +847,15 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
LIBSSH2_SFTP_OPENFILE) ? LIBSSH2_SFTP_ATTR_PFILETYPE_FILE : LIBSSH2_SFTP_OPENFILE) ? LIBSSH2_SFTP_ATTR_PFILETYPE_FILE :
LIBSSH2_SFTP_ATTR_PFILETYPE_DIR); LIBSSH2_SFTP_ATTR_PFILETYPE_DIR);
_libssh2_htonu32(s, sftp->open_packet_len - 4); _libssh2_store_u32(&s, sftp->open_packet_len - 4);
s += 4; *(s++) = (open_type ==
*(s++) = LIBSSH2_SFTP_OPENFILE) ? SSH_FXP_OPEN : SSH_FXP_OPENDIR;
(open_type ==
LIBSSH2_SFTP_OPENFILE) ? SSH_FXP_OPEN : SSH_FXP_OPENDIR;
sftp->open_request_id = sftp->request_id++; sftp->open_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->open_request_id); _libssh2_store_u32(&s, sftp->open_request_id);
s += 4; _libssh2_store_str(&s, filename, filename_len);
_libssh2_htonu32(s, filename_len);
s += 4;
memcpy(s, filename, filename_len);
s += filename_len;
if (open_type == LIBSSH2_SFTP_OPENFILE) { if (open_type == LIBSSH2_SFTP_OPENFILE) {
_libssh2_htonu32(s, flags); _libssh2_store_u32(&s, flags);
s += 4;
s += sftp_attr2bin(s, &attrs); s += sftp_attr2bin(s, &attrs);
} }
@ -880,7 +869,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
if (sftp->open_state == libssh2_NB_state_created) { if (sftp->open_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->open_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->open_packet,
sftp->open_packet_len); sftp->open_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block sending FXP_OPEN or FXP_OPENDIR command"); "Would block sending FXP_OPEN or FXP_OPENDIR command");
return NULL; return NULL;
@ -906,7 +895,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
rc = sftp_packet_requirev(sftp, 2, fopen_responses, rc = sftp_packet_requirev(sftp, 2, fopen_responses,
sftp->open_request_id, &data, sftp->open_request_id, &data,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting for status message"); "Would block waiting for status message");
return NULL; return NULL;
@ -934,7 +923,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
/* silly situation, but check for a HANDLE */ /* silly situation, but check for a HANDLE */
rc = sftp_packet_require(sftp, SSH_FXP_HANDLE, rc = sftp_packet_require(sftp, SSH_FXP_HANDLE,
sftp->open_request_id, &data, &data_len); sftp->open_request_id, &data, &data_len);
if(rc == PACKET_EAGAIN) { if(rc == LIBSSH2_ERROR_EAGAIN) {
/* go back to sent state and wait for something else */ /* go back to sent state and wait for something else */
sftp->open_state = libssh2_NB_state_sent; sftp->open_state = libssh2_NB_state_sent;
return NULL; return NULL;
@ -1067,23 +1056,13 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
#endif #endif
if (sftp->read_state == libssh2_NB_state_allocated) { if (sftp->read_state == libssh2_NB_state_allocated) {
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_READ; *(s++) = SSH_FXP_READ;
request_id = sftp->request_id++; request_id = sftp->request_id++;
_libssh2_htonu32(s, request_id); _libssh2_store_u32(&s, request_id);
s += 4; _libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_htonu32(s, handle->handle_len); _libssh2_store_u64(&s, handle->u.file.offset);
s += 4; _libssh2_store_u32(&s, bytes_requested);
memcpy(s, handle->handle, handle->handle_len);
s += handle->handle_len;
_libssh2_htonu64(s, handle->u.file.offset);
s += 8;
_libssh2_htonu32(s, bytes_requested);
s += 4;
sftp->read_state = libssh2_NB_state_created; sftp->read_state = libssh2_NB_state_created;
} }
@ -1091,7 +1070,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
if (sftp->read_state == libssh2_NB_state_created) { if (sftp->read_state == libssh2_NB_state_created) {
retcode = _libssh2_channel_write(channel, 0, (char *) packet, retcode = _libssh2_channel_write(channel, 0, (char *) packet,
packet_len); packet_len);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
sftp->read_packet = packet; sftp->read_packet = packet;
sftp->read_request_id = request_id; sftp->read_request_id = request_id;
sftp->read_total_read = total_read; sftp->read_total_read = total_read;
@ -1114,7 +1093,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
retcode = retcode =
sftp_packet_requirev(sftp, 2, read_responses, sftp_packet_requirev(sftp, 2, read_responses,
request_id, &data, &data_len); request_id, &data, &data_len);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, retcode, return _libssh2_error(session, retcode,
"Would block waiting for status message"); "Would block waiting for status message");
} else if (retcode) { } else if (retcode) {
@ -1264,16 +1243,11 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
"Unable to allocate memory for " "Unable to allocate memory for "
"FXP_READDIR packet"); "FXP_READDIR packet");
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_READDIR; *(s++) = SSH_FXP_READDIR;
sftp->readdir_request_id = sftp->request_id++; sftp->readdir_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->readdir_request_id); _libssh2_store_u32(&s, sftp->readdir_request_id);
s += 4; _libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_htonu32(s, handle->handle_len);
s += 4;
memcpy(s, handle->handle, handle->handle_len);
s += handle->handle_len;
sftp->readdir_state = libssh2_NB_state_created; sftp->readdir_state = libssh2_NB_state_created;
} }
@ -1284,7 +1258,7 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
retcode = _libssh2_channel_write(channel, 0, retcode = _libssh2_channel_write(channel, 0,
(char *) sftp->readdir_packet, (char *) sftp->readdir_packet,
packet_len); packet_len);
if (retcode == PACKET_EAGAIN) { if (retcode == LIBSSH2_ERROR_EAGAIN) {
return retcode; return retcode;
} }
else if (packet_len != retcode) { else if (packet_len != retcode) {
@ -1304,7 +1278,7 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
retcode = sftp_packet_requirev(sftp, 2, read_responses, retcode = sftp_packet_requirev(sftp, 2, read_responses,
sftp->readdir_request_id, &data, sftp->readdir_request_id, &data,
&data_len); &data_len);
if (retcode == PACKET_EAGAIN) if (retcode == LIBSSH2_ERROR_EAGAIN)
return retcode; return retcode;
else if (retcode) { else if (retcode) {
sftp->readdir_state = libssh2_NB_state_idle; sftp->readdir_state = libssh2_NB_state_idle;
@ -1398,22 +1372,14 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_WRITE"); "Unable to allocate memory for FXP_WRITE");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_WRITE; *(s++) = SSH_FXP_WRITE;
sftp->write_request_id = sftp->request_id++; sftp->write_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->write_request_id); _libssh2_store_u32(&s, sftp->write_request_id);
s += 4; _libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_htonu32(s, handle->handle_len); _libssh2_store_u64(&s, handle->u.file.offset);
s += 4; _libssh2_store_str(&s, buffer, count);
memcpy(s, handle->handle, handle->handle_len);
s += handle->handle_len;
_libssh2_htonu64(s, handle->u.file.offset);
s += 8;
_libssh2_htonu32(s, count);
s += 4;
memcpy(s, buffer, count);
s += count;
sftp->write_state = libssh2_NB_state_created; sftp->write_state = libssh2_NB_state_created;
} }
@ -1439,7 +1405,7 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->write_request_id, &data, &data_len); sftp->write_request_id, &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc) { else if (rc) {
@ -1507,16 +1473,12 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
"FSTAT/FSETSTAT packet"); "FSTAT/FSETSTAT packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT; *(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT;
sftp->fstat_request_id = sftp->request_id++; sftp->fstat_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->fstat_request_id); _libssh2_store_u32(&s, sftp->fstat_request_id);
s += 4; _libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_htonu32(s, handle->handle_len);
s += 4;
memcpy(s, handle->handle, handle->handle_len);
s += handle->handle_len;
if (setstat) { if (setstat) {
s += sftp_attr2bin(s, attrs); s += sftp_attr2bin(s, attrs);
} }
@ -1527,7 +1489,7 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
if (sftp->fstat_state == libssh2_NB_state_created) { if (sftp->fstat_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->fstat_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->fstat_packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, sftp->fstat_packet); LIBSSH2_FREE(session, sftp->fstat_packet);
@ -1546,7 +1508,7 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
rc = sftp_packet_requirev(sftp, 2, fstat_responses, rc = sftp_packet_requirev(sftp, 2, fstat_responses,
sftp->fstat_request_id, &data, sftp->fstat_request_id, &data,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
sftp->fstat_state = libssh2_NB_state_idle; sftp->fstat_state = libssh2_NB_state_idle;
@ -1655,24 +1617,18 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
"packet"); "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_CLOSE; *(s++) = SSH_FXP_CLOSE;
handle->close_request_id = sftp->request_id++; handle->close_request_id = sftp->request_id++;
_libssh2_htonu32(s, handle->close_request_id); _libssh2_store_u32(&s, handle->close_request_id);
s += 4; _libssh2_store_str(&s, handle->handle, handle->handle_len);
_libssh2_htonu32(s, handle->handle_len);
s += 4;
memcpy(s, handle->handle, handle->handle_len);
s += handle->handle_len;
handle->close_state = libssh2_NB_state_created; handle->close_state = libssh2_NB_state_created;
} }
if (handle->close_state == libssh2_NB_state_created) { if (handle->close_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) handle->close_packet, rc = _libssh2_channel_write(channel, 0, (char *) handle->close_packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, handle->close_packet); LIBSSH2_FREE(session, handle->close_packet);
@ -1691,7 +1647,7 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
handle->close_request_id, &data, handle->close_request_id, &data,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
handle->close_state = libssh2_NB_state_idle; handle->close_state = libssh2_NB_state_idle;
@ -1764,24 +1720,18 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
"packet"); "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_REMOVE; *(s++) = SSH_FXP_REMOVE;
sftp->unlink_request_id = sftp->request_id++; sftp->unlink_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->unlink_request_id); _libssh2_store_u32(&s, sftp->unlink_request_id);
s += 4; _libssh2_store_str(&s, filename, filename_len);
_libssh2_htonu32(s, filename_len);
s += 4;
memcpy(s, filename, filename_len);
s += filename_len;
sftp->unlink_state = libssh2_NB_state_created; sftp->unlink_state = libssh2_NB_state_created;
} }
if (sftp->unlink_state == libssh2_NB_state_created) { if (sftp->unlink_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->unlink_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->unlink_packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, sftp->unlink_packet); LIBSSH2_FREE(session, sftp->unlink_packet);
@ -1799,7 +1749,7 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->unlink_request_id, &data, sftp->unlink_request_id, &data,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc) { else if (rc) {
@ -1873,25 +1823,16 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
"packet"); "packet");
} }
_libssh2_htonu32(sftp->rename_s, packet_len - 4); _libssh2_store_u32(&sftp->rename_s, packet_len - 4);
sftp->rename_s += 4;
*(sftp->rename_s++) = SSH_FXP_RENAME; *(sftp->rename_s++) = SSH_FXP_RENAME;
sftp->rename_request_id = sftp->request_id++; sftp->rename_request_id = sftp->request_id++;
_libssh2_htonu32(sftp->rename_s, sftp->rename_request_id); _libssh2_store_u32(&sftp->rename_s, sftp->rename_request_id);
sftp->rename_s += 4; _libssh2_store_str(&sftp->rename_s, source_filename,
_libssh2_htonu32(sftp->rename_s, source_filename_len); source_filename_len);
sftp->rename_s += 4; _libssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len);
memcpy(sftp->rename_s, source_filename, source_filename_len);
sftp->rename_s += source_filename_len;
_libssh2_htonu32(sftp->rename_s, dest_filename_len);
sftp->rename_s += 4;
memcpy(sftp->rename_s, dest_filename, dest_filename_len);
sftp->rename_s += dest_filename_len;
if (sftp->version >= 5) { if (sftp->version >= 5)
_libssh2_htonu32(sftp->rename_s, flags); _libssh2_store_u32(&sftp->rename_s, flags);
sftp->rename_s += 4;
}
sftp->rename_state = libssh2_NB_state_created; sftp->rename_state = libssh2_NB_state_created;
} }
@ -1899,7 +1840,7 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
if (sftp->rename_state == libssh2_NB_state_created) { if (sftp->rename_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->rename_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->rename_packet,
sftp->rename_s - sftp->rename_packet); sftp->rename_s - sftp->rename_packet);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, sftp->rename_packet); LIBSSH2_FREE(session, sftp->rename_packet);
@ -1917,7 +1858,7 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->rename_request_id, &data, sftp->rename_request_id, &data,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
sftp->rename_state = libssh2_NB_state_idle; sftp->rename_state = libssh2_NB_state_idle;
@ -2007,16 +1948,12 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
/* Filetype in SFTP 3 and earlier */ /* Filetype in SFTP 3 and earlier */
attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR; attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR;
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_MKDIR; *(s++) = SSH_FXP_MKDIR;
sftp->mkdir_request_id = sftp->request_id++; sftp->mkdir_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->mkdir_request_id); _libssh2_store_u32(&s, sftp->mkdir_request_id);
s += 4; _libssh2_store_str(&s, path, path_len);
_libssh2_htonu32(s, path_len);
s += 4;
memcpy(s, path, path_len);
s += path_len;
s += sftp_attr2bin(s, &attrs); s += sftp_attr2bin(s, &attrs);
sftp->mkdir_state = libssh2_NB_state_created; sftp->mkdir_state = libssh2_NB_state_created;
@ -2027,7 +1964,7 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
if (sftp->mkdir_state == libssh2_NB_state_created) { if (sftp->mkdir_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) packet, packet_len); rc = _libssh2_channel_write(channel, 0, (char *) packet, packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
sftp->mkdir_packet = packet; sftp->mkdir_packet = packet;
return rc; return rc;
} }
@ -2044,7 +1981,7 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, sftp->mkdir_request_id, rc = sftp_packet_require(sftp, SSH_FXP_STATUS, sftp->mkdir_request_id,
&data, &data_len); &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
sftp->mkdir_state = libssh2_NB_state_idle; sftp->mkdir_state = libssh2_NB_state_idle;
@ -2107,16 +2044,11 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
"packet"); "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
*(s++) = SSH_FXP_RMDIR; *(s++) = SSH_FXP_RMDIR;
sftp->rmdir_request_id = sftp->request_id++; sftp->rmdir_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->rmdir_request_id); _libssh2_store_u32(&s, sftp->rmdir_request_id);
s += 4; _libssh2_store_str(&s, path, path_len);
_libssh2_htonu32(s, path_len);
s += 4;
memcpy(s, path, path_len);
s += path_len;
sftp->rmdir_state = libssh2_NB_state_created; sftp->rmdir_state = libssh2_NB_state_created;
} }
@ -2124,7 +2056,7 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
if (sftp->rmdir_state == libssh2_NB_state_created) { if (sftp->rmdir_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->rmdir_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->rmdir_packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, sftp->rmdir_packet); LIBSSH2_FREE(session, sftp->rmdir_packet);
@ -2141,7 +2073,7 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
rc = sftp_packet_require(sftp, SSH_FXP_STATUS, rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
sftp->rmdir_request_id, &data, &data_len); sftp->rmdir_request_id, &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
sftp->rmdir_state = libssh2_NB_state_idle; sftp->rmdir_state = libssh2_NB_state_idle;
@ -2208,8 +2140,8 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
"packet"); "packet");
} }
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
switch (stat_type) { switch (stat_type) {
case LIBSSH2_SFTP_SETSTAT: case LIBSSH2_SFTP_SETSTAT:
*(s++) = SSH_FXP_SETSTAT; *(s++) = SSH_FXP_SETSTAT;
@ -2224,15 +2156,11 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
*(s++) = SSH_FXP_STAT; *(s++) = SSH_FXP_STAT;
} }
sftp->stat_request_id = sftp->request_id++; sftp->stat_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->stat_request_id); _libssh2_store_u32(&s, sftp->stat_request_id);
s += 4; _libssh2_store_str(&s, path, path_len);
_libssh2_htonu32(s, path_len);
s += 4; if (stat_type == LIBSSH2_SFTP_SETSTAT)
memcpy(s, path, path_len);
s += path_len;
if (stat_type == LIBSSH2_SFTP_SETSTAT) {
s += sftp_attr2bin(s, attrs); s += sftp_attr2bin(s, attrs);
}
sftp->stat_state = libssh2_NB_state_created; sftp->stat_state = libssh2_NB_state_created;
} }
@ -2240,7 +2168,7 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
if (sftp->stat_state == libssh2_NB_state_created) { if (sftp->stat_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->stat_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->stat_packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, sftp->stat_packet); LIBSSH2_FREE(session, sftp->stat_packet);
@ -2257,7 +2185,7 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
rc = sftp_packet_requirev(sftp, 2, stat_responses, rc = sftp_packet_requirev(sftp, 2, stat_responses,
sftp->stat_request_id, &data, &data_len); sftp->stat_request_id, &data, &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (rc) { } else if (rc) {
sftp->stat_state = libssh2_NB_state_idle; sftp->stat_state = libssh2_NB_state_idle;
@ -2340,8 +2268,8 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
(link_type == (link_type ==
LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", path); LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", path);
_libssh2_htonu32(s, packet_len - 4); _libssh2_store_u32(&s, packet_len - 4);
s += 4;
switch (link_type) { switch (link_type) {
case LIBSSH2_SFTP_REALPATH: case LIBSSH2_SFTP_REALPATH:
*(s++) = SSH_FXP_REALPATH; *(s++) = SSH_FXP_REALPATH;
@ -2356,18 +2284,11 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
*(s++) = SSH_FXP_READLINK; *(s++) = SSH_FXP_READLINK;
} }
sftp->symlink_request_id = sftp->request_id++; sftp->symlink_request_id = sftp->request_id++;
_libssh2_htonu32(s, sftp->symlink_request_id); _libssh2_store_u32(&s, sftp->symlink_request_id);
s += 4; _libssh2_store_str(&s, path, path_len);
_libssh2_htonu32(s, path_len);
s += 4; if (link_type == LIBSSH2_SFTP_SYMLINK)
memcpy(s, path, path_len); _libssh2_store_str(&s, target, target_len);
s += path_len;
if (link_type == LIBSSH2_SFTP_SYMLINK) {
_libssh2_htonu32(s, target_len);
s += 4;
memcpy(s, target, target_len);
s += target_len;
}
sftp->symlink_state = libssh2_NB_state_created; sftp->symlink_state = libssh2_NB_state_created;
} }
@ -2375,7 +2296,7 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
if (sftp->symlink_state == libssh2_NB_state_created) { if (sftp->symlink_state == libssh2_NB_state_created) {
rc = _libssh2_channel_write(channel, 0, (char *) sftp->symlink_packet, rc = _libssh2_channel_write(channel, 0, (char *) sftp->symlink_packet,
packet_len); packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} else if (packet_len != rc) { } else if (packet_len != rc) {
LIBSSH2_FREE(session, sftp->symlink_packet); LIBSSH2_FREE(session, sftp->symlink_packet);
@ -2393,7 +2314,7 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
rc = sftp_packet_requirev(sftp, 2, link_responses, rc = sftp_packet_requirev(sftp, 2, link_responses,
sftp->symlink_request_id, &data, sftp->symlink_request_id, &data,
&data_len); &data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc; return rc;
} }
else if (rc) { else if (rc) {

View File

@ -138,7 +138,7 @@ decrypt(LIBSSH2_SESSION * session, unsigned char *source,
if (session->remote.crypt->crypt(session, source, if (session->remote.crypt->crypt(session, source,
&session->remote.crypt_abstract)) { &session->remote.crypt_abstract)) {
LIBSSH2_FREE(session, p->payload); LIBSSH2_FREE(session, p->payload);
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
/* if the crypt() function would write to a given address it /* if the crypt() function would write to a given address it
@ -150,7 +150,7 @@ decrypt(LIBSSH2_SESSION * session, unsigned char *source,
dest += blocksize; /* advance write pointer */ dest += blocksize; /* advance write pointer */
source += blocksize; /* advance read pointer */ source += blocksize; /* advance read pointer */
} }
return PACKET_NONE; /* all is fine */ return LIBSSH2_ERROR_NONE; /* all is fine */
} }
/* /*
@ -209,7 +209,7 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
session->fullpacket_payload_len, session->fullpacket_payload_len,
&session->remote.comp_abstract)) { &session->remote.comp_abstract)) {
LIBSSH2_FREE(session, p->payload); LIBSSH2_FREE(session, p->payload);
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
if (free_payload) { if (free_payload) {
@ -233,7 +233,7 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
* brigade won't know what to do with it */ * brigade won't know what to do with it */
p->payload = LIBSSH2_ALLOC(session, data_len); p->payload = LIBSSH2_ALLOC(session, data_len);
if (!p->payload) if (!p->payload)
return PACKET_ENOMEM; return LIBSSH2_ERROR_ALLOC;
memcpy(p->payload, data, data_len); memcpy(p->payload, data, data_len);
session->fullpacket_payload_len = data_len; session->fullpacket_payload_len = data_len;
@ -331,7 +331,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
do { do {
if (session->socket_state == LIBSSH2_SOCKET_DISCONNECTED) { if (session->socket_state == LIBSSH2_SOCKET_DISCONNECTED) {
return PACKET_NONE; return LIBSSH2_ERROR_NONE;
} }
if (session->state & LIBSSH2_STATE_NEWKEYS) { if (session->state & LIBSSH2_STATE_NEWKEYS) {
@ -391,9 +391,9 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
if ((nread < 0) && (errno == EAGAIN)) { if ((nread < 0) && (errno == EAGAIN)) {
session->socket_block_directions |= session->socket_block_directions |=
LIBSSH2_SESSION_BLOCK_INBOUND; LIBSSH2_SESSION_BLOCK_INBOUND;
return PACKET_EAGAIN; return LIBSSH2_ERROR_EAGAIN;
} }
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
debugdump(session, "libssh2_transport_read() raw", debugdump(session, "libssh2_transport_read() raw",
@ -420,12 +420,12 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
*/ */
session->socket_block_directions |= session->socket_block_directions |=
LIBSSH2_SESSION_BLOCK_INBOUND; LIBSSH2_SESSION_BLOCK_INBOUND;
return PACKET_EAGAIN; return LIBSSH2_ERROR_EAGAIN;
} }
if (encrypted) { if (encrypted) {
rc = decrypt(session, &p->buf[p->readidx], block, blocksize); rc = decrypt(session, &p->buf[p->readidx], block, blocksize);
if (rc != PACKET_NONE) { if (rc != LIBSSH2_ERROR_NONE) {
return rc; return rc;
} }
/* save the first 5 bytes of the decrypted package, to be /* save the first 5 bytes of the decrypted package, to be
@ -445,11 +445,11 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
*/ */
p->packet_length = _libssh2_ntohu32(block); p->packet_length = _libssh2_ntohu32(block);
if (p->packet_length < 1) if (p->packet_length < 1)
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
p->padding_length = block[4]; p->padding_length = block[4];
if (p->padding_length < 0) if (p->padding_length < 0)
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
/* total_num is the number of bytes following the initial /* total_num is the number of bytes following the initial
(5 bytes) packet length and padding length fields */ (5 bytes) packet length and padding length fields */
@ -466,14 +466,14 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
* padding, and MAC.)." * padding, and MAC.)."
*/ */
if (p->total_num > LIBSSH2_PACKET_MAXPAYLOAD) { if (p->total_num > LIBSSH2_PACKET_MAXPAYLOAD) {
return PACKET_TOOBIG; return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
} }
/* Get a packet handle put data into. We get one to /* Get a packet handle put data into. We get one to
hold all data, including padding and MAC. */ hold all data, including padding and MAC. */
p->payload = LIBSSH2_ALLOC(session, p->total_num); p->payload = LIBSSH2_ALLOC(session, p->total_num);
if (!p->payload) { if (!p->payload) {
return PACKET_ENOMEM; return LIBSSH2_ERROR_ALLOC;
} }
/* init write pointer to start of payload buffer */ /* init write pointer to start of payload buffer */
p->wptr = p->payload; p->wptr = p->payload;
@ -538,7 +538,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
if (numdecrypt > 0) { if (numdecrypt > 0) {
/* now decrypt the lot */ /* now decrypt the lot */
rc = decrypt(session, &p->buf[p->readidx], p->wptr, numdecrypt); rc = decrypt(session, &p->buf[p->readidx], p->wptr, numdecrypt);
if (rc != PACKET_NONE) { if (rc != LIBSSH2_ERROR_NONE) {
return rc; return rc;
} }
@ -574,13 +574,13 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
/* we have a full packet */ /* we have a full packet */
libssh2_transport_read_point1: libssh2_transport_read_point1:
rc = fullpacket(session, encrypted); rc = fullpacket(session, encrypted);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
if (session->packAdd_state != libssh2_NB_state_idle) if (session->packAdd_state != libssh2_NB_state_idle)
{ {
/* fullpacket only returns PACKET_EAGAIN if /* fullpacket only returns LIBSSH2_ERROR_EAGAIN if
* libssh2_packet_add returns PACKET_EAGAIN. If that * libssh2_packet_add returns LIBSSH2_ERROR_EAGAIN. If that
* returns PACKET_EAGAIN but the packAdd_state is idle, * returns LIBSSH2_ERROR_EAGAIN but the packAdd_state is idle,
* then the packet has been added to the brigade, but some * then the packet has been added to the brigade, but some
* immediate action that was taken based on the packet * immediate action that was taken based on the packet
* type (such as key re-exchange) is not yet complete. * type (such as key re-exchange) is not yet complete.
@ -599,7 +599,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
} }
} while (1); /* loop */ } while (1); /* loop */
return PACKET_FAIL; /* we never reach this point */ return LIBSSH2_ERROR_SOCKET_NONE; /* we never reach this point */
} }
/* /*
@ -626,7 +626,7 @@ send_existing(LIBSSH2_SESSION * session, unsigned char *data,
if (!p->outbuf) { if (!p->outbuf) {
*ret = 0; *ret = 0;
return PACKET_NONE; return LIBSSH2_ERROR_NONE;
} }
/* send as much as possible of the existing packet */ /* send as much as possible of the existing packet */
@ -636,7 +636,7 @@ send_existing(LIBSSH2_SESSION * session, unsigned char *data,
we don't add this one up until the previous one has been sent. To we don't add this one up until the previous one has been sent. To
make the caller really notice his/hers flaw, we return error for make the caller really notice his/hers flaw, we return error for
this case */ this case */
return PACKET_BADUSE; return LIBSSH2_ERROR_BAD_USE;
} }
*ret = 1; /* set to make our parent return */ *ret = 1; /* set to make our parent return */
@ -669,15 +669,15 @@ send_existing(LIBSSH2_SESSION * session, unsigned char *data,
/* nothing was sent */ /* nothing was sent */
if (errno != EAGAIN) { if (errno != EAGAIN) {
/* send failure! */ /* send failure! */
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
session->socket_block_directions |= LIBSSH2_SESSION_BLOCK_OUTBOUND; session->socket_block_directions |= LIBSSH2_SESSION_BLOCK_OUTBOUND;
return PACKET_EAGAIN; return LIBSSH2_ERROR_EAGAIN;
} }
p->osent += rc; /* we sent away this much data */ p->osent += rc; /* we sent away this much data */
return rc < length ? PACKET_EAGAIN : PACKET_NONE; return rc < length ? LIBSSH2_ERROR_EAGAIN : LIBSSH2_ERROR_NONE;
} }
/* /*
@ -686,7 +686,7 @@ send_existing(LIBSSH2_SESSION * session, unsigned char *data,
* Send a packet, encrypting it and adding a MAC code if necessary * Send a packet, encrypting it and adding a MAC code if necessary
* Returns 0 on success, non-zero on failure. * Returns 0 on success, non-zero on failure.
* *
* Returns PACKET_EAGAIN if it would block - and if it does so, you should * Returns LIBSSH2_ERROR_EAGAIN if it would block - and if it does so, you should
* call this function again as soon as it is likely that more data can be * call this function again as soon as it is likely that more data can be
* sent, and this function should then be called with the same argument set * sent, and this function should then be called with the same argument set
* (same data pointer and same data_len) until zero or failure is returned. * (same data pointer and same data_len) until zero or failure is returned.
@ -740,7 +740,7 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data,
LIBSSH2_PACKET_MAXCOMP, LIBSSH2_PACKET_MAXCOMP,
&free_data, data, data_len, &free_data, data, data_len,
&session->local.comp_abstract)) { &session->local.comp_abstract)) {
return PACKET_COMPRESS; /* compression failure */ return LIBSSH2_ERROR_COMPRESS; /* compression failure */
} }
} }
@ -788,7 +788,7 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data,
returns. */ returns. */
p->outbuf = LIBSSH2_ALLOC(session, total_length); p->outbuf = LIBSSH2_ALLOC(session, total_length);
if (!p->outbuf) { if (!p->outbuf) {
return PACKET_ENOMEM; return LIBSSH2_ERROR_ALLOC;
} }
/* store packet_length, which is the size of the whole packet except /* store packet_length, which is the size of the whole packet except
@ -820,7 +820,7 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data,
unsigned char *ptr = &p->outbuf[i]; unsigned char *ptr = &p->outbuf[i];
if (session->local.crypt->crypt(session, ptr, if (session->local.crypt->crypt(session, ptr,
&session->local.crypt_abstract)) &session->local.crypt_abstract))
return PACKET_FAIL; /* encryption failure */ return LIBSSH2_ERROR_SOCKET_NONE; /* encryption failure */
} }
} }
@ -846,9 +846,9 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data,
p->olen = orgdata_len; p->olen = orgdata_len;
p->osent = (ret == -1) ? 0 : ret; p->osent = (ret == -1) ? 0 : ret;
p->ototal_num = total_length; p->ototal_num = total_length;
return PACKET_EAGAIN; return LIBSSH2_ERROR_EAGAIN;
} }
return PACKET_FAIL; return LIBSSH2_ERROR_SOCKET_NONE;
} }
/* the whole thing got sent away */ /* the whole thing got sent away */
@ -857,5 +857,5 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data,
LIBSSH2_FREE(session, p->outbuf); LIBSSH2_FREE(session, p->outbuf);
p->outbuf = NULL; p->outbuf = NULL;
return PACKET_NONE; /* all is good */ return LIBSSH2_ERROR_NONE; /* all is good */
} }

View File

@ -42,6 +42,7 @@
*/ */
#include "libssh2_priv.h" #include "libssh2_priv.h"
#include "packet.h"
/* /*
* libssh2_transport_write * libssh2_transport_write

View File

@ -84,22 +84,9 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
} }
*(s++) = SSH_MSG_USERAUTH_REQUEST; *(s++) = SSH_MSG_USERAUTH_REQUEST;
_libssh2_htonu32(s, username_len); _libssh2_store_str(&s, username, username_len);
s += 4; _libssh2_store_str(&s, "ssh-connection", 14);
if (username) { _libssh2_store_str(&s, "none", 4);
memcpy(s, username, username_len);
s += username_len;
}
_libssh2_htonu32(s, 14);
s += 4;
memcpy(s, "ssh-connection", 14);
s += 14;
_libssh2_htonu32(s, 4);
s += 4;
memcpy(s, "none", 4);
s += 4;
session->userauth_list_state = libssh2_NB_state_created; session->userauth_list_state = libssh2_NB_state_created;
} }
@ -107,7 +94,7 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
if (session->userauth_list_state == libssh2_NB_state_created) { if (session->userauth_list_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->userauth_list_data, rc = _libssh2_transport_write(session, session->userauth_list_data,
session->userauth_list_data_len); session->userauth_list_data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list"); "Would block requesting userauth list");
return NULL; return NULL;
@ -132,7 +119,7 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
&session->userauth_list_data_len, 0, &session->userauth_list_data_len, 0,
NULL, 0, NULL, 0,
&session->userauth_list_packet_requirev_state); &session->userauth_list_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
_libssh2_error(session, LIBSSH2_ERROR_EAGAIN, _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list"); "Would block requesting userauth list");
return NULL; return NULL;
@ -236,28 +223,11 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
} }
*(s++) = SSH_MSG_USERAUTH_REQUEST; *(s++) = SSH_MSG_USERAUTH_REQUEST;
_libssh2_htonu32(s, username_len); _libssh2_store_str(&s, username, username_len);
s += 4; _libssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1);
memcpy(s, username, username_len); _libssh2_store_str(&s, "password", sizeof("password") - 1);
s += username_len; *s++ = '\0';
_libssh2_store_str(&s, password, password_len);
_libssh2_htonu32(s, sizeof("ssh-connection") - 1);
s += 4;
memcpy(s, "ssh-connection", sizeof("ssh-connection") - 1);
s += sizeof("ssh-connection") - 1;
_libssh2_htonu32(s, sizeof("password") - 1);
s += 4;
memcpy(s, "password", sizeof("password") - 1);
s += sizeof("password") - 1;
*s = '\0';
s++;
_libssh2_htonu32(s, password_len);
s += 4;
memcpy(s, password, password_len);
s += password_len;
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, _libssh2_debug(session, LIBSSH2_TRACE_AUTH,
"Attempting to login using password authentication"); "Attempting to login using password authentication");
@ -268,7 +238,7 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
if (session->userauth_pswd_state == libssh2_NB_state_created) { if (session->userauth_pswd_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->userauth_pswd_data, rc = _libssh2_transport_write(session, session->userauth_pswd_data,
session->userauth_pswd_data_len); session->userauth_pswd_data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block writing password request"); "Would block writing password request");
} }
@ -298,7 +268,7 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
0, NULL, 0, 0, NULL, 0,
&session-> &session->
userauth_pswd_packet_requirev_state); userauth_pswd_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting"); "Would block waiting");
} else if (rc) { } else if (rc) {
@ -379,35 +349,15 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
} }
*(s++) = SSH_MSG_USERAUTH_REQUEST; *(s++) = SSH_MSG_USERAUTH_REQUEST;
_libssh2_htonu32(s, username_len); _libssh2_store_str(&s, username, username_len);
s += 4; _libssh2_store_str(&s, "ssh-connection",
memcpy(s, username, username_len); sizeof("ssh-connection") - 1);
s += username_len; _libssh2_store_str(&s, "password",
sizeof("password") - 1);
_libssh2_htonu32(s, sizeof("ssh-connection") - 1); *s++ = 0x01;
s += 4; _libssh2_store_str(&s, password, password_len);
memcpy(s, "ssh-connection", _libssh2_store_str(&s, session->userauth_pswd_newpw,
sizeof("ssh-connection") - 1); session->userauth_pswd_newpw_len);
s += sizeof("ssh-connection") - 1;
_libssh2_htonu32(s, sizeof("password") - 1);
s += 4;
memcpy(s, "password", sizeof("password") - 1);
s += sizeof("password") - 1;
*s = 0x01;
s++;
_libssh2_htonu32(s, password_len);
s += 4;
memcpy(s, password, password_len);
s += password_len;
_libssh2_htonu32(s, session->userauth_pswd_newpw_len);
s += 4;
memcpy(s, session->userauth_pswd_newpw,
session->userauth_pswd_newpw_len);
s += session->userauth_pswd_newpw_len;
session->userauth_pswd_state = libssh2_NB_state_sent2; session->userauth_pswd_state = libssh2_NB_state_sent2;
} }
@ -417,7 +367,7 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
session->userauth_pswd_data, session->userauth_pswd_data,
session-> session->
userauth_pswd_data_len); userauth_pswd_data_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting"); "Would block waiting");
} }
@ -493,9 +443,9 @@ libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const char *username,
*/ */
static int static int
file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method, file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
unsigned long *method_len, size_t *method_len,
unsigned char **pubkeydata, unsigned char **pubkeydata,
unsigned long *pubkeydata_len, size_t *pubkeydata_len,
const char *pubkeyfile) const char *pubkeyfile)
{ {
FILE *fd; FILE *fd;
@ -652,7 +602,7 @@ sign_fromfile(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len,
datavec.iov_base = (unsigned char *)data; datavec.iov_base = (unsigned char *)data;
datavec.iov_len = data_len; datavec.iov_len = data_len;
if (privkeyobj->signv(session, sig, (unsigned long *)sig_len, 1, &datavec, if (privkeyobj->signv(session, sig, sig_len, 1, &datavec,
&hostkey_abstract)) { &hostkey_abstract)) {
if (privkeyobj->dtor) { if (privkeyobj->dtor) {
privkeyobj->dtor(session, abstract); privkeyobj->dtor(session, abstract);
@ -673,20 +623,20 @@ sign_fromfile(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len,
*/ */
static int static int
userauth_hostbased_fromfile(LIBSSH2_SESSION *session, userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
const char *username, unsigned int username_len, const char *username, size_t username_len,
const char *publickey, const char *privatekey, const char *publickey, const char *privatekey,
const char *passphrase, const char *hostname, const char *passphrase, const char *hostname,
unsigned int hostname_len, size_t hostname_len,
const char *local_username, const char *local_username,
unsigned int local_username_len) size_t local_username_len)
{ {
int rc; int rc;
if (session->userauth_host_state == libssh2_NB_state_idle) { if (session->userauth_host_state == libssh2_NB_state_idle) {
const LIBSSH2_HOSTKEY_METHOD *privkeyobj; const LIBSSH2_HOSTKEY_METHOD *privkeyobj;
unsigned char *pubkeydata, *sig; unsigned char *pubkeydata, *sig;
unsigned long pubkeydata_len; size_t pubkeydata_len;
unsigned long sig_len; size_t sig_len;
void *abstract; void *abstract;
unsigned char buf[5]; unsigned char buf[5];
struct iovec datavec[4]; struct iovec datavec[4];
@ -732,45 +682,18 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
} }
*(session->userauth_host_s++) = SSH_MSG_USERAUTH_REQUEST; *(session->userauth_host_s++) = SSH_MSG_USERAUTH_REQUEST;
_libssh2_htonu32(session->userauth_host_s, username_len); _libssh2_store_str(&session->userauth_host_s, username, username_len);
session->userauth_host_s += 4; _libssh2_store_str(&session->userauth_host_s, "ssh-connection", 14);
memcpy(session->userauth_host_s, username, username_len); _libssh2_store_str(&session->userauth_host_s, "hostbased", 9);
session->userauth_host_s += username_len; _libssh2_store_str(&session->userauth_host_s,
(const char *)session->userauth_host_method,
/* TODO: change the hideous '14' to a nice defined */ session->userauth_host_method_len);
_libssh2_htonu32(session->userauth_host_s, 14); _libssh2_store_str(&session->userauth_host_s, (const char *)pubkeydata,
session->userauth_host_s += 4; pubkeydata_len);
memcpy(session->userauth_host_s, "ssh-connection", 14);
session->userauth_host_s += 14;
/* TODO: change the hideous '9' to a nice defined */
_libssh2_htonu32(session->userauth_host_s, 9);
session->userauth_host_s += 4;
memcpy(session->userauth_host_s, "hostbased", 9);
session->userauth_host_s += 9;
_libssh2_htonu32(session->userauth_host_s,
session->userauth_host_method_len);
session->userauth_host_s += 4;
memcpy(session->userauth_host_s, session->userauth_host_method,
session->userauth_host_method_len);
session->userauth_host_s += session->userauth_host_method_len;
_libssh2_htonu32(session->userauth_host_s, pubkeydata_len);
session->userauth_host_s += 4;
memcpy(session->userauth_host_s, pubkeydata, pubkeydata_len);
session->userauth_host_s += pubkeydata_len;
LIBSSH2_FREE(session, pubkeydata); LIBSSH2_FREE(session, pubkeydata);
_libssh2_store_str(&session->userauth_host_s, hostname, hostname_len);
_libssh2_htonu32(session->userauth_host_s, hostname_len); _libssh2_store_str(&session->userauth_host_s, local_username,
session->userauth_host_s += 4; local_username_len);
memcpy(session->userauth_host_s, hostname, hostname_len);
session->userauth_host_s += hostname_len;
_libssh2_htonu32(session->userauth_host_s, local_username_len);
session->userauth_host_s += 4;
memcpy(session->userauth_host_s, local_username, local_username_len);
session->userauth_host_s += local_username_len;
rc = file_read_privatekey(session, &privkeyobj, &abstract, rc = file_read_privatekey(session, &privkeyobj, &abstract,
session->userauth_host_method, session->userauth_host_method,
@ -831,23 +754,16 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
session->userauth_host_s = session->userauth_host_s =
session->userauth_host_packet + session->userauth_host_packet_len; session->userauth_host_packet + session->userauth_host_packet_len;
_libssh2_htonu32(session->userauth_host_s, _libssh2_store_u32(&session->userauth_host_s,
4 + session->userauth_host_method_len + 4 + sig_len); 4 + session->userauth_host_method_len + 4 + sig_len);
session->userauth_host_s += 4; _libssh2_store_str(&session->userauth_host_s,
(const char *)session->userauth_host_method,
_libssh2_htonu32(session->userauth_host_s, session->userauth_host_method_len);
session->userauth_host_method_len);
session->userauth_host_s += 4;
memcpy(session->userauth_host_s, session->userauth_host_method,
session->userauth_host_method_len);
session->userauth_host_s += session->userauth_host_method_len;
LIBSSH2_FREE(session, session->userauth_host_method); LIBSSH2_FREE(session, session->userauth_host_method);
session->userauth_host_method = NULL; session->userauth_host_method = NULL;
_libssh2_htonu32(session->userauth_host_s, sig_len); _libssh2_store_str(&session->userauth_host_s, (const char *)sig,
session->userauth_host_s += 4; sig_len);
memcpy(session->userauth_host_s, sig, sig_len);
session->userauth_host_s += sig_len;
LIBSSH2_FREE(session, sig); LIBSSH2_FREE(session, sig);
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, _libssh2_debug(session, LIBSSH2_TRACE_AUTH,
@ -860,7 +776,7 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
rc = _libssh2_transport_write(session, session->userauth_host_packet, rc = _libssh2_transport_write(session, session->userauth_host_packet,
session->userauth_host_s - session->userauth_host_s -
session->userauth_host_packet); session->userauth_host_packet);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} }
else if (rc) { else if (rc) {
@ -879,13 +795,13 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
if (session->userauth_host_state == libssh2_NB_state_sent) { if (session->userauth_host_state == libssh2_NB_state_sent) {
static const unsigned char reply_codes[3] = static const unsigned char reply_codes[3] =
{ SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, 0 }; { SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, 0 };
unsigned long data_len; size_t data_len;
rc = _libssh2_packet_requirev(session, reply_codes, rc = _libssh2_packet_requirev(session, reply_codes,
&session->userauth_host_data, &session->userauth_host_data,
&data_len, 0, NULL, 0, &data_len, 0, NULL, 0,
&session-> &session->
userauth_host_packet_requirev_state); userauth_host_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} }
@ -954,6 +870,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
SSH_MSG_USERAUTH_PK_OK, 0 SSH_MSG_USERAUTH_PK_OK, 0
}; };
int rc; int rc;
unsigned char *s;
if (session->userauth_pblc_state == libssh2_NB_state_idle) { if (session->userauth_pblc_state == libssh2_NB_state_idle) {
/* Zero the whole thing out */ /* Zero the whole thing out */
@ -996,7 +913,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
* the signature, which won't be any larger than the size of the * the signature, which won't be any larger than the size of the
* publickeydata itself * publickeydata itself
*/ */
session->userauth_pblc_s = session->userauth_pblc_packet = s = session->userauth_pblc_packet =
LIBSSH2_ALLOC(session, LIBSSH2_ALLOC(session,
session->userauth_pblc_packet_len + 4 + session->userauth_pblc_packet_len + 4 +
(4 + session->userauth_pblc_method_len) (4 + session->userauth_pblc_method_len)
@ -1008,38 +925,18 @@ userauth_publickey(LIBSSH2_SESSION *session,
"Out of memory"); "Out of memory");
} }
*(session->userauth_pblc_s++) = SSH_MSG_USERAUTH_REQUEST; *s++ = SSH_MSG_USERAUTH_REQUEST;
_libssh2_htonu32(session->userauth_pblc_s, username_len); _libssh2_store_str(&s, username, username_len);
session->userauth_pblc_s += 4; _libssh2_store_str(&s, "ssh-connection", 14);
memcpy(session->userauth_pblc_s, username, username_len); _libssh2_store_str(&s, "publickey", 9);
session->userauth_pblc_s += username_len;
_libssh2_htonu32(session->userauth_pblc_s, 14); session->userauth_pblc_b = s;
session->userauth_pblc_s += 4;
memcpy(session->userauth_pblc_s, "ssh-connection", 14);
session->userauth_pblc_s += 14;
_libssh2_htonu32(session->userauth_pblc_s, 9);
session->userauth_pblc_s += 4;
memcpy(session->userauth_pblc_s, "publickey", 9);
session->userauth_pblc_s += 9;
session->userauth_pblc_b = session->userauth_pblc_s;
/* Not sending signature with *this* packet */ /* Not sending signature with *this* packet */
*(session->userauth_pblc_s++) = 0; *s++ = 0;
_libssh2_htonu32(session->userauth_pblc_s,
session->userauth_pblc_method_len);
session->userauth_pblc_s += 4;
memcpy(session->userauth_pblc_s, session->userauth_pblc_method,
session->userauth_pblc_method_len);
session->userauth_pblc_s += session->userauth_pblc_method_len;
_libssh2_htonu32(session->userauth_pblc_s, pubkeydata_len);
session->userauth_pblc_s += 4;
memcpy(session->userauth_pblc_s, pubkeydata, pubkeydata_len);
session->userauth_pblc_s += pubkeydata_len;
_libssh2_store_str(&s, (const char *)session->userauth_pblc_method,
session->userauth_pblc_method_len);
_libssh2_store_str(&s, (const char *)pubkeydata, pubkeydata_len);
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, _libssh2_debug(session, LIBSSH2_TRACE_AUTH,
"Attempting publickey authentication"); "Attempting publickey authentication");
@ -1049,7 +946,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
if (session->userauth_pblc_state == libssh2_NB_state_created) { if (session->userauth_pblc_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->userauth_pblc_packet, rc = _libssh2_transport_write(session, session->userauth_pblc_packet,
session->userauth_pblc_packet_len); session->userauth_pblc_packet_len);
if (rc == PACKET_EAGAIN) if (rc == LIBSSH2_ERROR_EAGAIN)
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
else if (rc) { else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
@ -1071,7 +968,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
NULL, 0, NULL, 0,
&session-> &session->
userauth_pblc_packet_requirev_state); userauth_pblc_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} }
else if (rc) { else if (rc) {
@ -1136,17 +1033,16 @@ userauth_publickey(LIBSSH2_SESSION *session,
"userauth-publickey signed data"); "userauth-publickey signed data");
} }
_libssh2_htonu32(s, session->session_id_len); _libssh2_store_str(&s, (const char *)session->session_id,
s += 4; session->session_id_len);
memcpy (s, session->session_id, session->session_id_len);
s += session->session_id_len;
memcpy (s, session->userauth_pblc_packet, memcpy (s, session->userauth_pblc_packet,
session->userauth_pblc_packet_len); session->userauth_pblc_packet_len);
s += session->userauth_pblc_packet_len; s += session->userauth_pblc_packet_len;
rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract); rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract);
LIBSSH2_FREE(session, buf); LIBSSH2_FREE(session, buf);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
@ -1184,32 +1080,24 @@ userauth_publickey(LIBSSH2_SESSION *session,
session->userauth_pblc_packet = newpacket; session->userauth_pblc_packet = newpacket;
} }
session->userauth_pblc_s = s = session->userauth_pblc_packet + session->userauth_pblc_packet_len;
session->userauth_pblc_packet + session->userauth_pblc_packet_len;
session->userauth_pblc_b = NULL; session->userauth_pblc_b = NULL;
_libssh2_htonu32(session->userauth_pblc_s, _libssh2_store_u32(&s,
4 + session->userauth_pblc_method_len + 4 + sig_len); 4 + session->userauth_pblc_method_len + 4 + sig_len);
session->userauth_pblc_s += 4; _libssh2_store_str(&s, (const char *)session->userauth_pblc_method,
session->userauth_pblc_method_len);
_libssh2_htonu32(session->userauth_pblc_s,
session->userauth_pblc_method_len);
session->userauth_pblc_s += 4;
memcpy(session->userauth_pblc_s, session->userauth_pblc_method,
session->userauth_pblc_method_len);
session->userauth_pblc_s += session->userauth_pblc_method_len;
LIBSSH2_FREE(session, session->userauth_pblc_method); LIBSSH2_FREE(session, session->userauth_pblc_method);
session->userauth_pblc_method = NULL; session->userauth_pblc_method = NULL;
_libssh2_htonu32(session->userauth_pblc_s, sig_len); _libssh2_store_str(&s, (const char *)sig, sig_len);
session->userauth_pblc_s += 4;
memcpy(session->userauth_pblc_s, sig, sig_len);
session->userauth_pblc_s += sig_len;
LIBSSH2_FREE(session, sig); LIBSSH2_FREE(session, sig);
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, _libssh2_debug(session, LIBSSH2_TRACE_AUTH,
"Attempting publickey authentication -- phase 2"); "Attempting publickey authentication -- phase 2");
session->userauth_pblc_s = s;
session->userauth_pblc_state = libssh2_NB_state_sent2; session->userauth_pblc_state = libssh2_NB_state_sent2;
} }
@ -1217,7 +1105,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
rc = _libssh2_transport_write(session, session->userauth_pblc_packet, rc = _libssh2_transport_write(session, session->userauth_pblc_packet,
session->userauth_pblc_s - session->userauth_pblc_s -
session->userauth_pblc_packet); session->userauth_pblc_packet);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, session->userauth_pblc_packet); LIBSSH2_FREE(session, session->userauth_pblc_packet);
@ -1239,7 +1127,7 @@ userauth_publickey(LIBSSH2_SESSION *session,
&session->userauth_pblc_data, &session->userauth_pblc_data,
&session->userauth_pblc_data_len, 0, NULL, 0, &session->userauth_pblc_data_len, 0, NULL, 0,
&session->userauth_pblc_packet_requirev_state); &session->userauth_pblc_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list"); "Would block requesting userauth list");
} else if (rc) { } else if (rc) {
@ -1275,13 +1163,13 @@ userauth_publickey(LIBSSH2_SESSION *session,
static int static int
userauth_publickey_fromfile(LIBSSH2_SESSION *session, userauth_publickey_fromfile(LIBSSH2_SESSION *session,
const char *username, const char *username,
unsigned int username_len, size_t username_len,
const char *publickey, const char *publickey,
const char *privatekey, const char *privatekey,
const char *passphrase) const char *passphrase)
{ {
unsigned char *pubkeydata = NULL; unsigned char *pubkeydata = NULL;
unsigned long pubkeydata_len = 0; size_t pubkeydata_len = 0;
struct privkey_file privkey_file; struct privkey_file privkey_file;
void *abstract = &privkey_file; void *abstract = &privkey_file;
int rc; int rc;
@ -1407,30 +1295,19 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
*s++ = SSH_MSG_USERAUTH_REQUEST; *s++ = SSH_MSG_USERAUTH_REQUEST;
/* user name */ /* user name */
_libssh2_htonu32(s, username_len); _libssh2_store_str(&s, username, username_len);
s += 4;
memcpy(s, username, username_len);
s += username_len;
/* service name */ /* service name */
_libssh2_htonu32(s, sizeof("ssh-connection") - 1); _libssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1);
s += 4;
memcpy(s, "ssh-connection", sizeof("ssh-connection") - 1);
s += sizeof("ssh-connection") - 1;
/* "keyboard-interactive" */ /* "keyboard-interactive" */
_libssh2_htonu32(s, sizeof("keyboard-interactive") - 1); _libssh2_store_str(&s, "keyboard-interactive",
s += 4; sizeof("keyboard-interactive") - 1);
memcpy(s, "keyboard-interactive", sizeof("keyboard-interactive") - 1);
s += sizeof("keyboard-interactive") - 1;
/* language tag */ /* language tag */
_libssh2_htonu32(s, 0); _libssh2_store_u32(&s, 0);
s += 4;
/* submethods */ /* submethods */
_libssh2_htonu32(s, 0); _libssh2_store_u32(&s, 0);
s += 4;
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, _libssh2_debug(session, LIBSSH2_TRACE_AUTH,
"Attempting keyboard-interactive authentication"); "Attempting keyboard-interactive authentication");
@ -1441,7 +1318,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (session->userauth_kybd_state == libssh2_NB_state_created) { if (session->userauth_kybd_state == libssh2_NB_state_created) {
rc = _libssh2_transport_write(session, session->userauth_kybd_data, rc = _libssh2_transport_write(session, session->userauth_kybd_data,
session->userauth_kybd_packet_len); session->userauth_kybd_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block");
} else if (rc) { } else if (rc) {
LIBSSH2_FREE(session, session->userauth_kybd_data); LIBSSH2_FREE(session, session->userauth_kybd_data);
@ -1464,7 +1341,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
0, NULL, 0, 0, NULL, 0,
&session-> &session->
userauth_kybd_packet_requirev_state); userauth_kybd_packet_requirev_state);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block"); "Would block");
} else if (rc) { } else if (rc) {
@ -1630,15 +1507,12 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
*s = SSH_MSG_USERAUTH_INFO_RESPONSE; *s = SSH_MSG_USERAUTH_INFO_RESPONSE;
s++; s++;
_libssh2_htonu32(s, session->userauth_kybd_num_prompts); _libssh2_store_u32(&s, session->userauth_kybd_num_prompts);
s += 4;
for(i = 0; i != session->userauth_kybd_num_prompts; ++i) { for(i = 0; i != session->userauth_kybd_num_prompts; ++i) {
_libssh2_htonu32(s, session->userauth_kybd_responses[i].length); _libssh2_store_str(&s,
s += 4; session->userauth_kybd_responses[i].text,
memcpy(s, session->userauth_kybd_responses[i].text, session->userauth_kybd_responses[i].length);
session->userauth_kybd_responses[i].length);
s += session->userauth_kybd_responses[i].length;
} }
session->userauth_kybd_state = libssh2_NB_state_sent1; session->userauth_kybd_state = libssh2_NB_state_sent1;
@ -1647,7 +1521,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if (session->userauth_kybd_state == libssh2_NB_state_sent1) { if (session->userauth_kybd_state == libssh2_NB_state_sent1) {
rc = _libssh2_transport_write(session, session->userauth_kybd_data, rc = _libssh2_transport_write(session, session->userauth_kybd_data,
session->userauth_kybd_packet_len); session->userauth_kybd_packet_len);
if (rc == PACKET_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block"); "Would block");
} }