mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
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>
This commit is contained in:
parent
3e044fdd62
commit
e076d56709
@ -295,7 +295,7 @@ void handle_node_announcement(
|
||||
"Received node_announcement for node %s",
|
||||
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)) {
|
||||
log_debug(peer->dstate->base_log,
|
||||
"Ignoring node announcement, signature verification failed.");
|
||||
@ -359,7 +359,7 @@ static void broadcast_channel_update(struct lightningd_state *dstate, struct pee
|
||||
1,
|
||||
dstate->config.fee_base,
|
||||
dstate->config.fee_per_satoshi);
|
||||
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64,
|
||||
privkey_sign(dstate, serialized + 66, tal_count(serialized) - 66,
|
||||
&signature);
|
||||
serialized = towire_channel_update(tmpctx, &signature, &channel_id,
|
||||
timestamp,
|
||||
@ -397,7 +397,7 @@ static void broadcast_node_announcement(struct lightningd_state *dstate)
|
||||
timestamp,
|
||||
&ipv6, dstate->portnum,
|
||||
&dstate->id, rgb_color, alias);
|
||||
privkey_sign(dstate, serialized + 64, tal_count(serialized) - 64,
|
||||
privkey_sign(dstate, serialized + 66, tal_count(serialized) - 66,
|
||||
&signature);
|
||||
serialized = towire_node_announcement(tmpctx, &signature,
|
||||
timestamp,
|
||||
|
@ -137,9 +137,6 @@ class Message(object):
|
||||
self.fields.append(field)
|
||||
|
||||
def print_fromwire(self,is_header):
|
||||
if not self.fields:
|
||||
return
|
||||
|
||||
if self.has_variable_fields:
|
||||
ctx_arg = 'const tal_t *ctx, '
|
||||
else:
|
||||
@ -165,14 +162,17 @@ class Message(object):
|
||||
return
|
||||
|
||||
print(')\n'
|
||||
'{\n')
|
||||
'{')
|
||||
|
||||
for f in self.fields:
|
||||
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'
|
||||
'')
|
||||
'\n'
|
||||
'\tif (fromwire_u16(&cursor, plen) != {})\n'
|
||||
'\t\treturn false;'
|
||||
.format(self.enum.name))
|
||||
|
||||
for f in self.fields:
|
||||
basetype=f.typename
|
||||
@ -213,9 +213,6 @@ class Message(object):
|
||||
'}\n')
|
||||
|
||||
def print_towire(self,is_header):
|
||||
if not self.fields:
|
||||
return
|
||||
|
||||
print('u8 *towire_{}(const tal_t *ctx'
|
||||
.format(self.name), end='')
|
||||
|
||||
@ -234,9 +231,10 @@ class Message(object):
|
||||
return
|
||||
|
||||
print(')\n'
|
||||
'{\n'
|
||||
'{{\n'
|
||||
'\tu8 *p = tal_arr(ctx, u8, 0);\n'
|
||||
'')
|
||||
''
|
||||
'\ttowire_u16(&p, {});'.format(self.enum.name))
|
||||
|
||||
for f in self.fields:
|
||||
basetype=f.typename
|
||||
|
@ -29,6 +29,17 @@ const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t 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 ret;
|
||||
|
@ -232,7 +232,7 @@ static void *towire_struct_channel_announcement(const tal_t *ctx,
|
||||
&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);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (fromwire_open_channel(p, plen,
|
||||
&s->temporary_channel_id,
|
||||
&s->funding_satoshis,
|
||||
@ -312,9 +313,10 @@ static void *towire_struct_accept_channel(const tal_t *ctx,
|
||||
&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);
|
||||
|
||||
if (fromwire_accept_channel(p, plen,
|
||||
&s->temporary_channel_id,
|
||||
&s->dust_limit_satoshis,
|
||||
@ -347,7 +349,7 @@ static void *towire_struct_node_announcement(const tal_t *ctx,
|
||||
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);
|
||||
fromwire_pad_arr = s->padding;
|
||||
@ -377,9 +379,10 @@ static void *towire_struct_channel_update(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (fromwire_channel_update(p, plen,
|
||||
&s->signature,
|
||||
&s->channel_id,
|
||||
@ -404,9 +407,10 @@ static void *towire_struct_funding_locked(const tal_t *ctx,
|
||||
&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);
|
||||
|
||||
if (fromwire_funding_locked(p, plen,
|
||||
&s->temporary_channel_id,
|
||||
&s->channel_id,
|
||||
@ -426,9 +430,10 @@ static void *towire_struct_update_fail_htlc(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (fromwire_update_fail_htlc(p, plen,
|
||||
&s->channel_id,
|
||||
&s->id,
|
||||
@ -446,9 +451,10 @@ static void *towire_struct_update_fulfill_htlc(const tal_t *ctx,
|
||||
&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);
|
||||
|
||||
if (fromwire_update_fulfill_htlc(p, plen,
|
||||
&s->channel_id,
|
||||
&s->id,
|
||||
@ -467,9 +473,10 @@ static void *towire_struct_commit_sig(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (!fromwire_commit_sig(s, p, plen,
|
||||
&s->channel_id,
|
||||
&s->signature,
|
||||
@ -491,9 +498,10 @@ static void *towire_struct_revoke_and_ack(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
fromwire_pad_arr = s->padding;
|
||||
if (!fromwire_revoke_and_ack(s, p, plen,
|
||||
&s->channel_id,
|
||||
@ -514,9 +522,10 @@ static void *towire_struct_funding_signed(const tal_t *ctx,
|
||||
&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);
|
||||
|
||||
if (fromwire_funding_signed(p, plen,
|
||||
&s->temporary_channel_id,
|
||||
&s->signature))
|
||||
@ -533,9 +542,10 @@ static void *towire_struct_closing_signed(const tal_t *ctx,
|
||||
&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);
|
||||
|
||||
if (fromwire_closing_signed(p, plen,
|
||||
&s->channel_id,
|
||||
&s->fee_satoshis,
|
||||
@ -553,9 +563,10 @@ static void *towire_struct_shutdown(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (!fromwire_shutdown(s, p, plen,
|
||||
&s->channel_id,
|
||||
&s->scriptpubkey))
|
||||
@ -574,9 +585,10 @@ static void *towire_struct_funding_created(const tal_t *ctx,
|
||||
&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);
|
||||
|
||||
if (fromwire_funding_created(p, plen,
|
||||
&s->temporary_channel_id,
|
||||
&s->txid,
|
||||
@ -595,9 +607,10 @@ static void *towire_struct_error(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (!fromwire_error(s, p, plen,
|
||||
&s->channel_id,
|
||||
&s->data))
|
||||
@ -618,9 +631,10 @@ static void *towire_struct_update_add_htlc(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (fromwire_update_add_htlc(p, plen,
|
||||
&s->channel_id,
|
||||
&s->id,
|
||||
@ -641,9 +655,10 @@ static void *towire_struct_update_fee(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (fromwire_update_fee(p, plen,
|
||||
&s->channel_id,
|
||||
&s->feerate_per_kw))
|
||||
@ -661,9 +676,10 @@ static void *towire_struct_init(const tal_t *ctx,
|
||||
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);
|
||||
|
||||
if (!fromwire_init(s, p, plen,
|
||||
&s->globalfeatures,
|
||||
&s->localfeatures))
|
||||
|
@ -18,6 +18,9 @@ struct ipv6 {
|
||||
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_pubkey(u8 **pptr, const struct pubkey *pubkey);
|
||||
void towire_signature(u8 **pptr, const struct signature *signature);
|
||||
|
Loading…
Reference in New Issue
Block a user