Browse Source

protobuf_convert: expose helpers for unwrapping protobufs into tal heirarchies

It's still ugly, but at least it's encapsulated.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
169c6b53cb
  1. 24
      daemon/onion.c
  2. 32
      protobuf_convert.c
  3. 7
      protobuf_convert.h

24
daemon/onion.c

@ -54,41 +54,25 @@ const u8 *onion_create(const tal_t *ctx,
return to_onion(ctx, r);
}
static void *proto_tal_alloc(void *allocator_data, size_t size)
{
return tal_arr(allocator_data, char, size);
}
static void proto_tal_free(void *allocator_data, void *pointer)
{
tal_free(pointer);
}
/* Decode next step in the route, and fill out the onion to send onwards. */
RouteStep *onion_unwrap(struct peer *peer,
const void *data, size_t len, const u8 **next)
{
struct ProtobufCAllocator prototal;
struct ProtobufCAllocator *prototal = make_prototal(peer);
Route *r;
RouteStep *step;
/* De-protobuf it. */
prototal.alloc = proto_tal_alloc;
prototal.free = proto_tal_free;
prototal.allocator_data = tal(peer, char);
r = route__unpack(&prototal, len, data);
r = route__unpack(prototal, len, data);
if (!r || r->n_steps == 0) {
log_unusual(peer->log, "Failed to unwrap onion");
tal_free(prototal.allocator_data);
tal_free(prototal);
return NULL;
}
/* Remove first step. */
step = r->steps[0];
/* Make sure that step owns the rest */
tal_steal(peer, step);
tal_steal(step, prototal.allocator_data);
steal_from_prototal(peer, prototal, step);
/* Re-pack with remaining steps. */
r->n_steps--;

32
protobuf_convert.c

@ -173,3 +173,35 @@ Locktime *abs_locktime_to_proto(const tal_t *ctx,
}
return l;
}
static void *proto_tal_alloc(void *allocator_data, size_t size)
{
return tal_arr(allocator_data, char, size);
}
static void proto_tal_free(void *allocator_data, void *pointer)
{
tal_free(pointer);
}
/* Get allocator so decoded protobuf will be tal off it. */
struct ProtobufCAllocator *make_prototal(const tal_t *ctx)
{
struct ProtobufCAllocator *prototal;
prototal = tal(ctx, struct ProtobufCAllocator);
prototal->alloc = proto_tal_alloc;
prototal->free = proto_tal_free;
prototal->allocator_data = tal(prototal, char);
return prototal;
}
/* Now steal object off of allocator (and free prototal) */
void steal_from_prototal(const tal_t *ctx, struct ProtobufCAllocator *prototal,
const void *pb)
{
tal_steal(ctx, pb);
tal_steal(pb, prototal->allocator_data);
tal_free(prototal);
}

7
protobuf_convert.h

@ -42,4 +42,11 @@ Locktime *rel_locktime_to_proto(const tal_t *ctx,
const struct rel_locktime *locktime);
Locktime *abs_locktime_to_proto(const tal_t *ctx,
const struct abs_locktime *locktime);
/* Get allocator so decoded protobuf will be tal off it. */
struct ProtobufCAllocator *make_prototal(const tal_t *ctx);
/* Now steal object off of allocator (and free prototal) */
void steal_from_prototal(const tal_t *ctx, struct ProtobufCAllocator *prototal,
const void *pb);
#endif /* LIGHTNING_PROTOBUF_CONVERT_H */

Loading…
Cancel
Save