forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_sync.c
37 lines (32 loc) · 797 Bytes
/
wire_sync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "config.h"
#include <assert.h>
#include <ccan/read_write_all/read_write_all.h>
#include <errno.h>
#include <wire/wire_io.h>
#include <wire/wire_sync.h>
bool wire_sync_write(int fd, const void *msg TAKES)
{
wire_len_t hdr = cpu_to_wirelen(tal_bytelen(msg));
bool ret;
assert(tal_bytelen(msg) < WIRE_LEN_LIMIT);
ret = write_all(fd, &hdr, sizeof(hdr))
&& write_all(fd, msg, tal_count(msg));
if (taken(msg))
tal_free(msg);
return ret;
}
u8 *wire_sync_read(const tal_t *ctx, int fd)
{
wire_len_t len;
u8 *msg;
if (!read_all(fd, &len, sizeof(len)))
return NULL;
if (wirelen_to_cpu(len) >= WIRE_LEN_LIMIT) {
errno = E2BIG;
return NULL;
}
msg = tal_arr(ctx, u8, wirelen_to_cpu(len));
if (!read_all(fd, msg, wirelen_to_cpu(len)))
return tal_free(msg);
return msg;
}