Browse Source

generate-wire.py: include type bytes in towire/fromwire routines.

This removes some redundancy in creating messages, but also allows
a lazy form or parsing without explicitly checking the type.

A helper fromwire_peektype() is added to look up the type and handle
the too-short-for-type problem.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
e076d56709
  1. 6
      daemon/p2p_announce.c
  2. 20
      tools/generate-wire.py
  3. 11
      wire/fromwire.c
  4. 52
      wire/test/run-peer-wire.c
  5. 3
      wire/wire.h

6
daemon/p2p_announce.c

@ -295,7 +295,7 @@ void handle_node_announcement(
"Received node_announcement for node %s", "Received node_announcement for node %s",
struct pubkey, &node_id); struct pubkey, &node_id);
sha256_double(&hash, serialized + 64, tal_count(serialized) - 64); sha256_double(&hash, serialized + 66, tal_count(serialized) - 66);
if (!check_signed_hash(&hash, &signature, &node_id)) { if (!check_signed_hash(&hash, &signature, &node_id)) {
log_debug(peer->dstate->base_log, log_debug(peer->dstate->base_log,
"Ignoring node announcement, signature verification failed."); "Ignoring node announcement, signature verification failed.");
@ -359,7 +359,7 @@ static void broadcast_channel_update(struct lightningd_state *dstate, struct pee
1, 1,
dstate->config.fee_base, dstate->config.fee_base,
dstate->config.fee_per_satoshi); dstate->config.fee_per_satoshi);
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64, privkey_sign(dstate, serialized + 66, tal_count(serialized) - 66,
&signature); &signature);
serialized = towire_channel_update(tmpctx, &signature, &channel_id, serialized = towire_channel_update(tmpctx, &signature, &channel_id,
timestamp, timestamp,
@ -397,7 +397,7 @@ static void broadcast_node_announcement(struct lightningd_state *dstate)
timestamp, timestamp,
&ipv6, dstate->portnum, &ipv6, dstate->portnum,
&dstate->id, rgb_color, alias); &dstate->id, rgb_color, alias);
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64, privkey_sign(dstate, serialized + 66, tal_count(serialized) - 66,
&signature); &signature);
serialized = towire_node_announcement(tmpctx, &signature, serialized = towire_node_announcement(tmpctx, &signature,
timestamp, timestamp,

20
tools/generate-wire.py

@ -137,9 +137,6 @@ class Message(object):
self.fields.append(field) self.fields.append(field)
def print_fromwire(self,is_header): def print_fromwire(self,is_header):
if not self.fields:
return
if self.has_variable_fields: if self.has_variable_fields:
ctx_arg = 'const tal_t *ctx, ' ctx_arg = 'const tal_t *ctx, '
else: else:
@ -165,14 +162,17 @@ class Message(object):
return return
print(')\n' print(')\n'
'{\n') '{')
for f in self.fields: for f in self.fields:
if f.is_len_var: if f.is_len_var:
print('\t{} {};\n'.format(f.typename, f.name)); print('\t{} {};'.format(f.typename, f.name));
print('\tconst u8 *cursor = p;\n' print('\tconst u8 *cursor = p;\n'
'') '\n'
'\tif (fromwire_u16(&cursor, plen) != {})\n'
'\t\treturn false;'
.format(self.enum.name))
for f in self.fields: for f in self.fields:
basetype=f.typename basetype=f.typename
@ -213,9 +213,6 @@ class Message(object):
'}\n') '}\n')
def print_towire(self,is_header): def print_towire(self,is_header):
if not self.fields:
return
print('u8 *towire_{}(const tal_t *ctx' print('u8 *towire_{}(const tal_t *ctx'
.format(self.name), end='') .format(self.name), end='')
@ -234,9 +231,10 @@ class Message(object):
return return
print(')\n' print(')\n'
'{\n' '{{\n'
'\tu8 *p = tal_arr(ctx, u8, 0);\n' '\tu8 *p = tal_arr(ctx, u8, 0);\n'
'') ''
'\ttowire_u16(&p, {});'.format(self.enum.name))
for f in self.fields: for f in self.fields:
basetype=f.typename basetype=f.typename

11
wire/fromwire.c

