From b7db06b577c448ef7c96fb88bd5cdf42381dc81e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 8 Feb 2018 11:55:12 +1030 Subject: [PATCH] tools/generate-wire.py: wirestring type for handing strings. A convenient alias for char *, though we don't allow control characters so our logs can't be fooled with embedded \n. Signed-off-by: Rusty Russell --- tools/generate-wire.py | 1 + wire/fromwire.c | 21 +++++++++++++++++++++ wire/towire.c | 5 +++++ wire/wire.h | 5 +++++ 4 files changed, 32 insertions(+) diff --git a/tools/generate-wire.py b/tools/generate-wire.py index 6a52669a5..0900b81e0 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -33,6 +33,7 @@ varlen_structs = [ 'failed_htlc', 'utxo', 'bitcoin_tx', + 'wirestring', ] class FieldType(object): diff --git a/wire/fromwire.c b/wire/fromwire.c index b86808a35..80147b69c 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -206,6 +206,27 @@ void fromwire_pad(const u8 **cursor, size_t *max, size_t num) fromwire(cursor, max, NULL, num); } +/* + * Don't allow control chars except spaces: we only use this for stuff + * from subdaemons, who shouldn't do that. + */ +char *fromwire_wirestring(const tal_t *ctx, const u8 **cursor, size_t *max) +{ + size_t i; + + for (i = 0; i < *max; i++) { + if ((*cursor)[i] == '\0') { + char *str = tal_arr(ctx, char, i + 1); + fromwire(cursor, max, str, i + 1); + return str; + } + if ((*cursor)[i] < ' ') + break; + } + fromwire_fail(cursor, max); + return NULL; +} + REGISTER_TYPE_TO_STRING(short_channel_id, short_channel_id_to_str); REGISTER_TYPE_TO_HEXSTR(channel_id); diff --git a/wire/towire.c b/wire/towire.c index 154825a0c..229e585bf 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -151,6 +151,11 @@ void towire_pad(u8 **pptr, size_t num) memset(*pptr + oldsize, 0, num); } +void towire_wirestring(u8 **pptr, const char *str) +{ + towire(pptr, str, strlen(str) + 1); +} + void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx) { tal_t *tmpctx = tal_tmpctx(NULL); diff --git a/wire/wire.h b/wire/wire.h index 167f342ec..571232492 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -21,6 +21,9 @@ struct bitcoin_txid; struct preimage; struct ripemd160; +/* Makes generate-wire.py work */ +typedef char wirestring; + void derive_channel_id(struct channel_id *channel_id, struct bitcoin_txid *txid, u16 txout); @@ -55,6 +58,7 @@ void towire_bool(u8 **pptr, bool v); void towire_u8_array(u8 **pptr, const u8 *arr, size_t num); void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx); +void towire_wirestring(u8 **pptr, const char *str); const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n); u8 fromwire_u8(const u8 **cursor, size_t *max); @@ -86,6 +90,7 @@ void fromwire_ripemd160(const u8 **cursor, size_t *max, struct ripemd160 *ripemd void fromwire_pad(const u8 **cursor, size_t *max, size_t num); void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num); +char *fromwire_wirestring(const tal_t *ctx, const u8 **cursor, size_t *max); struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx, const u8 **cursor, size_t *max);