2018-01-31 03:53:35 +01:00
|
|
|
#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/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>
|
2018-04-26 06:51:01 +02:00
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
|
|
|
#include <sys/select.h>
|
2018-01-31 03:53:35 +01:00
|
|
|
#include <wire/peer_wire.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd,
|
|
|
|
struct crypto_state *cs,
|
|
|
|
bool (*send_msg)(struct crypto_state *cs, int fd,
|
|
|
|
const u8 *TAKES, void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
u8 *gossip;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_send_gossip(tmpctx, msg, &gossip)) {
|
|
|
|
status_broken("Got bad message from gossipd: %s",
|
|
|
|
tal_hex(msg, msg));
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-04-26 06:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Gossipd can send us gossip messages, OR errors */
|
|
|
|
if (is_msg_for_gossipd(gossip)) {
|
|
|
|
if (!send_msg(cs, peer_fd, gossip, arg))
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-04-26 06:51:01 +02:00
|
|
|
} else if (fromwire_peektype(gossip) == WIRE_ERROR) {
|
2018-04-26 06:51:02 +02:00
|
|
|
status_debug("Gossipd told us to send error");
|
2018-04-26 06:51:01 +02:00
|
|
|
send_msg(cs, peer_fd, gossip, arg);
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-04-26 06:51:01 +02:00
|
|
|
} else {
|
|
|
|
status_broken("Gossipd gave us bad send_gossip message %s",
|
|
|
|
tal_hex(msg, msg));
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-04-26 06:51:01 +02:00
|
|
|
}
|
|
|
|
if (taken(msg))
|
|
|
|
tal_free(msg);
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:53:35 +01:00
|
|
|
u8 *read_peer_msg_(const tal_t *ctx,
|
|
|
|
int peer_fd, int gossip_fd,
|
2018-04-26 06:51:01 +02:00
|
|
|
struct crypto_state *cs,
|
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-01-31 03:53:35 +01:00
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
struct channel_id chanid;
|
2018-04-26 06:51:01 +02:00
|
|
|
fd_set readfds;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(peer_fd, &readfds);
|
2018-07-24 08:18:59 +02:00
|
|
|
if (gossip_fd >= 0)
|
|
|
|
FD_SET(gossip_fd, &readfds);
|
2018-04-26 06:51:01 +02:00
|
|
|
|
|
|
|
select(peer_fd > gossip_fd ? peer_fd + 1 : gossip_fd + 1,
|
|
|
|
&readfds, NULL, NULL, NULL);
|
|
|
|
|
2018-07-24 08:18:59 +02:00
|
|
|
if (gossip_fd >= 0 && FD_ISSET(gossip_fd, &readfds)) {
|
2018-04-26 06:51:01 +02:00
|
|
|
msg = wire_sync_read(NULL, gossip_fd);
|
2018-07-24 08:18:59 +02:00
|
|
|
if (!msg)
|
|
|
|
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
|
|
"Error reading gossip msg: %s",
|
|
|
|
strerror(errno));
|
2018-04-26 06:51:01 +02:00
|
|
|
|
2018-06-22 01:55:57 +02:00
|
|
|
handle_gossip_msg_(msg, peer_fd, cs, send_reply, arg);
|
2018-04-26 06:51:01 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-31 03:53:35 +01:00
|
|
|
|
|
|
|
msg = sync_crypto_read(ctx, cs, peer_fd);
|
|
|
|
if (!msg)
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-01-31 03:53:35 +01:00
|
|
|
|
2018-03-08 04:17:05 +01:00
|
|
|
if (is_msg_for_gossipd(msg)) {
|
2018-01-31 03:53:35 +01:00
|
|
|
/* Forward to gossip daemon */
|
|
|
|
wire_sync_write(gossip_fd, take(msg));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-06-28 04:01:21 +02:00
|
|
|
* message, if that channel is with the sending node.
|
2018-01-31 03:53:35 +01:00
|
|
|
* - if no existing channel is referred to by the
|
|
|
|
* message:
|
|
|
|
* - MUST ignore the message.
|
|
|
|
*/
|
2018-07-04 07:30:02 +02:00
|
|
|
if (channel_id_eq(&chanid, channel)
|
|
|
|
|| channel_id_is_all(&chanid)) {
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed_received_errmsg(peer_fd, gossip_fd,
|
2018-04-26 06:51:01 +02:00
|
|
|
cs, err, &chanid);
|
2018-07-04 07:30:02 +02:00
|
|
|
}
|
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)
|
2018-07-04 07:30:02 +02:00
|
|
|
&& !channel_id_eq(&chanid, channel)) {
|
2018-01-31 03:53:35 +01:00
|
|
|
status_trace("Rejecting %s for unknown channel_id %s",
|
|
|
|
wire_type_name(fromwire_peektype(msg)),
|
2018-03-15 07:10:22 +01:00
|
|
|
type_to_string(tmpctx, struct channel_id, &chanid));
|
2018-01-31 03:53:35 +01:00
|
|
|
if (!send_reply(cs, peer_fd,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_errorfmt(NULL, &chanid,
|
2018-01-31 03:53:35 +01:00
|
|
|
"Multiple channels"
|
|
|
|
" unsupported")),
|
|
|
|
arg))
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
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,
|
2018-02-21 16:06:07 +01:00
|
|
|
void *unused UNUSED)
|
2018-01-31 03:53:35 +01:00
|
|
|
{
|
|
|
|
return sync_crypto_write(cs, fd, msg);
|
|
|
|
}
|