status: separate types for peer failure vs "impossible" failures.

Ideally we'd rename status_failed() to status_fatal(), but that's
too much churn for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-02-08 11:55:12 +10:30
committed by Christian Decker
parent fd498be7ca
commit cc9ca82821
12 changed files with 228 additions and 151 deletions

View File

@@ -15,31 +15,19 @@ void peer_failed(int peer_fd, struct crypto_state *cs,
const char *fmt, ...)
{
va_list ap;
const char *errmsg;
struct channel_id all_channels;
const char *desc;
u8 *msg;
/* BOLT #1:
*
* The channel is referred to by `channel_id` unless `channel_id` is
* zero (ie. all bytes zero), in which case it refers to all channels.
*/
if (!channel_id) {
memset(&all_channels, 0, sizeof(all_channels));
channel_id = &all_channels;
}
va_start(ap, fmt);
errmsg = tal_vfmt(NULL, fmt, ap);
va_start(ap, fmt);
desc = tal_vfmt(NULL, fmt, ap);
va_end(ap);
va_start(ap, fmt);
msg = towire_errorfmtv(errmsg, channel_id, fmt, ap);
va_end(ap);
status_broken("SENT ERROR:%s", desc);
msg = towire_errorfmt(desc, channel_id, "%s", desc);
/* This is only best-effort; don't block. */
io_fd_block(peer_fd, false);
sync_crypto_write(cs, peer_fd, take(msg));
sync_crypto_write(cs, peer_fd, msg);
status_failed(STATUS_FAIL_PEER_BAD, "%s", errmsg);
status_fatal_sent_errmsg(take(msg), desc, channel_id);
}

View File

