2017-07-04 02:47:02 +02:00
|
|
|
#include <ccan/io/io.h>
|
2017-02-24 06:52:55 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <lightningd/crypto_sync.h>
|
|
|
|
#include <lightningd/peer_failed.h>
|
2017-03-19 21:24:12 +01:00
|
|
|
#include <lightningd/status.h>
|
2017-02-24 06:52:55 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
|
|
|
|
/* We only support one channel per peer anyway */
|
|
|
|
void peer_failed(int peer_fd, struct crypto_state *cs,
|
|
|
|
const struct channel_id *channel_id,
|
|
|
|
u16 error_code, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
const char *errmsg;
|
|
|
|
struct channel_id all_channels;
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The channel is referred to by `channel_id` unless `channel_id` is
|
2017-03-02 13:21:49 +01:00
|
|
|
* zero (ie. all bytes zero), in which case it refers to all channels.
|
2017-02-24 06:52:55 +01:00
|
|
|
*/
|
|
|
|
if (!channel_id) {
|
2017-03-02 13:21:49 +01:00
|
|
|
memset(&all_channels, 0, sizeof(all_channels));
|
2017-02-24 06:52:55 +01:00
|
|
|
channel_id = &all_channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
errmsg = tal_vfmt(NULL, fmt, ap);
|
|
|
|
va_end(ap);
|
2017-05-22 09:28:07 +02:00
|
|
|
/* Make sure it's correct length for error. */
|
|
|
|
tal_resize(&errmsg, strlen(errmsg)+1);
|
2017-02-24 06:52:55 +01:00
|
|
|
msg = towire_error(errmsg, channel_id, (const u8 *)errmsg);
|
|
|
|
|
|
|
|
/* This is only best-effort; don't block. */
|
2017-07-04 02:47:02 +02:00
|
|
|
io_fd_block(peer_fd, false);
|
2017-06-27 04:55:06 +02:00
|
|
|
sync_crypto_write(cs, peer_fd, take(msg));
|
2017-02-24 06:52:55 +01:00
|
|
|
|
|
|
|
status_failed(error_code, "%s", errmsg);
|
|
|
|
}
|