From cc70b9c4ec413582e0038a55fa198f89d853c10c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 30 Jul 2019 14:55:12 +0930 Subject: [PATCH] wire: use common/bigsize routines Signed-off-by: Rusty Russell --- channeld/test/run-commit_tx.c | 6 ++++ channeld/test/run-full_channel.c | 6 ++++ cli/test/run-large-input.c | 7 ++++ closingd/Makefile | 1 + connectd/Makefile | 1 + gossipd/Makefile | 1 + hsmd/Makefile | 1 + lightningd/test/run-find_my_abspath.c | 6 ++++ lightningd/test/run-invoice-select-inchan.c | 6 ++++ lightningd/test/run-jsonrpc.c | 6 ++++ onchaind/Makefile | 1 + openingd/Makefile | 1 + plugins/Makefile | 1 + wallet/test/run-db.c | 6 ++++ wallet/test/run-wallet.c | 6 ++++ wire/fromwire.c | 37 +++++---------------- wire/test/run-peer-wire.c | 6 ++++ wire/test/run-tlvstream.c | 4 +++ wire/towire.c | 19 ++++------- wire/wire.h | 4 +-- 20 files changed, 83 insertions(+), 43 deletions(-) diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index ccaf0d6c4..d63919237 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -20,6 +20,12 @@ static bool print_superverbose; /*#define DEBUG */ /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for status_fmt */ void status_fmt(enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 304ed075c..c57c1d2cd 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -14,6 +14,12 @@ #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_failreason code UNNEEDED, const char *fmt UNNEEDED, ...) diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index 10ef6052d..4793e87f0 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -1,6 +1,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -26,6 +27,12 @@ int test_printf(const char *format, ...); #undef main /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for version_and_exit */ char *version_and_exit(const void *unused UNNEEDED) { fprintf(stderr, "version_and_exit called!\n"); abort(); } diff --git a/closingd/Makefile b/closingd/Makefile index 02b27fa3f..26cc3fbde 100644 --- a/closingd/Makefile +++ b/closingd/Makefile @@ -44,6 +44,7 @@ $(LIGHTNINGD_CLOSING_OBJS): $(LIGHTNINGD_HEADERS) CLOSINGD_COMMON_OBJS := \ common/amount.o \ common/base32.o \ + common/bigsize.o \ common/bip32.o \ common/close_tx.o \ common/crypto_state.o \ diff --git a/connectd/Makefile b/connectd/Makefile index e543f6a64..6f76ebfd2 100644 --- a/connectd/Makefile +++ b/connectd/Makefile @@ -41,6 +41,7 @@ CONNECTD_COMMON_OBJS := \ common/base32.o \ common/bech32.o \ common/bech32_util.o \ + common/bigsize.o \ common/bip32.o \ common/crypto_state.o \ common/cryptomsg.o \ diff --git a/gossipd/Makefile b/gossipd/Makefile index 5884a863b..72704512e 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -40,6 +40,7 @@ GOSSIPD_COMMON_OBJS := \ common/base32.o \ common/bech32.o \ common/bech32_util.o \ + common/bigsize.o \ common/bip32.o \ common/crypto_state.o \ common/cryptomsg.o \ diff --git a/hsmd/Makefile b/hsmd/Makefile index 7e2ceed27..9b4c49c25 100644 --- a/hsmd/Makefile +++ b/hsmd/Makefile @@ -14,6 +14,7 @@ LIGHTNINGD_HSM_OBJS := $(LIGHTNINGD_HSM_SRC:.c=.o) # Common source we use. HSMD_COMMON_OBJS := \ common/amount.o \ + common/bigsize.o \ common/bip32.o \ common/daemon.o \ common/daemon_conn.o \ diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 8aa1a44b2..77abcc44f 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -18,6 +18,12 @@ char *add_plugin_dir(struct plugins *plugins UNNEEDED, const char *dir UNNEEDED, /* Generated stub for begin_topology */ void begin_topology(struct chain_topology *topo UNNEEDED) { fprintf(stderr, "begin_topology called!\n"); abort(); } +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for channel_notify_new_block */ void channel_notify_new_block(struct lightningd *ld UNNEEDED, u32 block_height UNNEEDED) diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 3373aca7b..1d8d909b6 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -6,6 +6,12 @@ bool deprecated_apis = false; /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for bitcoind_gettxout */ void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED, diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 184bc530a..6c1b14e67 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -3,6 +3,12 @@ #include "../json.c" /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for db_begin_transaction_ */ void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED) { fprintf(stderr, "db_begin_transaction_ called!\n"); abort(); } diff --git a/onchaind/Makefile b/onchaind/Makefile index 0738fd42e..2b9112da9 100644 --- a/onchaind/Makefile +++ b/onchaind/Makefile @@ -48,6 +48,7 @@ $(LIGHTNINGD_ONCHAIN_OBJS): $(LIGHTNINGD_HEADERS) # Common source we use. ONCHAIND_COMMON_OBJS := \ common/amount.o \ + common/bigsize.o \ common/bip32.o \ common/daemon.o \ common/daemon_conn.o \ diff --git a/openingd/Makefile b/openingd/Makefile index a214df5a8..c9b734cb6 100644 --- a/openingd/Makefile +++ b/openingd/Makefile @@ -37,6 +37,7 @@ LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_OPENING_HEADERS_NOGEN) OPENINGD_COMMON_OBJS := \ common/amount.o \ common/base32.o \ + common/bigsize.o \ common/bip32.o \ common/channel_config.o \ common/crypto_state.o \ diff --git a/plugins/Makefile b/plugins/Makefile index 50106c2b6..e1fa9a9bb 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -21,6 +21,7 @@ PLUGIN_COMMON_OBJS := \ common/amount.o \ common/bech32.o \ common/bech32_util.o \ + common/bigsize.o \ common/bolt11.o \ common/daemon.o \ common/hash_u5.o \ diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 20cc404c6..355c6458b 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -18,6 +18,12 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, bool ca #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ static char *db_err; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 584744bfb..dc652028e 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -35,6 +35,12 @@ const struct chainparams *get_chainparams(const struct lightningd *ld) } /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* Generated stub for bitcoind_gettxout */ void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED, diff --git a/wire/fromwire.c b/wire/fromwire.c index d00fb20de..2d7ca0759 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -163,37 +163,18 @@ bool fromwire_bool(const u8 **cursor, size_t *max) return ret; } -u64 fromwire_bigsize(const u8 **cursor, size_t *max) +bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max) { - u8 flag = fromwire_u8(cursor, max); - u64 ret; + bigsize_t v; + size_t len = bigsize_get(*cursor, *max, &v); - switch(flag) { - case 0xff: - ret = fromwire_u64(cursor, max); - if ((ret >> 32) == 0) { - SUPERVERBOSE("not minimal encoded"); - fromwire_fail(cursor, max); - } - break; - case 0xfe: - ret = fromwire_u32(cursor, max); - if ((ret >> 16) == 0) { - SUPERVERBOSE("not minimal encoded"); - fromwire_fail(cursor, max); - } - break; - case 0xfd: - ret = fromwire_u16(cursor, max); - if (ret < 0xfd) { - SUPERVERBOSE("not minimal encoded"); - fromwire_fail(cursor, max); - } - break; - default: - ret = flag; + if (len == 0) { + fromwire_fail(cursor, max); + return 0; } - return ret; + assert(len <= *max); + fromwire(cursor, max, NULL, len); + return v; } void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey) diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index 6231ceed5..1ad5db3ca 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -12,6 +12,12 @@ secp256k1_context *secp256k1_ctx; /* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ /* memsetting pubkeys doesn't work */ diff --git a/wire/test/run-tlvstream.c b/wire/test/run-tlvstream.c index 3b39f7196..2535851ae 100644 --- a/wire/test/run-tlvstream.c +++ b/wire/test/run-tlvstream.c @@ -1,9 +1,13 @@ #include #include #include +#include #include +#include + static const char *reason; +#undef SUPERVERBOSE #define SUPERVERBOSE(r) do { reason = (r); } while(0) #include diff --git a/wire/towire.c b/wire/towire.c index d83867550..eb17a7550 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -87,20 +87,13 @@ void towire_bool(u8 **pptr, bool v) towire(pptr, &val, sizeof(val)); } -void towire_bigsize(u8 **pptr, const u64 val) +void towire_bigsize(u8 **pptr, const bigsize_t val) { - if (val < 0xfd) { - towire_u8(pptr, val); - } else if (val <= 0xffff) { - towire_u8(pptr, 0xfd); - towire_u16(pptr, val); - } else if (val <= 0xffffffff) { - towire_u8(pptr, 0xfe); - towire_u32(pptr, val); - } else { - towire_u8(pptr, 0xff); - towire_u64(pptr, val); - } + u8 buf[BIGSIZE_MAX_LEN]; + size_t len; + + len = bigsize_put(buf, val); + towire(pptr, buf, len); } void towire_pubkey(u8 **pptr, const struct pubkey *pubkey) diff --git a/wire/wire.h b/wire/wire.h index 6a945869e..b0bfd0bb9 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -79,7 +79,7 @@ void towire_tu64(u8 **pptr, u64 v); void towire_double(u8 **pptr, const double *v); void towire_pad(u8 **pptr, size_t num); void towire_bool(u8 **pptr, bool v); -void towire_bigsize(u8 **pptr, const u64 val); +void towire_bigsize(u8 **pptr, const bigsize_t val); void towire_u8_array(u8 **pptr, const u8 *arr, size_t num); @@ -99,7 +99,7 @@ u32 fromwire_tu32(const u8 **cursor, size_t *max); u64 fromwire_tu64(const u8 **cursor, size_t *max); void fromwire_double(const u8 **cursor, size_t *max, double *v); bool fromwire_bool(const u8 **cursor, size_t *max); -u64 fromwire_bigsize(const u8 **cursor, size_t *max); +bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max); void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret); void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey); void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);