diff --git a/wire/wire_io.c b/wire/wire_io.c index 4e58b9da7..3c2e1b9ea 100644 --- a/wire/wire_io.c +++ b/wire/wire_io.c @@ -34,7 +34,7 @@ static int do_read_wire_header(int fd, struct io_plan_arg *arg) /* Length bytes read? Set up for normal read of data. */ if (arg->u2.s == INSIDE_HEADER_BIT + HEADER_LEN) { - arg->u2.s = *(wire_len_t *)p; + arg->u2.s = wirelen_to_cpu(*(wire_len_t *)p); if (arg->u2.s >= INSIDE_HEADER_BIT) { errno = E2BIG; return -1; @@ -88,7 +88,7 @@ static int do_write_wire_header(int fd, struct io_plan_arg *arg) { ssize_t ret; size_t len = arg->u2.s & ~INSIDE_HEADER_BIT; - wire_len_t hdr = tal_count(arg->u1.const_vp); + wire_len_t hdr = cpu_to_wirelen(tal_count(arg->u1.const_vp)); ret = write(fd, (char *)&hdr + len, HEADER_LEN - len); if (ret <= 0) diff --git a/wire/wire_io.h b/wire/wire_io.h index b381ece5e..9bff19000 100644 --- a/wire/wire_io.h +++ b/wire/wire_io.h @@ -2,12 +2,15 @@ #define LIGHTNING_WIRE_WIRE_IO_H #include "config.h" #include +#include #include /* We don't allow > 64M msgs: enough for 483 64k failure msgs. */ #define WIRE_LEN_LIMIT (1 << 26) -typedef u32 wire_len_t; +typedef be32 wire_len_t; +#define wirelen_to_cpu be32_to_cpu +#define cpu_to_wirelen cpu_to_be32 /* Read message into *data, allocating off ctx. */ struct io_plan *io_read_wire_(struct io_conn *conn, diff --git a/wire/wire_sync.c b/wire/wire_sync.c index 0dc65af7b..a04b98be5 100644 --- a/wire/wire_sync.c +++ b/wire/wire_sync.c @@ -7,12 +7,12 @@ bool wire_sync_write(int fd, const void *msg TAKES) { - wire_len_t len = tal_len(msg); + wire_len_t hdr = cpu_to_wirelen(tal_len(msg)); bool ret; assert(tal_len(msg) < WIRE_LEN_LIMIT); - ret = write_all(fd, &len, sizeof(len)) - && write_all(fd, msg, len); + ret = write_all(fd, &hdr, sizeof(hdr)) + && write_all(fd, msg, tal_count(msg)); if (taken(msg)) tal_free(msg); @@ -26,12 +26,12 @@ u8 *wire_sync_read(const tal_t *ctx, int fd) if (!read_all(fd, &len, sizeof(len))) return NULL; - if (len >= WIRE_LEN_LIMIT) { + if (wirelen_to_cpu(len) >= WIRE_LEN_LIMIT) { errno = E2BIG; return NULL; } - msg = tal_arr(ctx, u8, len); - if (!read_all(fd, msg, len)) + msg = tal_arr(ctx, u8, wirelen_to_cpu(len)); + if (!read_all(fd, msg, wirelen_to_cpu(len))) return tal_free(msg); return msg; }