2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/container_of/container_of.h>
|
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/list/list.h>
|
|
|
|
#include <ccan/noerr/noerr.h>
|
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2017-03-07 02:08:20 +01:00
|
|
|
#include <ccan/take/take.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <ccan/timer/timer.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/daemon_conn.h>
|
|
|
|
#include <common/debug.h>
|
|
|
|
#include <common/ping.h>
|
|
|
|
#include <common/status.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:03:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/broadcast.h>
|
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
|
|
|
#include <gossipd/routing.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <inttypes.h>
|
2017-03-12 13:39:23 +01:00
|
|
|
#include <lightningd/gossip_msg.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
#include <sodium/randombytes.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
|
|
|
|
|
|
|
struct daemon {
|
|
|
|
struct list_head peers;
|
2017-03-19 21:28:29 +01:00
|
|
|
|
|
|
|
/* Connection to main daemon. */
|
|
|
|
struct daemon_conn master;
|
2017-02-01 15:49:01 +01:00
|
|
|
|
|
|
|
/* Routing information */
|
|
|
|
struct routing_state *rstate;
|
2017-02-04 16:28:35 +01:00
|
|
|
|
|
|
|
struct timers timers;
|
2017-04-24 14:31:26 +02:00
|
|
|
|
|
|
|
u32 broadcast_interval;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct peer {
|
|
|
|
struct daemon *daemon;
|
|
|
|
/* daemon->peers */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
u64 unique_id;
|
2017-02-24 06:52:56 +01:00
|
|
|
struct peer_crypto_state pcs;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
/* File descriptor corresponding to conn. */
|
|
|
|
int fd;
|
2017-02-01 15:49:01 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Our connection (and owner) */
|
|
|
|
struct io_conn *conn;
|
|
|
|
|
|
|
|
/* If this is non-NULL, it means we failed. */
|
|
|
|
const char *error;
|
2017-02-01 15:49:01 +01:00
|
|
|
|
|
|
|
/* High water mark for the staggered broadcast */
|
|
|
|
u64 broadcast_index;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
/* Message queue for outgoing. */
|
|
|
|
struct msg_queue peer_out;
|
|
|
|
|
2017-02-08 14:05:21 +01:00
|
|
|
/* Is it time to continue the staggered broadcast? */
|
|
|
|
bool gossip_sync;
|
2017-03-09 14:24:32 +01:00
|
|
|
|
|
|
|
/* The peer owner will use this to talk to gossipd */
|
2017-03-11 15:31:17 +01:00
|
|
|
struct daemon_conn owner_conn;
|
2017-03-10 13:06:51 +01:00
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
/* How many pongs are we expecting? */
|
|
|
|
size_t num_pings_outstanding;
|
|
|
|
|
2017-03-10 13:06:51 +01:00
|
|
|
/* Are we the owner of the peer? */
|
|
|
|
bool local;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
|
|
|
|
2017-03-11 14:45:54 +01:00
|
|
|
static void wake_pkt_out(struct peer *peer);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static void destroy_peer(struct peer *peer)
|
|
|
|
{
|
|
|
|
list_del_from(&peer->daemon->peers, &peer->list);
|
2017-03-19 21:28:34 +01:00
|
|
|
if (peer->error) {
|
|
|
|
u8 *msg = towire_gossipstatus_peer_bad_msg(peer,
|
|
|
|
peer->unique_id,
|
|
|
|
(u8 *)peer->error);
|
|
|
|
daemon_conn_send(&peer->daemon->master, take(msg));
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct peer *setup_new_peer(struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct peer *peer = tal(daemon, struct peer);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
init_peer_crypto_state(peer, &peer->pcs);
|
|
|
|
if (!fromwire_gossipctl_new_peer(msg, NULL, &peer->unique_id,
|
|
|
|
&peer->pcs.cs))
|
2017-01-10 06:08:33 +01:00
|
|
|
return tal_free(peer);
|
|
|
|
peer->daemon = daemon;
|
|
|
|
peer->error = NULL;
|
2017-03-10 13:06:51 +01:00
|
|
|
peer->local = true;
|
2017-04-12 20:20:48 +02:00
|
|
|
peer->num_pings_outstanding = 0;
|
2017-05-01 11:50:52 +02:00
|
|
|
peer->broadcast_index = 0;
|
2017-04-12 18:10:10 +02:00
|
|
|
msg_queue_init(&peer->peer_out, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
list_add_tail(&daemon->peers, &peer->list);
|
|
|
|
tal_add_destructor(peer, destroy_peer);
|
2017-03-11 14:45:54 +01:00
|
|
|
wake_pkt_out(peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2017-06-24 08:50:23 +02:00
|
|
|
static struct peer *setup_new_remote_peer(struct daemon *daemon,
|
|
|
|
u64 unique_id, bool sync)
|
|
|
|
{
|
|
|
|
struct peer *peer = tal(daemon, struct peer);
|
|
|
|
|
|
|
|
peer->daemon = daemon;
|
|
|
|
peer->error = NULL;
|
|
|
|
peer->local = false;
|
|
|
|
peer->num_pings_outstanding = 0;
|
|
|
|
peer->fd = -1;
|
|
|
|
peer->unique_id = unique_id;
|
|
|
|
if (sync)
|
|
|
|
peer->broadcast_index = 0;
|
|
|
|
else
|
|
|
|
peer->broadcast_index = daemon->rstate->broadcasts->next_index;
|
|
|
|
|
|
|
|
msg_queue_init(&peer->peer_out, peer);
|
|
|
|
list_add_tail(&daemon->peers, &peer->list);
|
|
|
|
tal_add_destructor(peer, destroy_peer);
|
2017-07-04 20:36:24 +02:00
|
|
|
wake_pkt_out(peer);
|
2017-06-24 08:50:23 +02:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:40 +01:00
|
|
|
static struct io_plan *owner_msg_in(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc);
|
|
|
|
static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc);
|
|
|
|
|
|
|
|
/* When a peer is to be owned by another daemon, we create a socket
|
|
|
|
* pair to send/receive gossip from it */
|
|
|
|
static void send_peer_with_fds(struct peer *peer, const u8 *msg)
|
|
|
|
{
|
|
|
|
int fds[2];
|
|
|
|
u8 *out;
|
|
|
|
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
|
|
|
|
out = towire_gossipstatus_peer_failed(msg,
|
|
|
|
peer->unique_id,
|
|
|
|
(u8 *)tal_fmt(msg,
|
|
|
|
"Failed to create socketpair: %s",
|
|
|
|
strerror(errno)));
|
|
|
|
daemon_conn_send(&peer->daemon->master, take(out));
|
|
|
|
|
|
|
|
/* FIXME: Send error to peer? */
|
|
|
|
/* Peer will be freed when caller closes conn. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we talk to socket to get to peer's owner daemon. */
|
|
|
|
peer->local = false;
|
2017-05-23 14:17:19 +02:00
|
|
|
/* FIXME: Forget peer if other end is closed. */
|
2017-06-06 05:08:42 +02:00
|
|
|
daemon_conn_init(peer, &peer->owner_conn, fds[0], owner_msg_in, NULL);
|
2017-03-19 21:32:40 +01:00
|
|
|
peer->owner_conn.msg_queue_cleared_cb = nonlocal_dump_gossip;
|
|
|
|
|
|
|
|
/* Peer stays around, even though we're going to free conn. */
|
|
|
|
tal_steal(peer->daemon, peer);
|
|
|
|
|
|
|
|
daemon_conn_send(&peer->daemon->master, msg);
|
|
|
|
daemon_conn_send_fd(&peer->daemon->master, peer->fd);
|
|
|
|
daemon_conn_send_fd(&peer->daemon->master, fds[1]);
|
|
|
|
|
|
|
|
/* Don't get confused: we can't use this any more. */
|
|
|
|
peer->fd = -1;
|
|
|
|
}
|
|
|
|
|
2017-03-11 14:45:54 +01:00
|
|
|
static void handle_gossip_msg(struct routing_state *rstate, u8 *msg)
|
|
|
|
{
|
|
|
|
int t = fromwire_peektype(msg);
|
|
|
|
switch(t) {
|
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT:
|
|
|
|
handle_channel_announcement(rstate, msg, tal_count(msg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
|
|
|
handle_node_announcement(rstate, msg, tal_count(msg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIRE_CHANNEL_UPDATE:
|
|
|
|
handle_channel_update(rstate, msg, tal_count(msg));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
static bool handle_ping(struct peer *peer, u8 *ping)
|
|
|
|
{
|
|
|
|
u8 *pong;
|
|
|
|
|
|
|
|
if (!check_ping_make_pong(peer, ping, &pong)) {
|
|
|
|
peer->error = "Bad ping";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pong)
|
|
|
|
msg_enqueue(&peer->peer_out, take(pong));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
static bool handle_pong(struct peer *peer, const u8 *pong)
|
|
|
|
{
|
|
|
|
u8 *ignored;
|
|
|
|
|
|
|
|
status_trace("Got pong!");
|
|
|
|
if (!fromwire_pong(pong, pong, NULL, &ignored)) {
|
|
|
|
peer->error = "pad pong";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->num_pings_outstanding) {
|
|
|
|
peer->error = "unexpected pong";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->num_pings_outstanding--;
|
|
|
|
daemon_conn_send(&peer->daemon->master,
|
|
|
|
take(towire_gossip_ping_reply(pong, tal_len(pong))));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *peer_msgin(struct io_conn *conn,
|
|
|
|
struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
u8 *s;
|
|
|
|
enum wire_type t = fromwire_peektype(msg);
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_ERROR:
|
|
|
|
/* FIXME: Report error from msg. */
|
|
|
|
peer->error = "ERROR message received";
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT:
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
|
|
|
case WIRE_CHANNEL_UPDATE:
|
2017-03-11 14:45:54 +01:00
|
|
|
handle_gossip_msg(peer->daemon->rstate, msg);
|
2017-02-24 06:52:56 +01:00
|
|
|
return peer_read_message(conn, &peer->pcs, peer_msgin);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
case WIRE_PING:
|
|
|
|
if (!handle_ping(peer, msg))
|
|
|
|
return io_close(conn);
|
|
|
|
return peer_read_message(conn, &peer->pcs, peer_msgin);
|
|
|
|
|
|
|
|
case WIRE_PONG:
|
2017-04-12 20:20:48 +02:00
|
|
|
if (!handle_pong(peer, msg))
|
|
|
|
return io_close(conn);
|
|
|
|
return peer_read_message(conn, &peer->pcs, peer_msgin);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_OPEN_CHANNEL:
|
2017-06-20 07:40:03 +02:00
|
|
|
case WIRE_CHANNEL_REESTABLISH:
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_ACCEPT_CHANNEL:
|
|
|
|
case WIRE_FUNDING_CREATED:
|
|
|
|
case WIRE_FUNDING_SIGNED:
|
|
|
|
case WIRE_FUNDING_LOCKED:
|
2017-02-07 02:44:14 +01:00
|
|
|
case WIRE_ANNOUNCEMENT_SIGNATURES:
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_UPDATE_FEE:
|
|
|
|
case WIRE_SHUTDOWN:
|
|
|
|
case WIRE_CLOSING_SIGNED:
|
|
|
|
case WIRE_UPDATE_ADD_HTLC:
|
|
|
|
case WIRE_UPDATE_FULFILL_HTLC:
|
|
|
|
case WIRE_UPDATE_FAIL_HTLC:
|
|
|
|
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
|
2017-03-29 12:53:15 +02:00
|
|
|
case WIRE_COMMITMENT_SIGNED:
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_REVOKE_AND_ACK:
|
2017-05-22 13:26:51 +02:00
|
|
|
case WIRE_INIT:
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Not our place to handle this, so we punt */
|
|
|
|
s = towire_gossipstatus_peer_nongossip(msg, peer->unique_id,
|
2017-02-24 06:52:56 +01:00
|
|
|
&peer->pcs.cs, msg);
|
2017-03-19 21:32:40 +01:00
|
|
|
send_peer_with_fds(peer, take(s));
|
2017-03-19 21:28:34 +01:00
|
|
|
return io_close_taken_fd(conn);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The type follows the _it's ok to be odd_ rule, so nodes MAY send
|
|
|
|
* odd-numbered types without ascertaining that the recipient
|
|
|
|
* understands it. */
|
|
|
|
if (t & 1) {
|
|
|
|
status_trace("Peer %"PRIu64" sent unknown packet %u, ignoring",
|
|
|
|
peer->unique_id, t);
|
2017-02-24 06:52:56 +01:00
|
|
|
return peer_read_message(conn, &peer->pcs, peer_msgin);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
peer->error = tal_fmt(peer, "Unknown packet %u", t);
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:10:21 +01:00
|
|
|
/* Wake up the outgoing direction of the connection and write any
|
|
|
|
* queued messages. Needed since the `io_wake` method signature does
|
2017-02-08 14:05:21 +01:00
|
|
|
* not allow us to specify it as the callback for `new_reltimer`, but
|
|
|
|
* it allows us to set an additional flag for the routing dump..
|
2017-02-06 19:10:21 +01:00
|
|
|
*/
|
|
|
|
static void wake_pkt_out(struct peer *peer)
|
|
|
|
{
|
2017-02-08 14:05:21 +01:00
|
|
|
peer->gossip_sync = true;
|
2017-04-24 14:31:26 +02:00
|
|
|
new_reltimer(&peer->daemon->timers, peer,
|
|
|
|
time_from_msec(peer->daemon->broadcast_interval),
|
2017-03-11 14:45:54 +01:00
|
|
|
wake_pkt_out, peer);
|
2017-03-11 15:31:17 +01:00
|
|
|
/* Notify the peer-write loop */
|
2017-04-12 18:10:10 +02:00
|
|
|
msg_wake(&peer->peer_out);
|
2017-03-11 15:31:17 +01:00
|
|
|
/* Notify the daemon_conn-write loop */
|
2017-04-12 18:10:10 +02:00
|
|
|
msg_wake(&peer->owner_conn.out);
|
2017-02-06 19:10:21 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
static struct io_plan *peer_pkt_out(struct io_conn *conn, struct peer *peer)
|
2017-02-01 15:49:01 +01:00
|
|
|
{
|
2017-04-12 18:10:10 +02:00
|
|
|
/* First priority is queued packets, if any */
|
2017-04-12 18:10:10 +02:00
|
|
|
const u8 *out = msg_dequeue(&peer->peer_out);
|
|
|
|
if (out)
|
2017-04-12 18:10:10 +02:00
|
|
|
return peer_write_message(conn, &peer->pcs, take(out),
|
|
|
|
peer_pkt_out);
|
|
|
|
|
|
|
|
/* If we're supposed to be sending gossip, do so now. */
|
|
|
|
if (peer->gossip_sync) {
|
|
|
|
struct queued_message *next;
|
2017-02-06 19:10:21 +01:00
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
next = next_broadcast_message(peer->daemon->rstate->broadcasts,
|
|
|
|
&peer->broadcast_index);
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
return peer_write_message(conn, &peer->pcs,
|
|
|
|
next->payload, peer_pkt_out);
|
|
|
|
|
|
|
|
/* Gossip is drained. Wait for next timer. */
|
2017-02-08 14:05:21 +01:00
|
|
|
peer->gossip_sync = false;
|
|
|
|
}
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
return msg_queue_wait(conn, &peer->peer_out, peer_pkt_out, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
/**
|
|
|
|
* owner_msg_in - Called by the `peer->owner_conn` upon receiving a
|
|
|
|
* message
|
|
|
|
*/
|
|
|
|
static struct io_plan *owner_msg_in(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
2017-03-09 16:56:04 +01:00
|
|
|
{
|
2017-03-11 15:31:17 +01:00
|
|
|
struct peer *peer = container_of(dc, struct peer, owner_conn);
|
|
|
|
u8 *msg = dc->msg_in;
|
2017-03-09 16:56:04 +01:00
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
int type = fromwire_peektype(msg);
|
|
|
|
if (type == WIRE_CHANNEL_ANNOUNCEMENT || type == WIRE_CHANNEL_UPDATE ||
|
|
|
|
type == WIRE_NODE_ANNOUNCEMENT) {
|
|
|
|
handle_gossip_msg(peer->daemon->rstate, dc->msg_in);
|
|
|
|
}
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
2017-03-09 16:56:04 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
/**
|
|
|
|
* nonlocal_dump_gossip - catch the nonlocal peer up with the latest gossip.
|
|
|
|
*
|
|
|
|
* Registered as `msg_queue_cleared_cb` by the `peer->owner_conn`.
|
|
|
|
*/
|
|
|
|
static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc)
|
2017-03-10 13:06:51 +01:00
|
|
|
{
|
|
|
|
struct queued_message *next;
|
2017-03-11 15:31:17 +01:00
|
|
|
struct peer *peer = container_of(dc, struct peer, owner_conn);
|
|
|
|
|
|
|
|
|
|
|
|
/* Make sure we are not connected directly */
|
|
|
|
if (peer->local)
|
2017-05-01 11:50:52 +02:00
|
|
|
return msg_queue_wait(conn, &peer->owner_conn.out,
|
|
|
|
daemon_conn_write_next, dc);
|
2017-03-11 15:31:17 +01:00
|
|
|
|
2017-03-10 13:06:51 +01:00
|
|
|
next = next_broadcast_message(peer->daemon->rstate->broadcasts,
|
|
|
|
&peer->broadcast_index);
|
|
|
|
|
|
|
|
if (!next) {
|
2017-05-01 11:50:52 +02:00
|
|
|
return msg_queue_wait(conn, &peer->owner_conn.out,
|
|
|
|
daemon_conn_write_next, dc);
|
2017-03-10 13:06:51 +01:00
|
|
|
} else {
|
2017-03-11 15:31:17 +01:00
|
|
|
return io_write_wire(conn, next->payload, nonlocal_dump_gossip, dc);
|
2017-03-10 13:06:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:26:51 +02:00
|
|
|
static struct io_plan *peer_start_gossip(struct io_conn *conn, struct peer *peer)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-02-01 15:49:01 +01:00
|
|
|
/* Need to go duplex here, otherwise backpressure would mean
|
|
|
|
* we both wait indefinitely */
|
|
|
|
return io_duplex(conn,
|
2017-02-24 06:52:56 +01:00
|
|
|
peer_read_message(conn, &peer->pcs, peer_msgin),
|
2017-04-12 18:10:10 +02:00
|
|
|
peer_pkt_out(conn, peer));
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *new_peer_got_fd(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-05-22 13:26:51 +02:00
|
|
|
peer->conn = io_new_conn(conn, peer->fd, peer_start_gossip, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
if (!peer->conn) {
|
|
|
|
peer->error = "Could not create connection";
|
|
|
|
tal_free(peer);
|
2017-05-23 13:01:17 +02:00
|
|
|
} else {
|
|
|
|
/* If conn dies, we forget peer. */
|
|
|
|
tal_steal(peer->conn, peer);
|
2017-03-09 11:35:52 +01:00
|
|
|
}
|
2017-03-19 21:28:29 +01:00
|
|
|
return daemon_conn_read_next(conn,&peer->daemon->master);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *new_peer(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct peer *peer = setup_new_peer(daemon, msg);
|
|
|
|
if (!peer)
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_NEW_PEER_REQUEST,
|
|
|
|
"%s", tal_hex(trc, msg));
|
|
|
|
return io_recv_fd(conn, &peer->fd, new_peer_got_fd, peer);
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
static struct peer *find_peer(struct daemon *daemon, u64 unique_id)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
list_for_each(&daemon->peers, peer, list)
|
|
|
|
if (peer->unique_id == unique_id)
|
|
|
|
return peer;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
u64 unique_id;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossipctl_release_peer(msg, NULL, &unique_id))
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_RELEASE_REQUEST,
|
|
|
|
"%s", tal_hex(trc, msg));
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
peer = find_peer(daemon, unique_id);
|
2017-08-14 22:44:44 +02:00
|
|
|
if (!peer || !peer->local) {
|
2017-05-23 14:17:34 +02:00
|
|
|
/* This can happen with a reconnect vs connect race.
|
2017-08-14 22:44:44 +02:00
|
|
|
* See gossip_peer_released in master daemon. It may
|
|
|
|
* also happen if we asked to release just before
|
|
|
|
* failing the peer*/
|
2017-05-23 14:17:34 +02:00
|
|
|
daemon_conn_send(&daemon->master,
|
2017-06-24 08:25:53 +02:00
|
|
|
take(towire_gossipctl_release_peer_replyfail(msg)));
|
2017-05-23 14:17:34 +02:00
|
|
|
} else {
|
|
|
|
send_peer_with_fds(peer,
|
|
|
|
take(towire_gossipctl_release_peer_reply(msg,
|
2017-03-19 21:32:40 +01:00
|
|
|
&peer->pcs.cs)));
|
2017-05-23 14:17:34 +02:00
|
|
|
io_close_taken_fd(peer->conn);
|
|
|
|
}
|
2017-04-12 20:20:48 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-05-23 13:02:17 +02:00
|
|
|
static struct io_plan *fail_peer(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
u64 unique_id;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossipctl_fail_peer(msg, NULL, &unique_id))
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_FAIL_REQUEST,
|
|
|
|
"%s", tal_hex(trc, msg));
|
|
|
|
|
|
|
|
/* This may not find the peer, if we fail beforehand. */
|
|
|
|
peer = find_peer(daemon, unique_id);
|
|
|
|
if (!peer)
|
|
|
|
status_trace("Unknown fail_peer %"PRIu64, unique_id);
|
2017-08-14 22:44:44 +02:00
|
|
|
else if (peer->local) {
|
2017-05-23 13:02:17 +02:00
|
|
|
status_trace("fail_peer %"PRIu64, unique_id);
|
|
|
|
/* This owns the peer, so we can free it */
|
|
|
|
io_close(peer->conn);
|
2017-08-14 22:44:44 +02:00
|
|
|
} else {
|
|
|
|
status_trace("Could not fail_peer %"PRIu64", it's not local",
|
|
|
|
unique_id);
|
2017-05-23 13:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-06-24 08:50:23 +02:00
|
|
|
static void forget_peer(struct io_conn *conn, struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
/* Free peer. */
|
|
|
|
tal_free(dc->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *new_peer_fd(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
int fds[2];
|
|
|
|
u8 *out;
|
|
|
|
u64 unique_id;
|
|
|
|
bool sync;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossipctl_get_peer_gossipfd(msg, NULL,
|
|
|
|
&unique_id, &sync))
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_FAIL_REQUEST,
|
|
|
|
"%s", tal_hex(trc, msg));
|
|
|
|
|
|
|
|
peer = setup_new_remote_peer(daemon, unique_id, sync);
|
|
|
|
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
|
|
|
|
status_trace("Failed to create socketpair: %s",
|
|
|
|
strerror(errno));
|
|
|
|
out = towire_gossipctl_get_peer_gossipfd_replyfail(msg);
|
|
|
|
daemon_conn_send(&peer->daemon->master, take(out));
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_init(peer, &peer->owner_conn, fds[0], owner_msg_in,
|
|
|
|
forget_peer);
|
|
|
|
peer->owner_conn.msg_queue_cleared_cb = nonlocal_dump_gossip;
|
|
|
|
|
|
|
|
out = towire_gossipctl_get_peer_gossipfd_reply(msg);
|
|
|
|
daemon_conn_send(&peer->daemon->master, out);
|
|
|
|
daemon_conn_send_fd(&peer->daemon->master, fds[1]);
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-03-15 13:46:29 +01:00
|
|
|
static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
u8 *msg)
|
2017-03-15 11:36:52 +01:00
|
|
|
{
|
2017-03-15 13:46:29 +01:00
|
|
|
tal_t *tmpctx = tal_tmpctx(msg);
|
|
|
|
struct pubkey source, destination;
|
|
|
|
u32 msatoshi;
|
|
|
|
u16 riskfactor;
|
|
|
|
u8 *out;
|
|
|
|
struct route_hop *hops;
|
|
|
|
|
|
|
|
fromwire_gossip_getroute_request(msg, NULL, &source, &destination,
|
|
|
|
&msatoshi, &riskfactor);
|
|
|
|
status_trace("Trying to find a route from %s to %s for %d msatoshi",
|
|
|
|
pubkey_to_hexstr(tmpctx, &source),
|
|
|
|
pubkey_to_hexstr(tmpctx, &destination), msatoshi);
|
|
|
|
|
|
|
|
hops = get_route(tmpctx, daemon->rstate, &source, &destination,
|
|
|
|
msatoshi, 1);
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-15 13:46:29 +01:00
|
|
|
out = towire_gossip_getroute_reply(msg, hops);
|
|
|
|
tal_free(tmpctx);
|
|
|
|
daemon_conn_send(&daemon->master, out);
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-22 13:30:09 +01:00
|
|
|
static struct io_plan *getchannels_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
u8 *msg)
|
|
|
|
{
|
|
|
|
tal_t *tmpctx = tal_tmpctx(daemon);
|
|
|
|
u8 *out;
|
|
|
|
size_t j, num_chans = 0;
|
|
|
|
struct gossip_getchannels_entry *entries;
|
|
|
|
struct node *n;
|
|
|
|
struct node_map_iter i;
|
|
|
|
|
|
|
|
entries = tal_arr(tmpctx, struct gossip_getchannels_entry, num_chans);
|
|
|
|
n = node_map_first(daemon->rstate->nodes, &i);
|
|
|
|
while (n != NULL) {
|
|
|
|
for (j=0; j<tal_count(n->out); j++){
|
|
|
|
tal_resize(&entries, num_chans + 1);
|
|
|
|
entries[num_chans].source = n->out[j]->src->id;
|
|
|
|
entries[num_chans].destination = n->out[j]->dst->id;
|
|
|
|
entries[num_chans].active = n->out[j]->active;
|
|
|
|
entries[num_chans].delay = n->out[j]->delay;
|
|
|
|
entries[num_chans].fee_per_kw = n->out[j]->proportional_fee;
|
|
|
|
entries[num_chans].last_update_timestamp = n->out[j]->last_timestamp;
|
|
|
|
entries[num_chans].flags = n->out[j]->flags;
|
|
|
|
entries[num_chans].short_channel_id = n->out[j]->short_channel_id;
|
|
|
|
num_chans++;
|
|
|
|
}
|
|
|
|
n = node_map_next(daemon->rstate->nodes, &i);
|
|
|
|
}
|
|
|
|
|
|
|
|
out = towire_gossip_getchannels_reply(daemon, entries);
|
|
|
|
daemon_conn_send(&daemon->master, take(out));
|
|
|
|
tal_free(tmpctx);
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:39:23 +01:00
|
|
|
static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon)
|
|
|
|
{
|
|
|
|
tal_t *tmpctx = tal_tmpctx(daemon);
|
2017-03-16 05:05:26 +01:00
|
|
|
u8 *out;
|
2017-03-12 13:39:23 +01:00
|
|
|
struct node *n;
|
|
|
|
struct node_map_iter i;
|
2017-03-16 05:05:26 +01:00
|
|
|
struct gossip_getnodes_entry *nodes;
|
|
|
|
size_t node_count = 0;
|
2017-03-12 13:39:23 +01:00
|
|
|
|
2017-03-16 05:05:26 +01:00
|
|
|
nodes = tal_arr(tmpctx, struct gossip_getnodes_entry, node_count);
|
2017-03-12 13:39:23 +01:00
|
|
|
n = node_map_first(daemon->rstate->nodes, &i);
|
|
|
|
while (n != NULL) {
|
2017-03-16 05:05:26 +01:00
|
|
|
tal_resize(&nodes, node_count + 1);
|
|
|
|
nodes[node_count].nodeid = n->id;
|
2017-05-08 23:22:59 +02:00
|
|
|
nodes[node_count].addresses = n->addresses;
|
2017-03-16 05:05:26 +01:00
|
|
|
node_count++;
|
2017-03-12 13:39:23 +01:00
|
|
|
n = node_map_next(daemon->rstate->nodes, &i);
|
|
|
|
}
|
2017-03-16 05:05:26 +01:00
|
|
|
out = towire_gossip_getnodes_reply(daemon, nodes);
|
2017-03-19 21:28:29 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(out));
|
2017-03-12 13:39:23 +01:00
|
|
|
tal_free(tmpctx);
|
2017-03-19 21:28:29 +01:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-03-12 13:39:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
static struct io_plan *ping_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
u64 unique_id;
|
|
|
|
u16 num_pong_bytes, len;
|
|
|
|
struct peer *peer;
|
|
|
|
u8 *ping;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_ping(msg, NULL, &unique_id, &num_pong_bytes, &len))
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_REQUEST,
|
|
|
|
"%s", tal_hex(trc, msg));
|
|
|
|
|
|
|
|
peer = find_peer(daemon, unique_id);
|
|
|
|
if (!peer)
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_REQUEST,
|
|
|
|
"Unknown peer %"PRIu64, unique_id);
|
|
|
|
|
|
|
|
ping = make_ping(peer, num_pong_bytes, len);
|
|
|
|
if (tal_len(ping) > 65535)
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_REQUEST, "Oversize ping");
|
|
|
|
|
|
|
|
msg_enqueue(&peer->peer_out, take(ping));
|
|
|
|
status_trace("sending ping expecting %sresponse",
|
|
|
|
num_pong_bytes >= 65532 ? "no " : "");
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* if `num_pong_bytes` is less than 65532 it MUST respond by sending a
|
|
|
|
* `pong` message with `byteslen` equal to `num_pong_bytes`, otherwise
|
|
|
|
* it MUST ignore the `ping`.
|
|
|
|
*/
|
|
|
|
if (num_pong_bytes >= 65532)
|
|
|
|
daemon_conn_send(&daemon->master,
|
|
|
|
take(towire_gossip_ping_reply(peer, 0)));
|
|
|
|
else
|
|
|
|
peer->num_pings_outstanding++;
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-04-24 14:31:26 +02:00
|
|
|
/* Parse an incoming gossip init message and assign config variables
|
|
|
|
* to the daemon.
|
|
|
|
*/
|
|
|
|
static struct io_plan *gossip_init(struct daemon_conn *master,
|
|
|
|
struct daemon *daemon, u8 *msg)
|
|
|
|
{
|
2017-08-22 07:06:37 +02:00
|
|
|
struct sha256_double chain_hash;
|
|
|
|
|
|
|
|
if (!fromwire_gossipctl_init(msg, NULL, &daemon->broadcast_interval,
|
|
|
|
&chain_hash)) {
|
2017-04-24 14:31:26 +02:00
|
|
|
status_failed(WIRE_GOSSIPSTATUS_INIT_FAILED,
|
|
|
|
"Unable to parse init message");
|
|
|
|
}
|
2017-08-28 18:03:01 +02:00
|
|
|
daemon->rstate = new_routing_state(daemon, &chain_hash);
|
2017-04-24 14:31:26 +02:00
|
|
|
return daemon_conn_read_next(master->conn, master);
|
|
|
|
}
|
|
|
|
|
2017-04-30 23:49:15 +02:00
|
|
|
static struct io_plan *resolve_channel_req(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
|
|
|
struct node_connection *nc;
|
2017-05-05 08:41:44 +02:00
|
|
|
struct pubkey *keys;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_resolve_channel_request(msg, NULL, &scid))
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_REQUEST,
|
|
|
|
"Unable to parse resolver request");
|
|
|
|
|
|
|
|
nc = get_connection_by_scid(daemon->rstate, &scid, 0);
|
|
|
|
if (!nc) {
|
2017-06-06 01:47:10 +02:00
|
|
|
status_trace("Failed to resolve channel %s",
|
|
|
|
type_to_string(trc, struct short_channel_id, &scid));
|
2017-05-05 08:41:44 +02:00
|
|
|
keys = NULL;
|
2017-04-30 23:49:15 +02:00
|
|
|
} else {
|
2017-05-05 08:41:44 +02:00
|
|
|
keys = tal_arr(msg, struct pubkey, 2);
|
|
|
|
keys[0] = nc->src->id;
|
|
|
|
keys[1] = nc->dst->id;
|
2017-06-06 01:47:10 +02:00
|
|
|
status_trace("Resolved channel %s %s<->%s",
|
|
|
|
type_to_string(trc, struct short_channel_id, &scid),
|
|
|
|
type_to_string(trc, struct pubkey, &keys[0]),
|
|
|
|
type_to_string(trc, struct pubkey, &keys[1]));
|
2017-04-30 23:49:15 +02:00
|
|
|
}
|
2017-05-05 08:41:44 +02:00
|
|
|
daemon_conn_send(&daemon->master,
|
|
|
|
take(towire_gossip_resolve_channel_reply(msg, keys)));
|
2017-04-30 23:49:15 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-05-07 01:59:48 +02:00
|
|
|
static void handle_forwarded_msg(struct io_conn *conn, struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
u8 *payload;
|
|
|
|
if (!fromwire_gossip_forwarded_msg(msg, msg, NULL, &payload)) {
|
|
|
|
status_trace("Malformed forwarded message: %s", tal_hex(trc, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handle_gossip_msg(daemon->rstate, payload);
|
|
|
|
}
|
2017-03-19 21:28:29 +01:00
|
|
|
static struct io_plan *recv_req(struct io_conn *conn, struct daemon_conn *master)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-03-19 21:28:29 +01:00
|
|
|
struct daemon *daemon = container_of(master, struct daemon, master);
|
|
|
|
enum gossip_wire_type t = fromwire_peektype(master->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
status_trace("req: type %s len %zu",
|
2017-03-19 21:28:29 +01:00
|
|
|
gossip_wire_type_name(t), tal_count(master->msg_in));
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
switch (t) {
|
2017-04-24 14:31:26 +02:00
|
|
|
case WIRE_GOSSIPCTL_INIT:
|
|
|
|
return gossip_init(master, daemon, master->msg_in);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_GOSSIPCTL_NEW_PEER:
|
2017-03-19 21:28:29 +01:00
|
|
|
return new_peer(conn, daemon, master->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER:
|
2017-03-19 21:28:29 +01:00
|
|
|
return release_peer(conn, daemon, master->msg_in);
|
2017-05-23 13:02:17 +02:00
|
|
|
case WIRE_GOSSIPCTL_FAIL_PEER:
|
|
|
|
return fail_peer(conn, daemon, master->msg_in);
|
2017-06-24 08:50:23 +02:00
|
|
|
case WIRE_GOSSIPCTL_GET_PEER_GOSSIPFD:
|
|
|
|
return new_peer_fd(conn, daemon, master->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REQUEST:
|
|
|
|
return getnodes(conn, daemon);
|
|
|
|
|
2017-03-15 11:36:52 +01:00
|
|
|
case WIRE_GOSSIP_GETROUTE_REQUEST:
|
2017-03-15 13:46:29 +01:00
|
|
|
return getroute_req(conn, daemon, daemon->master.msg_in);
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-22 13:30:09 +01:00
|
|
|
case WIRE_GOSSIP_GETCHANNELS_REQUEST:
|
|
|
|
return getchannels_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
case WIRE_GOSSIP_PING:
|
|
|
|
return ping_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-04-30 23:49:15 +02:00
|
|
|
case WIRE_GOSSIP_RESOLVE_CHANNEL_REQUEST:
|
|
|
|
return resolve_channel_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-05-07 01:59:48 +02:00
|
|
|
case WIRE_GOSSIP_FORWARDED_MSG:
|
|
|
|
handle_forwarded_msg(conn, daemon, daemon->master.msg_in);
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-03-10 11:50:43 +01:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY:
|
2017-06-24 08:25:53 +02:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER_REPLYFAIL:
|
2017-06-24 08:50:23 +02:00
|
|
|
case WIRE_GOSSIPCTL_GET_PEER_GOSSIPFD_REPLY:
|
|
|
|
case WIRE_GOSSIPCTL_GET_PEER_GOSSIPFD_REPLYFAIL:
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REPLY:
|
2017-03-15 11:36:52 +01:00
|
|
|
case WIRE_GOSSIP_GETROUTE_REPLY:
|
2017-03-22 13:30:09 +01:00
|
|
|
case WIRE_GOSSIP_GETCHANNELS_REPLY:
|
2017-04-12 20:20:48 +02:00
|
|
|
case WIRE_GOSSIP_PING_REPLY:
|
2017-04-30 23:49:15 +02:00
|
|
|
case WIRE_GOSSIP_RESOLVE_CHANNEL_REPLY:
|
2017-03-10 11:50:43 +01:00
|
|
|
case WIRE_GOSSIPSTATUS_INIT_FAILED:
|
|
|
|
case WIRE_GOSSIPSTATUS_BAD_NEW_PEER_REQUEST:
|
|
|
|
case WIRE_GOSSIPSTATUS_BAD_RELEASE_REQUEST:
|
2017-05-23 13:02:17 +02:00
|
|
|
case WIRE_GOSSIPSTATUS_BAD_FAIL_REQUEST:
|
2017-03-10 11:50:43 +01:00
|
|
|
case WIRE_GOSSIPSTATUS_BAD_REQUEST:
|
|
|
|
case WIRE_GOSSIPSTATUS_FDPASS_FAILED:
|
|
|
|
case WIRE_GOSSIPSTATUS_PEER_BAD_MSG:
|
2017-03-19 21:32:40 +01:00
|
|
|
case WIRE_GOSSIPSTATUS_PEER_FAILED:
|
2017-03-10 11:50:43 +01:00
|
|
|
case WIRE_GOSSIPSTATUS_PEER_NONGOSSIP:
|
2017-01-10 06:08:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Control shouldn't give bad requests. */
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_REQUEST, "%i", t);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TESTING
|
2017-06-06 05:08:42 +02:00
|
|
|
static void master_gone(struct io_conn *unused, struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
/* Can't tell master, it's gone. */
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
subdaemon_debug(argc, argv);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
if (argc == 2 && streq(argv[1], "--version")) {
|
|
|
|
printf("%s\n", version());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:36:42 +01:00
|
|
|
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY |
|
|
|
|
SECP256K1_CONTEXT_SIGN);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
daemon = tal(NULL, struct daemon);
|
|
|
|
list_head_init(&daemon->peers);
|
2017-02-04 16:28:35 +01:00
|
|
|
timers_init(&daemon->timers, time_mono());
|
2017-04-24 14:31:26 +02:00
|
|
|
daemon->broadcast_interval = 30000;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
/* stdin == control */
|
2017-06-06 05:08:42 +02:00
|
|
|
daemon_conn_init(daemon, &daemon->master, STDIN_FILENO, recv_req,
|
|
|
|
master_gone);
|
2017-03-19 21:32:44 +01:00
|
|
|
status_setup_async(&daemon->master);
|
2017-04-12 08:12:15 +02:00
|
|
|
|
|
|
|
/* When conn closes, everything is freed. */
|
|
|
|
tal_steal(daemon->master.conn, daemon);
|
|
|
|
|
2017-02-04 16:28:35 +01:00
|
|
|
for (;;) {
|
|
|
|
struct timer *expired = NULL;
|
|
|
|
io_loop(&daemon->timers, &expired);
|
|
|
|
|
|
|
|
if (!expired) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
timer_expired(daemon, expired);
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|