From 5c167d16abf6506e610648f423a146bb09ab2e6d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 3 Dec 2020 20:04:03 +1030 Subject: [PATCH] tools/generate-wire.py: use helpers. This was terrible cut & paste. Signed-off-by: Rusty Russell --- channeld/channeld_wiregen.c | 2 +- channeld/channeld_wiregen.h | 2 +- closingd/closingd_wiregen.c | 2 +- closingd/closingd_wiregen.h | 2 +- common/peer_status_wiregen.c | 2 +- common/peer_status_wiregen.h | 2 +- common/status_wiregen.c | 2 +- common/status_wiregen.h | 2 +- common/test/run-json.c | 13 + common/test/run-param.c | 13 + common/test/run-sphinx.c | 13 + connectd/connectd_gossipd_wiregen.c | 2 +- connectd/connectd_gossipd_wiregen.h | 2 +- connectd/connectd_wiregen.c | 2 +- connectd/connectd_wiregen.h | 2 +- devtools/Makefile | 3 +- gossipd/gossip_store_wiregen.c | 2 +- gossipd/gossip_store_wiregen.h | 2 +- gossipd/gossipd_peerd_wiregen.c | 2 +- gossipd/gossipd_peerd_wiregen.h | 2 +- gossipd/gossipd_wiregen.c | 2 +- gossipd/gossipd_wiregen.h | 2 +- gossipd/test/Makefile | 1 + hsmd/hsmd_wiregen.c | 2 +- hsmd/hsmd_wiregen.h | 2 +- onchaind/onchaind_wiregen.c | 2 +- onchaind/onchaind_wiregen.h | 2 +- openingd/dualopend_wiregen.c | 2 +- openingd/dualopend_wiregen.h | 2 +- openingd/openingd_wiregen.c | 2 +- openingd/openingd_wiregen.h | 2 +- tools/gen/impl_template | 145 +--- wire/common_wiregen.c | 2 +- wire/common_wiregen.h | 2 +- wire/onion_printgen.c | 2 +- wire/onion_printgen.h | 2 +- wire/onion_wiregen.c | 147 +--- wire/onion_wiregen.h | 2 +- wire/peer_printgen.c | 2 +- wire/peer_printgen.h | 2 +- wire/peer_wiregen.c | 1162 +-------------------------- wire/peer_wiregen.h | 2 +- wire/test/run-peer-wire.c | 1 + wire/test/run-tlvstream.c | 1 + wire/tlvstream.c | 159 ++++ wire/tlvstream.h | 9 + wire/wire.h | 1 + 47 files changed, 279 insertions(+), 1457 deletions(-) diff --git a/channeld/channeld_wiregen.c b/channeld/channeld_wiregen.c index 3737097f1..7a3f1b9ac 100644 --- a/channeld/channeld_wiregen.c +++ b/channeld/channeld_wiregen.c @@ -1248,4 +1248,4 @@ bool fromwire_send_onionmsg(const tal_t *ctx, const void *p, u8 onion[1366], str } return cursor != NULL; } -// SHA256STAMP:67432ca0aef376273ef2057a16fa75711b248eb33191ec4de63d8c9a537e2757 +// SHA256STAMP:a33294db19a7303fee551c13179ad7d4b00acdefe081ce9a0d9806f4e44176fe diff --git a/channeld/channeld_wiregen.h b/channeld/channeld_wiregen.h index c6199f655..693d31812 100644 --- a/channeld/channeld_wiregen.h +++ b/channeld/channeld_wiregen.h @@ -246,4 +246,4 @@ bool fromwire_send_onionmsg(const tal_t *ctx, const void *p, u8 onion[1366], str #endif /* LIGHTNING_CHANNELD_CHANNELD_WIREGEN_H */ -// SHA256STAMP:67432ca0aef376273ef2057a16fa75711b248eb33191ec4de63d8c9a537e2757 +// SHA256STAMP:a33294db19a7303fee551c13179ad7d4b00acdefe081ce9a0d9806f4e44176fe diff --git a/closingd/closingd_wiregen.c b/closingd/closingd_wiregen.c index 7aacf44df..78084500c 100644 --- a/closingd/closingd_wiregen.c +++ b/closingd/closingd_wiregen.c @@ -201,4 +201,4 @@ bool fromwire_closingd_complete(const void *p) return false; return cursor != NULL; } -// SHA256STAMP:e6800a03e1351462b19de79fdc56a2866bc5de80d229065dc6d2685b458b5611 +// SHA256STAMP:90bf34ec2ea0eea83908d5a5be93757fd5967d797258c358a60b4d7f2eff3801 diff --git a/closingd/closingd_wiregen.h b/closingd/closingd_wiregen.h index 66f63dcb7..ec5107c95 100644 --- a/closingd/closingd_wiregen.h +++ b/closingd/closingd_wiregen.h @@ -56,4 +56,4 @@ bool fromwire_closingd_complete(const void *p); #endif /* LIGHTNING_CLOSINGD_CLOSINGD_WIREGEN_H */ -// SHA256STAMP:e6800a03e1351462b19de79fdc56a2866bc5de80d229065dc6d2685b458b5611 +// SHA256STAMP:90bf34ec2ea0eea83908d5a5be93757fd5967d797258c358a60b4d7f2eff3801 diff --git a/common/peer_status_wiregen.c b/common/peer_status_wiregen.c index 94c7ad1aa..4a45a99f6 100644 --- a/common/peer_status_wiregen.c +++ b/common/peer_status_wiregen.c @@ -80,4 +80,4 @@ bool fromwire_status_peer_error(const tal_t *ctx, const void *p, struct channel_ fromwire_u8_array(&cursor, &plen, *error_for_them, len); return cursor != NULL; } -// SHA256STAMP:c422e1bdb12318ac77a581f0e23ef5f9b41c24524b01d9a786c47c5809eea76b +// SHA256STAMP:ffe5ce4130de4968d0f46ba1dd28cfb4260caba13c9699c572dc3c192291fa60 diff --git a/common/peer_status_wiregen.h b/common/peer_status_wiregen.h index 72943ced2..e43ca9877 100644 --- a/common/peer_status_wiregen.h +++ b/common/peer_status_wiregen.h @@ -34,4 +34,4 @@ bool fromwire_status_peer_error(const tal_t *ctx, const void *p, struct channel_ #endif /* LIGHTNING_COMMON_PEER_STATUS_WIREGEN_H */ -// SHA256STAMP:c422e1bdb12318ac77a581f0e23ef5f9b41c24524b01d9a786c47c5809eea76b +// SHA256STAMP:ffe5ce4130de4968d0f46ba1dd28cfb4260caba13c9699c572dc3c192291fa60 diff --git a/common/status_wiregen.c b/common/status_wiregen.c index 655498110..7d9dea8ed 100644 --- a/common/status_wiregen.c +++ b/common/status_wiregen.c @@ -191,4 +191,4 @@ bool fromwire_status_peer_billboard(const tal_t *ctx, const void *p, bool *perm, *happenings = fromwire_wirestring(ctx, &cursor, &plen); return cursor != NULL; } -// SHA256STAMP:9d44fada024f78edb1b187537bbf25a5d2e830cff9c822086aa456eb37260926 +// SHA256STAMP:3d02340d94ac275095ca7839fed86b3da622aca2551e39a334a8c25c562a0ef7 diff --git a/common/status_wiregen.h b/common/status_wiregen.h index 72fa365df..a30404afc 100644 --- a/common/status_wiregen.h +++ b/common/status_wiregen.h @@ -53,4 +53,4 @@ bool fromwire_status_peer_billboard(const tal_t *ctx, const void *p, bool *perm, #endif /* LIGHTNING_COMMON_STATUS_WIREGEN_H */ -// SHA256STAMP:9d44fada024f78edb1b187537bbf25a5d2e830cff9c822086aa456eb37260926 +// SHA256STAMP:3d02340d94ac275095ca7839fed86b3da622aca2551e39a334a8c25c562a0ef7 diff --git a/common/test/run-json.c b/common/test/run-json.c index 4fc7be9cc..ce6dd5c48 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -10,9 +10,22 @@ #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for fromwire_tlv */ +bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + void *record UNNEEDED, struct tlv_field **fields UNNEEDED) +{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); } /* Generated stub for node_id_from_hexstr */ bool node_id_from_hexstr(const char *str UNNEEDED, size_t slen UNNEEDED, struct node_id *id UNNEEDED) { fprintf(stderr, "node_id_from_hexstr called!\n"); abort(); } +/* Generated stub for tlv_fields_valid */ +bool tlv_fields_valid(const struct tlv_field *fields UNNEEDED, size_t *err_index UNNEEDED) +{ fprintf(stderr, "tlv_fields_valid called!\n"); abort(); } +/* Generated stub for towire_tlv */ +void towire_tlv(u8 **pptr UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + const void *record UNNEEDED) +{ fprintf(stderr, "towire_tlv called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ diff --git a/common/test/run-param.c b/common/test/run-param.c index 321074571..38399e13e 100644 --- a/common/test/run-param.c +++ b/common/test/run-param.c @@ -39,6 +39,11 @@ struct command_result *command_fail(struct command *cmd, } /* AUTOGENERATED MOCKS START */ +/* Generated stub for fromwire_tlv */ +bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + void *record UNNEEDED, struct tlv_field **fields UNNEEDED) +{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); } /* Generated stub for json_to_channel_id */ bool json_to_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct channel_id *cid UNNEEDED) @@ -64,6 +69,14 @@ int segwit_addr_decode( const char* addr ) { fprintf(stderr, "segwit_addr_decode called!\n"); abort(); } +/* Generated stub for tlv_fields_valid */ +bool tlv_fields_valid(const struct tlv_field *fields UNNEEDED, size_t *err_index UNNEEDED) +{ fprintf(stderr, "tlv_fields_valid called!\n"); abort(); } +/* Generated stub for towire_tlv */ +void towire_tlv(u8 **pptr UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + const void *record UNNEEDED) +{ fprintf(stderr, "towire_tlv called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ /* We do this lightningd-style: */ diff --git a/common/test/run-sphinx.c b/common/test/run-sphinx.c index 8ef97651e..5125182e8 100644 --- a/common/test/run-sphinx.c +++ b/common/test/run-sphinx.c @@ -68,9 +68,17 @@ struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UN /* Generated stub for fromwire_bigsize */ bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_bigsize called!\n"); abort(); } +/* Generated stub for fromwire_tlv */ +bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + void *record UNNEEDED, struct tlv_field **fields UNNEEDED) +{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); } /* Generated stub for pubkey_from_node_id */ bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED) { fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); } +/* Generated stub for tlv_fields_valid */ +bool tlv_fields_valid(const struct tlv_field *fields UNNEEDED, size_t *err_index UNNEEDED) +{ fprintf(stderr, "tlv_fields_valid called!\n"); abort(); } /* Generated stub for towire_amount_msat */ void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED) { fprintf(stderr, "towire_amount_msat called!\n"); abort(); } @@ -80,6 +88,11 @@ void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED) /* Generated stub for towire_bigsize */ void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) { fprintf(stderr, "towire_bigsize called!\n"); abort(); } +/* Generated stub for towire_tlv */ +void towire_tlv(u8 **pptr UNNEEDED, + const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED, + const void *record UNNEEDED) +{ fprintf(stderr, "towire_tlv called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ extern secp256k1_context *secp256k1_ctx; diff --git a/connectd/connectd_gossipd_wiregen.c b/connectd/connectd_gossipd_wiregen.c index 41a9ca445..0e476152f 100644 --- a/connectd/connectd_gossipd_wiregen.c +++ b/connectd/connectd_gossipd_wiregen.c @@ -161,4 +161,4 @@ bool fromwire_gossipd_get_addrs_reply(const tal_t *ctx, const void *p, struct wi fromwire_wireaddr(&cursor, &plen, *addrs + i); return cursor != NULL; } -// SHA256STAMP:5b1b8cca0deddcf16c20684b4e0088b488447e2cab3116477e0e3832d7c9309c +// SHA256STAMP:1eaf731ef4b4233dc782d407020adf0d7c2729d9a7f80bcb83c69cb7a3d4555f diff --git a/connectd/connectd_gossipd_wiregen.h b/connectd/connectd_gossipd_wiregen.h index 62543a8e3..23b6fe30c 100644 --- a/connectd/connectd_gossipd_wiregen.h +++ b/connectd/connectd_gossipd_wiregen.h @@ -54,4 +54,4 @@ bool fromwire_gossipd_get_addrs_reply(const tal_t *ctx, const void *p, struct wi #endif /* LIGHTNING_CONNECTD_CONNECTD_GOSSIPD_WIREGEN_H */ -// SHA256STAMP:5b1b8cca0deddcf16c20684b4e0088b488447e2cab3116477e0e3832d7c9309c +// SHA256STAMP:1eaf731ef4b4233dc782d407020adf0d7c2729d9a7f80bcb83c69cb7a3d4555f diff --git a/connectd/connectd_wiregen.c b/connectd/connectd_wiregen.c index 90da2beb4..37715ec8a 100644 --- a/connectd/connectd_wiregen.c +++ b/connectd/connectd_wiregen.c @@ -406,4 +406,4 @@ bool fromwire_connectd_dev_memleak_reply(const void *p, bool *leak) *leak = fromwire_bool(&cursor, &plen); return cursor != NULL; } -// SHA256STAMP:05735b72da2bcd3e099ca9a3d48dba9b2bf3fd89f7bba8b58042b6ce8b0e8e2e +// SHA256STAMP:28a1014a8d84df52f1ca48befe0a9af817dba4788e3bfd642e94e218583bcfce diff --git a/connectd/connectd_wiregen.h b/connectd/connectd_wiregen.h index 171aaef05..081f5902f 100644 --- a/connectd/connectd_wiregen.h +++ b/connectd/connectd_wiregen.h @@ -103,4 +103,4 @@ bool fromwire_connectd_dev_memleak_reply(const void *p, bool *leak); #endif /* LIGHTNING_CONNECTD_CONNECTD_WIREGEN_H */ -// SHA256STAMP:05735b72da2bcd3e099ca9a3d48dba9b2bf3fd89f7bba8b58042b6ce8b0e8e2e +// SHA256STAMP:28a1014a8d84df52f1ca48befe0a9af817dba4788e3bfd642e94e218583bcfce diff --git a/devtools/Makefile b/devtools/Makefile index cf82e83ea..17c75cd84 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -44,7 +44,8 @@ DEVTOOLS_COMMON_OBJS := \ common/version.o \ common/wireaddr.o \ wire/onion$(EXP)_wiregen.o \ - wire/peer$(EXP)_wiregen.o + wire/peer$(EXP)_wiregen.o \ + wire/tlvstream.o devtools/bolt11-cli: $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/bolt11-cli.o diff --git a/gossipd/gossip_store_wiregen.c b/gossipd/gossip_store_wiregen.c index 0a98f4b13..010128ede 100644 --- a/gossipd/gossip_store_wiregen.c +++ b/gossipd/gossip_store_wiregen.c @@ -187,4 +187,4 @@ bool fromwire_gossipd_local_add_channel_obs(const tal_t *ctx, const void *p, str fromwire_u8_array(&cursor, &plen, *features, flen); return cursor != NULL; } -// SHA256STAMP:c366132d32c26c52e94d122eb16df0b04f9a28bc8eaa437d11565da35ebdbc09 +// SHA256STAMP:ef3ea23ab28e4c27ad11e843929e17a304f58886570438d34f3fe9822af36dc9 diff --git a/gossipd/gossip_store_wiregen.h b/gossipd/gossip_store_wiregen.h index 50515006a..339be0cb2 100644 --- a/gossipd/gossip_store_wiregen.h +++ b/gossipd/gossip_store_wiregen.h @@ -58,4 +58,4 @@ bool fromwire_gossipd_local_add_channel_obs(const tal_t *ctx, const void *p, str #endif /* LIGHTNING_GOSSIPD_GOSSIP_STORE_WIREGEN_H */ -// SHA256STAMP:c366132d32c26c52e94d122eb16df0b04f9a28bc8eaa437d11565da35ebdbc09 +// SHA256STAMP:ef3ea23ab28e4c27ad11e843929e17a304f58886570438d34f3fe9822af36dc9 diff --git a/gossipd/gossipd_peerd_wiregen.c b/gossipd/gossipd_peerd_wiregen.c index 9130b35cb..a04e7a4f0 100644 --- a/gossipd/gossipd_peerd_wiregen.c +++ b/gossipd/gossipd_peerd_wiregen.c @@ -187,4 +187,4 @@ bool fromwire_gossipd_local_channel_announcement(const tal_t *ctx, const void *p fromwire_u8_array(&cursor, &plen, *cannount, len); return cursor != NULL; } -// SHA256STAMP:aa566d781788b1f286ed3e1a2f370aeeea3a20c2ef407e4f2260363b1b4e343e +// SHA256STAMP:467e6e1c97a006551a6ddd32fa4625f075628a268d689cbf65e2556b1e0781b7 diff --git a/gossipd/gossipd_peerd_wiregen.h b/gossipd/gossipd_peerd_wiregen.h index 34e8d2307..f7c385481 100644 --- a/gossipd/gossipd_peerd_wiregen.h +++ b/gossipd/gossipd_peerd_wiregen.h @@ -64,4 +64,4 @@ bool fromwire_gossipd_local_channel_announcement(const tal_t *ctx, const void *p #endif /* LIGHTNING_GOSSIPD_GOSSIPD_PEERD_WIREGEN_H */ -// SHA256STAMP:aa566d781788b1f286ed3e1a2f370aeeea3a20c2ef407e4f2260363b1b4e343e +// SHA256STAMP:467e6e1c97a006551a6ddd32fa4625f075628a268d689cbf65e2556b1e0781b7 diff --git a/gossipd/gossipd_wiregen.c b/gossipd/gossipd_wiregen.c index f6177cab2..f49b3d69c 100644 --- a/gossipd/gossipd_wiregen.c +++ b/gossipd/gossipd_wiregen.c @@ -857,4 +857,4 @@ bool fromwire_gossipd_new_blockheight(const void *p, u32 *blockheight) *blockheight = fromwire_u32(&cursor, &plen); return cursor != NULL; } -// SHA256STAMP:9a3085a0900af1be982379d809293c95374675211d9b254f0471ac62ecc6a40c +// SHA256STAMP:957869c71b6b3ea4d1ec80c7ea4aa093b3738a4d9bc4d145fc429691ae7338e7 diff --git a/gossipd/gossipd_wiregen.h b/gossipd/gossipd_wiregen.h index c0960aea8..d45a3e543 100644 --- a/gossipd/gossipd_wiregen.h +++ b/gossipd/gossipd_wiregen.h @@ -199,4 +199,4 @@ bool fromwire_gossipd_new_blockheight(const void *p, u32 *blockheight); #endif /* LIGHTNING_GOSSIPD_GOSSIPD_WIREGEN_H */ -// SHA256STAMP:9a3085a0900af1be982379d809293c95374675211d9b254f0471ac62ecc6a40c +// SHA256STAMP:957869c71b6b3ea4d1ec80c7ea4aa093b3738a4d9bc4d145fc429691ae7338e7 diff --git a/gossipd/test/Makefile b/gossipd/test/Makefile index 512ff0a0b..c94652515 100644 --- a/gossipd/test/Makefile +++ b/gossipd/test/Makefile @@ -20,6 +20,7 @@ GOSSIPD_TEST_COMMON_OBJS := \ common/utils.o \ wire/peer$(EXP)_wiregen.o \ wire/fromwire.o \ + wire/tlvstream.o \ wire/towire.o ALL_C_SOURCES += $(GOSSIPD_TEST_SRC) diff --git a/hsmd/hsmd_wiregen.c b/hsmd/hsmd_wiregen.c index e42b3f653..fadda2f25 100644 --- a/hsmd/hsmd_wiregen.c +++ b/hsmd/hsmd_wiregen.c @@ -1214,4 +1214,4 @@ bool fromwire_hsmd_get_output_scriptpubkey_reply(const tal_t *ctx, const void *p fromwire_u8_array(&cursor, &plen, *script, script_len); return cursor != NULL; } -// SHA256STAMP:29a74e9db2d286909d13567701c2f6da7e3565d84cc8793f6c06c428f4fb1172 +// SHA256STAMP:abae586a8f129c46f47522b542ff868bdbdab5bda085c0874dbde87dccee4ac0 diff --git a/hsmd/hsmd_wiregen.h b/hsmd/hsmd_wiregen.h index a45022d1f..7f6defeca 100644 --- a/hsmd/hsmd_wiregen.h +++ b/hsmd/hsmd_wiregen.h @@ -271,4 +271,4 @@ bool fromwire_hsmd_get_output_scriptpubkey_reply(const tal_t *ctx, const void *p #endif /* LIGHTNING_HSMD_HSMD_WIREGEN_H */ -// SHA256STAMP:29a74e9db2d286909d13567701c2f6da7e3565d84cc8793f6c06c428f4fb1172 +// SHA256STAMP:abae586a8f129c46f47522b542ff868bdbdab5bda085c0874dbde87dccee4ac0 diff --git a/onchaind/onchaind_wiregen.c b/onchaind/onchaind_wiregen.c index a6359569d..178d0a04d 100644 --- a/onchaind/onchaind_wiregen.c +++ b/onchaind/onchaind_wiregen.c @@ -635,4 +635,4 @@ bool fromwire_onchaind_notify_coin_mvt(const void *p, struct chain_coin_mvt *mvt fromwire_chain_coin_mvt(&cursor, &plen, mvt); return cursor != NULL; } -// SHA256STAMP:b839f62e9b25740472792b8876eabb51f0b8761c3ab175f163e445579dc87d38 +// SHA256STAMP:41d048b3838729465dedb317e7e44c381f6c96f02b78d2423ef504d01d5898f8 diff --git a/onchaind/onchaind_wiregen.h b/onchaind/onchaind_wiregen.h index f0a3a290e..f408ad71b 100644 --- a/onchaind/onchaind_wiregen.h +++ b/onchaind/onchaind_wiregen.h @@ -161,4 +161,4 @@ bool fromwire_onchaind_notify_coin_mvt(const void *p, struct chain_coin_mvt *mvt #endif /* LIGHTNING_ONCHAIND_ONCHAIND_WIREGEN_H */ -// SHA256STAMP:b839f62e9b25740472792b8876eabb51f0b8761c3ab175f163e445579dc87d38 +// SHA256STAMP:41d048b3838729465dedb317e7e44c381f6c96f02b78d2423ef504d01d5898f8 diff --git a/openingd/dualopend_wiregen.c b/openingd/dualopend_wiregen.c index f3a34ea1c..9e0715387 100644 --- a/openingd/dualopend_wiregen.c +++ b/openingd/dualopend_wiregen.c @@ -485,4 +485,4 @@ bool fromwire_dualopend_dev_memleak_reply(const void *p, bool *leak) *leak = fromwire_bool(&cursor, &plen); return cursor != NULL; } -// SHA256STAMP:880d3ebf21b58a7c545945c97269534c55cafe5583a21e3b6d6a441be01edd86 +// SHA256STAMP:d7f4b124313a7d21052b25e737ec35bf6f5dfa3db37aa5ec13ac8cef8c120190 diff --git a/openingd/dualopend_wiregen.h b/openingd/dualopend_wiregen.h index 47b6878fe..f1bef2fcc 100644 --- a/openingd/dualopend_wiregen.h +++ b/openingd/dualopend_wiregen.h @@ -109,4 +109,4 @@ bool fromwire_dualopend_dev_memleak_reply(const void *p, bool *leak); #endif /* LIGHTNING_OPENINGD_DUALOPEND_WIREGEN_H */ -// SHA256STAMP:880d3ebf21b58a7c545945c97269534c55cafe5583a21e3b6d6a441be01edd86 +// SHA256STAMP:d7f4b124313a7d21052b25e737ec35bf6f5dfa3db37aa5ec13ac8cef8c120190 diff --git a/openingd/openingd_wiregen.c b/openingd/openingd_wiregen.c index 6ab5374b6..cd8ad0c3a 100644 --- a/openingd/openingd_wiregen.c +++ b/openingd/openingd_wiregen.c @@ -579,4 +579,4 @@ bool fromwire_openingd_dev_memleak_reply(const void *p, bool *leak) *leak = fromwire_bool(&cursor, &plen); return cursor != NULL; } -// SHA256STAMP:1b4570bed9532371073012f9035708a41aab2d4db70e405561c0182e98a364d8 +// SHA256STAMP:9c977c121d6ff95a6680804710457cf353ee1bb0686a4e51045017b7590fb428 diff --git a/openingd/openingd_wiregen.h b/openingd/openingd_wiregen.h index 52a267c95..dc2b2dc2f 100644 --- a/openingd/openingd_wiregen.h +++ b/openingd/openingd_wiregen.h @@ -121,4 +121,4 @@ bool fromwire_openingd_dev_memleak_reply(const void *p, bool *leak); #endif /* LIGHTNING_OPENINGD_OPENINGD_WIREGEN_H */ -// SHA256STAMP:1b4570bed9532371073012f9035708a41aab2d4db70e405561c0182e98a364d8 +// SHA256STAMP:9c977c121d6ff95a6680804710457cf353ee1bb0686a4e51045017b7590fb428 diff --git a/tools/gen/impl_template b/tools/gen/impl_template index dc15b5673..0d1cc6991 100644 --- a/tools/gen/impl_template +++ b/tools/gen/impl_template @@ -259,157 +259,18 @@ ${static}const struct tlv_record_type tlvs_${tlv.name}[] = { void towire_${tlv.name}(u8 **pptr, const struct ${tlv.struct_name()} *record) { - size_t num_types = ${len(tlv.messages)}; - const struct tlv_record_type *types = tlvs_${tlv.name}; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_${tlv.name}, ${len(tlv.messages)}, record); } bool fromwire_${tlv.name}(const u8 **cursor, size_t *max, struct ${tlv.struct_name()} *record) { - size_t num_types = ${len(tlv.messages)}; - const struct tlv_record_type *types = tlvs_${tlv.name}; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_${tlv.name}, ${len(tlv.messages)}, record, &record->fields); } bool ${tlv.name}_is_valid(const struct ${tlv.struct_name()} *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } % endfor ## END TLV's diff --git a/wire/common_wiregen.c b/wire/common_wiregen.c index db548a1f8..42d564e4c 100644 --- a/wire/common_wiregen.c +++ b/wire/common_wiregen.c @@ -100,4 +100,4 @@ bool fromwire_custommsg_out(const tal_t *ctx, const void *p, u8 **msg) fromwire_u8_array(&cursor, &plen, *msg, msg_len); return cursor != NULL; } -// SHA256STAMP:fe62497ee0bd7740759a6486bfa924b7ababe84fc52a1bb73f4b447549f47613 +// SHA256STAMP:9d2e11b0a9eefdde3d80dc605aeda2c85089c894388740b1d94d7ca97bd95acc diff --git a/wire/common_wiregen.h b/wire/common_wiregen.h index b1596a6fe..4a46a183b 100644 --- a/wire/common_wiregen.h +++ b/wire/common_wiregen.h @@ -41,4 +41,4 @@ bool fromwire_custommsg_out(const tal_t *ctx, const void *p, u8 **msg); #endif /* LIGHTNING_WIRE_COMMON_WIREGEN_H */ -// SHA256STAMP:fe62497ee0bd7740759a6486bfa924b7ababe84fc52a1bb73f4b447549f47613 +// SHA256STAMP:9d2e11b0a9eefdde3d80dc605aeda2c85089c894388740b1d94d7ca97bd95acc diff --git a/wire/onion_printgen.c b/wire/onion_printgen.c index 73439f7dc..e5a858189 100644 --- a/wire/onion_printgen.c +++ b/wire/onion_printgen.c @@ -653,4 +653,4 @@ void printonion_wire_tlv_message(const char *tlv_name, const u8 *msg) { printwire_tlvs(tlv_name, &msg, &plen, print_tlvs_tlv_payload, ARRAY_SIZE(print_tlvs_tlv_payload)); } } -// SHA256STAMP:9e9fd8224814641537178262b21b7ea3c898d5d9282066228938f69236a9df2c +// SHA256STAMP:3edc0ac5906e013377cc195c01e5cb67274c2a7560f51b946ff5149d755d36bd diff --git a/wire/onion_printgen.h b/wire/onion_printgen.h index 66f66b478..cfd8dd1a8 100644 --- a/wire/onion_printgen.h +++ b/wire/onion_printgen.h @@ -57,4 +57,4 @@ void printwire_mpp_timeout(const char *fieldname, const u8 *cursor); #endif /* LIGHTNING_WIRE_ONION_PRINTGEN_H */ -// SHA256STAMP:9e9fd8224814641537178262b21b7ea3c898d5d9282066228938f69236a9df2c +// SHA256STAMP:3edc0ac5906e013377cc195c01e5cb67274c2a7560f51b946ff5149d755d36bd diff --git a/wire/onion_wiregen.c b/wire/onion_wiregen.c index 1dc960439..bdc7c9633 100644 --- a/wire/onion_wiregen.c +++ b/wire/onion_wiregen.c @@ -196,157 +196,18 @@ const struct tlv_record_type tlvs_tlv_payload[] = { void towire_tlv_payload(u8 **pptr, const struct tlv_tlv_payload *record) { - size_t num_types = 4; - const struct tlv_record_type *types = tlvs_tlv_payload; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_tlv_payload, 4, record); } bool fromwire_tlv_payload(const u8 **cursor, size_t *max, struct tlv_tlv_payload *record) { - size_t num_types = 4; - const struct tlv_record_type *types = tlvs_tlv_payload; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_tlv_payload, 4, record, &record->fields); } bool tlv_payload_is_valid(const struct tlv_tlv_payload *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -836,4 +697,4 @@ bool fromwire_mpp_timeout(const void *p) return false; return cursor != NULL; } -// SHA256STAMP:9e9fd8224814641537178262b21b7ea3c898d5d9282066228938f69236a9df2c +// SHA256STAMP:3edc0ac5906e013377cc195c01e5cb67274c2a7560f51b946ff5149d755d36bd diff --git a/wire/onion_wiregen.h b/wire/onion_wiregen.h index e1bdc99a1..c5c5f9ea0 100644 --- a/wire/onion_wiregen.h +++ b/wire/onion_wiregen.h @@ -207,4 +207,4 @@ bool fromwire_mpp_timeout(const void *p); #endif /* LIGHTNING_WIRE_ONION_WIREGEN_H */ -// SHA256STAMP:9e9fd8224814641537178262b21b7ea3c898d5d9282066228938f69236a9df2c +// SHA256STAMP:3edc0ac5906e013377cc195c01e5cb67274c2a7560f51b946ff5149d755d36bd diff --git a/wire/peer_printgen.c b/wire/peer_printgen.c index 8ad533aac..8b41be04c 100644 --- a/wire/peer_printgen.c +++ b/wire/peer_printgen.c @@ -2036,4 +2036,4 @@ void printpeer_wire_tlv_message(const char *tlv_name, const u8 *msg) { printwire_tlvs(tlv_name, &msg, &plen, print_tlvs_reply_channel_range_tlvs, ARRAY_SIZE(print_tlvs_reply_channel_range_tlvs)); } } -// SHA256STAMP:c38a17eb3c7f3c12612754d5419f2ed44c0114be7252eece347fcb2902aaf9f4 +// SHA256STAMP:a2fdacbfaf7025bcb919b426f2e17fdc317a0bc6a80fe3c05f5dedac30f00256 diff --git a/wire/peer_printgen.h b/wire/peer_printgen.h index 29a71c5f5..23c3b37e5 100644 --- a/wire/peer_printgen.h +++ b/wire/peer_printgen.h @@ -70,4 +70,4 @@ void printwire_gossip_timestamp_filter(const char *fieldname, const u8 *cursor); void printwire_channel_update_checksums(const char *fieldname, const u8 **cursor, size_t *plen); void printwire_channel_update_timestamps(const char *fieldname, const u8 **cursor, size_t *plen); #endif /* LIGHTNING_WIRE_PEER_PRINTGEN_H */ -// SHA256STAMP:c38a17eb3c7f3c12612754d5419f2ed44c0114be7252eece347fcb2902aaf9f4 +// SHA256STAMP:a2fdacbfaf7025bcb919b426f2e17fdc317a0bc6a80fe3c05f5dedac30f00256 diff --git a/wire/peer_wiregen.c b/wire/peer_wiregen.c index 6679cd04e..78bd51468 100644 --- a/wire/peer_wiregen.c +++ b/wire/peer_wiregen.c @@ -166,157 +166,18 @@ static const struct tlv_record_type tlvs_init_tlvs[] = { void towire_init_tlvs(u8 **pptr, const struct tlv_init_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_init_tlvs; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_init_tlvs, 1, record); } bool fromwire_init_tlvs(const u8 **cursor, size_t *max, struct tlv_init_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_init_tlvs; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_init_tlvs, 1, record, &record->fields); } bool init_tlvs_is_valid(const struct tlv_init_tlvs *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -437,157 +298,18 @@ const struct tlv_record_type tlvs_n1[] = { void towire_n1(u8 **pptr, const struct tlv_n1 *record) { - size_t num_types = 4; - const struct tlv_record_type *types = tlvs_n1; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_n1, 4, record); } bool fromwire_n1(const u8 **cursor, size_t *max, struct tlv_n1 *record) { - size_t num_types = 4; - const struct tlv_record_type *types = tlvs_n1; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_n1, 4, record, &record->fields); } bool n1_is_valid(const struct tlv_n1 *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -655,157 +377,18 @@ const struct tlv_record_type tlvs_n2[] = { void towire_n2(u8 **pptr, const struct tlv_n2 *record) { - size_t num_types = 2; - const struct tlv_record_type *types = tlvs_n2; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_n2, 2, record); } bool fromwire_n2(const u8 **cursor, size_t *max, struct tlv_n2 *record) { - size_t num_types = 2; - const struct tlv_record_type *types = tlvs_n2; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_n2, 2, record, &record->fields); } bool n2_is_valid(const struct tlv_n2 *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -848,157 +431,18 @@ static const struct tlv_record_type tlvs_open_channel_tlvs[] = { void towire_open_channel_tlvs(u8 **pptr, const struct tlv_open_channel_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_open_channel_tlvs; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_open_channel_tlvs, 1, record); } bool fromwire_open_channel_tlvs(const u8 **cursor, size_t *max, struct tlv_open_channel_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_open_channel_tlvs; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_open_channel_tlvs, 1, record, &record->fields); } bool open_channel_tlvs_is_valid(const struct tlv_open_channel_tlvs *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -1041,157 +485,18 @@ static const struct tlv_record_type tlvs_accept_channel_tlvs[] = { void towire_accept_channel_tlvs(u8 **pptr, const struct tlv_accept_channel_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_accept_channel_tlvs; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_accept_channel_tlvs, 1, record); } bool fromwire_accept_channel_tlvs(const u8 **cursor, size_t *max, struct tlv_accept_channel_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_accept_channel_tlvs; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_accept_channel_tlvs, 1, record, &record->fields); } bool accept_channel_tlvs_is_valid(const struct tlv_accept_channel_tlvs *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -1238,157 +543,18 @@ static const struct tlv_record_type tlvs_query_short_channel_ids_tlvs[] = { void towire_query_short_channel_ids_tlvs(u8 **pptr, const struct tlv_query_short_channel_ids_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_query_short_channel_ids_tlvs; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_query_short_channel_ids_tlvs, 1, record); } bool fromwire_query_short_channel_ids_tlvs(const u8 **cursor, size_t *max, struct tlv_query_short_channel_ids_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_query_short_channel_ids_tlvs; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_query_short_channel_ids_tlvs, 1, record, &record->fields); } bool query_short_channel_ids_tlvs_is_valid(const struct tlv_query_short_channel_ids_tlvs *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -1432,157 +598,18 @@ static const struct tlv_record_type tlvs_query_channel_range_tlvs[] = { void towire_query_channel_range_tlvs(u8 **pptr, const struct tlv_query_channel_range_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_query_channel_range_tlvs; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_query_channel_range_tlvs, 1, record); } bool fromwire_query_channel_range_tlvs(const u8 **cursor, size_t *max, struct tlv_query_channel_range_tlvs *record) { - size_t num_types = 1; - const struct tlv_record_type *types = tlvs_query_channel_range_tlvs; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_query_channel_range_tlvs, 1, record, &record->fields); } bool query_channel_range_tlvs_is_valid(const struct tlv_query_channel_range_tlvs *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -1657,157 +684,18 @@ static const struct tlv_record_type tlvs_reply_channel_range_tlvs[] = { void towire_reply_channel_range_tlvs(u8 **pptr, const struct tlv_reply_channel_range_tlvs *record) { - size_t num_types = 2; - const struct tlv_record_type *types = tlvs_reply_channel_range_tlvs; - if (!record) - return; - - for (size_t i = 0; i < num_types; i++) { - u8 *val; - if (i != 0) - assert(types[i].type > types[i-1].type); - val = types[i].towire(NULL, record); - if (!val) - continue; - - /* BOLT #1: - * - * The sending node: - ... - * - MUST minimally encode `type` and `length`. - */ - towire_bigsize(pptr, types[i].type); - towire_bigsize(pptr, tal_bytelen(val)); - towire(pptr, val, tal_bytelen(val)); - tal_free(val); - } + towire_tlv(pptr, tlvs_reply_channel_range_tlvs, 2, record); } bool fromwire_reply_channel_range_tlvs(const u8 **cursor, size_t *max, struct tlv_reply_channel_range_tlvs *record) { - size_t num_types = 2; - const struct tlv_record_type *types = tlvs_reply_channel_range_tlvs; - while (*max > 0) { - struct tlv_field field; - - /* BOLT #1: - * - * The `type` is encoded using the BigSize format. - */ - field.numtype = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("type"); - goto fail; - } - field.length = fromwire_bigsize(cursor, max); - - /* BOLT #1: - * - if a `type` or `length` is not minimally encoded: - * - MUST fail to parse the `tlv_stream`. - */ - if (!*cursor) { - SUPERVERBOSE("length"); - goto fail; - } - - /* BOLT #1: - * - if `length` exceeds the number of bytes remaining in the - * message: - * - MUST fail to parse the `tlv_stream`. - */ - if (field.length > *max) { - SUPERVERBOSE("value"); - goto fail; - } - field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); - - /* BOLT #1: - * - if `type` is known: - * - MUST decode the next `length` bytes using the known - * encoding for `type`. - */ - field.meta = NULL; - for (size_t i = 0; i < num_types; i++) { - if (types[i].type == field.numtype) - field.meta = &types[i]; - } - - if (field.meta) { - /* Length of message can't exceed 16 bits anyway. */ - size_t tlvlen = field.length; - field.meta->fromwire(cursor, &tlvlen, record); - - if (!*cursor) - goto fail; - - /* BOLT #1: - * - if `length` is not exactly equal to that required - * for the known encoding for `type`: - * - MUST fail to parse the `tlv_stream`. - */ - if (tlvlen != 0) { - SUPERVERBOSE("greater than encoding length"); - goto fail; - } - } else { - /* We didn't read from *cursor through a fromwire, so - * update manually. */ - *cursor += field.length; - } - /* We've read bytes in ->fromwire, so update max */ - *max -= field.length; - tal_arr_expand(&record->fields, field); - } - return true; -fail: - fromwire_fail(cursor, max); - return false; + return fromwire_tlv(cursor, max, tlvs_reply_channel_range_tlvs, 2, record, &record->fields); } bool reply_channel_range_tlvs_is_valid(const struct tlv_reply_channel_range_tlvs *record, size_t *err_index) { - size_t numfields = tal_count(record->fields); - bool first = true; - u64 prev_type = 0; - for (int i=0; ifields[i]; - if (f->numtype % 2 == 0 && f->meta == NULL) { - /* BOLT #1: - * - otherwise, if `type` is unknown: - * - if `type` is even: - * - MUST fail to parse the `tlv_stream`. - * - otherwise, if `type` is odd: - * - MUST discard the next `length` bytes. - */ - SUPERVERBOSE("unknown even"); - if (err_index != NULL) - *err_index = i; - return false; - } else if (!first && f->numtype <= prev_type) { - /* BOLT #1: - * - if decoded `type`s are not strictly-increasing - * (including situations when two or more occurrences - * of the same `type` are met): - * - MUST fail to parse the `tlv_stream`. - */ - if (f->numtype == prev_type) - SUPERVERBOSE("duplicate tlv type"); - else - SUPERVERBOSE("invalid ordering"); - if (err_index != NULL) - *err_index = i; - return false; - } - first = false; - prev_type = f->numtype; - } - return true; + return tlv_fields_valid(record->fields, err_index); } @@ -2742,4 +1630,4 @@ bool fromwire_channel_update_option_channel_htlc_max(const void *p, secp256k1_ec *htlc_maximum_msat = fromwire_amount_msat(&cursor, &plen); return cursor != NULL; } -// SHA256STAMP:c38a17eb3c7f3c12612754d5419f2ed44c0114be7252eece347fcb2902aaf9f4 +// SHA256STAMP:a2fdacbfaf7025bcb919b426f2e17fdc317a0bc6a80fe3c05f5dedac30f00256 diff --git a/wire/peer_wiregen.h b/wire/peer_wiregen.h index 652316ee7..6fc1b8385 100644 --- a/wire/peer_wiregen.h +++ b/wire/peer_wiregen.h @@ -595,4 +595,4 @@ bool fromwire_channel_update_option_channel_htlc_max(const void *p, secp256k1_ec #endif /* LIGHTNING_WIRE_PEER_WIREGEN_H */ -// SHA256STAMP:c38a17eb3c7f3c12612754d5419f2ed44c0114be7252eece347fcb2902aaf9f4 +// SHA256STAMP:a2fdacbfaf7025bcb919b426f2e17fdc317a0bc6a80fe3c05f5dedac30f00256 diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index 62b3124c8..57fa34c00 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -16,6 +16,7 @@ #include #include #include +#include extern secp256k1_context *secp256k1_ctx; diff --git a/wire/test/run-tlvstream.c b/wire/test/run-tlvstream.c index 51d3cfb09..6821f3d5b 100644 --- a/wire/test/run-tlvstream.c +++ b/wire/test/run-tlvstream.c @@ -21,6 +21,7 @@ static const char *reason; #endif #include #include +#include /* AUTOGENERATED MOCKS START */ /* Generated stub for fromwire_channel_id */ diff --git a/wire/tlvstream.c b/wire/tlvstream.c index 788744483..7c8e5afd2 100644 --- a/wire/tlvstream.c +++ b/wire/tlvstream.c @@ -1,7 +1,12 @@ +#include #include #include #include +#ifndef SUPERVERBOSE +#define SUPERVERBOSE(...) +#endif + void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields) { if (!fields) @@ -112,3 +117,157 @@ bool tlvstream_get_tu32(struct tlv_field *stream, u64 type, u32 *value) *value = fromwire_tu64(&v, &max); return true; } + +bool fromwire_tlv(const u8 **cursor, size_t *max, + const struct tlv_record_type *types, size_t num_types, + void *record, struct tlv_field **fields) +{ + while (*max > 0) { + struct tlv_field field; + + /* BOLT #1: + * + * The `type` is encoded using the BigSize format. + */ + field.numtype = fromwire_bigsize(cursor, max); + + /* BOLT #1: + * - if a `type` or `length` is not minimally encoded: + * - MUST fail to parse the `tlv_stream`. + */ + if (!*cursor) { + SUPERVERBOSE("type"); + goto fail; + } + field.length = fromwire_bigsize(cursor, max); + + /* BOLT #1: + * - if a `type` or `length` is not minimally encoded: + * - MUST fail to parse the `tlv_stream`. + */ + if (!*cursor) { + SUPERVERBOSE("length"); + goto fail; + } + + /* BOLT #1: + * - if `length` exceeds the number of bytes remaining in the + * message: + * - MUST fail to parse the `tlv_stream`. + */ + if (field.length > *max) { + SUPERVERBOSE("value"); + goto fail; + } + field.value = tal_dup_arr(record, u8, *cursor, field.length, 0); + + /* BOLT #1: + * - if `type` is known: + * - MUST decode the next `length` bytes using the known + * encoding for `type`. + */ + field.meta = NULL; + for (size_t i = 0; i < num_types; i++) { + if (types[i].type == field.numtype) + field.meta = &types[i]; + } + + if (field.meta) { + /* Length of message can't exceed 16 bits anyway. */ + size_t tlvlen = field.length; + field.meta->fromwire(cursor, &tlvlen, record); + + if (!*cursor) + goto fail; + + /* BOLT #1: + * - if `length` is not exactly equal to that required + * for the known encoding for `type`: + * - MUST fail to parse the `tlv_stream`. + */ + if (tlvlen != 0) { + SUPERVERBOSE("greater than encoding length"); + goto fail; + } + } else { + /* We didn't read from *cursor through a fromwire, so + * update manually. */ + *cursor += field.length; + } + /* We've read bytes in ->fromwire, so update max */ + *max -= field.length; + tal_arr_expand(fields, field); + } + return true; +fail: + fromwire_fail(cursor, max); + return false; +} + +bool tlv_fields_valid(const struct tlv_field *fields, size_t *err_index) +{ + size_t numfields = tal_count(fields); + bool first = true; + u64 prev_type = 0; + for (int i=0; inumtype % 2 == 0 && f->meta == NULL) { + /* BOLT #1: + * - otherwise, if `type` is unknown: + * - if `type` is even: + * - MUST fail to parse the `tlv_stream`. + * - otherwise, if `type` is odd: + * - MUST discard the next `length` bytes. + */ + SUPERVERBOSE("unknown even"); + if (err_index != NULL) + *err_index = i; + return false; + } else if (!first && f->numtype <= prev_type) { + /* BOLT #1: + * - if decoded `type`s are not strictly-increasing + * (including situations when two or more occurrences + * of the same `type` are met): + * - MUST fail to parse the `tlv_stream`. + */ + if (f->numtype == prev_type) + SUPERVERBOSE("duplicate tlv type"); + else + SUPERVERBOSE("invalid ordering"); + if (err_index != NULL) + *err_index = i; + return false; + } + first = false; + prev_type = f->numtype; + } + return true; +} + +void towire_tlv(u8 **pptr, + const struct tlv_record_type *types, size_t num_types, + const void *record) +{ + if (!record) + return; + + for (size_t i = 0; i < num_types; i++) { + u8 *val; + if (i != 0) + assert(types[i].type > types[i-1].type); + val = types[i].towire(NULL, record); + if (!val) + continue; + + /* BOLT #1: + * + * The sending node: + ... + * - MUST minimally encode `type` and `length`. + */ + towire_bigsize(pptr, types[i].type); + towire_bigsize(pptr, tal_bytelen(val)); + towire(pptr, val, tal_bytelen(val)); + tal_free(val); + } +} diff --git a/wire/tlvstream.h b/wire/tlvstream.h index d36ac6593..a6a12982b 100644 --- a/wire/tlvstream.h +++ b/wire/tlvstream.h @@ -30,6 +30,15 @@ struct tlv_field { /* Given any tlvstream serialize the raw fields (untyped ones). */ void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields); +/* Generic TLV decode/encode */ +bool fromwire_tlv(const u8 **cursor, size_t *max, + const struct tlv_record_type *types, size_t num_types, + void *record, struct tlv_field **fields); +void towire_tlv(u8 **pptr, + const struct tlv_record_type *types, size_t num_types, + const void *record); +bool tlv_fields_valid(const struct tlv_field *fields, size_t *err_index); + /* Generic primitive setters for tlvstreams. */ diff --git a/wire/wire.h b/wire/wire.h index bddbb5637..96f93c42c 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -6,6 +6,7 @@ #include #include #include +#include struct ripemd160; struct sha256;