From e6cefc4b008d5426140158f05702d8e05372aca6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 2 Feb 2021 23:18:01 +1030 Subject: [PATCH] common: infrastructure to construct warning messages. Signed-off-by: Rusty Russell --- common/wire_error.c | 37 +++++++++++++++++++++++++++++++++++++ common/wire_error.h | 24 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/common/wire_error.c b/common/wire_error.c index 7bd1c5d4f..bff0c0cb7 100644 --- a/common/wire_error.c +++ b/common/wire_error.c @@ -42,6 +42,43 @@ u8 *towire_errorfmt(const tal_t *ctx, return msg; } +u8 *towire_warningfmtv(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, + va_list ap) +{ + /* BOLT #1: + * + * The channel is referred to by `channel_id`, unless `channel_id` is + * 0 (i.e. all bytes are 0), in which case it refers to all + * channels. */ + static const struct channel_id all_channels; + char *estr; + u8 *msg; + + estr = tal_vfmt(ctx, fmt, ap); + /* We need tal_len to work, so we use copy. */ + msg = towire_warning(ctx, channel ? channel : &all_channels, + (u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0)); + tal_free(estr); + + return msg; +} + +u8 *towire_warningfmt(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, ...) +{ + va_list ap; + u8 *msg; + + va_start(ap, fmt); + msg = towire_warningfmtv(ctx, channel, fmt, ap); + va_end(ap); + + return msg; +} + bool channel_id_is_all(const struct channel_id *channel_id) { return memeqzero(channel_id, sizeof(*channel_id)); diff --git a/common/wire_error.h b/common/wire_error.h index fdccff3f1..6334956ed 100644 --- a/common/wire_error.h +++ b/common/wire_error.h @@ -32,6 +32,30 @@ u8 *towire_errorfmtv(const tal_t *ctx, const char *fmt, va_list ap); +/** + * towire_warningfmt - helper to turn string into WIRE_WARNING. + * + * @ctx: context to allocate from + * @channel: specific channel to complain about, or NULL for all. + * @fmt: format for warning. + */ +u8 *towire_warningfmt(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, ...) PRINTF_FMT(3,4); + +/** + * towire_warningfmtv - helper to turn string into WIRE_WARNING. + * + * @ctx: context to allocate from + * @channel: specific channel to complain about, or NULL for all. + * @fmt: format for warning. + * @ap: accumulated varargs. + */ +u8 *towire_warningfmtv(const tal_t *ctx, + const struct channel_id *channel, + const char *fmt, + va_list ap); + /* BOLT #1: * * The channel is referred to by `channel_id`, unless `channel_id` is 0