diff --git a/Makefile.inc b/Makefile.inc index 61966bd..0f2b205 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -4,4 +4,4 @@ CSOURCES = channel.c comp.c crypt.c hostkey.c kex.c mac.c misc.c \ global.c 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 diff --git a/src/agent.c b/src/agent.c index f2c31e5..928671b 100644 --- a/src/agent.c +++ b/src/agent.c @@ -284,8 +284,8 @@ agent_transact_pageant(LIBSSH2_AGENT *agent, agent_transaction_ctx_t transctx) return -1; } p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); - _libssh2_htonu32(p, transctx->request_len); - memcpy(p + 4, transctx->request, transctx->request_len); + _libssh2_store_str(p, transctx->request, transctx->request_len); + cds.dwData = PAGEANT_COPYDATA_ID; cds.cbData = 1 + strlen(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; /* key blob */ - _libssh2_htonu32(s, identity->external.blob_len); - s += 4; - memcpy(s, identity->external.blob, identity->external.blob_len); - s += identity->external.blob_len; + _libssh2_store_str(&s, (const char *)identity->external.blob, + identity->external.blob_len); /* data */ - _libssh2_htonu32(s, data_len); - s += 4; - memcpy(s, data, data_len); - s += data_len; + _libssh2_store_str(&s, (const char *)data, data_len); + /* flags */ - _libssh2_htonu32(s, 0); - s += 4; + _libssh2_store_u32(&s, 0); + transctx->request_len = s - transctx->request; transctx->state = agent_NB_state_request_created; } diff --git a/src/channel.c b/src/channel.c index 96602c1..69f8941 100644 --- a/src/channel.c +++ b/src/channel.c @@ -49,6 +49,7 @@ #include "channel.h" #include "transport.h" +#include "packet.h" /* * _libssh2_channel_nextid @@ -192,20 +193,10 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type, goto channel_error; } *(s++) = SSH_MSG_CHANNEL_OPEN; - _libssh2_htonu32(s, channel_type_len); - s += 4; - - memcpy(s, channel_type, channel_type_len); - 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; + _libssh2_store_str(&s, channel_type, channel_type_len); + _libssh2_store_u32(&s, session->open_local_channel); + _libssh2_store_u32(&s, window_size); + _libssh2_store_u32(&s, packet_size); if (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) { rc = _libssh2_transport_write(session, session->open_packet, session->open_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending channel-open request"); return NULL; @@ -238,7 +229,7 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type, session->open_packet + 5 + channel_type_len, 4, &session->open_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return NULL; } else if (rc) { @@ -372,19 +363,10 @@ channel_direct_tcpip(LIBSSH2_SESSION * session, const char *host, "Unable to allocate memory for direct-tcpip connection"); return NULL; } - _libssh2_htonu32(s, session->direct_host_len); - s += 4; - memcpy(s, host, session->direct_host_len); - s += session->direct_host_len; - _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; + _libssh2_store_str(&s, host, session->direct_host_len); + _libssh2_store_u32(&s, port); + _libssh2_store_str(&s, shost, session->direct_shost_len); + _libssh2_store_u32(&s, sport); } channel = @@ -439,7 +421,7 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host, unsigned char *s, *data; static const unsigned char reply_codes[3] = { SSH_MSG_REQUEST_SUCCESS, SSH_MSG_REQUEST_FAILURE, 0 }; - unsigned long data_len; + size_t data_len; int rc; 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; - _libssh2_htonu32(s, sizeof("tcpip-forward") - 1); - s += 4; - memcpy(s, "tcpip-forward", sizeof("tcpip-forward") - 1); - s += sizeof("tcpip-forward") - 1; + _libssh2_store_str(&s, "tcpip-forward", sizeof("tcpip-forward") - 1); *(s++) = 0x01; /* want_reply */ - _libssh2_htonu32(s, session->fwdLstn_host_len); - s += 4; - memcpy(s, host ? host : "0.0.0.0", session->fwdLstn_host_len); - s += session->fwdLstn_host_len; - _libssh2_htonu32(s, port); - s += 4; + _libssh2_store_str(&s, host ? host : "0.0.0.0", + session->fwdLstn_host_len); + _libssh2_store_u32(&s, port); 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) { rc = _libssh2_transport_write(session, session->fwdLstn_packet, session->fwdLstn_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending global-request packet for " "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, 0, NULL, 0, &session->fwdLstn_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return NULL; } 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 * 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) { LIBSSH2_SESSION *session = listener->session; LIBSSH2_CHANNEL *queued; 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) + port(4) */ - unsigned long packet_len = + size_t packet_len = host_len + 14 + sizeof("cancel-tcpip-forward") - 1; int rc; @@ -632,18 +608,12 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener) } *(s++) = SSH_MSG_GLOBAL_REQUEST; - _libssh2_htonu32(s, sizeof("cancel-tcpip-forward") - 1); - s += 4; - memcpy(s, "cancel-tcpip-forward", sizeof("cancel-tcpip-forward") - 1); - s += sizeof("cancel-tcpip-forward") - 1; + _libssh2_store_str(&s, "cancel-tcpip-forward", + sizeof("cancel-tcpip-forward") - 1); *(s++) = 0x00; /* want_reply */ - _libssh2_htonu32(s, host_len); - s += 4; - memcpy(s, listener->host, host_len); - s += host_len; - _libssh2_htonu32(s, listener->port); - s += 4; + _libssh2_store_str(&s, listener->host, host_len); + _libssh2_store_u32(&s, listener->port); listener->chanFwdCncl_state = libssh2_NB_state_created; } else { @@ -652,7 +622,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener) if (listener->chanFwdCncl_state == libssh2_NB_state_created) { rc = _libssh2_transport_write(session, packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { listener->chanFwdCncl_data = packet; return rc; } @@ -674,7 +644,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener) LIBSSH2_CHANNEL *next = _libssh2_list_next(&queued->node); rc = libssh2_channel_free(queued); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } queued = next; @@ -697,7 +667,7 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener) * Stop listening on a remote port and free the listener * 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_channel_forward_cancel(LIBSSH2_LISTENER *listener) @@ -735,7 +705,7 @@ channel_forward_accept(LIBSSH2_LISTENER *listener) return channel; } - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for packet"); } @@ -773,7 +743,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel, unsigned char *s, *data; static const unsigned char reply_codes[3] = { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; - unsigned long data_len; + size_t data_len; int rc; if (channel->setenv_state == libssh2_NB_state_idle) { @@ -799,24 +769,11 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel, } *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_htonu32(s, channel->remote.id); - s += 4; - _libssh2_htonu32(s, sizeof("env") - 1); - s += 4; - memcpy(s, "env", sizeof("env") - 1); - s += sizeof("env") - 1; - + _libssh2_store_u32(&s, channel->remote.id); + _libssh2_store_str(&s, "env", sizeof("env") - 1); *(s++) = 0x01; - - _libssh2_htonu32(s, varname_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; + _libssh2_store_str(&s, varname, varname_len); + _libssh2_store_str(&s, value, value_len); 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) { rc = _libssh2_transport_write(session, channel->setenv_packet, channel->setenv_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { LIBSSH2_FREE(session, channel->setenv_packet); @@ -847,7 +804,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel, 1, channel->setenv_local_channel, 4, &channel-> setenv_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if (rc) { @@ -900,7 +857,7 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel, unsigned char *s, *data; static const unsigned char reply_codes[3] = { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; - unsigned long data_len; + size_t data_len; int rc; 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; - _libssh2_htonu32(s, channel->remote.id); - s += 4; - _libssh2_htonu32(s, sizeof("pty-req") - 1); - s += 4; - memcpy(s, "pty-req", sizeof("pty-req") - 1); - s += sizeof("pty-req") - 1; + _libssh2_store_u32(&s, channel->remote.id); + _libssh2_store_str(&s, (char *)"pty-req", sizeof("pty-req") - 1); *(s++) = 0x01; - _libssh2_htonu32(s, term_len); - s += 4; - if (term) { - memcpy(s, term, term_len); - s += term_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; - } + _libssh2_store_str(&s, term, term_len); + _libssh2_store_u32(&s, width); + _libssh2_store_u32(&s, height); + _libssh2_store_u32(&s, width_px); + _libssh2_store_u32(&s, height_px); + _libssh2_store_str(&s, modes, modes_len); 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) { rc = _libssh2_transport_write(session, channel->reqPTY_packet, channel->reqPTY_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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, 1, channel->reqPTY_local_channel, 4, &channel->reqPTY_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { channel->reqPTY_state = libssh2_NB_state_idle; @@ -1045,28 +982,19 @@ channel_request_pty_size(LIBSSH2_CHANNEL * channel, int width, s = channel->reqPTY_packet = LIBSSH2_ALLOC(session, channel->reqPTY_packet_len); - if (!channel->reqPTY_packet) { + if (!channel->reqPTY_packet) return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for pty-request"); - } *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_htonu32(s, channel->remote.id); - s += 4; - _libssh2_htonu32(s, sizeof("window-change") - 1); - s += 4; - memcpy(s, "window-change", sizeof("window-change") - 1); - s += sizeof("window-change") - 1; - + _libssh2_store_u32(&s, channel->remote.id); + _libssh2_store_str(&s, (char *)"window-change", + sizeof("window-change") - 1); *(s++) = 0x00; /* Don't reply */ - _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_store_u32(&s, width); + _libssh2_store_u32(&s, height); + _libssh2_store_u32(&s, width_px); + _libssh2_store_u32(&s, height_px); 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) { rc = _libssh2_transport_write(session, channel->reqPTY_packet, channel->reqPTY_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { LIBSSH2_FREE(session, channel->reqPTY_packet); @@ -1122,10 +1050,10 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, unsigned char *s, *data; static const unsigned char reply_codes[3] = { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; - unsigned long data_len; - unsigned long proto_len = + size_t data_len; + size_t proto_len = 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; int rc; @@ -1155,23 +1083,16 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, } *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_htonu32(s, channel->remote.id); - s += 4; - _libssh2_htonu32(s, sizeof("x11-req") - 1); - s += 4; - memcpy(s, "x11-req", sizeof("x11-req") - 1); - s += sizeof("x11-req") - 1; + _libssh2_store_u32(&s, channel->remote.id); + _libssh2_store_str(&s, "x11-req", sizeof("x11-req") - 1); *(s++) = 0x01; /* want_reply */ *(s++) = single_connection ? 0x01 : 0x00; - _libssh2_htonu32(s, proto_len); - s += 4; - memcpy(s, auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", proto_len); - s += proto_len; + _libssh2_store_str(&s, auth_proto?auth_proto:"MIT-MAGIC-COOKIE-1", + proto_len); - _libssh2_htonu32(s, cookie_len); - s += 4; + _libssh2_store_u32(&s, cookie_len); if (auth_cookie) { memcpy(s, auth_cookie, cookie_len); } else { @@ -1189,16 +1110,14 @@ channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, } s += cookie_len; - _libssh2_htonu32(s, screen_number); - s += 4; - + _libssh2_store_u32(&s, screen_number); channel->reqX11_state = libssh2_NB_state_created; } if (channel->reqX11_state == libssh2_NB_state_created) { rc = _libssh2_transport_write(session, channel->reqX11_packet, channel->reqX11_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return 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, 1, channel->reqX11_local_channel, 4, &channel->reqX11_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { channel->reqX11_state = libssh2_NB_state_idle; @@ -1271,7 +1190,7 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, unsigned char *s, *data; static const unsigned char reply_codes[3] = { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; - unsigned long data_len; + size_t data_len; int rc; if (channel->process_state == libssh2_NB_state_idle) { @@ -1299,21 +1218,12 @@ _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, } *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_htonu32(s, channel->remote.id); - s += 4; - _libssh2_htonu32(s, request_len); - s += 4; - memcpy(s, request, request_len); - s += request_len; - + _libssh2_store_u32(&s, channel->remote.id); + _libssh2_store_str(&s, request, request_len); *(s++) = 0x01; - if (message) { - _libssh2_htonu32(s, message_len); - s += 4; - memcpy(s, message, message_len); - s += message_len; - } + if (message) + _libssh2_store_str(&s, message, message_len); 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) { rc = _libssh2_transport_write(session, channel->process_packet, channel->process_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return 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, 1, channel->process_local_channel, 4, &channel->process_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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, channel->flush_refund_bytes, 0, NULL); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; } @@ -1545,7 +1455,7 @@ _libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL * channel, } rc = _libssh2_transport_write(channel->session, channel->adjust_adjust, 9); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { @@ -1636,7 +1546,7 @@ _libssh2_channel_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode) int rc = _libssh2_channel_flush(channel, LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA); - if(PACKET_EAGAIN == rc) + if(LIBSSH2_ERROR_EAGAIN == 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 * 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, char *buf, size_t buflen) @@ -1714,7 +1624,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, while (rc > 0) 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"); /* @@ -1813,7 +1723,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, return 0; else /* 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 /* 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 */ rc = _libssh2_channel_receive_window_adjust(channel, (LIBSSH2_CHANNEL_WINDOW_DEFAULT*60), 0, NULL); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; _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 * 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 - * PACKET_EAGAIN. + * LIBSSH2_ERROR_EAGAIN. */ LIBSSH2_API ssize_t 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++) = stream_id ? SSH_MSG_CHANNEL_EXTENDED_DATA : SSH_MSG_CHANNEL_DATA; - _libssh2_htonu32(channel->write_s, channel->remote.id); - channel->write_s += 4; - if (stream_id) { - _libssh2_htonu32(channel->write_s, stream_id); - channel->write_s += 4; - } + _libssh2_store_u32(&channel->write_s, channel->remote.id); + if (stream_id) + _libssh2_store_u32(&channel->write_s, stream_id); /* Don't exceed the remote end's limits */ /* 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->write_bufwrite = channel->local.packet_size; } - _libssh2_htonu32(channel->write_s, channel->write_bufwrite); - channel->write_s += 4; - memcpy(channel->write_s, buf, channel->write_bufwrite); - channel->write_s += channel->write_bufwrite; + _libssh2_store_str(&channel->write_s, buf, channel->write_bufwrite); _libssh2_debug(session, LIBSSH2_TRACE_CONN, "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, channel->write_s - channel->write_packet); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { if(wrote) { /* some pieces of data was sent before the EAGAIN so we 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; _libssh2_htonu32(packet + 1, channel->remote.id); rc = _libssh2_transport_write(session, packet, 5); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { @@ -2186,7 +2090,7 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel) break; } rc = _libssh2_transport_read(session); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc < 0) { @@ -2246,7 +2150,7 @@ channel_close(LIBSSH2_CHANNEL * channel) if (channel->close_state == libssh2_NB_state_created) { retcode = _libssh2_transport_write(session, channel->close_packet, 5); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { return retcode; } else if (retcode) { channel->close_state = libssh2_NB_state_idle; @@ -2366,7 +2270,7 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel) LIBSSH2_SESSION *session = channel->session; unsigned char channel_id[4]; unsigned char *data; - unsigned long data_len; + size_t data_len; int rc; assert(session); @@ -2384,7 +2288,7 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel) && (session->socket_state == LIBSSH2_SOCKET_CONNECTED)) { rc = channel_close(channel); - if(rc == PACKET_EAGAIN) + if(rc == LIBSSH2_ERROR_EAGAIN) return rc; else if (rc < 0) { channel->free_state = libssh2_NB_state_idle; @@ -2476,7 +2380,7 @@ libssh2_channel_window_read_ex(LIBSSH2_CHANNEL * channel, } if (read_avail) { - unsigned long bytes_queued = 0; + size_t bytes_queued = 0; LIBSSH2_PACKET *packet = _libssh2_list_first(&channel->session->packets); diff --git a/src/hostkey.c b/src/hostkey.c index 83b73f8..7a7972a 100644 --- a/src/hostkey.c +++ b/src/hostkey.c @@ -60,7 +60,7 @@ static int hostkey_method_ssh_rsa_dtor(LIBSSH2_SESSION * session, static int hostkey_method_ssh_rsa_init(LIBSSH2_SESSION * session, const unsigned char *hostkey_data, - unsigned long hostkey_data_len, + size_t hostkey_data_len, void **abstract) { libssh2_rsa_ctx *rsactx; @@ -139,9 +139,9 @@ hostkey_method_ssh_rsa_initPEM(LIBSSH2_SESSION * session, static int hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION * session, const unsigned char *sig, - unsigned long sig_len, + size_t sig_len, const unsigned char *m, - unsigned long m_len, void **abstract) + size_t m_len, void **abstract) { libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *) (*abstract); (void) session; @@ -160,8 +160,8 @@ hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION * session, static int hostkey_method_ssh_rsa_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) { @@ -232,7 +232,7 @@ static int hostkey_method_ssh_dss_dtor(LIBSSH2_SESSION * session, static int hostkey_method_ssh_dss_init(LIBSSH2_SESSION * session, const unsigned char *hostkey_data, - unsigned long hostkey_data_len, + size_t hostkey_data_len, void **abstract) { libssh2_dsa_ctx *dsactx; @@ -314,9 +314,9 @@ hostkey_method_ssh_dss_initPEM(LIBSSH2_SESSION * session, static int hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session, const unsigned char *sig, - unsigned long sig_len, + size_t sig_len, const unsigned char *m, - unsigned long m_len, void **abstract) + size_t m_len, void **abstract) { libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *) (*abstract); @@ -338,8 +338,8 @@ hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION * session, static int hostkey_method_ssh_dss_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) { diff --git a/src/keepalive.c b/src/keepalive.c index 8cdf326..34f226f 100644 --- a/src/keepalive.c +++ b/src/keepalive.c @@ -79,7 +79,7 @@ libssh2_keepalive_send (LIBSSH2_SESSION *session, rc = _libssh2_transport_write(session, keepalive_data, len); /* Silently ignore PACKET_EAGAIN here: if the write buffer is 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, "Unable to send keepalive message"); return rc; diff --git a/src/kex.c b/src/kex.c index 5f00d36..d0767ca 100644 --- a/src/kex.c +++ b/src/kex.c @@ -142,7 +142,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session, if (exchange_state->state == libssh2_NB_state_created) { rc = _libssh2_transport_write(session, exchange_state->e_packet, exchange_state->e_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (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)"); burn_type = _libssh2_packet_burn(session, &exchange_state->burn_state); - if (burn_type == PACKET_EAGAIN) { + if (burn_type == LIBSSH2_ERROR_EAGAIN) { return burn_type; } else if (burn_type <= 0) { /* 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_len, 0, NULL, 0, &exchange_state->req_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if (rc) { @@ -411,7 +411,7 @@ static int diffie_hellman_sha1(LIBSSH2_SESSION *session, if (exchange_state->state == libssh2_NB_state_sent2) { rc = _libssh2_transport_write(session, &exchange_state->c, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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_len, 0, NULL, 0, &exchange_state->req_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY, NULL, 0, &key_state->exchange_state); - if (ret == PACKET_EAGAIN) { + if (ret == LIBSSH2_ERROR_EAGAIN) { 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, 256, SSH_MSG_KEXDH_INIT, SSH_MSG_KEXDH_REPLY, NULL, 0, &key_state->exchange_state); - if (ret == PACKET_EAGAIN) { + if (ret == LIBSSH2_ERROR_EAGAIN) { return ret; } @@ -812,7 +812,7 @@ kex_method_diffie_hellman_group_exchange_sha1_key_exchange if (key_state->state == libssh2_NB_state_created) { rc = _libssh2_transport_write(session, key_state->request, key_state->request_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (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, &key_state->data, &key_state->data_len, 0, NULL, 0, &key_state->req_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (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_len - 1, &key_state->exchange_state); - if (ret == PACKET_EAGAIN) { + if (ret == LIBSSH2_ERROR_EAGAIN) { return ret; } @@ -1107,7 +1107,7 @@ static int kexinit(LIBSSH2_SESSION * session) } rc = _libssh2_transport_write(session, data, data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { session->kexinit_data = data; session->kexinit_data_len = data_len; return rc; @@ -1675,7 +1675,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange, if (key_state->state == libssh2_NB_state_sent) { retcode = kexinit(session); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; return retcode; } else if (retcode) { @@ -1696,7 +1696,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange, &key_state->data, &key_state->data_len, 0, NULL, 0, &key_state->req_state); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; return retcode; } @@ -1732,7 +1732,7 @@ libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange, if (key_state->state == libssh2_NB_state_sent2) { retcode = session->kex->exchange_keys(session, &key_state->key_state_low); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; return retcode; } else if (retcode) { diff --git a/src/libgcrypt.c b/src/libgcrypt.c index 2da0559..b36c78b 100644 --- a/src/libgcrypt.c +++ b/src/libgcrypt.c @@ -344,8 +344,8 @@ int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session, libssh2_dsa_ctx * rsactx, const unsigned char *hash, - unsigned long hash_len, - unsigned char **signature, unsigned long *signature_len) + size_t hash_len, + unsigned char **signature, size_t *signature_len) { gcry_sexp_t sig_sexp; gcry_sexp_t data; diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index da5f67b..1414444 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -83,6 +83,21 @@ # endif #endif +/* Needed for struct iovec on some platforms */ +#ifdef HAVE_SYS_UIO_H +#include +#endif + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_SYS_IOCTL_H +# include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif + #include "libssh2.h" #include "libssh2_publickey.h" #include "libssh2_sftp.h" @@ -116,20 +131,6 @@ static inline int writev(int sock, struct iovec *iov, int nvecs) #endif /* WIN32 */ -/* Needed for struct iovec on some platforms */ -#ifdef HAVE_SYS_UIO_H -#include -#endif - -#ifdef HAVE_SYS_SOCKET_H -# include -#endif -#ifdef HAVE_SYS_IOCTL_H -# include -#endif -#ifdef HAVE_INTTYPES_H -#include -#endif #ifdef LIBSSH2_LIBGCRYPT #include "libgcrypt.h" @@ -235,9 +236,9 @@ typedef struct kmdhgGPsha1kex_state_t unsigned char *tmp; unsigned char h_sig_comp[SHA_DIGEST_LENGTH]; unsigned char c; - unsigned long e_packet_len; - unsigned long s_packet_len; - unsigned long tmp_len; + size_t e_packet_len; + size_t s_packet_len; + size_t tmp_len; _libssh2_bn_ctx *ctx; _libssh2_bn *x; _libssh2_bn *e; @@ -247,9 +248,9 @@ typedef struct kmdhgGPsha1kex_state_t unsigned char *f_value; unsigned char *k_value; unsigned char *h_sig; - unsigned long f_value_len; - unsigned long k_value_len; - unsigned long h_sig_len; + size_t f_value_len; + size_t k_value_len; + size_t h_sig_len; libssh2_sha1_ctx exchange_hash; packet_require_state_t req_state; libssh2_nonblocking_states burn_state; @@ -264,8 +265,8 @@ typedef struct key_exchange_state_low_t _libssh2_bn *g; /* SSH2 defined value (2) */ unsigned char request[13]; unsigned char *data; - unsigned long request_len; - unsigned long data_len; + size_t request_len; + size_t data_len; } key_exchange_state_low_t; typedef struct key_exchange_state_t @@ -274,9 +275,9 @@ typedef struct key_exchange_state_t packet_require_state_t req_state; key_exchange_state_low_t key_state_low; unsigned char *data; - unsigned long data_len; + size_t data_len; unsigned char *oldlocal; - unsigned long oldlocal_len; + size_t oldlocal_len; } key_exchange_state_t; #define FwdNotReq "Forward not requested" @@ -320,7 +321,7 @@ struct _LIBSSH2_PACKET /* Unencrypted Payload (no type byte, no padding, just the facts ma'am) */ unsigned char *data; - unsigned long data_len; + size_t data_len; /* Where to start reading data from, * used for channel data that's been partially consumed */ @@ -536,7 +537,7 @@ struct _LIBSSH2_PUBLICKEY unsigned char *listFetch_s; unsigned char listFetch_buffer[12]; unsigned char *listFetch_data; - unsigned long listFetch_data_len; + size_t listFetch_data_len; }; #define SFTP_HANDLE_MAXLEN 256 /* according to spec! */ @@ -754,9 +755,9 @@ struct _LIBSSH2_SESSION /* State variables used in libssh2_session_startup() */ libssh2_nonblocking_states startup_state; unsigned char *startup_data; - unsigned long startup_data_len; + size_t startup_data_len; 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; key_exchange_state_t startup_key_state; @@ -766,7 +767,7 @@ struct _LIBSSH2_SESSION /* State variables used in libssh2_session_disconnect_ex() */ libssh2_nonblocking_states disconnect_state; unsigned char *disconnect_data; - unsigned long disconnect_data_len; + size_t disconnect_data_len; /* State variables used in libssh2_packet_read() */ libssh2_nonblocking_states readPack_state; @@ -775,14 +776,14 @@ struct _LIBSSH2_SESSION /* State variables used in libssh2_userauth_list() */ libssh2_nonblocking_states userauth_list_state; 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; /* State variables used in libssh2_userauth_password_ex() */ libssh2_nonblocking_states userauth_pswd_state; unsigned char *userauth_pswd_data; unsigned char userauth_pswd_data0; - unsigned long userauth_pswd_data_len; + size_t userauth_pswd_data_len; char *userauth_pswd_newpw; int userauth_pswd_newpw_len; 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() */ libssh2_nonblocking_states userauth_host_state; unsigned char *userauth_host_data; - unsigned long userauth_host_data_len; + size_t userauth_host_data_len; unsigned char *userauth_host_packet; - unsigned long userauth_host_packet_len; + size_t userauth_host_packet_len; unsigned char *userauth_host_method; - unsigned long userauth_host_method_len; + size_t userauth_host_method_len; unsigned char *userauth_host_s; packet_requirev_state_t userauth_host_packet_requirev_state; /* State variables used in libssh2_userauth_publickey_fromfile_ex() */ libssh2_nonblocking_states userauth_pblc_state; unsigned char *userauth_pblc_data; - unsigned long userauth_pblc_data_len; + size_t userauth_pblc_data_len; unsigned char *userauth_pblc_packet; - unsigned long userauth_pblc_packet_len; + size_t userauth_pblc_packet_len; 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_b; 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() */ libssh2_nonblocking_states userauth_kybd_state; unsigned char *userauth_kybd_data; - unsigned long userauth_kybd_data_len; + size_t userauth_kybd_data_len; unsigned char *userauth_kybd_packet; - unsigned long userauth_kybd_packet_len; + size_t userauth_kybd_packet_len; unsigned int userauth_kybd_auth_name_len; char *userauth_kybd_auth_name; unsigned userauth_kybd_auth_instruction_len; @@ -831,17 +832,17 @@ struct _LIBSSH2_SESSION packet_requirev_state_t open_packet_requirev_state; LIBSSH2_CHANNEL *open_channel; unsigned char *open_packet; - unsigned long open_packet_len; + size_t open_packet_len; unsigned char *open_data; - unsigned long open_data_len; - unsigned long open_local_channel; + size_t open_data_len; + uint32_t open_local_channel; /* State variables used in libssh2_channel_direct_tcpip_ex() */ libssh2_nonblocking_states direct_state; unsigned char *direct_message; - unsigned long direct_host_len; - unsigned long direct_shost_len; - unsigned long direct_message_len; + size_t direct_host_len; + size_t direct_shost_len; + size_t direct_message_len; /* State variables used in libssh2_channel_forward_listen_ex() */ libssh2_nonblocking_states fwdLstn_state; @@ -855,7 +856,7 @@ struct _LIBSSH2_SESSION LIBSSH2_PUBLICKEY *pkeyInit_pkey; LIBSSH2_CHANNEL *pkeyInit_channel; unsigned char *pkeyInit_data; - unsigned long pkeyInit_data_len; + size_t pkeyInit_data_len; /* State variables used in libssh2_packet_add() */ libssh2_nonblocking_states packAdd_state; @@ -955,18 +956,18 @@ struct _LIBSSH2_HOSTKEY_METHOD unsigned long hash_len; 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, unsigned const char *passphrase, void **abstract); int (*sig_verify) (LIBSSH2_SESSION * session, const unsigned char *sig, - unsigned long sig_len, const unsigned char *m, - unsigned long m_len, void **abstract); + size_t sig_len, const unsigned char *m, + size_t m_len, void **abstract); 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); int (*encrypt) (LIBSSH2_SESSION * session, unsigned char **dst, - unsigned long *dst_len, const unsigned char *src, - unsigned long src_len, void **abstract); + size_t *dst_len, const unsigned char *src, + size_t src_len, 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); -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 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); @@ -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); -/* 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, key_exchange_state_t * state); unsigned long _libssh2_channel_nextid(LIBSSH2_SESSION * session); diff --git a/src/misc.c b/src/misc.c index dd10a6f..5a4ec9b 100644 --- a/src/misc.c +++ b/src/misc.c @@ -145,7 +145,7 @@ _libssh2_ntohu64(const unsigned char *buf) /* _libssh2_htonu32 */ void -_libssh2_htonu32(unsigned char *buf, unsigned int value) +_libssh2_htonu32(unsigned char *buf, uint32_t value) { buf[0] = (value >> 24) & 0xFF; buf[1] = (value >> 16) & 0xFF; @@ -153,6 +153,25 @@ _libssh2_htonu32(unsigned char *buf, unsigned int value) 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 */ static const char base64_table[] = diff --git a/src/misc.h b/src/misc.h index 7771d65..453f79b 100644 --- a/src/misc.h +++ b/src/misc.h @@ -71,4 +71,11 @@ void _libssh2_list_remove(struct list_node *entry); size_t _libssh2_base64_encode(struct _LIBSSH2_SESSION *session, 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 */ diff --git a/src/openssl.c b/src/openssl.c index 7ae324d..92e63a6 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -385,8 +385,8 @@ int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session, libssh2_rsa_ctx * rsactx, const unsigned char *hash, - unsigned long hash_len, - unsigned char **signature, unsigned long *signature_len) + size_t hash_len, + unsigned char **signature, size_t *signature_len) { int ret; unsigned char *sig; diff --git a/src/openssl.h b/src/openssl.h index 0febc1f..5f0b292 100644 --- a/src/openssl.h +++ b/src/openssl.h @@ -162,9 +162,9 @@ int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx * rsa, int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session, libssh2_rsa_ctx * rsactx, const unsigned char *hash, - unsigned long hash_len, + size_t hash_len, unsigned char **signature, - unsigned long *signature_len); + size_t *signature_len); #define _libssh2_rsa_free(rsactx) RSA_free(rsactx) diff --git a/src/packet.c b/src/packet.c index d159cfc..323d258 100644 --- a/src/packet.c +++ b/src/packet.c @@ -62,6 +62,7 @@ #include "transport.h" #include "channel.h" +#include "packet.h" /* * 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) { rc = _libssh2_transport_write(session, listen_state->packet, 17); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; else if (rc) { 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, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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) { rc = _libssh2_transport_write(session, x11open_state->packet, 17); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { x11open_state->state = libssh2_NB_state_idle; @@ -403,7 +404,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data, _libssh2_htonu32(p, 0); rc = _libssh2_transport_write(session, x11open_state->packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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; data[0] = SSH_MSG_REQUEST_FAILURE; rc = _libssh2_transport_write(session, data, 1); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; LIBSSH2_FREE(session, data); session->packAdd_state = libssh2_NB_state_idle; @@ -665,7 +666,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data, packAdd_channel, datalen - 13, 0, NULL); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; 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; data[0] = SSH_MSG_CHANNEL_FAILURE; rc = _libssh2_transport_write(session, data, 5); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; LIBSSH2_FREE(session, data); 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; rc = packet_queue_listener(session, data, datalen, &session->packAdd_Qlstn_state); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; LIBSSH2_FREE(session, data); @@ -842,7 +843,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data, session->packAdd_state = libssh2_NB_state_jump3; rc = packet_x11_open(session, data, datalen, &session->packAdd_x11open_state); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; 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 */ rc = libssh2_kex_exchange(session, 1, &session->startup_key_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } } @@ -958,9 +959,9 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data, */ 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) + unsigned char **data, size_t *data_len, + int match_ofs, const unsigned char *match_buf, + size_t match_len) { LIBSSH2_PACKET *packet = _libssh2_list_first(&session->packets); @@ -997,10 +998,10 @@ _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type, int _libssh2_packet_askv(LIBSSH2_SESSION * session, const unsigned char *packet_types, - unsigned char **data, unsigned long *data_len, - unsigned long match_ofs, + unsigned char **data, size_t *data_len, + int match_ofs, const unsigned char *match_buf, - unsigned long match_len) + size_t match_len) { int i, packet_types_len = strlen((char *) packet_types); @@ -1026,10 +1027,10 @@ _libssh2_packet_askv(LIBSSH2_SESSION * session, */ int _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, - unsigned char **data, unsigned long *data_len, - unsigned long match_ofs, + unsigned char **data, size_t *data_len, + int match_ofs, const unsigned char *match_buf, - unsigned long match_len, + size_t match_len, packet_require_state_t *state) { 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) { int ret = _libssh2_transport_read(session); - if (ret == PACKET_EAGAIN) + if (ret == LIBSSH2_ERROR_EAGAIN) return ret; else if (ret < 0) { state->start = 0; @@ -1063,7 +1064,7 @@ _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, if (left <= 0) { state->start = 0; - return PACKET_TIMEOUT; + return LIBSSH2_ERROR_TIMEOUT; } return -1; /* no packet available yet */ } @@ -1085,7 +1086,7 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session, libssh2_nonblocking_states * state) { unsigned char *data; - unsigned long data_len; + size_t data_len; unsigned char all_packets[255]; int i; int ret; @@ -1110,7 +1111,7 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session, while (session->socket_state == LIBSSH2_SOCKET_CONNECTED) { ret = _libssh2_transport_read(session); - if (ret == PACKET_EAGAIN) { + if (ret == LIBSSH2_ERROR_EAGAIN) { return ret; } else if (ret < 0) { *state = libssh2_NB_state_idle; @@ -1143,12 +1144,11 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session, */ int -_libssh2_packet_requirev(LIBSSH2_SESSION * session, +_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, + unsigned char **data, size_t *data_len, + int match_ofs, + const unsigned char *match_buf, size_t match_len, packet_requirev_state_t * state) { 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) { int ret = _libssh2_transport_read(session); - if ((ret < 0) && (ret != PACKET_EAGAIN)) { + if ((ret < 0) && (ret != LIBSSH2_ERROR_EAGAIN)) { state->start = 0; return ret; } @@ -1174,9 +1174,9 @@ _libssh2_packet_requirev(LIBSSH2_SESSION * session, if (left <= 0) { state->start = 0; - return PACKET_TIMEOUT; + return LIBSSH2_ERROR_TIMEOUT; } - else if (ret == PACKET_EAGAIN) { + else if (ret == LIBSSH2_ERROR_EAGAIN) { return ret; } } diff --git a/src/packet.h b/src/packet.h new file mode 100644 index 0000000..d66b15b --- /dev/null +++ b/src/packet.h @@ -0,0 +1,76 @@ +#ifndef LIBSSH2_PACKET_H +#define LIBSSH2_PACKET_H +/* + * Copyright (C) 2010 by Daniel Stenberg + * Author: Daniel Stenberg + * + * 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 */ diff --git a/src/publickey.c b/src/publickey.c index 2a0330d..10dd8ac 100644 --- a/src/publickey.c +++ b/src/publickey.c @@ -128,7 +128,7 @@ publickey_status_error(const LIBSSH2_PUBLICKEY *pkey, */ static int 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_SESSION *session = channel->session; @@ -137,7 +137,7 @@ publickey_packet_receive(LIBSSH2_PUBLICKEY * pkey, if (pkey->receive_state == libssh2_NB_state_idle) { rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc != 4) { 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) { rc = _libssh2_channel_read(channel, 0, (char *) pkey->receive_packet, pkey->receive_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc != (int)pkey->receive_packet_len) { 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 */ 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; 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); data += 4; data_len -= 4; - if (data_len < (int)response_len) { + if (data_len < response_len) { /* Malformed response */ return -1; } @@ -224,13 +224,13 @@ publickey_response_success(LIBSSH2_PUBLICKEY * pkey) { LIBSSH2_SESSION *session = pkey->channel->session; unsigned char *data, *s; - unsigned long data_len; + size_t data_len; int response; int rc; while (1) { rc = publickey_packet_receive(pkey, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, @@ -332,7 +332,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session) "subsystem", sizeof("subsystem") - 1, "publickey", strlen("publickey")); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block starting publickey subsystem"); return NULL; @@ -348,7 +348,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session) if (session->pkeyInit_state == libssh2_NB_state_sent1) { rc = libssh2_channel_handle_extended_data2(session->pkeyInit_channel, LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block starting publickey subsystem"); return NULL; @@ -385,7 +385,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session) if (session->pkeyInit_state == libssh2_NB_state_sent2) { rc = _libssh2_channel_write(session->pkeyInit_channel, 0, (char *) buffer, (s - buffer)); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending publickey version packet"); return NULL; @@ -403,7 +403,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session) rc = publickey_packet_receive(session->pkeyInit_pkey, &session->pkeyInit_data, &session->pkeyInit_data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for response from " "publickey subsystem"); @@ -489,7 +489,7 @@ libssh2_publickey_init(LIBSSH2_SESSION * session) session->pkeyInit_state = libssh2_NB_state_sent4; if (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, "Would block closing channel"); 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) { rc = _libssh2_channel_write(channel, 0, (char *) pkey->add_packet, (pkey->add_s - pkey->add_packet)); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if ((pkey->add_s - pkey->add_packet) != rc) { 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); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } @@ -694,7 +694,7 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey, if (pkey->remove_state == libssh2_NB_state_created) { rc = _libssh2_channel_write(channel, 0, (char *) pkey->remove_packet, (pkey->remove_s - pkey->remove_packet)); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if ((pkey->remove_s - pkey->remove_packet) != rc) { LIBSSH2_FREE(session, pkey->remove_packet); @@ -710,7 +710,7 @@ libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY * pkey, } rc = publickey_response_success(pkey); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } @@ -756,7 +756,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys, (char *) pkey->listFetch_buffer, (pkey->listFetch_s - pkey->listFetch_buffer)); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if ((pkey->listFetch_s - pkey->listFetch_buffer) != rc) { pkey->listFetch_state = libssh2_NB_state_idle; @@ -770,7 +770,7 @@ libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY * pkey, unsigned long *num_keys, while (1) { rc = publickey_packet_receive(pkey, &pkey->listFetch_data, &pkey->listFetch_data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, @@ -997,7 +997,7 @@ libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY * pkey) } rc = libssh2_channel_free(pkey->channel); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; LIBSSH2_FREE(session, pkey); diff --git a/src/scp.c b/src/scp.c index d7745e3..d7093fd 100644 --- a/src/scp.c +++ b/src/scp.c @@ -338,7 +338,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb) sizeof("exec") - 1, (char *) session->scpRecv_command, session->scpRecv_command_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block requesting SCP startup"); 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) { rc = _libssh2_channel_write(session->scpRecv_channel, 0, (char *) session->scpRecv_response, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending initial wakeup"); return NULL; @@ -385,7 +385,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb) (char *) session-> scpRecv_response + session->scpRecv_response_len, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for SCP response"); 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, * it is already in the systems so it can't return - * PACKET_EAGAIN + * LIBSSH2_ERROR_EAGAIN */ _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, "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, (char *) session-> scpRecv_response, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting to send SCP ACK"); return NULL; @@ -594,7 +594,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb) (char *) session-> scpRecv_response + session->scpRecv_response_len, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for SCP response"); 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, (char *) session-> scpRecv_response, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending SCP ACK"); return NULL; @@ -747,7 +747,7 @@ scp_recv(LIBSSH2_SESSION * session, const char *path, struct stat * sb) return session->scpRecv_channel; 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_state = libssh2_NB_state_idle; return NULL; @@ -841,7 +841,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, sizeof("exec") - 1, (char *) session->scpSend_command, session->scpSend_command_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block requesting SCP startup"); return NULL; @@ -865,7 +865,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, /* Wait for ACK */ rc = _libssh2_channel_read(session->scpSend_channel, 0, (char *) session->scpSend_response, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for response from remote"); return NULL; @@ -894,7 +894,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, rc = _libssh2_channel_write(session->scpSend_channel, 0, (char *) session->scpSend_response, session->scpSend_response_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending time data for SCP file"); return NULL; @@ -911,7 +911,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, /* Wait for ACK */ rc = _libssh2_channel_read(session->scpSend_channel, 0, (char *) session->scpSend_response, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for response"); return NULL; @@ -952,7 +952,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, rc = _libssh2_channel_write(session->scpSend_channel, 0, (char *) session->scpSend_response, session->scpSend_response_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block send core file data for SCP file"); return NULL; @@ -969,7 +969,7 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, /* Wait for ACK */ rc = _libssh2_channel_read(session->scpSend_channel, 0, (char *) session->scpSend_response, 1); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for response"); return NULL; @@ -1001,7 +1001,8 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, if (rc <= 0) { /* * 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); session->scpSend_err_msg = NULL; @@ -1018,7 +1019,8 @@ scp_send(LIBSSH2_SESSION * session, const char *path, int mode, return session->scpSend_channel; 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_state = libssh2_NB_state_idle; return NULL; diff --git a/src/session.c b/src/session.c index c45b5ae..780fc21 100644 --- a/src/session.c +++ b/src/session.c @@ -86,7 +86,7 @@ LIBSSH2_REALLOC_FUNC(libssh2_default_realloc) * * Wait for a hello from the remote host * 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 banner_receive(LIBSSH2_SESSION * session) @@ -124,7 +124,7 @@ banner_receive(LIBSSH2_SESSION * session) session->socket_block_directions = LIBSSH2_SESSION_BLOCK_INBOUND; session->banner_TxRx_total_send = banner_len; - return PACKET_EAGAIN; + return LIBSSH2_ERROR_EAGAIN; } /* Some kinda error */ @@ -135,7 +135,7 @@ banner_receive(LIBSSH2_SESSION * session) if (ret == 0) { session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; } if (c == '\0') { @@ -178,7 +178,7 @@ banner_receive(LIBSSH2_SESSION * session) * * 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 * 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. @@ -239,11 +239,11 @@ banner_send(LIBSSH2_SESSION * session) session->socket_block_directions = LIBSSH2_SESSION_BLOCK_OUTBOUND; session->banner_TxRx_total_send += ret; - return PACKET_EAGAIN; + return LIBSSH2_ERROR_EAGAIN; } session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_total_send = 0; - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; } /* Set the state back to idle */ @@ -738,7 +738,7 @@ session_free(LIBSSH2_SESSION *session) while ((ch = _libssh2_list_first(&session->channels))) { rc = libssh2_channel_free(ch); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; #if 0 /* 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) { while ((l = _libssh2_list_first(&session->listeners))) { rc = libssh2_channel_forward_cancel(l); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return rc; } @@ -1010,29 +1010,16 @@ session_disconnect(LIBSSH2_SESSION *session, int reason, } *(s++) = SSH_MSG_DISCONNECT; - _libssh2_htonu32(s, reason); - s += 4; - - _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; - } + _libssh2_store_u32(&s, reason); + _libssh2_store_str(&s, description, descr_len); + _libssh2_store_str(&s, lang, lang_len); session->disconnect_state = libssh2_NB_state_created; } rc = _libssh2_transport_write(session, session->disconnect_data, session->disconnect_data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } diff --git a/src/sftp.c b/src/sftp.c index 8faaa52..980cdaa 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -88,12 +88,12 @@ static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle); -/* libssh2_htonu64 +/* _libssh2_store_u64 */ -static void -_libssh2_htonu64(unsigned char *buf, libssh2_uint64_t value) +static void _libssh2_store_u64(unsigned char **ptr, libssh2_uint64_t value) { unsigned long msl = (unsigned long)(value >> 32); + unsigned char *buf = *ptr; buf[0] = (unsigned char)((msl >> 24) & 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[6] = (unsigned char)((value >> 8) & 0xFF); buf[7] = (unsigned char)( value & 0xFF); + + *ptr += 8; } /* @@ -167,7 +169,7 @@ sftp_packet_read(LIBSSH2_SFTP *sftp) } else { rc = _libssh2_channel_read(channel, 0, (char *) buffer, 4); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (4 != rc) { @@ -202,7 +204,7 @@ sftp_packet_read(LIBSSH2_SFTP *sftp) (char *) packet + 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 * 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) { ret = sftp_packet_read(sftp); - if (ret == PACKET_EAGAIN) { + if (ret == LIBSSH2_ERROR_EAGAIN) { return ret; } else if (ret <= 0) { return -1; @@ -350,7 +352,7 @@ sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses, } ret = sftp_packet_read(sftp); - if ((ret < 0) && (ret != PACKET_EAGAIN)) { + if ((ret < 0) && (ret != LIBSSH2_ERROR_EAGAIN)) { sftp->requirev_start = 0; return -1; } else if (ret <= 0) { @@ -360,9 +362,9 @@ sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses, if (left <= 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; } } @@ -415,31 +417,24 @@ sftp_attr2bin(unsigned char *p, const LIBSSH2_SFTP_ATTRIBUTES * attrs) return 4; } - _libssh2_htonu32(s, attrs->flags & flag_mask); - s += 4; + _libssh2_store_u32(&s, attrs->flags & flag_mask); if (attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) { - _libssh2_htonu64(s, attrs->filesize); - s += 8; + _libssh2_store_u64(&s, attrs->filesize); } if (attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) { - _libssh2_htonu32(s, attrs->uid); - s += 4; - _libssh2_htonu32(s, attrs->gid); - s += 4; + _libssh2_store_u32(&s, attrs->uid); + _libssh2_store_u32(&s, attrs->gid); } if (attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { - _libssh2_htonu32(s, attrs->permissions); - s += 4; + _libssh2_store_u32(&s, attrs->permissions); } if (attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) { - _libssh2_htonu32(s, attrs->atime); - s += 4; - _libssh2_htonu32(s, attrs->mtime); - s += 4; + _libssh2_store_u32(&s, attrs->atime); + _libssh2_store_u32(&s, attrs->mtime); } return (s - p); @@ -573,7 +568,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) "subsystem", sizeof("subsystem") - 1, "sftp", strlen("sftp")); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block to request SFTP subsystem"); return NULL; @@ -589,7 +584,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) if (session->sftpInit_state == libssh2_NB_state_sent1) { rc = _libssh2_channel_extended_data(session->sftpInit_channel, LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block requesting handle extended data"); return NULL; @@ -625,7 +620,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) (char *)session->sftpInit_buffer + session->sftpInit_sent, 9 - session->sftpInit_sent); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending SSH_FXP_INIT"); return NULL; @@ -649,7 +644,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION, 0, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for response from SFTP subsystem"); return NULL; @@ -710,7 +705,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) return sftp_handle; 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; if (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_ATTR_PFILETYPE_DIR); - _libssh2_htonu32(s, sftp->open_packet_len - 4); - s += 4; - *(s++) = - (open_type == - LIBSSH2_SFTP_OPENFILE) ? SSH_FXP_OPEN : SSH_FXP_OPENDIR; + _libssh2_store_u32(&s, sftp->open_packet_len - 4); + *(s++) = (open_type == + LIBSSH2_SFTP_OPENFILE) ? SSH_FXP_OPEN : SSH_FXP_OPENDIR; sftp->open_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->open_request_id); - s += 4; - _libssh2_htonu32(s, filename_len); - s += 4; - memcpy(s, filename, filename_len); - s += filename_len; + _libssh2_store_u32(&s, sftp->open_request_id); + _libssh2_store_str(&s, filename, filename_len); + if (open_type == LIBSSH2_SFTP_OPENFILE) { - _libssh2_htonu32(s, flags); - s += 4; + _libssh2_store_u32(&s, flags); 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) { rc = _libssh2_channel_write(channel, 0, (char *) sftp->open_packet, sftp->open_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block sending FXP_OPEN or FXP_OPENDIR command"); return NULL; @@ -906,7 +895,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename, rc = sftp_packet_requirev(sftp, 2, fopen_responses, sftp->open_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting for status message"); return NULL; @@ -934,7 +923,7 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename, /* silly situation, but check for a HANDLE */ rc = sftp_packet_require(sftp, SSH_FXP_HANDLE, 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 */ sftp->open_state = libssh2_NB_state_sent; return NULL; @@ -1067,23 +1056,13 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer, #endif if (sftp->read_state == libssh2_NB_state_allocated) { - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_READ; request_id = sftp->request_id++; - _libssh2_htonu32(s, request_id); - s += 4; - _libssh2_htonu32(s, handle->handle_len); - s += 4; - - 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; + _libssh2_store_u32(&s, request_id); + _libssh2_store_str(&s, handle->handle, handle->handle_len); + _libssh2_store_u64(&s, handle->u.file.offset); + _libssh2_store_u32(&s, bytes_requested); 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) { retcode = _libssh2_channel_write(channel, 0, (char *) packet, packet_len); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { sftp->read_packet = packet; sftp->read_request_id = request_id; sftp->read_total_read = total_read; @@ -1114,7 +1093,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer, retcode = sftp_packet_requirev(sftp, 2, read_responses, request_id, &data, &data_len); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, retcode, "Would block waiting for status message"); } else if (retcode) { @@ -1264,16 +1243,11 @@ static int sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, "Unable to allocate memory for " "FXP_READDIR packet"); - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_READDIR; sftp->readdir_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->readdir_request_id); - s += 4; - _libssh2_htonu32(s, handle->handle_len); - s += 4; - memcpy(s, handle->handle, handle->handle_len); - s += handle->handle_len; + _libssh2_store_u32(&s, sftp->readdir_request_id); + _libssh2_store_str(&s, handle->handle, handle->handle_len); 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, (char *) sftp->readdir_packet, packet_len); - if (retcode == PACKET_EAGAIN) { + if (retcode == LIBSSH2_ERROR_EAGAIN) { return 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, sftp->readdir_request_id, &data, &data_len); - if (retcode == PACKET_EAGAIN) + if (retcode == LIBSSH2_ERROR_EAGAIN) return retcode; else if (retcode) { 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, "Unable to allocate memory for FXP_WRITE"); } - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); + *(s++) = SSH_FXP_WRITE; sftp->write_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->write_request_id); - s += 4; - _libssh2_htonu32(s, handle->handle_len); - s += 4; - 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; + _libssh2_store_u32(&s, sftp->write_request_id); + _libssh2_store_str(&s, handle->handle, handle->handle_len); + _libssh2_store_u64(&s, handle->u.file.offset); + _libssh2_store_str(&s, buffer, count); 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, sftp->write_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { @@ -1507,16 +1473,12 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle, "FSTAT/FSETSTAT packet"); } - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT; sftp->fstat_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->fstat_request_id); - s += 4; - _libssh2_htonu32(s, handle->handle_len); - s += 4; - memcpy(s, handle->handle, handle->handle_len); - s += handle->handle_len; + _libssh2_store_u32(&s, sftp->fstat_request_id); + _libssh2_store_str(&s, handle->handle, handle->handle_len); + if (setstat) { 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) { rc = _libssh2_channel_write(channel, 0, (char *) sftp->fstat_packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, sftp->fstat_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { sftp->fstat_state = libssh2_NB_state_idle; @@ -1655,24 +1617,18 @@ sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle) "packet"); } - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_CLOSE; handle->close_request_id = sftp->request_id++; - _libssh2_htonu32(s, handle->close_request_id); - s += 4; - _libssh2_htonu32(s, handle->handle_len); - s += 4; - memcpy(s, handle->handle, handle->handle_len); - s += handle->handle_len; - + _libssh2_store_u32(&s, handle->close_request_id); + _libssh2_store_str(&s, handle->handle, handle->handle_len); 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, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, handle->close_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { handle->close_state = libssh2_NB_state_idle; @@ -1764,24 +1720,18 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename, "packet"); } - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_REMOVE; sftp->unlink_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->unlink_request_id); - s += 4; - _libssh2_htonu32(s, filename_len); - s += 4; - memcpy(s, filename, filename_len); - s += filename_len; - + _libssh2_store_u32(&s, sftp->unlink_request_id); + _libssh2_store_str(&s, filename, filename_len); 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, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, sftp->unlink_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { @@ -1873,25 +1823,16 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename, "packet"); } - _libssh2_htonu32(sftp->rename_s, packet_len - 4); - sftp->rename_s += 4; + _libssh2_store_u32(&sftp->rename_s, packet_len - 4); *(sftp->rename_s++) = SSH_FXP_RENAME; sftp->rename_request_id = sftp->request_id++; - _libssh2_htonu32(sftp->rename_s, sftp->rename_request_id); - sftp->rename_s += 4; - _libssh2_htonu32(sftp->rename_s, source_filename_len); - sftp->rename_s += 4; - 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; + _libssh2_store_u32(&sftp->rename_s, sftp->rename_request_id); + _libssh2_store_str(&sftp->rename_s, source_filename, + source_filename_len); + _libssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len); - if (sftp->version >= 5) { - _libssh2_htonu32(sftp->rename_s, flags); - sftp->rename_s += 4; - } + if (sftp->version >= 5) + _libssh2_store_u32(&sftp->rename_s, flags); 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) { rc = _libssh2_channel_write(channel, 0, (char *) sftp->rename_packet, sftp->rename_s - sftp->rename_packet); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, sftp->rename_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { 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 */ attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR; - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_MKDIR; sftp->mkdir_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->mkdir_request_id); - s += 4; - _libssh2_htonu32(s, path_len); - s += 4; - memcpy(s, path, path_len); - s += path_len; + _libssh2_store_u32(&s, sftp->mkdir_request_id); + _libssh2_store_str(&s, path, path_len); + s += sftp_attr2bin(s, &attrs); 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) { rc = _libssh2_channel_write(channel, 0, (char *) packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { sftp->mkdir_packet = packet; 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, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { sftp->mkdir_state = libssh2_NB_state_idle; @@ -2107,16 +2044,11 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path, "packet"); } - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_RMDIR; sftp->rmdir_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->rmdir_request_id); - s += 4; - _libssh2_htonu32(s, path_len); - s += 4; - memcpy(s, path, path_len); - s += path_len; + _libssh2_store_u32(&s, sftp->rmdir_request_id); + _libssh2_store_str(&s, path, path_len); 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) { rc = _libssh2_channel_write(channel, 0, (char *) sftp->rmdir_packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, sftp->rmdir_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { sftp->rmdir_state = libssh2_NB_state_idle; @@ -2208,8 +2140,8 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path, "packet"); } - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); + switch (stat_type) { case LIBSSH2_SFTP_SETSTAT: *(s++) = SSH_FXP_SETSTAT; @@ -2224,15 +2156,11 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path, *(s++) = SSH_FXP_STAT; } sftp->stat_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->stat_request_id); - s += 4; - _libssh2_htonu32(s, path_len); - s += 4; - memcpy(s, path, path_len); - s += path_len; - if (stat_type == LIBSSH2_SFTP_SETSTAT) { + _libssh2_store_u32(&s, sftp->stat_request_id); + _libssh2_store_str(&s, path, path_len); + + if (stat_type == LIBSSH2_SFTP_SETSTAT) s += sftp_attr2bin(s, attrs); - } 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) { rc = _libssh2_channel_write(channel, 0, (char *) sftp->stat_packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, sftp->stat_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { sftp->stat_state = libssh2_NB_state_idle; @@ -2340,8 +2268,8 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path, (link_type == LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", path); - _libssh2_htonu32(s, packet_len - 4); - s += 4; + _libssh2_store_u32(&s, packet_len - 4); + switch (link_type) { case LIBSSH2_SFTP_REALPATH: *(s++) = SSH_FXP_REALPATH; @@ -2356,18 +2284,11 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path, *(s++) = SSH_FXP_READLINK; } sftp->symlink_request_id = sftp->request_id++; - _libssh2_htonu32(s, sftp->symlink_request_id); - s += 4; - _libssh2_htonu32(s, path_len); - s += 4; - memcpy(s, path, path_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; - } + _libssh2_store_u32(&s, sftp->symlink_request_id); + _libssh2_store_str(&s, path, path_len); + + if (link_type == LIBSSH2_SFTP_SYMLINK) + _libssh2_store_str(&s, target, target_len); 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) { rc = _libssh2_channel_write(channel, 0, (char *) sftp->symlink_packet, packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (packet_len != rc) { 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, sftp->symlink_request_id, &data, &data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if (rc) { diff --git a/src/transport.c b/src/transport.c index 7d96545..fc4ea97 100644 --- a/src/transport.c +++ b/src/transport.c @@ -138,7 +138,7 @@ decrypt(LIBSSH2_SESSION * session, unsigned char *source, if (session->remote.crypt->crypt(session, source, &session->remote.crypt_abstract)) { LIBSSH2_FREE(session, p->payload); - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; } /* 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 */ 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->remote.comp_abstract)) { LIBSSH2_FREE(session, p->payload); - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; } 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 */ p->payload = LIBSSH2_ALLOC(session, data_len); if (!p->payload) - return PACKET_ENOMEM; + return LIBSSH2_ERROR_ALLOC; memcpy(p->payload, data, data_len); session->fullpacket_payload_len = data_len; @@ -331,7 +331,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) do { if (session->socket_state == LIBSSH2_SOCKET_DISCONNECTED) { - return PACKET_NONE; + return LIBSSH2_ERROR_NONE; } if (session->state & LIBSSH2_STATE_NEWKEYS) { @@ -391,9 +391,9 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) if ((nread < 0) && (errno == EAGAIN)) { session->socket_block_directions |= 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", @@ -420,12 +420,12 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) */ session->socket_block_directions |= LIBSSH2_SESSION_BLOCK_INBOUND; - return PACKET_EAGAIN; + return LIBSSH2_ERROR_EAGAIN; } if (encrypted) { rc = decrypt(session, &p->buf[p->readidx], block, blocksize); - if (rc != PACKET_NONE) { + if (rc != LIBSSH2_ERROR_NONE) { return rc; } /* 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); if (p->packet_length < 1) - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; p->padding_length = block[4]; if (p->padding_length < 0) - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; /* total_num is the number of bytes following the initial (5 bytes) packet length and padding length fields */ @@ -466,14 +466,14 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) * padding, and MAC.)." */ 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 hold all data, including padding and MAC. */ p->payload = LIBSSH2_ALLOC(session, p->total_num); if (!p->payload) { - return PACKET_ENOMEM; + return LIBSSH2_ERROR_ALLOC; } /* init write pointer to start of payload buffer */ p->wptr = p->payload; @@ -538,7 +538,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) if (numdecrypt > 0) { /* now decrypt the lot */ rc = decrypt(session, &p->buf[p->readidx], p->wptr, numdecrypt); - if (rc != PACKET_NONE) { + if (rc != LIBSSH2_ERROR_NONE) { return rc; } @@ -574,13 +574,13 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) /* we have a full packet */ libssh2_transport_read_point1: rc = fullpacket(session, encrypted); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { if (session->packAdd_state != libssh2_NB_state_idle) { - /* fullpacket only returns PACKET_EAGAIN if - * libssh2_packet_add returns PACKET_EAGAIN. If that - * returns PACKET_EAGAIN but the packAdd_state is idle, + /* fullpacket only returns LIBSSH2_ERROR_EAGAIN if + * libssh2_packet_add returns LIBSSH2_ERROR_EAGAIN. If that + * returns LIBSSH2_ERROR_EAGAIN but the packAdd_state is idle, * then the packet has been added to the brigade, but some * immediate action that was taken based on the packet * type (such as key re-exchange) is not yet complete. @@ -599,7 +599,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session) } } 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) { *ret = 0; - return PACKET_NONE; + return LIBSSH2_ERROR_NONE; } /* 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 make the caller really notice his/hers flaw, we return error for this case */ - return PACKET_BADUSE; + return LIBSSH2_ERROR_BAD_USE; } *ret = 1; /* set to make our parent return */ @@ -669,15 +669,15 @@ send_existing(LIBSSH2_SESSION * session, unsigned char *data, /* nothing was sent */ if (errno != EAGAIN) { /* send failure! */ - return PACKET_FAIL; + return LIBSSH2_ERROR_SOCKET_NONE; } session->socket_block_directions |= LIBSSH2_SESSION_BLOCK_OUTBOUND; - return PACKET_EAGAIN; + return LIBSSH2_ERROR_EAGAIN; } 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 * 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 * 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. @@ -740,7 +740,7 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data, LIBSSH2_PACKET_MAXCOMP, &free_data, data, data_len, &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. */ p->outbuf = LIBSSH2_ALLOC(session, total_length); if (!p->outbuf) { - return PACKET_ENOMEM; + return LIBSSH2_ERROR_ALLOC; } /* 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]; if (session->local.crypt->crypt(session, ptr, &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->osent = (ret == -1) ? 0 : ret; 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 */ @@ -857,5 +857,5 @@ _libssh2_transport_write(LIBSSH2_SESSION * session, unsigned char *data, LIBSSH2_FREE(session, p->outbuf); p->outbuf = NULL; - return PACKET_NONE; /* all is good */ + return LIBSSH2_ERROR_NONE; /* all is good */ } diff --git a/src/transport.h b/src/transport.h index 95ab526..9bef1ff 100644 --- a/src/transport.h +++ b/src/transport.h @@ -42,6 +42,7 @@ */ #include "libssh2_priv.h" +#include "packet.h" /* * libssh2_transport_write diff --git a/src/userauth.c b/src/userauth.c index da09927..5527ff5 100644 --- a/src/userauth.c +++ b/src/userauth.c @@ -84,22 +84,9 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username, } *(s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_htonu32(s, username_len); - s += 4; - if (username) { - 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; + _libssh2_store_str(&s, username, username_len); + _libssh2_store_str(&s, "ssh-connection", 14); + _libssh2_store_str(&s, "none", 4); 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) { rc = _libssh2_transport_write(session, session->userauth_list_data, session->userauth_list_data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block requesting userauth list"); return NULL; @@ -132,7 +119,7 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username, &session->userauth_list_data_len, 0, NULL, 0, &session->userauth_list_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block requesting userauth list"); return NULL; @@ -236,28 +223,11 @@ userauth_password(LIBSSH2_SESSION *session, const char *username, } *(s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_htonu32(s, username_len); - s += 4; - memcpy(s, username, username_len); - s += username_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_store_str(&s, username, username_len); + _libssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1); + _libssh2_store_str(&s, "password", sizeof("password") - 1); + *s++ = '\0'; + _libssh2_store_str(&s, password, password_len); _libssh2_debug(session, LIBSSH2_TRACE_AUTH, "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) { rc = _libssh2_transport_write(session, session->userauth_pswd_data, session->userauth_pswd_data_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block writing password request"); } @@ -298,7 +268,7 @@ userauth_password(LIBSSH2_SESSION *session, const char *username, 0, NULL, 0, &session-> userauth_pswd_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting"); } else if (rc) { @@ -379,35 +349,15 @@ userauth_password(LIBSSH2_SESSION *session, const char *username, } *(s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_htonu32(s, username_len); - s += 4; - memcpy(s, username, username_len); - s += username_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 = 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; + _libssh2_store_str(&s, username, username_len); + _libssh2_store_str(&s, "ssh-connection", + sizeof("ssh-connection") - 1); + _libssh2_store_str(&s, "password", + sizeof("password") - 1); + *s++ = 0x01; + _libssh2_store_str(&s, password, password_len); + _libssh2_store_str(&s, session->userauth_pswd_newpw, + session->userauth_pswd_newpw_len); 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_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block waiting"); } @@ -493,9 +443,9 @@ libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const char *username, */ static int file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method, - unsigned long *method_len, + size_t *method_len, unsigned char **pubkeydata, - unsigned long *pubkeydata_len, + size_t *pubkeydata_len, const char *pubkeyfile) { 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_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)) { if (privkeyobj->dtor) { privkeyobj->dtor(session, abstract); @@ -673,20 +623,20 @@ sign_fromfile(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len, */ static int 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 *passphrase, const char *hostname, - unsigned int hostname_len, + size_t hostname_len, const char *local_username, - unsigned int local_username_len) + size_t local_username_len) { int rc; if (session->userauth_host_state == libssh2_NB_state_idle) { const LIBSSH2_HOSTKEY_METHOD *privkeyobj; unsigned char *pubkeydata, *sig; - unsigned long pubkeydata_len; - unsigned long sig_len; + size_t pubkeydata_len; + size_t sig_len; void *abstract; unsigned char buf[5]; struct iovec datavec[4]; @@ -732,45 +682,18 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session, } *(session->userauth_host_s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_htonu32(session->userauth_host_s, username_len); - session->userauth_host_s += 4; - memcpy(session->userauth_host_s, username, username_len); - session->userauth_host_s += username_len; - - /* TODO: change the hideous '14' to a nice defined */ - _libssh2_htonu32(session->userauth_host_s, 14); - session->userauth_host_s += 4; - 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_store_str(&session->userauth_host_s, username, username_len); + _libssh2_store_str(&session->userauth_host_s, "ssh-connection", 14); + _libssh2_store_str(&session->userauth_host_s, "hostbased", 9); + _libssh2_store_str(&session->userauth_host_s, + (const char *)session->userauth_host_method, + session->userauth_host_method_len); + _libssh2_store_str(&session->userauth_host_s, (const char *)pubkeydata, + pubkeydata_len); LIBSSH2_FREE(session, pubkeydata); - - _libssh2_htonu32(session->userauth_host_s, hostname_len); - session->userauth_host_s += 4; - 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; + _libssh2_store_str(&session->userauth_host_s, hostname, hostname_len); + _libssh2_store_str(&session->userauth_host_s, local_username, + local_username_len); rc = file_read_privatekey(session, &privkeyobj, &abstract, session->userauth_host_method, @@ -831,23 +754,16 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session, session->userauth_host_s = session->userauth_host_packet + session->userauth_host_packet_len; - _libssh2_htonu32(session->userauth_host_s, - 4 + session->userauth_host_method_len + 4 + sig_len); - session->userauth_host_s += 4; - - _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_store_u32(&session->userauth_host_s, + 4 + session->userauth_host_method_len + 4 + sig_len); + _libssh2_store_str(&session->userauth_host_s, + (const char *)session->userauth_host_method, + session->userauth_host_method_len); LIBSSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - _libssh2_htonu32(session->userauth_host_s, sig_len); - session->userauth_host_s += 4; - memcpy(session->userauth_host_s, sig, sig_len); - session->userauth_host_s += sig_len; + _libssh2_store_str(&session->userauth_host_s, (const char *)sig, + sig_len); LIBSSH2_FREE(session, sig); _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, session->userauth_host_s - session->userauth_host_packet); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if (rc) { @@ -879,13 +795,13 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session, if (session->userauth_host_state == libssh2_NB_state_sent) { static const unsigned char reply_codes[3] = { SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, 0 }; - unsigned long data_len; + size_t data_len; rc = _libssh2_packet_requirev(session, reply_codes, &session->userauth_host_data, &data_len, 0, NULL, 0, &session-> userauth_host_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } @@ -954,6 +870,7 @@ userauth_publickey(LIBSSH2_SESSION *session, SSH_MSG_USERAUTH_PK_OK, 0 }; int rc; + unsigned char *s; if (session->userauth_pblc_state == libssh2_NB_state_idle) { /* 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 * publickeydata itself */ - session->userauth_pblc_s = session->userauth_pblc_packet = + s = session->userauth_pblc_packet = LIBSSH2_ALLOC(session, session->userauth_pblc_packet_len + 4 + (4 + session->userauth_pblc_method_len) @@ -1008,38 +925,18 @@ userauth_publickey(LIBSSH2_SESSION *session, "Out of memory"); } - *(session->userauth_pblc_s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_htonu32(session->userauth_pblc_s, username_len); - session->userauth_pblc_s += 4; - memcpy(session->userauth_pblc_s, username, username_len); - session->userauth_pblc_s += username_len; + *s++ = SSH_MSG_USERAUTH_REQUEST; + _libssh2_store_str(&s, username, username_len); + _libssh2_store_str(&s, "ssh-connection", 14); + _libssh2_store_str(&s, "publickey", 9); - _libssh2_htonu32(session->userauth_pblc_s, 14); - 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; + session->userauth_pblc_b = s; /* Not sending signature with *this* packet */ - *(session->userauth_pblc_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; + *s++ = 0; + _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, "Attempting publickey authentication"); @@ -1049,7 +946,7 @@ userauth_publickey(LIBSSH2_SESSION *session, if (session->userauth_pblc_state == libssh2_NB_state_created) { rc = _libssh2_transport_write(session, session->userauth_pblc_packet, session->userauth_pblc_packet_len); - if (rc == PACKET_EAGAIN) + if (rc == LIBSSH2_ERROR_EAGAIN) return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); else if (rc) { LIBSSH2_FREE(session, session->userauth_pblc_packet); @@ -1071,7 +968,7 @@ userauth_publickey(LIBSSH2_SESSION *session, NULL, 0, &session-> userauth_pblc_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if (rc) { @@ -1136,17 +1033,16 @@ userauth_publickey(LIBSSH2_SESSION *session, "userauth-publickey signed data"); } - _libssh2_htonu32(s, session->session_id_len); - s += 4; - memcpy (s, session->session_id, session->session_id_len); - s += session->session_id_len; + _libssh2_store_str(&s, (const char *)session->session_id, + session->session_id_len); + memcpy (s, session->userauth_pblc_packet, session->userauth_pblc_packet_len); s += session->userauth_pblc_packet_len; rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract); LIBSSH2_FREE(session, buf); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if (rc) { LIBSSH2_FREE(session, session->userauth_pblc_method); @@ -1184,32 +1080,24 @@ userauth_publickey(LIBSSH2_SESSION *session, session->userauth_pblc_packet = newpacket; } - session->userauth_pblc_s = - session->userauth_pblc_packet + session->userauth_pblc_packet_len; + s = session->userauth_pblc_packet + session->userauth_pblc_packet_len; session->userauth_pblc_b = NULL; - _libssh2_htonu32(session->userauth_pblc_s, - 4 + session->userauth_pblc_method_len + 4 + sig_len); - session->userauth_pblc_s += 4; + _libssh2_store_u32(&s, + 4 + session->userauth_pblc_method_len + 4 + sig_len); + _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); session->userauth_pblc_method = NULL; - _libssh2_htonu32(session->userauth_pblc_s, sig_len); - session->userauth_pblc_s += 4; - memcpy(session->userauth_pblc_s, sig, sig_len); - session->userauth_pblc_s += sig_len; + _libssh2_store_str(&s, (const char *)sig, sig_len); LIBSSH2_FREE(session, sig); _libssh2_debug(session, LIBSSH2_TRACE_AUTH, "Attempting publickey authentication -- phase 2"); + session->userauth_pblc_s = s; 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, session->userauth_pblc_s - session->userauth_pblc_packet); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if (rc) { LIBSSH2_FREE(session, session->userauth_pblc_packet); @@ -1239,7 +1127,7 @@ userauth_publickey(LIBSSH2_SESSION *session, &session->userauth_pblc_data, &session->userauth_pblc_data_len, 0, NULL, 0, &session->userauth_pblc_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block requesting userauth list"); } else if (rc) { @@ -1275,13 +1163,13 @@ userauth_publickey(LIBSSH2_SESSION *session, static int userauth_publickey_fromfile(LIBSSH2_SESSION *session, const char *username, - unsigned int username_len, + size_t username_len, const char *publickey, const char *privatekey, const char *passphrase) { unsigned char *pubkeydata = NULL; - unsigned long pubkeydata_len = 0; + size_t pubkeydata_len = 0; struct privkey_file privkey_file; void *abstract = &privkey_file; int rc; @@ -1407,30 +1295,19 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session, *s++ = SSH_MSG_USERAUTH_REQUEST; /* user name */ - _libssh2_htonu32(s, username_len); - s += 4; - memcpy(s, username, username_len); - s += username_len; + _libssh2_store_str(&s, username, username_len); /* service name */ - _libssh2_htonu32(s, sizeof("ssh-connection") - 1); - s += 4; - memcpy(s, "ssh-connection", sizeof("ssh-connection") - 1); - s += sizeof("ssh-connection") - 1; + _libssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1); /* "keyboard-interactive" */ - _libssh2_htonu32(s, sizeof("keyboard-interactive") - 1); - s += 4; - memcpy(s, "keyboard-interactive", sizeof("keyboard-interactive") - 1); - s += sizeof("keyboard-interactive") - 1; - + _libssh2_store_str(&s, "keyboard-interactive", + sizeof("keyboard-interactive") - 1); /* language tag */ - _libssh2_htonu32(s, 0); - s += 4; + _libssh2_store_u32(&s, 0); /* submethods */ - _libssh2_htonu32(s, 0); - s += 4; + _libssh2_store_u32(&s, 0); _libssh2_debug(session, LIBSSH2_TRACE_AUTH, "Attempting keyboard-interactive authentication"); @@ -1441,7 +1318,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session, if (session->userauth_kybd_state == libssh2_NB_state_created) { rc = _libssh2_transport_write(session, session->userauth_kybd_data, session->userauth_kybd_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if (rc) { LIBSSH2_FREE(session, session->userauth_kybd_data); @@ -1464,7 +1341,7 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session, 0, NULL, 0, &session-> userauth_kybd_packet_requirev_state); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if (rc) { @@ -1630,15 +1507,12 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session, *s = SSH_MSG_USERAUTH_INFO_RESPONSE; s++; - _libssh2_htonu32(s, session->userauth_kybd_num_prompts); - s += 4; + _libssh2_store_u32(&s, session->userauth_kybd_num_prompts); for(i = 0; i != session->userauth_kybd_num_prompts; ++i) { - _libssh2_htonu32(s, session->userauth_kybd_responses[i].length); - s += 4; - memcpy(s, session->userauth_kybd_responses[i].text, - session->userauth_kybd_responses[i].length); - s += session->userauth_kybd_responses[i].length; + _libssh2_store_str(&s, + session->userauth_kybd_responses[i].text, + session->userauth_kybd_responses[i].length); } 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) { rc = _libssh2_transport_write(session, session->userauth_kybd_data, session->userauth_kybd_packet_len); - if (rc == PACKET_EAGAIN) { + if (rc == LIBSSH2_ERROR_EAGAIN) { return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); }