2021-02-01 03:58:50 +01:00
|
|
|
#include "config.h"
|
2017-01-10 05:51:20 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2017-09-28 05:42:19 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <wire/wire_io.h>
|
|
|
|
#include <wire/wire_sync.h>
|
2017-01-10 05:51:20 +01:00
|
|
|
|
2017-06-23 08:33:20 +02:00
|
|
|
bool wire_sync_write(int fd, const void *msg TAKES)
|
2017-01-10 05:51:20 +01:00
|
|
|
{
|
2018-07-28 08:00:16 +02:00
|
|
|
wire_len_t hdr = cpu_to_wirelen(tal_bytelen(msg));
|
2017-06-23 08:33:20 +02:00
|
|
|
bool ret;
|
2017-01-10 05:51:20 +01:00
|
|
|
|
2018-07-28 08:00:16 +02:00
|
|
|
assert(tal_bytelen(msg) < WIRE_LEN_LIMIT);
|
2017-09-29 02:56:32 +02:00
|
|
|
ret = write_all(fd, &hdr, sizeof(hdr))
|
|
|
|
&& write_all(fd, msg, tal_count(msg));
|
2017-06-23 08:33:20 +02:00
|
|
|
|
|
|
|
if (taken(msg))
|
|
|
|
tal_free(msg);
|
|
|
|
return ret;
|
2017-01-10 05:51:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 *wire_sync_read(const tal_t *ctx, int fd)
|
|
|
|
{
|
2017-09-28 05:42:19 +02:00
|
|
|
wire_len_t len;
|
2017-01-10 05:51:20 +01:00
|
|
|
u8 *msg;
|
|
|
|
|
2017-09-28 05:42:19 +02:00
|
|
|
if (!read_all(fd, &len, sizeof(len)))
|
2017-01-10 05:51:20 +01:00
|
|
|
return NULL;
|
2017-09-29 02:56:32 +02:00
|
|
|
if (wirelen_to_cpu(len) >= WIRE_LEN_LIMIT) {
|
2017-09-28 05:42:19 +02:00
|
|
|
errno = E2BIG;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-29 02:56:32 +02:00
|
|
|
msg = tal_arr(ctx, u8, wirelen_to_cpu(len));
|
|
|
|
if (!read_all(fd, msg, wirelen_to_cpu(len)))
|
2017-01-10 05:51:20 +01:00
|
|
|
return tal_free(msg);
|
|
|
|
return msg;
|
|
|
|
}
|