Skip to content

Commit

Permalink
gossip: Change gossip_store message length to u32
Browse files Browse the repository at this point in the history
Since we now store additional data along with the original messages they exceed
the length of the peer wire protocol messages.

Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed Mar 25, 2018
1 parent b5602a0 commit 6894f20
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions gossipd/gossip_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,36 @@ struct gossip_store *gossip_store_new(const tal_t *ctx)

void gossip_store_append(struct gossip_store *gs, const u8 *msg)
{
u16 msglen = tal_len(msg);
beint16_t belen = cpu_to_be16(msglen);
u32 msglen = tal_len(msg);
beint32_t belen = cpu_to_be32(msglen);

if (pwrite(gs->fd, &belen, sizeof(belen), gs->write_pos) != 2 ||
pwrite(gs->fd, msg, msglen, gs->write_pos + 2) != msglen) {
if (pwrite(gs->fd, &belen, sizeof(belen), gs->write_pos) != sizeof(belen) ||
pwrite(gs->fd, msg, msglen, gs->write_pos + sizeof(belen)) != msglen) {
return;
} else
gs->write_pos += 2 + msglen;
gs->write_pos += sizeof(belen) + msglen;
}

const u8 *gossip_store_read_next(const tal_t *ctx, struct gossip_store *gs)
{
beint16_t belen;
u16 msglen;
beint32_t belen;
u32 msglen;
u8 *msg;

/* Did we already reach the end of the gossip_store? */
if (gs->read_pos == -1)
return NULL;

/* Can we read one message? */
if (pread(gs->fd, &belen, sizeof(belen), gs->read_pos) != 2) {
if (pread(gs->fd, &belen, sizeof(belen), gs->read_pos) != sizeof(belen)) {
gs->read_pos = -1;
return NULL;
}

msglen = be16_to_cpu(belen);
msglen = be32_to_cpu(belen);
msg = tal_arr(ctx, u8, msglen);

if (!pread(gs->fd, msg, msglen, gs->read_pos + 2)) {
if (!pread(gs->fd, msg, msglen, gs->read_pos + sizeof(belen))) {
status_trace("Short read from gossip-store, expected lenght %d",
msglen);

Expand All @@ -85,7 +85,7 @@ const u8 *gossip_store_read_next(const tal_t *ctx, struct gossip_store *gs)
gs->read_pos = -1;
ftruncate(gs->fd, gs->write_pos);
} else
gs->read_pos += 2 + msglen;
gs->read_pos += sizeof(belen) + msglen;

return msg;
}

0 comments on commit 6894f20

Please sign in to comment.