2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/crypto_sync.h>
|
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/dev_disconnect.h>
|
2018-08-02 08:49:55 +02:00
|
|
|
#include <common/peer_failed.h>
|
2019-06-03 20:11:25 +02:00
|
|
|
#include <common/per_peer_state.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2018-08-09 02:30:30 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2021-09-19 13:07:58 +02:00
|
|
|
#include <sys/socket.h>
|
2017-05-24 12:10:16 +02:00
|
|
|
#include <wire/wire.h>
|
2022-01-08 14:22:29 +01:00
|
|
|
#include <wire/wire_io.h>
|
|
|
|
#include <wire/wire_sync.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
void sync_crypto_write(struct per_peer_state *pps, const void *msg TAKES)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2021-06-14 23:07:36 +02:00
|
|
|
bool post_sabotage = false, post_close;
|
2017-06-27 04:55:06 +02:00
|
|
|
int type = fromwire_peektype(msg);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2019-11-17 12:42:33 +01:00
|
|
|
status_peer_io(LOG_IO_OUT, NULL, msg);
|
2018-02-05 05:09:28 +01:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-06-27 04:55:06 +02:00
|
|
|
switch (dev_disconnect(type)) {
|
2017-05-24 12:10:16 +02:00
|
|
|
case DEV_DISCONNECT_BEFORE:
|
2021-06-14 23:07:36 +02:00
|
|
|
dev_sabotage_fd(pps->peer_fd, true);
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-05-24 12:10:16 +02:00
|
|
|
case DEV_DISCONNECT_AFTER:
|
|
|
|
post_sabotage = true;
|
2021-06-14 23:07:36 +02:00
|
|
|
post_close = true;
|
2017-05-24 12:10:16 +02:00
|
|
|
break;
|
2017-09-06 03:07:19 +02:00
|
|
|
case DEV_DISCONNECT_BLACKHOLE:
|
2019-06-03 20:11:25 +02:00
|
|
|
dev_blackhole_fd(pps->peer_fd);
|
2017-09-06 03:07:19 +02:00
|
|
|
break;
|
2017-09-26 06:57:31 +02:00
|
|
|
case DEV_DISCONNECT_NORMAL:
|
2017-05-24 12:10:16 +02:00
|
|
|
break;
|
2021-06-14 23:07:36 +02:00
|
|
|
case DEV_DISCONNECT_DISABLE_AFTER:
|
|
|
|
post_sabotage = true;
|
|
|
|
post_close = false;
|
|
|
|
break;
|
2017-05-24 12:10:16 +02:00
|
|
|
}
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2022-01-08 14:22:29 +01:00
|
|
|
if (!wire_sync_write(pps->peer_fd, msg))
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-05-24 12:10:16 +02:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-05-24 12:10:16 +02:00
|
|
|
if (post_sabotage)
|
2021-06-14 23:07:36 +02:00
|
|
|
dev_sabotage_fd(pps->peer_fd, post_close);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:30:30 +02:00
|
|
|
/* We're happy for the kernel to batch update and gossip messages, but a
|
|
|
|
* commitment message, for example, should be instantly sent. There's no
|
|
|
|
* great way of doing this, unfortunately.
|
|
|
|
*
|
|
|
|
* Setting TCP_NODELAY on Linux flushes the socket, which really means
|
|
|
|
* we'd want to toggle on then off it *after* sending. But Linux has
|
|
|
|
* TCP_CORK. On FreeBSD, it seems (looking at source) not to, so
|
|
|
|
* there we'd want to set it before the send, and reenable it
|
|
|
|
* afterwards. Even if this is wrong on other non-Linux platforms, it
|
|
|
|
* only means one extra packet.
|
|
|
|
*/
|
2019-06-03 20:11:25 +02:00
|
|
|
void sync_crypto_write_no_delay(struct per_peer_state *pps,
|
2018-08-09 02:30:30 +02:00
|
|
|
const void *msg TAKES)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
int opt;
|
|
|
|
const char *optname;
|
|
|
|
static bool complained = false;
|
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
opt = TCP_CORK;
|
|
|
|
optname = "TCP_CORK";
|
|
|
|
#elif defined(TCP_NODELAY)
|
|
|
|
opt = TCP_NODELAY;
|
|
|
|
optname = "TCP_NODELAY";
|
|
|
|
#else
|
|
|
|
#error "Please report platform with neither TCP_CORK nor TCP_NODELAY?"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
val = 1;
|
2019-06-03 20:11:25 +02:00
|
|
|
if (setsockopt(pps->peer_fd, IPPROTO_TCP, opt, &val, sizeof(val)) != 0) {
|
2018-08-09 02:30:30 +02:00
|
|
|
/* This actually happens in testing, where we blackhole the fd */
|
|
|
|
if (!complained) {
|
2019-06-30 02:42:44 +02:00
|
|
|
status_unusual("setsockopt %s=1: %s",
|
|
|
|
optname,
|
|
|
|
strerror(errno));
|
2018-08-09 02:30:30 +02:00
|
|
|
complained = true;
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 20:11:25 +02:00
|
|
|
sync_crypto_write(pps, msg);
|
2018-08-09 02:30:30 +02:00
|
|
|
|
|
|
|
val = 0;
|
2019-06-03 20:11:25 +02:00
|
|
|
setsockopt(pps->peer_fd, IPPROTO_TCP, opt, &val, sizeof(val));
|
2018-08-09 02:30:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
u8 *sync_crypto_read(const tal_t *ctx, struct per_peer_state *pps)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2022-01-08 14:22:29 +01:00
|
|
|
u8 *dec = wire_sync_read(ctx, pps->peer_fd);
|
2017-02-24 06:52:56 +01:00
|
|
|
if (!dec)
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2022-01-08 14:22:29 +01:00
|
|
|
|
|
|
|
status_peer_io(LOG_IO_IN, NULL, dec);
|
2018-02-05 05:09:28 +01:00
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
return dec;
|
|
|
|
}
|