rgb-cln/lightningd/gossip_msg.c

137 lines
4.4 KiB
C
Raw Normal View History

#include <common/wireaddr.h>
#include <lightningd/gossip_msg.h>
#include <wire/wire.h>
2018-04-20 09:09:50 +01:00
struct gossip_getnodes_entry *fromwire_gossip_getnodes_entry(const tal_t *ctx,
const u8 **pptr, size_t *max)
{
u8 numaddresses, i;
struct gossip_getnodes_entry *entry;
u16 flen;
entry = tal(ctx, struct gossip_getnodes_entry);
fromwire_pubkey(pptr, max, &entry->nodeid);
flen = fromwire_u16(pptr, max);
entry->globalfeatures = tal_arr(entry, u8, flen);
fromwire_u8_array(pptr, max, entry->globalfeatures, flen);
entry->last_timestamp = fromwire_u64(pptr, max);
if (entry->last_timestamp < 0) {
entry->addresses = NULL;
entry->alias = NULL;
return entry;
}
numaddresses = fromwire_u8(pptr, max);
entry->addresses = tal_arr(entry, struct wireaddr, numaddresses);
for (i=0; i<numaddresses; i++) {
/* Gossipd doesn't hand us addresses we can't understand. */
if (!fromwire_wireaddr(pptr, max, &entry->addresses[i])) {
fromwire_fail(pptr, max);
return NULL;
}
}
gossip_msg: make sure alias is NUL-terminated. Valgrind error file: valgrind-errors.772802 ==772802== Invalid read of size 1 ==772802== at 0x4C32D04: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==772802== by 0x14479C: escape (json_escaped.c:41) ==772802== by 0x144B6C: json_escape (json_escaped.c:117) ==772802== by 0x118518: json_getnodes_reply (gossip_control.c:209) ==772802== by 0x139394: sd_msg_reply (subd.c:281) ==772802== by 0x139972: sd_msg_read (subd.c:418) ==772802== by 0x17ABB1: next_plan (io.c:59) ==772802== by 0x17B6A9: do_plan (io.c:387) ==772802== by 0x17B6E7: io_ready (io.c:397) ==772802== by 0x17D2C8: io_loop (poll.c:310) ==772802== by 0x121973: main (lightningd.c:450) ==772802== Address 0x6fe5168 is 0 bytes after a block of size 72 alloc'd ==772802== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==772802== by 0x18843E: allocate (tal.c:245) ==772802== by 0x18899D: tal_alloc_ (tal.c:421) ==772802== by 0x188B5E: tal_alloc_arr_ (tal.c:464) ==772802== by 0x119BAB: fromwire_gossip_getnodes_entry (gossip_msg.c:35) ==772802== by 0x15CCD6: fromwire_gossip_getnodes_reply (gen_gossip_wire.c:111) ==772802== by 0x118436: json_getnodes_reply (gossip_control.c:192) ==772802== by 0x139394: sd_msg_reply (subd.c:281) ==772802== by 0x139972: sd_msg_read (subd.c:418) ==772802== by 0x17ABB1: next_plan (io.c:59) ==772802== by 0x17B6A9: do_plan (io.c:387) ==772802== by 0x17B6E7: io_ready (io.c:397) Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-07-28 06:53:25 +01:00
/* Make sure alias is NUL terminated */
entry->alias = tal_arrz(entry, u8, fromwire_u8(pptr, max)+1);
fromwire(pptr, max, entry->alias, tal_count(entry->alias)-1);
fromwire(pptr, max, entry->color, sizeof(entry->color));
return entry;
}
2018-04-20 09:09:50 +01:00
void towire_gossip_getnodes_entry(u8 **pptr,
const struct gossip_getnodes_entry *entry)
{
u8 i, numaddresses = tal_count(entry->addresses);
towire_pubkey(pptr, &entry->nodeid);
towire_u16(pptr, tal_count(entry->globalfeatures));
towire_u8_array(pptr, entry->globalfeatures,
tal_count(entry->globalfeatures));
towire_u64(pptr, entry->last_timestamp);
if (entry->last_timestamp < 0)
return;
towire_u8(pptr, numaddresses);
for (i=0; i<numaddresses; i++) {
towire_wireaddr(pptr, &entry->addresses[i]);
}
towire_u8(pptr, tal_count(entry->alias));
towire(pptr, entry->alias, tal_count(entry->alias));
towire(pptr, entry->color, sizeof(entry->color));
}
void fromwire_route_hop(const u8 **pptr, size_t *max, struct route_hop *entry)
{
fromwire_pubkey(pptr, max, &entry->nodeid);
fromwire_short_channel_id(pptr, max, &entry->channel_id);
entry->amount = fromwire_u64(pptr, max);
entry->delay = fromwire_u32(pptr, max);
}
void towire_route_hop(u8 **pptr, const struct route_hop *entry)
{
towire_pubkey(pptr, &entry->nodeid);
towire_short_channel_id(pptr, &entry->channel_id);
towire_u64(pptr, entry->amount);
towire_u32(pptr, entry->delay);
}
void fromwire_gossip_getchannels_entry(const u8 **pptr, size_t *max,
struct gossip_getchannels_entry *entry)
{
fromwire_short_channel_id(pptr, max, &entry->short_channel_id);
fromwire_pubkey(pptr, max, &entry->source);
fromwire_pubkey(pptr, max, &entry->destination);
entry->satoshis = fromwire_u64(pptr, max);
entry->message_flags = fromwire_u8(pptr, max);
entry->channel_flags = fromwire_u8(pptr, max);
entry->public = fromwire_bool(pptr, max);
entry->local_disabled = fromwire_bool(pptr, max);
entry->last_update_timestamp = fromwire_u32(pptr, max);
entry->base_fee_msat = fromwire_u32(pptr, max);
entry->fee_per_millionth = fromwire_u32(pptr, max);
entry->delay = fromwire_u32(pptr, max);
}
2018-04-20 09:09:50 +01:00
void towire_gossip_getchannels_entry(u8 **pptr,
const struct gossip_getchannels_entry *entry)
{
towire_short_channel_id(pptr, &entry->short_channel_id);
towire_pubkey(pptr, &entry->source);
towire_pubkey(pptr, &entry->destination);
towire_u64(pptr, entry->satoshis);
towire_u8(pptr, entry->message_flags);
towire_u8(pptr, entry->channel_flags);
towire_bool(pptr, entry->public);
towire_bool(pptr, entry->local_disabled);
towire_u32(pptr, entry->last_update_timestamp);
towire_u32(pptr, entry->base_fee_msat);
towire_u32(pptr, entry->fee_per_millionth);
towire_u32(pptr, entry->delay);
}
struct peer_features *
fromwire_peer_features(const tal_t *ctx, const u8 **pptr, size_t *max)
{
struct peer_features *pf = tal(ctx, struct peer_features);
size_t len;
len = fromwire_u16(pptr, max);
pf->localfeatures = tal_arr(pf, u8, len);
fromwire_u8_array(pptr, max, pf->localfeatures, len);
len = fromwire_u16(pptr, max);
pf->globalfeatures = tal_arr(pf, u8, len);
fromwire_u8_array(pptr, max, pf->globalfeatures, len);
return pf;
}
void towire_peer_features(u8 **pptr, const struct peer_features *pf)
{
towire_u16(pptr, tal_count(pf->localfeatures));
towire_u8_array(pptr, pf->localfeatures, tal_count(pf->localfeatures));
towire_u16(pptr, tal_count(pf->globalfeatures));
towire_u8_array(pptr, pf->globalfeatures, tal_count(pf->globalfeatures));
}