You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
106 lines
2.7 KiB
5 years ago
|
#include <ccan/cast/cast.h>
|
||
5 years ago
|
#include <common/decode_array.h>
|
||
7 years ago
|
#include <common/utils.h>
|
||
5 years ago
|
#include <wire/gen_peer_wire.h>
|
||
7 years ago
|
#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;
|
||
6 years ago
|
size_t max = tal_count(encoded);
|
||
5 years ago
|
enum arr_encode_types type;
|
||
7 years ago
|
|
||
|
/* 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) {
|
||
5 years ago
|
case ARR_ZLIB:
|
||
7 years ago
|
encoded = unzlib(tmpctx, encoded, max);
|
||
|
if (!encoded)
|
||
|
return NULL;
|
||
7 years ago
|
max = tal_count(encoded);
|
||
7 years ago
|
/* fall thru */
|
||
5 years ago
|
case ARR_UNCOMPRESSED:
|
||
6 years ago
|
scids = tal_arr(ctx, struct short_channel_id, 0);
|
||
7 years ago
|
while (max) {
|
||
6 years ago
|
struct short_channel_id scid;
|
||
|
fromwire_short_channel_id(&encoded, &max, &scid);
|
||
|
tal_arr_expand(&scids, scid);
|
||
7 years ago
|
}
|
||
|
|
||
|
/* encoded is set to NULL if we ran over */
|
||
|
if (!encoded)
|
||
|
return tal_free(scids);
|
||
|
return scids;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
5 years ago
|
|
||
|
bigsize_t *decode_scid_query_flags(const tal_t *ctx,
|
||
|
const struct tlv_query_short_channel_ids_tlvs_query_flags *qf)
|
||
|
{
|
||
|
u8 *encoded = qf->encoded_query_flags;
|
||
|
size_t max = tal_count(encoded);
|
||
|
bigsize_t *flags;
|
||
|
|
||
5 years ago
|
/* BOLT #7:
|
||
5 years ago
|
*
|
||
|
* The receiver:
|
||
|
*...
|
||
|
* - if the incoming message includes `query_short_channel_ids_tlvs`:
|
||
|
* - if `encoding_type` is not a known encoding type:
|
||
|
* - MAY fail the connection
|
||
|
* - if `encoded_query_flags` does not decode to exactly one flag per
|
||
|
* `short_channel_id`:
|
||
|
* - MAY fail the connection.
|
||
|
*/
|
||
|
switch (qf->encoding_type) {
|
||
5 years ago
|
case ARR_ZLIB:
|
||
5 years ago
|
encoded = unzlib(tmpctx, encoded, max);
|
||
|
if (!encoded)
|
||
|
return NULL;
|
||
|
max = tal_count(encoded);
|
||
|
/* fall thru */
|
||
5 years ago
|
case ARR_UNCOMPRESSED:
|
||
5 years ago
|
flags = tal_arr(ctx, bigsize_t, 0);
|
||
|
while (max)
|
||
|
tal_arr_expand(&flags,
|
||
|
fromwire_bigsize(cast_const2(const u8 **,
|
||
|
&encoded),
|
||
|
&max));
|
||
|
|
||
|
/* encoded is set to NULL if we ran over */
|
||
|
if (!encoded)
|
||
|
return tal_free(flags);
|
||
|
return flags;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|