diff --git a/wire/tlvstream.c b/wire/tlvstream.c index 1a7d653c0..86662f6d5 100644 --- a/wire/tlvstream.c +++ b/wire/tlvstream.c @@ -20,3 +20,88 @@ void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields) towire(pptr, field->value, field->length); } } + +void tlvstream_set_raw(struct tlv_field **stream, u64 type, u8 *value TAKES) +{ + struct tlv_field f; + f.length = tal_bytelen(value); + f.numtype = type; + f.value = tal_dup_arr(*stream, u8, value, f.length, 0); + tal_arr_expand(stream, f); +} + +void tlvstream_set_short_channel_id(struct tlv_field **stream, u64 type, + struct short_channel_id *value) +{ + u8 *ser = tal_arr(NULL, u8, 0); + towire_short_channel_id(&ser, value); + tlvstream_set_raw(stream, type, take(ser)); +} + +void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value) +{ + u8 *ser = tal_arr(NULL, u8, 0); + towire_tu64(&ser, value); + tlvstream_set_raw(stream, type, take(ser)); +} + +void tlvstream_set_tu32(struct tlv_field **stream, u64 type, u32 value) +{ + u8 *ser = tal_arr(NULL, u8, 0); + towire_tu64(&ser, value); + tlvstream_set_raw(stream, type, take(ser)); +} + +static struct tlv_field *tlvstream_get_raw(struct tlv_field *stream, u64 type) +{ + for (size_t i=0; ilength != 8) + return false; + + max = raw->length; + v = raw->value; + fromwire_short_channel_id(&v, &max, value); + + return true; +} + +bool tlvstream_get_tu64(struct tlv_field *stream, u64 type, u64 *value) +{ + struct tlv_field *raw = tlvstream_get_raw(stream, type); + const u8 *v; + size_t max; + if (raw == NULL || raw->length != 8) + return false; + + max = raw->length; + v = raw->value; + *value = fromwire_tu64(&v, &max); + + return true; +} + +bool tlvstream_get_tu32(struct tlv_field *stream, u64 type, u32 *value) +{ + struct tlv_field *raw = tlvstream_get_raw(stream, type); + const u8 *v; + size_t max; + if (raw == NULL || raw->length != 8) + return false; + + max = raw->length; + v = raw->value; + *value = fromwire_tu64(&v, &max); + return true; +} diff --git a/wire/tlvstream.h b/wire/tlvstream.h index 5ec9939a7..b119c9f5c 100644 --- a/wire/tlvstream.h +++ b/wire/tlvstream.h @@ -1,10 +1,9 @@ #ifndef LIGHTNING_WIRE_TLVSTREAM_H #define LIGHTNING_WIRE_TLVSTREAM_H #include "config.h" +#include #include #include -#include -#include struct tlv_record_type { u64 type; @@ -37,4 +36,19 @@ void towire_tlvs(u8 **pptr, /* Given any tlvstream serialize the raw fields (untyped ones). */ void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields); + + +/* Generic primitive setters for tlvstreams. */ +void tlvstream_set_raw(struct tlv_field **stream, u64 type, u8 *value TAKES); +void tlvstream_set_short_channel_id(struct tlv_field **stream, u64 type, + struct short_channel_id *value); +void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value); +void tlvstream_set_tu32(struct tlv_field **stream, u64 type, u32 value); + +/* Generic primitive gettes for tlvstreams. */ +bool tlvstream_get_short_channel_id(struct tlv_field *stream, u64 type, + struct short_channel_id *value); +bool tlvstream_get_tu64(struct tlv_field *stream, u64 type, u64 *value); +bool tlvstream_get_tu32(struct tlv_field *stream, u64 type, u32 *value); + #endif /* LIGHTNING_WIRE_TLVSTREAM_H */