-
Notifications
You must be signed in to change notification settings - Fork 15
/
HandshakeBuilder.js
206 lines (158 loc) · 6.1 KB
/
HandshakeBuilder.js
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
"use strict";
var log = require( 'logg' ).getLogger( 'dtls.HandshakeBuilder' );
var DtlsHandshake = require( './packets/DtlsHandshake' );
var HandshakeBuilder = function() {
this.buffers = {};
this.merged = {};
this.messageSeqToDecode = 0;
this.messageSeqToRead = 0;
this.outgoingMessageSeq = 0;
this.packetLength = 1000;
};
HandshakeBuilder.prototype.createHandshakes = function( message ) {
var handshakes = [];
// If parameter was an array, recurse into this function with single values.
if( message instanceof Array ) {
for( var m in message )
handshakes = handshakes.concat( this.createHandshakes( message[m] ) );
return handshakes;
}
var buffer = message.getBuffer();
var handshake = new DtlsHandshake({
msgType: message.messageType,
length: buffer.length,
messageSeq: this.outgoingMessageSeq,
fragmentOffset: 0,
body: buffer
});
this.outgoingMessageSeq++;
return handshake;
};
HandshakeBuilder.prototype.fragmentHandshakes = function( packet ) {
var packets = [];
// If parameter was an array, recurse into this function with single values.
if( packet instanceof Array ) {
for( var p in packet )
packets = packets.concat( this.fragmentHandshakes( packet[p] ) );
return packets;
}
if( packet instanceof DtlsHandshake )
packet = packet.getBuffer();
// Get the raw body.
// The header before body includes:
// msgType : uint8 (1 byte),
// length : uint24 (3 bytes),
// messageSeq : uint16 (2 bytes),
// fragmentOffset : uint24 (3 bytes),
// bodyLength : uint24 (3 bytes)
var remainingBody = packet.slice( 1 + 3 + 2 + 3 + 3 );
// Create the fragments
// Make sure there is at least one fragment even if body is 0 bytes.
var offset = 0;
var first = true;
while( first || remainingBody.length ) {
first = false;
// Create each handshake message and insert the fragment into it.
var fragmentSize = Math.min( this.packetLength, remainingBody.length );
packets.push( new DtlsHandshake({
msgType: packet.readUInt8( 0 ),
length: packet.readUInt32BE( 0 ) & 0x00ffffff,
messageSeq: packet.readUInt16BE( 4 ),
fragmentOffset: offset,
body: remainingBody.slice( 0, fragmentSize )
}));
// Advance the packet
remainingBody = remainingBody.slice( fragmentSize );
offset += fragmentSize;
}
return packets;
};
HandshakeBuilder.prototype.add = function( handshake ) {
// Ignore this if it's part of a handshake we've already read.
if( handshake.messageSeq < this.messageSeqToDecode ) {
log.warn( 'seq < decode' );
return false;
}
log.info( 'Received fragment of sequence:', handshake.messageSeq );
var buffer = this._getBuffer( handshake );
// Ignore this fragment if we've already got all bytes it would contain.
if( handshake.body.length > 0 &&
handshake.fragmentOffset + handshake.body.length <= buffer.bytesRead ) {
log.warn( 'no new data' );
return false;
}
// Buffer the data if we're not ready to read it yet.
if( handshake.fragmentOffset > buffer.bytesRead ) {
log.warn( 'not ready to handle' );
buffer.fragments.push( handshake );
return false;
}
log.info( 'Valid data' );
// Write the fragment into the buffer
this._writeToBuffer( handshake, buffer );
// Sort the buffered fragments so we can read them in order.
buffer.fragments.sort( function( a, b ) {
return a.fragmentOffset - b.fragmentOffset;
});
// Go through as many of the buffered fragments as we can while
// still not skipping any bytes in the body buffer.
while( buffer.fragments.length > 0 &&
buffer.fragments[0].fragmentOffset <= buffer.bytesRead ) {
this._writeToBuffer( buffer.fragments.shift(), buffer );
}
// Check if the buffer is ready.
// Return false if it isn't. We'll keep buffering more.
if( buffer.bytesRead < buffer.length )
return false;
// Store the completed Handshake message in the merged array.
log.info( 'Merged' );
this.merged[ buffer.messageSeq ] = new DtlsHandshake({
msgType: buffer.msgType,
length: buffer.length,
messageSeq: buffer.messageSeq,
fragmentOffset: 0,
body: buffer.body
});
// Clear the buffer and raise the messageSeqToDecode so we won't read this
// message again.
delete this.buffers[ buffer.messageSeq ];
this.messageSeqToDecode++;
log.info( 'Raised decode++', this.messageSeqToDecode );
return true;
};
HandshakeBuilder.prototype.next = function() {
// Return false if we are still buffering the next to read packet.
if( this.messageSeqToRead === this.messageSeqToDecode )
return false;
// Pop the next to read out of the merged collection and advance the
// counter.
var msg = this.merged[ this.messageSeqToRead ];
delete this.merged[ this.messageSeqToRead ];
this.messageSeqToRead++;
return msg;
};
HandshakeBuilder.prototype._getBuffer = function( handshake ) {
if( !this.buffers[ handshake.messageSeq ] ) {
this.buffers[ handshake.messageSeq ] = {
msgType: handshake.msgType,
messageSeq: handshake.messageSeq,
length: handshake.length,
bytesRead: 0,
fragments: [],
body: new Buffer( handshake.length )
};
}
return this.buffers[ handshake.messageSeq ];
};
HandshakeBuilder.prototype._writeToBuffer = function( handshake, buffer ) {
// Ignore this fragment if we've already got all bytes it would contain.
//
// We do this check again because we might have buffered overlapping
// fragments.
if( handshake.fragmentOffset + handshake.body.length <= buffer.bytesRead ) {
return;
}
handshake.body.copy( buffer.body, handshake.fragmentOffset );
buffer.bytesRead = handshake.fragmentOffset + handshake.body.length;
};
module.exports = HandshakeBuilder;