@@ -14,8 +14,10 @@ export const parsePacket = (data: Buffer) => {
1414 const packets : DtlsPlaintext [ ] = [ ] ;
1515 while ( data . length > start ) {
1616 const fragmentLength = data . readUInt16BE ( start + 11 ) ;
17- if ( data . length < start + ( 12 + fragmentLength ) ) break ;
18- const packet = DtlsPlaintext . deSerialize ( data . slice ( start ) ) ;
17+ if ( data . length < start + ( 12 + fragmentLength ) ) {
18+ break ;
19+ }
20+ const packet = DtlsPlaintext . deSerialize ( data . subarray ( start ) ) ;
1921 packets . push ( packet ) ;
2022
2123 start += 13 + fragmentLength ;
@@ -25,16 +27,24 @@ export const parsePacket = (data: Buffer) => {
2527} ;
2628
2729export const parsePlainText =
28- ( dtls : DtlsContext , cipher : CipherContext ) => ( plain : DtlsPlaintext ) => {
30+ ( dtls : DtlsContext , cipher : CipherContext ) =>
31+ (
32+ plain : DtlsPlaintext ,
33+ ) : {
34+ type : ContentType ;
35+ data : any ;
36+ } [ ] => {
2937 const contentType = plain . recordLayerHeader . contentType ;
3038
3139 switch ( contentType ) {
3240 case ContentType . changeCipherSpec : {
3341 log ( dtls . sessionId , "change cipher spec" ) ;
34- return {
35- type : ContentType . changeCipherSpec ,
36- data : undefined ,
37- } ;
42+ return [
43+ {
44+ type : ContentType . changeCipherSpec ,
45+ data : undefined ,
46+ } ,
47+ ] ;
3848 }
3949 case ContentType . handshake : {
4050 let raw = plain . fragment ;
@@ -48,20 +58,29 @@ export const parsePlainText =
4858 throw error ;
4959 }
5060 try {
51- return {
52- type : ContentType . handshake ,
53- data : FragmentedHandshake . deSerialize ( raw ) ,
54- } ;
61+ let start = 0 ;
62+ const handshakes : { type : ContentType ; data : any } [ ] = [ ] ;
63+ while ( raw . length > start ) {
64+ const handshake = FragmentedHandshake . deSerialize (
65+ raw . subarray ( start ) ,
66+ ) ;
67+ handshakes . push ( { type : ContentType . handshake , data : handshake } ) ;
68+ start += handshake . fragment_length + 12 ;
69+ }
70+
71+ return handshakes ;
5572 } catch ( error ) {
5673 err ( dtls . sessionId , "decSerialize failed" , error , raw ) ;
5774 throw error ;
5875 }
5976 }
6077 case ContentType . applicationData : {
61- return {
62- type : ContentType . applicationData ,
63- data : cipher . decryptPacket ( plain ) ,
64- } ;
78+ return [
79+ {
80+ type : ContentType . applicationData ,
81+ data : cipher . decryptPacket ( plain ) ,
82+ } ,
83+ ] ;
6584 }
6685 case ContentType . alert : {
6786 let alert = Alert . deSerialize ( plain . fragment ) ;
@@ -84,10 +103,10 @@ export const parsePlainText =
84103 if ( alert . level > 1 ) {
85104 throw new Error ( "alert fatal error" ) ;
86105 }
87- return { type : ContentType . alert , data : undefined } ;
106+ return [ { type : ContentType . alert , data : undefined } ] ;
88107 }
89108 default : {
90- return { type : ContentType . alert , data : undefined } ;
109+ return [ { type : ContentType . alert , data : undefined } ] ;
91110 }
92111 }
93112 } ;
0 commit comments