Browse Source

wire: add towire_int() and use it in connectd

Add towire_int() and fromwire_int() functions to "(de)serialize"
"int". This will only work as long as both the caller of towire_int()
and the caller of fromwire_int() use the same in-memory representation
of signed integers and have the same sizeof(int).

Changelog-None
travis-debug
Vasil Dimov 5 years ago
committed by Christian Decker
parent
commit
fb7c006187
  1. 2
      connectd/connect_wire.csv
  2. 4
      connectd/connectd.c
  3. 8
      lightningd/connect_control.c
  4. 2
      tools/generate-wire.py
  5. 9
      wire/fromwire.c
  6. 5
      wire/towire.c
  7. 2
      wire/wire.h

2
connectd/connect_wire.csv

@ -44,7 +44,7 @@ msgdata,connectctl_connect_to_peer,addrhint,?wireaddr_internal,
# Connectd->master: connect failed.
msgtype,connectctl_connect_failed,2020
msgdata,connectctl_connect_failed,id,node_id,
msgdata,connectctl_connect_failed,failcode,u32,
msgdata,connectctl_connect_failed,failcode,int,
msgdata,connectctl_connect_failed,failreason,wirestring,
msgdata,connectctl_connect_failed,seconds_to_delay,u32,
msgdata,connectctl_connect_failed,addrhint,?wireaddr_internal,

Can't render this file because it has a wrong number of fields in line 6.

4
connectd/connectd.c

@ -566,7 +566,7 @@ static void connect_failed(struct daemon *daemon,
const struct node_id *id,
u32 seconds_waited,
const struct wireaddr_internal *addrhint,
u32 errcode,
int errcode,
const char *errfmt, ...)
PRINTF_FMT(6,7);
@ -574,7 +574,7 @@ static void connect_failed(struct daemon *daemon,
const struct node_id *id,
u32 seconds_waited,
const struct wireaddr_internal *addrhint,
u32 errcode,
int errcode,
const char *errfmt, ...)
{
u8 *msg;

8
lightningd/connect_control.c

@ -16,7 +16,6 @@
#include <errno.h>
#include <hsmd/capabilities.h>
#include <hsmd/gen_hsm_wire.h>
#include <inttypes.h>
#include <lightningd/channel.h>
#include <lightningd/connect_control.h>
#include <lightningd/hsm_control.h>
@ -232,7 +231,7 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay,
static void connect_failed(struct lightningd *ld, const u8 *msg)
{
struct node_id id;
u32 errcode;
int errcode;
char *errmsg;
struct connect *c;
u32 seconds_to_delay;
@ -240,15 +239,14 @@ static void connect_failed(struct lightningd *ld, const u8 *msg)
struct channel *channel;
if (!fromwire_connectctl_connect_failed(tmpctx, msg, &id, &errcode, &errmsg,
&seconds_to_delay, &addrhint) ||
errcode > INT_MAX)
&seconds_to_delay, &addrhint))
fatal("Connect gave bad CONNECTCTL_CONNECT_FAILED message %s",
tal_hex(msg, msg));
/* We can have multiple connect commands: fail them all */
while ((c = find_connect(ld, &id)) != NULL) {
/* They delete themselves from list */
was_pending(command_fail(c->cmd, (int)errcode, "%s", errmsg));
was_pending(command_fail(c->cmd, errcode, "%s", errmsg));
}
/* If we have an active channel, then reconnect. */

2
tools/generate-wire.py

@ -185,6 +185,7 @@ class Type(FieldSet):
'bool',
'amount_sat',
'amount_msat',
'int',
'bigsize',
'varint'
]
@ -199,6 +200,7 @@ class Type(FieldSet):
'secp256k1_ecdsa_recoverable_signature',
'wirestring',
'double',
'int',
'bigsize',
'varint',
]

9
wire/fromwire.c

@ -165,6 +165,15 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
return ret;
}
int fromwire_int(const u8 **cursor, size_t *max)
{
int ret;
if (!fromwire(cursor, max, &ret, sizeof(ret)))
return 0;
return ret;
}
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max)
{
bigsize_t v;

5
wire/towire.c

@ -87,6 +87,11 @@ void towire_bool(u8 **pptr, bool v)
towire(pptr, &val, sizeof(val));
}
void towire_int(u8 **pptr, int v)
{
towire(pptr, &v, sizeof(v));
}
void towire_bigsize(u8 **pptr, const bigsize_t val)
{
u8 buf[BIGSIZE_MAX_LEN];

2
wire/wire.h

@ -79,6 +79,7 @@ void towire_tu64(u8 **pptr, u64 v);
void towire_double(u8 **pptr, const double *v);
void towire_pad(u8 **pptr, size_t num);
void towire_bool(u8 **pptr, bool v);
void towire_int(u8 **pptr, int v);
void towire_bigsize(u8 **pptr, const bigsize_t val);
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
@ -101,6 +102,7 @@ u32 fromwire_tu32(const u8 **cursor, size_t *max);
u64 fromwire_tu64(const u8 **cursor, size_t *max);
void fromwire_double(const u8 **cursor, size_t *max, double *v);
bool fromwire_bool(const u8 **cursor, size_t *max);
int fromwire_int(const u8 **cursor, size_t *max);
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max);
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);

Loading…
Cancel
Save