2021-02-01 03:58:50 +01:00
|
|
|
#include "config.h"
|
2019-12-04 23:02:54 +01:00
|
|
|
#include <assert.h>
|
2021-05-08 23:09:41 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2018-02-19 02:06:15 +01:00
|
|
|
#include <common/peer_failed.h>
|
2022-01-08 14:23:29 +01:00
|
|
|
#include <common/peer_io.h>
|
2019-06-03 20:11:25 +02:00
|
|
|
#include <common/per_peer_state.h>
|
2021-10-07 14:53:18 +02:00
|
|
|
#include <common/ping.h>
|
2018-01-31 03:53:35 +01:00
|
|
|
#include <common/read_peer_msg.h>
|
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/wire_error.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <wire/peer_wire.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
bool is_peer_error(const tal_t *ctx, const u8 *msg,
|
|
|
|
const struct channel_id *channel_id,
|
2021-02-02 13:47:01 +01:00
|
|
|
char **desc, bool *warning)
|
2018-08-02 08:49:55 +02:00
|
|
|
{
|
|
|
|
struct channel_id err_chanid;
|
|
|
|
|
2021-02-02 13:46:01 +01:00
|
|
|
if (fromwire_peektype(msg) == WIRE_ERROR)
|
|
|
|
*warning = false;
|
|
|
|
else if (fromwire_peektype(msg) == WIRE_WARNING)
|
|
|
|
*warning = true;
|
|
|
|
else
|
2018-08-02 08:49:55 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*desc = sanitize_error(ctx, msg, &err_chanid);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
* ...
|
|
|
|
* The receiving node:
|
2022-03-31 11:10:50 +02:00
|
|
|
* - upon receiving `error`:
|
|
|
|
* - if `channel_id` is all zero:
|
|
|
|
* - MUST fail all channels with the sending node.
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST fail the channel referred to by `channel_id`, if that channel is with the sending node.
|
|
|
|
* - upon receiving `warning`:
|
|
|
|
* - SHOULD log the message for later diagnosis.
|
|
|
|
* - MAY disconnect.
|
|
|
|
* - MAY reconnect after some delay to retry.
|
|
|
|
* - MAY attempt `shutdown` if permitted at this point.
|
|
|
|
* - if no existing channel is referred to by `channel_id`:
|
|
|
|
* - MUST ignore the message.
|
2018-08-02 08:49:55 +02:00
|
|
|
*/
|
2022-03-31 11:10:50 +02:00
|
|
|
|
|
|
|
/* FIXME: We don't actually free all channels at once, they'll
|
|
|
|
* have to error each in turn. */
|
|
|
|
if (!channel_id_is_all(&err_chanid)
|
|
|
|
&& !channel_id_eq(&err_chanid, channel_id))
|
2018-08-02 08:49:55 +02:00
|
|
|
*desc = tal_free(*desc);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_wrong_channel(const u8 *msg, const struct channel_id *expected,
|
|
|
|
struct channel_id *actual)
|
|
|
|
{
|
2021-12-28 00:24:09 +01:00
|
|
|
if (!expected)
|
|
|
|
return false;
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
if (!extract_channel_id(msg, actual))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !channel_id_eq(expected, actual);
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:33:05 +01:00
|
|
|
bool handle_peer_error(struct per_peer_state *pps,
|
|
|
|
const struct channel_id *channel_id,
|
|
|
|
const u8 *msg TAKES)
|
2018-08-02 08:49:55 +02:00
|
|
|
{
|
|
|
|
char *err;
|
2021-02-02 13:47:01 +01:00
|
|
|
bool warning;
|
|
|
|
if (is_peer_error(tmpctx, msg, channel_id, &err, &warning)) {
|
2018-08-02 08:49:55 +02:00
|
|
|
/* Ignore unknown channel errors. */
|
2022-01-29 04:33:05 +01:00
|
|
|
if (!err) {
|
|
|
|
if (taken(msg))
|
|
|
|
tal_free(msg);
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-02 13:47:01 +01:00
|
|
|
|
|
|
|
/* We hang up when a warning is received. */
|
2022-01-20 05:55:06 +01:00
|
|
|
peer_failed_received_errmsg(pps, err, channel_id, warning);
|
2018-08-02 08:49:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|