2018-09-28 05:24:17 +02:00
|
|
|
/*~ Welcome to the connect daemon: maintainer of connectivity!
|
|
|
|
*
|
|
|
|
* This is another separate daemon which is responsible for reaching out to
|
|
|
|
* other peers, and also accepting their incoming connections. It talks to
|
|
|
|
* them for just long enough to validate their identity using a cryptographic
|
|
|
|
* handshake, then receive and send supported feature sets; then it hands them
|
|
|
|
* up to lightningd which will fire up a specific per-peer daemon to talk to
|
|
|
|
* it.
|
|
|
|
*/
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <ccan/asort/asort.h>
|
|
|
|
#include <ccan/build_assert/build_assert.h>
|
|
|
|
#include <ccan/cast/cast.h>
|
|
|
|
#include <ccan/container_of/container_of.h>
|
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
|
|
|
#include <ccan/crypto/siphash24/siphash24.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/mem/mem.h>
|
|
|
|
#include <ccan/noerr/noerr.h>
|
2018-10-18 02:52:43 +02:00
|
|
|
#include <ccan/str/str.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <ccan/take/take.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <common/bech32.h>
|
|
|
|
#include <common/bech32_util.h>
|
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/daemon_conn.h>
|
|
|
|
#include <common/decode_short_channel_ids.h>
|
|
|
|
#include <common/features.h>
|
2018-11-22 03:17:29 +01:00
|
|
|
#include <common/memleak.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <common/ping.h>
|
|
|
|
#include <common/pseudorand.h>
|
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/subdaemon.h>
|
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
|
|
|
#include <common/wire_error.h>
|
|
|
|
#include <common/wireaddr.h>
|
2018-09-03 05:40:00 +02:00
|
|
|
#include <connectd/connectd.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/gen_connect_gossip_wire.h>
|
|
|
|
#include <connectd/gen_connect_wire.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/handshake.h>
|
|
|
|
#include <connectd/netaddress.h>
|
2018-09-27 23:04:19 +02:00
|
|
|
#include <connectd/peer_exchange_initmsg.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/tor.h>
|
|
|
|
#include <connectd/tor_autoservice.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2018-09-20 05:06:42 +02:00
|
|
|
#include <hsmd/gen_hsm_wire.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <lightningd/gossip_msg.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
#include <sodium/randombytes.h>
|
2018-08-09 02:25:29 +02:00
|
|
|
#include <stdarg.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
#include <wire/peer_wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ We are passed two file descriptors when exec'ed from `lightningd`: the
|
|
|
|
* first is a connection to `hsmd`, which we need for the cryptographic
|
|
|
|
* handshake, and the second is to `gossipd`: it gathers network gossip and
|
|
|
|
* thus may know how to reach certain peers. */
|
2018-07-24 08:18:58 +02:00
|
|
|
#define HSM_FD 3
|
|
|
|
#define GOSSIPCTL_FD 4
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ In C convention, constants are UPPERCASE macros. Not everything needs to
|
2018-09-28 05:24:23 +02:00
|
|
|
* be a constant, but it soothes the programmer's conscience to encapsulate
|
2018-09-28 05:24:17 +02:00
|
|
|
* arbitrary decisions like these in one place. */
|
|
|
|
#define MAX_CONNECT_ATTEMPTS 10
|
2018-07-24 08:18:58 +02:00
|
|
|
#define INITIAL_WAIT_SECONDS 1
|
|
|
|
#define MAX_WAIT_SECONDS 300
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ We keep a hash table (ccan/htable) of public keys, which tells us what
|
|
|
|
* peers are already connected. The HTABLE_DEFINE_TYPE() macro needs a
|
2018-09-28 05:24:23 +02:00
|
|
|
* keyof() function to extract the key. For this simple use case, that's the
|
2018-09-28 05:24:17 +02:00
|
|
|
* identity function: */
|
2018-09-27 22:58:19 +02:00
|
|
|
static const struct pubkey *pubkey_keyof(const struct pubkey *pk)
|
2018-08-09 02:25:29 +02:00
|
|
|
{
|
|
|
|
return pk;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ We also need to define a hashing function. siphash24 is a fast yet
|
|
|
|
* cryptographic hash in ccan/crypto/siphash24; we might be able to get away
|
|
|
|
* with a slightly faster hash with fewer guarantees, but it's good hygiene to
|
|
|
|
* use this unless it's a proven bottleneck. siphash_seed() is a function in
|
|
|
|
* common/pseudorand which sets up a seed for our hashing; it's different
|
|
|
|
* every time the program is run. */
|
2018-08-09 02:25:29 +02:00
|
|
|
static size_t pubkey_hash(const struct pubkey *id)
|
|
|
|
{
|
|
|
|
return siphash24(siphash_seed(), id, sizeof(*id));
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This defines 'struct pubkey_set' which contains 'struct pubkey' pointers. */
|
2018-08-09 02:25:29 +02:00
|
|
|
HTABLE_DEFINE_TYPE(struct pubkey,
|
|
|
|
pubkey_keyof,
|
|
|
|
pubkey_hash,
|
|
|
|
pubkey_eq,
|
|
|
|
pubkey_set);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This is the global state, like `struct lightningd *ld` in lightningd. */
|
2018-07-24 08:18:58 +02:00
|
|
|
struct daemon {
|
|
|
|
/* Who am I? */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Peers that we've handed to `lightningd`, which it hasn't told us
|
|
|
|
* have disconnected. */
|
2018-08-09 02:25:29 +02:00
|
|
|
struct pubkey_set peers;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
/* Peers we are trying to reach */
|
2018-09-27 23:06:19 +02:00
|
|
|
struct list_head connecting;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
/* Connection to main daemon. */
|
2018-10-25 01:43:05 +02:00
|
|
|
struct daemon_conn *master;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Allow localhost to be considered "public": DEVELOPER-only option,
|
|
|
|
* but for simplicity we don't #if DEVELOPER-wrap it here. */
|
2018-07-24 08:18:58 +02:00
|
|
|
bool dev_allow_localhost;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We support use of a SOCKS5 proxy (e.g. Tor) */
|
2018-07-24 08:18:58 +02:00
|
|
|
struct addrinfo *proxyaddr;
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* They can tell us we must use proxy even for non-Tor addresses. */
|
2018-07-24 08:18:58 +02:00
|
|
|
bool use_proxy_always;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* There are DNS seeds we can use to look up node addresses as a last
|
|
|
|
* resort, but doing so leaks our address so can be disabled. */
|
2018-07-24 08:18:58 +02:00
|
|
|
bool use_dns;
|
|
|
|
|
|
|
|
/* The address that the broken response returns instead of
|
|
|
|
* NXDOMAIN. NULL if we have not detected a broken resolver. */
|
|
|
|
struct sockaddr *broken_resolver_response;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
/* File descriptors to listen on once we're activated. */
|
2018-08-06 06:57:30 +02:00
|
|
|
struct listen_fd *listen_fds;
|
2018-07-24 08:18:58 +02:00
|
|
|
};
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Peers we're trying to reach: we iterate through addrs until we succeed
|
|
|
|
* or fail. */
|
2018-09-27 23:06:19 +02:00
|
|
|
struct connecting {
|
|
|
|
/* daemon->connecting */
|
2018-07-24 08:18:58 +02:00
|
|
|
struct list_node list;
|
|
|
|
|
2018-08-24 08:57:14 +02:00
|
|
|
struct daemon *daemon;
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* The ID of the peer (not necessarily unique, in transit!) */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2018-08-09 02:27:30 +02:00
|
|
|
/* We iterate through the tal_count(addrs) */
|
|
|
|
size_t addrnum;
|
|
|
|
struct wireaddr_internal *addrs;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
/* NULL if there wasn't a hint. */
|
|
|
|
struct wireaddr_internal *addrhint;
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* How far did we get? */
|
|
|
|
const char *connstate;
|
2018-08-09 02:25:29 +02:00
|
|
|
|
2018-08-09 02:27:30 +02:00
|
|
|
/* Accumulated errors */
|
|
|
|
char *errors;
|
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
/* How many seconds did we wait this time? */
|
|
|
|
u32 seconds_waited;
|
2018-07-24 08:18:58 +02:00
|
|
|
};
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ C programs should generally be written bottom-to-top, with the root
|
|
|
|
* function at the bottom, and functions it calls above it. That avoids
|
|
|
|
* us having to pre-declare functions; but in the case of mutual recursion
|
|
|
|
* pre-declarations are necessary (also, sometimes we do it to avoid making
|
|
|
|
* a patch hard to review with gratuitous reorganizations). */
|
2018-09-27 23:06:19 +02:00
|
|
|
static void try_connect_one_addr(struct connecting *connect);
|
2018-08-09 02:27:30 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Some ISP resolvers will reply with a dummy IP to queries that would otherwise
|
2018-07-24 08:18:58 +02:00
|
|
|
* result in an NXDOMAIN reply. This just checks whether we have one such
|
|
|
|
* resolver upstream and remembers its reply so we can try to filter future
|
|
|
|
* dummies out.
|
|
|
|
*/
|
|
|
|
static bool broken_resolver(struct daemon *daemon)
|
|
|
|
{
|
|
|
|
struct addrinfo *addrinfo;
|
|
|
|
struct addrinfo hints;
|
2018-09-27 23:00:19 +02:00
|
|
|
const char *hostname = "nxdomain-test.doesntexist";
|
2018-07-24 08:18:58 +02:00
|
|
|
int err;
|
2018-09-28 05:24:13 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* If they told us to never do DNS queries, don't even do this one */
|
2018-09-28 05:24:13 +02:00
|
|
|
if (!daemon->use_dns) {
|
|
|
|
daemon->broken_resolver_response = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
|
|
|
err = getaddrinfo(hostname, tal_fmt(tmpctx, "%d", 42),
|
2018-09-27 23:00:19 +02:00
|
|
|
&hints, &addrinfo);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Note the use of tal_dup here: it is a memdup for tal, but it's
|
|
|
|
* type-aware so it's less error-prone. */
|
2018-07-24 08:18:58 +02:00
|
|
|
if (err == 0) {
|
2018-09-27 23:00:19 +02:00
|
|
|
daemon->broken_resolver_response
|
|
|
|
= tal_dup(daemon, struct sockaddr, addrinfo->ai_addr);
|
2018-07-24 08:18:58 +02:00
|
|
|
freeaddrinfo(addrinfo);
|
2018-09-27 23:00:19 +02:00
|
|
|
} else
|
|
|
|
daemon->broken_resolver_response = NULL;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-27 23:00:19 +02:00
|
|
|
return daemon->broken_resolver_response != NULL;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Here we see our first tal destructor: in this case the 'struct connect'
|
|
|
|
* simply removes itself from the list of all 'connect' structs. */
|
2018-09-27 23:06:19 +02:00
|
|
|
static void destroy_connecting(struct connecting *connect)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ We don't *need* the list_head here; `list_del(&connect->list)`
|
|
|
|
* would work. But we have access to it, and `list_del_from()` is
|
|
|
|
* clearer for readers, and also does a very brief sanity check that
|
|
|
|
* the list isn't already empty which catches a surprising number of
|
|
|
|
* bugs! (If CCAN_LIST_DEBUG were defined, it would perform a
|
|
|
|
* complete list traverse to check it was in the list before
|
|
|
|
* deletion). */
|
2018-09-27 23:06:19 +02:00
|
|
|
list_del_from(&connect->daemon->connecting, &connect->list);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Most simple search functions start with find_; in this case, search
|
|
|
|
* for an existing attempt to connect the given peer id. */
|
2018-09-27 23:06:19 +02:00
|
|
|
static struct connecting *find_connecting(struct daemon *daemon,
|
|
|
|
const struct pubkey *id)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-09-27 23:06:19 +02:00
|
|
|
struct connecting *i;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Note the pubkey_eq function: this is generally preferred over
|
|
|
|
* doing a memcmp() manually, as it is both typesafe and can handle
|
|
|
|
* any padding which the C compiler is allowed to insert between
|
|
|
|
* members (unnecessary here, as there's no padding in a `struct
|
|
|
|
* pubkey`). */
|
2018-09-27 23:06:19 +02:00
|
|
|
list_for_each(&daemon->connecting, i, list)
|
|
|
|
if (pubkey_eq(id, &i->id))
|
|
|
|
return i;
|
2018-07-24 08:18:58 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Once we've connected, we disable the callback which would cause us to
|
|
|
|
* to try the next address. */
|
2018-09-27 23:06:19 +02:00
|
|
|
static void connected_to_peer(struct daemon *daemon,
|
|
|
|
struct io_conn *conn,
|
|
|
|
const struct pubkey *id)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-08-09 02:25:29 +02:00
|
|
|
/* Don't call destroy_io_conn */
|
2018-07-24 08:18:58 +02:00
|
|
|
io_set_finish(conn, NULL, NULL);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We allocate 'conn' as a child of 'connect': we don't want to free
|
|
|
|
* it just yet though. tal_steal() it onto the permanent 'daemon'
|
|
|
|
* struct. */
|
2018-09-27 23:04:19 +02:00
|
|
|
tal_steal(daemon, conn);
|
2018-09-27 23:06:19 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Now free the 'connecting' struct. */
|
2018-09-27 23:06:19 +02:00
|
|
|
tal_free(find_connecting(daemon, id));
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Every per-peer daemon needs a connection to the gossip daemon; this allows
|
|
|
|
* it to forward gossip to/from the peer. The gossip daemon needs to know a
|
|
|
|
* few of the features of the peer and its id (for reporting).
|
|
|
|
*
|
2018-09-28 05:24:24 +02:00
|
|
|
* The 'localfeatures' is a field in the `init` message, indicating properties
|
|
|
|
* when you're connected to it like we are: there are also 'globalfeatures'
|
2018-09-28 05:24:17 +02:00
|
|
|
* which specify requirements to route a payment through a node. */
|
2018-09-27 23:04:19 +02:00
|
|
|
static int get_gossipfd(struct daemon *daemon,
|
|
|
|
const struct pubkey *id,
|
2018-09-28 05:24:24 +02:00
|
|
|
const u8 *localfeatures)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
bool gossip_queries_feature, initial_routing_sync, success;
|
|
|
|
u8 *msg;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ The way features generally work is that both sides need to offer it;
|
|
|
|
* we always offer `gossip_queries`, but this check is explicit. */
|
2018-07-24 08:18:58 +02:00
|
|
|
gossip_queries_feature
|
2018-12-03 00:06:06 +01:00
|
|
|
= local_feature_negotiated(localfeatures, LOCAL_GOSSIP_QUERIES);
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/*~ `initial_routing_sync is supported by every node, since it was in
|
|
|
|
* the initial lightning specification: it means the peer wants the
|
|
|
|
* backlog of existing gossip. */
|
2018-07-24 08:18:58 +02:00
|
|
|
initial_routing_sync
|
2018-09-28 05:24:24 +02:00
|
|
|
= feature_offered(localfeatures, LOCAL_INITIAL_ROUTING_SYNC);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ We do this communication sync, since gossipd is our friend and
|
|
|
|
* it's easier. If gossipd fails, we fail. */
|
2018-09-27 23:04:19 +02:00
|
|
|
msg = towire_gossip_new_peer(NULL, id, gossip_queries_feature,
|
2018-07-24 08:18:58 +02:00
|
|
|
initial_routing_sync);
|
|
|
|
if (!wire_sync_write(GOSSIPCTL_FD, take(msg)))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed writing to gossipctl: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
2018-09-27 23:04:19 +02:00
|
|
|
msg = wire_sync_read(tmpctx, GOSSIPCTL_FD);
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!fromwire_gossip_new_peer_reply(msg, &success))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed parsing msg gossipctl: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* Gossipd might run out of file descriptors, so it tell us, and we
|
|
|
|
* give up on connecting this peer. */
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!success) {
|
|
|
|
status_broken("Gossipd did not give us an fd: losing peer %s",
|
2018-09-27 23:04:19 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey, id));
|
2018-07-24 08:18:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* Otherwise, the next thing in the socket will be the file descriptor
|
|
|
|
* for the per-peer daemon. */
|
2018-07-24 08:18:58 +02:00
|
|
|
return fdpass_recv(GOSSIPCTL_FD);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This is an ad-hoc marshalling structure where we store arguments so we
|
|
|
|
* can call peer_connected again. */
|
2018-09-27 23:04:19 +02:00
|
|
|
struct peer_reconnected {
|
|
|
|
struct daemon *daemon;
|
|
|
|
struct pubkey id;
|
|
|
|
const u8 *peer_connected_msg;
|
2018-09-28 05:24:24 +02:00
|
|
|
const u8 *localfeatures;
|
2018-09-27 23:04:19 +02:00
|
|
|
};
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ For simplicity, lightningd only ever deals with a single connection per
|
|
|
|
* peer. So if we already know about a peer, we tell lightning to disconnect
|
|
|
|
* the old one and retry once it does. */
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct io_plan *retry_peer_connected(struct io_conn *conn,
|
2018-09-27 23:04:19 +02:00
|
|
|
struct peer_reconnected *pr)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-09-27 23:04:19 +02:00
|
|
|
struct io_plan *plan;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ As you can see, we've had issues with this code before :( */
|
2018-07-24 08:18:58 +02:00
|
|
|
status_trace("peer %s: processing now old peer gone",
|
2018-09-27 23:04:19 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey, &pr->id));
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Usually the pattern is to return this directly, but we have to free
|
|
|
|
* our temporary structure. */
|
2018-09-27 23:04:19 +02:00
|
|
|
plan = peer_connected(conn, pr->daemon, &pr->id,
|
|
|
|
take(pr->peer_connected_msg),
|
2018-09-28 05:24:24 +02:00
|
|
|
take(pr->localfeatures));
|
2018-09-27 23:04:19 +02:00
|
|
|
tal_free(pr);
|
|
|
|
return plan;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:23 +02:00
|
|
|
/*~ If we already know about this peer, we tell lightningd and it disconnects
|
|
|
|
* the old one. We wait until it tells us that's happened. */
|
|
|
|
static struct io_plan *peer_reconnected(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const struct pubkey *id,
|
|
|
|
const u8 *peer_connected_msg TAKES,
|
2018-09-28 05:24:24 +02:00
|
|
|
const u8 *localfeatures TAKES)
|
2018-09-28 05:24:23 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
2018-11-21 23:41:50 +01:00
|
|
|
struct peer_reconnected *pr;
|
2018-09-28 05:24:23 +02:00
|
|
|
|
|
|
|
status_trace("peer %s: reconnect",
|
|
|
|
type_to_string(tmpctx, struct pubkey, id));
|
|
|
|
|
|
|
|
/* Tell master to kill it: will send peer_disconnect */
|
|
|
|
msg = towire_connect_reconnected(NULL, id);
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon_conn_send(daemon->master, take(msg));
|
2018-09-28 05:24:23 +02:00
|
|
|
|
|
|
|
/* Save arguments for next time. */
|
2018-11-21 23:41:50 +01:00
|
|
|
pr = tal(daemon, struct peer_reconnected);
|
|
|
|
pr->daemon = daemon;
|
|
|
|
pr->id = *id;
|
2018-09-28 05:24:23 +02:00
|
|
|
|
|
|
|
/*~ Note that tal_dup_arr() will do handle the take() of
|
2018-09-28 05:24:24 +02:00
|
|
|
* peer_connected_msg and localfeatures (turning it into a simply
|
2018-09-28 05:24:23 +02:00
|
|
|
* tal_steal() in those cases). */
|
2018-11-21 23:41:50 +01:00
|
|
|
pr->peer_connected_msg
|
|
|
|
= tal_dup_arr(pr, u8, peer_connected_msg,
|
2018-09-28 05:24:23 +02:00
|
|
|
tal_count(peer_connected_msg), 0);
|
2018-11-21 23:41:50 +01:00
|
|
|
pr->localfeatures
|
|
|
|
= tal_dup_arr(pr, u8, localfeatures, tal_count(localfeatures), 0);
|
2018-09-28 05:24:23 +02:00
|
|
|
|
|
|
|
/*~ ccan/io supports waiting on an address: in this case, the key in
|
|
|
|
* the peer set. When someone calls `io_wake()` on that address, it
|
|
|
|
* will call retry_peer_connected above. */
|
|
|
|
return io_wait(conn, pubkey_set_get(&daemon->peers, id),
|
2018-11-22 03:17:29 +01:00
|
|
|
/*~ The notleak() wrapper is a DEVELOPER-mode hack so
|
|
|
|
* that our memory leak detection doesn't consider 'pr'
|
|
|
|
* (which is not referenced from our code) to be a
|
|
|
|
* memory leak. */
|
|
|
|
retry_peer_connected, notleak(pr));
|
2018-09-28 05:24:23 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Note the lack of static: this is called by peer_exchange_initmsg.c once the
|
|
|
|
* INIT messages are exchanged, and also by the retry code above. */
|
2018-09-27 23:04:19 +02:00
|
|
|
struct io_plan *peer_connected(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
2018-09-28 05:24:23 +02:00
|
|
|
const struct pubkey *id,
|
2018-09-27 23:04:19 +02:00
|
|
|
const u8 *peer_connected_msg TAKES,
|
2018-09-28 05:24:24 +02:00
|
|
|
const u8 *localfeatures TAKES)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-09-27 23:04:19 +02:00
|
|
|
int gossip_fd;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:23 +02:00
|
|
|
if (pubkey_set_get(&daemon->peers, id))
|
|
|
|
return peer_reconnected(conn, daemon, id, peer_connected_msg,
|
2018-09-28 05:24:24 +02:00
|
|
|
localfeatures);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:23 +02:00
|
|
|
/* We've successfully connected. */
|
2018-09-27 23:06:19 +02:00
|
|
|
connected_to_peer(daemon, conn, id);
|
2018-09-27 23:04:19 +02:00
|
|
|
|
2018-09-28 05:24:24 +02:00
|
|
|
gossip_fd = get_gossipfd(daemon, id, localfeatures);
|
2018-09-27 23:04:19 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We promised we'd take it by marking it TAKEN above; simply free it. */
|
2018-09-28 05:24:24 +02:00
|
|
|
if (taken(localfeatures))
|
|
|
|
tal_free(localfeatures);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* If gossipd can't give us a file descriptor, we give up connecting. */
|
2018-08-09 02:25:29 +02:00
|
|
|
if (gossip_fd < 0)
|
2018-07-24 08:18:58 +02:00
|
|
|
return io_close(conn);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ daemon_conn is a message queue for inter-daemon communication: we
|
|
|
|
* queue up the `connect_peer_connected` message to tell lightningd
|
|
|
|
* we have connected, and give the the peer and gossip fds. */
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon_conn_send(daemon->master, peer_connected_msg);
|
2018-09-28 05:24:17 +02:00
|
|
|
/* io_conn_fd() extracts the fd from ccan/io's io_conn */
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon_conn_send_fd(daemon->master, io_conn_fd(conn));
|
|
|
|
daemon_conn_send_fd(daemon->master, gossip_fd);
|
2018-08-09 02:25:29 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Finally, we add it to the set of pubkeys: tal_dup will handle
|
|
|
|
* take() args for us, by simply tal_steal()ing it. */
|
2018-09-27 23:04:19 +02:00
|
|
|
pubkey_set_add(&daemon->peers, tal_dup(daemon, struct pubkey, id));
|
2018-08-09 02:25:29 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ We want to free the connection, but not close the fd (which is
|
|
|
|
* queued to go to lightningd), so use this variation on io_close: */
|
2018-07-24 08:18:58 +02:00
|
|
|
return io_close_taken_fd(conn);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ handshake.c's handles setting up the crypto state once we get a connection
|
|
|
|
* in; we hand it straight to peer_exchange_initmsg() to send and receive INIT
|
|
|
|
* and call peer_connected(). */
|
2018-08-08 16:09:58 +02:00
|
|
|
static struct io_plan *handshake_in_success(struct io_conn *conn,
|
|
|
|
const struct pubkey *id,
|
|
|
|
const struct wireaddr_internal *addr,
|
|
|
|
const struct crypto_state *cs,
|
|
|
|
struct daemon *daemon)
|
|
|
|
{
|
|
|
|
status_trace("Connect IN from %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, id));
|
2018-09-27 23:04:19 +02:00
|
|
|
return peer_exchange_initmsg(conn, daemon, cs, id, addr);
|
2018-08-08 16:09:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:23 +02:00
|
|
|
/*~ When we get a connection in we set up its network address then call
|
2018-09-28 05:24:17 +02:00
|
|
|
* handshake.c to set up the crypto state. */
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct io_plan *connection_in(struct io_conn *conn, struct daemon *daemon)
|
|
|
|
{
|
|
|
|
struct wireaddr_internal addr;
|
|
|
|
struct sockaddr_storage s = {};
|
|
|
|
socklen_t len = sizeof(s);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* The cast here is a weird Berkeley sockets API feature... */
|
2018-07-24 08:18:58 +02:00
|
|
|
if (getpeername(io_conn_fd(conn), (struct sockaddr *)&s, &len) != 0) {
|
|
|
|
status_trace("Failed to get peername for incoming conn: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *s6 = (void *)&s;
|
|
|
|
addr.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
wireaddr_from_ipv6(&addr.u.wireaddr,
|
|
|
|
&s6->sin6_addr, ntohs(s6->sin6_port));
|
|
|
|
} else if (s.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s4 = (void *)&s;
|
|
|
|
addr.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
wireaddr_from_ipv4(&addr.u.wireaddr,
|
|
|
|
&s4->sin_addr, ntohs(s4->sin_port));
|
|
|
|
} else if (s.ss_family == AF_UNIX) {
|
|
|
|
struct sockaddr_un *sun = (void *)&s;
|
|
|
|
addr.itype = ADDR_INTERNAL_SOCKNAME;
|
|
|
|
memcpy(addr.u.sockname, sun->sun_path, sizeof(sun->sun_path));
|
|
|
|
} else {
|
|
|
|
status_broken("Unknown socket type %i for incoming conn",
|
|
|
|
s.ss_family);
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Timeout */
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ The crypto handshake differs depending on whether you received or
|
2018-11-22 03:17:29 +01:00
|
|
|
* initiated the socket connection, so there are two entry points.
|
|
|
|
* Note, again, the notleak() to avoid our simplistic leak detection
|
|
|
|
* code from thinking `conn` (which we don't keep a pointer to) is
|
|
|
|
* leaked */
|
|
|
|
return responder_handshake(notleak(conn), &daemon->id, &addr,
|
2018-08-08 16:09:58 +02:00
|
|
|
handshake_in_success, daemon);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ These are the mirror functions for the connecting-out case. */
|
2018-09-28 05:23:36 +02:00
|
|
|
static struct io_plan *handshake_out_success(struct io_conn *conn,
|
|
|
|
const struct pubkey *id,
|
|
|
|
const struct wireaddr_internal *addr,
|
|
|
|
const struct crypto_state *cs,
|
|
|
|
struct connecting *connect)
|
|
|
|
{
|
|
|
|
connect->connstate = "Exchanging init messages";
|
|
|
|
status_trace("Connect OUT to %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, id));
|
|
|
|
return peer_exchange_initmsg(conn, connect->daemon, cs, id, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect)
|
|
|
|
{
|
|
|
|
/* FIXME: Timeout */
|
|
|
|
status_trace("Connected out for %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &connect->id));
|
|
|
|
|
|
|
|
connect->connstate = "Cryptographic handshake";
|
|
|
|
return initiator_handshake(conn, &connect->daemon->id, &connect->id,
|
|
|
|
&connect->addrs[connect->addrnum],
|
|
|
|
handshake_out_success, connect);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ When we've exhausted all addresses without success, we come here. */
|
2018-09-28 05:23:36 +02:00
|
|
|
static void PRINTF_FMT(5,6)
|
|
|
|
connect_failed(struct daemon *daemon,
|
|
|
|
const struct pubkey *id,
|
|
|
|
u32 seconds_waited,
|
|
|
|
const struct wireaddr_internal *addrhint,
|
|
|
|
const char *errfmt, ...)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
va_list ap;
|
|
|
|
char *err;
|
|
|
|
u32 wait_seconds;
|
|
|
|
|
|
|
|
va_start(ap, errfmt);
|
|
|
|
err = tal_vfmt(tmpctx, errfmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Wait twice as long to reconnect, between min and max. */
|
|
|
|
wait_seconds = seconds_waited * 2;
|
|
|
|
if (wait_seconds > MAX_WAIT_SECONDS)
|
|
|
|
wait_seconds = MAX_WAIT_SECONDS;
|
|
|
|
if (wait_seconds < INITIAL_WAIT_SECONDS)
|
|
|
|
wait_seconds = INITIAL_WAIT_SECONDS;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* lightningd may have a connect command waiting to know what
|
|
|
|
* happened. We leave it to lightningd to decide if it wants to try
|
|
|
|
* again, with the wait_seconds as a hint of how long before
|
|
|
|
* asking. */
|
2018-09-28 05:23:36 +02:00
|
|
|
msg = towire_connectctl_connect_failed(NULL, id, err, wait_seconds,
|
|
|
|
addrhint);
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon_conn_send(daemon->master, take(msg));
|
2018-09-28 05:23:36 +02:00
|
|
|
|
|
|
|
status_trace("Failed connected out for %s: %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, id),
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This is the destructor for the (unsuccessful) connection. We accumulate
|
|
|
|
* the errors which occurred, so we can report to lightningd properly in case
|
|
|
|
* they all fail, and try the next address.
|
|
|
|
*
|
|
|
|
* This is a specialized form of destructor which takes an extra argument;
|
|
|
|
* it set up by either the creatively-named tal_add_destructor2(), or by
|
2018-09-28 05:24:23 +02:00
|
|
|
* the ccan/io's io_set_finish() on a connection. */
|
2018-09-28 05:23:36 +02:00
|
|
|
static void destroy_io_conn(struct io_conn *conn, struct connecting *connect)
|
|
|
|
{
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ tal_append_fmt appends to a tal string. It's terribly convenient */
|
2018-10-18 02:52:43 +02:00
|
|
|
const char *errstr = strerror(errno);
|
|
|
|
/* errno 0 means they hung up on us. */
|
|
|
|
if (errno == 0) {
|
|
|
|
errstr = "peer closed connection";
|
|
|
|
if (streq(connect->connstate, "Cryptographic handshake"))
|
|
|
|
errstr = "peer closed connection (wrong key?)";
|
|
|
|
}
|
2018-09-28 05:23:36 +02:00
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: %s: %s. ",
|
|
|
|
type_to_string(tmpctx, struct wireaddr_internal,
|
|
|
|
&connect->addrs[connect->addrnum]),
|
2018-10-18 02:52:43 +02:00
|
|
|
connect->connstate, errstr);
|
2018-09-28 05:23:36 +02:00
|
|
|
connect->addrnum++;
|
|
|
|
try_connect_one_addr(connect);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This initializes a fresh io_conn by setting it to io_connect to the
|
|
|
|
* destination */
|
2018-09-28 05:23:36 +02:00
|
|
|
static struct io_plan *conn_init(struct io_conn *conn,
|
|
|
|
struct connecting *connect)
|
|
|
|
{
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ I generally dislike the pattern of "set to NULL, assert if NULL at
|
|
|
|
* bottom". On -O2 and above the compiler will warn you at compile time
|
|
|
|
* if a there is a path by which the variable is not set, which is always
|
|
|
|
* preferable to a runtime assertion. In this case, it's the best way
|
|
|
|
* to use the "enum in a switch" trick to make sure we handle all enum
|
|
|
|
* cases, so I use it. */
|
2018-09-28 05:23:36 +02:00
|
|
|
struct addrinfo *ai = NULL;
|
|
|
|
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
|
|
|
|
|
|
|
|
switch (addr->itype) {
|
|
|
|
case ADDR_INTERNAL_SOCKNAME:
|
|
|
|
ai = wireaddr_internal_to_addrinfo(tmpctx, addr);
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_ALLPROTO:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect to all protocols");
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect to autotor address");
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect to forproxy address");
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
/* If it was a Tor address, we wouldn't be here. */
|
|
|
|
ai = wireaddr_to_addrinfo(tmpctx, &addr->u.wireaddr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(ai);
|
|
|
|
|
|
|
|
io_set_finish(conn, destroy_io_conn, connect);
|
|
|
|
return io_connect(conn, ai, connection_out, connect);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This initializes a fresh io_conn by setting it to io_connect to the
|
|
|
|
* SOCKS proxy, as handled in tor.c. */
|
2018-09-28 05:23:36 +02:00
|
|
|
static struct io_plan *conn_proxy_init(struct io_conn *conn,
|
|
|
|
struct connecting *connect)
|
|
|
|
{
|
|
|
|
const char *host = NULL;
|
|
|
|
u16 port;
|
|
|
|
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
|
|
|
|
|
|
|
|
switch (addr->itype) {
|
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
host = addr->u.unresolved.name;
|
|
|
|
port = addr->u.unresolved.port;
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
host = fmt_wireaddr_without_port(tmpctx, &addr->u.wireaddr);
|
|
|
|
port = addr->u.wireaddr.port;
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_SOCKNAME:
|
|
|
|
case ADDR_INTERNAL_ALLPROTO:
|
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect to %u address", addr->itype);
|
|
|
|
|
|
|
|
io_set_finish(conn, destroy_io_conn, connect);
|
|
|
|
return io_tor_connect(conn, connect->daemon->proxyaddr, host, port,
|
|
|
|
connect);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This is the routine which tries to connect. */
|
2018-09-28 05:23:36 +02:00
|
|
|
static void try_connect_one_addr(struct connecting *connect)
|
|
|
|
{
|
|
|
|
int fd, af;
|
|
|
|
bool use_proxy = connect->daemon->use_proxy_always;
|
|
|
|
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Out of addresses? */
|
2018-09-28 05:23:36 +02:00
|
|
|
if (connect->addrnum == tal_count(connect->addrs)) {
|
|
|
|
connect_failed(connect->daemon, &connect->id,
|
|
|
|
connect->seconds_waited,
|
|
|
|
connect->addrhint, "%s", connect->errors);
|
|
|
|
tal_free(connect);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Might not even be able to create eg. IPv6 sockets */
|
|
|
|
af = -1;
|
|
|
|
|
|
|
|
switch (addr->itype) {
|
|
|
|
case ADDR_INTERNAL_SOCKNAME:
|
|
|
|
af = AF_LOCAL;
|
|
|
|
/* Local sockets don't use tor proxy */
|
|
|
|
use_proxy = false;
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_ALLPROTO:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect ALLPROTO");
|
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect AUTOTOR");
|
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
use_proxy = true;
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
switch (addr->u.wireaddr.type) {
|
|
|
|
case ADDR_TYPE_TOR_V2:
|
|
|
|
case ADDR_TYPE_TOR_V3:
|
|
|
|
use_proxy = true;
|
|
|
|
break;
|
|
|
|
case ADDR_TYPE_IPV4:
|
|
|
|
af = AF_INET;
|
|
|
|
break;
|
|
|
|
case ADDR_TYPE_IPV6:
|
|
|
|
af = AF_INET6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have to use proxy but we don't have one, we fail. */
|
|
|
|
if (use_proxy) {
|
|
|
|
if (!connect->daemon->proxyaddr) {
|
|
|
|
status_debug("Need proxy");
|
|
|
|
af = -1;
|
|
|
|
} else
|
|
|
|
af = connect->daemon->proxyaddr->ai_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (af == -1) {
|
|
|
|
fd = -1;
|
|
|
|
errno = EPROTONOSUPPORT;
|
|
|
|
} else
|
|
|
|
fd = socket(af, SOCK_STREAM, 0);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We might not have eg. IPv6 support, or it might be an onion addr
|
|
|
|
* and we have no proxy. */
|
2018-09-28 05:23:36 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: opening %i socket gave %s. ",
|
|
|
|
type_to_string(tmpctx, struct wireaddr_internal,
|
|
|
|
addr),
|
|
|
|
af, strerror(errno));
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This causes very limited recursion. */
|
2018-09-28 05:23:36 +02:00
|
|
|
connect->addrnum++;
|
|
|
|
try_connect_one_addr(connect);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This creates the new connection using our fd, with the initialization
|
|
|
|
* function one of the above. */
|
2018-09-28 05:23:36 +02:00
|
|
|
if (use_proxy)
|
2018-11-22 03:17:29 +01:00
|
|
|
notleak(io_new_conn(connect, fd, conn_proxy_init, connect));
|
2018-09-28 05:23:36 +02:00
|
|
|
else
|
2018-11-22 03:17:29 +01:00
|
|
|
notleak(io_new_conn(connect, fd, conn_init, connect));
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ connectd is responsible for incoming connections, but it's the process of
|
|
|
|
* setting up the listening ports which gives us information we need for startup
|
|
|
|
* (such as our own address). So we perform setup in two phases: first we bind
|
|
|
|
* the sockets according to the command line arguments (if any), then we start
|
|
|
|
* listening for connections to them once lightningd is ready.
|
|
|
|
*
|
|
|
|
* This stores the fds we're going to listen on: */
|
2018-09-28 05:23:36 +02:00
|
|
|
struct listen_fd {
|
|
|
|
int fd;
|
|
|
|
/* If we bind() IPv6 then IPv4 to same port, we *may* fail to listen()
|
|
|
|
* on the IPv4 socket: under Linux, by default, the IPv6 listen()
|
|
|
|
* covers IPv4 too. Normally we'd consider failing to listen on a
|
|
|
|
* port to be fatal, so we note this when setting up addresses. */
|
|
|
|
bool mayfail;
|
|
|
|
};
|
|
|
|
|
2018-08-06 06:57:30 +02:00
|
|
|
static void add_listen_fd(struct daemon *daemon, int fd, bool mayfail)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ utils.h contains a convenience macro tal_arr_expand which
|
|
|
|
* reallocates a tal_arr to make it one longer, then returns a pointer
|
|
|
|
* to the (new) last element. */
|
2018-09-27 02:19:24 +02:00
|
|
|
struct listen_fd *l = tal_arr_expand(&daemon->listen_fds);
|
|
|
|
l->fd = fd;
|
|
|
|
l->mayfail = mayfail;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Helper routine to create and bind a socket of a given type; like many
|
|
|
|
* daemons we set it SO_REUSEADDR so we won't have to wait 2 minutes to reuse
|
|
|
|
* it on restart.
|
|
|
|
*
|
|
|
|
* I generally avoid "return -1 on error", but for file-descriptors it's the
|
|
|
|
* UNIX standard, so it's not as offensive here as it would be in other
|
|
|
|
* contexts.
|
|
|
|
*/
|
2018-09-28 05:23:36 +02:00
|
|
|
static int make_listen_fd(int domain, void *addr, socklen_t len, bool mayfail)
|
|
|
|
{
|
|
|
|
int fd = socket(domain, SOCK_STREAM, 0);
|
2018-09-28 05:23:52 +02:00
|
|
|
int on = 1;
|
|
|
|
|
2018-09-28 05:23:36 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
if (!mayfail)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed to create %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
|
|
|
status_trace("Failed to create %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:23:52 +02:00
|
|
|
|
|
|
|
/* Re-use, please.. */
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
|
|
|
|
status_unusual("Failed setting socket reuse: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
if (bind(fd, addr, len) != 0) {
|
|
|
|
if (!mayfail)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed to bind on %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
|
|
|
status_trace("Failed to create %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
|
|
|
goto fail;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
fail:
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ ccan/noerr contains convenient routines which don't clobber the
|
|
|
|
* errno global; in this case, the caller can report errno. */
|
2018-09-28 05:23:36 +02:00
|
|
|
close_noerr(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* Return true if it created socket successfully. */
|
|
|
|
static bool handle_wireaddr_listen(struct daemon *daemon,
|
|
|
|
const struct wireaddr *wireaddr,
|
|
|
|
bool mayfail)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Note the use of a switch() over enum here, even though it must be
|
|
|
|
* IPv4 or IPv6 here; that will catch future changes. */
|
2018-07-24 08:18:58 +02:00
|
|
|
switch (wireaddr->type) {
|
|
|
|
case ADDR_TYPE_IPV4:
|
|
|
|
wireaddr_to_ipv4(wireaddr, &addr);
|
|
|
|
/* We might fail if IPv6 bound to port first */
|
|
|
|
fd = make_listen_fd(AF_INET, &addr, sizeof(addr), mayfail);
|
|
|
|
if (fd >= 0) {
|
|
|
|
status_trace("Created IPv4 listener on port %u",
|
|
|
|
wireaddr->port);
|
2018-08-06 06:57:30 +02:00
|
|
|
add_listen_fd(daemon, fd, mayfail);
|
2018-07-24 08:18:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case ADDR_TYPE_IPV6:
|
|
|
|
wireaddr_to_ipv6(wireaddr, &addr6);
|
|
|
|
fd = make_listen_fd(AF_INET6, &addr6, sizeof(addr6), mayfail);
|
|
|
|
if (fd >= 0) {
|
|
|
|
status_trace("Created IPv6 listener on port %u",
|
|
|
|
wireaddr->port);
|
2018-08-06 06:57:30 +02:00
|
|
|
add_listen_fd(daemon, fd, mayfail);
|
2018-07-24 08:18:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case ADDR_TYPE_TOR_V2:
|
|
|
|
case ADDR_TYPE_TOR_V3:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Invalid listener wireaddress type %u", wireaddr->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's a wildcard, turns it into a real address pointing to internet */
|
|
|
|
static bool public_address(struct daemon *daemon, struct wireaddr *wireaddr)
|
|
|
|
{
|
|
|
|
if (wireaddr_is_wildcard(wireaddr)) {
|
|
|
|
if (!guess_address(wireaddr))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* --dev-allow-localhost treats the localhost as "public" for testing */
|
2018-07-24 08:18:58 +02:00
|
|
|
return address_routable(wireaddr, daemon->dev_allow_localhost);
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
static void add_announcable(struct wireaddr **announcable,
|
|
|
|
const struct wireaddr *addr)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-09-27 02:19:24 +02:00
|
|
|
*tal_arr_expand(announcable) = *addr;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_binding(struct wireaddr_internal **binding,
|
|
|
|
const struct wireaddr_internal *addr)
|
|
|
|
{
|
2018-09-27 02:19:24 +02:00
|
|
|
*tal_arr_expand(binding) = *addr;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ ccan/asort provides a type-safe sorting function; it requires a comparison
|
|
|
|
* function, which takes an optional extra argument which is usually unused as
|
|
|
|
* here, but deeply painful if you need it and don't have it! */
|
2018-07-24 08:18:58 +02:00
|
|
|
static int wireaddr_cmp_type(const struct wireaddr *a,
|
|
|
|
const struct wireaddr *b, void *unused)
|
|
|
|
{
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Returns > 0 if a belongs after b, < 0 if before, == 0 if don't care */
|
2018-07-24 08:18:58 +02:00
|
|
|
return (int)a->type - (int)b->type;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ The spec for we-can't-remember reasons specifies only one address of each
|
|
|
|
* type. I think there was a bias against "hubs" which would want this. So
|
|
|
|
* we sort and uniquify. */
|
2018-08-09 02:25:29 +02:00
|
|
|
static void finalize_announcable(struct wireaddr **announcable)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-08-09 02:25:29 +02:00
|
|
|
size_t n = tal_count(*announcable);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The origin node:
|
|
|
|
*...
|
|
|
|
* - MUST NOT include more than one `address descriptor` of the same
|
|
|
|
* type.
|
|
|
|
*/
|
2018-08-09 02:25:29 +02:00
|
|
|
asort(*announcable, n, wireaddr_cmp_type, NULL);
|
2018-07-24 08:18:58 +02:00
|
|
|
for (size_t i = 1; i < n; i++) {
|
|
|
|
/* Note we use > instead of !=: catches asort bugs too. */
|
2018-08-09 02:25:29 +02:00
|
|
|
if ((*announcable)[i].type > (*announcable)[i-1].type)
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
status_unusual("WARNING: Cannot announce address %s,"
|
|
|
|
" already announcing %s",
|
|
|
|
type_to_string(tmpctx, struct wireaddr,
|
2018-08-09 02:25:29 +02:00
|
|
|
&(*announcable)[i]),
|
2018-07-24 08:18:58 +02:00
|
|
|
type_to_string(tmpctx, struct wireaddr,
|
2018-08-09 02:25:29 +02:00
|
|
|
&(*announcable)[i-1]));
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* Move and shrink; step back because i++ above would skip. */
|
2018-08-09 02:25:29 +02:00
|
|
|
memmove(*announcable + i,
|
|
|
|
*announcable + i + 1,
|
|
|
|
(n - i - 1) * sizeof((*announcable)[0]));
|
|
|
|
tal_resize(announcable, --n);
|
2018-07-24 08:18:58 +02:00
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ The user can specify three kinds of addresses: ones we bind to but don't
|
|
|
|
* announce, ones we announce but don't bind to, and ones we bind to and
|
|
|
|
* announce if they seem to be public addresses.
|
|
|
|
*
|
|
|
|
* This routine sorts out the mess: it populates the daemon->announcable array,
|
|
|
|
* and returns the addresses we bound to (by convention, return is allocated
|
|
|
|
* off `ctx` argument).
|
|
|
|
*/
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct wireaddr_internal *setup_listeners(const tal_t *ctx,
|
2018-08-09 02:25:29 +02:00
|
|
|
struct daemon *daemon,
|
2018-09-28 05:24:17 +02:00
|
|
|
/* The proposed address. */
|
2018-08-09 02:25:29 +02:00
|
|
|
const struct wireaddr_internal *proposed_wireaddr,
|
2018-09-28 05:24:17 +02:00
|
|
|
/* For each one, listen,
|
|
|
|
announce or both */
|
2018-08-09 02:25:29 +02:00
|
|
|
const enum addr_listen_announce *proposed_listen_announce,
|
|
|
|
const char *tor_password,
|
|
|
|
struct wireaddr **announcable)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_un addrun;
|
|
|
|
int fd;
|
|
|
|
struct wireaddr_internal *binding;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Start with empty arrays, for tal_arr_expand() */
|
2018-07-24 08:18:58 +02:00
|
|
|
binding = tal_arr(ctx, struct wireaddr_internal, 0);
|
2018-08-09 02:25:29 +02:00
|
|
|
*announcable = tal_arr(ctx, struct wireaddr, 0);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
/* Add addresses we've explicitly been told to *first*: implicit
|
|
|
|
* addresses will be discarded then if we have multiple. */
|
2018-08-09 02:25:29 +02:00
|
|
|
for (size_t i = 0; i < tal_count(proposed_wireaddr); i++) {
|
|
|
|
struct wireaddr_internal wa = proposed_wireaddr[i];
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We want announce-only addresses. */
|
2018-08-09 02:25:29 +02:00
|
|
|
if (proposed_listen_announce[i] & ADDR_LISTEN)
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
assert(proposed_listen_announce[i] & ADDR_ANNOUNCE);
|
2018-09-28 05:24:17 +02:00
|
|
|
/* You can only announce wiretypes, not internal formats! */
|
2018-08-09 02:25:29 +02:00
|
|
|
assert(proposed_wireaddr[i].itype
|
2018-07-24 08:18:58 +02:00
|
|
|
== ADDR_INTERNAL_WIREADDR);
|
2018-08-09 02:25:29 +02:00
|
|
|
add_announcable(announcable, &wa.u.wireaddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now look for listening addresses. */
|
2018-08-09 02:25:29 +02:00
|
|
|
for (size_t i = 0; i < tal_count(proposed_wireaddr); i++) {
|
|
|
|
struct wireaddr_internal wa = proposed_wireaddr[i];
|
|
|
|
bool announce = (proposed_listen_announce[i] & ADDR_ANNOUNCE);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
if (!(proposed_listen_announce[i] & ADDR_LISTEN))
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (wa.itype) {
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We support UNIX domain sockets, but can't announce */
|
2018-07-24 08:18:58 +02:00
|
|
|
case ADDR_INTERNAL_SOCKNAME:
|
|
|
|
addrun.sun_family = AF_UNIX;
|
|
|
|
memcpy(addrun.sun_path, wa.u.sockname,
|
|
|
|
sizeof(addrun.sun_path));
|
|
|
|
fd = make_listen_fd(AF_INET, &addrun, sizeof(addrun),
|
|
|
|
false);
|
|
|
|
status_trace("Created socket listener on file %s",
|
|
|
|
addrun.sun_path);
|
2018-08-06 06:57:30 +02:00
|
|
|
add_listen_fd(daemon, fd, false);
|
2018-07-24 08:18:58 +02:00
|
|
|
/* We don't announce socket names */
|
|
|
|
assert(!announce);
|
|
|
|
add_binding(&binding, &wa);
|
|
|
|
continue;
|
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
/* We handle these after we have all bindings. */
|
|
|
|
continue;
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Special case meaning IPv6 and IPv4 */
|
2018-07-24 08:18:58 +02:00
|
|
|
case ADDR_INTERNAL_ALLPROTO: {
|
|
|
|
bool ipv6_ok;
|
|
|
|
|
|
|
|
wa.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
wa.u.wireaddr.port = wa.u.port;
|
|
|
|
|
2018-08-06 06:57:30 +02:00
|
|
|
/* First, create wildcard IPv6 address. */
|
2018-07-24 08:18:58 +02:00
|
|
|
wa.u.wireaddr.type = ADDR_TYPE_IPV6;
|
|
|
|
wa.u.wireaddr.addrlen = 16;
|
2018-08-06 06:57:30 +02:00
|
|
|
memset(wa.u.wireaddr.addr, 0,
|
|
|
|
sizeof(wa.u.wireaddr.addr));
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
ipv6_ok = handle_wireaddr_listen(daemon, &wa.u.wireaddr,
|
|
|
|
true);
|
|
|
|
if (ipv6_ok) {
|
|
|
|
add_binding(&binding, &wa);
|
|
|
|
if (announce
|
|
|
|
&& public_address(daemon, &wa.u.wireaddr))
|
2018-08-09 02:25:29 +02:00
|
|
|
add_announcable(announcable,
|
|
|
|
&wa.u.wireaddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
2018-08-06 06:57:30 +02:00
|
|
|
|
|
|
|
/* Now, create wildcard IPv4 address. */
|
2018-07-24 08:18:58 +02:00
|
|
|
wa.u.wireaddr.type = ADDR_TYPE_IPV4;
|
|
|
|
wa.u.wireaddr.addrlen = 4;
|
2018-08-06 06:57:30 +02:00
|
|
|
memset(wa.u.wireaddr.addr, 0,
|
|
|
|
sizeof(wa.u.wireaddr.addr));
|
2018-07-24 08:18:58 +02:00
|
|
|
/* OK if this fails, as long as one succeeds! */
|
|
|
|
if (handle_wireaddr_listen(daemon, &wa.u.wireaddr,
|
|
|
|
ipv6_ok)) {
|
|
|
|
add_binding(&binding, &wa);
|
|
|
|
if (announce
|
|
|
|
&& public_address(daemon, &wa.u.wireaddr))
|
2018-08-09 02:25:29 +02:00
|
|
|
add_announcable(announcable,
|
|
|
|
&wa.u.wireaddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This is a vanilla wireaddr as per BOLT #7 */
|
2018-07-24 08:18:58 +02:00
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
handle_wireaddr_listen(daemon, &wa.u.wireaddr, false);
|
|
|
|
add_binding(&binding, &wa);
|
|
|
|
if (announce && public_address(daemon, &wa.u.wireaddr))
|
2018-08-09 02:25:29 +02:00
|
|
|
add_announcable(announcable, &wa.u.wireaddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Shouldn't happen. */
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Invalid listener address type %u",
|
2018-08-09 02:25:29 +02:00
|
|
|
proposed_wireaddr[i].itype);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Now we have bindings, set up any Tor auto addresses: we will point
|
|
|
|
* it at the first bound IPv4 or IPv6 address we have. */
|
2018-08-09 02:25:29 +02:00
|
|
|
for (size_t i = 0; i < tal_count(proposed_wireaddr); i++) {
|
|
|
|
if (!(proposed_listen_announce[i] & ADDR_LISTEN))
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
if (proposed_wireaddr[i].itype != ADDR_INTERNAL_AUTOTOR)
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
2018-12-19 10:01:40 +01:00
|
|
|
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
|
|
|
|
tor_autoservice(tmpctx,
|
|
|
|
&proposed_wireaddr[i].u.torservice,
|
|
|
|
tor_password,
|
|
|
|
binding);
|
|
|
|
continue;
|
|
|
|
};
|
2018-08-09 02:25:29 +02:00
|
|
|
add_announcable(announcable,
|
2018-07-24 08:18:58 +02:00
|
|
|
tor_autoservice(tmpctx,
|
2018-08-09 02:25:29 +02:00
|
|
|
&proposed_wireaddr[i].u.torservice,
|
|
|
|
tor_password,
|
2018-07-24 08:18:58 +02:00
|
|
|
binding));
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Sort and uniquify. */
|
2018-08-09 02:25:29 +02:00
|
|
|
finalize_announcable(announcable);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
return binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Parse the incoming connect init message from lightningd ("master") and
|
|
|
|
* assign config variables to the daemon; it should be the first message we
|
|
|
|
* get. */
|
2018-10-25 01:43:05 +02:00
|
|
|
static struct io_plan *connect_init(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
struct wireaddr *proxyaddr;
|
2018-07-24 08:18:58 +02:00
|
|
|
struct wireaddr_internal *binding;
|
2018-08-09 02:25:29 +02:00
|
|
|
struct wireaddr_internal *proposed_wireaddr;
|
|
|
|
enum addr_listen_announce *proposed_listen_announce;
|
|
|
|
struct wireaddr *announcable;
|
|
|
|
char *tor_password;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Fields which require allocation are allocated off daemon */
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!fromwire_connectctl_init(
|
2018-07-24 08:18:58 +02:00
|
|
|
daemon, msg,
|
2018-12-03 00:06:06 +01:00
|
|
|
&daemon->id,
|
|
|
|
&proposed_wireaddr,
|
2018-08-09 02:25:29 +02:00
|
|
|
&proposed_listen_announce,
|
2018-07-24 08:18:58 +02:00
|
|
|
&proxyaddr, &daemon->use_proxy_always,
|
|
|
|
&daemon->dev_allow_localhost, &daemon->use_dns,
|
2018-08-09 02:25:29 +02:00
|
|
|
&tor_password)) {
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This is a helper which prints the type expected and the actual
|
|
|
|
* message, then exits (it should never be called!). */
|
2018-07-24 08:18:58 +02:00
|
|
|
master_badmsg(WIRE_CONNECTCTL_INIT, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Resolve Tor proxy address if any: we need an addrinfo to connect()
|
|
|
|
* to. */
|
2018-07-24 08:18:58 +02:00
|
|
|
if (proxyaddr) {
|
|
|
|
status_trace("Proxy address: %s",
|
|
|
|
fmt_wireaddr(tmpctx, proxyaddr));
|
|
|
|
daemon->proxyaddr = wireaddr_to_addrinfo(daemon, proxyaddr);
|
2018-11-22 03:17:23 +01:00
|
|
|
tal_free(proxyaddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
} else
|
|
|
|
daemon->proxyaddr = NULL;
|
|
|
|
|
|
|
|
if (broken_resolver(daemon)) {
|
|
|
|
status_trace("Broken DNS resolver detected, will check for "
|
|
|
|
"dummy replies");
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Figure out our addresses. */
|
2018-08-09 02:25:29 +02:00
|
|
|
binding = setup_listeners(tmpctx, daemon,
|
|
|
|
proposed_wireaddr,
|
|
|
|
proposed_listen_announce,
|
|
|
|
tor_password,
|
|
|
|
&announcable);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
/* Free up old allocations */
|
|
|
|
tal_free(proposed_wireaddr);
|
|
|
|
tal_free(proposed_listen_announce);
|
|
|
|
tal_free(tor_password);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Tell it we're ready, handing it the addresses we have. */
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon_conn_send(daemon->master,
|
2018-07-24 08:18:58 +02:00
|
|
|
take(towire_connectctl_init_reply(NULL,
|
|
|
|
binding,
|
2018-08-09 02:25:29 +02:00
|
|
|
announcable)));
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Read the next message. */
|
2018-10-25 01:43:05 +02:00
|
|
|
return daemon_conn_read_next(conn, daemon->master);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ lightningd tells us to go! */
|
2018-10-25 01:43:05 +02:00
|
|
|
static struct io_plan *connect_activate(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
bool do_listen;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!fromwire_connectctl_activate(msg, &do_listen))
|
|
|
|
master_badmsg(WIRE_CONNECTCTL_ACTIVATE, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* If we're --offline, lightningd tells us not to actually listen. */
|
2018-07-24 08:18:58 +02:00
|
|
|
if (do_listen) {
|
|
|
|
for (size_t i = 0; i < tal_count(daemon->listen_fds); i++) {
|
2018-08-06 06:57:30 +02:00
|
|
|
/* On Linux, at least, we may bind to all addresses
|
|
|
|
* for IPv4 and IPv6, but we'll fail to listen. */
|
|
|
|
if (listen(daemon->listen_fds[i].fd, 5) != 0) {
|
|
|
|
if (daemon->listen_fds[i].mayfail)
|
|
|
|
continue;
|
2018-07-24 08:18:58 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed to listen on socket: %s",
|
|
|
|
strerror(errno));
|
2018-08-06 06:57:30 +02:00
|
|
|
}
|
2018-11-22 03:17:29 +01:00
|
|
|
notleak(io_new_listener(daemon,
|
|
|
|
daemon->listen_fds[i].fd,
|
|
|
|
connection_in, daemon));
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Free, with NULL assignment just as an extra sanity check. */
|
2018-07-24 08:18:58 +02:00
|
|
|
daemon->listen_fds = tal_free(daemon->listen_fds);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
/* OK, we're ready! */
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon_conn_send(daemon->master,
|
2018-07-24 08:18:58 +02:00
|
|
|
take(towire_connectctl_activate_reply(NULL)));
|
2018-10-25 01:43:05 +02:00
|
|
|
return daemon_conn_read_next(conn, daemon->master);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This is where we'd put a BOLT #10 reference, but it doesn't exist :( */
|
2018-07-24 08:18:58 +02:00
|
|
|
static const char *seedname(const tal_t *ctx, const struct pubkey *id)
|
|
|
|
{
|
|
|
|
char bech32[100];
|
|
|
|
u8 der[PUBKEY_DER_LEN];
|
|
|
|
u5 *data = tal_arr(ctx, u5, 0);
|
|
|
|
|
|
|
|
pubkey_to_der(der, id);
|
|
|
|
bech32_push_bits(&data, der, PUBKEY_DER_LEN*8);
|
|
|
|
bech32_encode(bech32, "ln", data, tal_count(data), sizeof(bech32));
|
|
|
|
return tal_fmt(ctx, "%s.lseed.bitcoinstats.com", bech32);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ As a last resort, we do a DNS lookup to the lightning DNS seed to
|
|
|
|
* resolve a node name when they say to connect to it. This is synchronous,
|
|
|
|
* so connectd blocks, but it's not very common so we haven't fixed it.
|
|
|
|
*
|
|
|
|
* This "seed by DNS" approach is similar to what bitcoind uses, and in fact
|
|
|
|
* has the nice property that DNS is cached, and the seed only sees a request
|
|
|
|
* from the ISP, not directly from the user. */
|
2018-08-09 02:27:30 +02:00
|
|
|
static void add_seed_addrs(struct wireaddr_internal **addrs,
|
|
|
|
const struct pubkey *id,
|
|
|
|
struct sockaddr *broken_reply)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-08-09 02:27:30 +02:00
|
|
|
struct wireaddr_internal a;
|
2018-07-24 08:18:58 +02:00
|
|
|
const char *addr;
|
|
|
|
|
|
|
|
addr = seedname(tmpctx, id);
|
|
|
|
status_trace("Resolving %s", addr);
|
|
|
|
|
2018-08-09 02:27:30 +02:00
|
|
|
a.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
/* FIXME: wireaddr_from_hostname should return multiple addresses. */
|
|
|
|
if (!wireaddr_from_hostname(&a.u.wireaddr, addr, DEFAULT_PORT, NULL,
|
2018-07-24 08:18:58 +02:00
|
|
|
broken_reply, NULL)) {
|
|
|
|
status_trace("Could not resolve %s", addr);
|
|
|
|
} else {
|
|
|
|
status_trace("Resolved %s to %s", addr,
|
2018-08-09 02:27:30 +02:00
|
|
|
type_to_string(tmpctx, struct wireaddr,
|
|
|
|
&a.u.wireaddr));
|
2018-09-27 02:19:24 +02:00
|
|
|
*tal_arr_expand(addrs) = a;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ This asks gossipd for any addresses advertized by the node. */
|
2018-08-09 02:27:30 +02:00
|
|
|
static void add_gossip_addrs(struct wireaddr_internal **addrs,
|
|
|
|
const struct pubkey *id)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
u8 *msg;
|
2018-08-09 02:27:30 +02:00
|
|
|
struct wireaddr *normal_addrs;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* For simplicity, we do this synchronous. */
|
2018-07-24 08:18:58 +02:00
|
|
|
msg = towire_gossip_get_addrs(NULL, id);
|
|
|
|
if (!wire_sync_write(GOSSIPCTL_FD, take(msg)))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed writing to gossipctl: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* This returns 'struct wireaddr's since that's what's supported by
|
|
|
|
* the BOLT #7 protocol. */
|
2018-07-24 08:18:58 +02:00
|
|
|
msg = wire_sync_read(tmpctx, GOSSIPCTL_FD);
|
2018-08-09 02:27:30 +02:00
|
|
|
if (!fromwire_gossip_get_addrs_reply(tmpctx, msg, &normal_addrs))
|
2018-07-24 08:18:58 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed parsing get_addrs_reply gossipctl: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
|
2018-08-09 02:27:30 +02:00
|
|
|
/* Wrap each one in a wireaddr_internal and add to addrs. */
|
|
|
|
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
|
|
|
|
struct wireaddr_internal addr;
|
|
|
|
addr.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
addr.u.wireaddr = normal_addrs[i];
|
2018-09-27 02:19:24 +02:00
|
|
|
*tal_arr_expand(addrs) = addr;
|
2018-08-09 02:27:30 +02:00
|
|
|
}
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Consumes addrhint if not NULL.
|
|
|
|
*
|
|
|
|
* That's a pretty ugly interface: we should use TAKEN, but we only have one
|
|
|
|
* caller so it's marginal. */
|
2018-09-27 23:06:19 +02:00
|
|
|
static void try_connect_peer(struct daemon *daemon,
|
2018-09-28 05:24:17 +02:00
|
|
|
const struct pubkey *id,
|
|
|
|
u32 seconds_waited,
|
|
|
|
struct wireaddr_internal *addrhint)
|
2018-08-09 02:27:30 +02:00
|
|
|
{
|
|
|
|
struct wireaddr_internal *addrs;
|
|
|
|
bool use_proxy = daemon->use_proxy_always;
|
2018-09-27 23:06:19 +02:00
|
|
|
struct connecting *connect;
|
2018-08-09 02:27:30 +02:00
|
|
|
|
|
|
|
/* Already done? May happen with timer. */
|
|
|
|
if (pubkey_set_get(&daemon->peers, id))
|
|
|
|
return;
|
|
|
|
|
2018-09-27 23:06:19 +02:00
|
|
|
/* If we're trying to connect it right now, that's OK. */
|
|
|
|
if (find_connecting(daemon, id))
|
2018-08-09 02:27:30 +02:00
|
|
|
return;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Start an array of addresses to try. */
|
2018-08-09 02:27:30 +02:00
|
|
|
addrs = tal_arr(tmpctx, struct wireaddr_internal, 0);
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* They can supply an optional address for the connect RPC */
|
2018-08-09 02:27:30 +02:00
|
|
|
if (addrhint)
|
2018-09-27 02:19:24 +02:00
|
|
|
*tal_arr_expand(&addrs) = *addrhint;
|
2018-08-09 02:27:30 +02:00
|
|
|
|
|
|
|
add_gossip_addrs(&addrs, id);
|
|
|
|
|
|
|
|
if (tal_count(addrs) == 0) {
|
|
|
|
/* Don't resolve via DNS seed if we're supposed to use proxy. */
|
|
|
|
if (use_proxy) {
|
2018-09-28 05:24:17 +02:00
|
|
|
/* You're allowed to use names with proxies; in fact it's
|
|
|
|
* a good idea. */
|
2018-08-09 02:27:30 +02:00
|
|
|
struct wireaddr_internal unresolved;
|
|
|
|
wireaddr_from_unresolved(&unresolved,
|
|
|
|
seedname(tmpctx, id),
|
|
|
|
DEFAULT_PORT);
|
2018-09-27 02:19:24 +02:00
|
|
|
*tal_arr_expand(&addrs) = unresolved;
|
2018-08-09 02:27:30 +02:00
|
|
|
} else if (daemon->use_dns) {
|
|
|
|
add_seed_addrs(&addrs, id,
|
|
|
|
daemon->broken_resolver_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Still no address? Fail immediately. Lightningd can still choose
|
|
|
|
* to retry; an address may get gossiped or appear on the DNS seed. */
|
2018-08-09 02:27:30 +02:00
|
|
|
if (tal_count(addrs) == 0) {
|
2018-08-09 02:25:29 +02:00
|
|
|
connect_failed(daemon, id, seconds_waited, addrhint,
|
2018-08-09 02:27:30 +02:00
|
|
|
"No address known");
|
2018-07-24 08:18:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Start connecting to it: since this is the only place we allocate
|
|
|
|
* a 'struct connecting' we don't write a separate new_connecting(). */
|
2018-09-27 23:06:19 +02:00
|
|
|
connect = tal(daemon, struct connecting);
|
|
|
|
connect->daemon = daemon;
|
|
|
|
connect->id = *id;
|
|
|
|
connect->addrs = tal_steal(connect, addrs);
|
|
|
|
connect->addrnum = 0;
|
2018-09-28 05:24:17 +02:00
|
|
|
/* connstate is supposed to be updated as we go, to give context for
|
|
|
|
* errors which occur. We miss it in a few places; would be nice to
|
|
|
|
* fix! */
|
2018-09-27 23:06:19 +02:00
|
|
|
connect->connstate = "Connection establishment";
|
|
|
|
connect->seconds_waited = seconds_waited;
|
|
|
|
connect->addrhint = tal_steal(connect, addrhint);
|
|
|
|
connect->errors = tal_strdup(connect, "");
|
|
|
|
list_add_tail(&daemon->connecting, &connect->list);
|
|
|
|
tal_add_destructor(connect, destroy_connecting);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Now we kick it off by trying connect->addrs[connect->addrnum] */
|
2018-09-27 23:06:19 +02:00
|
|
|
try_connect_one_addr(connect);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* lightningd tells us to connect to a peer by id, with optional addr hint. */
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct io_plan *connect_to_peer(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
2018-08-09 02:25:29 +02:00
|
|
|
u32 seconds_waited;
|
2018-08-09 02:25:29 +02:00
|
|
|
struct wireaddr_internal *addrhint;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
if (!fromwire_connectctl_connect_to_peer(tmpctx, msg,
|
|
|
|
&id, &seconds_waited,
|
|
|
|
&addrhint))
|
2018-07-24 08:18:58 +02:00
|
|
|
master_badmsg(WIRE_CONNECTCTL_CONNECT_TO_PEER, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-27 23:06:19 +02:00
|
|
|
try_connect_peer(daemon, &id, seconds_waited, addrhint);
|
2018-10-25 01:43:05 +02:00
|
|
|
return daemon_conn_read_next(conn, daemon->master);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* lightningd tells us a peer has disconnected. */
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct io_plan *peer_disconnected(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
2018-08-09 02:25:29 +02:00
|
|
|
struct pubkey id, *key;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!fromwire_connectctl_peer_disconnected(msg, &id))
|
|
|
|
master_badmsg(WIRE_CONNECTCTL_PEER_DISCONNECTED, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* We should stay in sync with lightningd at all times. */
|
2018-08-09 02:25:29 +02:00
|
|
|
key = pubkey_set_get(&daemon->peers, &id);
|
|
|
|
if (!key)
|
2018-07-24 08:18:58 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"peer_disconnected unknown peer: %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
2018-08-09 02:25:29 +02:00
|
|
|
pubkey_set_del(&daemon->peers, key);
|
2018-09-27 23:03:19 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Wake up in case there's a reconnecting peer waiting in io_wait. */
|
2018-09-27 23:03:19 +02:00
|
|
|
io_wake(key);
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* Note: deleting from a htable (a-la pubkey_set_del) does not free it:
|
|
|
|
* htable doesn't assume it's a tal object at all. */
|
2018-08-09 02:25:29 +02:00
|
|
|
tal_free(key);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Read the next message from lightningd. */
|
2018-10-25 01:43:05 +02:00
|
|
|
return daemon_conn_read_next(conn, daemon->master);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
static struct io_plan *dev_connect_memleak(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct htable *memtable;
|
|
|
|
bool found_leak;
|
|
|
|
|
|
|
|
memtable = memleak_enter_allocations(tmpctx, msg, msg);
|
|
|
|
|
|
|
|
/* Now delete daemon and those which it has pointers to. */
|
|
|
|
memleak_remove_referenced(memtable, daemon);
|
|
|
|
memleak_remove_htable(memtable, &daemon->peers.raw);
|
|
|
|
|
|
|
|
found_leak = dump_memleak(memtable);
|
|
|
|
daemon_conn_send(daemon->master,
|
|
|
|
take(towire_connect_dev_memleak_reply(NULL,
|
|
|
|
found_leak)));
|
|
|
|
return daemon_conn_read_next(conn, daemon->master);
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2018-10-25 01:43:05 +02:00
|
|
|
static struct io_plan *recv_req(struct io_conn *conn,
|
|
|
|
const u8 *msg,
|
|
|
|
struct daemon *daemon)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2018-10-25 01:43:05 +02:00
|
|
|
enum connect_wire_type t = fromwire_peektype(msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Demux requests from lightningd: we expect INIT then ACTIVATE, then
|
|
|
|
* connect requests and disconnected messages. */
|
2018-07-24 08:18:58 +02:00
|
|
|
switch (t) {
|
2018-07-24 08:18:58 +02:00
|
|
|
case WIRE_CONNECTCTL_INIT:
|
2018-10-25 01:43:05 +02:00
|
|
|
return connect_init(conn, daemon, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
case WIRE_CONNECTCTL_ACTIVATE:
|
2018-10-25 01:43:05 +02:00
|
|
|
return connect_activate(conn, daemon, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
|
|
|
case WIRE_CONNECTCTL_CONNECT_TO_PEER:
|
2018-10-25 01:43:05 +02:00
|
|
|
return connect_to_peer(conn, daemon, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
case WIRE_CONNECTCTL_PEER_DISCONNECTED:
|
2018-10-25 01:43:05 +02:00
|
|
|
return peer_disconnected(conn, daemon, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CONNECT_DEV_MEMLEAK:
|
|
|
|
#if DEVELOPER
|
|
|
|
return dev_connect_memleak(conn, daemon, msg);
|
|
|
|
#endif
|
2018-07-24 08:18:58 +02:00
|
|
|
/* We send these, we don't receive them */
|
2018-07-24 08:18:58 +02:00
|
|
|
case WIRE_CONNECTCTL_INIT_REPLY:
|
|
|
|
case WIRE_CONNECTCTL_ACTIVATE_REPLY:
|
|
|
|
case WIRE_CONNECT_PEER_CONNECTED:
|
|
|
|
case WIRE_CONNECT_RECONNECTED:
|
2018-08-09 02:25:29 +02:00
|
|
|
case WIRE_CONNECTCTL_CONNECT_FAILED:
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CONNECT_DEV_MEMLEAK_REPLY:
|
2018-07-24 08:18:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Master shouldn't give bad requests. */
|
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "%i: %s",
|
2018-10-25 01:43:05 +02:00
|
|
|
t, tal_hex(tmpctx, msg));
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ Helper for handshake.c: we ask `hsmd` to do the ECDH to get the shared
|
|
|
|
* secret. It's here because it's nicer then giving the handshake code
|
|
|
|
* knowledge of the HSM, but also at one stage I made a hacky gossip vampire
|
|
|
|
* tool which used the handshake code, so it's nice to keep that
|
|
|
|
* standalone. */
|
2019-01-08 01:20:50 +01:00
|
|
|
struct secret *hsm_do_ecdh(const tal_t *ctx, const struct pubkey *point)
|
2018-09-20 05:06:42 +02:00
|
|
|
{
|
|
|
|
u8 *req = towire_hsm_ecdh_req(tmpctx, point), *resp;
|
2019-01-08 01:20:50 +01:00
|
|
|
struct secret *secret = tal(ctx, struct secret);
|
2018-09-20 05:06:42 +02:00
|
|
|
|
|
|
|
if (!wire_sync_write(HSM_FD, req))
|
2019-01-08 01:20:50 +01:00
|
|
|
return tal_free(secret);
|
2018-09-20 05:06:42 +02:00
|
|
|
resp = wire_sync_read(req, HSM_FD);
|
|
|
|
if (!resp)
|
2019-01-08 01:20:50 +01:00
|
|
|
return tal_free(secret);
|
|
|
|
|
|
|
|
/* Note: hsmd will actually hang up on us if it can't ECDH: that implies
|
|
|
|
* that our node private key is invalid, and we shouldn't have made
|
|
|
|
* it this far. */
|
|
|
|
if (!fromwire_hsm_ecdh_resp(resp, secret))
|
|
|
|
return tal_free(secret);
|
|
|
|
|
|
|
|
return secret;
|
2018-09-20 05:06:42 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ UNUSED is defined to an __attribute__ for GCC; at one stage we tried to use
|
|
|
|
* it ubiquitously to make us compile cleanly with -Wunused, but it's bitrotted
|
|
|
|
* and we'd need to start again.
|
|
|
|
*
|
|
|
|
* The C++ method of omitting unused parameter names is *much* neater, and I
|
|
|
|
* hope we'll eventually see it in a C standard. */
|
2018-10-25 01:43:05 +02:00
|
|
|
static void master_gone(struct daemon_conn *master UNUSED)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
/* Can't tell master, it's gone. */
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
setup_locale();
|
|
|
|
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Common subdaemon setup code. */
|
2018-07-24 08:18:58 +02:00
|
|
|
subdaemon_setup(argc, argv);
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Allocate and set up our simple top-level structure. */
|
2018-07-24 08:18:58 +02:00
|
|
|
daemon = tal(NULL, struct daemon);
|
2018-08-09 02:25:29 +02:00
|
|
|
pubkey_set_init(&daemon->peers);
|
2018-09-27 23:06:19 +02:00
|
|
|
list_head_init(&daemon->connecting);
|
2018-08-06 06:57:30 +02:00
|
|
|
daemon->listen_fds = tal_arr(daemon, struct listen_fd, 0);
|
2018-07-24 08:18:58 +02:00
|
|
|
/* stdin == control */
|
2018-10-25 01:43:05 +02:00
|
|
|
daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL,
|
|
|
|
daemon);
|
|
|
|
tal_add_destructor(daemon->master, master_gone);
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/* This tells the status_* subsystem to use this connection to send
|
|
|
|
* our status_ and failed messages. */
|
2018-10-25 01:43:05 +02:00
|
|
|
status_setup_async(daemon->master);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-27 22:59:19 +02:00
|
|
|
/* Should never exit. */
|
|
|
|
io_loop(NULL, NULL);
|
|
|
|
abort();
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
2018-09-28 05:24:17 +02:00
|
|
|
|
|
|
|
/*~ Getting bored? This was a pretty simple daemon!
|
|
|
|
*
|
|
|
|
* The good news is that the next daemon gossipd/gossipd.c is the most complex
|
|
|
|
* global daemon we have!
|
|
|
|
*/
|