2018-01-31 03:53:35 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
|
|
|
#include <common/crypto_sync.h>
|
2018-02-19 02:06:15 +01:00
|
|
|
#include <common/peer_failed.h>
|
2018-01-31 03:53:35 +01:00
|
|
|
#include <common/ping.h>
|
|
|
|
#include <common/read_peer_msg.h>
|
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/wire_error.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <wire/peer_wire.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
|
|
|
static void handle_ping(const u8 *msg,
|
|
|
|
int peer_fd,
|
|
|
|
struct crypto_state *cs,
|
|
|
|
const struct channel_id *channel,
|
|
|
|
bool (*send_reply)(struct crypto_state *, int,
|
|
|
|
const u8 *, void *),
|
2018-02-19 02:06:15 +01:00
|
|
|
void (*io_error)(void *),
|
2018-01-31 03:53:35 +01:00
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
u8 *pong;
|
|
|
|
|
|
|
|
if (!check_ping_make_pong(msg, msg, &pong)) {
|
|
|
|
send_reply(cs, peer_fd,
|
|
|
|
take(towire_errorfmt(msg, channel,
|
|
|
|
"Bad ping %s",
|
|
|
|
tal_hex(msg, msg))), arg);
|
2018-02-19 02:06:15 +01:00
|
|
|
io_error(arg);
|
2018-01-31 03:53:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 05:09:28 +01:00
|
|
|
status_debug("Got ping, sending %s", pong ?
|
2018-01-31 03:53:35 +01:00
|
|
|
wire_type_name(fromwire_peektype(pong))
|
|
|
|
: "nothing");
|
|
|
|
|
|
|
|
if (pong && !send_reply(cs, peer_fd, pong, arg))
|
2018-02-19 02:06:15 +01:00
|
|
|
io_error(arg);
|
2018-01-31 03:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 *read_peer_msg_(const tal_t *ctx,
|
|
|
|
int peer_fd, int gossip_fd,
|
2018-02-19 02:06:15 +01:00
|
|
|
struct crypto_state *cs, u64 gossip_index,
|
2018-01-31 03:53:35 +01:00
|
|
|
const struct channel_id *channel,
|
2018-02-08 02:25:12 +01:00
|
|
|
bool (*send_reply)(struct crypto_state *cs, int fd,
|
|
|
|
const u8 *TAKES, void *arg),
|
2018-02-19 02:06:15 +01:00
|
|
|
void (*io_error)(void *arg),
|
|
|
|
void (*err_pkt)(int peer_fd, int gossip_fd,
|
|
|
|
struct crypto_state *cs, u64 gossip_index,
|
|
|
|
const char *desc,
|
|
|
|
const struct channel_id *channel_id,
|
2018-01-31 03:53:35 +01:00
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
struct channel_id chanid;
|
|
|
|
|
|
|
|
msg = sync_crypto_read(ctx, cs, peer_fd);
|
|
|
|
if (!msg)
|
2018-02-19 02:06:15 +01:00
|
|
|
io_error(arg);
|
2018-01-31 03:53:35 +01:00
|
|
|
|
|
|
|
if (is_gossip_msg(msg)) {
|
|
|
|
/* Forward to gossip daemon */
|
|
|
|
wire_sync_write(gossip_fd, take(msg));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromwire_peektype(msg) == WIRE_PING) {
|
|
|
|
handle_ping(msg, peer_fd, cs, channel,
|
|
|
|
send_reply, io_error, arg);
|
|
|
|
return tal_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromwire_peektype(msg) == WIRE_ERROR) {
|
|
|
|
char *err = sanitize_error(msg, msg, &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:
|
|
|
|
* - upon receiving `error`:
|
|
|
|
* - MUST fail the channel referred to by the error
|
|
|
|
* message.
|
|
|
|
* - if no existing channel is referred to by the
|
|
|
|
* message:
|
|
|
|
* - MUST ignore the message.
|
|
|
|
*/
|
2018-02-19 02:06:15 +01:00
|
|
|
if (structeq(&chanid, channel) || channel_id_is_all(&chanid))
|
2018-02-19 02:06:15 +01:00
|
|
|
err_pkt(peer_fd, gossip_fd, cs, gossip_index,
|
|
|
|
err, &chanid, arg);
|
2018-01-31 03:53:35 +01:00
|
|
|
|
|
|
|
return tal_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* They're talking about a different channel? */
|
|
|
|
if (extract_channel_id(msg, &chanid)
|
|
|
|
&& !structeq(&chanid, channel)) {
|
|
|
|
status_trace("Rejecting %s for unknown channel_id %s",
|
|
|
|
wire_type_name(fromwire_peektype(msg)),
|
|
|
|
type_to_string(msg, struct channel_id, &chanid));
|
|
|
|
if (!send_reply(cs, peer_fd,
|
|
|
|
take(towire_errorfmt(msg, &chanid,
|
|
|
|
"Multiple channels"
|
|
|
|
" unsupported")),
|
|
|
|
arg))
|
2018-02-19 02:06:15 +01:00
|
|
|
io_error(arg);
|
2018-01-31 03:53:35 +01:00
|
|
|
return tal_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper: sync_crypto_write, with extra args it ignores */
|
|
|
|
bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *msg,
|
|
|
|
void *unused)
|
|
|
|
{
|
|
|
|
return sync_crypto_write(cs, fd, msg);
|
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:15 +01:00
|
|
|
/* Helper: calls peer_failed_connection_lost. */
|
|
|
|
void status_fail_io(void *unused)
|
2018-01-31 03:53:35 +01:00
|
|
|
{
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed_connection_lost();
|
2018-01-31 03:53:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:15 +01:00
|
|
|
/* Helper: calls peer_failed_received_errmsg() */
|
|
|
|
void status_fail_errpkt(int peer_fd, int gossip_fd,
|
|
|
|
struct crypto_state *cs, u64 gossip_index,
|
|
|
|
const char *desc,
|
|
|
|
const struct channel_id *channel_id,
|
2018-02-08 02:25:12 +01:00
|
|
|
void *unused)
|
2018-01-31 03:53:35 +01:00
|
|
|
{
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed_received_errmsg(peer_fd, gossip_fd,
|
|
|
|
cs, gossip_index, desc, channel_id);
|
2018-01-31 03:53:35 +01:00
|
|
|
}
|