-
Notifications
You must be signed in to change notification settings - Fork 70
/
packet.rs
1073 lines (964 loc) · 39.2 KB
/
packet.rs
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::crypto::{decrypt, encrypt};
use byteorder::ReadBytesExt;
use bytes::{BufMut, BytesMut};
use interledger_packet::{
oer::{BufOerExt, MutBufOerExt},
Address, PacketType as IlpPacketType, ParseError,
};
#[cfg(test)]
use once_cell::sync::Lazy;
use std::{convert::TryFrom, fmt, str, u64};
use tracing::warn;
/// The Stream Protocol's version
const STREAM_VERSION: u8 = 1;
/// Builder for [Stream Packets](https://interledger.org/rfcs/0029-stream/#52-stream-packet)
pub struct StreamPacketBuilder<'a> {
/// The stream packet's sequence number
pub sequence: u64,
/// The [ILP Packet Type](../interledger_packet/enum.PacketType.html)
pub ilp_packet_type: IlpPacketType,
/// Destination amount of the ILP Prepare, used for enforcing minimum exchange rates and congestion control.
/// Within an ILP Prepare, represents the minimum amount the recipient needs to receive in order to fulfill the packet.
/// Within an ILP Fulfill or ILP Reject, represents the amount received by the recipient.
pub prepare_amount: u64,
/// The stream frames
pub frames: &'a [Frame<'a>],
}
impl<'a> StreamPacketBuilder<'a> {
/// Serializes the builder into a Stream Packet
pub fn build(&self) -> StreamPacket {
let mut buffer_unencrypted = Vec::with_capacity(26);
buffer_unencrypted.put_u8(STREAM_VERSION);
buffer_unencrypted.put_u8(self.ilp_packet_type as u8);
buffer_unencrypted.put_var_uint(self.sequence);
buffer_unencrypted.put_var_uint(self.prepare_amount);
buffer_unencrypted.put_var_uint(self.frames.len() as u64);
let frames_offset = buffer_unencrypted.len();
for frame in self.frames {
let mut contents = Vec::new();
match frame {
Frame::ConnectionClose(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionClose as u8);
frame.put_contents(&mut contents);
}
Frame::ConnectionNewAddress(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionNewAddress as u8);
frame.put_contents(&mut contents);
}
Frame::ConnectionAssetDetails(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionAssetDetails as u8);
frame.put_contents(&mut contents);
}
Frame::ConnectionMaxData(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionMaxData as u8);
frame.put_contents(&mut contents);
}
Frame::ConnectionDataBlocked(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionDataBlocked as u8);
frame.put_contents(&mut contents);
}
Frame::ConnectionMaxStreamId(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionMaxStreamId as u8);
frame.put_contents(&mut contents);
}
Frame::ConnectionStreamIdBlocked(ref frame) => {
buffer_unencrypted.put_u8(FrameType::ConnectionStreamIdBlocked as u8);
frame.put_contents(&mut contents);
}
Frame::StreamClose(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamClose as u8);
frame.put_contents(&mut contents);
}
Frame::StreamMoney(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamMoney as u8);
frame.put_contents(&mut contents);
}
Frame::StreamMaxMoney(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamMaxMoney as u8);
frame.put_contents(&mut contents);
}
Frame::StreamMoneyBlocked(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamMoneyBlocked as u8);
frame.put_contents(&mut contents);
}
Frame::StreamData(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamData as u8);
frame.put_contents(&mut contents);
}
Frame::StreamMaxData(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamMaxData as u8);
frame.put_contents(&mut contents);
}
Frame::StreamDataBlocked(ref frame) => {
buffer_unencrypted.put_u8(FrameType::StreamDataBlocked as u8);
frame.put_contents(&mut contents);
}
Frame::Unknown => continue,
}
buffer_unencrypted.put_var_octet_string(&*contents);
}
StreamPacket {
buffer_unencrypted: BytesMut::from(&buffer_unencrypted[..]),
sequence: self.sequence,
ilp_packet_type: self.ilp_packet_type,
prepare_amount: self.prepare_amount,
frames_offset,
}
}
}
/// A Stream Packet as specified in its [ASN.1 definition](https://interledger.org/rfcs/asn1/Stream.asn)
#[derive(PartialEq, Clone)]
pub struct StreamPacket {
/// The cleartext serialized packet
pub(crate) buffer_unencrypted: BytesMut,
/// The packet's sequence number
sequence: u64,
/// The [ILP Packet Type](../interledger_packet/enum.PacketType.html)
ilp_packet_type: IlpPacketType,
/// Destination amount of the ILP Prepare, used for enforcing minimum exchange rates and congestion control.
/// Within an ILP Prepare, represents the minimum amount the recipient needs to receive in order to fulfill the packet.
/// Within an ILP Fulfill or ILP Reject, represents the amount received by the recipient.
prepare_amount: u64,
/// The offset after which frames can be found inside the `buffer_unencrypted` field
frames_offset: usize,
}
impl StreamPacket {
/// Constructs a [Stream Packet](./struct.StreamPacket.html) from an encrypted buffer
/// and a shared secret
///
/// # Errors
/// 1. If the version of Stream Protocol doesn't match the hardcoded [stream version](constant.STREAM_VERSION.html)
/// 1. If the decryption fails
/// 1. If the decrypted bytes cannot be parsed to an unencrypted [Stream Packet](./struct.StreamPacket.html)
pub fn from_encrypted(shared_secret: &[u8], ciphertext: BytesMut) -> Result<Self, ParseError> {
// TODO handle decryption failure
let decrypted = decrypt(shared_secret, ciphertext)
.map_err(|_| ParseError::InvalidPacket(String::from("Unable to decrypt packet")))?;
StreamPacket::from_bytes_unencrypted(decrypted)
}
#[cfg(any(fuzzing, test))]
pub fn from_decrypted(data: BytesMut) -> Result<Self, ParseError> {
Self::from_bytes_unencrypted(data)
}
/// Constructs a [Stream Packet](./struct.StreamPacket.html) from a buffer
///
/// # Errors
/// 1. If the version of Stream Protocol doesn't match the hardcoded [stream version](constant.STREAM_VERSION.html)
/// 1. If the decrypted bytes cannot be parsed to an unencrypted [Stream Packet](./struct.StreamPacket.html)
fn from_bytes_unencrypted(buffer_unencrypted: BytesMut) -> Result<Self, ParseError> {
// TODO don't copy the whole packet again
let mut reader = &buffer_unencrypted[..];
let version = reader.read_u8()?;
if version != STREAM_VERSION {
return Err(ParseError::InvalidPacket(format!(
"Unsupported STREAM version: {}",
version
)));
}
let ilp_packet_type = IlpPacketType::try_from(reader.read_u8()?)?;
let sequence = reader.read_var_uint()?;
let prepare_amount = reader.read_var_uint()?;
// TODO save num_frames?
let num_frames = reader.read_var_uint()?;
let frames_offset = buffer_unencrypted.len() - reader.len();
// Try reading through all the frames to make sure they can be parsed correctly
if num_frames
== (FrameIterator {
buffer: &buffer_unencrypted[frames_offset..],
})
.count() as u64
{
Ok(StreamPacket {
buffer_unencrypted,
sequence,
ilp_packet_type,
prepare_amount,
frames_offset,
})
} else {
Err(ParseError::InvalidPacket(
"Incorrect number of frames or unable to parse all frames".to_string(),
))
}
}
/// Consumes the packet and a shared secret and returns a serialized encrypted
/// Stream packet
pub fn into_encrypted(self, shared_secret: &[u8]) -> BytesMut {
encrypt(shared_secret, self.buffer_unencrypted)
}
/// The packet's sequence number
pub fn sequence(&self) -> u64 {
self.sequence
}
/// The packet's [type](../interledger_packet/enum.PacketType.html)
pub fn ilp_packet_type(&self) -> IlpPacketType {
self.ilp_packet_type
}
/// Destination amount of the ILP Prepare, used for enforcing minimum exchange rates and congestion control.
pub fn prepare_amount(&self) -> u64 {
self.prepare_amount
}
/// Returns a [FrameIterator](./struct.FrameIterator.html) over the packet's [frames](./enum.Frame.html)
pub fn frames(&self) -> FrameIterator {
FrameIterator {
buffer: &self.buffer_unencrypted[self.frames_offset..],
}
}
}
impl fmt::Debug for StreamPacket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"StreamPacket {{ sequence: {}, ilp_packet_type: {:?}, prepare_amount: {}, frames: {:?} }}",
self.sequence,
self.ilp_packet_type,
self.prepare_amount,
self.frames()
)
}
}
/// Iterator over a serialized Frame to support zero-copy deserialization
pub struct FrameIterator<'a> {
buffer: &'a [u8],
}
impl<'a> FrameIterator<'a> {
/// Reads a u8 from the iterator's buffer, and depending on the type it returns
/// a [`Frame`](./enum.Frame.html)
fn try_read_next_frame(&mut self) -> Result<Frame<'a>, ParseError> {
let frame_type = self.buffer.read_u8()?;
let contents: &'a [u8] = self.buffer.read_var_octet_string()?;
let frame: Frame<'a> = match FrameType::from(frame_type) {
FrameType::ConnectionClose => {
Frame::ConnectionClose(ConnectionCloseFrame::read_contents(&contents)?)
}
FrameType::ConnectionNewAddress => {
Frame::ConnectionNewAddress(ConnectionNewAddressFrame::read_contents(&contents)?)
}
FrameType::ConnectionAssetDetails => Frame::ConnectionAssetDetails(
ConnectionAssetDetailsFrame::read_contents(&contents)?,
),
FrameType::ConnectionMaxData => {
Frame::ConnectionMaxData(ConnectionMaxDataFrame::read_contents(&contents)?)
}
FrameType::ConnectionDataBlocked => {
Frame::ConnectionDataBlocked(ConnectionDataBlockedFrame::read_contents(&contents)?)
}
FrameType::ConnectionMaxStreamId => {
Frame::ConnectionMaxStreamId(ConnectionMaxStreamIdFrame::read_contents(&contents)?)
}
FrameType::ConnectionStreamIdBlocked => Frame::ConnectionStreamIdBlocked(
ConnectionStreamIdBlockedFrame::read_contents(&contents)?,
),
FrameType::StreamClose => {
Frame::StreamClose(StreamCloseFrame::read_contents(&contents)?)
}
FrameType::StreamMoney => {
Frame::StreamMoney(StreamMoneyFrame::read_contents(&contents)?)
}
FrameType::StreamMaxMoney => {
Frame::StreamMaxMoney(StreamMaxMoneyFrame::read_contents(&contents)?)
}
FrameType::StreamMoneyBlocked => {
Frame::StreamMoneyBlocked(StreamMoneyBlockedFrame::read_contents(&contents)?)
}
FrameType::StreamData => Frame::StreamData(StreamDataFrame::read_contents(&contents)?),
FrameType::StreamMaxData => {
Frame::StreamMaxData(StreamMaxDataFrame::read_contents(&contents)?)
}
FrameType::StreamDataBlocked => {
Frame::StreamDataBlocked(StreamDataBlockedFrame::read_contents(&contents)?)
}
FrameType::Unknown => {
warn!(
"Ignoring unknown frame of type {}: {:x?}",
frame_type, contents,
);
Frame::Unknown
}
};
Ok(frame)
}
}
impl<'a> Iterator for FrameIterator<'a> {
type Item = Frame<'a>;
fn next(&mut self) -> Option<Self::Item> {
while !self.buffer.is_empty() {
// TODO don't ignore errors if the packet is just invalid
match self.try_read_next_frame() {
Ok(frame) => return Some(frame),
Err(err) => warn!("Error reading STREAM frame: {:?}", err),
}
}
None
}
}
impl<'a> fmt::Debug for FrameIterator<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[ ")?;
let mut iter = FrameIterator {
buffer: self.buffer,
};
if let Some(next) = iter.next() {
write!(f, "{:?}", next)?;
}
for frame in iter {
write!(f, ", {:?}", frame)?;
}
write!(f, " ]")
}
}
/// Enum around the different Stream Frame types
#[derive(PartialEq, Clone)]
pub enum Frame<'a> {
ConnectionClose(ConnectionCloseFrame<'a>),
ConnectionNewAddress(ConnectionNewAddressFrame),
ConnectionAssetDetails(ConnectionAssetDetailsFrame<'a>),
ConnectionMaxData(ConnectionMaxDataFrame),
ConnectionDataBlocked(ConnectionDataBlockedFrame),
ConnectionMaxStreamId(ConnectionMaxStreamIdFrame),
ConnectionStreamIdBlocked(ConnectionStreamIdBlockedFrame),
StreamClose(StreamCloseFrame<'a>),
StreamMoney(StreamMoneyFrame),
StreamMaxMoney(StreamMaxMoneyFrame),
StreamMoneyBlocked(StreamMoneyBlockedFrame),
StreamData(StreamDataFrame<'a>),
StreamMaxData(StreamMaxDataFrame),
StreamDataBlocked(StreamDataBlockedFrame),
Unknown,
}
impl<'a> fmt::Debug for Frame<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Frame::ConnectionClose(frame) => write!(f, "{:?}", frame),
Frame::ConnectionNewAddress(frame) => write!(f, "{:?}", frame),
Frame::ConnectionAssetDetails(frame) => write!(f, "{:?}", frame),
Frame::ConnectionMaxData(frame) => write!(f, "{:?}", frame),
Frame::ConnectionDataBlocked(frame) => write!(f, "{:?}", frame),
Frame::ConnectionMaxStreamId(frame) => write!(f, "{:?}", frame),
Frame::ConnectionStreamIdBlocked(frame) => write!(f, "{:?}", frame),
Frame::StreamClose(frame) => write!(f, "{:?}", frame),
Frame::StreamMoney(frame) => write!(f, "{:?}", frame),
Frame::StreamMaxMoney(frame) => write!(f, "{:?}", frame),
Frame::StreamMoneyBlocked(frame) => write!(f, "{:?}", frame),
Frame::StreamData(frame) => write!(f, "{:?}", frame),
Frame::StreamMaxData(frame) => write!(f, "{:?}", frame),
Frame::StreamDataBlocked(frame) => write!(f, "{:?}", frame),
Frame::Unknown => write!(f, "UnknownFrame"),
}
}
}
/// The Stream Frame types [as defined in the RFC](https://interledger.org/rfcs/0029-stream/#53-frames)
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum FrameType {
ConnectionClose = 0x01,
ConnectionNewAddress = 0x02,
ConnectionMaxData = 0x03,
ConnectionDataBlocked = 0x04,
ConnectionMaxStreamId = 0x05,
ConnectionStreamIdBlocked = 0x06,
ConnectionAssetDetails = 0x07,
StreamClose = 0x10,
StreamMoney = 0x11,
StreamMaxMoney = 0x12,
StreamMoneyBlocked = 0x13,
StreamData = 0x14,
StreamMaxData = 0x15,
StreamDataBlocked = 0x16,
Unknown,
}
impl From<u8> for FrameType {
fn from(num: u8) -> Self {
match num {
0x01 => FrameType::ConnectionClose,
0x02 => FrameType::ConnectionNewAddress,
0x03 => FrameType::ConnectionMaxData,
0x04 => FrameType::ConnectionDataBlocked,
0x05 => FrameType::ConnectionMaxStreamId,
0x06 => FrameType::ConnectionStreamIdBlocked,
0x07 => FrameType::ConnectionAssetDetails,
0x10 => FrameType::StreamClose,
0x11 => FrameType::StreamMoney,
0x12 => FrameType::StreamMaxMoney,
0x13 => FrameType::StreamMoneyBlocked,
0x14 => FrameType::StreamData,
0x15 => FrameType::StreamMaxData,
0x16 => FrameType::StreamDataBlocked,
_ => FrameType::Unknown,
}
}
}
/// The STREAM Error Codes [as defined in the RFC](https://interledger.org/rfcs/0029-stream/#54-error-codes)
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum ErrorCode {
NoError = 0x01,
InternalError = 0x02,
EndpointBusy = 0x03,
FlowControlError = 0x04,
StreamIdError = 0x05,
StreamStateError = 0x06,
FrameFormatError = 0x07,
ProtocolViolation = 0x08,
ApplicationError = 0x09,
Unknown,
}
impl From<u8> for ErrorCode {
fn from(num: u8) -> Self {
match num {
0x01 => ErrorCode::NoError,
0x02 => ErrorCode::InternalError,
0x03 => ErrorCode::EndpointBusy,
0x04 => ErrorCode::FlowControlError,
0x05 => ErrorCode::StreamIdError,
0x06 => ErrorCode::StreamStateError,
0x07 => ErrorCode::FrameFormatError,
0x08 => ErrorCode::ProtocolViolation,
0x09 => ErrorCode::ApplicationError,
_ => ErrorCode::Unknown,
}
}
}
/// Helper trait for having a common interface to read/write on Frames
pub trait SerializableFrame<'a>: Sized {
fn put_contents(&self, buf: &mut impl MutBufOerExt);
fn read_contents(reader: &'a [u8]) -> Result<Self, ParseError>;
}
/// Frame after which a connection must be closed.
/// If implementations allow half-open connections, an endpoint may continue sending packets after receiving a ConnectionClose frame.
#[derive(Debug, PartialEq, Clone)]
pub struct ConnectionCloseFrame<'a> {
/// Machine-readable [Error Code](./enum.ErrorCode.html) indicating why the connection was closed.
pub code: ErrorCode,
/// Human-readable string intended to give more information helpful for debugging purposes.
pub message: &'a str,
}
impl<'a> SerializableFrame<'a> for ConnectionCloseFrame<'a> {
fn read_contents(mut reader: &'a [u8]) -> Result<Self, ParseError> {
let code = ErrorCode::from(reader.read_u8()?);
let message_bytes = reader.read_var_octet_string()?;
let message = str::from_utf8(message_bytes)?;
Ok(ConnectionCloseFrame { code, message })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_u8(self.code as u8);
buf.put_var_octet_string(self.message.as_bytes());
}
}
/// Frame which contains the sender of the Stream payment
#[derive(PartialEq, Clone)]
pub struct ConnectionNewAddressFrame {
/// New ILP address of the endpoint that sent the frame.
pub source_account: Address,
}
impl<'a> SerializableFrame<'a> for ConnectionNewAddressFrame {
fn read_contents(mut reader: &'a [u8]) -> Result<Self, ParseError> {
let source_account = reader.read_var_octet_string()?;
let source_account = Address::try_from(source_account)?;
Ok(ConnectionNewAddressFrame { source_account })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
let data: &[u8] = self.source_account.as_ref();
buf.put_var_octet_string(data);
}
}
impl<'a> fmt::Debug for ConnectionNewAddressFrame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"ConnectionNewAddressFrame {{ source_account: {} }}",
self.source_account,
)
}
}
/// The assets being transported in this Stream payment
/// Asset details exposed by this frame MUST NOT change during the lifetime of a Connection.
#[derive(Debug, PartialEq, Clone)]
pub struct ConnectionAssetDetailsFrame<'a> {
/// Asset code of endpoint that sent the frame.
pub source_asset_code: &'a str,
/// Asset scale of endpoint that sent the frame.
pub source_asset_scale: u8,
}
impl<'a> SerializableFrame<'a> for ConnectionAssetDetailsFrame<'a> {
fn read_contents(mut reader: &'a [u8]) -> Result<Self, ParseError> {
let source_asset_code = str::from_utf8(reader.read_var_octet_string()?)?;
let source_asset_scale = reader.read_u8()?;
Ok(ConnectionAssetDetailsFrame {
source_asset_scale,
source_asset_code,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_octet_string(self.source_asset_code.as_bytes());
buf.put_u8(self.source_asset_scale);
}
}
/// Endpoints MUST NOT exceed the total number of bytes the other endpoint is willing to accept.
#[derive(Debug, PartialEq, Clone)]
pub struct ConnectionMaxDataFrame {
/// The total number of bytes the endpoint is willing to receive on this connection.
pub max_offset: u64,
}
impl<'a> SerializableFrame<'a> for ConnectionMaxDataFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let max_offset = reader.read_var_uint()?;
Ok(ConnectionMaxDataFrame { max_offset })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.max_offset);
}
}
/// Frame specifying the amount of data which is going to be sent
#[derive(Debug, PartialEq, Clone)]
pub struct ConnectionDataBlockedFrame {
/// The total number of bytes the endpoint wants to send.
pub max_offset: u64,
}
impl<'a> SerializableFrame<'a> for ConnectionDataBlockedFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let max_offset = reader.read_var_uint()?;
Ok(ConnectionDataBlockedFrame { max_offset })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.max_offset);
}
}
/// Frame specifying the maximum stream ID the endpoint is willing to accept.
#[derive(Debug, PartialEq, Clone)]
pub struct ConnectionMaxStreamIdFrame {
/// The maximum stream ID the endpoint is willing to accept.
pub max_stream_id: u64,
}
impl<'a> SerializableFrame<'a> for ConnectionMaxStreamIdFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let max_stream_id = reader.read_var_uint()?;
Ok(ConnectionMaxStreamIdFrame { max_stream_id })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.max_stream_id);
}
}
/// Frame specifying the maximum stream ID the endpoint wishes to open.
#[derive(Debug, PartialEq, Clone)]
pub struct ConnectionStreamIdBlockedFrame {
/// The maximum stream ID the endpoint wishes to open.
pub max_stream_id: u64,
}
impl<'a> SerializableFrame<'a> for ConnectionStreamIdBlockedFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let max_stream_id = reader.read_var_uint()?;
Ok(ConnectionStreamIdBlockedFrame { max_stream_id })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.max_stream_id);
}
}
/// Endpoints MUST close the stream after receiving this stream immediately.
/// If implementations allow half-open streams, an endpoint MAY continue sending
/// money or data for this stream after receiving a StreamClose frame.
#[derive(Debug, PartialEq, Clone)]
pub struct StreamCloseFrame<'a> {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// Machine-readable [Error Code](./enum.ErrorCode.html) indicating why the connection was closed.
pub code: ErrorCode,
/// Human-readable string intended to give more information helpful for debugging purposes.
pub message: &'a str,
}
impl<'a> SerializableFrame<'a> for StreamCloseFrame<'a> {
fn read_contents(mut reader: &'a [u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let code = ErrorCode::from(reader.read_u8()?);
let message_bytes = reader.read_var_octet_string()?;
let message = str::from_utf8(message_bytes)?;
Ok(StreamCloseFrame {
stream_id,
code,
message,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_u8(self.code as u8);
buf.put_var_octet_string(self.message.as_bytes());
}
}
/// Frame specifying the amount of money that should go to each stream
///
/// The amount of money that should go to each stream is calculated by
/// dividing the number of shares for the given stream by the total number
/// of shares in all of the StreamMoney frames in the packet.
///
/// For example, if an ILP Prepare packet has an amount of 100 and three
/// StreamMoney frames with 5, 15, and 30 shares for streams 2, 4, and 6,
/// respectively, that would indicate that stream 2 should get 10 units,
/// stream 4 gets 30 units, and stream 6 gets 60 units.
/// If the Prepare amount is not divisible by the total number of shares,
/// stream amounts are rounded down.
///
/// The remainder is be allocated to the lowest-numbered open stream that has not reached its maximum receive amount.
#[derive(Debug, PartialEq, Clone)]
pub struct StreamMoneyFrame {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// Proportion of the ILP Prepare amount destined for the stream specified.
pub shares: u64,
}
impl<'a> SerializableFrame<'a> for StreamMoneyFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let shares = reader.read_var_uint()?;
Ok(StreamMoneyFrame { stream_id, shares })
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_var_uint(self.shares);
}
}
/// Specifies the max amount of money the endpoint wants to send
///
/// The amounts in this frame are denominated in the units of the
/// endpoint sending the frame, so the other endpoint must use their
/// calculated exchange rate to determine how much more they can send
/// for this stream.
#[derive(Debug, PartialEq, Clone)]
pub struct StreamMaxMoneyFrame {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// Total amount, denominated in the units of the endpoint
/// sending this frame, that the endpoint is willing to receive on this stream.
pub receive_max: u64,
/// Total amount, denominated in the units of the endpoint
/// sending this frame, that the endpoint has received thus far.
pub total_received: u64,
}
impl<'a> SerializableFrame<'a> for StreamMaxMoneyFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let receive_max = saturating_read_var_uint(&mut reader)?;
let total_received = reader.read_var_uint()?;
Ok(StreamMaxMoneyFrame {
stream_id,
receive_max,
total_received,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_var_uint(self.receive_max);
buf.put_var_uint(self.total_received);
}
}
/// Frame specifying the maximum amount of money the sending endpoint will send
#[derive(Debug, PartialEq, Clone)]
pub struct StreamMoneyBlockedFrame {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// Total amount, denominated in the units of the endpoint
/// sending this frame, that the endpoint wants to send.
pub send_max: u64,
/// Total amount, denominated in the units of the endpoint
/// sending this frame, that the endpoint has sent already.
pub total_sent: u64,
}
impl<'a> SerializableFrame<'a> for StreamMoneyBlockedFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let send_max = saturating_read_var_uint(&mut reader)?;
let total_sent = reader.read_var_uint()?;
Ok(StreamMoneyBlockedFrame {
stream_id,
send_max,
total_sent,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_var_uint(self.send_max);
buf.put_var_uint(self.total_sent);
}
}
/// Packets may be received out of order so the Offset is used to
/// indicate the correct position of the byte segment in the overall stream.
/// The first StreamData frame sent for a given stream MUST start with an Offset of zero.
/// Fragments of data provided by a stream's StreamData frames
/// MUST NOT ever overlap with one another. For example, the following combination
/// of frames is forbidden because bytes 15-19 were provided twice:
///
/// ```ignore
/// StreamData { StreamID: 1, Offset: 10, Data: "1234567890" }
/// StreamData { StreamID: 1, Offset: 15, Data: "67890" }
/// ```
///
/// In other words, if a sender resends data (e.g. because a packet was lost),
/// it MUST resend the exact frames — offset and data.
/// This rule exists to simplify data reassembly for the receiver
#[derive(Debug, PartialEq, Clone)]
pub struct StreamDataFrame<'a> {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// Position of this data in the byte stream.
pub offset: u64,
/// Application data
pub data: &'a [u8],
}
impl<'a> SerializableFrame<'a> for StreamDataFrame<'a> {
fn read_contents(mut reader: &'a [u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let offset = reader.read_var_uint()?;
let data = reader.read_var_octet_string()?;
Ok(StreamDataFrame {
stream_id,
offset,
data,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_var_uint(self.offset);
buf.put_var_octet_string(self.data);
}
}
/// The maximum amount of data the endpoint is willing to receive on this stream
#[derive(Debug, PartialEq, Clone)]
pub struct StreamMaxDataFrame {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// The total number of bytes the endpoint is willing to receive on this stream.
pub max_offset: u64,
}
impl<'a> SerializableFrame<'a> for StreamMaxDataFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let max_offset = reader.read_var_uint()?;
Ok(StreamMaxDataFrame {
stream_id,
max_offset,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_var_uint(self.max_offset);
}
}
/// The maximum amount of data the endpoint is willing to send on this stream
#[derive(Debug, PartialEq, Clone)]
pub struct StreamDataBlockedFrame {
/// Identifier of the stream this frame refers to.
pub stream_id: u64,
/// The total number of bytes the endpoint wants to send on this stream.
pub max_offset: u64,
}
impl<'a> SerializableFrame<'a> for StreamDataBlockedFrame {
fn read_contents(mut reader: &[u8]) -> Result<Self, ParseError> {
let stream_id = reader.read_var_uint()?;
let max_offset = reader.read_var_uint()?;
Ok(StreamDataBlockedFrame {
stream_id,
max_offset,
})
}
fn put_contents(&self, buf: &mut impl MutBufOerExt) {
buf.put_var_uint(self.stream_id);
buf.put_var_uint(self.max_offset);
}
}
/// See: https://github.com/interledger/rfcs/blob/master/0029-stream/0029-stream.md#514-maximum-varuint-size
fn saturating_read_var_uint<'a>(reader: &mut impl BufOerExt<'a>) -> Result<u64, ParseError> {
if reader.peek_var_octet_string()?.len() > 8 {
reader.skip_var_octet_string()?;
Ok(u64::MAX)
} else {
Ok(reader.read_var_uint()?)
}
}
#[cfg(test)]
mod fuzzing {
use super::{StreamPacket, StreamPacketBuilder};
#[test]
#[ignore]
fn fuzzed_0_extra_trailer_bytes() {
// junk trailer mentioned in RFC might be applicable here, see
// fuzzed_1_unknown_frames_dont_roundtrip
roundtrip(&[
1, 14, 3, 19, 5, 3, 1, 14, 3, 0, 0, 0, 0, 14, 3, 0, 14, 5, 17,
]);
// roundtripped version looks like:
//
// [1, 14, 3, 19, 5, 3, 1, 14, 1, 0]
// ^
// differs, was 3
//
// which is almost &input[0..10] except
}
#[test]
#[ignore]
fn fuzzed_1_unknown_frames_dont_roundtrip() {
// from the [RFC]'s it sounds like the at least trailer junk should be kept around,
// it would help if unknown frames would be kept around as well for fuzzing at least.
//
// [RFC]: https://github.com/interledger/rfcs/blob/master/0029-stream/0029-stream.md#52-stream-packet
roundtrip(&[
1, 14, 3, 5, 0, 0, 3, 5, 14, 9, 1, 3, 1, 3, 0, 4, 20, 3, 7, 14, 5, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
]);
// roundtripped version looks like:
//
// [1, 14, 3, 5, 0, 0, 3, 5, 14, 9, 1, 3]
//
// which is &input[0..12]
}
fn roundtrip(input: &[u8]) {
use bytes::{Buf, BytesMut};
// this started off as almost copy of crate::fuzz_decrypted_stream_packet but should be
// extended if necessary
let b = BytesMut::from(input);
let pkt = StreamPacket::from_decrypted(b).unwrap();
let other = StreamPacketBuilder {
sequence: pkt.sequence(),
ilp_packet_type: pkt.ilp_packet_type(),
prepare_amount: pkt.prepare_amount(),
frames: &pkt.frames().collect::<Vec<_>>(),
}
.build();
println!("{:?}", pkt.buffer_unencrypted.bytes());
println!("{:?}", other.buffer_unencrypted.bytes());
assert_eq!(pkt, other);
}
}
#[cfg(test)]
mod serialization {
use super::*;
use std::str::FromStr;
static PACKET: Lazy<StreamPacket> = Lazy::new(|| {
StreamPacketBuilder {
sequence: 1,
ilp_packet_type: IlpPacketType::try_from(12).unwrap(),
prepare_amount: 99,
frames: &[
Frame::ConnectionClose(ConnectionCloseFrame {
code: ErrorCode::NoError,
message: "oop",
}),
Frame::ConnectionNewAddress(ConnectionNewAddressFrame {
source_account: Address::from_str("example.blah").unwrap(),
}),
Frame::ConnectionMaxData(ConnectionMaxDataFrame { max_offset: 1000 }),
Frame::ConnectionDataBlocked(ConnectionDataBlockedFrame { max_offset: 2000 }),
Frame::ConnectionMaxStreamId(ConnectionMaxStreamIdFrame {
max_stream_id: 3000,
}),
Frame::ConnectionStreamIdBlocked(ConnectionStreamIdBlockedFrame {
max_stream_id: 4000,
}),
Frame::ConnectionAssetDetails(ConnectionAssetDetailsFrame {
source_asset_code: "XYZ",
source_asset_scale: 9,
}),
Frame::StreamClose(StreamCloseFrame {
stream_id: 76,
code: ErrorCode::InternalError,
message: "blah",
}),
Frame::StreamMoney(StreamMoneyFrame {
stream_id: 88,
shares: 99,
}),
Frame::StreamMaxMoney(StreamMaxMoneyFrame {
stream_id: 11,
receive_max: 987,
total_received: 500,
}),
Frame::StreamMoneyBlocked(StreamMoneyBlockedFrame {
stream_id: 66,
send_max: 20000,
total_sent: 6000,
}),
Frame::StreamData(StreamDataFrame {
stream_id: 34,
offset: 9000,
data: b"hello",
}),
Frame::StreamMaxData(StreamMaxDataFrame {
stream_id: 35,
max_offset: 8766,
}),
Frame::StreamDataBlocked(StreamDataBlockedFrame {
stream_id: 888,
max_offset: 44444,
}),
],
}
.build()
});
static SERIALIZED: Lazy<BytesMut> = Lazy::new(|| {
BytesMut::from(
&vec![