2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2017-10-11 12:01:50 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/wire_error.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2017-10-11 12:01:50 +02:00
|
|
|
|
|
|
|
u8 *towire_errorfmtv(const tal_t *ctx,
|
|
|
|
const struct channel_id *channel,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
char *estr;
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
estr = tal_vfmt(ctx, fmt, ap);
|
|
|
|
/* We need tal_len to work, so we use copy. */
|
2021-02-03 03:51:41 +01:00
|
|
|
msg = towire_error(ctx, channel,
|
2017-10-11 12:01:50 +02:00
|
|
|
(u8 *)tal_dup_arr(estr, char, estr, strlen(estr), 0));
|
|
|
|
tal_free(estr);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *towire_errorfmt(const tal_t *ctx,
|
|
|
|
const struct channel_id *channel,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
msg = towire_errorfmtv(ctx, channel, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:48:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-22 11:03:03 +02:00
|
|
|
bool channel_id_is_all(const struct channel_id *channel_id)
|
2017-10-11 12:01:50 +02:00
|
|
|
{
|
|
|
|
return memeqzero(channel_id, sizeof(*channel_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
char *sanitize_error(const tal_t *ctx, const u8 *errmsg,
|
|
|
|
struct channel_id *channel_id)
|
|
|
|
{
|
|
|
|
struct channel_id dummy;
|
|
|
|
u8 *data;
|
|
|
|
size_t i;
|
2024-02-10 18:40:05 +01:00
|
|
|
const char *tag;
|
2017-10-11 12:01:50 +02:00
|
|
|
|
|
|
|
if (!channel_id)
|
|
|
|
channel_id = &dummy;
|
|
|
|
|
2021-02-02 13:46:01 +01:00
|
|
|
if (fromwire_error(ctx, errmsg, channel_id, &data))
|
2024-01-23 00:06:03 +01:00
|
|
|
tag = "ERROR";
|
2021-02-02 13:46:01 +01:00
|
|
|
else if (fromwire_warning(ctx, errmsg, channel_id, &data))
|
2024-01-23 00:06:03 +01:00
|
|
|
tag = "WARNING";
|
2022-12-01 22:36:06 +01:00
|
|
|
else if (fromwire_tx_abort(ctx, errmsg, channel_id, &data))
|
2024-01-23 00:06:03 +01:00
|
|
|
tag = "ABORT";
|
2021-02-02 13:46:01 +01:00
|
|
|
else
|
2017-10-11 12:01:50 +02:00
|
|
|
return tal_fmt(ctx, "Invalid ERROR message '%s'",
|
|
|
|
tal_hex(ctx, errmsg));
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node:
|
|
|
|
*...
|
|
|
|
* - if `data` is not composed solely of printable ASCII characters
|
|
|
|
* (For reference: the printable character set includes byte values 32
|
|
|
|
* through 126, inclusive):
|
|
|
|
* - SHOULD NOT print out `data` verbatim.
|
2017-10-11 12:01:50 +02:00
|
|
|
*/
|
2018-07-28 08:00:16 +02:00
|
|
|
for (i = 0; i < tal_count(data); i++) {
|
2017-10-11 12:01:50 +02:00
|
|
|
if (data[i] < 32 || data[i] > 127) {
|
|
|
|
/* Convert to hex, minus NUL term */
|
|
|
|
data = (u8 *)tal_hex(ctx, data);
|
2018-07-28 08:00:16 +02:00
|
|
|
tal_resize(&data, strlen((const char *)data));
|
2017-10-11 12:01:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:46:01 +01:00
|
|
|
return tal_fmt(ctx, "%s%s%s: %.*s",
|
2024-01-23 00:06:03 +01:00
|
|
|
tag,
|
2021-02-02 13:46:01 +01:00
|
|
|
channel_id_is_all(channel_id) ? "": " channel ",
|
|
|
|
channel_id_is_all(channel_id) ? ""
|
|
|
|
: type_to_string(tmpctx, struct channel_id, channel_id),
|
2018-07-28 08:00:16 +02:00
|
|
|
(int)tal_count(data), (char *)data);
|
2017-10-11 12:01:50 +02:00
|
|
|
}
|
2023-10-22 06:07:32 +02:00
|
|
|
|
|
|
|
const char *is_peer_warning(const tal_t *ctx, const u8 *msg)
|
|
|
|
{
|
|
|
|
if (fromwire_peektype(msg) != WIRE_WARNING)
|
|
|
|
return NULL;
|
|
|
|
/* connectd demuxes, so we only see it if channel_id is ours. */
|
|
|
|
return sanitize_error(ctx, msg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *is_peer_error(const tal_t *ctx, const u8 *msg)
|
|
|
|
{
|
|
|
|
if (fromwire_peektype(msg) != WIRE_ERROR)
|
|
|
|
return NULL;
|
|
|
|
/* connectd demuxes, so we only see it if channel_id is ours. */
|
|
|
|
return sanitize_error(ctx, msg, NULL);
|
|
|
|
}
|
|
|
|
|