From 3776af4a3531c85e2fc905bc2e72c89596a1ac90 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 8 Dec 2020 17:18:53 +1030 Subject: [PATCH] common/sphinx: make TOTAL_PACKET_SIZE a macro. This paves the way for using it on different-sized onions. Signed-off-by: Rusty Russell --- channeld/channeld.c | 10 +++++----- channeld/full_channel.c | 6 +++--- channeld/full_channel.h | 2 +- channeld/test/run-full_channel.c | 4 ++-- common/htlc_wire.c | 2 +- common/htlc_wire.h | 6 +++--- common/sphinx.c | 6 +++--- common/sphinx.h | 2 +- devtools/blindedpath.c | 2 +- devtools/onion.c | 4 ++-- lightningd/htlc_end.h | 4 ++-- lightningd/onion_message.c | 2 +- lightningd/peer_htlcs.c | 2 +- wire/test/run-peer-wire.c | 2 +- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/channeld/channeld.c b/channeld/channeld.c index 449f4ba5b..d1880f659 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -684,7 +684,7 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg) struct amount_msat amount; u32 cltv_expiry; struct sha256 payment_hash; - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; enum channel_add_err add_err; struct htlc *htlc; #if EXPERIMENTAL_FEATURES @@ -1813,7 +1813,7 @@ static void handle_onion_message(struct peer *peer, const u8 *msg) struct secret ss, *blinding_ss; struct pubkey *blinding_in; struct route_step *rs; - u8 onion[TOTAL_PACKET_SIZE]; + u8 onion[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; const u8 *cursor; size_t max, maxlen; struct tlv_onionmsg_payload *om; @@ -1825,7 +1825,7 @@ static void handle_onion_message(struct peer *peer, const u8 *msg) "Bad onion_message %s", tal_hex(peer, msg)); /* We unwrap the onion now. */ - badreason = parse_onionpacket(onion, TOTAL_PACKET_SIZE, &op); + badreason = parse_onionpacket(onion, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE), &op); if (badreason != 0) { status_debug("onion msg: can't parse onionpacket: %s", onion_wire_name(badreason)); @@ -2014,7 +2014,7 @@ static void handle_onion_message(struct peer *peer, const u8 *msg) /* We send onion msg. */ static void send_onionmsg(struct peer *peer, const u8 *msg) { - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; struct pubkey *blinding; struct tlv_onion_message_tlvs *tlvs = tlv_onion_message_tlvs_new(msg); @@ -3057,7 +3057,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) u32 cltv_expiry; struct amount_msat amount; struct sha256 payment_hash; - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; enum channel_add_err e; const u8 *failwiremsg; const char *failstr; diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 2c6b7fb10..c269b34ec 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -477,7 +477,7 @@ static enum channel_add_err add_htlc(struct channel *channel, struct amount_msat amount, u32 cltv_expiry, const struct sha256 *payment_hash, - const u8 routing[TOTAL_PACKET_SIZE], + const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], const struct pubkey *blinding TAKES, struct htlc **htlcp, bool enforce_aggregate_limits, @@ -517,7 +517,7 @@ static enum channel_add_err add_htlc(struct channel *channel, htlc->blinding = NULL; htlc->failed = NULL; htlc->r = NULL; - htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE, 0); + htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE), 0); old = htlc_get(channel->htlcs, htlc->id, htlc_owner(htlc)); if (old) { @@ -746,7 +746,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel, struct amount_msat amount, u32 cltv_expiry, const struct sha256 *payment_hash, - const u8 routing[TOTAL_PACKET_SIZE], + const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], const struct pubkey *blinding TAKES, struct htlc **htlcp, struct amount_sat *htlc_fee) diff --git a/channeld/full_channel.h b/channeld/full_channel.h index 86ebe07ce..11a43451f 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -112,7 +112,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel, struct amount_msat msatoshi, u32 cltv_expiry, const struct sha256 *payment_hash, - const u8 routing[TOTAL_PACKET_SIZE], + const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], const struct pubkey *blinding TAKES, struct htlc **htlcp, struct amount_sat *htlc_fee); diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 64fdc7aea..85f1c455b 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -136,7 +136,7 @@ static const struct htlc **include_htlcs(struct channel *channel, enum side side int i; const struct htlc **htlcs = tal_arr(channel, const struct htlc *, 5); const struct htlc **changed_htlcs; - u8 *dummy_routing = tal_arr(htlcs, u8, TOTAL_PACKET_SIZE); + u8 *dummy_routing = tal_arr(htlcs, u8, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)); bool ret; for (i = 0; i < 5; i++) { @@ -257,7 +257,7 @@ static void send_and_fulfill_htlc(struct channel *channel, { struct preimage r; struct sha256 rhash; - u8 *dummy_routing = tal_arr(channel, u8, TOTAL_PACKET_SIZE); + u8 *dummy_routing = tal_arr(channel, u8, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)); bool ret; const struct htlc **changed_htlcs; diff --git a/common/htlc_wire.c b/common/htlc_wire.c index c7ed2c4b1..1e1f63f22 100644 --- a/common/htlc_wire.c +++ b/common/htlc_wire.c @@ -34,7 +34,7 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx, struct amount_msat amount, const struct sha256 *payment_hash, u32 cltv_expiry, - const u8 onion_routing_packet[TOTAL_PACKET_SIZE], + const u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], const struct pubkey *blinding TAKES, const struct preimage *preimage TAKES, const struct failed_htlc *failed TAKES) diff --git a/common/htlc_wire.h b/common/htlc_wire.h index e5d085e13..cbf332394 100644 --- a/common/htlc_wire.h +++ b/common/htlc_wire.h @@ -17,7 +17,7 @@ struct added_htlc { struct amount_msat amount; struct sha256 payment_hash; u32 cltv_expiry; - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; /* If this is non-NULL, secret is the resulting shared secret */ struct pubkey *blinding; @@ -31,7 +31,7 @@ struct existing_htlc { struct amount_msat amount; struct sha256 payment_hash; u32 cltv_expiry; - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; /* If this is non-NULL, this is blinding to send with (outgoing) HTLC */ struct pubkey *blinding; /* If fulfilled, this is non-NULL */ @@ -71,7 +71,7 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx, struct amount_msat amount, const struct sha256 *payment_hash, u32 cltv_expiry, - const u8 onion_routing_packet[TOTAL_PACKET_SIZE], + const u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], const struct pubkey *blinding TAKES, const struct preimage *preimage TAKES, const struct failed_htlc *failed TAKES); diff --git a/common/sphinx.c b/common/sphinx.c index 44429c6d5..b27a773c5 100644 --- a/common/sphinx.c +++ b/common/sphinx.c @@ -131,7 +131,7 @@ u8 *serialize_onionpacket( const tal_t *ctx, const struct onionpacket *m) { - u8 *dst = tal_arr(ctx, u8, TOTAL_PACKET_SIZE); + u8 *dst = tal_arr(ctx, u8, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)); u8 der[PUBKEY_CMPR_LEN]; int p = 0; @@ -151,7 +151,7 @@ enum onion_wire parse_onionpacket(const u8 *src, const u8 *cursor = src; size_t max = srclen; - assert(srclen == TOTAL_PACKET_SIZE); + assert(srclen == TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)); dest->version = fromwire_u8(&cursor, &max); if (dest->version != 0x00) { @@ -833,7 +833,7 @@ sphinx_compressed_onion_deserialize(const tal_t *ctx, const u8 *src) tal(ctx, struct sphinx_compressed_onion); /* This is not a compressed onion, so let's not parse it. */ - if (max > TOTAL_PACKET_SIZE) + if (max > TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)) return tal_free(dst); dst->version = fromwire_u8(&cursor, &max); diff --git a/common/sphinx.h b/common/sphinx.h index daad044e0..417a7e686 100644 --- a/common/sphinx.h +++ b/common/sphinx.h @@ -20,7 +20,7 @@ struct node_id; #define PUBKEY_SIZE 33 #define FRAME_SIZE 65 #define ROUTING_INFO_SIZE 1300 -#define TOTAL_PACKET_SIZE (VERSION_SIZE + PUBKEY_SIZE + HMAC_SIZE + ROUTING_INFO_SIZE) +#define TOTAL_PACKET_SIZE(payload) (VERSION_SIZE + PUBKEY_SIZE + (payload) + HMAC_SIZE) struct onionpacket { /* Cleartext information */ diff --git a/devtools/blindedpath.c b/devtools/blindedpath.c index 38611ad99..50cd77ad2 100644 --- a/devtools/blindedpath.c +++ b/devtools/blindedpath.c @@ -205,7 +205,7 @@ int main(int argc, char **argv) } else if (streq(argv[1], "unwrap")) { struct privkey privkey; struct pubkey blinding; - u8 onion[TOTAL_PACKET_SIZE], *dec; + u8 onion[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], *dec; struct onionpacket op; struct secret ss, onion_ss; struct secret hmac, rho; diff --git a/devtools/onion.c b/devtools/onion.c index f02ac1795..638a6b272 100644 --- a/devtools/onion.c +++ b/devtools/onion.c @@ -129,7 +129,7 @@ static struct route_step *decode_with_privkey(const tal_t *ctx, const u8 *onion, if (!hex_decode(hexprivkey, strlen(hexprivkey), &seckey, sizeof(seckey))) errx(1, "Invalid private key hex '%s'", hexprivkey); - why_bad = parse_onionpacket(onion, TOTAL_PACKET_SIZE, &packet); + why_bad = parse_onionpacket(onion, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE), &packet); if (why_bad != 0) errx(1, "Error parsing message: %s", onion_wire_name(why_bad)); @@ -146,7 +146,7 @@ static struct route_step *decode_with_privkey(const tal_t *ctx, const u8 *onion, static void do_decode(int argc, char **argv, const u8 *assocdata) { const tal_t *ctx = talz(NULL, tal_t); - u8 serialized[TOTAL_PACKET_SIZE]; + u8 serialized[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; struct route_step *step; if (argc != 4) diff --git a/lightningd/htlc_end.h b/lightningd/htlc_end.h index c502ce44a..0c5affa61 100644 --- a/lightningd/htlc_end.h +++ b/lightningd/htlc_end.h @@ -31,7 +31,7 @@ struct htlc_in { enum htlc_state hstate; /* Onion information */ - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; /* Shared secret for us to send any failure message (NULL if malformed) */ struct secret *shared_secret; @@ -70,7 +70,7 @@ struct htlc_out { enum htlc_state hstate; /* Onion information */ - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; /* If a local error, this is non-NULL. */ const u8 *failmsg; diff --git a/lightningd/onion_message.c b/lightningd/onion_message.c index bf7c5f1aa..bf0ddd101 100644 --- a/lightningd/onion_message.c +++ b/lightningd/onion_message.c @@ -168,7 +168,7 @@ void handle_onionmsg_forward(struct channel *channel, const u8 *msg) struct short_channel_id *next_scid; struct node_id *next_node; struct pubkey *next_blinding; - u8 onion[TOTAL_PACKET_SIZE]; + u8 onion[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; struct channel *outchan; if (!fromwire_got_onionmsg_forward(msg, msg, &next_scid, &next_node, diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 7ab4cbed8..8d66c002b 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -678,7 +678,7 @@ static void forward_htlc(struct htlc_in *hin, struct amount_msat amt_to_forward, u32 outgoing_cltv_value, const struct short_channel_id *scid, - const u8 next_onion[TOTAL_PACKET_SIZE], + const u8 next_onion[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)], const struct pubkey *next_blinding) { const u8 *failmsg; diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index 57fa34c00..cd7b7665d 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -218,7 +218,7 @@ struct msg_update_add_htlc { struct amount_msat amount_msat; u32 expiry; struct sha256 payment_hash; - u8 onion_routing_packet[TOTAL_PACKET_SIZE]; + u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)]; }; struct msg_update_fee { struct channel_id channel_id;