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.
|
|
|
|
*/
|
2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-04-08 11:58:32 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <ccan/asort/asort.h>
|
2022-03-16 02:26:41 +01:00
|
|
|
#include <ccan/closefrom/closefrom.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/noerr/noerr.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <common/bech32.h>
|
|
|
|
#include <common/bech32_util.h>
|
|
|
|
#include <common/daemon_conn.h>
|
2022-01-08 14:25:29 +01:00
|
|
|
#include <common/dev_disconnect.h>
|
2020-04-03 05:21:22 +02:00
|
|
|
#include <common/ecdh_hsmd.h>
|
2022-01-08 14:28:29 +01:00
|
|
|
#include <common/gossip_rcvd_filter.h>
|
|
|
|
#include <common/gossip_store.h>
|
2020-01-05 18:17:25 +01:00
|
|
|
#include <common/jsonrpc_errors.h>
|
2018-11-22 03:17:29 +01:00
|
|
|
#include <common/memleak.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/subdaemon.h>
|
2020-09-11 08:48:12 +02:00
|
|
|
#include <common/timeout.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/wire_error.h>
|
2018-09-03 05:40:00 +02:00
|
|
|
#include <connectd/connectd.h>
|
2020-08-25 04:16:22 +02:00
|
|
|
#include <connectd/connectd_gossipd_wiregen.h>
|
|
|
|
#include <connectd/connectd_wiregen.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/handshake.h>
|
2022-01-08 14:19:29 +01:00
|
|
|
#include <connectd/multiplex.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/netaddress.h>
|
2022-01-29 04:32:32 +01:00
|
|
|
#include <connectd/onion_message.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>
|
2021-10-15 07:49:05 +02:00
|
|
|
#include <fcntl.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <netdb.h>
|
2021-09-21 11:30:37 +02:00
|
|
|
#include <netinet/in.h>
|
2022-03-16 02:26:41 +01:00
|
|
|
#include <signal.h>
|
2019-11-15 09:44:22 +01:00
|
|
|
#include <sodium.h>
|
2021-10-15 07:49:05 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <wire/wire_sync.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
|
|
|
/* 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;
|
|
|
|
|
2020-11-17 02:06:24 +01:00
|
|
|
struct io_conn *conn;
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* The ID of the peer (not necessarily unique, in transit!) */
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
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
|
|
|
|
2021-08-18 12:47:42 +02:00
|
|
|
/* If they told us to never do DNS queries, don't even do this one and
|
|
|
|
* also not if we just say that we don't */
|
2021-08-18 12:52:21 +02:00
|
|
|
if (!daemon->use_dns || daemon->always_use_proxy) {
|
2018-09-28 05:24:13 +02:00
|
|
|
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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *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
|
|
|
|
2020-05-03 22:57:34 +02:00
|
|
|
/*~ Note the node_id_eq function: this is generally preferred over
|
2018-09-28 05:24:17 +02:00
|
|
|
* 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
|
2020-05-03 22:57:34 +02:00
|
|
|
* node_id`). */
|
2018-09-27 23:06:19 +02:00
|
|
|
list_for_each(&daemon->connecting, i, list)
|
2019-04-08 11:58:32 +02:00
|
|
|
if (node_id_eq(id, &i->id))
|
2018-09-27 23:06:19 +02:00
|
|
|
return i;
|
2018-07-24 08:18:58 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-24 05:37:50 +01:00
|
|
|
/*~ Once we've connected out, we disable the callback which would cause us to
|
2018-09-28 05:24:17 +02:00
|
|
|
* to try the next address. */
|
2021-03-24 05:37:50 +01:00
|
|
|
static void connected_out_to_peer(struct daemon *daemon,
|
|
|
|
struct io_conn *conn,
|
|
|
|
const struct node_id *id)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2021-03-24 05:37:50 +01:00
|
|
|
struct connecting *connect = find_connecting(daemon, id);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
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
|
|
|
|
2021-03-24 05:37:50 +01:00
|
|
|
/* We only allow one outgoing attempt at a time */
|
|
|
|
assert(connect->conn == conn);
|
|
|
|
|
|
|
|
/* Don't call destroy_io_conn, since we're done. */
|
|
|
|
io_set_finish(conn, NULL, NULL);
|
|
|
|
|
|
|
|
/* Now free the 'connecting' struct. */
|
|
|
|
tal_free(connect);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ Once they've connected in, stop trying to connect out (if we were). */
|
|
|
|
static void peer_connected_in(struct daemon *daemon,
|
|
|
|
struct io_conn *conn,
|
|
|
|
const struct node_id *id)
|
|
|
|
{
|
|
|
|
struct connecting *connect = find_connecting(daemon, id);
|
|
|
|
|
|
|
|
if (!connect)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Don't call destroy_io_conn, since we're done. */
|
|
|
|
io_set_finish(connect->conn, NULL, NULL);
|
|
|
|
|
|
|
|
/* Now free the 'connecting' struct since we succeeded. */
|
|
|
|
tal_free(connect);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2019-06-03 20:14:25 +02:00
|
|
|
struct wireaddr_internal addr;
|
2021-10-12 13:16:37 +02:00
|
|
|
const struct wireaddr *remote_addr;
|
2019-06-03 20:14:25 +02:00
|
|
|
struct crypto_state cs;
|
2020-04-03 02:03:59 +02:00
|
|
|
const u8 *their_features;
|
2021-03-24 05:37:50 +01:00
|
|
|
bool incoming;
|
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 :( */
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(&pr->id, "processing now old peer gone");
|
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. */
|
2021-10-12 13:16:37 +02:00
|
|
|
plan = peer_connected(conn, pr->daemon, &pr->id, &pr->addr,
|
|
|
|
pr->remote_addr,
|
|
|
|
&pr->cs, take(pr->their_features), pr->incoming);
|
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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2019-06-03 20:14:25 +02:00
|
|
|
const struct wireaddr_internal *addr,
|
2021-10-12 13:16:37 +02:00
|
|
|
const struct wireaddr *remote_addr,
|
2019-06-03 20:14:25 +02:00
|
|
|
const struct crypto_state *cs,
|
2021-03-24 05:37:50 +01:00
|
|
|
const u8 *their_features TAKES,
|
|
|
|
bool incoming)
|
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
|
|
|
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(id, "reconnect");
|
2018-09-28 05:24:23 +02:00
|
|
|
|
|
|
|
/* Tell master to kill it: will send peer_disconnect */
|
2020-08-25 04:16:22 +02:00
|
|
|
msg = towire_connectd_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;
|
2019-06-03 20:14:25 +02:00
|
|
|
pr->cs = *cs;
|
|
|
|
pr->addr = *addr;
|
2021-10-12 13:16:37 +02:00
|
|
|
pr->remote_addr = remote_addr;
|
2021-03-24 05:37:50 +01:00
|
|
|
pr->incoming = incoming;
|
2018-09-28 05:24:23 +02:00
|
|
|
|
2020-02-27 03:17:01 +01:00
|
|
|
/*~ Note that tal_dup_talarr() will do handle the take() of features
|
2019-10-11 04:52:04 +02:00
|
|
|
* (turning it into a simply tal_steal() in those cases). */
|
2020-04-03 02:03:59 +02:00
|
|
|
pr->their_features = tal_dup_talarr(pr, u8, their_features);
|
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. */
|
2022-01-08 14:19:29 +01:00
|
|
|
return io_wait(conn, peer_htable_get(&daemon->peers, id),
|
2019-09-01 22:14:50 +02: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. */
|
2018-11-22 03:17:29 +01:00
|
|
|
retry_peer_connected, notleak(pr));
|
2018-09-28 05:24:23 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 14:19:29 +01:00
|
|
|
/*~ When we free a peer, we remove it from the daemon's hashtable */
|
|
|
|
static void destroy_peer(struct peer *peer, struct daemon *daemon)
|
|
|
|
{
|
|
|
|
peer_htable_del(&daemon->peers, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ This is where we create a new peer. */
|
|
|
|
static struct peer *new_peer(struct daemon *daemon,
|
|
|
|
const struct node_id *id,
|
|
|
|
const struct crypto_state *cs,
|
|
|
|
const u8 *their_features,
|
|
|
|
struct io_conn *conn STEALS,
|
|
|
|
int *fd_for_subd)
|
|
|
|
{
|
|
|
|
struct peer *peer = tal(daemon, struct peer);
|
|
|
|
|
2022-01-08 14:28:29 +01:00
|
|
|
peer->daemon = daemon;
|
2022-01-08 14:19:29 +01:00
|
|
|
peer->id = *id;
|
2022-01-08 14:24:29 +01:00
|
|
|
peer->cs = *cs;
|
2022-01-08 14:19:29 +01:00
|
|
|
peer->final_msg = NULL;
|
|
|
|
peer->subd_in = NULL;
|
|
|
|
peer->peer_in = NULL;
|
|
|
|
peer->sent_to_peer = NULL;
|
2022-01-08 14:26:29 +01:00
|
|
|
peer->urgent = false;
|
2022-01-11 02:16:49 +01:00
|
|
|
peer->told_to_close = false;
|
2022-01-11 02:16:18 +01:00
|
|
|
peer->peer_outq = msg_queue_new(peer, false);
|
|
|
|
peer->subd_outq = msg_queue_new(peer, false);
|
2022-01-08 14:19:29 +01:00
|
|
|
|
2022-01-11 02:16:10 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
peer->dev_writes_enabled = NULL;
|
|
|
|
peer->dev_read_enabled = true;
|
|
|
|
#endif
|
|
|
|
|
2022-01-11 02:17:01 +01:00
|
|
|
peer->to_peer = conn;
|
|
|
|
|
2022-01-08 14:19:29 +01:00
|
|
|
/* Aim for connection to shuffle data back and forth: sets up
|
|
|
|
* peer->to_subd */
|
|
|
|
if (!multiplex_subd_setup(peer, fd_for_subd))
|
|
|
|
return tal_free(peer);
|
|
|
|
|
2022-01-11 02:17:01 +01:00
|
|
|
/* Now we own it */
|
|
|
|
tal_steal(peer, peer->to_peer);
|
2022-01-08 14:19:29 +01:00
|
|
|
peer_htable_add(&daemon->peers, peer);
|
|
|
|
tal_add_destructor2(peer, destroy_peer, daemon);
|
2022-01-08 14:28:29 +01:00
|
|
|
|
2022-01-08 14:19:29 +01:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2019-06-03 20:14:25 +02:00
|
|
|
const struct wireaddr_internal *addr,
|
2021-10-12 13:16:37 +02:00
|
|
|
const struct wireaddr *remote_addr,
|
2020-04-02 06:03:47 +02:00
|
|
|
struct crypto_state *cs,
|
2021-03-24 05:37:50 +01:00
|
|
|
const u8 *their_features TAKES,
|
|
|
|
bool incoming)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2019-06-03 20:14:25 +02:00
|
|
|
u8 *msg;
|
2022-01-08 14:19:29 +01:00
|
|
|
struct peer *peer;
|
2020-04-02 06:03:47 +02:00
|
|
|
int unsup;
|
2020-08-13 19:38:02 +02:00
|
|
|
size_t depender, missing;
|
2022-01-29 04:33:05 +01:00
|
|
|
int subd_fd;
|
2022-01-29 04:33:05 +01:00
|
|
|
bool option_gossip_queries;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2022-01-08 14:19:29 +01:00
|
|
|
peer = peer_htable_get(&daemon->peers, id);
|
|
|
|
if (peer)
|
2021-10-12 13:16:37 +02:00
|
|
|
return peer_reconnected(conn, daemon, id, addr, remote_addr, cs,
|
2021-03-24 05:37:50 +01:00
|
|
|
their_features, incoming);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2019-05-04 07:53:13 +02:00
|
|
|
/* We promised we'd take it by marking it TAKEN above; prepare to free it. */
|
2020-04-03 02:03:59 +02:00
|
|
|
if (taken(their_features))
|
|
|
|
tal_steal(tmpctx, their_features);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2020-04-02 06:03:47 +02:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The receiving node:
|
|
|
|
* ...
|
|
|
|
* - upon receiving unknown _odd_ feature bits that are non-zero:
|
|
|
|
* - MUST ignore the bit.
|
|
|
|
* - upon receiving unknown _even_ feature bits that are non-zero:
|
|
|
|
* - MUST fail the connection.
|
|
|
|
*/
|
2020-04-03 02:03:59 +02:00
|
|
|
unsup = features_unsupported(daemon->our_features, their_features,
|
|
|
|
INIT_FEATURE);
|
2020-04-02 06:03:47 +02:00
|
|
|
if (unsup != -1) {
|
2021-07-12 22:48:58 +02:00
|
|
|
status_peer_unusual(id, "Unsupported feature %u", unsup);
|
2021-02-03 03:51:41 +01:00
|
|
|
msg = towire_warningfmt(NULL, NULL, "Unsupported feature %u",
|
|
|
|
unsup);
|
2020-04-02 06:03:47 +02:00
|
|
|
msg = cryptomsg_encrypt_msg(tmpctx, cs, take(msg));
|
|
|
|
return io_write(conn, msg, tal_count(msg), io_close_cb, NULL);
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:38:02 +02:00
|
|
|
if (!feature_check_depends(their_features, &depender, &missing)) {
|
2021-07-12 22:48:58 +02:00
|
|
|
status_peer_unusual(id, "Feature %zu requires feature %zu",
|
|
|
|
depender, missing);
|
2021-02-03 03:51:41 +01:00
|
|
|
msg = towire_warningfmt(NULL, NULL,
|
2020-08-13 19:38:02 +02:00
|
|
|
"Feature %zu requires feature %zu",
|
|
|
|
depender, missing);
|
|
|
|
msg = cryptomsg_encrypt_msg(tmpctx, cs, take(msg));
|
|
|
|
return io_write(conn, msg, tal_count(msg), io_close_cb, NULL);
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:03:47 +02:00
|
|
|
/* We've successfully connected. */
|
2021-03-24 05:37:50 +01:00
|
|
|
if (incoming)
|
|
|
|
peer_connected_in(daemon, conn, id);
|
|
|
|
else
|
|
|
|
connected_out_to_peer(daemon, conn, id);
|
|
|
|
|
|
|
|
if (find_connecting(daemon, id))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"After %s connection on %p, still trying to connect conn %p?",
|
|
|
|
incoming ? "incoming" : "outgoing",
|
|
|
|
conn, find_connecting(daemon, id)->conn);
|
2020-04-02 06:03:47 +02:00
|
|
|
|
2019-06-03 20:15:25 +02:00
|
|
|
/* This contains the per-peer state info; gossipd fills in pps->gs */
|
2022-01-08 14:19:29 +01:00
|
|
|
peer = new_peer(daemon, id, cs, their_features, conn, &subd_fd);
|
|
|
|
/* Only takes over conn if it succeeds. */
|
|
|
|
if (!peer)
|
2018-07-24 08:18:58 +02:00
|
|
|
return io_close(conn);
|
|
|
|
|
2022-01-29 04:33:05 +01:00
|
|
|
/* Tell gossipd it can ask query this new peer for gossip */
|
|
|
|
option_gossip_queries = feature_negotiated(daemon->our_features,
|
|
|
|
their_features,
|
|
|
|
OPT_GOSSIP_QUERIES);
|
|
|
|
msg = towire_gossipd_new_peer(NULL, id, option_gossip_queries);
|
|
|
|
daemon_conn_send(daemon->gossipd, take(msg));
|
2022-01-08 14:24:29 +01:00
|
|
|
|
2022-01-08 14:28:29 +01:00
|
|
|
/* Get ready for streaming gossip from the store */
|
|
|
|
setup_peer_gossip_store(peer, daemon->our_features, their_features);
|
|
|
|
|
2019-06-03 20:14:25 +02:00
|
|
|
/* Create message to tell master peer has connected. */
|
2021-10-12 13:16:37 +02:00
|
|
|
msg = towire_connectd_peer_connected(NULL, id, addr, remote_addr,
|
|
|
|
incoming, their_features);
|
2019-06-03 20:14:25 +02:00
|
|
|
|
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
|
2022-01-29 04:33:05 +01:00
|
|
|
* we have connected, and give the peer fd. */
|
2019-06-03 20:14:25 +02:00
|
|
|
daemon_conn_send(daemon->master, take(msg));
|
2022-01-08 14:19:29 +01:00
|
|
|
daemon_conn_send_fd(daemon->master, subd_fd);
|
2018-08-09 02:25:29 +02:00
|
|
|
|
2022-01-08 14:19:29 +01:00
|
|
|
/*~ Now we set up this connection to read/write from subd */
|
|
|
|
return multiplex_peer_setup(conn, peer);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct pubkey *id_key,
|
2018-08-08 16:09:58 +02:00
|
|
|
const struct wireaddr_internal *addr,
|
2020-04-02 06:03:47 +02:00
|
|
|
struct crypto_state *cs,
|
2022-01-08 14:17:29 +01:00
|
|
|
struct oneshot *timeout,
|
2018-08-08 16:09:58 +02:00
|
|
|
struct daemon *daemon)
|
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
|
|
|
node_id_from_pubkey(&id, id_key);
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(&id, "Connect IN");
|
2020-04-03 02:03:59 +02:00
|
|
|
return peer_exchange_initmsg(conn, daemon, daemon->our_features,
|
2022-01-08 14:17:29 +01:00
|
|
|
cs, &id, addr, timeout, true);
|
2018-08-08 16:09:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:48:12 +02:00
|
|
|
/*~ If the timer goes off, we simply free everything, which hangs up. */
|
|
|
|
static void conn_timeout(struct io_conn *conn)
|
|
|
|
{
|
2020-09-11 09:03:22 +02:00
|
|
|
status_debug("conn timed out");
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
io_close(conn);
|
2020-09-11 08:48:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 07:49:05 +02:00
|
|
|
/*~ So, where are you from? */
|
|
|
|
static bool get_remote_address(struct io_conn *conn,
|
|
|
|
struct wireaddr_internal *addr)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
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) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Failed to get peername for incoming conn: %s",
|
2018-07-24 08:18:58 +02:00
|
|
|
strerror(errno));
|
2021-10-15 07:49:05 +02:00
|
|
|
return false;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *s6 = (void *)&s;
|
2021-10-15 07:49:05 +02:00
|
|
|
addr->itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
wireaddr_from_ipv6(&addr->u.wireaddr,
|
2018-07-24 08:18:58 +02:00
|
|
|
&s6->sin6_addr, ntohs(s6->sin6_port));
|
|
|
|
} else if (s.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s4 = (void *)&s;
|
2021-10-15 07:49:05 +02:00
|
|
|
addr->itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
wireaddr_from_ipv4(&addr->u.wireaddr,
|
2018-07-24 08:18:58 +02:00
|
|
|
&s4->sin_addr, ntohs(s4->sin_port));
|
|
|
|
} else if (s.ss_family == AF_UNIX) {
|
|
|
|
struct sockaddr_un *sun = (void *)&s;
|
2021-10-15 07:49:05 +02:00
|
|
|
addr->itype = ADDR_INTERNAL_SOCKNAME;
|
|
|
|
memcpy(addr->u.sockname, sun->sun_path, sizeof(sun->sun_path));
|
2018-07-24 08:18:58 +02:00
|
|
|
} else {
|
|
|
|
status_broken("Unknown socket type %i for incoming conn",
|
|
|
|
s.ss_family);
|
2021-10-15 07:49:05 +02:00
|
|
|
return false;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
2021-10-15 07:49:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ As so common in C, we need to bundle two args into a callback, so we
|
|
|
|
* allocate a temporary structure to hold them: */
|
|
|
|
struct conn_in {
|
|
|
|
struct wireaddr_internal addr;
|
|
|
|
struct daemon *daemon;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*~ Once we've got a connection in, we set it up here (whether it's via the
|
|
|
|
* websocket proxy, or direct). */
|
|
|
|
static struct io_plan *conn_in(struct io_conn *conn,
|
|
|
|
struct conn_in *conn_in_arg)
|
|
|
|
{
|
|
|
|
struct daemon *daemon = conn_in_arg->daemon;
|
2022-01-08 14:17:29 +01:00
|
|
|
struct oneshot *timeout;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2022-01-08 14:17:29 +01:00
|
|
|
/* If they don't complete handshake in reasonable time, we hang up */
|
|
|
|
timeout = new_reltimer(&daemon->timers, conn,
|
|
|
|
time_from_sec(daemon->timeout_secs),
|
|
|
|
conn_timeout, conn);
|
2020-09-11 08:48:12 +02:00
|
|
|
|
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 */
|
2021-10-15 07:49:05 +02:00
|
|
|
return responder_handshake(notleak(conn), &daemon->mykey,
|
2022-01-08 14:17:29 +01:00
|
|
|
&conn_in_arg->addr, timeout,
|
2018-08-08 16:09:58 +02:00
|
|
|
handshake_in_success, daemon);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 07:49:05 +02:00
|
|
|
/*~ When we get a direct connection in we set up its network address
|
|
|
|
* then call handshake.c to set up the crypto state. */
|
|
|
|
static struct io_plan *connection_in(struct io_conn *conn,
|
|
|
|
struct daemon *daemon)
|
|
|
|
{
|
|
|
|
struct conn_in conn_in_arg;
|
|
|
|
|
|
|
|
if (!get_remote_address(conn, &conn_in_arg.addr))
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
conn_in_arg.daemon = daemon;
|
|
|
|
return conn_in(conn, &conn_in_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ <hello>I speak web socket</hello>.
|
|
|
|
*
|
|
|
|
* Actually that's dumb, websocket (aka rfc6455) looks nothing like that. */
|
|
|
|
static struct io_plan *websocket_connection_in(struct io_conn *conn,
|
|
|
|
struct daemon *daemon)
|
|
|
|
{
|
|
|
|
int childmsg[2], execfail[2];
|
|
|
|
pid_t childpid;
|
|
|
|
int err;
|
|
|
|
struct conn_in conn_in_arg;
|
|
|
|
|
|
|
|
if (!get_remote_address(conn, &conn_in_arg.addr))
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
status_debug("Websocket connection in from %s",
|
|
|
|
type_to_string(tmpctx, struct wireaddr_internal,
|
|
|
|
&conn_in_arg.addr));
|
|
|
|
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, childmsg) != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (pipe(execfail) != 0)
|
|
|
|
goto close_msgfd_fail;
|
|
|
|
|
|
|
|
if (fcntl(execfail[1], F_SETFD, fcntl(execfail[1], F_GETFD)
|
|
|
|
| FD_CLOEXEC) < 0)
|
|
|
|
goto close_execfail_fail;
|
|
|
|
|
|
|
|
childpid = fork();
|
|
|
|
if (childpid < 0)
|
|
|
|
goto close_execfail_fail;
|
|
|
|
|
|
|
|
if (childpid == 0) {
|
|
|
|
close(childmsg[0]);
|
|
|
|
close(execfail[0]);
|
|
|
|
|
|
|
|
/* Attach remote socket to stdin. */
|
|
|
|
if (dup2(io_conn_fd(conn), STDIN_FILENO) == -1)
|
|
|
|
goto child_errno_fail;
|
|
|
|
|
|
|
|
/* Attach our socket to stdout. */
|
|
|
|
if (dup2(childmsg[1], STDOUT_FILENO) == -1)
|
|
|
|
goto child_errno_fail;
|
|
|
|
|
|
|
|
/* Make (fairly!) sure all other fds are closed. */
|
2022-03-16 02:26:41 +01:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
2021-10-15 07:49:05 +02:00
|
|
|
|
|
|
|
/* Tell websocket helper what we read so far. */
|
|
|
|
execlp(daemon->websocket_helper, daemon->websocket_helper,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
child_errno_fail:
|
|
|
|
err = errno;
|
|
|
|
/* Gcc's warn-unused-result fail. */
|
|
|
|
if (write(execfail[1], &err, sizeof(err))) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(childmsg[1]);
|
|
|
|
close(execfail[1]);
|
|
|
|
|
|
|
|
/* Child will close this without writing on successful exec. */
|
|
|
|
if (read(execfail[0], &err, sizeof(err)) == sizeof(err)) {
|
|
|
|
close(execfail[0]);
|
|
|
|
waitpid(childpid, NULL, 0);
|
|
|
|
status_broken("Exec of helper %s failed: %s",
|
|
|
|
daemon->websocket_helper, strerror(err));
|
|
|
|
errno = err;
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(execfail[0]);
|
|
|
|
|
|
|
|
/* New connection actually talks to proxy process. */
|
|
|
|
conn_in_arg.daemon = daemon;
|
|
|
|
io_new_conn(tal_parent(conn), childmsg[0], conn_in, &conn_in_arg);
|
|
|
|
|
|
|
|
/* Abandon original (doesn't close since child has dup'd fd) */
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
close_execfail_fail:
|
|
|
|
close_noerr(execfail[0]);
|
|
|
|
close_noerr(execfail[1]);
|
|
|
|
close_msgfd_fail:
|
|
|
|
close_noerr(childmsg[0]);
|
|
|
|
close_noerr(childmsg[1]);
|
|
|
|
fail:
|
|
|
|
status_broken("Preparation of helper failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct pubkey *key,
|
2018-09-28 05:23:36 +02:00
|
|
|
const struct wireaddr_internal *addr,
|
2020-04-02 06:03:47 +02:00
|
|
|
struct crypto_state *cs,
|
2022-01-08 14:17:29 +01:00
|
|
|
struct oneshot *timeout,
|
2018-09-28 05:23:36 +02:00
|
|
|
struct connecting *connect)
|
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
|
|
|
|
|
|
|
node_id_from_pubkey(&id, key);
|
2018-09-28 05:23:36 +02:00
|
|
|
connect->connstate = "Exchanging init messages";
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(&id, "Connect OUT");
|
2020-04-02 06:04:47 +02:00
|
|
|
return peer_exchange_initmsg(conn, connect->daemon,
|
2020-04-03 02:03:59 +02:00
|
|
|
connect->daemon->our_features,
|
2022-01-08 14:17:29 +01:00
|
|
|
cs, &id, addr, timeout, false);
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect)
|
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
struct pubkey outkey;
|
2022-01-08 14:17:29 +01:00
|
|
|
struct oneshot *timeout;
|
2019-04-08 11:58:32 +02:00
|
|
|
|
|
|
|
/* This shouldn't happen: lightningd should not give invalid ids! */
|
|
|
|
if (!pubkey_from_node_id(&outkey, &connect->id)) {
|
|
|
|
status_broken("Connection out to invalid id %s",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&connect->id));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:48:12 +02:00
|
|
|
/* If they don't complete handshake in reasonable time, hang up */
|
2022-01-08 14:17:29 +01:00
|
|
|
timeout = new_reltimer(&connect->daemon->timers, conn,
|
|
|
|
time_from_sec(connect->daemon->timeout_secs),
|
|
|
|
conn_timeout, conn);
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(&connect->id, "Connected out, starting crypto");
|
2018-09-28 05:23:36 +02:00
|
|
|
|
|
|
|
connect->connstate = "Cryptographic handshake";
|
2019-04-08 11:58:32 +02:00
|
|
|
return initiator_handshake(conn, &connect->daemon->mykey, &outkey,
|
2018-09-28 05:23:36 +02:00
|
|
|
&connect->addrs[connect->addrnum],
|
2022-01-08 14:17:29 +01:00
|
|
|
timeout, handshake_out_success, connect);
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 00:38:00 +01:00
|
|
|
/*~ When we've exhausted all addresses without success, we come here.
|
|
|
|
*
|
|
|
|
* Note that gcc gets upset if we put the PRINTF_FMT at the end like this if
|
|
|
|
* it's an actual function definition, but etags gets confused and ignores the
|
|
|
|
* rest of the file if we put PRINTF_FMT at the front. So we put it at the
|
|
|
|
* end, in a gratuitous declaration.
|
|
|
|
*/
|
|
|
|
static void connect_failed(struct daemon *daemon,
|
|
|
|
const struct node_id *id,
|
|
|
|
u32 seconds_waited,
|
|
|
|
const struct wireaddr_internal *addrhint,
|
2020-01-26 13:52:29 +01:00
|
|
|
errcode_t errcode,
|
2019-11-01 00:38:00 +01:00
|
|
|
const char *errfmt, ...)
|
2020-01-05 18:17:25 +01:00
|
|
|
PRINTF_FMT(6,7);
|
2019-11-01 00:38:00 +01:00
|
|
|
|
|
|
|
static void connect_failed(struct daemon *daemon,
|
|
|
|
const struct node_id *id,
|
|
|
|
u32 seconds_waited,
|
|
|
|
const struct wireaddr_internal *addrhint,
|
2020-01-26 13:52:29 +01:00
|
|
|
errcode_t errcode,
|
2019-11-01 00:38:00 +01:00
|
|
|
const char *errfmt, ...)
|
2018-09-28 05:23:36 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
va_list ap;
|
2020-01-05 18:17:25 +01:00
|
|
|
char *errmsg;
|
2018-09-28 05:23:36 +02:00
|
|
|
u32 wait_seconds;
|
|
|
|
|
|
|
|
va_start(ap, errfmt);
|
2020-01-05 18:17:25 +01:00
|
|
|
errmsg = tal_vfmt(tmpctx, errfmt, ap);
|
2018-09-28 05:23:36 +02:00
|
|
|
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. */
|
2020-08-25 04:16:22 +02:00
|
|
|
msg = towire_connectd_connect_failed(NULL, id, errcode, errmsg,
|
2020-01-05 18:17:25 +01:00
|
|
|
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
|
|
|
|
2020-01-05 18:17:25 +01:00
|
|
|
status_peer_debug(id, "Failed connected out: %s", errmsg);
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 16:03:04 +02:00
|
|
|
/* add errors to error list */
|
|
|
|
void add_errors_to_error_list(struct connecting *connect, const char *error)
|
|
|
|
{
|
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s. ", error);
|
|
|
|
}
|
|
|
|
|
2020-11-17 02:06:24 +01:00
|
|
|
/*~ This is the destructor for the (unsuccessful) outgoing connection. We accumulate
|
2018-09-28 05:24:17 +02:00
|
|
|
* 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?)";
|
|
|
|
}
|
2020-04-14 16:03:04 +02:00
|
|
|
|
|
|
|
add_errors_to_error_list(connect,
|
|
|
|
tal_fmt(tmpctx, "%s: %s: %s",
|
2018-09-28 05:23:36 +02:00
|
|
|
type_to_string(tmpctx, struct wireaddr_internal,
|
|
|
|
&connect->addrs[connect->addrnum]),
|
2020-04-14 16:03:04 +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;
|
2019-11-15 09:44:22 +01:00
|
|
|
case ADDR_INTERNAL_STATICTOR:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect to statictor address");
|
|
|
|
break;
|
2018-09-28 05:23:36 +02:00
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect to forproxy address");
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
2021-10-07 13:01:05 +02:00
|
|
|
/* DNS should have been resolved before */
|
|
|
|
assert(addr->u.wireaddr.type != ADDR_TYPE_DNS);
|
2018-09-28 05:23:36 +02:00
|
|
|
/* If it was a Tor address, we wouldn't be here. */
|
2021-10-01 13:14:46 +02:00
|
|
|
assert(!is_toraddr((char*)addr->u.wireaddr.addr));
|
2018-09-28 05:23:36 +02:00
|
|
|
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:
|
2019-11-15 09:44:22 +01:00
|
|
|
case ADDR_INTERNAL_STATICTOR:
|
2018-09-28 05:23:36 +02:00
|
|
|
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;
|
2021-08-18 12:52:21 +02:00
|
|
|
bool use_proxy = connect->daemon->always_use_proxy;
|
2018-09-28 05:23:36 +02:00
|
|
|
const struct wireaddr_internal *addr = &connect->addrs[connect->addrnum];
|
2021-01-28 00:36:49 +01:00
|
|
|
struct io_conn *conn;
|
2021-10-19 11:32:52 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
|
2021-10-07 13:01:05 +02:00
|
|
|
bool use_dns = connect->daemon->use_dns;
|
|
|
|
struct addrinfo hints, *ais, *aii;
|
|
|
|
struct wireaddr_internal addrhint;
|
|
|
|
int gai_err;
|
|
|
|
struct sockaddr_in *sa4;
|
|
|
|
struct sockaddr_in6 *sa6;
|
2021-10-19 11:32:52 +02:00
|
|
|
#endif
|
2018-09-28 05:23:36 +02:00
|
|
|
|
2020-11-17 02:06:24 +01:00
|
|
|
/* In case we fail without a connection, make destroy_io_conn happy */
|
|
|
|
connect->conn = NULL;
|
|
|
|
|
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,
|
2020-01-05 18:17:25 +01:00
|
|
|
connect->addrhint, CONNECT_ALL_ADDRESSES_FAILED,
|
2021-12-08 12:26:25 +01:00
|
|
|
"All addresses failed: %s",
|
|
|
|
connect->errors);
|
2018-09-28 05:23:36 +02:00
|
|
|
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");
|
2019-11-15 09:44:22 +01:00
|
|
|
case ADDR_INTERNAL_STATICTOR:
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Can't connect STATICTOR");
|
2018-09-28 05:23:36 +02:00
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
use_proxy = true;
|
|
|
|
break;
|
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
switch (addr->u.wireaddr.type) {
|
2021-11-10 01:27:41 +01:00
|
|
|
case ADDR_TYPE_TOR_V2_REMOVED:
|
|
|
|
af = -1;
|
|
|
|
break;
|
2018-09-28 05:23:36 +02:00
|
|
|
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;
|
2021-10-01 13:47:29 +02:00
|
|
|
case ADDR_TYPE_DNS:
|
2021-10-19 11:32:52 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
|
2021-10-07 13:01:05 +02:00
|
|
|
if (use_proxy) /* hand it to the proxy */
|
|
|
|
break;
|
2021-12-08 12:26:25 +01:00
|
|
|
if (!use_dns) { /* ignore DNS when we can't use it */
|
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: dns disabled. ",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
addr));
|
2021-10-07 13:01:05 +02:00
|
|
|
goto next;
|
2021-12-08 12:26:25 +01:00
|
|
|
}
|
2021-10-07 13:01:05 +02:00
|
|
|
/* Resolve with getaddrinfo */
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
|
|
|
gai_err = getaddrinfo((char *)addr->u.wireaddr.addr,
|
|
|
|
tal_fmt(tmpctx, "%d",
|
|
|
|
addr->u.wireaddr.port),
|
|
|
|
&hints, &ais);
|
|
|
|
if (gai_err != 0) {
|
2021-12-08 12:26:25 +01:00
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: getaddrinfo error '%s'. ",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
addr),
|
|
|
|
gai_strerror(gai_err));
|
2021-10-07 13:01:05 +02:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
/* create new addrhints on-the-fly per result ... */
|
|
|
|
for (aii = ais; aii; aii = aii->ai_next) {
|
|
|
|
addrhint.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
if (aii->ai_family == AF_INET) {
|
|
|
|
sa4 = (struct sockaddr_in *) aii->ai_addr;
|
|
|
|
wireaddr_from_ipv4(&addrhint.u.wireaddr,
|
|
|
|
&sa4->sin_addr,
|
|
|
|
addr->u.wireaddr.port);
|
|
|
|
} else if (aii->ai_family == AF_INET6) {
|
|
|
|
sa6 = (struct sockaddr_in6 *) aii->ai_addr;
|
|
|
|
wireaddr_from_ipv6(&addrhint.u.wireaddr,
|
|
|
|
&sa6->sin6_addr,
|
|
|
|
addr->u.wireaddr.port);
|
|
|
|
} else {
|
|
|
|
/* skip unsupported ai_family */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tal_arr_expand(&connect->addrs, addrhint);
|
|
|
|
/* don't forget to update convenience pointer */
|
|
|
|
addr = &connect->addrs[connect->addrnum];
|
|
|
|
}
|
|
|
|
freeaddrinfo(ais);
|
2021-10-19 11:32:52 +02:00
|
|
|
#endif
|
2021-12-08 12:26:25 +01:00
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: EXPERIMENTAL_FEATURES needed. ",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
addr));
|
2021-10-07 13:01:05 +02:00
|
|
|
goto next;
|
2021-10-15 07:48:44 +02:00
|
|
|
case ADDR_TYPE_WEBSOCKET:
|
|
|
|
af = -1;
|
|
|
|
break;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have to use proxy but we don't have one, we fail. */
|
|
|
|
if (use_proxy) {
|
|
|
|
if (!connect->daemon->proxyaddr) {
|
2021-10-15 07:48:44 +02:00
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: need a proxy. ",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
addr));
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
af = connect->daemon->proxyaddr->ai_family;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (af == -1) {
|
2021-10-15 07:48:44 +02:00
|
|
|
tal_append_fmt(&connect->errors,
|
|
|
|
"%s: not supported. ",
|
|
|
|
type_to_string(tmpctx, struct wireaddr_internal,
|
|
|
|
addr));
|
|
|
|
goto next;
|
|
|
|
}
|
2018-09-28 05:23:36 +02:00
|
|
|
|
2021-10-15 07:48:44 +02:00
|
|
|
fd = socket(af, SOCK_STREAM, 0);
|
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));
|
2021-10-15 07:48:44 +02:00
|
|
|
goto next;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2021-01-28 00:36:49 +01:00
|
|
|
conn = io_new_conn(connect, fd, conn_proxy_init, connect);
|
2018-09-28 05:23:36 +02:00
|
|
|
else
|
2021-01-28 00:36:49 +01:00
|
|
|
conn = io_new_conn(connect, fd, conn_init, connect);
|
|
|
|
|
|
|
|
/* Careful! io_new_conn can fail (immediate connect() failure), and
|
|
|
|
* that frees connect. */
|
|
|
|
if (conn)
|
|
|
|
connect->conn = conn;
|
2021-10-15 07:48:44 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
next:
|
|
|
|
/* This causes very limited recursion. */
|
|
|
|
connect->addrnum++;
|
|
|
|
try_connect_one_addr(connect);
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:25:11 +01:00
|
|
|
/*~ Sometimes it's nice to have an explicit enum instead of a bool to make
|
|
|
|
* arguments clearer: it kind of hacks around C's lack of naming formal
|
|
|
|
* arguments in callers (e.g. in Python we'd simply call func(websocket=False)).
|
|
|
|
*/
|
|
|
|
enum is_websocket {
|
|
|
|
NORMAL_SOCKET,
|
|
|
|
WEBSOCKET,
|
|
|
|
};
|
|
|
|
|
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 {
|
2022-03-03 11:25:11 +01:00
|
|
|
/* This is usually an IPv4/v6 address, but we also support local
|
|
|
|
* domain sockets (i.e. filesystem) */
|
|
|
|
struct wireaddr_internal wi;
|
|
|
|
/* The actual fd, ready to listen() on */
|
2018-09-28 05:23:36 +02:00
|
|
|
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;
|
2022-03-03 11:25:11 +01:00
|
|
|
/* Is this a websocket? */
|
|
|
|
enum is_websocket is_websocket;
|
2018-09-28 05:23:36 +02:00
|
|
|
};
|
|
|
|
|
2022-03-04 07:10:53 +01:00
|
|
|
static struct listen_fd *listen_fd_new(const tal_t *ctx,
|
|
|
|
const struct wireaddr_internal *wi,
|
|
|
|
int fd, bool mayfail,
|
|
|
|
enum is_websocket is_websocket)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2022-03-04 07:10:53 +01:00
|
|
|
struct listen_fd *l = tal(ctx, struct listen_fd);
|
|
|
|
|
|
|
|
l->wi = *wi;
|
|
|
|
l->fd = fd;
|
|
|
|
l->mayfail = mayfail;
|
|
|
|
l->is_websocket = is_websocket;
|
|
|
|
return l;
|
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.
|
|
|
|
*
|
2022-03-03 11:24:11 +01:00
|
|
|
* Note that it's generally an antipattern to have a function which
|
2022-03-04 07:10:53 +01:00
|
|
|
* returns an allocated object without an explicit tal ctx so the
|
|
|
|
* caller is aware. */
|
|
|
|
static struct listen_fd *make_listen_fd(const tal_t *ctx,
|
|
|
|
const struct wireaddr_internal *wi,
|
|
|
|
int domain, void *addr, socklen_t len,
|
|
|
|
bool listen_mayfail,
|
|
|
|
enum is_websocket is_websocket,
|
|
|
|
char **errstr)
|
2018-09-28 05:23:36 +02:00
|
|
|
{
|
|
|
|
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) {
|
2022-03-04 07:10:53 +01:00
|
|
|
*errstr = tal_fmt(ctx, "Failed to create socket for %s%s: %s",
|
|
|
|
is_websocket ? "websocket " : "",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
wi),
|
|
|
|
strerror(errno));
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Failed to create %u socket: %s",
|
2018-09-28 05:23:36 +02:00
|
|
|
domain, strerror(errno));
|
2022-03-04 07:10:53 +01:00
|
|
|
return NULL;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-03-04 07:10:53 +01:00
|
|
|
*errstr = tal_fmt(ctx, "Failed to bind socket for %s%s: %s",
|
|
|
|
is_websocket ? "websocket " : "",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
wi),
|
|
|
|
strerror(errno));
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Failed to create %u socket: %s",
|
2018-09-28 05:23:52 +02:00
|
|
|
domain, strerror(errno));
|
|
|
|
goto fail;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 07:10:53 +01:00
|
|
|
*errstr = NULL;
|
|
|
|
status_debug("Created %slistener on %s",
|
|
|
|
is_websocket ? "websocket ": "",
|
|
|
|
type_to_string(tmpctx, struct wireaddr_internal, wi));
|
|
|
|
return listen_fd_new(ctx, wi, fd, listen_mayfail, is_websocket);
|
2018-09-28 05:23:36 +02:00
|
|
|
|
|
|
|
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);
|
2022-03-04 07:10:53 +01:00
|
|
|
return NULL;
|
2018-09-28 05:23:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:24:11 +01:00
|
|
|
/* Return true if it created socket successfully. If errstr is non-NULL,
|
|
|
|
* allocate off ctx if return false, otherwise it implies it's OK to fail. */
|
2022-03-04 07:10:53 +01:00
|
|
|
static struct listen_fd *handle_wireaddr_listen(const tal_t *ctx,
|
|
|
|
const struct wireaddr_internal *wi,
|
|
|
|
bool listen_mayfail,
|
|
|
|
enum is_websocket is_websocket,
|
|
|
|
char **errstr)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct sockaddr_in6 addr6;
|
2022-03-03 11:24:11 +01:00
|
|
|
const struct wireaddr *wireaddr;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2022-03-03 11:24:11 +01:00
|
|
|
assert(wi->itype == ADDR_INTERNAL_WIREADDR);
|
|
|
|
wireaddr = &wi->u.wireaddr;
|
|
|
|
|
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 */
|
2022-03-04 07:10:53 +01:00
|
|
|
return make_listen_fd(ctx, wi, AF_INET, &addr, sizeof(addr),
|
|
|
|
listen_mayfail, is_websocket, errstr);
|
2018-07-24 08:18:58 +02:00
|
|
|
case ADDR_TYPE_IPV6:
|
|
|
|
wireaddr_to_ipv6(wireaddr, &addr6);
|
2022-03-04 07:10:53 +01:00
|
|
|
return make_listen_fd(ctx, wi, AF_INET6, &addr6, sizeof(addr6),
|
|
|
|
listen_mayfail, is_websocket, errstr);
|
2021-10-15 07:48:44 +02:00
|
|
|
/* Handle specially by callers. */
|
|
|
|
case ADDR_TYPE_WEBSOCKET:
|
2021-11-10 01:27:41 +01:00
|
|
|
case ADDR_TYPE_TOR_V2_REMOVED:
|
2018-07-24 08:18:58 +02:00
|
|
|
case ADDR_TYPE_TOR_V3:
|
2021-10-01 13:47:29 +02:00
|
|
|
case ADDR_TYPE_DNS:
|
2018-07-24 08:18:58 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:58:41 +01:00
|
|
|
static void add_announceable(struct wireaddr **announceable,
|
|
|
|
const struct wireaddr *addr)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2022-03-04 07:10:53 +01: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. */
|
2022-02-23 17:58:41 +01:00
|
|
|
tal_arr_expand(announceable, *addr);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:26:11 +01:00
|
|
|
/* We need to have a bound address we can tell Tor to connect to */
|
|
|
|
static const struct wireaddr *
|
2022-03-04 07:10:53 +01:00
|
|
|
find_local_address(const struct listen_fd **listen_fds)
|
2022-03-03 11:26:11 +01:00
|
|
|
{
|
2022-03-04 07:10:53 +01:00
|
|
|
for (size_t i = 0; i < tal_count(listen_fds); i++) {
|
|
|
|
if (listen_fds[i]->wi.itype != ADDR_INTERNAL_WIREADDR)
|
2022-03-03 11:26:11 +01:00
|
|
|
continue;
|
2022-03-04 07:10:53 +01:00
|
|
|
if (listen_fds[i]->wi.u.wireaddr.type != ADDR_TYPE_IPV4
|
|
|
|
&& listen_fds[i]->wi.u.wireaddr.type != ADDR_TYPE_IPV6)
|
2022-03-03 11:26:11 +01:00
|
|
|
continue;
|
2022-03-04 07:10:53 +01:00
|
|
|
return &listen_fds[i]->wi.u.wireaddr;
|
2022-03-03 11:26:11 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool want_tor(const struct wireaddr_internal *proposed_wireaddr)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(proposed_wireaddr); i++) {
|
|
|
|
if (proposed_wireaddr[i].itype == ADDR_INTERNAL_STATICTOR
|
|
|
|
|| proposed_wireaddr[i].itype == ADDR_INTERNAL_AUTOTOR)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2022-02-23 17:58:41 +01:00
|
|
|
* This routine sorts out the mess: it populates the *announceable array,
|
2018-09-28 05:24:17 +02:00
|
|
|
* and returns the addresses we bound to (by convention, return is allocated
|
|
|
|
* off `ctx` argument).
|
2022-03-04 07:10:53 +01:00
|
|
|
*
|
|
|
|
* Note the important difference between returning a zero-element array, and
|
|
|
|
* returning NULL! The latter means failure here, the former simply means
|
|
|
|
* we don't want to listen to anything.
|
2018-09-28 05:24:17 +02:00
|
|
|
*/
|
2022-03-04 07:10:53 +01:00
|
|
|
static const struct listen_fd **
|
|
|
|
setup_listeners(const tal_t *ctx,
|
|
|
|
struct daemon *daemon,
|
|
|
|
/* The proposed address. */
|
|
|
|
const struct wireaddr_internal *proposed_wireaddr,
|
|
|
|
/* For each one, listen, announce or both */
|
|
|
|
const enum addr_listen_announce *proposed_listen_announce,
|
|
|
|
const char *tor_password,
|
2022-02-23 17:58:41 +01:00
|
|
|
struct wireaddr **announceable,
|
2022-03-04 07:10:53 +01:00
|
|
|
char **errstr)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_un addrun;
|
2022-03-04 07:10:53 +01:00
|
|
|
const struct listen_fd **listen_fds, *lfd;
|
2021-11-10 01:27:42 +01:00
|
|
|
const char *blob = NULL;
|
2019-11-15 09:44:22 +01:00
|
|
|
struct secret random;
|
|
|
|
struct pubkey pb;
|
|
|
|
struct wireaddr *toraddr;
|
2022-03-04 07:10:53 +01:00
|
|
|
const struct wireaddr *localaddr;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Start with empty arrays, for tal_arr_expand() */
|
2022-03-04 07:10:53 +01:00
|
|
|
listen_fds = tal_arr(ctx, const struct listen_fd *, 0);
|
2022-02-23 17:58:41 +01:00
|
|
|
*announceable = 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);
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable, &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);
|
|
|
|
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));
|
2019-04-08 03:53:23 +02:00
|
|
|
/* Remove any existing one. */
|
|
|
|
unlink(wa.u.sockname);
|
2022-03-04 07:10:53 +01:00
|
|
|
lfd = make_listen_fd(ctx, &wa, AF_UNIX,
|
|
|
|
&addrun, sizeof(addrun),
|
|
|
|
false, NORMAL_SOCKET,
|
|
|
|
errstr);
|
2022-03-03 11:24:11 +01:00
|
|
|
/* Don't bother freeing here; we'll exit */
|
2022-03-04 07:10:53 +01:00
|
|
|
if (!lfd)
|
2022-03-03 11:24:11 +01:00
|
|
|
return NULL;
|
2019-04-08 03:53:23 +02:00
|
|
|
/* We don't announce socket names, though we allow
|
|
|
|
* them to lazily specify --addr=/socket. */
|
2022-03-04 07:10:53 +01:00
|
|
|
tal_arr_expand(&listen_fds, tal_steal(listen_fds, lfd));
|
2018-07-24 08:18:58 +02:00
|
|
|
continue;
|
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
/* We handle these after we have all bindings. */
|
|
|
|
continue;
|
2019-11-15 09:44:22 +01:00
|
|
|
case ADDR_INTERNAL_STATICTOR:
|
|
|
|
/* 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
|
|
|
|
2022-03-04 07:10:53 +01:00
|
|
|
/* This may fail due to no IPv6 support. */
|
|
|
|
lfd = handle_wireaddr_listen(ctx, &wa, false,
|
|
|
|
NORMAL_SOCKET, errstr);
|
|
|
|
if (lfd) {
|
|
|
|
tal_arr_expand(&listen_fds,
|
|
|
|
tal_steal(listen_fds, lfd));
|
2018-07-24 08:18:58 +02:00
|
|
|
if (announce
|
|
|
|
&& public_address(daemon, &wa.u.wireaddr))
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable,
|
|
|
|
&wa.u.wireaddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
2022-03-04 07:10:53 +01:00
|
|
|
ipv6_ok = (lfd != NULL);
|
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));
|
2022-03-04 07:10:53 +01:00
|
|
|
/* This listen *may* fail, as long as IPv6 succeeds! */
|
|
|
|
lfd = handle_wireaddr_listen(ctx, &wa, ipv6_ok,
|
|
|
|
NORMAL_SOCKET, errstr);
|
|
|
|
if (lfd) {
|
|
|
|
tal_arr_expand(&listen_fds,
|
|
|
|
tal_steal(listen_fds, lfd));
|
2018-07-24 08:18:58 +02:00
|
|
|
if (announce
|
|
|
|
&& public_address(daemon, &wa.u.wireaddr))
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable,
|
2018-08-09 02:25:29 +02:00
|
|
|
&wa.u.wireaddr);
|
2022-03-03 11:24:11 +01:00
|
|
|
} else if (!ipv6_ok) {
|
|
|
|
/* Both failed, return now, errstr set. */
|
|
|
|
return NULL;
|
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:
|
2022-03-04 07:10:53 +01:00
|
|
|
lfd = handle_wireaddr_listen(ctx, &wa, false,
|
|
|
|
NORMAL_SOCKET, errstr);
|
|
|
|
if (!lfd)
|
2022-03-03 11:24:11 +01:00
|
|
|
return NULL;
|
2022-03-04 07:10:53 +01:00
|
|
|
tal_arr_expand(&listen_fds, tal_steal(listen_fds, lfd));
|
2018-07-24 08:18:58 +02:00
|
|
|
if (announce && public_address(daemon, &wa.u.wireaddr))
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable, &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
|
|
|
}
|
|
|
|
|
2022-03-03 11:26:11 +01:00
|
|
|
/* Make sure we have at least one non-websocket address to send to,
|
|
|
|
* for Tor */
|
2022-03-04 07:10:53 +01:00
|
|
|
localaddr = find_local_address(listen_fds);
|
2022-03-03 11:26:11 +01:00
|
|
|
if (want_tor(proposed_wireaddr) && !localaddr) {
|
|
|
|
*errstr = "Need to bind at least one local address,"
|
|
|
|
" to send Tor connections to";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-15 07:49:05 +02:00
|
|
|
/* If we want websockets to match IPv4/v6, set it up now. */
|
|
|
|
if (daemon->websocket_port) {
|
|
|
|
bool announced_some = false;
|
2022-03-03 11:24:11 +01:00
|
|
|
struct wireaddr_internal addr;
|
2022-03-04 07:10:53 +01:00
|
|
|
/* Only consider bindings added before this! */
|
|
|
|
size_t num_nonws_listens = tal_count(listen_fds);
|
2021-10-15 07:49:05 +02:00
|
|
|
|
2022-03-03 11:24:11 +01:00
|
|
|
/* If not overriden below, this is the default. */
|
2022-03-04 07:10:53 +01:00
|
|
|
*errstr = "Cannot listen on websocket: not listening on any IPv4/6 addresses";
|
|
|
|
for (size_t i = 0; i < num_nonws_listens; i++) {
|
2021-10-15 07:49:05 +02:00
|
|
|
/* Ignore UNIX sockets */
|
2022-03-04 07:10:53 +01:00
|
|
|
if (listen_fds[i]->wi.itype != ADDR_INTERNAL_WIREADDR)
|
2021-10-15 07:49:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Override with websocket port */
|
2022-03-04 07:10:53 +01:00
|
|
|
addr = listen_fds[i]->wi;
|
2022-03-03 11:24:11 +01:00
|
|
|
addr.u.wireaddr.port = daemon->websocket_port;
|
2022-03-04 07:10:53 +01:00
|
|
|
|
|
|
|
/* We set mayfail on all but the first websocket;
|
|
|
|
* it's quite common to have multple overlapping
|
|
|
|
* addresses. */
|
|
|
|
lfd = handle_wireaddr_listen(ctx, &addr,
|
|
|
|
announced_some,
|
|
|
|
WEBSOCKET, errstr);
|
|
|
|
if (!lfd)
|
|
|
|
continue;
|
|
|
|
|
2022-03-04 07:10:57 +01:00
|
|
|
if (!announced_some) {
|
|
|
|
/* BOLT-websocket #7:
|
|
|
|
* - MUST NOT add a `type 6` address unless
|
|
|
|
* there is also at least one address of
|
|
|
|
* different type.
|
|
|
|
*/
|
2022-02-23 17:58:41 +01:00
|
|
|
if (tal_count(*announceable) != 0) {
|
2022-03-04 07:10:57 +01:00
|
|
|
wireaddr_from_websocket(&addr.u.wireaddr,
|
|
|
|
daemon->websocket_port);
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable,
|
2022-03-04 07:10:57 +01:00
|
|
|
&addr.u.wireaddr);
|
|
|
|
} else {
|
|
|
|
status_unusual("Bound to websocket %s,"
|
|
|
|
" but we cannot announce"
|
|
|
|
" the websocket as we don't"
|
|
|
|
" announce anything else!",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
&addr));
|
|
|
|
}
|
|
|
|
announced_some = true;
|
|
|
|
}
|
|
|
|
|
2022-03-04 07:10:53 +01:00
|
|
|
tal_arr_expand(&listen_fds, tal_steal(listen_fds, lfd));
|
2021-10-15 07:49:05 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 07:10:57 +01:00
|
|
|
/* If none of those was possible, it's a configuration error? */
|
|
|
|
if (tal_count(listen_fds) == num_nonws_listens)
|
2022-03-03 11:24:11 +01:00
|
|
|
return NULL;
|
2021-10-15 07:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Websocket over Tor (difficult for autotor, since we need
|
|
|
|
* to use the same onion addr!) */
|
|
|
|
|
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;
|
2019-11-15 09:44:22 +01:00
|
|
|
toraddr = tor_autoservice(tmpctx,
|
|
|
|
&proposed_wireaddr[i],
|
|
|
|
tor_password,
|
2022-03-03 11:26:11 +01:00
|
|
|
localaddr,
|
2019-11-15 09:44:22 +01:00
|
|
|
daemon->use_v3_autotor);
|
|
|
|
|
2018-12-19 10:01:40 +01:00
|
|
|
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
|
|
|
|
continue;
|
|
|
|
};
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable, toraddr);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 09:44:22 +01:00
|
|
|
/* Now we have bindings, set up any Tor static addresses: we will point
|
|
|
|
* it at the first bound IPv4 or IPv6 address we have. */
|
|
|
|
for (size_t i = 0; i < tal_count(proposed_wireaddr); i++) {
|
|
|
|
if (!(proposed_listen_announce[i] & ADDR_LISTEN))
|
|
|
|
continue;
|
|
|
|
if (proposed_wireaddr[i].itype != ADDR_INTERNAL_STATICTOR)
|
|
|
|
continue;
|
|
|
|
blob = proposed_wireaddr[i].u.torservice.blob;
|
|
|
|
|
|
|
|
if (tal_strreg(tmpctx, (char *)proposed_wireaddr[i].u.torservice.blob, STATIC_TOR_MAGIC_STRING)) {
|
|
|
|
if (pubkey_from_node_id(&pb, &daemon->id)) {
|
|
|
|
if (sodium_mlock(&random, sizeof(random)) != 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not lock the random prf key memory.");
|
|
|
|
randombytes_buf((void * const)&random, 32);
|
|
|
|
/* generate static tor node address, take first 32 bytes from secret of node_id plus 32 random bytes from sodiom */
|
|
|
|
struct sha256 sha;
|
2020-04-03 05:21:22 +02:00
|
|
|
struct secret ss;
|
|
|
|
|
|
|
|
ecdh(&pb, &ss);
|
2019-11-15 09:44:22 +01:00
|
|
|
/* let's sha, that will clear ctx of hsm data */
|
2020-04-03 05:21:22 +02:00
|
|
|
sha256(&sha, &ss, 32);
|
2019-11-15 09:44:22 +01:00
|
|
|
/* even if it's a secret pub derived, tor shall see only the single sha */
|
|
|
|
memcpy((void *)&blob[0], &sha, 32);
|
|
|
|
memcpy((void *)&blob[32], &random, 32);
|
|
|
|
/* clear our temp buffer, don't leak by extern libs core-dumps, our blob we/tal handle later */
|
|
|
|
sodium_munlock(&random, sizeof(random));
|
|
|
|
|
|
|
|
} else status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not get the pub of our node id from hsm");
|
|
|
|
}
|
|
|
|
|
|
|
|
toraddr = tor_fixed_service(tmpctx,
|
|
|
|
&proposed_wireaddr[i],
|
|
|
|
tor_password,
|
|
|
|
blob,
|
2022-03-03 11:26:11 +01:00
|
|
|
localaddr,
|
2019-11-15 09:44:22 +01:00
|
|
|
0);
|
|
|
|
/* get rid of blob data on our side of tor and add jitter */
|
|
|
|
randombytes_buf((void * const)proposed_wireaddr[i].u.torservice.blob, TOR_V3_BLOBLEN);
|
|
|
|
|
|
|
|
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
|
|
|
|
continue;
|
|
|
|
};
|
2022-02-23 17:58:41 +01:00
|
|
|
add_announceable(announceable, toraddr);
|
2019-11-15 09:44:22 +01:00
|
|
|
}
|
2020-03-30 11:22:12 +02:00
|
|
|
|
|
|
|
/*~ The spec used to ban more than one address of each type, but
|
|
|
|
* nobody could remember exactly why, so now that's allowed. */
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The origin node:
|
|
|
|
*...
|
|
|
|
* - MUST place address descriptors in ascending order.
|
|
|
|
*/
|
2022-02-23 17:58:41 +01:00
|
|
|
asort(*announceable, tal_count(*announceable), wireaddr_cmp_type, NULL);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2022-03-03 11:24:11 +01:00
|
|
|
*errstr = NULL;
|
2022-03-04 07:10:53 +01:00
|
|
|
return listen_fds;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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. */
|
2021-12-28 00:23:09 +01:00
|
|
|
static void connect_init(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;
|
2022-02-23 17:58:41 +01:00
|
|
|
struct wireaddr *announceable;
|
2018-08-09 02:25:29 +02:00
|
|
|
char *tor_password;
|
2022-01-08 14:28:29 +01:00
|
|
|
bool dev_fast_gossip;
|
2022-01-08 14:25:29 +01:00
|
|
|
bool dev_disconnect;
|
2022-03-03 11:24:11 +01:00
|
|
|
char *errstr;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Fields which require allocation are allocated off daemon */
|
2020-08-25 04:16:22 +02:00
|
|
|
if (!fromwire_connectd_init(
|
2018-07-24 08:18:58 +02:00
|
|
|
daemon, msg,
|
2019-11-28 12:17:57 +01:00
|
|
|
&chainparams,
|
2020-04-03 02:03:59 +02:00
|
|
|
&daemon->our_features,
|
2018-12-03 00:06:06 +01:00
|
|
|
&daemon->id,
|
|
|
|
&proposed_wireaddr,
|
2018-08-09 02:25:29 +02:00
|
|
|
&proposed_listen_announce,
|
2021-08-18 12:52:21 +02:00
|
|
|
&proxyaddr, &daemon->always_use_proxy,
|
2018-07-24 08:18:58 +02:00
|
|
|
&daemon->dev_allow_localhost, &daemon->use_dns,
|
2019-09-25 13:05:14 +02:00
|
|
|
&tor_password,
|
2020-09-11 08:48:12 +02:00
|
|
|
&daemon->use_v3_autotor,
|
2021-10-15 07:49:05 +02:00
|
|
|
&daemon->timeout_secs,
|
|
|
|
&daemon->websocket_helper,
|
2022-01-08 14:25:29 +01:00
|
|
|
&daemon->websocket_port,
|
2022-01-08 14:28:29 +01:00
|
|
|
&dev_fast_gossip,
|
2022-01-08 14:25:29 +01:00
|
|
|
&dev_disconnect)) {
|
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!). */
|
2020-08-25 04:16:22 +02:00
|
|
|
master_badmsg(WIRE_CONNECTD_INIT, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 14:28:29 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
/*~ Clearly mark this as a developer-only flag! */
|
|
|
|
daemon->dev_fast_gossip = dev_fast_gossip;
|
|
|
|
#endif
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!pubkey_from_node_id(&daemon->mykey, &daemon->id))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Invalid id for me %s",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&daemon->id));
|
|
|
|
|
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) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Proxy address: %s",
|
2018-07-24 08:18:58 +02:00
|
|
|
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)) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Broken DNS resolver detected, will check for "
|
2018-07-24 08:18:58 +02:00
|
|
|
"dummy replies");
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/* Figure out our addresses. */
|
2022-03-04 07:10:53 +01:00
|
|
|
daemon->listen_fds = setup_listeners(daemon, daemon,
|
|
|
|
proposed_wireaddr,
|
|
|
|
proposed_listen_announce,
|
|
|
|
tor_password,
|
2022-02-23 17:58:41 +01:00
|
|
|
&announceable,
|
2022-03-04 07:10:53 +01:00
|
|
|
&errstr);
|
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);
|
|
|
|
|
2022-03-04 07:10:53 +01:00
|
|
|
/* Create binding array to send to lightningd */
|
|
|
|
binding = tal_arr(tmpctx, struct wireaddr_internal, 0);
|
|
|
|
for (size_t i = 0; i < tal_count(daemon->listen_fds); i++) {
|
|
|
|
/* FIXME: Tell it about websockets! */
|
|
|
|
if (daemon->listen_fds[i]->is_websocket)
|
|
|
|
continue;
|
|
|
|
tal_arr_expand(&binding, daemon->listen_fds[i]->wi);
|
|
|
|
}
|
|
|
|
|
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,
|
2020-08-25 04:16:22 +02:00
|
|
|
take(towire_connectd_init_reply(NULL,
|
2022-03-03 11:24:11 +01:00
|
|
|
binding,
|
2022-02-23 17:58:41 +01:00
|
|
|
announceable,
|
2022-03-03 11:24:11 +01:00
|
|
|
errstr)));
|
2022-03-04 07:10:53 +01:00
|
|
|
/*~ Who cares about a little once-off memory leak? Turns out we do!
|
|
|
|
* We have a memory leak checker which scans for allocated memory
|
|
|
|
* with no pointers to it (a tell-tale leak sign, though with tal it's
|
|
|
|
* not always a real problem), and this would (did!) trigger it. */
|
2022-02-23 17:58:41 +01:00
|
|
|
tal_free(announceable);
|
2022-03-04 07:10:53 +01:00
|
|
|
|
2022-01-08 14:25:29 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
if (dev_disconnect)
|
2022-01-29 04:33:05 +01:00
|
|
|
dev_disconnect_init(5);
|
2022-01-08 14:25:29 +01:00
|
|
|
#endif
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 11:25:11 +01:00
|
|
|
/* Returning functions in C is ugly! */
|
|
|
|
static struct io_plan *(*get_in_cb(enum is_websocket is_websocket))(struct io_conn *, struct daemon *)
|
|
|
|
|
|
|
|
{
|
|
|
|
/*~ This switch and fall pattern serves a specific purpose:
|
|
|
|
* gcc will warn if we don't handle every case! */
|
|
|
|
switch (is_websocket) {
|
|
|
|
case WEBSOCKET:
|
|
|
|
return websocket_connection_in;
|
|
|
|
case NORMAL_SOCKET:
|
|
|
|
return connection_in;
|
|
|
|
}
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Invalid is_websocket %u", is_websocket);
|
|
|
|
}
|
|
|
|
|
2018-09-28 05:24:17 +02:00
|
|
|
/*~ lightningd tells us to go! */
|
2021-12-28 00:23:09 +01:00
|
|
|
static void connect_activate(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;
|
2022-03-04 07:10:57 +01:00
|
|
|
char *errmsg = NULL;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2020-08-25 04:16:22 +02:00
|
|
|
if (!fromwire_connectd_activate(msg, &do_listen))
|
|
|
|
master_badmsg(WIRE_CONNECTD_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++) {
|
2022-03-04 07:10:53 +01:00
|
|
|
if (listen(daemon->listen_fds[i]->fd, 64) != 0) {
|
|
|
|
if (daemon->listen_fds[i]->mayfail)
|
2018-08-06 06:57:30 +02:00
|
|
|
continue;
|
2022-03-04 07:10:57 +01:00
|
|
|
errmsg = tal_fmt(tmpctx,
|
|
|
|
"Failed to listen on socket %s: %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct wireaddr_internal,
|
|
|
|
&daemon->listen_fds[i]->wi),
|
|
|
|
strerror(errno));
|
|
|
|
break;
|
2018-08-06 06:57:30 +02:00
|
|
|
}
|
2018-11-22 03:17:29 +01:00
|
|
|
notleak(io_new_listener(daemon,
|
2022-03-04 07:10:53 +01:00
|
|
|
daemon->listen_fds[i]->fd,
|
2022-03-03 11:25:11 +01:00
|
|
|
get_in_cb(daemon->listen_fds[i]
|
2022-03-04 07:10:53 +01:00
|
|
|
->is_websocket),
|
2021-10-15 07:49:05 +02:00
|
|
|
daemon));
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 11:25:11 +01: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,
|
2022-03-04 07:10:57 +01:00
|
|
|
take(towire_connectd_activate_reply(NULL, errmsg)));
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 18:32:56 +02:00
|
|
|
/* BOLT #10:
|
|
|
|
*
|
|
|
|
* The DNS seed:
|
|
|
|
* ...
|
|
|
|
* - upon receiving a _node_ query:
|
|
|
|
* - MUST select the record matching the `node_id`, if any, AND return all
|
|
|
|
* addresses associated with that node.
|
|
|
|
*/
|
2019-09-02 18:14:34 +02:00
|
|
|
static const char **seednames(const tal_t *ctx, const struct node_id *id)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
|
|
|
char bech32[100];
|
|
|
|
u5 *data = tal_arr(ctx, u5, 0);
|
2019-09-02 18:14:34 +02:00
|
|
|
const char **seednames = tal_arr(ctx, const char *, 0);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
bech32_push_bits(&data, id->k, ARRAY_SIZE(id->k)*8);
|
2021-06-09 01:55:46 +02:00
|
|
|
bech32_encode(bech32, "ln", data, tal_count(data), sizeof(bech32),
|
|
|
|
BECH32_ENCODING_BECH32);
|
2019-09-02 18:14:34 +02:00
|
|
|
/* This is cdecker's seed */
|
|
|
|
tal_arr_expand(&seednames, tal_fmt(seednames, "%s.lseed.bitcoinstats.com", bech32));
|
2020-04-04 18:26:22 +02:00
|
|
|
/* This is darosior's seed */
|
|
|
|
tal_arr_expand(&seednames, tal_fmt(seednames, "%s.lseed.darosior.ninja", bech32));
|
2019-09-02 18:14:34 +02:00
|
|
|
return seednames;
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2018-08-09 02:27:30 +02:00
|
|
|
struct sockaddr *broken_reply)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2019-09-11 17:04:50 +02:00
|
|
|
struct wireaddr *new_addrs;
|
2019-09-21 16:48:03 +02:00
|
|
|
const char **hostnames = seednames(tmpctx, id);
|
2019-09-02 18:14:34 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_count(hostnames); i++) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(id, "Resolving %s", hostnames[i]);
|
2019-09-21 16:48:03 +02:00
|
|
|
new_addrs = wireaddr_from_hostname(tmpctx, hostnames[i], DEFAULT_PORT,
|
|
|
|
NULL, broken_reply, NULL);
|
|
|
|
if (new_addrs) {
|
2019-10-18 13:23:39 +02:00
|
|
|
for (size_t j = 0; j < tal_count(new_addrs); j++) {
|
2021-10-19 11:32:52 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES /* BOLT7 DNS RFC #911 */
|
2021-10-01 13:47:29 +02:00
|
|
|
if (new_addrs[j].type == ADDR_TYPE_DNS)
|
|
|
|
continue;
|
2021-10-19 11:32:52 +02:00
|
|
|
#endif
|
2019-09-02 18:14:34 +02:00
|
|
|
struct wireaddr_internal a;
|
|
|
|
a.itype = ADDR_INTERNAL_WIREADDR;
|
2019-10-18 13:23:39 +02:00
|
|
|
a.u.wireaddr = new_addrs[j];
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(id, "Resolved %s to %s", hostnames[i],
|
|
|
|
type_to_string(tmpctx, struct wireaddr,
|
|
|
|
&a.u.wireaddr));
|
2019-09-02 18:14:34 +02:00
|
|
|
tal_arr_expand(addrs, a);
|
|
|
|
}
|
2021-03-14 08:50:40 +01:00
|
|
|
/* Other seeds will likely have the same information. */
|
2020-04-04 18:26:22 +02:00
|
|
|
return;
|
2019-09-21 16:48:03 +02:00
|
|
|
} else
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(id, "Could not resolve %s", hostnames[i]);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 02:15:48 +01:00
|
|
|
static bool wireaddr_int_equals_wireaddr(const struct wireaddr_internal *addr_a,
|
|
|
|
const struct wireaddr *addr_b)
|
2021-08-20 12:50:28 +02:00
|
|
|
{
|
|
|
|
if (!addr_a || !addr_b)
|
|
|
|
return false;
|
|
|
|
return wireaddr_eq(&addr_a->u.wireaddr, addr_b);
|
|
|
|
}
|
|
|
|
|
2022-01-11 02:15:48 +01:00
|
|
|
/*~ Orders the addresses which lightningd gave us. */
|
2018-08-09 02:27:30 +02:00
|
|
|
static void add_gossip_addrs(struct wireaddr_internal **addrs,
|
2022-01-11 02:15:48 +01:00
|
|
|
const struct wireaddr *normal_addrs,
|
|
|
|
const struct wireaddr_internal *addrhint)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
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++) {
|
2021-11-10 01:27:41 +01:00
|
|
|
/* This is not supported, ignore. */
|
|
|
|
if (normal_addrs[i].type == ADDR_TYPE_TOR_V2_REMOVED)
|
|
|
|
continue;
|
|
|
|
|
2021-08-20 12:50:28 +02:00
|
|
|
/* add TOR addresses in a second loop */
|
2021-11-10 01:27:41 +01:00
|
|
|
if (normal_addrs[i].type == ADDR_TYPE_TOR_V3)
|
2021-08-20 12:50:28 +02:00
|
|
|
continue;
|
|
|
|
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
|
|
|
|
continue;
|
|
|
|
struct wireaddr_internal addr;
|
|
|
|
addr.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
addr.u.wireaddr = normal_addrs[i];
|
|
|
|
tal_arr_expand(addrs, addr);
|
|
|
|
}
|
|
|
|
/* so connectd prefers direct connections if possible. */
|
|
|
|
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
|
2021-11-10 01:27:41 +01:00
|
|
|
if (normal_addrs[i].type != ADDR_TYPE_TOR_V3)
|
2021-08-20 12:50:28 +02:00
|
|
|
continue;
|
|
|
|
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
|
|
|
|
continue;
|
2018-08-09 02:27:30 +02:00
|
|
|
struct wireaddr_internal addr;
|
|
|
|
addr.itype = ADDR_INTERNAL_WIREADDR;
|
|
|
|
addr.u.wireaddr = normal_addrs[i];
|
2019-01-15 04:51:27 +01: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,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2018-09-28 05:24:17 +02:00
|
|
|
u32 seconds_waited,
|
2022-01-11 02:15:48 +01:00
|
|
|
struct wireaddr *gossip_addrs,
|
2022-01-08 14:19:29 +01:00
|
|
|
struct wireaddr_internal *addrhint STEALS)
|
2018-08-09 02:27:30 +02:00
|
|
|
{
|
|
|
|
struct wireaddr_internal *addrs;
|
2021-08-18 12:52:21 +02:00
|
|
|
bool use_proxy = daemon->always_use_proxy;
|
2018-09-27 23:06:19 +02:00
|
|
|
struct connecting *connect;
|
2022-01-11 20:35:12 +01:00
|
|
|
struct peer *existing;
|
|
|
|
|
|
|
|
/* Already existing? */
|
|
|
|
existing = peer_htable_get(&daemon->peers, id);
|
|
|
|
if (existing) {
|
|
|
|
/* If it's exiting now, we've raced: reconnect after */
|
|
|
|
if (existing->to_subd
|
|
|
|
&& existing->to_peer
|
|
|
|
&& !existing->told_to_close)
|
|
|
|
return;
|
|
|
|
}
|
2018-08-09 02:27:30 +02:00
|
|
|
|
2018-09-27 23:06:19 +02:00
|
|
|
/* If we're trying to connect it right now, that's OK. */
|
2020-11-11 01:23:21 +01:00
|
|
|
if ((connect = find_connecting(daemon, id))) {
|
|
|
|
/* If we've been passed in new connection details
|
|
|
|
* for this connection, update our addrhint + add
|
|
|
|
* to addresses to check */
|
|
|
|
if (addrhint) {
|
|
|
|
connect->addrhint = tal_steal(connect, addrhint);
|
|
|
|
tal_arr_expand(&connect->addrs, *addrhint);
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:27:30 +02:00
|
|
|
return;
|
2020-11-11 01:23:21 +01:00
|
|
|
}
|
2018-08-09 02:27:30 +02:00
|
|
|
|
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 */
|
2021-08-20 12:50:28 +02:00
|
|
|
/* We add this first so its tried first by connectd */
|
2018-08-09 02:27:30 +02:00
|
|
|
if (addrhint)
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&addrs, *addrhint);
|
2018-08-09 02:27:30 +02:00
|
|
|
|
2022-01-11 02:15:48 +01:00
|
|
|
add_gossip_addrs(&addrs, gossip_addrs, addrhint);
|
2018-08-09 02:27:30 +02:00
|
|
|
|
|
|
|
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;
|
2019-09-02 18:14:34 +02:00
|
|
|
const char **hostnames = seednames(tmpctx, id);
|
|
|
|
for (size_t i = 0; i < tal_count(hostnames); i++) {
|
|
|
|
wireaddr_from_unresolved(&unresolved,
|
|
|
|
hostnames[i],
|
|
|
|
DEFAULT_PORT);
|
|
|
|
tal_arr_expand(&addrs, unresolved);
|
|
|
|
}
|
2018-08-09 02:27:30 +02:00
|
|
|
} else if (daemon->use_dns) {
|
|
|
|
add_seed_addrs(&addrs, id,
|
2019-09-11 17:04:50 +02:00
|
|
|
daemon->broken_resolver_response);
|
2018-08-09 02:27:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2020-01-05 18:17:25 +01:00
|
|
|
CONNECT_NO_KNOWN_ADDRESS,
|
2019-12-04 19:45:18 +01:00
|
|
|
"Unable to connect, no address known for peer");
|
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);
|
|
|
|
|
2019-09-01 22:14:50 +02:00
|
|
|
/* Now we kick it off by recursively trying connect->addrs[connect->addrnum] */
|
2022-01-11 20:35:12 +01:00
|
|
|
if (!existing)
|
|
|
|
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. */
|
2021-12-28 00:23:09 +01:00
|
|
|
static void connect_to_peer(struct daemon *daemon, const u8 *msg)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2018-08-09 02:25:29 +02:00
|
|
|
u32 seconds_waited;
|
2018-08-09 02:25:29 +02:00
|
|
|
struct wireaddr_internal *addrhint;
|
2022-01-11 02:15:48 +01:00
|
|
|
struct wireaddr *addrs;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2020-08-25 04:16:22 +02:00
|
|
|
if (!fromwire_connectd_connect_to_peer(tmpctx, msg,
|
2022-01-11 02:15:48 +01:00
|
|
|
&id, &seconds_waited,
|
|
|
|
&addrs, &addrhint))
|
2020-08-25 04:16:22 +02:00
|
|
|
master_badmsg(WIRE_CONNECTD_CONNECT_TO_PEER, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2022-01-11 02:15:48 +01:00
|
|
|
try_connect_peer(daemon, &id, seconds_waited, addrs, addrhint);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2022-01-11 02:16:49 +01:00
|
|
|
void peer_conn_closed(struct peer *peer)
|
|
|
|
{
|
2022-01-11 20:35:12 +01:00
|
|
|
struct connecting *connect = find_connecting(peer->daemon, &peer->id);
|
|
|
|
|
2022-01-11 02:17:01 +01:00
|
|
|
/* These should be closed already! */
|
|
|
|
assert(!peer->to_subd);
|
|
|
|
assert(!peer->to_peer);
|
|
|
|
assert(peer->told_to_close);
|
|
|
|
|
2022-01-29 04:33:05 +01:00
|
|
|
/* Tell gossipd to stop asking this peer gossip queries */
|
|
|
|
daemon_conn_send(peer->daemon->gossipd,
|
|
|
|
take(towire_gossipd_peer_gone(NULL, &peer->id)));
|
|
|
|
|
2022-01-11 02:16:49 +01:00
|
|
|
/* Wake up in case there's a reconnecting peer waiting in io_wait. */
|
|
|
|
io_wake(peer);
|
|
|
|
|
|
|
|
/* Note: deleting from a htable (a-la node_set_del) does not free it:
|
|
|
|
* htable doesn't assume it's a tal object at all. That's why we have
|
|
|
|
* a destructor attached to peer (called destroy_peer by
|
|
|
|
* convention). */
|
|
|
|
tal_free(peer);
|
2022-01-11 20:35:12 +01:00
|
|
|
|
|
|
|
/* If we wanted to connect to it, but found it was exiting, try again */
|
|
|
|
if (connect)
|
|
|
|
try_connect_one_addr(connect);
|
2022-01-11 02:16:49 +01:00
|
|
|
}
|
|
|
|
|
2021-06-03 04:01:16 +02:00
|
|
|
/* A peer is gone: clean things up. */
|
|
|
|
static void cleanup_dead_peer(struct daemon *daemon, const struct node_id *id)
|
2018-07-24 08:18:58 +02:00
|
|
|
{
|
2022-01-08 14:19:29 +01:00
|
|
|
struct peer *peer;
|
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. */
|
2022-01-08 14:19:29 +01:00
|
|
|
peer = peer_htable_get(&daemon->peers, id);
|
|
|
|
if (!peer)
|
2018-07-24 08:18:58 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"peer_disconnected unknown peer: %s",
|
2021-06-03 04:01:16 +02:00
|
|
|
type_to_string(tmpctx, struct node_id, id));
|
|
|
|
status_peer_debug(id, "disconnect");
|
2018-09-27 23:03:19 +02:00
|
|
|
|
2022-01-11 02:17:01 +01:00
|
|
|
/* When it's finished, it will call peer_conn_closed() */
|
|
|
|
close_peer_conn(peer);
|
2021-06-03 04:01:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* lightningd tells us a peer has disconnected. */
|
2021-12-28 00:23:09 +01:00
|
|
|
static void peer_disconnected(struct daemon *daemon, const u8 *msg)
|
2021-06-03 04:01:16 +02:00
|
|
|
{
|
|
|
|
struct node_id id;
|
|
|
|
|
|
|
|
if (!fromwire_connectd_peer_disconnected(msg, &id))
|
|
|
|
master_badmsg(WIRE_CONNECTD_PEER_DISCONNECTED, msg);
|
|
|
|
|
|
|
|
cleanup_dead_peer(daemon, &id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lightningd tells us to send a msg and disconnect. */
|
2021-12-28 00:23:09 +01:00
|
|
|
static void peer_final_msg(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
2021-06-03 04:01:16 +02:00
|
|
|
{
|
2022-01-08 14:19:29 +01:00
|
|
|
struct peer *peer;
|
|
|
|
struct node_id id;
|
2021-06-03 04:01:16 +02:00
|
|
|
u8 *finalmsg;
|
|
|
|
|
2022-01-08 14:29:29 +01:00
|
|
|
if (!fromwire_connectd_peer_final_msg(tmpctx, msg, &id, &finalmsg))
|
2021-06-03 04:01:16 +02:00
|
|
|
master_badmsg(WIRE_CONNECTD_PEER_FINAL_MSG, msg);
|
|
|
|
|
2022-01-08 14:19:29 +01:00
|
|
|
/* This can happen if peer hung up on us. */
|
|
|
|
peer = peer_htable_get(&daemon->peers, &id);
|
|
|
|
if (peer) {
|
2022-01-11 02:17:01 +01:00
|
|
|
/* Log message for peer. */
|
2022-01-08 14:19:29 +01:00
|
|
|
status_peer_io(LOG_IO_OUT, &id, finalmsg);
|
|
|
|
multiplex_final_msg(peer, take(finalmsg));
|
|
|
|
}
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
#if DEVELOPER
|
2021-12-28 00:23:09 +01:00
|
|
|
static void dev_connect_memleak(struct daemon *daemon, const u8 *msg)
|
2018-11-22 03:17:29 +01:00
|
|
|
{
|
|
|
|
struct htable *memtable;
|
|
|
|
bool found_leak;
|
|
|
|
|
2020-09-23 03:37:04 +02:00
|
|
|
memtable = memleak_find_allocations(tmpctx, msg, msg);
|
2018-11-22 03:17:29 +01:00
|
|
|
|
|
|
|
/* Now delete daemon and those which it has pointers to. */
|
2020-09-23 03:37:04 +02:00
|
|
|
memleak_remove_region(memtable, daemon, sizeof(daemon));
|
2022-01-08 14:19:29 +01:00
|
|
|
memleak_remove_htable(memtable, &daemon->peers.raw);
|
2018-11-22 03:17:29 +01:00
|
|
|
|
2021-09-06 14:39:27 +02:00
|
|
|
found_leak = dump_memleak(memtable, memleak_status_broken);
|
2018-11-22 03:17:29 +01:00
|
|
|
daemon_conn_send(daemon->master,
|
2020-08-25 04:16:22 +02:00
|
|
|
take(towire_connectd_dev_memleak_reply(NULL,
|
2018-11-22 03:17:29 +01:00
|
|
|
found_leak)));
|
|
|
|
}
|
|
|
|
#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
|
|
|
{
|
2020-08-25 04:16:22 +02:00
|
|
|
enum connectd_wire 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) {
|
2020-08-25 04:16:22 +02:00
|
|
|
case WIRE_CONNECTD_INIT:
|
2021-12-28 00:23:09 +01:00
|
|
|
connect_init(daemon, msg);
|
|
|
|
goto out;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2020-08-25 04:16:22 +02:00
|
|
|
case WIRE_CONNECTD_ACTIVATE:
|
2021-12-28 00:23:09 +01:00
|
|
|
connect_activate(daemon, msg);
|
|
|
|
goto out;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2020-08-25 04:16:22 +02:00
|
|
|
case WIRE_CONNECTD_CONNECT_TO_PEER:
|
2021-12-28 00:23:09 +01:00
|
|
|
connect_to_peer(daemon, msg);
|
|
|
|
goto out;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2020-08-25 04:16:22 +02:00
|
|
|
case WIRE_CONNECTD_PEER_DISCONNECTED:
|
2021-12-28 00:23:09 +01:00
|
|
|
peer_disconnected(daemon, msg);
|
|
|
|
goto out;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2021-06-03 04:01:16 +02:00
|
|
|
case WIRE_CONNECTD_PEER_FINAL_MSG:
|
2021-12-28 00:23:09 +01:00
|
|
|
peer_final_msg(conn, daemon, msg);
|
|
|
|
goto out;
|
2021-06-03 04:01:16 +02:00
|
|
|
|
2022-01-29 04:31:32 +01:00
|
|
|
case WIRE_CONNECTD_PING:
|
|
|
|
send_manual_ping(daemon, msg);
|
|
|
|
goto out;
|
|
|
|
|
2022-01-29 04:32:32 +01:00
|
|
|
case WIRE_CONNECTD_SEND_ONIONMSG:
|
|
|
|
onionmsg_req(daemon, msg);
|
|
|
|
goto out;
|
|
|
|
|
2022-01-29 04:33:05 +01:00
|
|
|
case WIRE_CONNECTD_CUSTOMMSG_OUT:
|
|
|
|
send_custommsg(daemon, msg);
|
|
|
|
goto out;
|
|
|
|
|
2020-08-25 04:16:22 +02:00
|
|
|
case WIRE_CONNECTD_DEV_MEMLEAK:
|
2018-11-22 03:17:29 +01:00
|
|
|
#if DEVELOPER
|
2021-12-28 00:23:09 +01:00
|
|
|
dev_connect_memleak(daemon, msg);
|
|
|
|
goto out;
|
2018-11-22 03:17:29 +01:00
|
|
|
#endif
|
2018-07-24 08:18:58 +02:00
|
|
|
/* We send these, we don't receive them */
|
2020-08-25 04:16:22 +02:00
|
|
|
case WIRE_CONNECTD_INIT_REPLY:
|
|
|
|
case WIRE_CONNECTD_ACTIVATE_REPLY:
|
|
|
|
case WIRE_CONNECTD_PEER_CONNECTED:
|
|
|
|
case WIRE_CONNECTD_RECONNECTED:
|
|
|
|
case WIRE_CONNECTD_CONNECT_FAILED:
|
|
|
|
case WIRE_CONNECTD_DEV_MEMLEAK_REPLY:
|
2022-01-29 04:31:32 +01:00
|
|
|
case WIRE_CONNECTD_PING_REPLY:
|
2022-01-29 04:32:32 +01:00
|
|
|
case WIRE_CONNECTD_GOT_ONIONMSG_TO_US:
|
2022-01-29 04:33:05 +01:00
|
|
|
case WIRE_CONNECTD_CUSTOMMSG_IN:
|
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));
|
2021-12-28 00:23:09 +01:00
|
|
|
|
|
|
|
out:
|
|
|
|
/* Read the next message. */
|
|
|
|
return daemon_conn_read_next(conn, daemon->master);
|
2018-07-24 08:18:58 +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);
|
|
|
|
}
|
|
|
|
|
2022-01-24 21:06:52 +01:00
|
|
|
/*~ gossipd sends us gossip to send to the peers. */
|
|
|
|
static struct io_plan *recv_gossip(struct io_conn *conn,
|
|
|
|
const u8 *msg,
|
|
|
|
struct daemon *daemon)
|
|
|
|
{
|
2022-01-24 21:07:52 +01:00
|
|
|
struct node_id dst;
|
|
|
|
u8 *gossip_msg;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossipd_send_gossip(msg, msg, &dst, &gossip_msg))
|
|
|
|
status_failed(STATUS_FAIL_GOSSIP_IO, "Unknown msg %i",
|
|
|
|
fromwire_peektype(msg));
|
|
|
|
|
|
|
|
peer = peer_htable_get(&daemon->peers, &dst);
|
|
|
|
if (peer)
|
2022-01-29 04:33:05 +01:00
|
|
|
inject_peer_msg(peer, take(gossip_msg));
|
2022-01-24 21:07:52 +01:00
|
|
|
|
2022-01-24 21:06:52 +01:00
|
|
|
return daemon_conn_read_next(conn, daemon->gossipd);
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:42:05 +02:00
|
|
|
/*~ This is a hook used by the memleak code (if DEVELOPER=1): it can't see
|
|
|
|
* pointers inside hash tables, so we give it a hint here. */
|
|
|
|
#if DEVELOPER
|
|
|
|
static void memleak_daemon_cb(struct htable *memtable, struct daemon *daemon)
|
|
|
|
{
|
|
|
|
memleak_remove_htable(memtable, &daemon->peers.raw);
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
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);
|
2022-01-08 14:19:29 +01:00
|
|
|
peer_htable_init(&daemon->peers);
|
2019-09-06 06:42:05 +02:00
|
|
|
memleak_add_helper(daemon, memleak_daemon_cb);
|
2018-09-27 23:06:19 +02:00
|
|
|
list_head_init(&daemon->connecting);
|
2020-09-11 08:48:12 +02:00
|
|
|
timers_init(&daemon->timers, time_mono());
|
2022-01-08 14:28:29 +01:00
|
|
|
daemon->gossip_store_fd = -1;
|
|
|
|
|
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
|
|
|
|
2022-03-16 02:26:41 +01:00
|
|
|
/* Don't leave around websocketd zombies. Technically not portable,
|
|
|
|
* but OK for Linux and BSD, so... */
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2022-01-24 21:06:52 +01:00
|
|
|
/* This streams gossip to and from gossipd */
|
2022-01-29 04:33:05 +01:00
|
|
|
daemon->gossipd = daemon_conn_new(daemon, GOSSIPCTL_FD,
|
2022-01-24 21:06:52 +01:00
|
|
|
recv_gossip, NULL,
|
|
|
|
daemon);
|
|
|
|
|
2020-04-03 05:21:22 +02:00
|
|
|
/* Set up ecdh() function so it uses our HSM fd, and calls
|
|
|
|
* status_failed on error. */
|
|
|
|
ecdh_hsmd_setup(HSM_FD, status_failed);
|
|
|
|
|
2020-09-11 09:03:22 +02:00
|
|
|
for (;;) {
|
|
|
|
struct timer *expired;
|
|
|
|
io_loop(&daemon->timers, &expired);
|
2021-12-06 15:45:50 +01:00
|
|
|
timer_expired(expired);
|
2020-09-11 09:03:22 +02:00
|
|
|
}
|
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!
|
|
|
|
*/
|