mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-26 20:30:59 +01:00
cryptomsg: add helpers to determine if we're partway through msg read/write.
For message read, we do it as header then body, so we can have io_plan_in_started(conn) false, but we're between header and body. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
a8f033f6ae
commit
4e14185961
2 changed files with 29 additions and 0 deletions
|
@ -132,6 +132,8 @@ static struct io_plan *peer_decrypt_body(struct io_conn *conn,
|
||||||
struct io_plan *plan;
|
struct io_plan *plan;
|
||||||
u8 *in, *decrypted;
|
u8 *in, *decrypted;
|
||||||
|
|
||||||
|
pcs->reading_body = false;
|
||||||
|
|
||||||
decrypted = cryptomsg_decrypt_body(pcs->in, &pcs->cs, pcs->in);
|
decrypted = cryptomsg_decrypt_body(pcs->in, &pcs->cs, pcs->in);
|
||||||
if (!decrypted)
|
if (!decrypted)
|
||||||
return io_close(conn);
|
return io_close(conn);
|
||||||
|
@ -198,6 +200,8 @@ static struct io_plan *peer_decrypt_header(struct io_conn *conn,
|
||||||
|
|
||||||
tal_free(pcs->in);
|
tal_free(pcs->in);
|
||||||
|
|
||||||
|
pcs->reading_body = true;
|
||||||
|
|
||||||
/* BOLT #8:
|
/* BOLT #8:
|
||||||
*
|
*
|
||||||
* * Read _exactly_ `l+16` bytes from the network buffer, let
|
* * Read _exactly_ `l+16` bytes from the network buffer, let
|
||||||
|
@ -224,6 +228,7 @@ struct io_plan *peer_read_message(struct io_conn *conn,
|
||||||
*
|
*
|
||||||
* * Read _exactly_ `18-bytes` from the network buffer.
|
* * Read _exactly_ `18-bytes` from the network buffer.
|
||||||
*/
|
*/
|
||||||
|
pcs->reading_body = false;
|
||||||
pcs->in = tal_arr(conn, u8, 18);
|
pcs->in = tal_arr(conn, u8, 18);
|
||||||
pcs->next_in = next;
|
pcs->next_in = next;
|
||||||
return io_read(conn, pcs->in, 18, peer_decrypt_header, pcs);
|
return io_read(conn, pcs->in, 18, peer_decrypt_header, pcs);
|
||||||
|
@ -366,8 +371,23 @@ struct io_plan *peer_write_message(struct io_conn *conn,
|
||||||
return io_write(conn, pcs->out, tal_count(pcs->out), post, pcs);
|
return io_write(conn, pcs->out, tal_count(pcs->out), post, pcs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* We write in one op, so it's all or nothing. */
|
||||||
|
bool peer_out_started(const struct io_conn *conn,
|
||||||
|
const struct peer_crypto_state *cs)
|
||||||
|
{
|
||||||
|
return io_plan_out_started(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We read in two parts, so we might have started body. */
|
||||||
|
bool peer_in_started(const struct io_conn *conn,
|
||||||
|
const struct peer_crypto_state *cs)
|
||||||
|
{
|
||||||
|
return io_plan_in_started(conn) || cs->reading_body;
|
||||||
|
}
|
||||||
|
|
||||||
void init_peer_crypto_state(struct peer *peer, struct peer_crypto_state *pcs)
|
void init_peer_crypto_state(struct peer *peer, struct peer_crypto_state *pcs)
|
||||||
{
|
{
|
||||||
pcs->peer = peer;
|
pcs->peer = peer;
|
||||||
pcs->out = pcs->in = NULL;
|
pcs->out = pcs->in = NULL;
|
||||||
|
pcs->reading_body = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,9 @@ struct peer_crypto_state {
|
||||||
/* Peer who owns us: peer->crypto_state == this */
|
/* Peer who owns us: peer->crypto_state == this */
|
||||||
struct peer *peer;
|
struct peer *peer;
|
||||||
|
|
||||||
|
/* Where we are up to in reading (we do in two parts). */
|
||||||
|
bool reading_body;
|
||||||
|
|
||||||
/* Output and input buffers. */
|
/* Output and input buffers. */
|
||||||
u8 *out, *in;
|
u8 *out, *in;
|
||||||
struct io_plan *(*next_in)(struct io_conn *, struct peer *, u8 *);
|
struct io_plan *(*next_in)(struct io_conn *, struct peer *, u8 *);
|
||||||
|
@ -30,6 +33,12 @@ struct io_plan *peer_read_message(struct io_conn *conn,
|
||||||
struct peer *,
|
struct peer *,
|
||||||
u8 *msg));
|
u8 *msg));
|
||||||
|
|
||||||
|
/* Have we already started writing/reading a message? */
|
||||||
|
bool peer_out_started(const struct io_conn *conn,
|
||||||
|
const struct peer_crypto_state *cs);
|
||||||
|
bool peer_in_started(const struct io_conn *conn,
|
||||||
|
const struct peer_crypto_state *cs);
|
||||||
|
|
||||||
/* Sends message: frees if taken(msg). */
|
/* Sends message: frees if taken(msg). */
|
||||||
struct io_plan *peer_write_message(struct io_conn *conn,
|
struct io_plan *peer_write_message(struct io_conn *conn,
|
||||||
struct peer_crypto_state *cs,
|
struct peer_crypto_state *cs,
|
||||||
|
|
Loading…
Add table
Reference in a new issue