core-lightning/common/crypto_sync.c
Rusty Russell 5889ad5fc4 dev-disconnect: add blackhole option.
To reproduce the next bug, I had to ensure that one node keeps thinking it's
disconnected, then the other node reconnects, then the first node realizes
it's disconnected.

This code does that, adding a '0' dev-disconnect modifier.  That means
we fork off a process which (due to pipebuf) will accept a little
data, but when the dev_disconnect file is truncated (a hacky, but
effective, signalling mechanism) will exit, as if the socket finally
realized it's not connected any more.

The python tests hang waiting for the daemon to terminate if you leave
the blackhole around; to give a clue as to what's happening in this
case I moved the log dump to before killing the daemon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-06 19:11:21 +02:00

71 lines
1.6 KiB
C

#include <ccan/read_write_all/read_write_all.h>
#include <common/crypto_sync.h>
#include <common/cryptomsg.h>
#include <common/dev_disconnect.h>
#include <common/status.h>
#include <common/utils.h>
#include <errno.h>
#include <inttypes.h>
#include <wire/wire.h>
#include <wire/wire_sync.h>
bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
{
int type = fromwire_peektype(msg);
u8 *enc = cryptomsg_encrypt_msg(NULL, cs, msg);
bool ret;
bool post_sabotage = false;
switch (dev_disconnect(type)) {
case DEV_DISCONNECT_BEFORE:
dev_sabotage_fd(fd);
return false;
case DEV_DISCONNECT_DROPPKT:
enc = tal_free(enc); /* FALL THRU */
case DEV_DISCONNECT_AFTER:
post_sabotage = true;
break;
case DEV_DISCONNECT_BLACKHOLE:
dev_blackhole_fd(fd);
break;
default:
break;
}
ret = write_all(fd, enc, tal_len(enc));
tal_free(enc);
if (post_sabotage)
dev_sabotage_fd(fd);
return ret;
}
u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd)
{
u8 hdr[18], *enc, *dec;
u16 len;
if (!read_all(fd, hdr, sizeof(hdr))) {
status_trace("Failed reading header: %s", strerror(errno));
return NULL;
}
if (!cryptomsg_decrypt_header(cs, hdr, &len)) {
status_trace("Failed hdr decrypt with rn=%"PRIu64, cs->rn-1);
return NULL;
}
enc = tal_arr(ctx, u8, len + 16);
if (!read_all(fd, enc, tal_len(enc))) {
status_trace("Failed reading body: %s", strerror(errno));
return tal_free(enc);
}
dec = cryptomsg_decrypt_body(ctx, cs, enc);
tal_free(enc);
if (!dec)
status_trace("Failed body decrypt with rn=%"PRIu64, cs->rn-2);
else
status_trace("Read decrypt %s", tal_hex(trc, dec));
return dec;
}