@ -29,6 +29,17 @@ const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n)
return memcheck(p, n); return memcheck(p, n);
} }
int fromwire_peektype(const u8 *cursor)
{
be16 be_type;
size_t max = tal_count(cursor);
fromwire(&cursor, &max, &be_type, sizeof(be_type));
if (!cursor)
return -1;
return be16_to_cpu(be_type);
}
u8 fromwire_u8(const u8 **cursor, size_t *max) u8 fromwire_u8(const u8 **cursor, size_t *max)
{ {
u8 ret; u8 ret;

52
wire/test/run-peer-wire.c

@ -232,7 +232,7 @@ static void *towire_struct_channel_announcement(const tal_t *ctx,
&s->bitcoin_key_2); &s->bitcoin_key_2);
} }
static struct msg_channel_announcement *fromwire_struct_channel_announcement(const tal_t *ctx, const void *p, size_t *plen) static struct msg_channel_announcement *fromwire_struct_channel_announcement(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_channel_announcement *s = tal(ctx, struct msg_channel_announcement); struct msg_channel_announcement *s = tal(ctx, struct msg_channel_announcement);
if (!fromwire_channel_announcement(p, plen, if (!fromwire_channel_announcement(p, plen,
@ -270,9 +270,10 @@ static void *towire_struct_open_channel(const tal_t *ctx,
&s->first_per_commitment_point); &s->first_per_commitment_point);
} }
static struct msg_open_channel *fromwire_struct_open_channel(const tal_t *ctx, const void *p, size_t *plen) static struct msg_open_channel *fromwire_struct_open_channel(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_open_channel *s = tal(ctx, struct msg_open_channel); struct msg_open_channel *s = tal(ctx, struct msg_open_channel);
if (fromwire_open_channel(p, plen, if (fromwire_open_channel(p, plen,
&s->temporary_channel_id, &s->temporary_channel_id,
&s->funding_satoshis, &s->funding_satoshis,
@ -312,9 +313,10 @@ static void *towire_struct_accept_channel(const tal_t *ctx,
&s->first_per_commitment_point); &s->first_per_commitment_point);
} }
static struct msg_accept_channel *fromwire_struct_accept_channel(const tal_t *ctx, const void *p, size_t *plen) static struct msg_accept_channel *fromwire_struct_accept_channel(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_accept_channel *s = tal(ctx, struct msg_accept_channel); struct msg_accept_channel *s = tal(ctx, struct msg_accept_channel);
if (fromwire_accept_channel(p, plen, if (fromwire_accept_channel(p, plen,
&s->temporary_channel_id, &s->temporary_channel_id,
&s->dust_limit_satoshis, &s->dust_limit_satoshis,
@ -347,7 +349,7 @@ static void *towire_struct_node_announcement(const tal_t *ctx,
s->alias); s->alias);
} }
static struct msg_node_announcement *fromwire_struct_node_announcement(const tal_t *ctx, const void *p, size_t *plen) static struct msg_node_announcement *fromwire_struct_node_announcement(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_node_announcement *s = tal(ctx, struct msg_node_announcement); struct msg_node_announcement *s = tal(ctx, struct msg_node_announcement);
fromwire_pad_arr = s->padding; fromwire_pad_arr = s->padding;
@ -377,9 +379,10 @@ static void *towire_struct_channel_update(const tal_t *ctx,
s->fee_proportional_millionths); s->fee_proportional_millionths);
} }
static struct msg_channel_update *fromwire_struct_channel_update(const tal_t *ctx, const void *p, size_t *plen) static struct msg_channel_update *fromwire_struct_channel_update(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_channel_update *s = tal(ctx, struct msg_channel_update); struct msg_channel_update *s = tal(ctx, struct msg_channel_update);
if (fromwire_channel_update(p, plen, if (fromwire_channel_update(p, plen,
&s->signature, &s->signature,
&s->channel_id, &s->channel_id,
@ -404,9 +407,10 @@ static void *towire_struct_funding_locked(const tal_t *ctx,
&s->next_per_commitment_point); &s->next_per_commitment_point);
} }
static struct msg_funding_locked *fromwire_struct_funding_locked(const tal_t *ctx, const void *p, size_t *plen) static struct msg_funding_locked *fromwire_struct_funding_locked(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_funding_locked *s = tal(ctx, struct msg_funding_locked); struct msg_funding_locked *s = tal(ctx, struct msg_funding_locked);
if (fromwire_funding_locked(p, plen, if (fromwire_funding_locked(p, plen,
&s->temporary_channel_id, &s->temporary_channel_id,
&s->channel_id, &s->channel_id,
@ -426,9 +430,10 @@ static void *towire_struct_update_fail_htlc(const tal_t *ctx,
s->reason); s->reason);
} }
static struct msg_update_fail_htlc *fromwire_struct_update_fail_htlc(const tal_t *ctx, const void *p, size_t *plen) static struct msg_update_fail_htlc *fromwire_struct_update_fail_htlc(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_update_fail_htlc *s = tal(ctx, struct msg_update_fail_htlc); struct msg_update_fail_htlc *s = tal(ctx, struct msg_update_fail_htlc);
if (fromwire_update_fail_htlc(p, plen, if (fromwire_update_fail_htlc(p, plen,
&s->channel_id, &s->channel_id,
&s->id, &s->id,
@ -446,9 +451,10 @@ static void *towire_struct_update_fulfill_htlc(const tal_t *ctx,
&s->payment_preimage); &s->payment_preimage);
} }
static struct msg_update_fulfill_htlc *fromwire_struct_update_fulfill_htlc(const tal_t *ctx, const void *p, size_t *plen) static struct msg_update_fulfill_htlc *fromwire_struct_update_fulfill_htlc(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_update_fulfill_htlc *s = tal(ctx, struct msg_update_fulfill_htlc); struct msg_update_fulfill_htlc *s = tal(ctx, struct msg_update_fulfill_htlc);
if (fromwire_update_fulfill_htlc(p, plen, if (fromwire_update_fulfill_htlc(p, plen,
&s->channel_id, &s->channel_id,
&s->id, &s->id,
@ -467,9 +473,10 @@ static void *towire_struct_commit_sig(const tal_t *ctx,
s->htlc_signature); s->htlc_signature);
} }
static struct msg_commit_sig *fromwire_struct_commit_sig(const tal_t *ctx, const void *p, size_t *plen) static struct msg_commit_sig *fromwire_struct_commit_sig(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_commit_sig *s = tal(ctx, struct msg_commit_sig); struct msg_commit_sig *s = tal(ctx, struct msg_commit_sig);
if (!fromwire_commit_sig(s, p, plen, if (!fromwire_commit_sig(s, p, plen,
&s->channel_id, &s->channel_id,
&s->signature, &s->signature,
@ -491,9 +498,10 @@ static void *towire_struct_revoke_and_ack(const tal_t *ctx,
s->htlc_timeout_signature); s->htlc_timeout_signature);
} }
static struct msg_revoke_and_ack *fromwire_struct_revoke_and_ack(const tal_t *ctx, const void *p, size_t *plen) static struct msg_revoke_and_ack *fromwire_struct_revoke_and_ack(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_revoke_and_ack *s = tal(ctx, struct msg_revoke_and_ack); struct msg_revoke_and_ack *s = tal(ctx, struct msg_revoke_and_ack);
fromwire_pad_arr = s->padding; fromwire_pad_arr = s->padding;
if (!fromwire_revoke_and_ack(s, p, plen, if (!fromwire_revoke_and_ack(s, p, plen,
&s->channel_id, &s->channel_id,
@ -514,9 +522,10 @@ static void *towire_struct_funding_signed(const tal_t *ctx,
&s->signature); &s->signature);
} }
static struct msg_funding_signed *fromwire_struct_funding_signed(const tal_t *ctx, const void *p, size_t *plen) static struct msg_funding_signed *fromwire_struct_funding_signed(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_funding_signed *s = tal(ctx, struct msg_funding_signed); struct msg_funding_signed *s = tal(ctx, struct msg_funding_signed);
if (fromwire_funding_signed(p, plen, if (fromwire_funding_signed(p, plen,
&s->temporary_channel_id, &s->temporary_channel_id,
&s->signature)) &s->signature))
@ -533,9 +542,10 @@ static void *towire_struct_closing_signed(const tal_t *ctx,
&s->signature); &s->signature);
} }
static struct msg_closing_signed *fromwire_struct_closing_signed(const tal_t *ctx, const void *p, size_t *plen) static struct msg_closing_signed *fromwire_struct_closing_signed(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_closing_signed *s = tal(ctx, struct msg_closing_signed); struct msg_closing_signed *s = tal(ctx, struct msg_closing_signed);
if (fromwire_closing_signed(p, plen, if (fromwire_closing_signed(p, plen,
&s->channel_id, &s->channel_id,
&s->fee_satoshis, &s->fee_satoshis,
@ -553,9 +563,10 @@ static void *towire_struct_shutdown(const tal_t *ctx,
s->scriptpubkey); s->scriptpubkey);
} }
static struct msg_shutdown *fromwire_struct_shutdown(const tal_t *ctx, const void *p, size_t *plen) static struct msg_shutdown *fromwire_struct_shutdown(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_shutdown *s = tal(ctx, struct msg_shutdown); struct msg_shutdown *s = tal(ctx, struct msg_shutdown);
if (!fromwire_shutdown(s, p, plen, if (!fromwire_shutdown(s, p, plen,
&s->channel_id, &s->channel_id,
&s->scriptpubkey)) &s->scriptpubkey))
@ -574,9 +585,10 @@ static void *towire_struct_funding_created(const tal_t *ctx,
&s->signature); &s->signature);
} }
static struct msg_funding_created *fromwire_struct_funding_created(const tal_t *ctx, const void *p, size_t *plen) static struct msg_funding_created *fromwire_struct_funding_created(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_funding_created *s = tal(ctx, struct msg_funding_created); struct msg_funding_created *s = tal(ctx, struct msg_funding_created);
if (fromwire_funding_created(p, plen, if (fromwire_funding_created(p, plen,
&s->temporary_channel_id, &s->temporary_channel_id,
&s->txid, &s->txid,
@ -595,9 +607,10 @@ static void *towire_struct_error(const tal_t *ctx,
s->data); s->data);
} }
static struct msg_error *fromwire_struct_error(const tal_t *ctx, const void *p, size_t *plen) static struct msg_error *fromwire_struct_error(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_error *s = tal(ctx, struct msg_error); struct msg_error *s = tal(ctx, struct msg_error);
if (!fromwire_error(s, p, plen, if (!fromwire_error(s, p, plen,
&s->channel_id, &s->channel_id,
&s->data)) &s->data))
@ -618,9 +631,10 @@ static void *towire_struct_update_add_htlc(const tal_t *ctx,
s->onion_routing_packet); s->onion_routing_packet);
} }
static struct msg_update_add_htlc *fromwire_struct_update_add_htlc(const tal_t *ctx, const void *p, size_t *plen) static struct msg_update_add_htlc *fromwire_struct_update_add_htlc(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_update_add_htlc *s = tal(ctx, struct msg_update_add_htlc); struct msg_update_add_htlc *s = tal(ctx, struct msg_update_add_htlc);
if (fromwire_update_add_htlc(p, plen, if (fromwire_update_add_htlc(p, plen,
&s->channel_id, &s->channel_id,
&s->id, &s->id,
@ -641,9 +655,10 @@ static void *towire_struct_update_fee(const tal_t *ctx,
s->feerate_per_kw); s->feerate_per_kw);
} }
static struct msg_update_fee *fromwire_struct_update_fee(const tal_t *ctx, const void *p, size_t *plen) static struct msg_update_fee *fromwire_struct_update_fee(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_update_fee *s = tal(ctx, struct msg_update_fee); struct msg_update_fee *s = tal(ctx, struct msg_update_fee);
if (fromwire_update_fee(p, plen, if (fromwire_update_fee(p, plen,
&s->channel_id, &s->channel_id,
&s->feerate_per_kw)) &s->feerate_per_kw))
@ -661,9 +676,10 @@ static void *towire_struct_init(const tal_t *ctx,
s->localfeatures); s->localfeatures);
} }
static struct msg_init *fromwire_struct_init(const tal_t *ctx, const void *p, size_t *plen) static struct msg_init *fromwire_struct_init(const tal_t *ctx, const u8 *p, size_t *plen)
{ {
struct msg_init *s = tal(ctx, struct msg_init); struct msg_init *s = tal(ctx, struct msg_init);
if (!fromwire_init(s, p, plen, if (!fromwire_init(s, p, plen,
&s->globalfeatures, &s->globalfeatures,
&s->localfeatures)) &s->localfeatures))

3
wire/wire.h

@ -18,6 +18,9 @@ struct ipv6 {
u8 addr[16]; u8 addr[16];
}; };
/* Read the type; returns -1 if not long enough. cursor is a tal ptr. */
int fromwire_peektype(const u8 *cursor);
void towire(u8 **pptr, const void *data, size_t len); void towire(u8 **pptr, const void *data, size_t len);
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey); void towire_pubkey(u8 **pptr, const struct pubkey *pubkey);
void towire_signature(u8 **pptr, const struct signature *signature); void towire_signature(u8 **pptr, const struct signature *signature);

Loading…
Cancel
Save