@@ -41,10 +41,10 @@ u8 *read_peer_msg_(const tal_t *ctx,
int peer_fd, int gossip_fd,
struct crypto_state *cs,
const struct channel_id *channel,
bool (*send_reply)(struct crypto_state *, int, const u8 *,
void *),
bool (*send_reply)(struct crypto_state *cs, int fd,
const u8 *TAKES, void *arg),
void (*io_error)(const char *what_i_was_doing, void *arg),
void (*err_pkt)(const char *desc, bool this_channel_only,
void (*err_pkt)(const char *desc, const struct channel_id *,
void *arg),
void *arg)
{
@@ -85,10 +85,7 @@ u8 *read_peer_msg_(const tal_t *ctx,
* message:
* - MUST ignore the message.
*/
if (channel_id_is_all(&chanid))
err_pkt(err, false, arg);
else if (structeq(&chanid, channel))
err_pkt(err, true, arg);
err_pkt(err, &chanid, arg);
return tal_free(msg);
}
@@ -118,15 +115,15 @@ bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *msg,
return sync_crypto_write(cs, fd, msg);
}
/* Helper: calls status_failed(STATUS_FAIL_PEER_IO) */
/* Helper: calls status_fatal_connection_lost. */
void status_fail_io(const char *what_i_was_doing, void *unused)
{
status_failed(STATUS_FAIL_PEER_IO,
"%s:%s", what_i_was_doing, strerror(errno));
status_fatal_connection_lost();
}
/* Helper: calls status_failed(STATUS_FAIL_PEER_BAD, <error>) */
void status_fail_errpkt(const char *desc, bool this_channel_only, void *unused)
/* Helper: calls status_fatal_received_errmsg() */
void status_fail_errpkt(const char *desc, const struct channel_id *c,
void *unused)
{
status_failed(STATUS_FAIL_PEER_BAD, "Peer sent ERROR: %s", desc);
status_fatal_received_errmsg(desc, c);
}

View File

@@ -12,7 +12,7 @@ struct channel_id;
* read_peer_msg - read & decode in a peer message, handling common ones.
* @ctx: context to allocate return packet from.
* @cs: the cryptostate (updated)
* @channel_id: the channel id (for identifying errors)
* @chanid: the channel id (for identifying errors)
* @send_reply: the way to send a reply packet (eg. sync_crypto_write_arg)
* @io_error: what to do if there's an IO error (eg. status_fail_io)
* (MUST NOT RETURN!)
@@ -22,26 +22,29 @@ struct channel_id;
* This returns NULL if it handled the message, so it's normally called in
* a loop.
*/
#define read_peer_msg(ctx, cs, channel_id, send_reply, io_error, err_pkt, arg) \
read_peer_msg_((ctx), PEER_FD, GOSSIP_FD, (cs), (channel_id), \
#define read_peer_msg(ctx, cs, chanid, send_reply, io_error, err_pkt, arg) \
read_peer_msg_((ctx), PEER_FD, GOSSIP_FD, (cs), (chanid), \
typesafe_cb_preargs(bool, void *, (send_reply), (arg), \
struct crypto_state *, int, \
const u8 *), \
typesafe_cb_preargs(void, void *, (io_error), (arg), \
const char *), \
typesafe_cb_preargs(void, void *, (err_pkt), (arg), \
const char *, bool), \
const char *, \
const struct channel_id *), \
arg)
/* Helper: sync_crypto_write, with extra args it ignores */
bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *TAKES,
void *unused);
/* Helper: calls status_failed(STATUS_FAIL_PEER_IO) */
/* Helper: calls status_fatal_connection_lost. */
/* FIXME: Remove what_i_was_doing arg */
void status_fail_io(const char *what_i_was_doing, void *unused);
/* Helper: calls status_failed(STATUS_FAIL_PEER_BAD, <error>) */
void status_fail_errpkt(const char *desc, bool this_channel_only, void *unused);
/* Helper: calls status_fatal_received_errmsg() */
void status_fail_errpkt(const char *desc, const struct channel_id *c,
void *unused);
u8 *read_peer_msg_(const tal_t *ctx,
int peer_fd, int gossip_fd,
@@ -50,7 +53,7 @@ u8 *read_peer_msg_(const tal_t *ctx,
bool (*send_reply)(struct crypto_state *cs, int fd,
const u8 *TAKES, void *arg),
void (*io_error)(const char *what_i_was_doing, void *arg),
void (*err_pkt)(const char *desc, bool this_channel_only,
void (*err_pkt)(const char *desc, const struct channel_id *,
void *arg),
void *arg);

View File

@@ -112,6 +112,17 @@ void status_fmt(enum log_level level, const char *fmt, ...)
va_end(ap);
}
static NORETURN void flush_and_exit(int reason)
{
/* Don't let it take forever. */
alarm(10);
if (status_conn)
daemon_conn_sync_flush(status_conn);
exit(0x80 | (reason & 0xFF));
}
/* FIXME: rename to status_fatal, s/fail/fatal/ in status_failreason enums */
void status_failed(enum status_failreason reason, const char *fmt, ...)
{
va_list ap;
@@ -123,21 +134,46 @@ void status_failed(enum status_failreason reason, const char *fmt, ...)
status_send(take(towire_status_fail(NULL, reason, str)));
va_end(ap);
/* Don't let it take forever. */
alarm(10);
if (status_conn)
daemon_conn_sync_flush(status_conn);
exit(0x80 | (reason & 0xFF));
flush_and_exit(reason);
}
void master_badmsg(u32 type_expected, const u8 *msg)
{
if (!msg)
status_failed(STATUS_FAIL_MASTER_IO,
"failed reading msg %u: %s",
type_expected, strerror(errno));
"failed reading msg %u: %s",
type_expected, strerror(errno));
status_failed(STATUS_FAIL_MASTER_IO,
"Error parsing %u: %s",
type_expected, tal_hex(trc, msg));
"Error parsing %u: %s",
type_expected, tal_hex(trc, msg));
}
void status_fatal_connection_lost(void)
{
status_send(take(towire_status_peer_connection_lost(NULL)));
flush_and_exit(WIRE_STATUS_PEER_CONNECTION_LOST);
}
/* Got an error for one or all channels */
void status_fatal_received_errmsg(const char *desc, const struct channel_id *c)
{
static const struct channel_id all_channels;
if (!c)
c = &all_channels;
status_send(take(towire_status_received_errmsg(NULL, c, desc)));
flush_and_exit(WIRE_STATUS_RECEIVED_ERRMSG);
}
/* Sent an error for one or all channels */
void status_fatal_sent_errmsg(const u8 *errmsg,
const char *desc, const struct channel_id *c)
{
static const struct channel_id all_channels;
if (!c)
c = &all_channels;
status_send(take(towire_status_sent_errmsg(NULL, c, desc, errmsg)));
flush_and_exit(WIRE_STATUS_SENT_ERRMSG);
}

