connectd: do dev_disconnect logic.

As connectd handles more packets itself, or diverts them to/from gossipd,
it's the only place we can implement the dev_disconnect logic.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2022-01-08 23:55:29 +10:30
parent 9c0bb444b7
commit 7a514112ec
17 changed files with 71 additions and 70 deletions

View File

@@ -18,6 +18,7 @@
#include <common/bech32.h>
#include <common/bech32_util.h>
#include <common/daemon_conn.h>
#include <common/dev_disconnect.h>
#include <common/ecdh_hsmd.h>
#include <common/jsonrpc_errors.h>
#include <common/memleak.h>
@@ -1598,6 +1599,7 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
enum addr_listen_announce *proposed_listen_announce;
struct wireaddr *announcable;
char *tor_password;
bool dev_disconnect;
/* Fields which require allocation are allocated off daemon */
if (!fromwire_connectd_init(
@@ -1613,7 +1615,8 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
&daemon->use_v3_autotor,
&daemon->timeout_secs,
&daemon->websocket_helper,
&daemon->websocket_port)) {
&daemon->websocket_port,
&dev_disconnect)) {
/* This is a helper which prints the type expected and the actual
* message, then exits (it should never be called!). */
master_badmsg(WIRE_CONNECTD_INIT, msg);
@@ -1657,6 +1660,10 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
take(towire_connectd_init_reply(NULL,
binding,
announcable)));
#if DEVELOPER
if (dev_disconnect)
dev_disconnect_init(5);
#endif
}
/*~ lightningd tells us to go! */

View File

@@ -21,6 +21,8 @@ msgdata,connectd_init,use_v3_autotor,bool,
msgdata,connectd_init,timeout_secs,u32,
msgdata,connectd_init,websocket_helper,wirestring,
msgdata,connectd_init,websocket_port,u16,
# If this is set, then fd 5 is dev_disconnect_fd.
msgdata,connectd_init,dev_disconnect,bool,
# Connectd->master, here are the addresses I bound, can announce.
msgtype,connectd_init_reply,2100
1 #include <bitcoin/block.h>
21 msgdata,connectd_init,websocket_helper,wirestring,
22 msgdata,connectd_init,websocket_port,u16,
23 # Connectd->master, here are the addresses I bound, can announce. # If this is set, then fd 5 is dev_disconnect_fd.
24 msgdata,connectd_init,dev_disconnect,bool,
25 # Connectd->master, here are the addresses I bound, can announce.
26 msgtype,connectd_init_reply,2100
27 msgdata,connectd_init_reply,num_bindings,u16,
28 msgdata,connectd_init_reply,bindings,wireaddr_internal,num_bindings

View File

@@ -4,6 +4,7 @@
#include <assert.h>
#include <ccan/io/io.h>
#include <common/cryptomsg.h>
#include <common/dev_disconnect.h>
#include <common/per_peer_state.h>
#include <common/status.h>
#include <common/utils.h>
@@ -11,6 +12,7 @@
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <wire/wire.h>
#include <wire/wire_io.h>
void queue_peer_msg(struct peer *peer, const u8 *msg TAKES)
@@ -32,12 +34,48 @@ static struct io_plan *after_final_msg(struct io_conn *peer_conn,
return io_close(peer_conn);
}
#if DEVELOPER
static struct io_plan *write_to_peer(struct io_conn *peer_conn,
struct peer *peer);
static struct io_plan *dev_leave_hanging(struct io_conn *peer_conn,
struct peer *peer)
{
/* We don't tell the peer we're disconnecting, but from now on
* our writes go nowhere, and there's nothing to read. */
dev_sabotage_fd(io_conn_fd(peer_conn), false);
return write_to_peer(peer_conn, peer);
}
#endif /* DEVELOPER */
static struct io_plan *encrypt_and_send(struct peer *peer,
const u8 *msg TAKES,
struct io_plan *(*next)
(struct io_conn *peer_conn,
struct peer *peer))
{
#if DEVELOPER
int type = fromwire_peektype(msg);
switch (dev_disconnect(&peer->id, type)) {
case DEV_DISCONNECT_BEFORE:
if (taken(msg))
tal_free(msg);
return io_close(peer->to_peer);
case DEV_DISCONNECT_AFTER:
next = (void *)io_close_cb;
break;
case DEV_DISCONNECT_BLACKHOLE:
dev_blackhole_fd(io_conn_fd(peer->to_peer));
break;
case DEV_DISCONNECT_NORMAL:
break;
case DEV_DISCONNECT_DISABLE_AFTER:
next = dev_leave_hanging;
break;
}
#endif
/* We free this and the encrypted version in next write_to_peer */
peer->sent_to_peer = cryptomsg_encrypt_msg(peer, &peer->cs, msg);
return io_write(peer->to_peer,

View File

@@ -208,7 +208,7 @@ struct io_plan *peer_exchange_initmsg(struct io_conn *conn,
next = read_init;
#if DEVELOPER
switch (dev_disconnect(WIRE_INIT)) {
switch (dev_disconnect(&peer->id, WIRE_INIT)) {
case DEV_DISCONNECT_BEFORE:
dev_sabotage_fd(io_conn_fd(conn), true);
break;