Skip to content

Commit

Permalink
tools/generate-wire.py: wirestring type for handing strings.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Feb 8, 2018
1 parent 9a6c36a commit b7db06b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/generate-wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'failed_htlc',
'utxo',
'bitcoin_tx',
'wirestring',
]

class FieldType(object):
Expand Down
21 changes: 21 additions & 0 deletions wire/fromwire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions wire/towire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions wire/wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b7db06b

Please sign in to comment.