1 #include <ccan/cast/cast.h>
2 #include <common/decode_array.h>
3 #include <wire/peer_wire.h>
4 #include <zlib.h>
5
unzlib(const tal_t * ctx,const u8 * encoded,size_t len)6 static u8 *unzlib(const tal_t *ctx, const u8 *encoded, size_t len)
7 {
8 /* http://www.zlib.net/zlib_tech.html gives 1032:1 as worst-case,
9 * which is 67632120 bytes for us. But they're not encoding zeroes,
10 * and each scid must be unique. So 1MB is far more reasonable. */
11 unsigned long unclen = 1024*1024;
12 int zerr;
13 u8 *unc = tal_arr(ctx, u8, unclen);
14
15 zerr = uncompress(unc, &unclen, encoded, len);
16 if (zerr != Z_OK)
17 return tal_free(unc);
18
19 /* Truncate and return. */
20 tal_resize(&unc, unclen);
21 return unc;
22 }
23
decode_short_ids(const tal_t * ctx,const u8 * encoded)24 struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded)
25 {
26 struct short_channel_id *scids;
27 size_t max = tal_count(encoded);
28 enum arr_encode_types type;
29
30 /* BOLT #7:
31 *
32 * The receiver:
33 * - if the first byte of `encoded_short_ids` is not a known encoding
34 * type:
35 * - MAY fail the connection
36 * - if `encoded_short_ids` does not decode into a whole number of
37 * `short_channel_id`:
38 * - MAY fail the connection
39 */
40 type = fromwire_u8(&encoded, &max);
41 switch (type) {
42 case ARR_ZLIB:
43 encoded = unzlib(tmpctx, encoded, max);
44 if (!encoded)
45 return NULL;
46 max = tal_count(encoded);
47 /* fall thru */
48 case ARR_UNCOMPRESSED:
49 scids = tal_arr(ctx, struct short_channel_id, 0);
50 while (max) {
51 struct short_channel_id scid;
52 fromwire_short_channel_id(&encoded, &max, &scid);
53 tal_arr_expand(&scids, scid);
54 }
55
56 /* encoded is set to NULL if we ran over */
57 if (!encoded)
58 return tal_free(scids);
59 return scids;
60 }
61 return NULL;
62 }
63
decode_scid_query_flags(const tal_t * ctx,const struct tlv_query_short_channel_ids_tlvs_query_flags * qf)64 bigsize_t *decode_scid_query_flags(const tal_t *ctx,
65 const struct tlv_query_short_channel_ids_tlvs_query_flags *qf)
66 {
67 u8 *encoded = qf->encoded_query_flags;
68 size_t max = tal_count(encoded);
69 bigsize_t *flags;
70
71 /* BOLT #7:
72 *
73 * The receiver:
74 *...
75 * - if the incoming message includes `query_short_channel_ids_tlvs`:
76 * - if `encoding_type` is not a known encoding type:
77 * - MAY fail the connection
78 * - if `encoded_query_flags` does not decode to exactly one flag per
79 * `short_channel_id`:
80 * - MAY fail the connection.
81 */
82 switch (qf->encoding_type) {
83 case ARR_ZLIB:
84 encoded = unzlib(tmpctx, encoded, max);
85 if (!encoded)
86 return NULL;
87 max = tal_count(encoded);
88 /* fall thru */
89 case ARR_UNCOMPRESSED:
90 flags = tal_arr(ctx, bigsize_t, 0);
91 while (max)
92 tal_arr_expand(&flags,
93 fromwire_bigsize(cast_const2(const u8 **,
94 &encoded),
95 &max));
96
97 /* encoded is set to NULL if we ran over */
98 if (!encoded)
99 return tal_free(flags);
100 return flags;
101 }
102 return NULL;
103 }
104
105 struct channel_update_timestamps *
decode_channel_update_timestamps(const tal_t * ctx,const struct tlv_reply_channel_range_tlvs_timestamps_tlv * timestamps_tlv)106 decode_channel_update_timestamps(const tal_t *ctx,
107 const struct tlv_reply_channel_range_tlvs_timestamps_tlv *timestamps_tlv)
108 {
109 /* Note that our parser will set this to NULL if there are no elements */
110 u8 *encoded = timestamps_tlv->encoded_timestamps;
111 size_t max = tal_count(encoded);
112 struct channel_update_timestamps *ts;
113
114 /* FIXME: BOLT #7 should have a requirements like it does for
115 * query_short_channel_ids_tlvs! */
116 switch (timestamps_tlv->encoding_type) {
117 case ARR_ZLIB:
118 encoded = unzlib(tmpctx, encoded, max);
119 if (!encoded)
120 return NULL;
121 max = tal_count(encoded);
122 /* fall thru */
123 case ARR_UNCOMPRESSED:
124 ts = tal_arr(ctx, struct channel_update_timestamps, 0);
125 while (max) {
126 struct channel_update_timestamps t;
127 fromwire_channel_update_timestamps
128 (cast_const2(const u8 **, &encoded),
129 &max, &t);
130 /* Sets this to NULL if it fails */
131 if (!encoded)
132 return tal_free(ts);
133 tal_arr_expand(&ts, t);
134 }
135 return ts;
136 }
137 return NULL;
138 }
139