mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
decode_short_ids: move to common.
We want to use it in devtools/decodemsg. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
0bd82a8138
commit
9d3ce87700
5 changed files with 85 additions and 73 deletions
|
@ -12,6 +12,7 @@ COMMON_SRC_NOGEN := \
|
||||||
common/cryptomsg.c \
|
common/cryptomsg.c \
|
||||||
common/daemon.c \
|
common/daemon.c \
|
||||||
common/daemon_conn.c \
|
common/daemon_conn.c \
|
||||||
|
common/decode_short_channel_ids.c \
|
||||||
common/derive_basepoints.c \
|
common/derive_basepoints.c \
|
||||||
common/dev_disconnect.c \
|
common/dev_disconnect.c \
|
||||||
common/features.c \
|
common/features.c \
|
||||||
|
|
62
common/decode_short_channel_ids.c
Normal file
62
common/decode_short_channel_ids.c
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
#include <common/decode_short_channel_ids.h>
|
||||||
|
#include <common/utils.h>
|
||||||
|
#include <wire/wire.h>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
|
static u8 *unzlib(const tal_t *ctx, const u8 *encoded, size_t len)
|
||||||
|
{
|
||||||
|
/* http://www.zlib.net/zlib_tech.html gives 1032:1 as worst-case,
|
||||||
|
* which is 67632120 bytes for us. But they're not encoding zeroes,
|
||||||
|
* and each scid must be unique. So 1MB is far more reasonable. */
|
||||||
|
unsigned long unclen = 1024*1024;
|
||||||
|
int zerr;
|
||||||
|
u8 *unc = tal_arr(ctx, u8, unclen);
|
||||||
|
|
||||||
|
zerr = uncompress(unc, &unclen, encoded, len);
|
||||||
|
if (zerr != Z_OK)
|
||||||
|
return tal_free(unc);
|
||||||
|
|
||||||
|
/* Truncate and return. */
|
||||||
|
tal_resize(&unc, unclen);
|
||||||
|
return unc;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded)
|
||||||
|
{
|
||||||
|
struct short_channel_id *scids;
|
||||||
|
size_t max = tal_len(encoded), n;
|
||||||
|
enum scid_encode_types type;
|
||||||
|
|
||||||
|
/* BOLT #7:
|
||||||
|
*
|
||||||
|
* The receiver:
|
||||||
|
* - if the first byte of `encoded_short_ids` is not a known encoding
|
||||||
|
* type:
|
||||||
|
* - MAY fail the connection
|
||||||
|
* - if `encoded_short_ids` does not decode into a whole number of
|
||||||
|
* `short_channel_id`:
|
||||||
|
* - MAY fail the connection
|
||||||
|
*/
|
||||||
|
type = fromwire_u8(&encoded, &max);
|
||||||
|
switch (type) {
|
||||||
|
case SHORTIDS_ZLIB:
|
||||||
|
encoded = unzlib(tmpctx, encoded, max);
|
||||||
|
if (!encoded)
|
||||||
|
return NULL;
|
||||||
|
max = tal_len(encoded);
|
||||||
|
/* fall thru */
|
||||||
|
case SHORTIDS_UNCOMPRESSED:
|
||||||
|
n = 0;
|
||||||
|
scids = tal_arr(ctx, struct short_channel_id, n);
|
||||||
|
while (max) {
|
||||||
|
tal_resize(&scids, n+1);
|
||||||
|
fromwire_short_channel_id(&encoded, &max, &scids[n++]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* encoded is set to NULL if we ran over */
|
||||||
|
if (!encoded)
|
||||||
|
return tal_free(scids);
|
||||||
|
return scids;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
20
common/decode_short_channel_ids.h
Normal file
20
common/decode_short_channel_ids.h
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef LIGHTNING_COMMON_DECODE_SHORT_CHANNEL_IDS_H
|
||||||
|
#define LIGHTNING_COMMON_DECODE_SHORT_CHANNEL_IDS_H
|
||||||
|
#include "config.h"
|
||||||
|
#include <ccan/short_types/short_types.h>
|
||||||
|
#include <ccan/tal/tal.h>
|
||||||
|
|
||||||
|
/* BOLT #7:
|
||||||
|
*
|
||||||
|
* Encoding types:
|
||||||
|
* * `0`: uncompressed array of `short_channel_id` types, in ascending order.
|
||||||
|
* * `1`: array of `short_channel_id` types, in ascending order, compressed with
|
||||||
|
* zlib<sup>[1](#reference-1)</sup>
|
||||||
|
*/
|
||||||
|
enum scid_encode_types {
|
||||||
|
SHORTIDS_UNCOMPRESSED = 0,
|
||||||
|
SHORTIDS_ZLIB = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded);
|
||||||
|
#endif /* LIGHTNING_COMMON_DECODE_SHORT_CHANNEL_IDS_H */
|
|
@ -48,6 +48,7 @@ GOSSIPD_COMMON_OBJS := \
|
||||||
common/cryptomsg.o \
|
common/cryptomsg.o \
|
||||||
common/daemon.o \
|
common/daemon.o \
|
||||||
common/daemon_conn.o \
|
common/daemon_conn.o \
|
||||||
|
common/decode_short_channel_ids.o \
|
||||||
common/dev_disconnect.o \
|
common/dev_disconnect.o \
|
||||||
common/features.o \
|
common/features.o \
|
||||||
common/gen_status_wire.o \
|
common/gen_status_wire.o \
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <common/bech32_util.h>
|
#include <common/bech32_util.h>
|
||||||
#include <common/cryptomsg.h>
|
#include <common/cryptomsg.h>
|
||||||
#include <common/daemon_conn.h>
|
#include <common/daemon_conn.h>
|
||||||
|
#include <common/decode_short_channel_ids.h>
|
||||||
#include <common/features.h>
|
#include <common/features.h>
|
||||||
#include <common/ping.h>
|
#include <common/ping.h>
|
||||||
#include <common/pseudorand.h>
|
#include <common/pseudorand.h>
|
||||||
|
@ -65,18 +66,6 @@
|
||||||
#define INITIAL_WAIT_SECONDS 1
|
#define INITIAL_WAIT_SECONDS 1
|
||||||
#define MAX_WAIT_SECONDS 300
|
#define MAX_WAIT_SECONDS 300
|
||||||
|
|
||||||
/* BOLT #7:
|
|
||||||
*
|
|
||||||
* Encoding types:
|
|
||||||
* * `0`: uncompressed array of `short_channel_id` types, in ascending order.
|
|
||||||
* * `1`: array of `short_channel_id` types, in ascending order, compressed with
|
|
||||||
* zlib<sup>[1](#reference-1)</sup>
|
|
||||||
*/
|
|
||||||
enum scid_encode_types {
|
|
||||||
SHORTIDS_UNCOMPRESSED = 0,
|
|
||||||
SHORTIDS_ZLIB = 1
|
|
||||||
};
|
|
||||||
|
|
||||||
/* We put everything in this struct (redundantly) to pass it to timer cb */
|
/* We put everything in this struct (redundantly) to pass it to timer cb */
|
||||||
struct important_peerid {
|
struct important_peerid {
|
||||||
struct daemon *daemon;
|
struct daemon *daemon;
|
||||||
|
@ -880,67 +869,6 @@ static u8 *handle_gossip_msg(struct daemon *daemon, const u8 *msg,
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static u8 *unzlib(const tal_t *ctx, const u8 *encoded, size_t len)
|
|
||||||
{
|
|
||||||
/* http://www.zlib.net/zlib_tech.html gives 1032:1 as worst-case,
|
|
||||||
* which is 67632120 bytes for us. But they're not encoding zeroes,
|
|
||||||
* and each scid must be unique. So 1MB is far more reasonable. */
|
|
||||||
unsigned long unclen = 1024*1024;
|
|
||||||
int zerr;
|
|
||||||
u8 *unc = tal_arr(ctx, u8, unclen);
|
|
||||||
|
|
||||||
zerr = uncompress(unc, &unclen, encoded, len);
|
|
||||||
if (zerr != Z_OK) {
|
|
||||||
status_trace("unzlib: error %i", zerr);
|
|
||||||
return tal_free(unc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Truncate and return. */
|
|
||||||
tal_resize(&unc, unclen);
|
|
||||||
return unc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct short_channel_id *decode_short_ids(const tal_t *ctx,
|
|
||||||
const u8 *encoded)
|
|
||||||
{
|
|
||||||
struct short_channel_id *scids;
|
|
||||||
size_t max = tal_len(encoded), n;
|
|
||||||
enum scid_encode_types type;
|
|
||||||
|
|
||||||
/* BOLT #7:
|
|
||||||
*
|
|
||||||
* The receiver:
|
|
||||||
* - if the first byte of `encoded_short_ids` is not a known encoding
|
|
||||||
* type:
|
|
||||||
* - MAY fail the connection
|
|
||||||
* - if `encoded_short_ids` does not decode into a whole number of
|
|
||||||
* `short_channel_id`:
|
|
||||||
* - MAY fail the connection
|
|
||||||
*/
|
|
||||||
type = fromwire_u8(&encoded, &max);
|
|
||||||
switch (type) {
|
|
||||||
case SHORTIDS_ZLIB:
|
|
||||||
encoded = unzlib(tmpctx, encoded, max);
|
|
||||||
if (!encoded)
|
|
||||||
return NULL;
|
|
||||||
max = tal_len(encoded);
|
|
||||||
/* fall thru */
|
|
||||||
case SHORTIDS_UNCOMPRESSED:
|
|
||||||
n = 0;
|
|
||||||
scids = tal_arr(ctx, struct short_channel_id, n);
|
|
||||||
while (max) {
|
|
||||||
tal_resize(&scids, n+1);
|
|
||||||
fromwire_short_channel_id(&encoded, &max, &scids[n++]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* encoded is set to NULL if we ran over */
|
|
||||||
if (!encoded)
|
|
||||||
return tal_free(scids);
|
|
||||||
return scids;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_query_short_channel_ids(struct peer *peer, u8 *msg)
|
static void handle_query_short_channel_ids(struct peer *peer, u8 *msg)
|
||||||
{
|
{
|
||||||
struct routing_state *rstate = peer->daemon->rstate;
|
struct routing_state *rstate = peer->daemon->rstate;
|
||||||
|
|
Loading…
Add table
Reference in a new issue