From e7728c0eea48d877120773d071b3c9b309d3d64f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 10 Aug 2019 01:36:01 +0930 Subject: [PATCH] common: implement decoding of query-flags for query_short_channel_ids. Signed-off-by: Rusty Russell --- common/decode_short_channel_ids.c | 45 +++++++++++++++++++++++++++++++ common/decode_short_channel_ids.h | 30 +++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/common/decode_short_channel_ids.c b/common/decode_short_channel_ids.c index c97abe4ab..de5e1b76f 100644 --- a/common/decode_short_channel_ids.c +++ b/common/decode_short_channel_ids.c @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include @@ -60,3 +62,46 @@ struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded) } return NULL; } + +#if EXPERIMENTAL_FEATURES +bigsize_t *decode_scid_query_flags(const tal_t *ctx, + const struct tlv_query_short_channel_ids_tlvs_query_flags *qf) +{ + u8 *encoded = qf->encoded_query_flags; + size_t max = tal_count(encoded); + bigsize_t *flags; + + /* BOLT-61a1365a45cc8b463ddbbe3429d350f8eac787dd #7: + * + * The receiver: + *... + * - if the incoming message includes `query_short_channel_ids_tlvs`: + * - if `encoding_type` is not a known encoding type: + * - MAY fail the connection + * - if `encoded_query_flags` does not decode to exactly one flag per + * `short_channel_id`: + * - MAY fail the connection. + */ + switch (qf->encoding_type) { + case SHORTIDS_ZLIB: + encoded = unzlib(tmpctx, encoded, max); + if (!encoded) + return NULL; + max = tal_count(encoded); + /* fall thru */ + case SHORTIDS_UNCOMPRESSED: + flags = tal_arr(ctx, bigsize_t, 0); + while (max) + tal_arr_expand(&flags, + fromwire_bigsize(cast_const2(const u8 **, + &encoded), + &max)); + + /* encoded is set to NULL if we ran over */ + if (!encoded) + return tal_free(flags); + return flags; + } + return NULL; +} +#endif /* EXPERIMENTAL_FEATURES */ diff --git a/common/decode_short_channel_ids.h b/common/decode_short_channel_ids.h index e73da863c..3af356551 100644 --- a/common/decode_short_channel_ids.h +++ b/common/decode_short_channel_ids.h @@ -3,6 +3,9 @@ #include "config.h" #include #include +#include + +struct tlv_query_short_channel_ids_tlvs_query_flags; /* BOLT #7: * @@ -16,4 +19,31 @@ enum scid_encode_types { }; struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded); + +/* BOLT-61a1365a45cc8b463ddbbe3429d350f8eac787dd #7: + * + * `encoded_query_flags` is an array of bitfields, one varint per bitfield, + * one bitfield for each `short_channel_id`. Bits have the following meaning: + * + * | Bit Position | Meaning | + * | ------------- | ---------------------------------------- | + * | 0 | Sender wants `channel_announcement` | + * | 1 | Sender wants `channel_update` for node 1 | + * | 2 | Sender wants `channel_update` for node 2 | + * | 3 | Sender wants `node_announcement` for node 1 | + * | 4 | Sender wants `node_announcement` for node 2 | + */ +enum scid_query_flag { + SCID_QF_ANNOUNCE = 0x1, + SCID_QF_UPDATE1 = 0x2, + SCID_QF_UPDATE2 = 0x4, + SCID_QF_NODE1 = 0x8, + SCID_QF_NODE2 = 0x10, +}; + +#if EXPERIMENTAL_FEATURES +bigsize_t *decode_scid_query_flags(const tal_t *ctx, + const struct tlv_query_short_channel_ids_tlvs_query_flags *qf); +#endif /* EXPERIMENTAL_FEATURES */ + #endif /* LIGHTNING_COMMON_DECODE_SHORT_CHANNEL_IDS_H */