From 169c6b53cbddc729ac36c2b96c5e853f9ed26ecb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 30 Aug 2016 20:15:57 +0930 Subject: [PATCH] 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 --- daemon/onion.c | 24 ++++-------------------- protobuf_convert.c | 32 ++++++++++++++++++++++++++++++++ protobuf_convert.h | 7 +++++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/daemon/onion.c b/daemon/onion.c index 9f68e87d9..7fae43f12 100644 --- a/daemon/onion.c +++ b/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--; diff --git a/protobuf_convert.c b/protobuf_convert.c index 8f6b489b3..59b0637fb 100644 --- a/protobuf_convert.c +++ b/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); +} diff --git a/protobuf_convert.h b/protobuf_convert.h index de6e49846..8da99b9c5 100644 --- a/protobuf_convert.h +++ b/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 */