2017-10-23 06:12:38 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2018-02-08 02:24:46 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/container_of/container_of.h>
|
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
2018-02-23 01:00:00 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/list/list.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/noerr/noerr.h>
|
2017-12-15 23:20:13 +01:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-03-07 02:08:20 +01:00
|
|
|
#include <ccan/take/take.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <ccan/timer/timer.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/daemon_conn.h>
|
2018-03-13 16:42:55 +01:00
|
|
|
#include <common/features.h>
|
2017-09-28 05:40:59 +02:00
|
|
|
#include <common/io_debug.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/ping.h>
|
|
|
|
#include <common/status.h>
|
2018-01-08 11:01:09 +01:00
|
|
|
#include <common/subdaemon.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:03:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <common/wire_error.h>
|
2017-10-23 06:17:38 +02:00
|
|
|
#include <common/wireaddr.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/broadcast.h>
|
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <gossipd/handshake.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/routing.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <hsmd/client.h>
|
2017-11-24 15:47:14 +01:00
|
|
|
#include <hsmd/gen_hsm_client_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <inttypes.h>
|
2017-03-12 13:39:23 +01:00
|
|
|
#include <lightningd/gossip_msg.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
#include <sodium/randombytes.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
2017-11-24 15:47:14 +01:00
|
|
|
#include <wire/wire_sync.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-10-11 11:58:50 +02:00
|
|
|
#define HSM_FD 3
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct daemon {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Who am I? */
|
|
|
|
struct pubkey id;
|
|
|
|
|
|
|
|
/* Peers we have directly or indirectly */
|
2017-01-10 06:08:33 +01:00
|
|
|
struct list_head peers;
|
2017-03-19 21:28:29 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Peers we are trying to reach */
|
|
|
|
struct list_head reaching;
|
|
|
|
|
2017-03-19 21:28:29 +01:00
|
|
|
/* Connection to main daemon. */
|
|
|
|
struct daemon_conn master;
|
2017-02-01 15:49:01 +01:00
|
|
|
|
|
|
|
/* Routing information */
|
|
|
|
struct routing_state *rstate;
|
2017-02-04 16:28:35 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Hacky list of known address hints. */
|
|
|
|
struct list_head addrhints;
|
|
|
|
|
2017-02-04 16:28:35 +01:00
|
|
|
struct timers timers;
|
2017-04-24 14:31:26 +02:00
|
|
|
|
|
|
|
u32 broadcast_interval;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
/* Local and global features to offer to peers. */
|
|
|
|
u8 *localfeatures, *globalfeatures;
|
2017-11-24 15:03:22 +01:00
|
|
|
|
|
|
|
u8 alias[33];
|
|
|
|
u8 rgb[3];
|
|
|
|
struct wireaddr *wireaddrs;
|
2018-01-04 12:40:46 +01:00
|
|
|
|
|
|
|
/* To make sure our node_announcement timestamps increase */
|
|
|
|
u32 last_announce_timestamp;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Peers we're trying to reach. */
|
|
|
|
struct reaching {
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
|
|
|
/* daemon->reaching */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
/* The ID of the peer (not necessarily unique, in transit!) */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2017-10-23 06:12:38 +02:00
|
|
|
/* Where I'm reaching to. */
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr addr;
|
2017-10-23 06:12:38 +02:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Did we succeed? */
|
|
|
|
bool succeeded;
|
2018-01-27 16:42:24 +01:00
|
|
|
|
|
|
|
/* How many times have we attempted to connect? */
|
|
|
|
u32 attempts;
|
|
|
|
|
2018-01-29 05:05:01 +01:00
|
|
|
/* How many times to attempt */
|
2018-01-27 16:42:24 +01:00
|
|
|
u32 max_attempts;
|
|
|
|
|
|
|
|
/* Timestamp of the first attempt */
|
|
|
|
u32 first_attempt;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
/* Things we need when we're talking direct to the peer. */
|
|
|
|
struct local_peer_state {
|
|
|
|
/* Cryptostate */
|
|
|
|
struct peer_crypto_state pcs;
|
|
|
|
|
|
|
|
/* File descriptor corresponding to conn. */
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Our connection (and owner) */
|
|
|
|
struct io_conn *conn;
|
|
|
|
|
|
|
|
/* Waiting to send_peer_with_fds to master? */
|
|
|
|
bool return_to_master;
|
|
|
|
|
|
|
|
/* If we're exiting due to non-gossip msg, otherwise release */
|
|
|
|
u8 *nongossip_msg;
|
|
|
|
|
|
|
|
/* How many pongs are we expecting? */
|
|
|
|
size_t num_pings_outstanding;
|
|
|
|
|
|
|
|
/* Message queue for outgoing. */
|
|
|
|
struct msg_queue peer_out;
|
|
|
|
};
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct peer {
|
|
|
|
struct daemon *daemon;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* daemon->peers */
|
|
|
|
struct list_node list;
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* The ID of the peer (not necessarily unique, in transit!) */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2017-10-23 06:12:38 +02:00
|
|
|
/* Where it's connected to. */
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr addr;
|
2017-10-23 06:12:38 +02:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Feature bitmaps. */
|
|
|
|
u8 *gfeatures, *lfeatures;
|
|
|
|
|
2017-02-01 15:49:01 +01:00
|
|
|
/* High water mark for the staggered broadcast */
|
|
|
|
u64 broadcast_index;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-02-08 14:05:21 +01:00
|
|
|
/* Is it time to continue the staggered broadcast? */
|
|
|
|
bool gossip_sync;
|
2017-03-09 14:24:32 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* If we die, should we reach again? */
|
|
|
|
bool reach_again;
|
2017-10-25 11:18:05 +02:00
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
/* Only one of these is set: */
|
|
|
|
struct local_peer_state *local;
|
|
|
|
struct daemon_conn *remote;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct addrhint {
|
|
|
|
/* Off ld->addrhints */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
struct pubkey id;
|
|
|
|
/* FIXME: use array... */
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr addr;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* FIXME: Reorder */
|
|
|
|
static struct io_plan *peer_start_gossip(struct io_conn *conn,
|
|
|
|
struct peer *peer);
|
2017-10-25 11:18:05 +02:00
|
|
|
static bool send_peer_with_fds(struct peer *peer, const u8 *msg);
|
2017-03-11 14:45:54 +01:00
|
|
|
static void wake_pkt_out(struct peer *peer);
|
2018-01-10 06:30:54 +01:00
|
|
|
static bool try_reach_peer(struct daemon *daemon, const struct pubkey *id);
|
2017-03-11 14:45:54 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static void destroy_peer(struct peer *peer)
|
|
|
|
{
|
|
|
|
list_del_from(&peer->daemon->peers, &peer->list);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
if (peer->reach_again)
|
|
|
|
try_reach_peer(peer->daemon, &peer->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct peer *find_peer(struct daemon *daemon, const struct pubkey *id)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
list_for_each(&daemon->peers, peer, list)
|
|
|
|
if (pubkey_eq(&peer->id, id))
|
|
|
|
return peer;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_addrhint(struct addrhint *a)
|
|
|
|
{
|
|
|
|
list_del(&a->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct addrhint *find_addrhint(struct daemon *daemon,
|
|
|
|
const struct pubkey *id)
|
|
|
|
{
|
|
|
|
struct addrhint *a;
|
|
|
|
|
|
|
|
list_for_each(&daemon->addrhints, a, list) {
|
|
|
|
if (pubkey_eq(&a->id, id))
|
|
|
|
return a;
|
2017-03-19 21:28:34 +01:00
|
|
|
}
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return NULL;
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
static struct local_peer_state *
|
|
|
|
new_local_peer_state(struct peer *peer, const struct crypto_state *cs)
|
|
|
|
{
|
|
|
|
struct local_peer_state *lps = tal(peer, struct local_peer_state);
|
|
|
|
|
|
|
|
init_peer_crypto_state(peer, &lps->pcs);
|
|
|
|
lps->pcs.cs = *cs;
|
|
|
|
lps->return_to_master = false;
|
|
|
|
lps->num_pings_outstanding = 0;
|
|
|
|
msg_queue_init(&lps->peer_out, peer);
|
|
|
|
|
|
|
|
return lps;
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static struct peer *new_peer(const tal_t *ctx,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const struct pubkey *their_id,
|
2017-10-23 06:17:38 +02:00
|
|
|
const struct wireaddr *addr,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
const struct crypto_state *cs)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct peer *peer = tal(ctx, struct peer);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer->id = *their_id;
|
2017-10-23 06:13:38 +02:00
|
|
|
peer->addr = *addr;
|
2017-01-10 06:08:33 +01:00
|
|
|
peer->daemon = daemon;
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local = new_local_peer_state(peer, cs);
|
|
|
|
peer->remote = NULL;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer->reach_again = false;
|
2017-10-11 11:57:50 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void peer_finalized(struct peer *peer)
|
2017-06-24 08:50:23 +02:00
|
|
|
{
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* No longer tied to peer->conn's lifetime. */
|
|
|
|
tal_steal(peer->daemon, peer);
|
2017-06-24 08:50:23 +02:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Now we can put this in the list of peers */
|
|
|
|
list_add_tail(&peer->daemon->peers, &peer->list);
|
2017-06-24 08:50:23 +02:00
|
|
|
tal_add_destructor(peer, destroy_peer);
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void destroy_reaching(struct reaching *reach)
|
|
|
|
{
|
|
|
|
list_del_from(&reach->daemon->reaching, &reach->list);
|
|
|
|
}
|
2017-03-19 21:32:40 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static struct reaching *find_reaching(struct daemon *daemon,
|
|
|
|
const struct pubkey *id)
|
2017-03-19 21:32:40 +01:00
|
|
|
{
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct reaching *r;
|
2017-03-19 21:32:40 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
list_for_each(&daemon->reaching, r, list)
|
|
|
|
if (pubkey_eq(id, &r->id))
|
|
|
|
return r;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-19 21:32:40 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void reached_peer(struct daemon *daemon, const struct pubkey *id,
|
|
|
|
struct io_conn *conn)
|
|
|
|
{
|
|
|
|
struct reaching *r = find_reaching(daemon, id);
|
|
|
|
|
|
|
|
if (!r)
|
2017-03-19 21:32:40 +01:00
|
|
|
return;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
/* OK, we've reached the peer successfully, stop retrying. */
|
|
|
|
|
|
|
|
/* Don't free conn with reach. */
|
|
|
|
tal_steal(daemon, conn);
|
|
|
|
/* Don't call connect_failed */
|
|
|
|
io_set_finish(conn, NULL, NULL);
|
|
|
|
|
|
|
|
tal_free(r);
|
|
|
|
}
|
|
|
|
|
2018-03-08 04:24:14 +01:00
|
|
|
static void queue_peer_msg(struct peer *peer, const u8 *msg TAKES)
|
|
|
|
{
|
|
|
|
if (peer->local) {
|
|
|
|
msg_enqueue(&peer->local->peer_out, msg);
|
2018-03-13 00:06:00 +01:00
|
|
|
} else {
|
|
|
|
/* Use gossip_index 0 meaning don't update index */
|
|
|
|
const u8 *send = towire_gossip_send_gossip(NULL, 0, msg);
|
|
|
|
if (taken(msg))
|
|
|
|
tal_free(msg);
|
|
|
|
daemon_conn_send(peer->remote, take(send));
|
2018-03-08 04:24:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void peer_error(struct peer *peer, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
status_trace("peer %s: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
tal_vfmt(tmpctx, fmt, ap));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Send error: we'll close after writing this. */
|
|
|
|
va_start(ap, fmt);
|
2018-03-08 04:24:14 +01:00
|
|
|
queue_peer_msg(peer, take(towire_errorfmtv(peer, NULL, fmt, ap)));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_all_channel_error(const u8 *msg)
|
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
|
|
|
u8 *data;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_error(msg, msg, &channel_id, &data))
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return false;
|
|
|
|
tal_free(data);
|
2017-10-22 11:03:03 +02:00
|
|
|
return channel_id_is_all(&channel_id);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_close_after_error(struct io_conn *conn,
|
|
|
|
struct peer *peer)
|
|
|
|
{
|
|
|
|
status_trace("%s: we sent them a fatal error, closing",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_init_received(struct io_conn *conn,
|
|
|
|
struct peer *peer,
|
|
|
|
u8 *msg)
|
|
|
|
{
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_init(peer, msg, &peer->gfeatures, &peer->lfeatures)){
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
status_trace("peer %s bad fromwire_init '%s', closing",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
tal_hex(tmpctx, msg));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return io_close(conn);
|
2017-03-19 21:32:40 +01:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
reached_peer(peer->daemon, &peer->id, conn);
|
2017-03-19 21:32:40 +01:00
|
|
|
|
2018-03-13 16:42:55 +01:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* Upon receiving an `init` message with the `initial_routing_sync`
|
|
|
|
* flag set the node sends `channel_announcement`s, `channel_update`s
|
|
|
|
* and `node_announcement`s for all known channels and nodes as if
|
|
|
|
* they were just received.
|
|
|
|
*/
|
|
|
|
if (feature_offered(peer->lfeatures, LOCAL_INITIAL_ROUTING_SYNC))
|
|
|
|
peer->broadcast_index = 0;
|
|
|
|
else
|
|
|
|
peer->broadcast_index
|
|
|
|
= peer->daemon->rstate->broadcasts->next_index;
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* This is a full peer now; we keep it around until its
|
|
|
|
* gossipfd closed (forget_peer) or reconnect. */
|
|
|
|
peer_finalized(peer);
|
2017-03-19 21:32:40 +01:00
|
|
|
|
2017-10-25 11:18:05 +02:00
|
|
|
/* We will not have anything queued, since we're not duplex. */
|
2017-10-23 06:13:38 +02:00
|
|
|
msg = towire_gossip_peer_connected(peer, &peer->id, &peer->addr,
|
2017-12-06 07:15:06 +01:00
|
|
|
&peer->local->pcs.cs,
|
2017-12-11 04:33:16 +01:00
|
|
|
peer->broadcast_index,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer->gfeatures, peer->lfeatures);
|
2017-10-25 11:18:05 +02:00
|
|
|
if (!send_peer_with_fds(peer, msg))
|
|
|
|
return io_close(conn);
|
2017-03-19 21:32:40 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Start the gossip flowing. */
|
|
|
|
/* FIXME: This is a bit wasteful in the common case where master
|
|
|
|
* simply hands it straight back to us and we restart the peer and
|
|
|
|
* restart gossip broadcast... */
|
|
|
|
wake_pkt_out(peer);
|
|
|
|
|
|
|
|
return io_close_taken_fd(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *read_init(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* Each node MUST wait to receive `init` before sending any other
|
|
|
|
* messages.
|
|
|
|
*/
|
2017-12-06 07:15:06 +01:00
|
|
|
return peer_read_message(conn, &peer->local->pcs, peer_init_received);
|
2017-03-19 21:32:40 +01:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* This creates a temporary peer which is not in the list and is owner
|
|
|
|
* by the connection; it's placed in the list and owned by daemon once
|
|
|
|
* we have the features. */
|
|
|
|
static struct io_plan *init_new_peer(struct io_conn *conn,
|
|
|
|
const struct pubkey *their_id,
|
2017-10-23 06:17:38 +02:00
|
|
|
const struct wireaddr *addr,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
const struct crypto_state *cs,
|
|
|
|
struct daemon *daemon)
|
|
|
|
{
|
2017-10-23 06:13:38 +02:00
|
|
|
struct peer *peer = new_peer(conn, daemon, their_id, addr, cs);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
u8 *initmsg;
|
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local->fd = io_conn_fd(conn);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* Each node MUST send `init` as the first lightning message for any
|
|
|
|
* connection.
|
|
|
|
*/
|
2018-03-15 07:10:22 +01:00
|
|
|
initmsg = towire_init(NULL,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
daemon->globalfeatures, daemon->localfeatures);
|
2017-12-06 07:15:06 +01:00
|
|
|
return peer_write_message(conn, &peer->local->pcs,
|
|
|
|
take(initmsg), read_init);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *owner_msg_in(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc);
|
2018-03-13 16:44:55 +01:00
|
|
|
static bool nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-11-24 15:47:14 +01:00
|
|
|
/* Create a node_announcement with the given signature. It may be NULL
|
|
|
|
* in the case we need to create a provisional announcement for the
|
|
|
|
* HSM to sign. This is typically called twice: once with the dummy
|
|
|
|
* signature to get it signed and a second time to build the full
|
|
|
|
* packet with the signature. The timestamp is handed in since that is
|
|
|
|
* the only thing that may change between the dummy creation and the
|
|
|
|
* call with a signature.*/
|
|
|
|
static u8 *create_node_announcement(const tal_t *ctx, struct daemon *daemon,
|
|
|
|
secp256k1_ecdsa_signature *sig,
|
|
|
|
u32 timestamp)
|
2017-03-11 14:45:54 +01:00
|
|
|
{
|
2017-11-24 15:47:14 +01:00
|
|
|
u8 *features = NULL;
|
|
|
|
u8 *addresses = tal_arr(ctx, u8, 0);
|
|
|
|
u8 *announcement;
|
|
|
|
size_t i;
|
|
|
|
if (!sig) {
|
|
|
|
sig = tal(ctx, secp256k1_ecdsa_signature);
|
|
|
|
memset(sig, 0, sizeof(*sig));
|
|
|
|
}
|
|
|
|
for (i = 0; i < tal_count(daemon->wireaddrs); i++)
|
|
|
|
towire_wireaddr(&addresses, daemon->wireaddrs+i);
|
|
|
|
|
|
|
|
announcement =
|
|
|
|
towire_node_announcement(ctx, sig, features, timestamp,
|
|
|
|
&daemon->id, daemon->rgb, daemon->alias,
|
|
|
|
addresses);
|
|
|
|
return announcement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_node_announcement(struct daemon *daemon)
|
|
|
|
{
|
|
|
|
u32 timestamp = time_now().ts.tv_sec;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
2018-03-08 05:10:26 +01:00
|
|
|
u8 *msg, *nannounce, *err;
|
2018-01-04 12:40:46 +01:00
|
|
|
|
|
|
|
/* Timestamps must move forward, or announce will be ignored! */
|
|
|
|
if (timestamp <= daemon->last_announce_timestamp)
|
|
|
|
timestamp = daemon->last_announce_timestamp + 1;
|
|
|
|
daemon->last_announce_timestamp = timestamp;
|
|
|
|
|
|
|
|
nannounce = create_node_announcement(tmpctx, daemon, NULL, timestamp);
|
2017-11-24 15:47:14 +01:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
if (!wire_sync_write(HSM_FD, take(towire_hsm_node_announcement_sig_req(NULL, nannounce))))
|
2017-11-24 15:47:14 +01:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "Could not write to HSM: %s", strerror(errno));
|
|
|
|
|
|
|
|
msg = wire_sync_read(tmpctx, HSM_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_node_announcement_sig_reply(msg, &sig))
|
2017-11-24 15:47:14 +01:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "HSM returned an invalid node_announcement sig");
|
|
|
|
|
|
|
|
/* We got the signature for out provisional node_announcement back
|
|
|
|
* from the HSM, create the real announcement and forward it to
|
|
|
|
* gossipd so it can take care of forwarding it. */
|
2018-03-15 07:10:22 +01:00
|
|
|
nannounce = create_node_announcement(NULL, daemon, &sig, timestamp);
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_node_announcement(daemon->rstate, take(nannounce), true);
|
2018-03-08 05:10:26 +01:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"rejected own node announcement: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, err));
|
2017-11-24 15:47:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 05:10:26 +01:00
|
|
|
/* Returns error if we should send an error. */
|
2018-03-20 16:55:52 +01:00
|
|
|
static u8 *handle_gossip_msg(struct daemon *daemon, const u8 *msg, bool store)
|
2017-11-24 15:47:14 +01:00
|
|
|
{
|
2018-03-18 14:57:15 +01:00
|
|
|
struct routing_state *rstate = daemon->rstate;
|
2017-03-11 14:45:54 +01:00
|
|
|
int t = fromwire_peektype(msg);
|
2018-03-08 05:10:26 +01:00
|
|
|
u8 *err;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2017-03-11 14:45:54 +01:00
|
|
|
switch(t) {
|
2018-01-04 12:40:58 +01:00
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT: {
|
|
|
|
const struct short_channel_id *scid;
|
|
|
|
/* If it's OK, tells us the short_channel_id to lookup */
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_channel_announcement(rstate, msg, &scid, store);
|
2018-03-08 05:10:31 +01:00
|
|
|
if (err)
|
2018-03-18 14:57:15 +01:00
|
|
|
return err;
|
2018-03-08 05:10:31 +01:00
|
|
|
else if (scid)
|
2018-03-18 14:57:15 +01:00
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-08 05:10:26 +01:00
|
|
|
take(towire_gossip_get_txout(NULL,
|
2018-01-04 12:40:58 +01:00
|
|
|
scid)));
|
2017-03-11 14:45:54 +01:00
|
|
|
break;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
2017-03-11 14:45:54 +01:00
|
|
|
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_node_announcement(rstate, msg, store);
|
2018-03-08 05:10:26 +01:00
|
|
|
if (err)
|
2018-03-18 14:57:15 +01:00
|
|
|
return err;
|
2017-03-11 14:45:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WIRE_CHANNEL_UPDATE:
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_channel_update(rstate, msg, store);
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err)
|
2018-03-18 14:57:15 +01:00
|
|
|
return err;
|
2017-03-11 14:45:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-03-18 14:57:15 +01:00
|
|
|
|
|
|
|
/* All good, no error to report */
|
|
|
|
return NULL;
|
2017-03-11 14:45:54 +01:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void handle_ping(struct peer *peer, u8 *ping)
|
2017-04-12 18:10:10 +02:00
|
|
|
{
|
|
|
|
u8 *pong;
|
|
|
|
|
|
|
|
if (!check_ping_make_pong(peer, ping, &pong)) {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer_error(peer, "Bad ping");
|
|
|
|
return;
|
2017-04-12 18:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pong)
|
2017-12-06 07:15:06 +01:00
|
|
|
msg_enqueue(&peer->local->peer_out, take(pong));
|
2017-04-12 18:10:10 +02:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void handle_pong(struct peer *peer, const u8 *pong)
|
2017-04-12 20:20:48 +02:00
|
|
|
{
|
2018-02-22 11:01:52 +01:00
|
|
|
const char *err = got_pong(pong, &peer->local->num_pings_outstanding);
|
2017-04-12 20:20:48 +02:00
|
|
|
|
2018-02-22 11:01:52 +01:00
|
|
|
if (err) {
|
|
|
|
peer_error(peer, "%s", err);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return;
|
2017-04-12 20:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_send(&peer->daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_ping_reply(NULL, true,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
tal_len(pong))));
|
2017-04-12 20:20:48 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 11:18:05 +02:00
|
|
|
/* If master asks us to release peer, we attach this destructor in case it
|
|
|
|
* dies while we're waiting for it to finish IO */
|
|
|
|
static void fail_release(struct peer *peer)
|
|
|
|
{
|
2018-03-15 07:10:22 +01:00
|
|
|
u8 *msg = towire_gossipctl_release_peer_replyfail(NULL);
|
2017-10-25 11:18:05 +02:00
|
|
|
daemon_conn_send(&peer->daemon->master, take(msg));
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:46:02 +02:00
|
|
|
static struct io_plan *ready_for_master(struct io_conn *conn, struct peer *peer)
|
2017-10-25 11:18:05 +02:00
|
|
|
{
|
2017-10-26 04:46:02 +02:00
|
|
|
u8 *msg;
|
2017-12-06 07:15:06 +01:00
|
|
|
if (peer->local->nongossip_msg)
|
2017-10-26 04:46:02 +02:00
|
|
|
msg = towire_gossip_peer_nongossip(peer, &peer->id,
|
2017-10-23 06:13:38 +02:00
|
|
|
&peer->addr,
|
2017-12-06 07:15:06 +01:00
|
|
|
&peer->local->pcs.cs,
|
2017-12-11 04:33:16 +01:00
|
|
|
peer->broadcast_index,
|
2017-10-26 04:46:02 +02:00
|
|
|
peer->gfeatures,
|
|
|
|
peer->lfeatures,
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local->nongossip_msg);
|
2017-10-26 04:46:02 +02:00
|
|
|
else
|
|
|
|
msg = towire_gossipctl_release_peer_reply(peer,
|
2017-10-23 06:13:38 +02:00
|
|
|
&peer->addr,
|
2017-12-06 07:15:06 +01:00
|
|
|
&peer->local->pcs.cs,
|
2017-12-11 04:33:16 +01:00
|
|
|
peer->broadcast_index,
|
2017-10-26 04:46:02 +02:00
|
|
|
peer->gfeatures,
|
|
|
|
peer->lfeatures);
|
2017-10-25 11:18:05 +02:00
|
|
|
|
2017-10-26 04:46:02 +02:00
|
|
|
if (send_peer_with_fds(peer, take(msg))) {
|
|
|
|
/* In case we set this earlier. */
|
|
|
|
tal_del_destructor(peer, fail_release);
|
|
|
|
return io_close_taken_fd(conn);
|
|
|
|
} else
|
|
|
|
return io_close(conn);
|
2017-10-25 11:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_msgin(struct io_conn *conn,
|
|
|
|
struct peer *peer, u8 *msg);
|
|
|
|
|
|
|
|
/* Wrapper around peer_read_message: don't read another if we want to
|
|
|
|
* pass up to master */
|
|
|
|
static struct io_plan *peer_next_in(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-12-06 07:15:06 +01:00
|
|
|
if (peer->local->return_to_master) {
|
|
|
|
assert(!peer_in_started(conn, &peer->local->pcs));
|
2018-02-08 04:21:41 +01:00
|
|
|
/* Wake writer. */
|
|
|
|
msg_wake(&peer->local->peer_out);
|
2017-10-26 04:46:02 +02:00
|
|
|
return io_wait(conn, peer, peer_next_in, peer);
|
|
|
|
}
|
2017-10-25 11:18:05 +02:00
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
return peer_read_message(conn, &peer->local->pcs, peer_msgin);
|
2017-10-25 11:18:05 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *peer_msgin(struct io_conn *conn,
|
|
|
|
struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
enum wire_type t = fromwire_peektype(msg);
|
2018-03-18 14:57:15 +01:00
|
|
|
u8 *err;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_ERROR:
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
status_trace("%s sent ERROR %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
sanitize_error(tmpctx, msg, NULL));
|
2017-01-10 06:08:33 +01:00
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT:
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
|
|
|
case WIRE_CHANNEL_UPDATE:
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_gossip_msg(peer->daemon, msg, true);
|
2018-03-18 14:57:15 +01:00
|
|
|
if (err)
|
|
|
|
queue_peer_msg(peer, take(err));
|
2017-10-25 11:18:05 +02:00
|
|
|
return peer_next_in(conn, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
case WIRE_PING:
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
handle_ping(peer, msg);
|
2017-10-25 11:18:05 +02:00
|
|
|
return peer_next_in(conn, peer);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
case WIRE_PONG:
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
handle_pong(peer, msg);
|
2017-10-25 11:18:05 +02:00
|
|
|
return peer_next_in(conn, peer);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_OPEN_CHANNEL:
|
2017-06-20 07:40:03 +02:00
|
|
|
case WIRE_CHANNEL_REESTABLISH:
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_ACCEPT_CHANNEL:
|
|
|
|
case WIRE_FUNDING_CREATED:
|
|
|
|
case WIRE_FUNDING_SIGNED:
|
|
|
|
case WIRE_FUNDING_LOCKED:
|
2017-02-07 02:44:14 +01:00
|
|
|
case WIRE_ANNOUNCEMENT_SIGNATURES:
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_UPDATE_FEE:
|
|
|
|
case WIRE_SHUTDOWN:
|
|
|
|
case WIRE_CLOSING_SIGNED:
|
|
|
|
case WIRE_UPDATE_ADD_HTLC:
|
|
|
|
case WIRE_UPDATE_FULFILL_HTLC:
|
|
|
|
case WIRE_UPDATE_FAIL_HTLC:
|
|
|
|
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
|
2017-03-29 12:53:15 +02:00
|
|
|
case WIRE_COMMITMENT_SIGNED:
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_REVOKE_AND_ACK:
|
2017-05-22 13:26:51 +02:00
|
|
|
case WIRE_INIT:
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Not our place to handle this, so we punt */
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local->return_to_master = true;
|
|
|
|
peer->local->nongossip_msg = tal_steal(peer, msg);
|
2017-10-25 11:18:05 +02:00
|
|
|
|
|
|
|
/* This will wait. */
|
|
|
|
return peer_next_in(conn, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The type follows the _it's ok to be odd_ rule, so nodes MAY send
|
|
|
|
* odd-numbered types without ascertaining that the recipient
|
|
|
|
* understands it. */
|
|
|
|
if (t & 1) {
|
2018-02-11 19:42:54 +01:00
|
|
|
status_trace("Peer %s sent packet with unknown message type %u, ignoring",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id), t);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
} else
|
2018-02-11 19:42:54 +01:00
|
|
|
peer_error(peer, "Packet with unknown message type %u", t);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-10-25 11:18:05 +02:00
|
|
|
return peer_next_in(conn, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 19:10:21 +01:00
|
|
|
/* Wake up the outgoing direction of the connection and write any
|
|
|
|
* queued messages. Needed since the `io_wake` method signature does
|
2017-02-08 14:05:21 +01:00
|
|
|
* not allow us to specify it as the callback for `new_reltimer`, but
|
|
|
|
* it allows us to set an additional flag for the routing dump..
|
2017-02-06 19:10:21 +01:00
|
|
|
*/
|
|
|
|
static void wake_pkt_out(struct peer *peer)
|
|
|
|
{
|
2017-02-08 14:05:21 +01:00
|
|
|
peer->gossip_sync = true;
|
2017-04-24 14:31:26 +02:00
|
|
|
new_reltimer(&peer->daemon->timers, peer,
|
|
|
|
time_from_msec(peer->daemon->broadcast_interval),
|
2017-03-11 14:45:54 +01:00
|
|
|
wake_pkt_out, peer);
|
2017-12-06 07:15:06 +01:00
|
|
|
|
|
|
|
if (peer->local)
|
|
|
|
/* Notify the peer-write loop */
|
|
|
|
msg_wake(&peer->local->peer_out);
|
|
|
|
else
|
|
|
|
/* Notify the daemon_conn-write loop */
|
|
|
|
msg_wake(&peer->remote->out);
|
2017-02-06 19:10:21 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
/* Mutual recursion. */
|
|
|
|
static struct io_plan *peer_pkt_out(struct io_conn *conn, struct peer *peer);
|
|
|
|
|
2017-04-12 18:10:10 +02:00
|
|
|
static struct io_plan *peer_pkt_out(struct io_conn *conn, struct peer *peer)
|
2017-02-01 15:49:01 +01:00
|
|
|
{
|
2017-04-12 18:10:10 +02:00
|
|
|
/* First priority is queued packets, if any */
|
2017-12-06 07:15:06 +01:00
|
|
|
const u8 *out = msg_dequeue(&peer->local->peer_out);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
if (out) {
|
|
|
|
if (is_all_channel_error(out))
|
2017-12-06 07:15:06 +01:00
|
|
|
return peer_write_message(conn, &peer->local->pcs,
|
|
|
|
take(out),
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer_close_after_error);
|
2017-12-06 07:15:06 +01:00
|
|
|
return peer_write_message(conn, &peer->local->pcs, take(out),
|
2017-04-12 18:10:10 +02:00
|
|
|
peer_pkt_out);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-10-25 11:18:05 +02:00
|
|
|
/* Do we want to send this peer to the master daemon? */
|
2017-12-06 07:15:06 +01:00
|
|
|
if (peer->local->return_to_master) {
|
|
|
|
if (!peer_in_started(conn, &peer->local->pcs))
|
2017-10-26 04:46:02 +02:00
|
|
|
return ready_for_master(conn, peer);
|
2018-02-08 04:21:41 +01:00
|
|
|
} else if (peer->gossip_sync) {
|
|
|
|
/* If we're supposed to be sending gossip, do so now. */
|
2018-03-13 16:45:55 +01:00
|
|
|
const u8 *next;
|
2017-02-06 19:10:21 +01:00
|
|
|
|
2018-03-13 16:45:55 +01:00
|
|
|
next = next_broadcast(peer->daemon->rstate->broadcasts,
|
|
|
|
&peer->broadcast_index);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
if (next)
|
2017-12-06 07:15:06 +01:00
|
|
|
return peer_write_message(conn, &peer->local->pcs,
|
2018-03-13 16:45:55 +01:00
|
|
|
next,
|
2018-03-13 16:43:55 +01:00
|
|
|
peer_pkt_out);
|
2017-04-12 18:10:10 +02:00
|
|
|
|
|
|
|
/* Gossip is drained. Wait for next timer. */
|
2017-02-08 14:05:21 +01:00
|
|
|
peer->gossip_sync = false;
|
|
|
|
}
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
return msg_queue_wait(conn, &peer->local->peer_out, peer_pkt_out, peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Now we're a fully-fledged peer. */
|
|
|
|
static struct io_plan *peer_start_gossip(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
wake_pkt_out(peer);
|
|
|
|
return io_duplex(conn,
|
2017-10-25 11:18:05 +02:00
|
|
|
peer_next_in(conn, peer),
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer_pkt_out(conn, peer));
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:52:32 +01:00
|
|
|
static void handle_get_update(struct peer *peer, const u8 *msg)
|
|
|
|
{
|
2018-03-02 09:59:16 +01:00
|
|
|
struct short_channel_id scid;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2017-11-28 23:52:32 +01:00
|
|
|
const u8 *update;
|
2018-03-13 16:40:55 +01:00
|
|
|
struct routing_state *rstate = peer->daemon->rstate;
|
2017-11-28 23:52:32 +01:00
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
if (!fromwire_gossip_get_update(msg, &scid)) {
|
2017-11-28 23:52:32 +01:00
|
|
|
status_trace("peer %s sent bad gossip_get_update %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
tal_hex(tmpctx, msg));
|
2017-11-28 23:52:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
chan = get_channel(rstate, &scid);
|
2018-03-02 09:59:16 +01:00
|
|
|
if (!chan) {
|
|
|
|
status_unusual("peer %s scid %s: unknown channel",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-03-02 09:59:16 +01:00
|
|
|
&scid));
|
2017-11-28 23:52:32 +01:00
|
|
|
update = NULL;
|
2018-03-02 09:59:16 +01:00
|
|
|
} else {
|
2018-03-02 09:59:17 +01:00
|
|
|
/* We want update that comes from our end. */
|
2018-03-02 09:59:16 +01:00
|
|
|
if (pubkey_eq(&chan->nodes[0]->id, &peer->daemon->id))
|
2018-03-13 16:40:55 +01:00
|
|
|
update = get_broadcast(rstate->broadcasts,
|
|
|
|
chan->half[0]
|
|
|
|
.channel_update_msgidx);
|
2018-03-02 09:59:16 +01:00
|
|
|
else if (pubkey_eq(&chan->nodes[1]->id, &peer->daemon->id))
|
2018-03-13 16:40:55 +01:00
|
|
|
update = get_broadcast(rstate->broadcasts,
|
|
|
|
chan->half[1]
|
|
|
|
.channel_update_msgidx);
|
2018-03-02 09:59:16 +01:00
|
|
|
else {
|
|
|
|
status_unusual("peer %s scid %s: not our channel?",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2018-03-02 09:59:16 +01:00
|
|
|
&peer->id),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx,
|
2018-03-02 09:59:16 +01:00
|
|
|
struct short_channel_id,
|
|
|
|
&scid));
|
2018-03-02 09:59:17 +01:00
|
|
|
update = NULL;
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
2017-11-28 23:52:32 +01:00
|
|
|
}
|
2018-03-02 09:59:16 +01:00
|
|
|
status_trace("peer %s schanid %s: %s update",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid),
|
2018-03-02 09:59:16 +01:00
|
|
|
update ? "got" : "no");
|
2017-11-28 23:52:32 +01:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_gossip_get_update_reply(NULL, update);
|
2017-12-06 07:15:06 +01:00
|
|
|
daemon_conn_send(peer->remote, take(msg));
|
2017-11-28 23:52:32 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 23:20:13 +01:00
|
|
|
static void handle_local_add_channel(struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
struct routing_state *rstate = peer->daemon->rstate;
|
|
|
|
struct short_channel_id scid;
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-12-15 23:20:13 +01:00
|
|
|
struct pubkey remote_node_id;
|
2018-03-02 09:59:17 +01:00
|
|
|
u16 cltv_expiry_delta;
|
2017-12-15 23:20:13 +01:00
|
|
|
u32 fee_base_msat, fee_proportional_millionths;
|
|
|
|
u64 htlc_minimum_msat;
|
2018-03-02 09:59:17 +01:00
|
|
|
int idx;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2017-12-15 23:20:13 +01:00
|
|
|
|
|
|
|
if (!fromwire_gossip_local_add_channel(
|
2018-03-02 09:59:16 +01:00
|
|
|
msg, &scid, &chain_hash, &remote_node_id,
|
2017-12-15 23:20:13 +01:00
|
|
|
&cltv_expiry_delta, &htlc_minimum_msat, &fee_base_msat,
|
|
|
|
&fee_proportional_millionths)) {
|
2018-03-02 09:59:16 +01:00
|
|
|
status_broken("Unable to parse local_add_channel message: %s", tal_hex(msg, msg));
|
2017-12-15 23:20:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!structeq(&chain_hash, &rstate->chain_hash)) {
|
2018-03-02 09:59:16 +01:00
|
|
|
status_broken("Received local_add_channel for unknown chain %s",
|
2017-12-18 07:44:10 +01:00
|
|
|
type_to_string(msg, struct bitcoin_blkid,
|
|
|
|
&chain_hash));
|
2017-12-15 23:20:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
if (get_channel(rstate, &scid)) {
|
|
|
|
status_broken("Attempted to local_add_channel a known channel");
|
2017-12-15 23:20:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
/* Create new channel */
|
|
|
|
chan = new_chan(rstate, &scid, &rstate->local_id, &remote_node_id);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
idx = pubkey_idx(&rstate->local_id, &remote_node_id),
|
2018-03-04 03:26:59 +01:00
|
|
|
/* Activate the half_chan from us to them. */
|
2018-03-02 09:59:17 +01:00
|
|
|
set_connection_values(chan, idx,
|
|
|
|
fee_base_msat,
|
|
|
|
fee_proportional_millionths,
|
|
|
|
cltv_expiry_delta,
|
|
|
|
true,
|
|
|
|
0,
|
|
|
|
htlc_minimum_msat);
|
2018-02-07 01:58:36 +01:00
|
|
|
/* Designed to match msg in handle_channel_update, for easy testing */
|
|
|
|
status_trace("Received local update for channel %s(%d) now ACTIVE",
|
2018-01-12 05:10:00 +01:00
|
|
|
type_to_string(msg, struct short_channel_id, &scid),
|
2018-03-02 09:59:17 +01:00
|
|
|
idx);
|
2017-12-15 23:20:13 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
/**
|
2018-02-27 21:21:07 +01:00
|
|
|
* owner_msg_in - Called by the `peer->remote` upon receiving a
|
2017-03-11 15:31:17 +01:00
|
|
|
* message
|
|
|
|
*/
|
|
|
|
static struct io_plan *owner_msg_in(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
2017-03-09 16:56:04 +01:00
|
|
|
{
|
2017-12-06 07:15:06 +01:00
|
|
|
struct peer *peer = dc->ctx;
|
2018-03-18 14:57:15 +01:00
|
|
|
u8 *msg = dc->msg_in, *err;
|
2017-03-09 16:56:04 +01:00
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
int type = fromwire_peektype(msg);
|
|
|
|
if (type == WIRE_CHANNEL_ANNOUNCEMENT || type == WIRE_CHANNEL_UPDATE ||
|
|
|
|
type == WIRE_NODE_ANNOUNCEMENT) {
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_gossip_msg(peer->daemon, dc->msg_in, true);
|
2018-03-18 14:57:15 +01:00
|
|
|
if (err)
|
|
|
|
queue_peer_msg(peer, take(err));
|
|
|
|
|
2017-11-28 23:52:32 +01:00
|
|
|
} else if (type == WIRE_GOSSIP_GET_UPDATE) {
|
|
|
|
handle_get_update(peer, dc->msg_in);
|
2017-12-15 23:20:13 +01:00
|
|
|
} else if (type == WIRE_GOSSIP_LOCAL_ADD_CHANNEL) {
|
|
|
|
handle_local_add_channel(peer, dc->msg_in);
|
|
|
|
} else {
|
2018-03-08 04:16:34 +01:00
|
|
|
status_broken("peer %s: send us unknown msg of type %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
2018-03-08 04:16:34 +01:00
|
|
|
gossip_wire_type_name(type));
|
|
|
|
/* Calls forget_peer */
|
|
|
|
return io_close(conn);
|
2017-03-11 15:31:17 +01:00
|
|
|
}
|
2017-12-15 23:20:13 +01:00
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
return daemon_conn_read_next(conn, dc);
|
2017-03-09 16:56:04 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static void forget_peer(struct io_conn *conn UNUSED, struct daemon_conn *dc)
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
{
|
|
|
|
struct peer *peer = dc->ctx;
|
|
|
|
|
|
|
|
status_trace("Forgetting %s peer %s",
|
|
|
|
peer->local ? "local" : "remote",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
/* Free peer. */
|
|
|
|
tal_free(dc->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When a peer is to be owned by another daemon, we create a socket
|
|
|
|
* pair to send/receive gossip from it */
|
2017-10-25 11:18:05 +02:00
|
|
|
static bool send_peer_with_fds(struct peer *peer, const u8 *msg)
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
{
|
|
|
|
int fds[2];
|
2017-12-06 07:15:06 +01:00
|
|
|
int peer_fd = peer->local->fd;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
|
|
|
|
status_trace("Failed to create socketpair: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
/* FIXME: Send error to peer? */
|
|
|
|
/* Peer will be freed when caller closes conn. */
|
2017-10-25 11:18:05 +02:00
|
|
|
return false;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we talk to socket to get to peer's owner daemon. */
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local = tal_free(peer->local);
|
|
|
|
peer->remote = tal(peer, struct daemon_conn);
|
|
|
|
daemon_conn_init(peer, peer->remote, fds[0],
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
owner_msg_in, forget_peer);
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->remote->msg_queue_cleared_cb = nonlocal_dump_gossip;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
/* Peer stays around, even though caller will close conn. */
|
|
|
|
tal_steal(peer->daemon, peer);
|
|
|
|
|
|
|
|
daemon_conn_send(&peer->daemon->master, msg);
|
2017-12-06 07:15:06 +01:00
|
|
|
daemon_conn_send_fd(&peer->daemon->master, peer_fd);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
daemon_conn_send_fd(&peer->daemon->master, fds[1]);
|
|
|
|
|
2017-10-25 11:18:05 +02:00
|
|
|
return true;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
/**
|
|
|
|
* nonlocal_dump_gossip - catch the nonlocal peer up with the latest gossip.
|
|
|
|
*
|
2018-02-27 21:21:07 +01:00
|
|
|
* Registered as `msg_queue_cleared_cb` by the `peer->remote`.
|
2017-03-11 15:31:17 +01:00
|
|
|
*/
|
2018-03-13 16:44:55 +01:00
|
|
|
static bool nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc)
|
2017-03-10 13:06:51 +01:00
|
|
|
{
|
2018-03-13 16:45:55 +01:00
|
|
|
const u8 *next;
|
2017-12-06 07:15:06 +01:00
|
|
|
struct peer *peer = dc->ctx;
|
2017-03-11 15:31:17 +01:00
|
|
|
|
|
|
|
/* Make sure we are not connected directly */
|
2017-12-06 07:15:06 +01:00
|
|
|
assert(!peer->local);
|
2017-03-11 15:31:17 +01:00
|
|
|
|
2018-02-27 21:23:13 +01:00
|
|
|
/* Nothing to do if we're not gossiping */
|
|
|
|
if (!peer->gossip_sync)
|
2018-03-13 16:44:55 +01:00
|
|
|
return false;
|
2018-02-27 21:23:13 +01:00
|
|
|
|
2018-03-13 16:45:55 +01:00
|
|
|
next = next_broadcast(peer->daemon->rstate->broadcasts,
|
|
|
|
&peer->broadcast_index);
|
2017-03-10 13:06:51 +01:00
|
|
|
|
|
|
|
if (!next) {
|
2018-02-27 21:23:13 +01:00
|
|
|
peer->gossip_sync = false;
|
2018-03-13 16:44:55 +01:00
|
|
|
return false;
|
2017-03-10 13:06:51 +01:00
|
|
|
} else {
|
2018-03-15 07:10:22 +01:00
|
|
|
u8 *msg = towire_gossip_send_gossip(NULL,
|
2017-12-11 04:33:16 +01:00
|
|
|
peer->broadcast_index,
|
2018-03-13 16:45:55 +01:00
|
|
|
next);
|
2018-03-13 16:44:55 +01:00
|
|
|
daemon_conn_send(peer->remote, take(msg));
|
|
|
|
return true;
|
2017-03-10 13:06:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *new_peer_got_fd(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local->conn = io_new_conn(conn, peer->local->fd,
|
|
|
|
peer_start_gossip, peer);
|
|
|
|
if (!peer->local->conn) {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
status_trace("Could not create connection for peer: %s",
|
|
|
|
strerror(errno));
|
2017-01-10 06:08:33 +01:00
|
|
|
tal_free(peer);
|
2017-05-23 13:01:17 +02:00
|
|
|
} else {
|
|
|
|
/* If conn dies, we forget peer. */
|
2017-12-06 07:15:06 +01:00
|
|
|
tal_steal(peer->local->conn, peer);
|
2017-03-09 11:35:52 +01:00
|
|
|
}
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return daemon_conn_read_next(conn, &peer->daemon->master);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
/* This lets us read the fds in before handling anything. */
|
|
|
|
struct returning_peer {
|
|
|
|
struct daemon *daemon;
|
|
|
|
struct pubkey id;
|
|
|
|
struct crypto_state cs;
|
2017-12-11 04:33:16 +01:00
|
|
|
u64 gossip_index;
|
2017-12-11 04:16:50 +01:00
|
|
|
u8 *inner_msg;
|
|
|
|
int peer_fd, gossip_fd;
|
|
|
|
};
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
static struct io_plan *handle_returning_peer(struct io_conn *conn,
|
|
|
|
struct returning_peer *rpeer)
|
2017-04-12 20:20:48 +02:00
|
|
|
{
|
2017-12-11 04:16:50 +01:00
|
|
|
struct daemon *daemon = rpeer->daemon;
|
2017-04-12 20:20:48 +02:00
|
|
|
struct peer *peer;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
peer = find_peer(daemon, &rpeer->id);
|
|
|
|
if (!peer)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"hand_back_peer unknown peer: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &rpeer->id));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-12-11 04:33:16 +01:00
|
|
|
/* We don't need the gossip_fd; we know what gossip it got
|
|
|
|
* from gossip_index */
|
2017-12-11 04:16:50 +01:00
|
|
|
close(rpeer->gossip_fd);
|
|
|
|
|
|
|
|
/* Possible if there's a reconnect: ignore handed back. */
|
|
|
|
if (peer->local) {
|
|
|
|
status_trace("hand_back_peer %s: reconnected, dropping handback",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &rpeer->id));
|
2017-12-11 04:16:50 +01:00
|
|
|
|
|
|
|
close(rpeer->peer_fd);
|
|
|
|
tal_free(rpeer);
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-10-11 11:57:50 +02:00
|
|
|
}
|
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
status_trace("hand_back_peer %s: now local again",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &rpeer->id));
|
2017-12-11 04:16:50 +01:00
|
|
|
|
|
|
|
/* Now we talk to peer directly again. */
|
|
|
|
daemon_conn_clear(peer->remote);
|
|
|
|
peer->remote = tal_free(peer->remote);
|
|
|
|
|
|
|
|
peer->local = new_local_peer_state(peer, &rpeer->cs);
|
|
|
|
peer->local->fd = rpeer->peer_fd;
|
2017-12-11 04:33:16 +01:00
|
|
|
peer->broadcast_index = rpeer->gossip_index;
|
2017-12-11 04:16:50 +01:00
|
|
|
|
|
|
|
/* If they told us to send a message, queue it now */
|
|
|
|
if (tal_len(rpeer->inner_msg))
|
|
|
|
msg_enqueue(&peer->local->peer_out, take(rpeer->inner_msg));
|
|
|
|
tal_free(rpeer);
|
|
|
|
|
|
|
|
return new_peer_got_fd(conn, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *read_returning_gossipfd(struct io_conn *conn,
|
|
|
|
struct returning_peer *rpeer)
|
|
|
|
{
|
|
|
|
return io_recv_fd(conn, &rpeer->gossip_fd,
|
|
|
|
handle_returning_peer, rpeer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *hand_back_peer(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct returning_peer *rpeer = tal(daemon, struct returning_peer);
|
2017-10-11 11:57:50 +02:00
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
rpeer->daemon = daemon;
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossipctl_hand_back_peer(msg, msg,
|
2017-12-11 04:16:50 +01:00
|
|
|
&rpeer->id, &rpeer->cs,
|
2017-12-11 04:33:16 +01:00
|
|
|
&rpeer->gossip_index,
|
2017-12-11 04:16:50 +01:00
|
|
|
&rpeer->inner_msg))
|
|
|
|
master_badmsg(WIRE_GOSSIPCTL_HAND_BACK_PEER, msg);
|
2017-10-11 11:57:50 +02:00
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
return io_recv_fd(conn, &rpeer->peer_fd,
|
|
|
|
read_returning_gossipfd, rpeer);
|
2017-10-11 11:57:50 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 17:16:20 +01:00
|
|
|
static struct io_plan *disconnect_peer(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossipctl_peer_disconnect(msg, &id))
|
|
|
|
master_badmsg(WIRE_GOSSIPCTL_PEER_DISCONNECT, msg);
|
|
|
|
|
|
|
|
peer = find_peer(daemon, &id);
|
|
|
|
if (peer && peer->local) {
|
2018-03-23 11:01:43 +01:00
|
|
|
/* This peer is local to this (gossipd) daemon */
|
2018-03-05 17:16:20 +01:00
|
|
|
io_close(peer->local->conn);
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_gossipctl_peer_disconnect_reply(NULL);
|
2018-03-05 17:16:20 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(msg));
|
|
|
|
} else {
|
|
|
|
status_trace("disconnect_peer: peer %s %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &id),
|
2018-03-07 14:23:43 +01:00
|
|
|
!peer ? "not connected" : "not gossiping");
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_gossipctl_peer_disconnect_replyfail(NULL, peer ? true : false);
|
2018-03-05 17:16:20 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(msg));
|
|
|
|
}
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2017-10-25 11:18:05 +02:00
|
|
|
{
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct pubkey id;
|
|
|
|
struct peer *peer;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossipctl_release_peer(msg, &id))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIPCTL_RELEASE_PEER, msg);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer = find_peer(daemon, &id);
|
2017-12-06 07:15:06 +01:00
|
|
|
if (!peer || !peer->local || peer->local->return_to_master) {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* This can happen with dying peers, or reconnect */
|
2017-10-25 11:18:05 +02:00
|
|
|
status_trace("release_peer: peer %s %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &id),
|
2017-10-25 11:18:05 +02:00
|
|
|
!peer ? "not found"
|
2017-12-06 07:15:06 +01:00
|
|
|
: peer->local ? "already releasing"
|
2017-10-25 11:18:05 +02:00
|
|
|
: "not local");
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_gossipctl_release_peer_replyfail(NULL);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
daemon_conn_send(&daemon->master, take(msg));
|
2017-05-23 14:17:34 +02:00
|
|
|
} else {
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local->return_to_master = true;
|
|
|
|
peer->local->nongossip_msg = NULL;
|
2017-10-25 11:18:05 +02:00
|
|
|
|
|
|
|
/* Wake output, in case it's idle. */
|
2017-12-06 07:15:06 +01:00
|
|
|
msg_wake(&peer->local->peer_out);
|
2017-05-23 14:17:34 +02:00
|
|
|
}
|
2017-04-12 20:20:48 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 13:46:29 +01:00
|
|
|
static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
u8 *msg)
|
2017-03-15 11:36:52 +01:00
|
|
|
{
|
2017-03-15 13:46:29 +01:00
|
|
|
struct pubkey source, destination;
|
2017-10-23 06:16:57 +02:00
|
|
|
u32 msatoshi, final_cltv;
|
2017-03-15 13:46:29 +01:00
|
|
|
u16 riskfactor;
|
|
|
|
u8 *out;
|
|
|
|
struct route_hop *hops;
|
2018-02-15 14:37:04 +01:00
|
|
|
double fuzz;
|
2018-02-23 01:00:00 +01:00
|
|
|
struct siphash_seed seed;
|
2017-03-15 13:46:29 +01:00
|
|
|
|
2018-02-26 03:32:58 +01:00
|
|
|
fromwire_gossip_getroute_request(msg,
|
2018-02-15 14:37:04 +01:00
|
|
|
&source, &destination,
|
|
|
|
&msatoshi, &riskfactor, &final_cltv,
|
2018-02-26 03:32:58 +01:00
|
|
|
&fuzz, &seed);
|
2017-03-15 13:46:29 +01:00
|
|
|
status_trace("Trying to find a route from %s to %s for %d msatoshi",
|
|
|
|
pubkey_to_hexstr(tmpctx, &source),
|
|
|
|
pubkey_to_hexstr(tmpctx, &destination), msatoshi);
|
|
|
|
|
|
|
|
hops = get_route(tmpctx, daemon->rstate, &source, &destination,
|
2018-02-16 04:50:51 +01:00
|
|
|
msatoshi, 1, final_cltv,
|
2018-02-23 01:00:00 +01:00
|
|
|
fuzz, &seed);
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-15 13:46:29 +01:00
|
|
|
out = towire_gossip_getroute_reply(msg, hops);
|
|
|
|
daemon_conn_send(&daemon->master, out);
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2018-03-02 03:27:30 +01:00
|
|
|
static void append_half_channel(struct gossip_getchannels_entry **entries,
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct chan *chan,
|
2018-03-04 03:26:56 +01:00
|
|
|
int idx)
|
2018-03-02 03:27:30 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct half_chan *c = &chan->half[idx];
|
2018-03-02 03:27:30 +01:00
|
|
|
struct gossip_getchannels_entry *e;
|
|
|
|
size_t n;
|
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Don't mention non-public inactive channels. */
|
2018-03-13 16:40:55 +01:00
|
|
|
if (!c->active && !c->channel_update_msgidx)
|
2018-03-02 09:59:17 +01:00
|
|
|
return;
|
|
|
|
|
2018-03-02 03:27:30 +01:00
|
|
|
n = tal_count(*entries);
|
|
|
|
tal_resize(entries, n+1);
|
|
|
|
e = &(*entries)[n];
|
|
|
|
|
2018-03-04 03:26:56 +01:00
|
|
|
e->source = chan->nodes[idx]->id;
|
|
|
|
e->destination = chan->nodes[!idx]->id;
|
2018-03-05 23:52:26 +01:00
|
|
|
e->satoshis = chan->satoshis;
|
2018-03-02 03:27:30 +01:00
|
|
|
e->active = c->active;
|
|
|
|
e->flags = c->flags;
|
2018-03-13 16:40:55 +01:00
|
|
|
e->public = (c->channel_update_msgidx != 0);
|
2018-03-04 03:26:56 +01:00
|
|
|
e->short_channel_id = chan->scid;
|
2018-03-13 16:40:55 +01:00
|
|
|
e->last_update_timestamp = c->channel_update_msgidx ? c->last_timestamp : -1;
|
2018-03-02 03:27:30 +01:00
|
|
|
if (e->last_update_timestamp >= 0) {
|
|
|
|
e->base_fee_msat = c->base_fee;
|
|
|
|
e->fee_per_millionth = c->proportional_fee;
|
|
|
|
e->delay = c->delay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
static void append_channel(struct gossip_getchannels_entry **entries,
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct chan *chan)
|
2018-03-02 09:59:16 +01:00
|
|
|
{
|
2018-03-04 03:26:56 +01:00
|
|
|
append_half_channel(entries, chan, 0);
|
|
|
|
append_half_channel(entries, chan, 1);
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:30:09 +01:00
|
|
|
static struct io_plan *getchannels_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
u8 *msg)
|
|
|
|
{
|
|
|
|
u8 *out;
|
|
|
|
struct gossip_getchannels_entry *entries;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2018-01-16 20:44:32 +01:00
|
|
|
struct short_channel_id *scid;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
fromwire_gossip_getchannels_request(msg, msg, &scid);
|
2017-03-22 13:30:09 +01:00
|
|
|
|
2018-03-02 03:27:30 +01:00
|
|
|
entries = tal_arr(tmpctx, struct gossip_getchannels_entry, 0);
|
2018-03-02 09:59:16 +01:00
|
|
|
if (scid) {
|
|
|
|
chan = get_channel(daemon->rstate, scid);
|
|
|
|
if (chan)
|
|
|
|
append_channel(&entries, chan);
|
|
|
|
} else {
|
|
|
|
u64 idx;
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
for (chan = uintmap_first(&daemon->rstate->chanmap, &idx);
|
2018-03-02 09:59:16 +01:00
|
|
|
chan;
|
2018-03-04 03:26:59 +01:00
|
|
|
chan = uintmap_after(&daemon->rstate->chanmap, &idx)) {
|
2018-03-02 09:59:16 +01:00
|
|
|
append_channel(&entries, chan);
|
2017-03-22 13:30:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
out = towire_gossip_getchannels_reply(NULL, entries);
|
2017-03-22 13:30:09 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(out));
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
static void append_node(const struct gossip_getnodes_entry ***nodes,
|
2018-01-16 20:44:32 +01:00
|
|
|
const struct node *n)
|
|
|
|
{
|
2018-02-08 02:24:46 +01:00
|
|
|
struct gossip_getnodes_entry *new;
|
2018-01-16 20:44:32 +01:00
|
|
|
size_t num_nodes = tal_count(*nodes);
|
2018-02-08 02:24:46 +01:00
|
|
|
|
|
|
|
new = tal(*nodes, struct gossip_getnodes_entry);
|
|
|
|
new->nodeid = n->id;
|
|
|
|
new->last_timestamp = n->last_timestamp;
|
2018-01-16 20:44:32 +01:00
|
|
|
if (n->last_timestamp < 0) {
|
2018-02-08 02:24:46 +01:00
|
|
|
new->addresses = NULL;
|
|
|
|
} else {
|
|
|
|
new->addresses = n->addresses;
|
|
|
|
new->alias = n->alias;
|
|
|
|
memcpy(new->color, n->rgb_color, 3);
|
2018-01-16 20:44:32 +01:00
|
|
|
}
|
2018-02-08 02:24:46 +01:00
|
|
|
tal_resize(nodes, num_nodes + 1);
|
|
|
|
(*nodes)[num_nodes] = new;
|
2018-01-16 20:44:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2017-03-12 13:39:23 +01:00
|
|
|
{
|
2017-03-16 05:05:26 +01:00
|
|
|
u8 *out;
|
2017-03-12 13:39:23 +01:00
|
|
|
struct node *n;
|
2018-02-08 02:24:46 +01:00
|
|
|
const struct gossip_getnodes_entry **nodes;
|
2018-01-16 20:44:32 +01:00
|
|
|
struct pubkey *ids;
|
2017-03-12 13:39:23 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
fromwire_gossip_getnodes_request(tmpctx, msg, &ids);
|
2018-01-16 20:44:32 +01:00
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
nodes = tal_arr(tmpctx, const struct gossip_getnodes_entry *, 0);
|
2018-01-16 20:44:32 +01:00
|
|
|
if (ids) {
|
|
|
|
for (size_t i = 0; i < tal_count(ids); i++) {
|
2018-02-27 21:16:43 +01:00
|
|
|
n = get_node(daemon->rstate, &ids[i]);
|
2018-01-16 20:44:32 +01:00
|
|
|
if (n)
|
|
|
|
append_node(&nodes, n);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct node_map_iter i;
|
|
|
|
n = node_map_first(daemon->rstate->nodes, &i);
|
|
|
|
while (n != NULL) {
|
|
|
|
append_node(&nodes, n);
|
|
|
|
n = node_map_next(daemon->rstate->nodes, &i);
|
|
|
|
}
|
2017-03-12 13:39:23 +01:00
|
|
|
}
|
2018-03-15 07:10:22 +01:00
|
|
|
out = towire_gossip_getnodes_reply(NULL, nodes);
|
2017-03-19 21:28:29 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(out));
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-03-12 13:39:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
static struct io_plan *ping_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct pubkey id;
|
2017-04-12 20:20:48 +02:00
|
|
|
u16 num_pong_bytes, len;
|
|
|
|
struct peer *peer;
|
|
|
|
u8 *ping;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_ping(msg, &id, &num_pong_bytes, &len))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIP_PING, msg);
|
2017-04-12 20:20:48 +02:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer = find_peer(daemon, &id);
|
|
|
|
if (!peer) {
|
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_ping_reply(NULL, false, 0)));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2017-04-12 20:20:48 +02:00
|
|
|
|
|
|
|
ping = make_ping(peer, num_pong_bytes, len);
|
|
|
|
if (tal_len(ping) > 65535)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "Oversize ping");
|
2017-04-12 20:20:48 +02:00
|
|
|
|
2017-12-06 07:15:06 +01:00
|
|
|
msg_enqueue(&peer->local->peer_out, take(ping));
|
2017-04-12 20:20:48 +02:00
|
|
|
status_trace("sending ping expecting %sresponse",
|
|
|
|
num_pong_bytes >= 65532 ? "no " : "");
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* if `num_pong_bytes` is less than 65532 it MUST respond by sending a
|
|
|
|
* `pong` message with `byteslen` equal to `num_pong_bytes`, otherwise
|
|
|
|
* it MUST ignore the `ping`.
|
|
|
|
*/
|
|
|
|
if (num_pong_bytes >= 65532)
|
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_ping_reply(NULL, true, 0)));
|
2017-04-12 20:20:48 +02:00
|
|
|
else
|
2017-12-06 07:15:06 +01:00
|
|
|
peer->local->num_pings_outstanding++;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
out:
|
2017-04-12 20:20:48 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-01-26 14:20:25 +01:00
|
|
|
static int make_listen_fd(int domain, void *addr, socklen_t len, bool reportfail)
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
{
|
|
|
|
int fd = socket(domain, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
status_trace("Failed to create %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr) {
|
|
|
|
int on = 1;
|
|
|
|
|
|
|
|
/* Re-use, please.. */
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("Failed setting socket reuse: %s",
|
|
|
|
strerror(errno));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
if (bind(fd, addr, len) != 0) {
|
2018-01-26 14:20:25 +01:00
|
|
|
if (reportfail)
|
2018-02-27 21:01:26 +01:00
|
|
|
status_broken("Failed to bind on %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, 5) != 0) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_broken("Failed to listen on %u socket: %s",
|
|
|
|
domain, strerror(errno));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_noerr(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-09 20:00:05 +01:00
|
|
|
static void gossip_send_keepalive_update(struct routing_state *rstate,
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan *hc)
|
2018-01-09 20:00:05 +01:00
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
struct short_channel_id scid;
|
|
|
|
u32 timestamp, fee_base_msat, fee_proportional_millionths;
|
|
|
|
u64 htlc_minimum_msat;
|
|
|
|
u16 flags, cltv_expiry_delta;
|
2018-03-08 05:10:33 +01:00
|
|
|
u8 *update, *msg, *err;
|
2018-03-13 16:40:55 +01:00
|
|
|
const u8 *old_update;
|
2018-01-09 20:00:05 +01:00
|
|
|
|
|
|
|
/* Parse old update */
|
2018-03-13 16:40:55 +01:00
|
|
|
old_update = get_broadcast(rstate->broadcasts,
|
|
|
|
hc->channel_update_msgidx);
|
|
|
|
|
2018-01-09 20:00:05 +01:00
|
|
|
if (!fromwire_channel_update(
|
2018-03-13 16:40:55 +01:00
|
|
|
old_update, &sig, &chain_hash, &scid, ×tamp,
|
2018-01-09 20:00:05 +01:00
|
|
|
&flags, &cltv_expiry_delta, &htlc_minimum_msat, &fee_base_msat,
|
|
|
|
&fee_proportional_millionths)) {
|
|
|
|
status_failed(
|
|
|
|
STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Unable to parse previously accepted channel_update");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now generate a new update, with up to date timestamp */
|
|
|
|
timestamp = time_now().ts.tv_sec;
|
|
|
|
update =
|
|
|
|
towire_channel_update(tmpctx, &sig, &chain_hash, &scid, timestamp,
|
|
|
|
flags, cltv_expiry_delta, htlc_minimum_msat,
|
|
|
|
fee_base_msat, fee_proportional_millionths);
|
|
|
|
|
|
|
|
if (!wire_sync_write(HSM_FD,
|
|
|
|
towire_hsm_cupdate_sig_req(tmpctx, update))) {
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO, "Writing cupdate_sig_req: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = wire_sync_read(tmpctx, HSM_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!msg || !fromwire_hsm_cupdate_sig_reply(tmpctx, msg, &update)) {
|
2018-01-09 20:00:05 +01:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Reading cupdate_sig_req: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
status_trace("Sending keepalive channel_update for %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid));
|
|
|
|
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_channel_update(rstate, update, true);
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"rejected keepalive channel_update: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, err));
|
2018-01-09 20:00:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
static void gossip_refresh_network(struct daemon *daemon)
|
2018-01-09 16:29:10 +01:00
|
|
|
{
|
2018-01-09 20:00:05 +01:00
|
|
|
u64 now = time_now().ts.tv_sec;
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Anything below this highwater mark could be pruned if not refreshed */
|
|
|
|
s64 highwater = now - daemon->rstate->prune_timeout / 2;
|
2018-01-13 12:30:52 +01:00
|
|
|
struct node *n;
|
2018-01-09 21:51:45 +01:00
|
|
|
|
2018-01-09 16:29:10 +01:00
|
|
|
/* Schedule next run now */
|
|
|
|
new_reltimer(&daemon->timers, daemon,
|
2018-03-02 09:59:17 +01:00
|
|
|
time_from_sec(daemon->rstate->prune_timeout/4),
|
|
|
|
gossip_refresh_network, daemon);
|
2018-01-09 16:29:10 +01:00
|
|
|
|
2018-01-09 20:00:05 +01:00
|
|
|
/* Find myself in the network */
|
2018-02-27 21:16:43 +01:00
|
|
|
n = get_node(daemon->rstate, &daemon->id);
|
2018-01-13 14:15:05 +01:00
|
|
|
if (n) {
|
|
|
|
/* Iterate through all outgoing connection and check whether
|
|
|
|
* it's time to re-announce */
|
2018-03-04 03:26:59 +01:00
|
|
|
for (size_t i = 0; i < tal_count(n->chans); i++) {
|
|
|
|
struct half_chan *hc = half_chan_from(n, n->chans[i]);
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
if (!hc->channel_update_msgidx) {
|
2018-01-13 14:15:05 +01:00
|
|
|
/* Connection is not public yet, so don't even
|
|
|
|
* try to re-announce it */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
if (hc->last_timestamp > highwater) {
|
2018-01-13 14:15:05 +01:00
|
|
|
/* No need to send a keepalive update message */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
if (!hc->active) {
|
2018-01-13 14:15:05 +01:00
|
|
|
/* Only send keepalives for active connections */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
gossip_send_keepalive_update(daemon->rstate, hc);
|
2018-01-09 20:00:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 21:51:45 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
route_prune(daemon->rstate);
|
2018-01-09 16:29:10 +01:00
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static struct io_plan *connection_in(struct io_conn *conn, struct daemon *daemon)
|
|
|
|
{
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr addr;
|
2017-10-23 06:12:38 +02:00
|
|
|
struct sockaddr_storage s;
|
|
|
|
socklen_t len = sizeof(s);
|
|
|
|
|
|
|
|
if (getpeername(io_conn_fd(conn), (struct sockaddr *)&s, &len) != 0) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("Failed to get peername for incoming conn: %s",
|
|
|
|
strerror(errno));
|
2017-10-23 06:12:38 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *s6 = (void *)&s;
|
|
|
|
addr.type = ADDR_TYPE_IPV6;
|
|
|
|
addr.addrlen = sizeof(s6->sin6_addr);
|
|
|
|
BUILD_ASSERT(sizeof(s6->sin6_addr) <= sizeof(addr.addr));
|
|
|
|
memcpy(addr.addr, &s6->sin6_addr, addr.addrlen);
|
|
|
|
addr.port = ntohs(s6->sin6_port);
|
|
|
|
} else if (s.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s4 = (void *)&s;
|
|
|
|
addr.type = ADDR_TYPE_IPV4;
|
|
|
|
addr.addrlen = sizeof(s4->sin_addr);
|
|
|
|
BUILD_ASSERT(sizeof(s4->sin_addr) <= sizeof(addr.addr));
|
|
|
|
memcpy(addr.addr, &s4->sin_addr, addr.addrlen);
|
|
|
|
addr.port = ntohs(s4->sin_port);
|
2018-02-27 21:01:26 +01:00
|
|
|
} else {
|
|
|
|
status_broken("Unknown socket type %i for incoming conn",
|
|
|
|
s.ss_family);
|
2017-10-23 06:12:38 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* FIXME: Timeout */
|
2017-10-23 06:12:38 +02:00
|
|
|
return responder_handshake(conn, &daemon->id, &addr,
|
|
|
|
init_new_peer, daemon);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_listeners(struct daemon *daemon, u16 portnum)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
socklen_t len;
|
|
|
|
int fd1, fd2;
|
|
|
|
|
|
|
|
if (!portnum) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("Zero portnum, not listening for incoming");
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = htons(portnum);
|
|
|
|
|
|
|
|
memset(&addr6, 0, sizeof(addr6));
|
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
|
addr6.sin6_addr = in6addr_any;
|
|
|
|
addr6.sin6_port = htons(portnum);
|
|
|
|
|
|
|
|
/* IPv6, since on Linux that (usually) binds to IPv4 too. */
|
2018-01-26 14:20:25 +01:00
|
|
|
fd1 = make_listen_fd(AF_INET6, &addr6, sizeof(addr6), true);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
if (fd1 >= 0) {
|
|
|
|
struct sockaddr_in6 in6;
|
|
|
|
|
|
|
|
len = sizeof(in6);
|
|
|
|
if (getsockname(fd1, (void *)&in6, &len) != 0) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_broken("Failed get IPv6 sockname: %s",
|
|
|
|
strerror(errno));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
close_noerr(fd1);
|
|
|
|
fd1 = -1;
|
|
|
|
} else {
|
|
|
|
addr.sin_port = in6.sin6_port;
|
|
|
|
assert(portnum == ntohs(addr.sin_port));
|
|
|
|
status_trace("Creating IPv6 listener on port %u",
|
|
|
|
portnum);
|
|
|
|
io_new_listener(daemon, fd1, connection_in, daemon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just in case, aim for the same port... */
|
2018-01-26 14:20:25 +01:00
|
|
|
fd2 = make_listen_fd(AF_INET, &addr, sizeof(addr), false);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
if (fd2 >= 0) {
|
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname(fd2, (void *)&addr, &len) != 0) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_broken("Failed get IPv4 sockname: %s",
|
|
|
|
strerror(errno));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
close_noerr(fd2);
|
|
|
|
fd2 = -1;
|
|
|
|
} else {
|
|
|
|
assert(portnum == ntohs(addr.sin_port));
|
|
|
|
status_trace("Creating IPv4 listener on port %u",
|
|
|
|
portnum);
|
|
|
|
io_new_listener(daemon, fd2, connection_in, daemon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd1 < 0 && fd2 < 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not bind to a network address on port %u",
|
|
|
|
portnum);
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:05:33 +01:00
|
|
|
/**
|
|
|
|
* Callback function to be called whenever the master connection is idle
|
|
|
|
*/
|
|
|
|
static bool master_conn_idle(struct io_conn *conn UNUSED,
|
|
|
|
struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
const u8 *msg;
|
|
|
|
struct daemon *daemon = container_of(dc, struct daemon, master);
|
|
|
|
msg = gossip_store_read_next(tmpctx, daemon->rstate->store);
|
|
|
|
|
|
|
|
if (msg) {
|
2018-03-20 16:55:52 +01:00
|
|
|
handle_gossip_msg(daemon, msg, false);
|
2018-03-18 15:05:33 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-04-24 14:31:26 +02:00
|
|
|
/* Parse an incoming gossip init message and assign config variables
|
|
|
|
* to the daemon.
|
|
|
|
*/
|
|
|
|
static struct io_plan *gossip_init(struct daemon_conn *master,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2017-04-24 14:31:26 +02:00
|
|
|
{
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
u16 port;
|
2018-03-02 09:59:17 +01:00
|
|
|
u32 update_channel_interval;
|
2017-08-22 07:06:37 +02:00
|
|
|
|
2018-01-09 15:52:21 +01:00
|
|
|
if (!fromwire_gossipctl_init(
|
2018-02-20 21:59:09 +01:00
|
|
|
daemon, msg, &daemon->broadcast_interval, &chain_hash,
|
2018-01-09 15:52:21 +01:00
|
|
|
&daemon->id, &port, &daemon->globalfeatures,
|
|
|
|
&daemon->localfeatures, &daemon->wireaddrs, daemon->rgb,
|
2018-03-02 09:59:17 +01:00
|
|
|
daemon->alias, &update_channel_interval)) {
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIPCTL_INIT, msg);
|
2017-04-24 14:31:26 +02:00
|
|
|
}
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Prune time is twice update time */
|
|
|
|
daemon->rstate = new_routing_state(daemon, &chain_hash, &daemon->id,
|
|
|
|
update_channel_interval * 2);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2018-03-18 15:05:33 +01:00
|
|
|
master->msg_queue_cleared_cb = master_conn_idle;
|
|
|
|
|
2018-02-22 10:58:42 +01:00
|
|
|
setup_listeners(daemon, port);
|
2018-01-09 16:29:10 +01:00
|
|
|
|
|
|
|
new_reltimer(&daemon->timers, daemon,
|
2018-03-02 09:59:17 +01:00
|
|
|
time_from_sec(daemon->rstate->prune_timeout/4),
|
|
|
|
gossip_refresh_network, daemon);
|
2018-01-09 16:29:10 +01:00
|
|
|
|
2017-04-24 14:31:26 +02:00
|
|
|
return daemon_conn_read_next(master->conn, master);
|
|
|
|
}
|
|
|
|
|
2017-04-30 23:49:15 +02:00
|
|
|
static struct io_plan *resolve_channel_req(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2017-05-05 08:41:44 +02:00
|
|
|
struct pubkey *keys;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_resolve_channel_request(msg, &scid))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIP_RESOLVE_CHANNEL_REQUEST, msg);
|
2017-05-05 08:41:44 +02:00
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
chan = get_channel(daemon->rstate, &scid);
|
|
|
|
if (!chan) {
|
2017-06-06 01:47:10 +02:00
|
|
|
status_trace("Failed to resolve channel %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid));
|
2017-05-05 08:41:44 +02:00
|
|
|
keys = NULL;
|
2017-04-30 23:49:15 +02:00
|
|
|
} else {
|
2017-05-05 08:41:44 +02:00
|
|
|
keys = tal_arr(msg, struct pubkey, 2);
|
2018-03-02 09:59:16 +01:00
|
|
|
keys[0] = chan->nodes[0]->id;
|
|
|
|
keys[1] = chan->nodes[1]->id;
|
2017-06-06 01:47:10 +02:00
|
|
|
status_trace("Resolved channel %s %s<->%s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid),
|
|
|
|
type_to_string(tmpctx, struct pubkey, &keys[0]),
|
|
|
|
type_to_string(tmpctx, struct pubkey, &keys[1]));
|
2017-04-30 23:49:15 +02:00
|
|
|
}
|
2017-05-05 08:41:44 +02:00
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_resolve_channel_reply(NULL, keys)));
|
2017-04-30 23:49:15 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static struct io_plan *handshake_out_success(struct io_conn *conn,
|
|
|
|
const struct pubkey *id,
|
2017-10-23 06:17:38 +02:00
|
|
|
const struct wireaddr *addr,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
const struct crypto_state *cs,
|
|
|
|
struct reaching *reach)
|
|
|
|
{
|
2017-10-23 06:12:38 +02:00
|
|
|
return init_new_peer(conn, id, addr, cs, reach->daemon);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct io_plan *connection_out(struct io_conn *conn,
|
|
|
|
struct reaching *reach)
|
|
|
|
{
|
|
|
|
/* FIXME: Timeout */
|
|
|
|
status_trace("Connected out for %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &reach->id));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
return initiator_handshake(conn, &reach->daemon->id, &reach->id,
|
2017-10-23 06:12:38 +02:00
|
|
|
&reach->addr,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
handshake_out_success, reach);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void try_connect(struct reaching *reach);
|
|
|
|
|
|
|
|
static void connect_failed(struct io_conn *conn, struct reaching *reach)
|
|
|
|
{
|
2018-01-27 16:42:24 +01:00
|
|
|
u32 diff = time_now().ts.tv_sec - reach->first_attempt;
|
|
|
|
reach->attempts++;
|
|
|
|
|
|
|
|
if (reach->attempts >= reach->max_attempts) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("Failed to connect after %d attempts, giving up "
|
|
|
|
"after %d seconds",
|
|
|
|
reach->attempts, diff);
|
2018-01-27 17:18:45 +01:00
|
|
|
daemon_conn_send(
|
|
|
|
&reach->daemon->master,
|
|
|
|
take(towire_gossip_peer_connection_failed(
|
2018-03-15 07:10:22 +01:00
|
|
|
NULL, &reach->id, diff, reach->attempts, false)));
|
2018-01-27 16:42:24 +01:00
|
|
|
tal_free(reach);
|
|
|
|
} else {
|
|
|
|
status_trace("Failed connected out for %s, will try again",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &reach->id));
|
2018-01-27 16:42:24 +01:00
|
|
|
/* FIXME: Configurable timer! */
|
|
|
|
new_reltimer(&reach->daemon->timers, reach, time_from_sec(5),
|
|
|
|
try_connect, reach);
|
|
|
|
}
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 06:12:38 +02:00
|
|
|
static struct io_plan *conn_init(struct io_conn *conn, struct reaching *reach)
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
{
|
|
|
|
struct addrinfo ai;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
|
|
|
|
/* FIXME: make generic */
|
|
|
|
ai.ai_flags = 0;
|
|
|
|
ai.ai_socktype = SOCK_STREAM;
|
|
|
|
ai.ai_protocol = 0;
|
|
|
|
ai.ai_canonname = NULL;
|
|
|
|
ai.ai_next = NULL;
|
|
|
|
|
2017-10-23 06:12:38 +02:00
|
|
|
switch (reach->addr.type) {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
case ADDR_TYPE_IPV4:
|
|
|
|
ai.ai_family = AF_INET;
|
|
|
|
sin.sin_family = AF_INET;
|
2017-10-23 06:12:38 +02:00
|
|
|
sin.sin_port = htons(reach->addr.port);
|
|
|
|
memcpy(&sin.sin_addr, reach->addr.addr, sizeof(sin.sin_addr));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
ai.ai_addrlen = sizeof(sin);
|
|
|
|
ai.ai_addr = (struct sockaddr *)&sin;
|
|
|
|
break;
|
|
|
|
case ADDR_TYPE_IPV6:
|
|
|
|
ai.ai_family = AF_INET6;
|
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
2017-10-23 06:12:38 +02:00
|
|
|
sin6.sin6_port = htons(reach->addr.port);
|
|
|
|
memcpy(&sin6.sin6_addr, reach->addr.addr, sizeof(sin6.sin6_addr));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
ai.ai_addrlen = sizeof(sin6);
|
|
|
|
ai.ai_addr = (struct sockaddr *)&sin6;
|
|
|
|
break;
|
|
|
|
case ADDR_TYPE_PADDING:
|
|
|
|
/* Shouldn't happen. */
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
io_set_finish(conn, connect_failed, reach);
|
|
|
|
return io_connect(conn, &ai, connection_out, reach);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void try_connect(struct reaching *reach)
|
|
|
|
{
|
|
|
|
struct addrhint *a;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Already succeeded somehow? */
|
|
|
|
if (find_peer(reach->daemon, &reach->id)) {
|
|
|
|
status_trace("Already reached %s, not retrying",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &reach->id));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
tal_free(reach);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = find_addrhint(reach->daemon, &reach->id);
|
|
|
|
if (!a) {
|
|
|
|
/* FIXME: now try node table, dns lookups... */
|
2018-02-27 21:01:26 +01:00
|
|
|
status_info("No address known for %s, giving up",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &reach->id));
|
2018-01-27 17:18:45 +01:00
|
|
|
daemon_conn_send(
|
|
|
|
&reach->daemon->master,
|
|
|
|
take(towire_gossip_peer_connection_failed(
|
2018-03-15 07:10:22 +01:00
|
|
|
NULL, &reach->id,
|
2018-01-27 17:18:45 +01:00
|
|
|
time_now().ts.tv_sec - reach->first_attempt,
|
|
|
|
reach->attempts, true)));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
tal_free(reach);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Might not even be able to create eg. IPv6 sockets */
|
|
|
|
switch (a->addr.type) {
|
|
|
|
case ADDR_TYPE_IPV4:
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
break;
|
|
|
|
case ADDR_TYPE_IPV6:
|
|
|
|
fd = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fd = -1;
|
|
|
|
errno = EPROTONOSUPPORT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd < 0) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_broken("Can't open %i socket for %s (%s), giving up",
|
|
|
|
a->addr.type,
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &reach->id),
|
2018-02-27 21:01:26 +01:00
|
|
|
strerror(errno));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
tal_free(reach);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-23 06:12:38 +02:00
|
|
|
reach->addr = a->addr;
|
|
|
|
io_new_conn(reach, fd, conn_init, reach);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:30:54 +01:00
|
|
|
/* Returns true if we're already connected. */
|
|
|
|
static bool try_reach_peer(struct daemon *daemon, const struct pubkey *id)
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
{
|
|
|
|
struct reaching *reach;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (find_reaching(daemon, id)) {
|
|
|
|
/* FIXME: Perhaps kick timer in this case? */
|
2018-01-10 06:30:54 +01:00
|
|
|
status_trace("try_reach_peer: already trying to reach %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, id));
|
2018-01-10 06:30:54 +01:00
|
|
|
return false;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Master might find out before we do that a peer is dead; if we
|
|
|
|
* seem to be connected just mark it for reconnect. */
|
|
|
|
peer = find_peer(daemon, id);
|
|
|
|
if (peer) {
|
|
|
|
status_trace("reach_peer: have %s, will retry if it dies",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, id));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer->reach_again = true;
|
2018-01-10 06:30:54 +01:00
|
|
|
return true;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reach = tal(daemon, struct reaching);
|
|
|
|
reach->succeeded = false;
|
|
|
|
reach->daemon = daemon;
|
|
|
|
reach->id = *id;
|
2018-01-27 16:42:24 +01:00
|
|
|
reach->first_attempt = time_now().ts.tv_sec;
|
|
|
|
reach->attempts = 0;
|
|
|
|
reach->max_attempts = 10;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
list_add_tail(&daemon->reaching, &reach->list);
|
|
|
|
tal_add_destructor(reach, destroy_reaching);
|
|
|
|
|
|
|
|
try_connect(reach);
|
2018-01-10 06:30:54 +01:00
|
|
|
return false;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This catches all kinds of failures, like network errors. */
|
|
|
|
static struct io_plan *reach_peer(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossipctl_reach_peer(msg, &id))
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
master_badmsg(WIRE_GOSSIPCTL_REACH_PEER, msg);
|
|
|
|
|
2018-01-10 06:30:54 +01:00
|
|
|
/* Master can't check this itself, because that's racy. */
|
|
|
|
if (try_reach_peer(daemon, &id)) {
|
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_peer_already_connected(NULL,
|
2018-01-10 06:30:54 +01:00
|
|
|
&id)));
|
|
|
|
}
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *addr_hint(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct addrhint *a = tal(daemon, struct addrhint);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossipctl_peer_addrhint(msg, &a->id, &a->addr))
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
master_badmsg(WIRE_GOSSIPCTL_PEER_ADDRHINT, msg);
|
|
|
|
|
|
|
|
/* Replace any old one. */
|
|
|
|
tal_free(find_addrhint(daemon, &a->id));
|
|
|
|
|
|
|
|
list_add_tail(&daemon->addrhints, &a->list);
|
|
|
|
tal_add_destructor(a, destroy_addrhint);
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-10-23 06:19:38 +02:00
|
|
|
static struct io_plan *get_peers(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
size_t n = 0;
|
|
|
|
struct pubkey *id = tal_arr(conn, struct pubkey, n);
|
|
|
|
struct wireaddr *wireaddr = tal_arr(conn, struct wireaddr, n);
|
2018-02-27 06:08:34 +01:00
|
|
|
const struct gossip_getnodes_entry **nodes;
|
2018-01-17 00:53:20 +01:00
|
|
|
struct pubkey *specific_id = NULL;
|
2018-02-27 06:08:34 +01:00
|
|
|
struct node_map_iter it;
|
2017-10-23 06:19:38 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_getpeers_request(msg, msg, &specific_id))
|
2017-10-23 06:19:38 +02:00
|
|
|
master_badmsg(WIRE_GOSSIPCTL_PEER_ADDRHINT, msg);
|
|
|
|
|
2018-02-27 06:08:34 +01:00
|
|
|
nodes = tal_arr(conn, const struct gossip_getnodes_entry*, 0);
|
2017-10-23 06:19:38 +02:00
|
|
|
list_for_each(&daemon->peers, peer, list) {
|
2018-01-17 00:53:20 +01:00
|
|
|
if (specific_id && !pubkey_eq(specific_id, &peer->id))
|
|
|
|
continue;
|
2017-10-23 06:19:38 +02:00
|
|
|
tal_resize(&id, n+1);
|
|
|
|
tal_resize(&wireaddr, n+1);
|
|
|
|
|
|
|
|
id[n] = peer->id;
|
|
|
|
wireaddr[n] = peer->addr;
|
2018-02-27 06:08:34 +01:00
|
|
|
|
|
|
|
struct node* nd = NULL;
|
|
|
|
for (nd = node_map_first(daemon->rstate->nodes, &it); nd; nd = node_map_next(daemon->rstate->nodes, &it)) {
|
|
|
|
if (pubkey_eq(&nd->id, &peer->id)) {
|
|
|
|
append_node(&nodes, nd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-23 06:19:38 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_getpeers_reply(NULL, id, wireaddr, nodes)));
|
2017-10-23 06:19:38 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
static struct io_plan *handle_txout_reply(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
|
|
|
u8 *outscript;
|
2018-03-05 23:25:00 +01:00
|
|
|
u64 satoshis;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-03-05 23:25:00 +01:00
|
|
|
if (!fromwire_gossip_get_txout_reply(msg, msg, &scid, &satoshis, &outscript))
|
2018-01-04 12:40:58 +01:00
|
|
|
master_badmsg(WIRE_GOSSIP_GET_TXOUT_REPLY, msg);
|
|
|
|
|
2018-03-05 23:32:04 +01:00
|
|
|
if (handle_pending_cannouncement(daemon->rstate, &scid, satoshis, outscript))
|
2018-01-04 12:40:58 +01:00
|
|
|
send_node_announcement(daemon);
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:13:19 +01:00
|
|
|
static struct io_plan *handle_disable_channel(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, u8 *msg)
|
|
|
|
{
|
2018-01-23 22:14:53 +01:00
|
|
|
struct short_channel_id scid;
|
|
|
|
u8 direction;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
|
|
|
struct half_chan *hc;
|
2018-01-23 22:14:53 +01:00
|
|
|
bool active;
|
|
|
|
u16 flags, cltv_expiry_delta;
|
|
|
|
u32 timestamp, fee_base_msat, fee_proportional_millionths;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
u64 htlc_minimum_msat;
|
2018-03-08 05:10:33 +01:00
|
|
|
u8 *err;
|
2018-03-13 16:40:55 +01:00
|
|
|
const u8 *old_update;
|
2018-01-23 22:14:53 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_disable_channel(msg, &scid, &direction, &active) ) {
|
2018-02-27 21:01:26 +01:00
|
|
|
status_unusual("Unable to parse %s",
|
|
|
|
gossip_wire_type_name(fromwire_peektype(msg)));
|
2018-01-23 22:14:53 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
chan = get_channel(daemon->rstate, &scid);
|
2018-03-02 09:59:17 +01:00
|
|
|
if (!chan) {
|
2018-01-23 22:14:53 +01:00
|
|
|
status_trace(
|
2018-03-02 09:59:17 +01:00
|
|
|
"Unable to find channel %s",
|
|
|
|
type_to_string(msg, struct short_channel_id, &scid));
|
2018-01-23 22:14:53 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-03-04 03:26:59 +01:00
|
|
|
hc = &chan->half[direction];
|
2018-01-23 22:14:53 +01:00
|
|
|
|
|
|
|
status_trace("Disabling channel %s/%d, active %d -> %d",
|
|
|
|
type_to_string(msg, struct short_channel_id, &scid),
|
2018-03-04 03:26:59 +01:00
|
|
|
direction, hc->active, active);
|
2018-01-23 22:14:53 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
hc->active = active;
|
2018-01-23 22:14:53 +01:00
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
if (!hc->channel_update_msgidx) {
|
2018-01-23 22:14:53 +01:00
|
|
|
status_trace(
|
|
|
|
"Channel %s/%d doesn't have a channel_update yet, can't "
|
|
|
|
"disable",
|
|
|
|
type_to_string(msg, struct short_channel_id, &scid),
|
|
|
|
direction);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
old_update = get_broadcast(daemon->rstate->broadcasts,
|
|
|
|
hc->channel_update_msgidx);
|
|
|
|
|
2018-01-23 22:14:53 +01:00
|
|
|
if (!fromwire_channel_update(
|
2018-03-13 16:40:55 +01:00
|
|
|
old_update, &sig, &chain_hash, &scid, ×tamp,
|
2018-01-23 22:14:53 +01:00
|
|
|
&flags, &cltv_expiry_delta, &htlc_minimum_msat, &fee_base_msat,
|
|
|
|
&fee_proportional_millionths)) {
|
|
|
|
status_failed(
|
|
|
|
STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Unable to parse previously accepted channel_update");
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp = time_now().ts.tv_sec;
|
2018-03-04 03:26:59 +01:00
|
|
|
if (timestamp <= hc->last_timestamp)
|
|
|
|
timestamp = hc->last_timestamp + 1;
|
2018-01-23 22:14:53 +01:00
|
|
|
|
|
|
|
/* Active is bit 1 << 1, mask and apply */
|
|
|
|
flags = (0xFFFD & flags) | (!active << 1);
|
|
|
|
|
|
|
|
msg = towire_channel_update(tmpctx, &sig, &chain_hash, &scid, timestamp,
|
|
|
|
flags, cltv_expiry_delta, htlc_minimum_msat,
|
|
|
|
fee_base_msat, fee_proportional_millionths);
|
|
|
|
|
|
|
|
if (!wire_sync_write(HSM_FD,
|
|
|
|
towire_hsm_cupdate_sig_req(tmpctx, msg))) {
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO, "Writing cupdate_sig_req: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = wire_sync_read(tmpctx, HSM_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!msg || !fromwire_hsm_cupdate_sig_reply(tmpctx, msg, &msg)) {
|
2018-01-23 22:14:53 +01:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Reading cupdate_sig_req: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
2018-03-20 16:55:52 +01:00
|
|
|
err = handle_channel_update(daemon->rstate, msg, true);
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"rejected disabling channel_update: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, err));
|
2018-01-23 22:14:53 +01:00
|
|
|
|
|
|
|
fail:
|
2018-01-23 22:13:19 +01:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-01-18 00:32:36 +01:00
|
|
|
static struct io_plan *handle_routing_failure(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey erring_node;
|
|
|
|
struct short_channel_id erring_channel;
|
|
|
|
u16 failcode;
|
2018-01-21 01:36:41 +01:00
|
|
|
u8 *channel_update;
|
2018-01-18 00:32:36 +01:00
|
|
|
|
2018-01-21 01:36:41 +01:00
|
|
|
if (!fromwire_gossip_routing_failure(msg,
|
2018-02-20 21:59:09 +01:00
|
|
|
msg,
|
2018-01-18 00:32:36 +01:00
|
|
|
&erring_node,
|
|
|
|
&erring_channel,
|
2018-01-21 01:36:41 +01:00
|
|
|
&failcode,
|
|
|
|
&channel_update))
|
2018-01-18 00:32:36 +01:00
|
|
|
master_badmsg(WIRE_GOSSIP_ROUTING_FAILURE, msg);
|
|
|
|
|
|
|
|
routing_failure(daemon->rstate,
|
|
|
|
&erring_node,
|
|
|
|
&erring_channel,
|
2018-01-21 01:36:41 +01:00
|
|
|
(enum onion_type) failcode,
|
|
|
|
channel_update);
|
2018-01-18 00:32:36 +01:00
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-02-06 16:32:06 +01:00
|
|
|
static struct io_plan *
|
|
|
|
handle_mark_channel_unroutable(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id channel;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_mark_channel_unroutable(msg, &channel))
|
2018-02-06 16:32:06 +01:00
|
|
|
master_badmsg(WIRE_GOSSIP_MARK_CHANNEL_UNROUTABLE, msg);
|
|
|
|
|
|
|
|
mark_channel_unroutable(daemon->rstate, &channel);
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-01-23 22:13:19 +01:00
|
|
|
|
2017-03-19 21:28:29 +01:00
|
|
|
static struct io_plan *recv_req(struct io_conn *conn, struct daemon_conn *master)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-03-19 21:28:29 +01:00
|
|
|
struct daemon *daemon = container_of(master, struct daemon, master);
|
|
|
|
enum gossip_wire_type t = fromwire_peektype(master->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
status_trace("req: type %s len %zu",
|
2017-03-19 21:28:29 +01:00
|
|
|
gossip_wire_type_name(t), tal_count(master->msg_in));
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
switch (t) {
|
2017-04-24 14:31:26 +02:00
|
|
|
case WIRE_GOSSIPCTL_INIT:
|
|
|
|
return gossip_init(master, daemon, master->msg_in);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER:
|
2017-03-19 21:28:29 +01:00
|
|
|
return release_peer(conn, daemon, master->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REQUEST:
|
2018-01-16 20:44:32 +01:00
|
|
|
return getnodes(conn, daemon, daemon->master.msg_in);
|
2017-03-12 13:39:23 +01:00
|
|
|
|
2017-03-15 11:36:52 +01:00
|
|
|
case WIRE_GOSSIP_GETROUTE_REQUEST:
|
2017-03-15 13:46:29 +01:00
|
|
|
return getroute_req(conn, daemon, daemon->master.msg_in);
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-22 13:30:09 +01:00
|
|
|
case WIRE_GOSSIP_GETCHANNELS_REQUEST:
|
|
|
|
return getchannels_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-04-12 20:20:48 +02:00
|
|
|
case WIRE_GOSSIP_PING:
|
|
|
|
return ping_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-04-30 23:49:15 +02:00
|
|
|
case WIRE_GOSSIP_RESOLVE_CHANNEL_REQUEST:
|
|
|
|
return resolve_channel_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-12-11 04:16:50 +01:00
|
|
|
case WIRE_GOSSIPCTL_HAND_BACK_PEER:
|
|
|
|
return hand_back_peer(conn, daemon, master->msg_in);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
case WIRE_GOSSIPCTL_REACH_PEER:
|
|
|
|
return reach_peer(conn, daemon, master->msg_in);
|
|
|
|
|
|
|
|
case WIRE_GOSSIPCTL_PEER_ADDRHINT:
|
|
|
|
return addr_hint(conn, daemon, master->msg_in);
|
2017-10-11 11:57:50 +02:00
|
|
|
|
2017-10-23 06:19:38 +02:00
|
|
|
case WIRE_GOSSIP_GETPEERS_REQUEST:
|
|
|
|
return get_peers(conn, daemon, master->msg_in);
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
case WIRE_GOSSIP_GET_TXOUT_REPLY:
|
2018-01-04 12:40:58 +01:00
|
|
|
return handle_txout_reply(conn, daemon, master->msg_in);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-01-23 22:13:19 +01:00
|
|
|
case WIRE_GOSSIP_DISABLE_CHANNEL:
|
|
|
|
return handle_disable_channel(conn, daemon, master->msg_in);
|
|
|
|
|
2018-01-18 00:32:36 +01:00
|
|
|
case WIRE_GOSSIP_ROUTING_FAILURE:
|
|
|
|
return handle_routing_failure(conn, daemon, master->msg_in);
|
|
|
|
|
2018-02-06 16:32:06 +01:00
|
|
|
case WIRE_GOSSIP_MARK_CHANNEL_UNROUTABLE:
|
|
|
|
return handle_mark_channel_unroutable(conn, daemon, master->msg_in);
|
|
|
|
|
2018-03-05 17:16:20 +01:00
|
|
|
case WIRE_GOSSIPCTL_PEER_DISCONNECT:
|
|
|
|
return disconnect_peer(conn, daemon, master->msg_in);
|
|
|
|
|
2017-12-15 15:16:42 +01:00
|
|
|
/* We send these, we don't receive them */
|
2017-03-10 11:50:43 +01:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY:
|
2017-06-24 08:25:53 +02:00
|
|
|
case WIRE_GOSSIPCTL_RELEASE_PEER_REPLYFAIL:
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REPLY:
|
2017-03-15 11:36:52 +01:00
|
|
|
case WIRE_GOSSIP_GETROUTE_REPLY:
|
2017-03-22 13:30:09 +01:00
|
|
|
case WIRE_GOSSIP_GETCHANNELS_REPLY:
|
2017-10-23 06:19:38 +02:00
|
|
|
case WIRE_GOSSIP_GETPEERS_REPLY:
|
2017-04-12 20:20:48 +02:00
|
|
|
case WIRE_GOSSIP_PING_REPLY:
|
2017-04-30 23:49:15 +02:00
|
|
|
case WIRE_GOSSIP_RESOLVE_CHANNEL_REPLY:
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
case WIRE_GOSSIP_PEER_CONNECTED:
|
2018-01-10 06:30:54 +01:00
|
|
|
case WIRE_GOSSIP_PEER_ALREADY_CONNECTED:
|
2018-01-27 15:59:18 +01:00
|
|
|
case WIRE_GOSSIP_PEER_CONNECTION_FAILED:
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
case WIRE_GOSSIP_PEER_NONGOSSIP:
|
2017-11-28 23:52:32 +01:00
|
|
|
case WIRE_GOSSIP_GET_UPDATE:
|
|
|
|
case WIRE_GOSSIP_GET_UPDATE_REPLY:
|
2017-12-11 04:33:16 +01:00
|
|
|
case WIRE_GOSSIP_SEND_GOSSIP:
|
2017-12-15 15:16:42 +01:00
|
|
|
case WIRE_GOSSIP_LOCAL_ADD_CHANNEL:
|
2018-01-04 12:40:58 +01:00
|
|
|
case WIRE_GOSSIP_GET_TXOUT:
|
2018-03-05 17:16:20 +01:00
|
|
|
case WIRE_GOSSIPCTL_PEER_DISCONNECT_REPLY:
|
|
|
|
case WIRE_GOSSIPCTL_PEER_DISCONNECT_REPLYFAIL:
|
2017-12-15 15:16:42 +01:00
|
|
|
break;
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 06:55:52 +02:00
|
|
|
/* Master shouldn't give bad requests. */
|
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "%i: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
t, tal_hex(tmpctx, master->msg_in));
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TESTING
|
2018-02-21 16:06:07 +01:00
|
|
|
static void master_gone(struct io_conn *unused UNUSED, struct daemon_conn *dc UNUSED)
|
2017-06-06 05:08:42 +02:00
|
|
|
{
|
|
|
|
/* Can't tell master, it's gone. */
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
2018-01-08 11:01:09 +01:00
|
|
|
subdaemon_setup(argc, argv);
|
2017-09-28 05:40:59 +02:00
|
|
|
io_poll_override(debug_poll);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
daemon = tal(NULL, struct daemon);
|
|
|
|
list_head_init(&daemon->peers);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
list_head_init(&daemon->reaching);
|
|
|
|
list_head_init(&daemon->addrhints);
|
2017-02-04 16:28:35 +01:00
|
|
|
timers_init(&daemon->timers, time_mono());
|
2017-04-24 14:31:26 +02:00
|
|
|
daemon->broadcast_interval = 30000;
|
2018-01-04 12:40:46 +01:00
|
|
|
daemon->last_announce_timestamp = 0;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
/* stdin == control */
|
2017-06-06 05:08:42 +02:00
|
|
|
daemon_conn_init(daemon, &daemon->master, STDIN_FILENO, recv_req,
|
|
|
|
master_gone);
|
2017-03-19 21:32:44 +01:00
|
|
|
status_setup_async(&daemon->master);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
hsm_setup(HSM_FD);
|
2017-04-12 08:12:15 +02:00
|
|
|
|
|
|
|
/* When conn closes, everything is freed. */
|
|
|
|
tal_steal(daemon->master.conn, daemon);
|
|
|
|
|
2017-02-04 16:28:35 +01:00
|
|
|
for (;;) {
|
|
|
|
struct timer *expired = NULL;
|
|
|
|
io_loop(&daemon->timers, &expired);
|
|
|
|
|
|
|
|
if (!expired) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
timer_expired(daemon, expired);
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|