Browse Source

tlv: Add generic getters and setters for tlvstream

Trying to rework the TLV streams to have a more homogenous interface to work
with. This is by no means a complete implementation, just the groundwork that
is going to be used by the wire code generator to generate the specific
accessors, but it's enough so we can manipulate TLV streams in the onion and
later just switch to the generated ones.
paymod-01
Christian Decker 5 years ago
parent
commit
b85f8f8006
  1. 85
      wire/tlvstream.c
  2. 18
      wire/tlvstream.h

85
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; i<tal_count(stream); i++)
if (stream[i].numtype == type)
return &stream[i];
return NULL;
}
bool tlvstream_get_short_channel_id(struct tlv_field *stream, u64 type,
struct short_channel_id *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;
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;
}

18
wire/tlvstream.h

@ -1,10 +1,9 @@
#ifndef LIGHTNING_WIRE_TLVSTREAM_H
#define LIGHTNING_WIRE_TLVSTREAM_H
#include "config.h"
#include <bitcoin/short_channel_id.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <stdbool.h>
#include <stdlib.h>
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 */

Loading…
Cancel
Save