From aa6aad0131dfa4764184e22faa58c54ecc830b75 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 22 Jan 2020 16:58:25 +1030 Subject: [PATCH] common: add struct onionreply I really want a type which means "I am a wrapped onion reply" as separate from "I am a normal wire msg". Currently both user u8 *, and I got very confused trying to figure out where each one was an unwrapped error msg, or where it still needed (un)wrapping. Signed-off-by: Rusty Russell --- common/Makefile | 1 + common/onionreply.c | 40 ++++++++++++++++++++++++++++++++++++++++ common/onionreply.h | 24 ++++++++++++++++++++++++ tools/generate-wire.py | 1 + wallet/db.c | 16 ++++++++++++++++ wallet/db.h | 6 ++++++ 6 files changed, 88 insertions(+) create mode 100644 common/onionreply.c create mode 100644 common/onionreply.h diff --git a/common/Makefile b/common/Makefile index 46a7cc6c1..a309d5c5e 100644 --- a/common/Makefile +++ b/common/Makefile @@ -41,6 +41,7 @@ COMMON_SRC_NOGEN := \ common/msg_queue.c \ common/node_id.c \ common/onion.c \ + common/onionreply.c \ common/param.c \ common/per_peer_state.c \ common/peer_billboard.c \ diff --git a/common/onionreply.c b/common/onionreply.c new file mode 100644 index 000000000..a0a20e10f --- /dev/null +++ b/common/onionreply.c @@ -0,0 +1,40 @@ +#include +#include +#include + +void towire_onionreply(u8 **cursor, const struct onionreply *r) +{ + towire_u16(cursor, tal_count(r->contents)); + towire_u8_array(cursor, r->contents, tal_count(r->contents)); +} + +struct onionreply *fromwire_onionreply(const tal_t *ctx, + const u8 **cursor, size_t *max) +{ + struct onionreply *r = tal(ctx, struct onionreply); + r->contents = tal_arr(r, u8, fromwire_u16(cursor, max)); + fromwire_u8_array(cursor, max, r->contents, tal_count(r->contents)); + if (!*cursor) + return tal_free(r); + return r; +} + +struct onionreply *dup_onionreply(const tal_t *ctx, + const struct onionreply *r TAKES) +{ + struct onionreply *n; + + if (taken(r)) + return cast_const(struct onionreply *, tal_steal(ctx, r)); + + n = tal(ctx, struct onionreply); + n->contents = tal_dup_arr(n, u8, r->contents, tal_count(r->contents), 0); + return n; +} + +struct onionreply *new_onionreply(const tal_t *ctx, const u8 *contents TAKES) +{ + struct onionreply *r = tal(ctx, struct onionreply); + r->contents = tal_dup_arr(r, u8, contents, tal_count(contents), 0); + return r; +} diff --git a/common/onionreply.h b/common/onionreply.h new file mode 100644 index 000000000..759a4df18 --- /dev/null +++ b/common/onionreply.h @@ -0,0 +1,24 @@ +#ifndef LIGHTNING_COMMON_ONIONREPLY_H +#define LIGHTNING_COMMON_ONIONREPLY_H +#include "config.h" +#include +#include + +/* A separate type for an onion reply, to differentiate from a wire msg. */ +struct onionreply { + u8 *contents; +}; + +/** + * Wire marshalling routines for onionreply + */ +void towire_onionreply(u8 **cursor, const struct onionreply *r); +struct onionreply *fromwire_onionreply(const tal_t *ctx, + const u8 **cursor, size_t *max); + + +struct onionreply *dup_onionreply(const tal_t *ctx, + const struct onionreply *r TAKES); + +struct onionreply *new_onionreply(const tal_t *ctx, const u8 *contents TAKES); +#endif /* LIGHTNING_COMMON_ONIONREPLY_H */ diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 7c4fa9967..acecb2258 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -224,6 +224,7 @@ class Type(FieldSet): 'bitcoin_tx_output', 'exclude_entry', 'fee_states', + 'onionreply', ] # Some BOLT types are re-typed based on their field name diff --git a/wallet/db.c b/wallet/db.c index edfe3f447..ada3972a9 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -1227,6 +1228,11 @@ void db_bind_json_escape(struct db_stmt *stmt, int pos, db_bind_text(stmt, pos, esc->s); } +void db_bind_onionreply(struct db_stmt *stmt, int pos, const struct onionreply *r) +{ + db_bind_blob(stmt, pos, r->contents, tal_bytelen(r->contents)); +} + void db_column_preimage(struct db_stmt *stmt, int col, struct preimage *preimage) { @@ -1409,6 +1415,16 @@ void db_column_txid(struct db_stmt *stmt, int pos, struct bitcoin_txid *t) db_column_sha256d(stmt, pos, &t->shad); } +struct onionreply *db_column_onionreply(const tal_t *ctx, + struct db_stmt *stmt, int col) +{ + struct onionreply *r = tal(ctx, struct onionreply); + r->contents = tal_dup_arr(r, u8, + db_column_blob(stmt, col), + db_column_bytes(stmt, col), 0); + return r; +} + bool db_exec_prepared_v2(struct db_stmt *stmt TAKES) { bool ret = stmt->db->config->exec_fn(stmt); diff --git a/wallet/db.h b/wallet/db.h index 5f352be2b..dace782e3 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -18,6 +18,7 @@ struct lightningd; struct log; struct node_id; +struct onionreply; struct db_stmt; struct db; @@ -120,6 +121,8 @@ void db_bind_amount_sat(struct db_stmt *stmt, int pos, const struct amount_sat *sat); void db_bind_json_escape(struct db_stmt *stmt, int pos, const struct json_escape *esc); +void db_bind_onionreply(struct db_stmt *stmt, int col, + const struct onionreply *r); bool db_step(struct db_stmt *stmt); u64 db_column_u64(struct db_stmt *stmt, int col); @@ -151,6 +154,9 @@ bool db_column_signature(struct db_stmt *stmt, int col, struct timeabs db_column_timeabs(struct db_stmt *stmt, int col); struct bitcoin_tx *db_column_tx(const tal_t *ctx, struct db_stmt *stmt, int col); +struct onionreply *db_column_onionreply(const tal_t *ctx, + struct db_stmt *stmt, int col); + #define db_column_arr(ctx, stmt, col, type) \ ((type *)db_column_arr_((ctx), (stmt), (col), \ sizeof(type), TAL_LABEL(type, "[]"), \