Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
b7db06b577
  1. 1
      tools/generate-wire.py
  2. 21
      wire/fromwire.c
  3. 5
      wire/towire.c
  4. 5
      wire/wire.h

1
tools/generate-wire.py

@ -33,6 +33,7 @@ varlen_structs = [
'failed_htlc', 'failed_htlc',
'utxo', 'utxo',
'bitcoin_tx', 'bitcoin_tx',
'wirestring',
] ]
class FieldType(object): class FieldType(object):

21
wire/fromwire.c

@ -206,6 +206,27 @@ void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
fromwire(cursor, max, NULL, 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_STRING(short_channel_id, short_channel_id_to_str);
REGISTER_TYPE_TO_HEXSTR(channel_id); REGISTER_TYPE_TO_HEXSTR(channel_id);

5
wire/towire.c

@ -151,6 +151,11 @@ void towire_pad(u8 **pptr, size_t num)
memset(*pptr + oldsize, 0, 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) void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx)
{ {
tal_t *tmpctx = tal_tmpctx(NULL); tal_t *tmpctx = tal_tmpctx(NULL);

5
wire/wire.h

@ -21,6 +21,9 @@ struct bitcoin_txid;
struct preimage; struct preimage;
struct ripemd160; struct ripemd160;
/* Makes generate-wire.py work */
typedef char wirestring;
void derive_channel_id(struct channel_id *channel_id, void derive_channel_id(struct channel_id *channel_id,
struct bitcoin_txid *txid, u16 txout); 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_u8_array(u8 **pptr, const u8 *arr, size_t num);
void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx); 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); const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n);
u8 fromwire_u8(const u8 **cursor, size_t *max); 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_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); 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, struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx,
const u8 **cursor, size_t *max); const u8 **cursor, size_t *max);

Loading…
Cancel
Save