2019-05-04 07:53:13 +02:00
|
|
|
#include <ccan/crc/crc.h>
|
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <common/gossip_store.h>
|
|
|
|
#include <common/status.h>
|
2019-06-03 20:09:25 +02:00
|
|
|
#include <common/utils.h>
|
2019-05-04 07:53:13 +02:00
|
|
|
#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));
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:09:25 +02:00
|
|
|
/* FIXME: We should skip over these deleted entries! */
|
|
|
|
msglen = be32_to_cpu(hdr[0]) & ~GOSSIP_STORE_LEN_DELETED_BIT;
|
2019-05-04 07:53:13 +02:00
|
|
|
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,
|
2019-06-03 20:09:25 +02:00
|
|
|
"gossip_store: bad checksum offset %"PRIu64": %s",
|
|
|
|
offset, tal_hex(tmpctx, msg));
|
2019-05-04 07:53:13 +02:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|