diff --git a/lightningd/channel_config.c b/lightningd/channel_config.c index e6b4f26a6..bace60853 100644 --- a/lightningd/channel_config.c +++ b/lightningd/channel_config.c @@ -12,11 +12,9 @@ void towire_channel_config(u8 **pptr, const struct channel_config *config) towire_u16(pptr, config->max_accepted_htlcs); } -struct channel_config *fromwire_channel_config(const tal_t *ctx, - const u8 **ptr, size_t *max) +void fromwire_channel_config(const u8 **ptr, size_t *max, + struct channel_config *config) { - struct channel_config *config = tal(ctx, struct channel_config); - config->dust_limit_satoshis = fromwire_u64(ptr, max); config->max_htlc_value_in_flight_msat = fromwire_u64(ptr, max); config->channel_reserve_satoshis = fromwire_u64(ptr, max); @@ -24,8 +22,4 @@ struct channel_config *fromwire_channel_config(const tal_t *ctx, config->htlc_minimum_msat = fromwire_u32(ptr, max); config->to_self_delay = fromwire_u16(ptr, max); config->max_accepted_htlcs = fromwire_u16(ptr, max); - - if (!*ptr) - return tal_free(config); - return config; } diff --git a/lightningd/channel_config.h b/lightningd/channel_config.h index 7d1e7c4a9..f9f6a2865 100644 --- a/lightningd/channel_config.h +++ b/lightningd/channel_config.h @@ -41,6 +41,6 @@ struct channel_config { }; void towire_channel_config(u8 **pptr, const struct channel_config *config); -struct channel_config *fromwire_channel_config(const tal_t *ctx, - const u8 **ptr, size_t *max); +void fromwire_channel_config(const u8 **ptr, size_t *max, + struct channel_config *config); #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONFIG_H */ diff --git a/lightningd/cryptomsg.c b/lightningd/cryptomsg.c index 49986c2b1..b39becdec 100644 --- a/lightningd/cryptomsg.c +++ b/lightningd/cryptomsg.c @@ -12,23 +12,6 @@ #include #include -struct crypto_state { - /* Received and sent nonces. */ - u64 rn, sn; - /* Sending and receiving keys. */ - struct sha256 sk, rk; - /* Chaining key for re-keying */ - struct sha256 s_ck, r_ck; - - /* Peer who owns us: peer->crypto_state == this */ - struct peer *peer; - - /* Output and input buffers. */ - u8 *out, *in; - struct io_plan *(*next_in)(struct io_conn *, struct peer *, u8 *); - struct io_plan *(*next_out)(struct io_conn *, struct peer *); -}; - static void hkdf_two_keys(struct sha256 *out1, struct sha256 *out2, const struct sha256 *in1, const struct sha256 *in2) @@ -332,20 +315,12 @@ void towire_crypto_state(u8 **ptr, const struct crypto_state *cs) towire_sha256(ptr, &cs->r_ck); } -struct crypto_state *fromwire_crypto_state(const tal_t *ctx, - const u8 **ptr, size_t *max) +void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs) { - struct crypto_state *cs = tal(ctx, struct crypto_state); - cs->rn = fromwire_u64(ptr, max); cs->sn = fromwire_u64(ptr, max); fromwire_sha256(ptr, max, &cs->sk); fromwire_sha256(ptr, max, &cs->rk); fromwire_sha256(ptr, max, &cs->s_ck); fromwire_sha256(ptr, max, &cs->r_ck); - cs->peer = (struct peer *)ctx; - - if (!*ptr) - return tal_free(cs); - return cs; } diff --git a/lightningd/cryptomsg.h b/lightningd/cryptomsg.h index 9fbecf81c..675bbdfec 100644 --- a/lightningd/cryptomsg.h +++ b/lightningd/cryptomsg.h @@ -1,12 +1,29 @@ #ifndef LIGHTNING_LIGHTNINGD_CRYPTOMSG_H #define LIGHTNING_LIGHTNINGD_CRYPTOMSG_H #include "config.h" +#include #include #include struct io_conn; struct peer; -struct sha256; + +struct crypto_state { + /* Received and sent nonces. */ + u64 rn, sn; + /* Sending and receiving keys. */ + struct sha256 sk, rk; + /* Chaining key for re-keying */ + struct sha256 s_ck, r_ck; + + /* Peer who owns us: peer->crypto_state == this */ + struct peer *peer; + + /* Output and input buffers. */ + u8 *out, *in; + struct io_plan *(*next_in)(struct io_conn *, struct peer *, u8 *); + struct io_plan *(*next_out)(struct io_conn *, struct peer *); +}; /* Initializes peer->crypto_state */ struct crypto_state *crypto_state(struct peer *peer, @@ -31,7 +48,5 @@ struct io_plan *peer_write_message(struct io_conn *conn, struct peer *)); void towire_crypto_state(u8 **pptr, const struct crypto_state *cs); -struct crypto_state *fromwire_crypto_state(const tal_t *ctx, - const u8 **ptr, size_t *max); - +void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs); #endif /* LIGHTNING_LIGHTNINGD_CRYPTOMSG_H */ diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index 52d3f7d95..68edfd028 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -69,9 +70,10 @@ static void destroy_peer(struct peer *peer) static struct peer *setup_new_peer(struct daemon *daemon, const u8 *msg) { struct peer *peer = tal(daemon, struct peer); - if (!fromwire_gossipctl_new_peer(peer, msg, NULL, - &peer->unique_id, &peer->cs)) + peer->cs = tal(peer, struct crypto_state); + if (!fromwire_gossipctl_new_peer(msg, NULL, &peer->unique_id, peer->cs)) return tal_free(peer); + peer->cs->peer = peer; peer->daemon = daemon; peer->error = NULL; list_add_tail(&daemon->peers, &peer->list); diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 18063cd98..57bbbdc33 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -45,16 +46,17 @@ static void peer_nongossip(struct subdaemon *gossip, const u8 *msg, int fd) u64 unique_id; struct peer *peer; u8 *inner; - struct crypto_state *cs; + struct crypto_state *cs = tal(msg, struct crypto_state); if (!fromwire_gossipstatus_peer_nongossip(msg, msg, NULL, - &unique_id, &cs, &inner)) + &unique_id, cs, &inner)) fatal("Gossip gave bad PEER_NONGOSSIP message %s", tal_hex(msg, msg)); peer = peer_by_unique_id(gossip->ld, unique_id); if (!peer) fatal("Gossip gave bad peerid %"PRIu64, unique_id); + cs->peer = peer; log_debug(gossip->log, "Peer %s said %s", type_to_string(msg, struct pubkey, peer->id), diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 481b46d9e..bd7f6387f 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -88,22 +88,23 @@ struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id) static void handshake_succeeded(struct subdaemon *hs, const u8 *msg, struct peer *peer) { - struct crypto_state *cs; + struct crypto_state *cs = tal(peer, struct crypto_state); if (!peer->id) { struct pubkey id; - if (!fromwire_handshake_responder_resp(msg, msg, NULL, &id, &cs)) + if (!fromwire_handshake_responder_resp(msg, NULL, &id, cs)) goto err; peer->id = tal_dup(peer, struct pubkey, &id); log_info_struct(hs->log, "Peer in from %s", struct pubkey, peer->id); } else { - if (!fromwire_handshake_initiator_resp(msg, msg, NULL, &cs)) + if (!fromwire_handshake_initiator_resp(msg, NULL, cs)) goto err; log_info_struct(hs->log, "Peer out to %s", struct pubkey, peer->id); } + cs->peer = peer; /* FIXME: Look for peer duplicates! */ diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 60fd67070..c3b27c377 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -54,7 +54,6 @@ class Field(object): self.comments = comments self.name = name.replace('-', '_') self.is_len_var = False - self.is_unknown = False self.lenvar = None # Size could be a literal number (eg. 33), or a field (eg 'len'), or @@ -78,7 +77,6 @@ class Field(object): # Unknown types are assumed to have base_size: div by 0 if that's unknown. if self.fieldtype.tsize == 0: - self.is_unknown = True self.fieldtype.tsize = base_size if base_size % self.fieldtype.tsize != 0: @@ -185,8 +183,6 @@ class Message(object): if field.is_variable_size(): self.checkLenField(field) self.has_variable_fields = True - elif field.is_unknown: - self.has_variable_fields = True self.fields.append(field) def print_fromwire(self,is_header): @@ -205,7 +201,7 @@ class Message(object): continue if f.is_array(): print(', {} {}[{}]'.format(f.fieldtype.name, f.name, f.num_elems), end='') - elif f.is_variable_size() or f.is_unknown: + elif f.is_variable_size(): print(', {} **{}'.format(f.fieldtype.name, f.name), end='') else: print(', {} *{}'.format(f.fieldtype.name, f.name), end='') @@ -240,14 +236,7 @@ class Message(object): for c in f.comments: print('\t/*{} */'.format(c)) - if f.is_unknown: - if f.is_variable_size(): - print('\t*{} = fromwire_{}_array(ctx, &cursor, plen, {});' - .format(f.name, basetype, f.lenvar)) - else: - print('\t*{} = fromwire_{}(ctx, &cursor, plen);' - .format(f.name, basetype)) - elif f.is_padding(): + if f.is_padding(): print('\tfromwire_pad(&cursor, plen, {});' .format(f.num_elems)) elif f.is_array():