From 9d316e39cdb10838eacd9527238caf0995a65a67 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 10 Jan 2017 15:21:20 +1030 Subject: [PATCH] wire/wire_sync: helper routines for direct read/write of messages. Some of the simple daemons want to use this, as do the status messages. Signed-off-by: Rusty Russell --- wire/Makefile | 6 ++++-- wire/wire_sync.c | 26 ++++++++++++++++++++++++++ wire/wire_sync.h | 10 ++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 wire/wire_sync.c create mode 100644 wire/wire_sync.h diff --git a/wire/Makefile b/wire/Makefile index 9f868caa5..493c8fa47 100644 --- a/wire/Makefile +++ b/wire/Makefile @@ -4,15 +4,17 @@ wire-wrongdir: $(MAKE) -C .. wire-all -WIRE_HEADERS := wire/wire.h +WIRE_HEADERS := wire/wire.h wire/wire_sync.h WIRE_GEN_HEADERS := wire/gen_peer_wire.h wire/gen_onion_wire.h WIRE_GEN_SRC := wire/gen_peer_wire.c WIRE_GEN_ONION_SRC := wire/gen_onion_wire.c -WIRE_SRC := wire/fromwire.c \ +WIRE_SRC := wire/wire_sync.c \ + wire/fromwire.c \ wire/towire.c WIRE_OBJS := $(WIRE_SRC:.c=.o) $(WIRE_GEN_SRC:.c=.o) WIRE_ONION_OBJS := $(WIRE_GEN_ONION_SRC:.c=.o) +$(WIRE_OBJS): $(CCAN_HEADERS) # They may not have the bolts. BOLT_EXTRACT=$(BOLTDIR)/tools/extract-formats.py diff --git a/wire/wire_sync.c b/wire/wire_sync.c new file mode 100644 index 000000000..7222e66e8 --- /dev/null +++ b/wire/wire_sync.c @@ -0,0 +1,26 @@ +#include "wire/wire_sync.h" +#include +#include +#include + +bool wire_sync_write(int fd, const void *msg) +{ + be16 be_len = cpu_to_be16(tal_count(msg)); + + assert(be16_to_cpu(be_len) == tal_count(msg)); + return write_all(fd, &be_len, sizeof(be_len)) + && write_all(fd, msg, tal_count(msg)); +} + +u8 *wire_sync_read(const tal_t *ctx, int fd) +{ + be16 be_len; + u8 *msg; + + if (!read_all(fd, &be_len, sizeof(be_len))) + return NULL; + msg = tal_arr(ctx, u8, be16_to_cpu(be_len)); + if (!read_all(fd, msg, be16_to_cpu(be_len))) + return tal_free(msg); + return msg; +} diff --git a/wire/wire_sync.h b/wire/wire_sync.h new file mode 100644 index 000000000..e96ab9188 --- /dev/null +++ b/wire/wire_sync.h @@ -0,0 +1,10 @@ +#ifndef LIGHTNING_WIRE_WIRE_SYNC_H +#define LIGHTNING_WIRE_WIRE_SYNC_H +#include "config.h" +#include +#include + +bool wire_sync_write(int fd, const void *msg); +u8 *wire_sync_read(const tal_t *ctx, int fd); + +#endif /* LIGHTNING_WIRE_WIRE_SYNC_H */