mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-23 17:14:22 +01:00
gossip: Added gossip store primitives
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
committed by
Rusty Russell
parent
e078fcefdd
commit
49b0c375ce
@@ -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
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <ccan/list/list.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/noerr/noerr.h>
|
||||
#include <ccan/read_write_all/read_write_all.h>
|
||||
#include <ccan/structeq/structeq.h>
|
||||
#include <ccan/take/take.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
|
||||
48
gossipd/gossip_store.c
Normal file
48
gossipd/gossip_store.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "gossip_store.h"
|
||||
|
||||
#include <ccan/endian/endian.h>
|
||||
#include <ccan/read_write_all/read_write_all.h>
|
||||
#include <common/status.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
35
gossipd/gossip_store.h
Normal file
35
gossipd/gossip_store.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef GOSSIPD_GOSSIP_STORE_H
|
||||
#define GOSSIPD_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
|
||||
*/
|
||||
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 */
|
||||
Reference in New Issue
Block a user