From 0e37ac2433ba1ef21d02216a6d5b27a6f27a3981 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 4 May 2019 15:23:13 +0930 Subject: [PATCH] common: move gossip_store read routine where subdaemons can access it. Signed-off-by: Rusty Russell --- common/Makefile | 1 + common/gossip_store.c | 41 ++++++++++++++++++++++++++ common/gossip_store.h | 19 ++++++++++++ devtools/create-gossipstore.c | 3 +- devtools/dump-gossipstore.c | 2 +- gossipd/Makefile | 1 + gossipd/gossip_store.c | 34 ++------------------- gossipd/gossip_store.h | 1 - gossipd/test/run-bench-find_route.c | 3 ++ gossipd/test/run-find_route-specific.c | 3 ++ gossipd/test/run-find_route.c | 3 ++ gossipd/test/run-overlong.c | 3 ++ 12 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 common/gossip_store.c create mode 100644 common/gossip_store.h diff --git a/common/Makefile b/common/Makefile index 5bb8d4658..c795c4df7 100644 --- a/common/Makefile +++ b/common/Makefile @@ -18,6 +18,7 @@ COMMON_SRC_NOGEN := \ common/dev_disconnect.c \ common/features.c \ common/funding_tx.c \ + common/gossip_store.c \ common/hash_u5.c \ common/htlc_state.c \ common/htlc_tx.c \ diff --git a/common/gossip_store.c b/common/gossip_store.c new file mode 100644 index 000000000..262ed1c86 --- /dev/null +++ b/common/gossip_store.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include + +u8 *gossip_store_read(const tal_t *ctx, int gossip_store_fd, u64 offset) +{ + beint32_t hdr[2]; + u32 msglen, checksum; + u8 *msg; + + if (offset == 0) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "gossip_store: can't access offset %"PRIu64, + offset); + if (pread(gossip_store_fd, hdr, sizeof(hdr), offset) != sizeof(hdr)) { + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "gossip_store: can't read hdr offset %"PRIu64 + ": %s", + offset, strerror(errno)); + } + + msglen = be32_to_cpu(hdr[0]); + checksum = be32_to_cpu(hdr[1]); + msg = tal_arr(ctx, u8, msglen); + if (pread(gossip_store_fd, msg, msglen, offset + sizeof(hdr)) != msglen) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "gossip_store: can't read len %u offset %"PRIu64, + msglen, offset); + + if (checksum != crc32c(0, msg, msglen)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "gossip_store: bad checksum offset %"PRIu64, + offset); + + return msg; +} + diff --git a/common/gossip_store.h b/common/gossip_store.h new file mode 100644 index 000000000..8ff203e66 --- /dev/null +++ b/common/gossip_store.h @@ -0,0 +1,19 @@ +#ifndef LIGHTNING_COMMON_GOSSIP_STORE_H +#define LIGHTNING_COMMON_GOSSIP_STORE_H +#include "config.h" +#include +#include + +/** + * gossip_store -- On-disk storage related information + */ +#define GOSSIP_STORE_VERSION 4 + +/** + * Direct store accessor: loads gossip msg from store. + * + * Doesn't return; status_failed() on error. + */ +u8 *gossip_store_read(const tal_t *ctx, int gossip_store_fd, u64 offset); + +#endif /* LIGHTNING_COMMON_GOSSIP_STORE_H */ diff --git a/devtools/create-gossipstore.c b/devtools/create-gossipstore.c index 53a00905d..9fc6f90df 100644 --- a/devtools/create-gossipstore.c +++ b/devtools/create-gossipstore.c @@ -4,11 +4,12 @@ #include #include #include +#include +#include #include #include #include #include -#include #include #include #include diff --git a/devtools/dump-gossipstore.c b/devtools/dump-gossipstore.c index 9dac334b1..bdf90d4a3 100644 --- a/devtools/dump-gossipstore.c +++ b/devtools/dump-gossipstore.c @@ -3,9 +3,9 @@ #include #include #include +#include #include #include -#include #include #include #include diff --git a/gossipd/Makefile b/gossipd/Makefile index 561c89f0f..a58978db4 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -50,6 +50,7 @@ GOSSIPD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/gossip_store.o \ common/key_derive.o \ common/memleak.o \ common/msg_queue.o \ diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index ef8bf97ab..c585a123b 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -531,38 +532,7 @@ const u8 *gossip_store_get(const tal_t *ctx, struct gossip_store *gs, u64 offset) { - beint32_t hdr[2]; - u32 msglen, checksum; - u8 *msg; - - if (offset == 0 || offset > gs->len) - status_failed(STATUS_FAIL_INTERNAL_ERROR, - "gossip_store: can't access offset %"PRIu64 - ", store len %"PRIu64, - offset, gs->len); - if (pread(gs->fd, hdr, sizeof(hdr), offset) != sizeof(hdr)) { - status_failed(STATUS_FAIL_INTERNAL_ERROR, - "gossip_store: can't read hdr offset %"PRIu64 - ", store len %"PRIu64": %s", - offset, gs->len, strerror(errno)); - } - - msglen = be32_to_cpu(hdr[0]); - checksum = be32_to_cpu(hdr[1]); - msg = tal_arr(ctx, u8, msglen); - if (pread(gs->fd, msg, msglen, offset + sizeof(hdr)) != msglen) - status_failed(STATUS_FAIL_INTERNAL_ERROR, - "gossip_store: can't read len %u offset %"PRIu64 - ", store len %"PRIu64, - msglen, offset, gs->len); - - if (checksum != crc32c(0, msg, msglen)) - status_failed(STATUS_FAIL_INTERNAL_ERROR, - "gossip_store: bad checksum offset %"PRIu64 - ", store len %"PRIu64, - offset, gs->len); - - return msg; + return gossip_store_read(ctx, gs->fd, offset); } int gossip_store_readonly_fd(struct gossip_store *gs) diff --git a/gossipd/gossip_store.h b/gossipd/gossip_store.h index 819657310..6cebedbed 100644 --- a/gossipd/gossip_store.h +++ b/gossipd/gossip_store.h @@ -11,7 +11,6 @@ /** * gossip_store -- On-disk storage related information */ -#define GOSSIP_STORE_VERSION 4 struct broadcast_state; struct gossip_store; diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index 80784e922..e19fc6702 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -69,6 +69,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) /* Generated stub for fromwire_wireaddr */ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } +/* Generated stub for gossip_store_read */ +u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED) +{ fprintf(stderr, "gossip_store_read called!\n"); abort(); } /* Generated stub for onion_type_name */ const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); } diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index 9abbf7fcc..6e3a5af67 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -58,6 +58,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) /* Generated stub for fromwire_wireaddr */ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } +/* Generated stub for gossip_store_read */ +u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED) +{ fprintf(stderr, "gossip_store_read called!\n"); abort(); } /* Generated stub for onion_type_name */ const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); } diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index ff97b1a3b..0b2f921b5 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -56,6 +56,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) /* Generated stub for fromwire_wireaddr */ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } +/* Generated stub for gossip_store_read */ +u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED) +{ fprintf(stderr, "gossip_store_read called!\n"); abort(); } /* Generated stub for onion_type_name */ const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); } diff --git a/gossipd/test/run-overlong.c b/gossipd/test/run-overlong.c index 92eff580a..284eabc3e 100644 --- a/gossipd/test/run-overlong.c +++ b/gossipd/test/run-overlong.c @@ -56,6 +56,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) /* Generated stub for fromwire_wireaddr */ bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } +/* Generated stub for gossip_store_read */ +u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED) +{ fprintf(stderr, "gossip_store_read called!\n"); abort(); } /* Generated stub for onion_type_name */ const char *onion_type_name(int e UNNEEDED) { fprintf(stderr, "onion_type_name called!\n"); abort(); }