diff --git a/ssh1bpp.c b/ssh1bpp.c index 92c89435..9640d35d 100644 --- a/ssh1bpp.c +++ b/ssh1bpp.c @@ -91,6 +91,12 @@ void ssh1_bpp_requested_compression(BinaryPacketProtocol *bpp) s->pending_compression_request = TRUE; } +#define BPP_READ(ptr, len) do \ + { \ + crMaybeWaitUntilV(bufchain_try_fetch_consume( \ + s->bpp.in_raw, ptr, len)); \ + } while (0) + static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp) { struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp); @@ -103,8 +109,7 @@ static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp) { unsigned char lenbuf[4]; - crMaybeWaitUntilV(bufchain_try_fetch_consume( - bpp->in_raw, lenbuf, 4)); + BPP_READ(lenbuf, 4); s->len = toint(GET_32BIT_MSB_FIRST(lenbuf)); } @@ -130,8 +135,7 @@ static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp) s->maxlen = s->biglen; s->data = snew_plus_get_aux(s->pktin); - crMaybeWaitUntilV(bufchain_try_fetch_consume( - bpp->in_raw, s->data, s->biglen)); + BPP_READ(s->data, s->biglen); if (s->cipher && detect_attack(s->crcda_ctx, s->data, s->biglen, NULL)) { diff --git a/ssh2bpp-bare.c b/ssh2bpp-bare.c index db645e2f..58e6ae32 100644 --- a/ssh2bpp-bare.c +++ b/ssh2bpp-bare.c @@ -48,6 +48,12 @@ static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp) sfree(s); } +#define BPP_READ(ptr, len) do \ + { \ + crMaybeWaitUntilV(bufchain_try_fetch_consume( \ + s->bpp.in_raw, ptr, len)); \ + } while (0) + static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp) { struct ssh2_bare_bpp_state *s = @@ -59,8 +65,7 @@ static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp) /* Read the length field. */ { unsigned char lenbuf[4]; - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, lenbuf, 4)); + BPP_READ(lenbuf, 4); s->packetlen = toint(GET_32BIT_MSB_FIRST(lenbuf)); } @@ -83,8 +88,7 @@ static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp) /* * Read the remainder of the packet. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, s->data, s->packetlen)); + BPP_READ(s->data, s->packetlen); /* * The data we just read is precisely the initial type byte diff --git a/ssh2bpp.c b/ssh2bpp.c index 272e1711..21ea2401 100644 --- a/ssh2bpp.c +++ b/ssh2bpp.c @@ -158,6 +158,12 @@ void ssh2_bpp_new_incoming_crypto( s->pending_newkeys = FALSE; } +#define BPP_READ(ptr, len) do \ + { \ + crMaybeWaitUntilV(bufchain_try_fetch_consume( \ + s->bpp.in_raw, ptr, len)); \ + } while (0) + static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) { struct ssh2_bpp_state *s = FROMFIELD(bpp, struct ssh2_bpp_state, bpp); @@ -206,8 +212,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) } /* Read an amount corresponding to the MAC. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, s->buf, s->maclen)); + BPP_READ(s->buf, s->maclen); s->packetlen = 0; ssh2_mac_start(s->in.mac); @@ -216,10 +221,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) for (;;) { /* Once around this loop per cipher block. */ /* Read another cipher-block's worth, and tack it on to * the end. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, - s->buf + (s->packetlen + s->maclen), - s->cipherblk)); + BPP_READ(s->buf + (s->packetlen + s->maclen), s->cipherblk); /* Decrypt one more block (a little further back in * the stream). */ ssh2_cipher_decrypt(s->in.cipher, @@ -262,8 +264,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) * OpenSSH encrypt-then-MAC mode: the packet length is * unencrypted, unless the cipher supports length encryption. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, s->buf, 4)); + BPP_READ(s->buf, 4); /* Cipher supports length decryption, so do it */ if (s->in.cipher && (ssh2_cipher_alg(s->in.cipher)->flags & @@ -307,9 +308,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) /* * Read the remainder of the packet. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, s->data + 4, - s->packetlen + s->maclen - 4)); + BPP_READ(s->data + 4, s->packetlen + s->maclen - 4); /* * Check the MAC. @@ -334,8 +333,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) * Acquire and decrypt the first block of the packet. This will * contain the length and padding details. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, s->buf, s->cipherblk)); + BPP_READ(s->buf, s->cipherblk); if (s->in.cipher) ssh2_cipher_decrypt( @@ -376,9 +374,8 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp) /* * Read and decrypt the remainder of the packet. */ - crMaybeWaitUntilV(bufchain_try_fetch_consume( - s->bpp.in_raw, s->data + s->cipherblk, - s->packetlen + s->maclen - s->cipherblk)); + BPP_READ(s->data + s->cipherblk, + s->packetlen + s->maclen - s->cipherblk); /* Decrypt everything _except_ the MAC. */ if (s->in.cipher) diff --git a/sshverstring.c b/sshverstring.c index 37644276..029b140c 100644 --- a/sshverstring.c +++ b/sshverstring.c @@ -197,6 +197,12 @@ static void ssh_verstring_send(struct ssh_verstring_state *s) vs_logevent(("We claim version: %s", s->our_vstring)); } +#define BPP_WAITFOR(minlen) do \ + { \ + crMaybeWaitUntilV( \ + bufchain_size(s->bpp.in_raw) >= (minlen)); \ + } while (0) + void ssh_verstring_handle_input(BinaryPacketProtocol *bpp) { struct ssh_verstring_state *s = @@ -221,8 +227,7 @@ void ssh_verstring_handle_input(BinaryPacketProtocol *bpp) * Every time round this loop, we're at the start of a new * line, so look for the prefix. */ - crMaybeWaitUntilV(bufchain_size(s->bpp.in_raw) >= - s->prefix_wanted.len); + BPP_WAITFOR(s->prefix_wanted.len); bufchain_fetch(s->bpp.in_raw, s->prefix, s->prefix_wanted.len); if (!memcmp(s->prefix, s->prefix_wanted.ptr, s->prefix_wanted.len)) { bufchain_consume(s->bpp.in_raw, s->prefix_wanted.len); @@ -237,7 +242,9 @@ void ssh_verstring_handle_input(BinaryPacketProtocol *bpp) void *data; char *nl; - crMaybeWaitUntilV(bufchain_size(s->bpp.in_raw) > 0); + /* Wait to receive at least 1 byte, but then consume more + * than that if it's there. */ + BPP_WAITFOR(1); bufchain_prefix(s->bpp.in_raw, &data, &len); if ((nl = memchr(data, '\012', len)) != NULL) { bufchain_consume(s->bpp.in_raw, nl - (char *)data + 1);