2017-01-10 06:08:33 +01:00
|
|
|
#include "gossip_control.h"
|
|
|
|
#include "lightningd.h"
|
|
|
|
#include "peer_control.h"
|
2017-03-10 11:50:43 +01:00
|
|
|
#include "subd.h"
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/take/take.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <daemon/jsonrpc.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <daemon/log.h>
|
|
|
|
#include <inttypes.h>
|
2017-02-21 05:45:19 +01:00
|
|
|
#include <lightningd/cryptomsg.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-02-24 06:52:56 +01:00
|
|
|
#include <wire/gen_peer_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
static void gossip_finished(struct subd *gossip, int status)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
errx(1, "Gossip failed (exit status %i), exiting.",
|
|
|
|
WEXITSTATUS(status));
|
|
|
|
errx(1, "Gossip failed (signal %u), exiting.", WTERMSIG(status));
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
static void peer_bad_message(struct subd *gossip, const u8 *msg)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
u64 unique_id;
|
|
|
|
struct peer *peer;
|
|
|
|
u8 *err;
|
|
|
|
|
|
|
|
if (!fromwire_gossipstatus_peer_bad_msg(msg, msg, NULL,
|
|
|
|
&unique_id, &err))
|
|
|
|
fatal("Gossip gave bad PEER_BAD message %s", tal_hex(msg, msg));
|
|
|
|
|
|
|
|
peer = peer_by_unique_id(gossip->ld, unique_id);
|
|
|
|
if (!peer)
|
|
|
|
fatal("Gossip gave bad peerid %"PRIu64, unique_id);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
log_debug(gossip->log, "Peer %s gave bad msg %s",
|
|
|
|
type_to_string(msg, struct pubkey, peer->id),
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
peer_set_condition(peer, "Bad message %s during gossip phase",
|
2017-03-10 11:50:43 +01:00
|
|
|
gossip_wire_type_name(fromwire_peektype(msg)));
|
2017-01-10 06:08:33 +01:00
|
|
|
tal_free(peer);
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
static void peer_nongossip(struct subd *gossip, const u8 *msg, int fd)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
u64 unique_id;
|
|
|
|
struct peer *peer;
|
|
|
|
u8 *inner;
|
2017-02-24 06:52:56 +01:00
|
|
|
struct crypto_state cs;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
if (!fromwire_gossipstatus_peer_nongossip(msg, msg, NULL,
|
2017-02-24 06:52:56 +01:00
|
|
|
&unique_id, &cs, &inner))
|
2017-01-10 06:08:33 +01:00
|
|
|
fatal("Gossip gave bad PEER_NONGOSSIP message %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
|
|
|
|
peer = peer_by_unique_id(gossip->ld, unique_id);
|
|
|
|
if (!peer)
|
|
|
|
fatal("Gossip gave bad peerid %"PRIu64, unique_id);
|
|
|
|
|
2017-03-10 11:57:17 +01:00
|
|
|
if (peer->owner != gossip)
|
2017-02-24 06:52:56 +01:00
|
|
|
fatal("Gossip gave bad peerid %"PRIu64" (owner %s)",
|
|
|
|
unique_id, peer->owner ? peer->owner->name : "(none)");
|
|
|
|
|
|
|
|
/* It returned the fd. */
|
|
|
|
assert(peer->fd == -1);
|
|
|
|
peer->fd = fd;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
peer_set_condition(peer, "Gossip ended up receipt of %s",
|
2017-02-24 06:52:56 +01:00
|
|
|
wire_type_name(fromwire_peektype(inner)));
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
peer_accept_open(peer, &cs, inner);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 14:24:32 +01:00
|
|
|
static void peer_ready(struct subd *gossip, const u8 *msg, int fd)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
u64 unique_id;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossipstatus_peer_ready(msg, NULL, &unique_id))
|
|
|
|
fatal("Gossip gave bad PEER_READY message %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
|
|
|
|
peer = peer_by_unique_id(gossip->ld, unique_id);
|
|
|
|
if (!peer)
|
|
|
|
fatal("Gossip gave bad peerid %"PRIu64, unique_id);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
log_debug_struct(gossip->log, "Peer %s ready for channel open",
|
|
|
|
struct pubkey, peer->id);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
if (peer->connect_cmd) {
|
|
|
|
struct json_result *response;
|
|
|
|
response = new_json_result(peer->connect_cmd);
|
|
|
|
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_pubkey(response, "id", peer->id);
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(peer->connect_cmd, response);
|
|
|
|
peer->connect_cmd = NULL;
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-03-09 14:24:32 +01:00
|
|
|
peer->gossip_client_fd = fd;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
peer_set_condition(peer, "Exchanging gossip");
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
static enum subd_msg_ret gossip_msg(struct subd *gossip,
|
|
|
|
const u8 *msg, int fd)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-03-10 11:50:43 +01:00
|
|
|
enum gossip_wire_type t = fromwire_peektype(msg);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
/* We don't get told about fatal errors. */
|
|
|
|
case WIRE_GOSSIPSTATUS_INIT_FAILED:
|
|
|
|
case WIRE_GOSSIPSTATUS_BAD_NEW_PEER_REQUEST:
|
|
|
|
case WIRE_GOSSIPSTATUS_BAD_REQUEST:
|
|
|
|
case WIRE_GOSSIPSTATUS_FDPASS_FAILED:
|
|
|
|
case WIRE_GOSSIPSTATUS_BAD_RELEASE_REQUEST:
|
2017-03-10 11:50:43 +01:00
|
|
|
/* These are messages we send, not them. */
|
|
|
|
case WIRE_GOSSIPCTL_NEW_PEER:
|
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER:
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REQUEST:
|
2017-03-10 11:50:43 +01:00
|
|
|
/* This is a reply, so never gets through to here. */
|
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY:
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REPLY:
|
2017-01-10 06:08:33 +01:00
|
|
|
break;
|
|
|
|
case WIRE_GOSSIPSTATUS_PEER_BAD_MSG:
|
|
|
|
peer_bad_message(gossip, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_GOSSIPSTATUS_PEER_NONGOSSIP:
|
|
|
|
if (fd == -1)
|
2017-03-10 11:50:43 +01:00
|
|
|
return SUBD_NEED_FD;
|
2017-01-10 06:08:33 +01:00
|
|
|
peer_nongossip(gossip, msg, fd);
|
|
|
|
break;
|
|
|
|
case WIRE_GOSSIPSTATUS_PEER_READY:
|
2017-03-09 14:24:32 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
return SUBD_NEED_FD;
|
|
|
|
}
|
|
|
|
peer_ready(gossip, msg, fd);
|
2017-01-10 06:08:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-03-10 11:50:43 +01:00
|
|
|
return SUBD_COMPLETE;
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void gossip_init(struct lightningd *ld)
|
|
|
|
{
|
2017-03-10 11:50:43 +01:00
|
|
|
ld->gossip = new_subd(ld, ld, "lightningd_gossip", NULL,
|
|
|
|
gossip_wire_type_name,
|
|
|
|
gossip_msg, gossip_finished, -1);
|
2017-01-10 06:08:33 +01:00
|
|
|
if (!ld->gossip)
|
|
|
|
err(1, "Could not subdaemon gossip");
|
|
|
|
}
|
2017-03-12 13:39:23 +01:00
|
|
|
|
|
|
|
static bool json_getnodes_reply(struct subd *gossip, const u8 *reply,
|
|
|
|
struct command *cmd)
|
|
|
|
{
|
2017-03-16 05:05:26 +01:00
|
|
|
struct gossip_getnodes_entry *nodes;
|
2017-03-12 13:39:23 +01:00
|
|
|
struct json_result *response = new_json_result(cmd);
|
2017-03-16 05:05:26 +01:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_getnodes_reply(reply, reply, NULL, &nodes)) {
|
|
|
|
command_fail(cmd, "Malformed gossip_getnodes response");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:39:23 +01:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_array_start(response, "nodes");
|
|
|
|
|
2017-03-16 05:05:26 +01:00
|
|
|
for (i = 0; i < tal_count(nodes); i++) {
|
2017-03-12 13:39:23 +01:00
|
|
|
json_object_start(response, NULL);
|
2017-03-16 05:05:26 +01:00
|
|
|
json_add_pubkey(response, "nodeid", &nodes[i].nodeid);
|
|
|
|
if (tal_len(nodes[i].hostname) > 0) {
|
|
|
|
json_add_string(response, "hostname", nodes[i].hostname);
|
2017-03-12 13:39:23 +01:00
|
|
|
} else {
|
|
|
|
json_add_null(response, "hostname");
|
|
|
|
}
|
2017-03-16 05:05:26 +01:00
|
|
|
json_add_num(response, "port", nodes[i].port);
|
2017-03-12 13:39:23 +01:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void json_getnodes(struct command *cmd, const char *buffer,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct lightningd *ld = ld_from_dstate(cmd->dstate);
|
|
|
|
u8 *req = towire_gossip_getnodes_request(cmd);
|
|
|
|
subd_req(ld->gossip, req, -1, NULL, json_getnodes_reply, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command getnodes_command = {
|
|
|
|
"getnodes", json_getnodes, "Retrieve all nodes in our local network view",
|
|
|
|
"Returns a list of all nodes that we know about"};
|
|
|
|
AUTODATA(json_command, &getnodes_command);
|