Browse Source

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 <rusty@rustcorp.com.au>
travis-debug
Rusty Russell 5 years ago
parent
commit
aa6aad0131
  1. 1
      common/Makefile
  2. 40
      common/onionreply.c
  3. 24
      common/onionreply.h
  4. 1
      tools/generate-wire.py
  5. 16
      wallet/db.c
  6. 6
      wallet/db.h

1
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 \

40
common/onionreply.c

@ -0,0 +1,40 @@
#include <ccan/cast/cast.h>
#include <common/onionreply.h>
#include <wire/wire.h>
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;
}

24
common/onionreply.h

@ -0,0 +1,24 @@
#ifndef LIGHTNING_COMMON_ONIONREPLY_H
#define LIGHTNING_COMMON_ONIONREPLY_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
/* 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 */

1
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

16
wallet/db.c

@ -3,6 +3,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/node_id.h>
#include <common/onionreply.h>
#include <common/version.h>
#include <inttypes.h>
#include <lightningd/lightningd.h>
@ -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);

6
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, "[]"), \

Loading…
Cancel
Save