View File

@@ -9,6 +9,7 @@
#include <stdbool.h>
#include <stdlib.h>
struct channel_id;
struct daemon_conn;
/* Simple status reporting API. */
@@ -46,8 +47,20 @@ void status_io(enum log_level iodir, const u8 *p);
void status_failed(enum status_failreason code,
const char *fmt, ...) PRINTF_FMT(2,3) NORETURN;
/* Helper for master failures: sends STATUS_FAIL_MASTER_IO.
/* Helper for master failures: sends STATUS_FATAL_MASTER_IO.
* msg NULL == read failure. */
void master_badmsg(u32 type_expected, const u8 *msg);
void master_badmsg(u32 type_expected, const u8 *msg) NORETURN;
/* I/O error */
void status_fatal_connection_lost(void) NORETURN;
/* Got an error for one or all channels (if c == NULL) */
void status_fatal_received_errmsg(const char *desc,
const struct channel_id *c) NORETURN;
/* Sent an error for one or all channels (if c == NULL) */
void status_fatal_sent_errmsg(const u8 *errmsg,
const char *desc,
const struct channel_id *c) NORETURN;
#endif /* LIGHTNING_COMMON_STATUS_H */

View File

@@ -17,10 +17,10 @@ enum log_level {
};
#define LOG_LEVEL_MAX LOG_BROKEN
/*
* These errors shouldn't happen:
*/
enum status_failreason {
/*
* These errors shouldn't happen:
*/
/* Master daemon sent unknown/malformed command, or fd failed */
STATUS_FAIL_MASTER_IO,
@@ -32,16 +32,7 @@ enum status_failreason {
/* Other internal error. */
STATUS_FAIL_INTERNAL_ERROR,
/*
* These errors happen when the other peer misbehaves:
*/
/* I/O failure (probably they closed the socket) */
STATUS_FAIL_PEER_IO,
/* Peer did something else wrong */
STATUS_FAIL_PEER_BAD
};
#define STATUS_FAIL_MAX STATUS_FAIL_PEER_BAD
#define STATUS_FAIL_MAX STATUS_FAIL_INTERNAL_ERROR
#endif /* LIGHTNING_COMMON_STATUS_LEVELS_H */

View File

@@ -12,3 +12,19 @@ status_io,,data,len*u8
status_fail,0xFFF2
status_fail,,failreason,enum status_failreason
status_fail,,desc,wirestring
status_peer_connection_lost,0xFFF3
# They sent us this error.
status_received_errmsg,0xFFF4
status_received_errmsg,,channel,struct channel_id
status_received_errmsg,,desc,wirestring
# We sent them this error.
status_sent_errmsg,0xFFF5
status_sent_errmsg,,channel,struct channel_id
status_sent_errmsg,,desc,wirestring
status_sent_errmsg,,len,u16
status_sent_errmsg,,errmsg,len*u8
# Note: 0xFFFF is reserved for MSG_PASS_FD!
1 #include <common/status_wire.h>
12 status_peer_connection_lost,0xFFF3
13 # They sent us this error.
14 status_received_errmsg,0xFFF4
15 status_received_errmsg,,channel,struct channel_id
16 status_received_errmsg,,desc,wirestring
17 # We sent them this error.
18 status_sent_errmsg,0xFFF5
19 status_sent_errmsg,,channel,struct channel_id
20 status_sent_errmsg,,desc,wirestring
21 status_sent_errmsg,,len,u16
22 status_sent_errmsg,,errmsg,len*u8
23 # Note: 0xFFFF is reserved for MSG_PASS_FD!
24
25
26
27
28
29
30