diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 26779fb0f..990507112 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -1049,25 +1049,16 @@ static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon, u8 *out; struct route_hop *hops; double fuzz; - u8 *rawseed; struct siphash_seed seed; - size_t seedbytes; - fromwire_gossip_getroute_request(tmpctx, msg, + fromwire_gossip_getroute_request(msg, &source, &destination, &msatoshi, &riskfactor, &final_cltv, - &fuzz, &rawseed); + &fuzz, &seed); status_trace("Trying to find a route from %s to %s for %d msatoshi", pubkey_to_hexstr(tmpctx, &source), pubkey_to_hexstr(tmpctx, &destination), msatoshi); - /* Initialize siphash */ - memset(&seed, 0, sizeof(seed)); - seedbytes = - (tal_len(rawseed) > sizeof(seed)) ? sizeof(seed) : - /*otherwise*/ tal_len(rawseed) ; - memcpy(&seed, rawseed, seedbytes); - hops = get_route(tmpctx, daemon->rstate, &source, &destination, msatoshi, 1, final_cltv, fuzz, &seed); diff --git a/gossipd/gossip_wire.csv b/gossipd/gossip_wire.csv index 469a041f2..53b455bfb 100644 --- a/gossipd/gossip_wire.csv +++ b/gossipd/gossip_wire.csv @@ -108,8 +108,7 @@ gossip_getroute_request,,msatoshi,u32 gossip_getroute_request,,riskfactor,u16 gossip_getroute_request,,final_cltv,u32 gossip_getroute_request,,fuzz,double -gossip_getroute_request,,seedlen,u16 -gossip_getroute_request,,seed,seedlen*u8 +gossip_getroute_request,,seed,struct siphash_seed gossip_getroute_reply,3106 gossip_getroute_reply,,num_hops,u16 diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index dba690752..479832a7d 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -313,7 +313,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok * be selected) at the cost of increasing the probability of * selecting the higher-fee paths. */ double fuzz = 75.0; - u8 *seed = tal_arrz(cmd, u8, sizeof(struct siphash_seed)); + struct siphash_seed seed; if (!json_get_params(cmd, buffer, params, "id", &idtok, @@ -373,13 +373,17 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok fuzz = fuzz / 100.0; if (seedtok) { - tal_resize(&seed, seedtok->end - seedtok->start); - memcpy(seed, buffer + seedtok->start, + if (seedtok->end - seedtok->start > sizeof(seed)) + command_fail(cmd, + "seed must be < %zu bytes", sizeof(seed)); + + memset(&seed, 0, sizeof(seed)); + memcpy(&seed, buffer + seedtok->start, seedtok->end - seedtok->start); } else - randombytes_buf(seed, tal_len(seed)); + randombytes_buf(&seed, sizeof(seed)); - u8 *req = towire_gossip_getroute_request(cmd, &source, &destination, msatoshi, riskfactor*1000, cltv, &fuzz, seed); + u8 *req = towire_gossip_getroute_request(cmd, &source, &destination, msatoshi, riskfactor*1000, cltv, &fuzz, &seed); subd_req(ld->gossip, ld->gossip, req, -1, 0, json_getroute_reply, cmd); command_still_pending(cmd); } diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index 87fb40467..dc1da928e 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -220,11 +220,11 @@ static void json_pay_getroute_reply(struct subd *gossip UNUSED, * false if resolved now. */ static bool json_pay_try(struct pay *pay) { - u8 *seed; u8 *req; struct command *cmd = pay->cmd; struct timeabs now = time_now(); struct json_result *data; + struct siphash_seed seed; /* If too late anyway, fail now. */ if (time_after(now, pay->expiry)) { @@ -243,8 +243,7 @@ static bool json_pay_try(struct pay *pay) pay->try_parent = tal(pay, char); /* Generate random seed */ - seed = tal_arr(pay->try_parent, u8, sizeof(struct siphash_seed)); - randombytes_buf(seed, tal_len(seed)); + randombytes_buf(&seed, sizeof(seed)); ++pay->getroute_tries; @@ -256,7 +255,7 @@ static bool json_pay_try(struct pay *pay) pay->riskfactor, pay->min_final_cltv_expiry, &pay->fuzz, - seed); + &seed); subd_req(pay->try_parent, cmd->ld->gossip, req, -1, 0, json_pay_getroute_reply, pay); return true; diff --git a/wire/fromwire.c b/wire/fromwire.c index ad3f50942..b54f26318 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -256,3 +257,9 @@ struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx, { return pull_bitcoin_tx(ctx, cursor, max); } + +void fromwire_siphash_seed(const u8 **cursor, size_t *max, + struct siphash_seed *seed) +{ + fromwire(cursor, max, seed, sizeof(*seed)); +} diff --git a/wire/towire.c b/wire/towire.c index 5e1162aee..94d9a930c 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -168,3 +169,8 @@ void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx) towire_u8_array(pptr, lin, tal_len(lin)); tal_free(tmpctx); } + +void towire_siphash_seed(u8 **pptr, const struct siphash_seed *seed) +{ + towire(pptr, seed, sizeof(*seed)); +} diff --git a/wire/wire.h b/wire/wire.h index 9769829c9..3de967e6e 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -20,6 +20,7 @@ struct bitcoin_blkid; struct bitcoin_txid; struct preimage; struct ripemd160; +struct siphash_seed; /* Makes generate-wire.py work */ typedef char wirestring; @@ -60,6 +61,7 @@ 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); +void towire_siphash_seed(u8 **cursor, const struct siphash_seed *seed); const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n); u8 fromwire_u8(const u8 **cursor, size_t *max); @@ -93,7 +95,8 @@ 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); +void fromwire_siphash_seed(const u8 **cursor, size_t *max, + struct siphash_seed *seed); #endif /* LIGHTNING_WIRE_WIRE_H */