wire: add explicit-length fromwire_peektype variant.

Useful for continuous streams.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2025-11-27 12:48:57 +10:30
parent fc23b538cd
commit f052408832
2 changed files with 8 additions and 2 deletions

View File

@@ -41,10 +41,9 @@ 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) int fromwire_peektypen(const u8 *cursor, size_t max)
{ {
be16 be_type; be16 be_type;
size_t max = tal_count(cursor);
fromwire(&cursor, &max, &be_type, sizeof(be_type)); fromwire(&cursor, &max, &be_type, sizeof(be_type));
if (!cursor) if (!cursor)
@@ -52,6 +51,11 @@ int fromwire_peektype(const u8 *cursor)
return be16_to_cpu(be_type); return be16_to_cpu(be_type);
} }
int fromwire_peektype(const u8 *cursor)
{
return fromwire_peektypen(cursor, tal_count(cursor));
}
u8 fromwire_u8(const u8 **cursor, size_t *max) u8 fromwire_u8(const u8 **cursor, size_t *max)
{ {
u8 ret; u8 ret;

View File

@@ -15,6 +15,8 @@ typedef char utf8;
/* Read the type; returns -1 if not long enough. cursor is a tal ptr. */ /* Read the type; returns -1 if not long enough. cursor is a tal ptr. */
int fromwire_peektype(const u8 *cursor); int fromwire_peektype(const u8 *cursor);
/* Same, but doesn't need to be a tal ptr */
int fromwire_peektypen(const u8 *cursor, size_t len);
void *fromwire_fail(const u8 **cursor, size_t *max); void *fromwire_fail(const u8 **cursor, size_t *max);
void towire(u8 **pptr, const void *data, size_t len); void towire(u8 **pptr, const void *data, size_t len);