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-02-01 15:49:01 +01:00
|
|
|
#include <daemon/broadcast.h>
|
2017-03-09 19:34:04 +01:00
|
|
|
#include <daemon/log.h>
|
2017-02-01 15:49:01 +01:00
|
|
|
#include <daemon/routing.h>
|
2017-02-04 16:28:35 +01:00
|
|
|
#include <daemon/timeout.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
2017-03-11 15:31:17 +01:00
|
|
|
#include <lightningd/connection.h>
|
2017-02-21 05:45:19 +01:00
|
|
|
#include <lightningd/cryptomsg.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <lightningd/debug.h>
|
2017-03-10 11:50:43 +01:00
|
|
|
#include <lightningd/gossip/gen_gossip_wire.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 <status.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <version.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
|
|
|
|
|
|
|
struct daemon {
|
|
|
|
struct list_head peers;
|
|
|
|
u8 *msg_in;
|
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-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-02-06 19:10:21 +01:00
|
|
|
u8 **msg_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
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (peer->error)
|
|
|
|
status_send(towire_gossipstatus_peer_bad_msg(peer,
|
|
|
|
peer->unique_id,
|
|
|
|
(u8 *)peer->error));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-06 19:10:21 +01:00
|
|
|
peer->msg_out = tal_arr(peer, u8*, 0);
|
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-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-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
|
|
|
|
|
|
|
case WIRE_INIT:
|
|
|
|
peer->error = "Duplicate INIT message received";
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
case WIRE_OPEN_CHANNEL:
|
|
|
|
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:
|
|
|
|
case WIRE_COMMIT_SIG:
|
|
|
|
case WIRE_REVOKE_AND_ACK:
|
|
|
|
/* 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-10 13:06:51 +01:00
|
|
|
peer->local = false;
|
2017-01-10 06:08:33 +01:00
|
|
|
status_send(s);
|
|
|
|
status_send_fd(io_conn_fd(conn));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-01 15:49:01 +01:00
|
|
|
/* Gets called by the outgoing IO loop when woken up. Sends messages
|
|
|
|
* to the peer if there are any queued. Also checks if we have any
|
|
|
|
* queued gossip messages and processes them. */
|
|
|
|
static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer);
|
|
|
|
|
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-03-11 14:45:54 +01:00
|
|
|
new_reltimer(&peer->daemon->timers, peer, time_from_sec(30),
|
|
|
|
wake_pkt_out, peer);
|
2017-03-11 15:31:17 +01:00
|
|
|
/* Notify the peer-write loop */
|
2017-02-06 19:10:21 +01:00
|
|
|
io_wake(peer);
|
2017-03-11 15:31:17 +01:00
|
|
|
/* Notify the daemon_conn-write loop */
|
|
|
|
io_wake(&peer->owner_conn);
|
2017-02-06 19:10:21 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 15:49:01 +01:00
|
|
|
/* Loop through the backlog of channel_{announcements,updates} and
|
|
|
|
* node_announcements, writing out one on each iteration. Once we are
|
|
|
|
* through wait for the broadcast interval and start again. */
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *peer_dump_gossip(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-02-01 15:49:01 +01:00
|
|
|
struct queued_message *next;
|
2017-03-09 11:16:37 +01:00
|
|
|
next = next_broadcast_message(peer->daemon->rstate->broadcasts,
|
|
|
|
&peer->broadcast_index);
|
2017-02-01 15:49:01 +01:00
|
|
|
|
|
|
|
if (!next) {
|
2017-03-09 11:16:37 +01:00
|
|
|
/* Going to wake up in pkt_out since we mix time based and
|
|
|
|
* message based wakeups */
|
2017-02-01 15:49:01 +01:00
|
|
|
return io_out_wait(conn, peer, pkt_out, peer);
|
|
|
|
} else {
|
2017-03-09 11:16:37 +01:00
|
|
|
/* Do not free the message after send, queue_broadcast takes
|
|
|
|
* care of that */
|
|
|
|
return peer_write_message(conn, &peer->pcs, next->payload,
|
|
|
|
peer_dump_gossip);
|
2017-02-01 15:49:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-03-10 13:06:51 +01:00
|
|
|
assert(peer->local);
|
2017-02-06 19:10:21 +01:00
|
|
|
/* First we process queued packets, if any */
|
|
|
|
u8 *out;
|
|
|
|
size_t n = tal_count(peer->msg_out);
|
|
|
|
if (n > 0) {
|
|
|
|
out = peer->msg_out[0];
|
|
|
|
memmove(peer->msg_out, peer->msg_out + 1, (sizeof(*peer->msg_out)*(n-1)));
|
|
|
|
tal_resize(&peer->msg_out, n-1);
|
2017-03-07 02:08:20 +01:00
|
|
|
return peer_write_message(conn, &peer->pcs, take(out), pkt_out);
|
2017-02-06 19:10:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 13:06:51 +01:00
|
|
|
if (peer->gossip_sync && peer->local){
|
2017-02-08 14:05:21 +01:00
|
|
|
/* Send any queued up broadcast messages */
|
|
|
|
peer->gossip_sync = false;
|
|
|
|
return peer_dump_gossip(conn, peer);
|
|
|
|
} else {
|
|
|
|
return io_out_wait(conn, peer, pkt_out, peer);
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool has_even_bit(const u8 *bitmap)
|
|
|
|
{
|
|
|
|
size_t len = tal_count(bitmap);
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
if (*bitmap & 0xAA)
|
|
|
|
return true;
|
|
|
|
len--;
|
|
|
|
bitmap++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return io_out_wait(conn, peer, daemon_conn_write_next, dc);
|
|
|
|
|
2017-03-10 13:06:51 +01:00
|
|
|
next = next_broadcast_message(peer->daemon->rstate->broadcasts,
|
|
|
|
&peer->broadcast_index);
|
|
|
|
|
|
|
|
if (!next) {
|
2017-03-11 15:31:17 +01:00
|
|
|
return io_out_wait(conn, peer, 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-03-11 15:31:17 +01:00
|
|
|
static int peer_create_owner_conn(struct peer *peer)
|
2017-03-09 14:24:32 +01:00
|
|
|
{
|
|
|
|
int fds[2];
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-11 15:31:17 +01:00
|
|
|
daemon_conn_init(peer, &peer->owner_conn, fds[0], owner_msg_in);
|
|
|
|
peer->owner_conn.msg_queue_cleared_cb = nonlocal_dump_gossip;
|
2017-03-09 14:24:32 +01:00
|
|
|
return fds[1];
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *peer_parse_init(struct io_conn *conn,
|
|
|
|
struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
u8 *gfeatures, *lfeatures;
|
2017-03-09 14:24:32 +01:00
|
|
|
int client_fd;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
if (!fromwire_init(msg, msg, NULL, &gfeatures, &lfeatures)) {
|
|
|
|
peer->error = tal_fmt(msg, "Bad init: %s", tal_hex(msg, msg));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The receiving node MUST fail the channels if it receives a
|
|
|
|
* `globalfeatures` or `localfeatures` with an even bit set which it
|
|
|
|
* does not understand.
|
|
|
|
*/
|
|
|
|
if (has_even_bit(gfeatures)) {
|
|
|
|
peer->error = tal_fmt(msg, "Bad globalfeatures: %s",
|
|
|
|
tal_hex(msg, gfeatures));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_even_bit(lfeatures)) {
|
|
|
|
peer->error = tal_fmt(msg, "Bad localfeatures: %s",
|
|
|
|
tal_hex(msg, lfeatures));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
client_fd = peer_create_owner_conn(peer);
|
|
|
|
|
2017-03-09 14:24:32 +01:00
|
|
|
if (client_fd == -1) {
|
|
|
|
peer->error = tal_fmt(msg, "Internal error");
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* Each node MUST wait to receive `init` before sending any other
|
|
|
|
* messages.
|
|
|
|
*/
|
|
|
|
status_send(towire_gossipstatus_peer_ready(msg, peer->unique_id));
|
2017-03-09 14:24:32 +01:00
|
|
|
status_send_fd(client_fd);
|
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-02-01 15:49:01 +01:00
|
|
|
peer_dump_gossip(conn, peer));
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_init_sent(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-02-24 06:52:56 +01:00
|
|
|
return peer_read_message(conn, &peer->pcs, peer_parse_init);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_send_init(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The sending node SHOULD use the minimum lengths required to
|
|
|
|
* represent the feature fields. The sending node MUST set feature
|
|
|
|
* bits corresponding to features it requires the peer to support, and
|
|
|
|
* SHOULD set feature bits corresponding to features it optionally
|
|
|
|
* supports.
|
|
|
|
*/
|
2017-02-24 06:52:56 +01:00
|
|
|
return peer_write_message(conn, &peer->pcs,
|
2017-03-07 02:08:20 +01:00
|
|
|
take(towire_init(peer, NULL, NULL)),
|
2017-01-10 06:08:33 +01:00
|
|
|
peer_init_sent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *next_req_in(struct io_conn *conn, struct daemon *daemon);
|
|
|
|
|
|
|
|
static struct io_plan *new_peer_got_fd(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
peer->conn = io_new_conn(conn, peer->fd, peer_send_init, peer);
|
|
|
|
if (!peer->conn) {
|
|
|
|
peer->error = "Could not create connection";
|
|
|
|
tal_free(peer);
|
2017-03-09 11:35:52 +01:00
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
return next_req_in(conn, peer->daemon);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *release_peer_fd(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-03-09 11:35:52 +01:00
|
|
|
return io_send_fd(conn, peer->fd, next_req_in, peer->daemon);
|
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));
|
|
|
|
|
|
|
|
list_for_each(&daemon->peers, peer, list) {
|
|
|
|
if (peer->unique_id == unique_id) {
|
|
|
|
u8 *out;
|
|
|
|
|
|
|
|
/* Don't talk to this peer any more. */
|
|
|
|
peer->fd = io_conn_fd(peer->conn);
|
|
|
|
tal_steal(daemon, peer);
|
|
|
|
io_close_taken_fd(peer->conn);
|
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
out = towire_gossipctl_release_peer_reply(msg,
|
|
|
|
unique_id,
|
|
|
|
&peer->pcs.cs);
|
2017-03-10 13:06:51 +01:00
|
|
|
peer->local = false;
|
2017-01-10 06:08:33 +01:00
|
|
|
return io_write_wire(conn, out, release_peer_fd, peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status_failed(WIRE_GOSSIPSTATUS_BAD_RELEASE_REQUEST,
|
|
|
|
"Unknown peer %"PRIu64, unique_id);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
nodes[node_count].hostname = n->hostname;
|
|
|
|
nodes[node_count].port = n->port;
|
|
|
|
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-12 13:39:23 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
return io_write_wire(conn, take(out), next_req_in, daemon);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *recv_req(struct io_conn *conn, struct daemon *daemon)
|
|
|
|
{
|
2017-03-10 11:50:43 +01:00
|
|
|
enum gossip_wire_type t = fromwire_peektype(daemon->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
status_trace("req: type %s len %zu",
|
2017-03-10 11:50:43 +01:00
|
|
|
gossip_wire_type_name(t), tal_count(daemon->msg_in));
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_GOSSIPCTL_NEW_PEER:
|
|
|
|
return new_peer(conn, daemon, daemon->msg_in);
|
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER:
|
|
|
|
return release_peer(conn, daemon, daemon->msg_in);
|
|
|
|
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REQUEST:
|
|
|
|
return getnodes(conn, daemon);
|
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY:
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_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:
|
|
|
|
case WIRE_GOSSIPSTATUS_BAD_REQUEST:
|
|
|
|
case WIRE_GOSSIPSTATUS_FDPASS_FAILED:
|
|
|
|
case WIRE_GOSSIPSTATUS_PEER_BAD_MSG:
|
|
|
|
case WIRE_GOSSIPSTATUS_PEER_READY:
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *next_req_in(struct io_conn *conn, struct daemon *daemon)
|
|
|
|
{
|
|
|
|
daemon->msg_in = tal_free(daemon->msg_in);
|
|
|
|
return io_read_wire(conn, daemon, &daemon->msg_in, recv_req, daemon);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TESTING
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct daemon *daemon;
|
2017-03-09 19:34:04 +01:00
|
|
|
struct log_book *log_book;
|
|
|
|
struct log *base_log;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
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);
|
2017-03-09 19:34:04 +01:00
|
|
|
log_book = new_log_book(daemon, 2 * 1024 * 1024, LOG_INFORM);
|
|
|
|
base_log =
|
|
|
|
new_log(daemon, log_book, "lightningd_gossip(%u):", (int)getpid());
|
|
|
|
daemon->rstate = new_routing_state(daemon, base_log);
|
2017-01-10 06:08:33 +01:00
|
|
|
list_head_init(&daemon->peers);
|
2017-02-04 16:28:35 +01:00
|
|
|
timers_init(&daemon->timers, time_mono());
|
2017-01-10 06:08:33 +01:00
|
|
|
daemon->msg_in = NULL;
|
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
/* stdin == control */
|
|
|
|
status_setup(STDIN_FILENO);
|
2017-01-10 06:08:33 +01:00
|
|
|
io_new_conn(NULL, STDIN_FILENO, next_req_in, 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
|
|
|
|
|
|
|
tal_free(daemon);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|