Skip to content

Commit

Permalink
common: move gossip_store read routine where subdaemons can access it.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed May 13, 2019
1 parent d8db4e8 commit 0e37ac2
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 35 deletions.
1 change: 1 addition & 0 deletions common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
41 changes: 41 additions & 0 deletions common/gossip_store.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <ccan/crc/crc.h>
#include <ccan/endian/endian.h>
#include <common/gossip_store.h>
#include <common/status.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>

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;
}

19 changes: 19 additions & 0 deletions common/gossip_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef LIGHTNING_COMMON_GOSSIP_STORE_H
#define LIGHTNING_COMMON_GOSSIP_STORE_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>

/**
* 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 */
3 changes: 2 additions & 1 deletion devtools/create-gossipstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <common/amount.h>
#include <common/gossip_store.h>
#include <common/node_id.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <fcntl.h>
#include <gossipd/gen_gossip_store.h>
#include <gossipd/gossip_store.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion devtools/dump-gossipstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <common/type_to_string.h>
#include <common/utils.h>
#include <fcntl.h>
#include <common/gossip_store.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_store.h>
#include <gossipd/gossip_store.h>
#include <inttypes.h>
#include <stdio.h>
#include <sys/stat.h>
Expand Down
1 change: 1 addition & 0 deletions gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
34 changes: 2 additions & 32 deletions gossipd/gossip_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ccan/crc/crc.h>
#include <ccan/endian/endian.h>
#include <ccan/read_write_all/read_write_all.h>
#include <common/gossip_store.h>
#include <common/status.h>
#include <common/utils.h>
#include <errno.h>
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion gossipd/gossip_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/**
* gossip_store -- On-disk storage related information
*/
#define GOSSIP_STORE_VERSION 4

struct broadcast_state;
struct gossip_store;
Expand Down
3 changes: 3 additions & 0 deletions gossipd/test/run-bench-find_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions gossipd/test/run-find_route-specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions gossipd/test/run-find_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
3 changes: 3 additions & 0 deletions gossipd/test/run-overlong.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down

0 comments on commit 0e37ac2

Please sign in to comment.