1
1
#include "config.h"
2
+ #include "wire/tlvstream.h"
2
3
#include <ccan/array_size/array_size.h>
3
4
#include <ccan/cast/cast.h>
4
5
#include <ccan/crypto/shachain/shachain.h>
5
6
#include <common/htlc_wire.h>
6
7
#include <common/onionreply.h>
8
+ #include <stdio.h>
7
9
8
10
static struct failed_htlc * failed_htlc_dup (const tal_t * ctx ,
9
11
const struct failed_htlc * f TAKES )
@@ -33,7 +35,8 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
33
35
const u8 onion_routing_packet [TOTAL_PACKET_SIZE (ROUTING_INFO_SIZE )],
34
36
const struct pubkey * path_key TAKES ,
35
37
const struct preimage * preimage TAKES ,
36
- const struct failed_htlc * failed TAKES )
38
+ const struct failed_htlc * failed TAKES ,
39
+ const struct tlv_field * extra_tlvs TAKES )
37
40
{
38
41
struct existing_htlc * existing = tal (ctx , struct existing_htlc );
39
42
@@ -51,6 +54,17 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
51
54
existing -> failed = failed_htlc_dup (existing , failed );
52
55
else
53
56
existing -> failed = NULL ;
57
+ if (extra_tlvs ) {
58
+ existing -> extra_tlvs = tal_dup_talarr (existing , struct tlv_field , extra_tlvs );
59
+ for (size_t i = 0 ; i < tal_count (extra_tlvs ); i ++ ) {
60
+ /* We need to attach the value to the correct parent */
61
+ existing -> extra_tlvs [i ].value
62
+ = tal_dup_talarr (existing , u8 ,
63
+ existing -> extra_tlvs [i ].value );
64
+ }
65
+ } else {
66
+ existing -> extra_tlvs = NULL ;
67
+ }
54
68
55
69
return existing ;
56
70
}
@@ -70,6 +84,16 @@ void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
70
84
towire_pubkey (pptr , added -> path_key );
71
85
} else
72
86
towire_bool (pptr , false);
87
+ if (added -> extra_tlvs ) {
88
+ u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
89
+ towire_tlvstream_raw (& tmp_pptr , added -> extra_tlvs );
90
+
91
+ towire_bool (pptr , true);
92
+ towire_u16 (pptr , tal_bytelen (tmp_pptr ));
93
+ towire_u8_array (pptr , tmp_pptr ,
94
+ tal_bytelen (tmp_pptr ));
95
+ } else
96
+ towire_bool (pptr , false);
73
97
towire_bool (pptr , added -> fail_immediate );
74
98
}
75
99
@@ -97,6 +121,16 @@ void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing)
97
121
towire_pubkey (pptr , existing -> path_key );
98
122
} else
99
123
towire_bool (pptr , false);
124
+ if (existing -> extra_tlvs ) {
125
+ u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
126
+ towire_tlvstream_raw (& tmp_pptr , existing -> extra_tlvs );
127
+
128
+ towire_bool (pptr , true);
129
+ towire_u16 (pptr , tal_bytelen (tmp_pptr ));
130
+ towire_u8_array (pptr , tmp_pptr ,
131
+ tal_bytelen (tmp_pptr ));
132
+ } else
133
+ towire_bool (pptr , false);
100
134
}
101
135
102
136
void towire_fulfilled_htlc (u8 * * pptr , const struct fulfilled_htlc * fulfilled )
@@ -163,6 +197,20 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
163
197
fromwire_pubkey (cursor , max , added -> path_key );
164
198
} else
165
199
added -> path_key = NULL ;
200
+ if (fromwire_bool (cursor , max )) {
201
+ size_t tlv_len = fromwire_u16 (cursor , max );
202
+ /* NOTE: We might consider to be more strict and only allow for
203
+ * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
204
+ * record. */
205
+ const u64 * allowed = cast_const (u64 * , FROMWIRE_TLV_ANY_TYPE );
206
+ added -> extra_tlvs = tal_arr (added , struct tlv_field , 0 );
207
+ if (!fromwire_tlv (cursor , & tlv_len , NULL , 0 , added ,
208
+ & added -> extra_tlvs , allowed , NULL , NULL )) {
209
+ tal_free (added -> extra_tlvs );
210
+ added -> extra_tlvs = NULL ;
211
+ }
212
+ } else
213
+ added -> extra_tlvs = NULL ;
166
214
added -> fail_immediate = fromwire_bool (cursor , max );
167
215
}
168
216
@@ -192,6 +240,20 @@ struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
192
240
fromwire_pubkey (cursor , max , existing -> path_key );
193
241
} else
194
242
existing -> path_key = NULL ;
243
+ if (fromwire_bool (cursor , max )) {
244
+ size_t tlv_len = fromwire_u16 (cursor , max );
245
+ /* NOTE: We might consider to be more strict and only allow for
246
+ * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
247
+ * record. */
248
+ const u64 * allowed = cast_const (u64 * , FROMWIRE_TLV_ANY_TYPE );
249
+ existing -> extra_tlvs = tal_arr (existing , struct tlv_field , 0 );
250
+ if (!fromwire_tlv (cursor , & tlv_len , NULL , 0 , existing ,
251
+ & existing -> extra_tlvs , allowed , NULL , NULL )) {
252
+ tal_free (existing -> extra_tlvs );
253
+ existing -> extra_tlvs = NULL ;
254
+ }
255
+ } else
256
+ existing -> extra_tlvs = NULL ;
195
257
return existing ;
196
258
}
197
259
0 commit comments