From 49b0c375ce575082859251c6ee77a9e33e127eba Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 13 Mar 2018 12:08:03 +0100 Subject: [PATCH] gossip: Added gossip store primitives Signed-off-by: Christian Decker --- gossipd/Makefile | 1 + gossipd/gossip.c | 1 - gossipd/gossip_store.c | 48 ++++++++++++++++++++++++++++++++++++++++++ gossipd/gossip_store.h | 35 ++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 gossipd/gossip_store.c create mode 100644 gossipd/gossip_store.h diff --git a/gossipd/Makefile b/gossipd/Makefile index 32fd30e49..3ac27e7de 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -13,6 +13,7 @@ LIGHTNINGD_GOSSIP_CONTROL_OBJS := $(LIGHTNINGD_GOSSIP_CONTROL_SRC:.c=.o) # gossipd needs these: LIGHTNINGD_GOSSIP_HEADERS := gossipd/gen_gossip_wire.h \ + gossipd/gossip_store.h \ gossipd/handshake.h \ gossipd/routing.h \ gossipd/broadcast.h diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 7c9c34b03..1c8f782c5 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c new file mode 100644 index 000000000..02791cc47 --- /dev/null +++ b/gossipd/gossip_store.c @@ -0,0 +1,48 @@ +#include "gossip_store.h" + +#include +#include +#include +#include + +#define GOSSIP_STORE_FILENAME "gossip_store" + +struct gossip_store { + int read_fd, write_fd; +}; + +struct gossip_store *gossip_store_new(const tal_t *ctx) +{ + struct gossip_store *gs = tal(ctx, struct gossip_store); + gs->write_fd = open(GOSSIP_STORE_FILENAME, O_RDWR|O_APPEND|O_CREAT, 0600); + gs->read_fd = open(GOSSIP_STORE_FILENAME, O_RDONLY); + return gs; +} + +void gossip_store_append(struct gossip_store *gs, const u8 *msg) +{ + u16 msglen = tal_len(msg); + beint16_t belen = cpu_to_be16(msglen); + + write_all(gs->write_fd, &belen, sizeof(belen)); + write_all(gs->write_fd, msg, msglen); +} + +const u8 *gossip_store_read_next(const tal_t *ctx, struct gossip_store *gs) +{ + beint16_t belen; + u16 msglen; + u8 *msg; + if (!read_all(gs->read_fd, &belen, sizeof(belen))) + return NULL; + + msglen = be16_to_cpu(belen); + msg = tal_arr(ctx, u8, msglen); + + if (!read_all(gs->read_fd, msg, msglen)) + status_failed( + STATUS_FAIL_INTERNAL_ERROR, + "Short read from gossip-store, expected lenght %d", msglen); + + return msg; +} diff --git a/gossipd/gossip_store.h b/gossipd/gossip_store.h new file mode 100644 index 000000000..d9bc86009 --- /dev/null +++ b/gossipd/gossip_store.h @@ -0,0 +1,35 @@ +#ifndef GOSSIPD_GOSSIP_STORE_H +#define GOSSIPD_GOSSIP_STORE_H + +#include "config.h" + +#include +#include + +/** + * gossip_store -- On-disk storage related information + */ +struct gossip_store; + +struct gossip_store *gossip_store_new(const tal_t *ctx); + +/** + * Write an incoming message to the `gossip_store` + * + * @param gs The gossip_store to write to + * @param msg The message to write + * @return The newly created and initialized `gossip_store` + */ +void gossip_store_append(struct gossip_store *gs, const u8 *msg); + +/** + * Retrieve the next gossip message if any + * + * @param ctx The context to allocate the message from + * @param gs The `gossip_store` to read from + * @return The gossip message allocated from `ctx`, `NULL` if no more messages are + * available. + */ +const u8 *gossip_store_read_next(const tal_t *ctx, struct gossip_store *gs); + +#endif /* GOSSIPD_GOSSIP_STORE_H */