2018-09-03 05:40:00 +02:00
|
|
|
#ifndef LIGHTNING_CONNECTD_CONNECTD_H
|
|
|
|
#define LIGHTNING_CONNECTD_CONNECTD_H
|
2018-05-10 01:18:23 +02:00
|
|
|
#include "config.h"
|
2018-09-27 23:04:19 +02:00
|
|
|
#include <bitcoin/pubkey.h>
|
2022-01-08 14:28:29 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
|
|
|
#include <ccan/htable/htable_type.h>
|
|
|
|
#include <ccan/timer/timer.h>
|
2018-09-27 23:04:19 +02:00
|
|
|
#include <common/crypto_state.h>
|
2022-01-08 14:28:29 +01:00
|
|
|
#include <common/node_id.h>
|
|
|
|
#include <common/pseudorand.h>
|
2018-05-10 01:18:23 +02:00
|
|
|
|
|
|
|
struct io_conn;
|
2018-09-27 23:06:19 +02:00
|
|
|
struct connecting;
|
2019-06-03 20:14:25 +02:00
|
|
|
struct wireaddr_internal;
|
2018-05-10 01:18:23 +02:00
|
|
|
|
2022-01-08 14:28:29 +01:00
|
|
|
/*~ We keep a hash table (ccan/htable) of peers, which tells us what peers are
|
|
|
|
* already connected (by peer->id). */
|
|
|
|
struct peer {
|
|
|
|
/* Main daemon */
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
|
|
|
/* The pubkey of the node */
|
|
|
|
struct node_id id;
|
|
|
|
/* Counters and keys for symmetric crypto */
|
|
|
|
struct crypto_state cs;
|
|
|
|
|
|
|
|
/* Connection to the peer */
|
|
|
|
struct io_conn *to_peer;
|
|
|
|
|
|
|
|
/* Connection to the subdaemon */
|
|
|
|
struct io_conn *to_subd;
|
|
|
|
|
|
|
|
/* Final message to send to peer (and hangup) */
|
|
|
|
u8 *final_msg;
|
|
|
|
|
|
|
|
/* When we write something which wants Nagle overridden */
|
|
|
|
bool urgent;
|
|
|
|
|
|
|
|
/* Input buffers. */
|
|
|
|
u8 *subd_in, *peer_in;
|
|
|
|
|
|
|
|
/* Output buffers. */
|
|
|
|
struct msg_queue *subd_outq, *peer_outq;
|
|
|
|
|
|
|
|
/* Peer sent buffer (for freeing after sending) */
|
|
|
|
const u8 *sent_to_peer;
|
|
|
|
|
|
|
|
/* Gossip store. */
|
|
|
|
struct gossip_state *gs;
|
|
|
|
/* FIXME: move into gs. */
|
|
|
|
struct gossip_rcvd_filter *grf;
|
|
|
|
size_t gossip_store_off;
|
|
|
|
|
|
|
|
struct oneshot *gossip_timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*~ The HTABLE_DEFINE_TYPE() macro needs a keyof() function to extract the key:
|
|
|
|
*/
|
|
|
|
static const struct node_id *peer_keyof(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return &peer->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ We also need to define a hashing function. siphash24 is a fast yet
|
|
|
|
* cryptographic hash in ccan/crypto/siphash24; we might be able to get away
|
|
|
|
* with a slightly faster hash with fewer guarantees, but it's good hygiene to
|
|
|
|
* use this unless it's a proven bottleneck. siphash_seed() is a function in
|
|
|
|
* common/pseudorand which sets up a seed for our hashing; it's different
|
|
|
|
* every time the program is run. */
|
|
|
|
static size_t node_id_hash(const struct node_id *id)
|
|
|
|
{
|
|
|
|
return siphash24(siphash_seed(), id->k, sizeof(id->k));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ We also define an equality function: is this element equal to this key? */
|
|
|
|
static bool peer_eq_node_id(const struct peer *peer,
|
|
|
|
const struct node_id *id)
|
|
|
|
{
|
|
|
|
return node_id_eq(&peer->id, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~ This defines 'struct peer_htable' which contains 'struct peer' pointers. */
|
|
|
|
HTABLE_DEFINE_TYPE(struct peer,
|
|
|
|
peer_keyof,
|
|
|
|
node_id_hash,
|
|
|
|
peer_eq_node_id,
|
|
|
|
peer_htable);
|
|
|
|
|
|
|
|
/*~ This is the global state, like `struct lightningd *ld` in lightningd. */
|
|
|
|
struct daemon {
|
|
|
|
/* Who am I? */
|
|
|
|
struct node_id id;
|
|
|
|
|
|
|
|
/* pubkey equivalent. */
|
|
|
|
struct pubkey mykey;
|
|
|
|
|
|
|
|
/* Base for timeout timers, and how long to wait for init msg */
|
|
|
|
struct timers timers;
|
|
|
|
u32 timeout_secs;
|
|
|
|
|
|
|
|
/* Peers that we've handed to `lightningd`, which it hasn't told us
|
|
|
|
* have disconnected. */
|
|
|
|
struct peer_htable peers;
|
|
|
|
|
|
|
|
/* Peers we are trying to reach */
|
|
|
|
struct list_head connecting;
|
|
|
|
|
|
|
|
/* Connection to main daemon. */
|
|
|
|
struct daemon_conn *master;
|
|
|
|
|
|
|
|
/* Allow localhost to be considered "public": DEVELOPER-only option,
|
|
|
|
* but for simplicity we don't #if DEVELOPER-wrap it here. */
|
|
|
|
bool dev_allow_localhost;
|
|
|
|
|
|
|
|
/* We support use of a SOCKS5 proxy (e.g. Tor) */
|
|
|
|
struct addrinfo *proxyaddr;
|
|
|
|
|
|
|
|
/* They can tell us we must use proxy even for non-Tor addresses. */
|
|
|
|
bool always_use_proxy;
|
|
|
|
|
|
|
|
/* There are DNS seeds we can use to look up node addresses as a last
|
|
|
|
* resort, but doing so leaks our address so can be disabled. */
|
|
|
|
bool use_dns;
|
|
|
|
|
|
|
|
/* The address that the broken response returns instead of
|
|
|
|
* NXDOMAIN. NULL if we have not detected a broken resolver. */
|
|
|
|
struct sockaddr *broken_resolver_response;
|
|
|
|
|
|
|
|
/* File descriptors to listen on once we're activated. */
|
|
|
|
struct listen_fd *listen_fds;
|
|
|
|
|
|
|
|
/* Allow to define the default behavior of tor services calls*/
|
|
|
|
bool use_v3_autotor;
|
|
|
|
|
|
|
|
/* Our features, as lightningd told us */
|
|
|
|
struct feature_set *our_features;
|
|
|
|
|
|
|
|
/* Subdaemon to proxy websocket requests. */
|
|
|
|
char *websocket_helper;
|
|
|
|
|
|
|
|
/* If non-zero, port to listen for websocket connections. */
|
|
|
|
u16 websocket_port;
|
|
|
|
|
|
|
|
/* The gossip_store */
|
|
|
|
int gossip_store_fd;
|
|
|
|
size_t gossip_store_end;
|
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Hack to speed up gossip timer */
|
|
|
|
bool dev_fast_gossip;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2018-09-27 23:04:19 +02:00
|
|
|
/* Called by io_tor_connect once it has a connection out. */
|
2018-09-27 23:06:19 +02:00
|
|
|
struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect);
|
2018-05-10 01:18:23 +02:00
|
|
|
|
2020-04-14 16:03:04 +02:00
|
|
|
/* add erros to error list */
|
|
|
|
void add_errors_to_error_list(struct connecting *connect, const char *error);
|
|
|
|
|
2018-09-27 23:04:19 +02:00
|
|
|
/* Called by peer_exchange_initmsg if successful. */
|
|
|
|
struct io_plan *peer_connected(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
2019-06-03 20:14:25 +02:00
|
|
|
const struct node_id *id,
|
|
|
|
const struct wireaddr_internal *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-09-27 23:04:19 +02:00
|
|
|
|
2018-09-03 05:40:00 +02:00
|
|
|
#endif /* LIGHTNING_CONNECTD_CONNECTD_H */
|