forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_wire.c
352 lines (342 loc) · 8.39 KB
/
peer_wire.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "config.h"
#include <ccan/mem/mem.h>
#include <wire/peer_wire.h>
static bool unknown_type(enum peer_wire t)
{
switch (t) {
case WIRE_WARNING:
case WIRE_INIT:
case WIRE_ERROR:
case WIRE_OPEN_CHANNEL:
case WIRE_ACCEPT_CHANNEL:
case WIRE_FUNDING_CREATED:
case WIRE_FUNDING_SIGNED:
case WIRE_CHANNEL_READY:
case WIRE_SHUTDOWN:
case WIRE_CLOSING_SIGNED:
case WIRE_UPDATE_ADD_HTLC:
case WIRE_UPDATE_FULFILL_HTLC:
case WIRE_UPDATE_FAIL_HTLC:
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
case WIRE_COMMITMENT_SIGNED:
case WIRE_REVOKE_AND_ACK:
case WIRE_UPDATE_FEE:
case WIRE_UPDATE_BLOCKHEIGHT:
case WIRE_CHANNEL_REESTABLISH:
case WIRE_ANNOUNCEMENT_SIGNATURES:
case WIRE_CHANNEL_ANNOUNCEMENT:
case WIRE_NODE_ANNOUNCEMENT:
case WIRE_CHANNEL_UPDATE:
case WIRE_PING:
case WIRE_PONG:
case WIRE_QUERY_SHORT_CHANNEL_IDS:
case WIRE_REPLY_SHORT_CHANNEL_IDS_END:
case WIRE_QUERY_CHANNEL_RANGE:
case WIRE_REPLY_CHANNEL_RANGE:
case WIRE_GOSSIP_TIMESTAMP_FILTER:
case WIRE_OBS2_ONION_MESSAGE:
case WIRE_ONION_MESSAGE:
case WIRE_TX_ADD_INPUT:
case WIRE_TX_REMOVE_INPUT:
case WIRE_TX_ADD_OUTPUT:
case WIRE_TX_REMOVE_OUTPUT:
case WIRE_TX_COMPLETE:
case WIRE_TX_SIGNATURES:
case WIRE_OPEN_CHANNEL2:
case WIRE_ACCEPT_CHANNEL2:
case WIRE_INIT_RBF:
case WIRE_ACK_RBF:
#if EXPERIMENTAL_FEATURES
case WIRE_STFU:
#endif
return false;
}
return true;
}
bool is_msg_for_gossipd(const u8 *cursor)
{
switch ((enum peer_wire)fromwire_peektype(cursor)) {
case WIRE_CHANNEL_ANNOUNCEMENT:
case WIRE_NODE_ANNOUNCEMENT:
case WIRE_CHANNEL_UPDATE:
case WIRE_QUERY_SHORT_CHANNEL_IDS:
case WIRE_REPLY_SHORT_CHANNEL_IDS_END:
case WIRE_QUERY_CHANNEL_RANGE:
case WIRE_REPLY_CHANNEL_RANGE:
return true;
case WIRE_WARNING:
case WIRE_INIT:
case WIRE_PING:
case WIRE_PONG:
case WIRE_ERROR:
case WIRE_OPEN_CHANNEL:
case WIRE_ACCEPT_CHANNEL:
case WIRE_FUNDING_CREATED:
case WIRE_FUNDING_SIGNED:
case WIRE_CHANNEL_READY:
case WIRE_SHUTDOWN:
case WIRE_CLOSING_SIGNED:
case WIRE_UPDATE_ADD_HTLC:
case WIRE_UPDATE_FULFILL_HTLC:
case WIRE_UPDATE_FAIL_HTLC:
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
case WIRE_COMMITMENT_SIGNED:
case WIRE_REVOKE_AND_ACK:
case WIRE_UPDATE_FEE:
case WIRE_UPDATE_BLOCKHEIGHT:
case WIRE_CHANNEL_REESTABLISH:
case WIRE_ANNOUNCEMENT_SIGNATURES:
case WIRE_GOSSIP_TIMESTAMP_FILTER:
case WIRE_TX_ADD_INPUT:
case WIRE_TX_REMOVE_INPUT:
case WIRE_TX_ADD_OUTPUT:
case WIRE_TX_REMOVE_OUTPUT:
case WIRE_TX_COMPLETE:
case WIRE_TX_SIGNATURES:
case WIRE_OPEN_CHANNEL2:
case WIRE_ACCEPT_CHANNEL2:
case WIRE_INIT_RBF:
case WIRE_ACK_RBF:
case WIRE_OBS2_ONION_MESSAGE:
case WIRE_ONION_MESSAGE:
#if EXPERIMENTAL_FEATURES
case WIRE_STFU:
#endif
break;
}
return false;
}
/* Return true if it's an unknown ODD message. cursor is a tal ptr. */
bool is_unknown_msg_discardable(const u8 *cursor)
{
enum peer_wire t = fromwire_peektype(cursor);
return unknown_type(t) && (t & 1);
}
/* Extract channel_id from various packets, return true if possible. */
bool extract_channel_id(const u8 *in_pkt, struct channel_id *channel_id)
{
const u8 *cursor = in_pkt;
size_t max = tal_bytelen(in_pkt);
enum peer_wire t;
t = fromwire_u16(&cursor, &max);
/* We carefully quote bolts here, in case anything changes! */
switch (t) {
/* These ones don't have a channel_id */
case WIRE_INIT:
case WIRE_PING:
case WIRE_PONG:
case WIRE_CHANNEL_ANNOUNCEMENT:
case WIRE_NODE_ANNOUNCEMENT:
case WIRE_CHANNEL_UPDATE:
case WIRE_QUERY_SHORT_CHANNEL_IDS:
case WIRE_REPLY_SHORT_CHANNEL_IDS_END:
case WIRE_QUERY_CHANNEL_RANGE:
case WIRE_REPLY_CHANNEL_RANGE:
case WIRE_GOSSIP_TIMESTAMP_FILTER:
case WIRE_OBS2_ONION_MESSAGE:
case WIRE_ONION_MESSAGE:
return false;
/* Special cases: */
case WIRE_ERROR:
/* BOLT #1:
* 1. type: 17 (`error`)
* 2. data:
* * [`channel_id`:`channel_id`]
*...
* The channel is referred to by `channel_id`, unless
* `channel_id` is 0
*/
/* fall thru */
case WIRE_WARNING:
/* BOLT-warning #1:
* 1. type: 1 (`warning`)
* 2. data:
* * [`channel_id`:`channel_id`]
*...
* The channel is referred to by `channel_id`, unless
* `channel_id` is 0
*/
if (!fromwire_channel_id(&cursor, &max, channel_id))
return false;
if (memeqzero(channel_id->id, sizeof(channel_id->id)))
return false;
return true;
case WIRE_OPEN_CHANNEL:
/* BOLT #2:
* 1. type: 32 (`open_channel`)
* 2. data:
* * [`chain_hash`:`chain_hash`]
* * [`32*byte`:`temporary_channel_id`]
*/
case WIRE_OPEN_CHANNEL2:
/* BOLT-dualfund #2:
* 1. type: 64 (`open_channel2`)
* 2. data:
* * [`chain_hash`:`chain_hash`]
* * [`channel_id`:`zerod_channel_id`]
*/
/* Skip over chain_hash */
fromwire_pad(&cursor, &max, sizeof(struct bitcoin_blkid));
/* These have them at the start */
case WIRE_TX_ADD_INPUT:
/* BOLT-dualfund #2:
* 1. type: 66 (`tx_add_input`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_TX_ADD_OUTPUT:
/* BOLT-dualfund #2:
* 1. type: 67 (`tx_add_output`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_TX_REMOVE_INPUT:
/* BOLT-dualfund #2:
* 1. type: 68 (`tx_remove_input`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_TX_REMOVE_OUTPUT:
/* BOLT-dualfund #2:
* 1. type: 69 (`tx_remove_output`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_TX_COMPLETE:
/* BOLT-dualfund #2:
* 1. type: 70 (`tx_complete`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_TX_SIGNATURES:
/* BOLT-dualfund #2:
* 1. type: 71 (`tx_signatures`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_ACCEPT_CHANNEL:
/* BOLT #2:
* 1. type: 33 (`accept_channel`)
* 2. data:
* * [`32*byte`:`temporary_channel_id`]
*/
case WIRE_FUNDING_CREATED:
/* BOLT #2:
* 1. type: 34 (`funding_created`)
* 2. data:
* * [`32*byte`:`temporary_channel_id`]
*/
case WIRE_FUNDING_SIGNED:
/* BOLT #2:
* 1. type: 35 (`funding_signed`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_CHANNEL_READY:
/* BOLT #2:
* 1. type: 36 (`channel_ready`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_ACCEPT_CHANNEL2:
/* BOLT-dualfund #2:
* 1. type: 65 (`accept_channel2`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_INIT_RBF:
/* BOLT-dualfund #2:
* 1. type: 72 (`init_rbf`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_ACK_RBF:
/* BOLT-dualfund #2:
* 1. type: 73 (`ack_rbf`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_SHUTDOWN:
/* BOLT #2:
* 1. type: 38 (`shutdown`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_CLOSING_SIGNED:
/* BOLT #2:
* 1. type: 39 (`closing_signed`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_UPDATE_ADD_HTLC:
/* BOLT #2:
* 1. type: 128 (`update_add_htlc`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_UPDATE_FULFILL_HTLC:
/* BOLT #2:
* 1. type: 130 (`update_fulfill_htlc`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_UPDATE_FAIL_HTLC:
/* BOLT #2:
* 1. type: 131 (`update_fail_htlc`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
/* BOLT #2:
* 1. type: 135 (`update_fail_malformed_htlc`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_COMMITMENT_SIGNED:
/* BOLT #2:
* 1. type: 132 (`commitment_signed`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_REVOKE_AND_ACK:
/* BOLT #2:
* 1. type: 133 (`revoke_and_ack`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_UPDATE_FEE:
/* BOLT #2:
* 1. type: 134 (`update_fee`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_UPDATE_BLOCKHEIGHT:
/* BOLT-liquidity-ads #2:
* 1. type: 137 (`update_blockheight`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_CHANNEL_REESTABLISH:
/* BOLT #2:
* 1. type: 136 (`channel_reestablish`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
case WIRE_ANNOUNCEMENT_SIGNATURES:
/* BOLT #7:
* 1. type: 259 (`announcement_signatures`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
#if EXPERIMENTAL_FEATURES
case WIRE_STFU:
/* BOLT-quiescent #2:
* 1. type: 2 (`stfu`)
* 2. data:
* * [`channel_id`:`channel_id`]
*/
#endif
return fromwire_channel_id(&cursor, &max, channel_id);
}
return false;
}