Browse Source

generate-wire: don't hand unknown structures specially.

It's awkward to handle them differently.  But this change means we
need to expose them to the generated code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
9fd40da38c
  1. 10
      lightningd/channel_config.c
  2. 4
      lightningd/channel_config.h
  3. 27
      lightningd/cryptomsg.c
  4. 23
      lightningd/cryptomsg.h
  5. 6
      lightningd/gossip/gossip.c
  6. 6
      lightningd/gossip_control.c
  7. 7
      lightningd/peer_control.c
  8. 15
      tools/generate-wire.py

10
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); towire_u16(pptr, config->max_accepted_htlcs);
} }
struct channel_config *fromwire_channel_config(const tal_t *ctx, void fromwire_channel_config(const u8 **ptr, size_t *max,
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->dust_limit_satoshis = fromwire_u64(ptr, max);
config->max_htlc_value_in_flight_msat = fromwire_u64(ptr, max); config->max_htlc_value_in_flight_msat = fromwire_u64(ptr, max);
config->channel_reserve_satoshis = 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->htlc_minimum_msat = fromwire_u32(ptr, max);
config->to_self_delay = fromwire_u16(ptr, max); config->to_self_delay = fromwire_u16(ptr, max);
config->max_accepted_htlcs = fromwire_u16(ptr, max); config->max_accepted_htlcs = fromwire_u16(ptr, max);
if (!*ptr)
return tal_free(config);
return config;
} }

4
lightningd/channel_config.h

@ -41,6 +41,6 @@ struct channel_config {
}; };
void towire_channel_config(u8 **pptr, const struct channel_config *config); void towire_channel_config(u8 **pptr, const struct channel_config *config);
struct channel_config *fromwire_channel_config(const tal_t *ctx, void fromwire_channel_config(const u8 **ptr, size_t *max,
const u8 **ptr, size_t *max); struct channel_config *config);
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONFIG_H */ #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONFIG_H */

27
lightningd/cryptomsg.c

@ -12,23 +12,6 @@
#include <wire/wire.h> #include <wire/wire.h>
#include <wire/wire_io.h> #include <wire/wire_io.h>
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, static void hkdf_two_keys(struct sha256 *out1, struct sha256 *out2,
const struct sha256 *in1, const struct sha256 *in1,
const struct sha256 *in2) 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); towire_sha256(ptr, &cs->r_ck);
} }
struct crypto_state *fromwire_crypto_state(const tal_t *ctx, void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs)
const u8 **ptr, size_t *max)
{ {
struct crypto_state *cs = tal(ctx, struct crypto_state);
cs->rn = fromwire_u64(ptr, max); cs->rn = fromwire_u64(ptr, max);
cs->sn = fromwire_u64(ptr, max); cs->sn = fromwire_u64(ptr, max);
fromwire_sha256(ptr, max, &cs->sk); fromwire_sha256(ptr, max, &cs->sk);
fromwire_sha256(ptr, max, &cs->rk); fromwire_sha256(ptr, max, &cs->rk);
fromwire_sha256(ptr, max, &cs->s_ck); fromwire_sha256(ptr, max, &cs->s_ck);
fromwire_sha256(ptr, max, &cs->r_ck); fromwire_sha256(ptr, max, &cs->r_ck);
cs->peer = (struct peer *)ctx;
if (!*ptr)
return tal_free(cs);
return cs;
} }

23
lightningd/cryptomsg.h

@ -1,12 +1,29 @@
#ifndef LIGHTNING_LIGHTNINGD_CRYPTOMSG_H #ifndef LIGHTNING_LIGHTNINGD_CRYPTOMSG_H
#define LIGHTNING_LIGHTNINGD_CRYPTOMSG_H #define LIGHTNING_LIGHTNINGD_CRYPTOMSG_H
#include "config.h" #include "config.h"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
struct io_conn; struct io_conn;
struct peer; 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 */ /* Initializes peer->crypto_state */
struct crypto_state *crypto_state(struct peer *peer, struct crypto_state *crypto_state(struct peer *peer,
@ -31,7 +48,5 @@ struct io_plan *peer_write_message(struct io_conn *conn,
struct peer *)); struct peer *));
void towire_crypto_state(u8 **pptr, const struct crypto_state *cs); void towire_crypto_state(u8 **pptr, const struct crypto_state *cs);
struct crypto_state *fromwire_crypto_state(const tal_t *ctx, void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs);
const u8 **ptr, size_t *max);
#endif /* LIGHTNING_LIGHTNINGD_CRYPTOMSG_H */ #endif /* LIGHTNING_LIGHTNINGD_CRYPTOMSG_H */

6
lightningd/gossip/gossip.c

@ -14,6 +14,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/gossip/gen_gossip_control_wire.h> #include <lightningd/gossip/gen_gossip_control_wire.h>
#include <lightningd/gossip/gen_gossip_status_wire.h> #include <lightningd/gossip/gen_gossip_status_wire.h>
#include <secp256k1_ecdh.h> #include <secp256k1_ecdh.h>
@ -69,9 +70,10 @@ static void destroy_peer(struct peer *peer)
static struct peer *setup_new_peer(struct daemon *daemon, const u8 *msg) static struct peer *setup_new_peer(struct daemon *daemon, const u8 *msg)
{ {
struct peer *peer = tal(daemon, struct peer); struct peer *peer = tal(daemon, struct peer);
if (!fromwire_gossipctl_new_peer(peer, msg, NULL, peer->cs = tal(peer, struct crypto_state);
&peer->unique_id, &peer->cs)) if (!fromwire_gossipctl_new_peer(msg, NULL, &peer->unique_id, peer->cs))
return tal_free(peer); return tal_free(peer);
peer->cs->peer = peer;
peer->daemon = daemon; peer->daemon = daemon;
peer->error = NULL; peer->error = NULL;
list_add_tail(&daemon->peers, &peer->list); list_add_tail(&daemon->peers, &peer->list);

6
lightningd/gossip_control.c

@ -7,6 +7,7 @@
#include <daemon/jsonrpc.h> #include <daemon/jsonrpc.h>
#include <daemon/log.h> #include <daemon/log.h>
#include <inttypes.h> #include <inttypes.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/gossip/gen_gossip_control_wire.h> #include <lightningd/gossip/gen_gossip_control_wire.h>
#include <lightningd/gossip/gen_gossip_status_wire.h> #include <lightningd/gossip/gen_gossip_status_wire.h>
@ -45,16 +46,17 @@ static void peer_nongossip(struct subdaemon *gossip, const u8 *msg, int fd)
u64 unique_id; u64 unique_id;
struct peer *peer; struct peer *peer;
u8 *inner; u8 *inner;
struct crypto_state *cs; struct crypto_state *cs = tal(msg, struct crypto_state);
if (!fromwire_gossipstatus_peer_nongossip(msg, msg, NULL, if (!fromwire_gossipstatus_peer_nongossip(msg, msg, NULL,
&unique_id, &cs, &inner)) &unique_id, cs, &inner))
fatal("Gossip gave bad PEER_NONGOSSIP message %s", fatal("Gossip gave bad PEER_NONGOSSIP message %s",
tal_hex(msg, msg)); tal_hex(msg, msg));
peer = peer_by_unique_id(gossip->ld, unique_id); peer = peer_by_unique_id(gossip->ld, unique_id);
if (!peer) if (!peer)
fatal("Gossip gave bad peerid %"PRIu64, unique_id); fatal("Gossip gave bad peerid %"PRIu64, unique_id);
cs->peer = peer;
log_debug(gossip->log, "Peer %s said %s", log_debug(gossip->log, "Peer %s said %s",
type_to_string(msg, struct pubkey, peer->id), type_to_string(msg, struct pubkey, peer->id),

7
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, static void handshake_succeeded(struct subdaemon *hs, const u8 *msg,
struct peer *peer) struct peer *peer)
{ {
struct crypto_state *cs; struct crypto_state *cs = tal(peer, struct crypto_state);
if (!peer->id) { if (!peer->id) {
struct pubkey 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; goto err;
peer->id = tal_dup(peer, struct pubkey, &id); peer->id = tal_dup(peer, struct pubkey, &id);
log_info_struct(hs->log, "Peer in from %s", log_info_struct(hs->log, "Peer in from %s",
struct pubkey, peer->id); struct pubkey, peer->id);
} else { } else {
if (!fromwire_handshake_initiator_resp(msg, msg, NULL, &cs)) if (!fromwire_handshake_initiator_resp(msg, NULL, cs))
goto err; goto err;
log_info_struct(hs->log, "Peer out to %s", log_info_struct(hs->log, "Peer out to %s",
struct pubkey, peer->id); struct pubkey, peer->id);
} }
cs->peer = peer;
/* FIXME: Look for peer duplicates! */ /* FIXME: Look for peer duplicates! */

15
tools/generate-wire.py

@ -54,7 +54,6 @@ class Field(object):
self.comments = comments self.comments = comments
self.name = name.replace('-', '_') self.name = name.replace('-', '_')
self.is_len_var = False self.is_len_var = False
self.is_unknown = False
self.lenvar = None self.lenvar = None
# Size could be a literal number (eg. 33), or a field (eg 'len'), or # 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. # Unknown types are assumed to have base_size: div by 0 if that's unknown.
if self.fieldtype.tsize == 0: if self.fieldtype.tsize == 0:
self.is_unknown = True
self.fieldtype.tsize = base_size self.fieldtype.tsize = base_size
if base_size % self.fieldtype.tsize != 0: if base_size % self.fieldtype.tsize != 0:
@ -185,8 +183,6 @@ class Message(object):
if field.is_variable_size(): if field.is_variable_size():
self.checkLenField(field) self.checkLenField(field)
self.has_variable_fields = True self.has_variable_fields = True
elif field.is_unknown:
self.has_variable_fields = True
self.fields.append(field) self.fields.append(field)
def print_fromwire(self,is_header): def print_fromwire(self,is_header):
@ -205,7 +201,7 @@ class Message(object):
continue continue
if f.is_array(): if f.is_array():
print(', {} {}[{}]'.format(f.fieldtype.name, f.name, f.num_elems), end='') 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='') print(', {} **{}'.format(f.fieldtype.name, f.name), end='')
else: else:
print(', {} *{}'.format(f.fieldtype.name, f.name), end='') print(', {} *{}'.format(f.fieldtype.name, f.name), end='')
@ -240,14 +236,7 @@ class Message(object):
for c in f.comments: for c in f.comments:
print('\t/*{} */'.format(c)) print('\t/*{} */'.format(c))
if f.is_unknown: if f.is_padding():
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():
print('\tfromwire_pad(&cursor, plen, {});' print('\tfromwire_pad(&cursor, plen, {});'
.format(f.num_elems)) .format(f.num_elems))
elif f.is_array(): elif f.is_array():

Loading…
Cancel
Save