48083
|
1 |
/*
|
49765
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
|
48083
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
49765
|
26 |
package jdk.internal.net.http.common;
|
48083
|
27 |
|
49765
|
28 |
import jdk.internal.net.http.common.SubscriberWrapper.SchedulingAction;
|
|
29 |
|
48083
|
30 |
import javax.net.ssl.SSLEngine;
|
|
31 |
import javax.net.ssl.SSLEngineResult;
|
|
32 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
|
|
33 |
import javax.net.ssl.SSLEngineResult.Status;
|
|
34 |
import javax.net.ssl.SSLException;
|
49765
|
35 |
import java.io.IOException;
|
|
36 |
import java.nio.ByteBuffer;
|
|
37 |
import java.util.ArrayList;
|
|
38 |
import java.util.Collections;
|
|
39 |
import java.util.Iterator;
|
|
40 |
import java.util.LinkedList;
|
|
41 |
import java.util.List;
|
|
42 |
import java.util.concurrent.CompletableFuture;
|
|
43 |
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
44 |
import java.util.concurrent.Executor;
|
|
45 |
import java.util.concurrent.Flow;
|
|
46 |
import java.util.concurrent.Flow.Subscriber;
|
|
47 |
import java.util.concurrent.atomic.AtomicInteger;
|
49944
|
48 |
import java.util.function.Consumer;
|
48083
|
49 |
|
|
50 |
/**
|
|
51 |
* Implements SSL using two SubscriberWrappers.
|
|
52 |
*
|
|
53 |
* <p> Constructor takes two Flow.Subscribers: one that receives the network
|
|
54 |
* data (after it has been encrypted by SSLFlowDelegate) data, and one that
|
|
55 |
* receives the application data (before it has been encrypted by SSLFlowDelegate).
|
|
56 |
*
|
|
57 |
* <p> Methods upstreamReader() and upstreamWriter() return the corresponding
|
|
58 |
* Flow.Subscribers containing Flows for the encrypted/decrypted upstream data.
|
|
59 |
* See diagram below.
|
|
60 |
*
|
|
61 |
* <p> How Flow.Subscribers are used in this class, and where they come from:
|
|
62 |
* <pre>
|
|
63 |
* {@code
|
|
64 |
*
|
|
65 |
*
|
|
66 |
*
|
|
67 |
* ---------> data flow direction
|
|
68 |
*
|
|
69 |
*
|
|
70 |
* +------------------+
|
|
71 |
* upstreamWriter | | downWriter
|
|
72 |
* ---------------> | | ------------>
|
|
73 |
* obtained from this | | supplied to constructor
|
|
74 |
* | SSLFlowDelegate |
|
|
75 |
* downReader | | upstreamReader
|
|
76 |
* <--------------- | | <--------------
|
|
77 |
* supplied to constructor | | obtained from this
|
|
78 |
* +------------------+
|
49765
|
79 |
*
|
|
80 |
* Errors are reported to the downReader Flow.Subscriber
|
|
81 |
*
|
48083
|
82 |
* }
|
|
83 |
* </pre>
|
|
84 |
*/
|
|
85 |
public class SSLFlowDelegate {
|
|
86 |
|
49765
|
87 |
final Logger debug =
|
|
88 |
Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
|
48083
|
89 |
|
|
90 |
final Executor exec;
|
|
91 |
final Reader reader;
|
|
92 |
final Writer writer;
|
|
93 |
final SSLEngine engine;
|
|
94 |
final String tubeName; // hack
|
|
95 |
final CompletableFuture<String> alpnCF; // completes on initial handshake
|
|
96 |
final static ByteBuffer SENTINEL = Utils.EMPTY_BYTEBUFFER;
|
|
97 |
volatile boolean close_notify_received;
|
49765
|
98 |
final CompletableFuture<Void> readerCF;
|
|
99 |
final CompletableFuture<Void> writerCF;
|
49944
|
100 |
final Consumer<ByteBuffer> recycler;
|
49765
|
101 |
static AtomicInteger scount = new AtomicInteger(1);
|
|
102 |
final int id;
|
48083
|
103 |
|
|
104 |
/**
|
|
105 |
* Creates an SSLFlowDelegate fed from two Flow.Subscribers. Each
|
|
106 |
* Flow.Subscriber requires an associated {@link CompletableFuture}
|
|
107 |
* for errors that need to be signaled from downstream to upstream.
|
|
108 |
*/
|
|
109 |
public SSLFlowDelegate(SSLEngine engine,
|
|
110 |
Executor exec,
|
|
111 |
Subscriber<? super List<ByteBuffer>> downReader,
|
|
112 |
Subscriber<? super List<ByteBuffer>> downWriter)
|
|
113 |
{
|
49944
|
114 |
this(engine, exec, null, downReader, downWriter);
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Creates an SSLFlowDelegate fed from two Flow.Subscribers. Each
|
|
119 |
* Flow.Subscriber requires an associated {@link CompletableFuture}
|
|
120 |
* for errors that need to be signaled from downstream to upstream.
|
|
121 |
*/
|
|
122 |
public SSLFlowDelegate(SSLEngine engine,
|
|
123 |
Executor exec,
|
|
124 |
Consumer<ByteBuffer> recycler,
|
|
125 |
Subscriber<? super List<ByteBuffer>> downReader,
|
|
126 |
Subscriber<? super List<ByteBuffer>> downWriter)
|
|
127 |
{
|
49765
|
128 |
this.id = scount.getAndIncrement();
|
48083
|
129 |
this.tubeName = String.valueOf(downWriter);
|
49944
|
130 |
this.recycler = recycler;
|
48083
|
131 |
this.reader = new Reader();
|
|
132 |
this.writer = new Writer();
|
|
133 |
this.engine = engine;
|
|
134 |
this.exec = exec;
|
|
135 |
this.handshakeState = new AtomicInteger(NOT_HANDSHAKING);
|
49765
|
136 |
this.readerCF = reader.completion();
|
|
137 |
this.writerCF = reader.completion();
|
|
138 |
readerCF.exceptionally(this::stopOnError);
|
|
139 |
writerCF.exceptionally(this::stopOnError);
|
|
140 |
|
|
141 |
CompletableFuture.allOf(reader.completion(), writer.completion())
|
|
142 |
.thenRun(this::normalStop);
|
48083
|
143 |
this.alpnCF = new MinimalFuture<>();
|
|
144 |
|
|
145 |
// connect the Reader to the downReader and the
|
|
146 |
// Writer to the downWriter.
|
|
147 |
connect(downReader, downWriter);
|
|
148 |
|
|
149 |
//Monitor.add(this::monitor);
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Returns true if the SSLFlowDelegate has detected a TLS
|
|
154 |
* close_notify from the server.
|
|
155 |
* @return true, if a close_notify was detected.
|
|
156 |
*/
|
|
157 |
public boolean closeNotifyReceived() {
|
|
158 |
return close_notify_received;
|
|
159 |
}
|
|
160 |
|
|
161 |
/**
|
|
162 |
* Connects the read sink (downReader) to the SSLFlowDelegate Reader,
|
|
163 |
* and the write sink (downWriter) to the SSLFlowDelegate Writer.
|
|
164 |
* Called from within the constructor. Overwritten by SSLTube.
|
|
165 |
*
|
|
166 |
* @param downReader The left hand side read sink (typically, the
|
|
167 |
* HttpConnection read subscriber).
|
|
168 |
* @param downWriter The right hand side write sink (typically
|
|
169 |
* the SocketTube write subscriber).
|
|
170 |
*/
|
|
171 |
void connect(Subscriber<? super List<ByteBuffer>> downReader,
|
|
172 |
Subscriber<? super List<ByteBuffer>> downWriter) {
|
|
173 |
this.reader.subscribe(downReader);
|
|
174 |
this.writer.subscribe(downWriter);
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Returns a CompletableFuture<String> which completes after
|
|
179 |
* the initial handshake completes, and which contains the negotiated
|
|
180 |
* alpn.
|
|
181 |
*/
|
|
182 |
public CompletableFuture<String> alpn() {
|
|
183 |
return alpnCF;
|
|
184 |
}
|
|
185 |
|
|
186 |
private void setALPN() {
|
|
187 |
// Handshake is finished. So, can retrieve the ALPN now
|
|
188 |
if (alpnCF.isDone())
|
|
189 |
return;
|
|
190 |
String alpn = engine.getApplicationProtocol();
|
49765
|
191 |
if (debug.on()) debug.log("setALPN = %s", alpn);
|
48083
|
192 |
alpnCF.complete(alpn);
|
|
193 |
}
|
|
194 |
|
|
195 |
public String monitor() {
|
|
196 |
StringBuilder sb = new StringBuilder();
|
49765
|
197 |
sb.append("SSL: id ").append(id);
|
|
198 |
sb.append(" HS state: " + states(handshakeState));
|
48083
|
199 |
sb.append(" Engine state: " + engine.getHandshakeStatus().toString());
|
49944
|
200 |
if (stateList != null) {
|
|
201 |
sb.append(" LL : ");
|
|
202 |
for (String s : stateList) {
|
|
203 |
sb.append(s).append(" ");
|
|
204 |
}
|
48083
|
205 |
}
|
|
206 |
sb.append("\r\n");
|
|
207 |
sb.append("Reader:: ").append(reader.toString());
|
|
208 |
sb.append("\r\n");
|
|
209 |
sb.append("Writer:: ").append(writer.toString());
|
|
210 |
sb.append("\r\n===================================");
|
|
211 |
return sb.toString();
|
|
212 |
}
|
|
213 |
|
|
214 |
protected SchedulingAction enterReadScheduling() {
|
|
215 |
return SchedulingAction.CONTINUE;
|
|
216 |
}
|
|
217 |
|
|
218 |
|
|
219 |
/**
|
|
220 |
* Processing function for incoming data. Pass it thru SSLEngine.unwrap().
|
|
221 |
* Any decrypted buffers returned to be passed downstream.
|
|
222 |
* Status codes:
|
|
223 |
* NEED_UNWRAP: do nothing. Following incoming data will contain
|
|
224 |
* any required handshake data
|
|
225 |
* NEED_WRAP: call writer.addData() with empty buffer
|
|
226 |
* NEED_TASK: delegate task to executor
|
|
227 |
* BUFFER_OVERFLOW: allocate larger output buffer. Repeat unwrap
|
|
228 |
* BUFFER_UNDERFLOW: keep buffer and wait for more data
|
|
229 |
* OK: return generated buffers.
|
|
230 |
*
|
|
231 |
* Upstream subscription strategy is to try and keep no more than
|
|
232 |
* TARGET_BUFSIZE bytes in readBuf
|
|
233 |
*/
|
49944
|
234 |
final class Reader extends SubscriberWrapper implements FlowTube.TubeSubscriber {
|
|
235 |
// Maximum record size is 16k.
|
|
236 |
// Because SocketTube can feeds us up to 3 16K buffers,
|
|
237 |
// then setting this size to 16K means that the readBuf
|
|
238 |
// can store up to 64K-1 (16K-1 + 3*16K)
|
|
239 |
static final int TARGET_BUFSIZE = 16 * 1024;
|
|
240 |
|
48083
|
241 |
final SequentialScheduler scheduler;
|
|
242 |
volatile ByteBuffer readBuf;
|
49765
|
243 |
volatile boolean completing;
|
48083
|
244 |
final Object readBufferLock = new Object();
|
49765
|
245 |
final Logger debugr = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
|
48083
|
246 |
|
49944
|
247 |
private final class ReaderDownstreamPusher implements Runnable {
|
48083
|
248 |
@Override public void run() { processData(); }
|
|
249 |
}
|
|
250 |
|
|
251 |
Reader() {
|
|
252 |
super();
|
|
253 |
scheduler = SequentialScheduler.synchronizedScheduler(
|
|
254 |
new ReaderDownstreamPusher());
|
|
255 |
this.readBuf = ByteBuffer.allocate(1024);
|
|
256 |
readBuf.limit(0); // keep in read mode
|
|
257 |
}
|
|
258 |
|
49944
|
259 |
@Override
|
|
260 |
public boolean supportsRecycling() {
|
|
261 |
return recycler != null;
|
|
262 |
}
|
|
263 |
|
48083
|
264 |
protected SchedulingAction enterScheduling() {
|
|
265 |
return enterReadScheduling();
|
|
266 |
}
|
|
267 |
|
|
268 |
public final String dbgString() {
|
|
269 |
return "SSL Reader(" + tubeName + ")";
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* entry point for buffers delivered from upstream Subscriber
|
|
274 |
*/
|
|
275 |
@Override
|
|
276 |
public void incoming(List<ByteBuffer> buffers, boolean complete) {
|
49765
|
277 |
if (debugr.on())
|
|
278 |
debugr.log("Adding %d bytes to read buffer",
|
|
279 |
Utils.remaining(buffers));
|
48083
|
280 |
addToReadBuf(buffers, complete);
|
49944
|
281 |
scheduler.runOrSchedule(exec);
|
48083
|
282 |
}
|
|
283 |
|
|
284 |
@Override
|
|
285 |
public String toString() {
|
|
286 |
return "READER: " + super.toString() + " readBuf: " + readBuf.toString()
|
|
287 |
+ " count: " + count.toString();
|
|
288 |
}
|
|
289 |
|
|
290 |
private void reallocReadBuf() {
|
|
291 |
int sz = readBuf.capacity();
|
|
292 |
ByteBuffer newb = ByteBuffer.allocate(sz*2);
|
|
293 |
readBuf.flip();
|
|
294 |
Utils.copy(readBuf, newb);
|
|
295 |
readBuf = newb;
|
|
296 |
}
|
|
297 |
|
|
298 |
@Override
|
|
299 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) {
|
|
300 |
if (readBuf.remaining() > TARGET_BUFSIZE) {
|
49944
|
301 |
if (debugr.on())
|
|
302 |
debugr.log("readBuf has more than TARGET_BUFSIZE: %d",
|
|
303 |
readBuf.remaining());
|
48083
|
304 |
return 0;
|
|
305 |
} else {
|
|
306 |
return super.upstreamWindowUpdate(currentWindow, downstreamQsize);
|
|
307 |
}
|
|
308 |
}
|
|
309 |
|
|
310 |
// readBuf is kept ready for reading outside of this method
|
|
311 |
private void addToReadBuf(List<ByteBuffer> buffers, boolean complete) {
|
|
312 |
synchronized (readBufferLock) {
|
|
313 |
for (ByteBuffer buf : buffers) {
|
|
314 |
readBuf.compact();
|
|
315 |
while (readBuf.remaining() < buf.remaining())
|
|
316 |
reallocReadBuf();
|
|
317 |
readBuf.put(buf);
|
|
318 |
readBuf.flip();
|
49944
|
319 |
// should be safe to call inside lock
|
|
320 |
// since the only implementation
|
|
321 |
// offers the buffer to an unbounded queue.
|
|
322 |
// WARNING: do not touch buf after this point!
|
|
323 |
if (recycler != null) recycler.accept(buf);
|
48083
|
324 |
}
|
|
325 |
if (complete) {
|
|
326 |
this.completing = complete;
|
|
327 |
}
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
void schedule() {
|
49944
|
332 |
scheduler.runOrSchedule(exec);
|
48083
|
333 |
}
|
|
334 |
|
|
335 |
void stop() {
|
49765
|
336 |
if (debugr.on()) debugr.log("stop");
|
48083
|
337 |
scheduler.stop();
|
|
338 |
}
|
|
339 |
|
|
340 |
AtomicInteger count = new AtomicInteger(0);
|
|
341 |
|
49944
|
342 |
// minimum number of bytes required to call unwrap.
|
|
343 |
// Usually this is 0, unless there was a buffer underflow.
|
|
344 |
// In this case we need to wait for more bytes than what
|
|
345 |
// we had before calling unwrap() again.
|
|
346 |
volatile int minBytesRequired;
|
48083
|
347 |
// work function where it all happens
|
49944
|
348 |
final void processData() {
|
48083
|
349 |
try {
|
49765
|
350 |
if (debugr.on())
|
|
351 |
debugr.log("processData:"
|
|
352 |
+ " readBuf remaining:" + readBuf.remaining()
|
|
353 |
+ ", state:" + states(handshakeState)
|
|
354 |
+ ", engine handshake status:" + engine.getHandshakeStatus());
|
48083
|
355 |
int len;
|
|
356 |
boolean complete = false;
|
49944
|
357 |
while (readBuf.remaining() > (len = minBytesRequired)) {
|
48083
|
358 |
boolean handshaking = false;
|
|
359 |
try {
|
|
360 |
EngineResult result;
|
|
361 |
synchronized (readBufferLock) {
|
|
362 |
complete = this.completing;
|
49944
|
363 |
if (debugr.on()) debugr.log("Unwrapping: %s", readBuf.remaining());
|
|
364 |
// Unless there is a BUFFER_UNDERFLOW, we should try to
|
|
365 |
// unwrap any number of bytes. Set minBytesRequired to 0:
|
|
366 |
// we only need to do that if minBytesRequired is not already 0.
|
|
367 |
len = len > 0 ? minBytesRequired = 0 : len;
|
48083
|
368 |
result = unwrapBuffer(readBuf);
|
49944
|
369 |
len = readBuf.remaining();
|
|
370 |
if (debugr.on()) {
|
|
371 |
debugr.log("Unwrapped: result: %s", result.result);
|
|
372 |
debugr.log("Unwrapped: consumed: %s", result.bytesConsumed());
|
|
373 |
}
|
48083
|
374 |
}
|
|
375 |
if (result.bytesProduced() > 0) {
|
49765
|
376 |
if (debugr.on())
|
|
377 |
debugr.log("sending %d", result.bytesProduced());
|
48083
|
378 |
count.addAndGet(result.bytesProduced());
|
|
379 |
outgoing(result.destBuffer, false);
|
|
380 |
}
|
|
381 |
if (result.status() == Status.BUFFER_UNDERFLOW) {
|
49765
|
382 |
if (debugr.on()) debugr.log("BUFFER_UNDERFLOW");
|
48083
|
383 |
// not enough data in the read buffer...
|
49944
|
384 |
// no need to try to unwrap again unless we get more bytes
|
|
385 |
// than minBytesRequired = len in the read buffer.
|
|
386 |
minBytesRequired = len;
|
48083
|
387 |
synchronized (readBufferLock) {
|
49944
|
388 |
// more bytes could already have been added...
|
|
389 |
assert readBuf.remaining() >= len;
|
|
390 |
// check if we have received some data, and if so
|
|
391 |
// we can just re-spin the loop
|
48083
|
392 |
if (readBuf.remaining() > len) continue;
|
|
393 |
}
|
49944
|
394 |
// request more data and return.
|
|
395 |
requestMore();
|
|
396 |
return;
|
48083
|
397 |
}
|
|
398 |
if (complete && result.status() == Status.CLOSED) {
|
49765
|
399 |
if (debugr.on()) debugr.log("Closed: completing");
|
48083
|
400 |
outgoing(Utils.EMPTY_BB_LIST, true);
|
|
401 |
return;
|
|
402 |
}
|
|
403 |
if (result.handshaking() && !complete) {
|
49765
|
404 |
if (debugr.on()) debugr.log("handshaking");
|
|
405 |
if (doHandshake(result, READER)) {
|
|
406 |
resumeActivity();
|
|
407 |
}
|
48083
|
408 |
handshaking = true;
|
|
409 |
} else {
|
49765
|
410 |
if ((handshakeState.getAndSet(NOT_HANDSHAKING)& ~DOING_TASKS) == HANDSHAKING) {
|
49944
|
411 |
handshaking = false;
|
|
412 |
applicationBufferSize = engine.getSession().getApplicationBufferSize();
|
|
413 |
packetBufferSize = engine.getSession().getPacketBufferSize();
|
48083
|
414 |
setALPN();
|
|
415 |
resumeActivity();
|
|
416 |
}
|
|
417 |
}
|
|
418 |
} catch (IOException ex) {
|
|
419 |
errorCommon(ex);
|
|
420 |
handleError(ex);
|
|
421 |
}
|
|
422 |
if (handshaking && !complete)
|
|
423 |
return;
|
|
424 |
}
|
|
425 |
if (!complete) {
|
|
426 |
synchronized (readBufferLock) {
|
|
427 |
complete = this.completing && !readBuf.hasRemaining();
|
|
428 |
}
|
|
429 |
}
|
|
430 |
if (complete) {
|
49765
|
431 |
if (debugr.on()) debugr.log("completing");
|
48083
|
432 |
// Complete the alpnCF, if not already complete, regardless of
|
|
433 |
// whether or not the ALPN is available, there will be no more
|
|
434 |
// activity.
|
|
435 |
setALPN();
|
|
436 |
outgoing(Utils.EMPTY_BB_LIST, true);
|
|
437 |
}
|
|
438 |
} catch (Throwable ex) {
|
|
439 |
errorCommon(ex);
|
|
440 |
handleError(ex);
|
|
441 |
}
|
|
442 |
}
|
|
443 |
|
49765
|
444 |
EngineResult unwrapBuffer(ByteBuffer src) throws IOException {
|
|
445 |
ByteBuffer dst = getAppBuffer();
|
|
446 |
while (true) {
|
|
447 |
SSLEngineResult sslResult = engine.unwrap(src, dst);
|
|
448 |
switch (sslResult.getStatus()) {
|
|
449 |
case BUFFER_OVERFLOW:
|
|
450 |
// may happen only if app size buffer was changed.
|
|
451 |
// get it again if app buffer size changed
|
49944
|
452 |
int appSize = applicationBufferSize =
|
|
453 |
engine.getSession().getApplicationBufferSize();
|
49765
|
454 |
ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
|
|
455 |
dst.flip();
|
|
456 |
b.put(dst);
|
|
457 |
dst = b;
|
|
458 |
break;
|
|
459 |
case CLOSED:
|
|
460 |
return doClosure(new EngineResult(sslResult));
|
|
461 |
case BUFFER_UNDERFLOW:
|
|
462 |
// handled implicitly by compaction/reallocation of readBuf
|
|
463 |
return new EngineResult(sslResult);
|
|
464 |
case OK:
|
|
465 |
dst.flip();
|
|
466 |
return new EngineResult(sslResult, dst);
|
|
467 |
}
|
|
468 |
}
|
|
469 |
}
|
48083
|
470 |
}
|
|
471 |
|
|
472 |
public interface Monitorable {
|
|
473 |
public String getInfo();
|
|
474 |
}
|
|
475 |
|
|
476 |
public static class Monitor extends Thread {
|
|
477 |
final List<Monitorable> list;
|
|
478 |
static Monitor themon;
|
|
479 |
|
|
480 |
static {
|
|
481 |
themon = new Monitor();
|
|
482 |
themon.start(); // uncomment to enable Monitor
|
|
483 |
}
|
|
484 |
|
|
485 |
Monitor() {
|
|
486 |
super("Monitor");
|
|
487 |
setDaemon(true);
|
|
488 |
list = Collections.synchronizedList(new LinkedList<>());
|
|
489 |
}
|
|
490 |
|
|
491 |
void addTarget(Monitorable o) {
|
|
492 |
list.add(o);
|
|
493 |
}
|
|
494 |
|
|
495 |
public static void add(Monitorable o) {
|
|
496 |
themon.addTarget(o);
|
|
497 |
}
|
|
498 |
|
|
499 |
@Override
|
|
500 |
public void run() {
|
|
501 |
System.out.println("Monitor starting");
|
49765
|
502 |
try {
|
|
503 |
while (true) {
|
|
504 |
Thread.sleep(20 * 1000);
|
|
505 |
synchronized (list) {
|
|
506 |
for (Monitorable o : list) {
|
|
507 |
System.out.println(o.getInfo());
|
|
508 |
System.out.println("-------------------------");
|
|
509 |
}
|
48083
|
510 |
}
|
49765
|
511 |
System.out.println("--o-o-o-o-o-o-o-o-o-o-o-o-o-o-");
|
48083
|
512 |
}
|
49765
|
513 |
} catch (InterruptedException e) {
|
|
514 |
System.out.println("Monitor exiting with " + e);
|
48083
|
515 |
}
|
|
516 |
}
|
|
517 |
}
|
|
518 |
|
|
519 |
/**
|
|
520 |
* Processing function for outgoing data. Pass it thru SSLEngine.wrap()
|
|
521 |
* Any encrypted buffers generated are passed downstream to be written.
|
|
522 |
* Status codes:
|
|
523 |
* NEED_UNWRAP: call reader.addData() with empty buffer
|
|
524 |
* NEED_WRAP: call addData() with empty buffer
|
|
525 |
* NEED_TASK: delegate task to executor
|
|
526 |
* BUFFER_OVERFLOW: allocate larger output buffer. Repeat wrap
|
|
527 |
* BUFFER_UNDERFLOW: shouldn't happen on writing side
|
|
528 |
* OK: return generated buffers
|
|
529 |
*/
|
|
530 |
class Writer extends SubscriberWrapper {
|
|
531 |
final SequentialScheduler scheduler;
|
|
532 |
// queues of buffers received from upstream waiting
|
|
533 |
// to be processed by the SSLEngine
|
|
534 |
final List<ByteBuffer> writeList;
|
49765
|
535 |
final Logger debugw = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
|
48083
|
536 |
volatile boolean completing;
|
|
537 |
boolean completed; // only accessed in processData
|
|
538 |
|
|
539 |
class WriterDownstreamPusher extends SequentialScheduler.CompleteRestartableTask {
|
|
540 |
@Override public void run() { processData(); }
|
|
541 |
}
|
|
542 |
|
|
543 |
Writer() {
|
|
544 |
super();
|
|
545 |
writeList = Collections.synchronizedList(new LinkedList<>());
|
|
546 |
scheduler = new SequentialScheduler(new WriterDownstreamPusher());
|
|
547 |
}
|
|
548 |
|
|
549 |
@Override
|
|
550 |
protected void incoming(List<ByteBuffer> buffers, boolean complete) {
|
49944
|
551 |
assert complete ? buffers == Utils.EMPTY_BB_LIST : true;
|
48083
|
552 |
assert buffers != Utils.EMPTY_BB_LIST ? complete == false : true;
|
|
553 |
if (complete) {
|
49765
|
554 |
if (debugw.on()) debugw.log("adding SENTINEL");
|
48083
|
555 |
completing = true;
|
|
556 |
writeList.add(SENTINEL);
|
|
557 |
} else {
|
|
558 |
writeList.addAll(buffers);
|
|
559 |
}
|
49765
|
560 |
if (debugw.on())
|
|
561 |
debugw.log("added " + buffers.size()
|
|
562 |
+ " (" + Utils.remaining(buffers)
|
|
563 |
+ " bytes) to the writeList");
|
48083
|
564 |
scheduler.runOrSchedule();
|
|
565 |
}
|
|
566 |
|
|
567 |
public final String dbgString() {
|
|
568 |
return "SSL Writer(" + tubeName + ")";
|
|
569 |
}
|
|
570 |
|
|
571 |
protected void onSubscribe() {
|
49765
|
572 |
if (debugw.on()) debugw.log("onSubscribe initiating handshaking");
|
|
573 |
addData(HS_TRIGGER); // initiates handshaking
|
48083
|
574 |
}
|
|
575 |
|
|
576 |
void schedule() {
|
|
577 |
scheduler.runOrSchedule();
|
|
578 |
}
|
|
579 |
|
|
580 |
void stop() {
|
49765
|
581 |
if (debugw.on()) debugw.log("stop");
|
48083
|
582 |
scheduler.stop();
|
|
583 |
}
|
|
584 |
|
|
585 |
@Override
|
|
586 |
public boolean closing() {
|
|
587 |
return closeNotifyReceived();
|
|
588 |
}
|
|
589 |
|
|
590 |
private boolean isCompleting() {
|
|
591 |
return completing;
|
|
592 |
}
|
|
593 |
|
|
594 |
@Override
|
|
595 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) {
|
|
596 |
if (writeList.size() > 10)
|
|
597 |
return 0;
|
|
598 |
else
|
|
599 |
return super.upstreamWindowUpdate(currentWindow, downstreamQsize);
|
|
600 |
}
|
|
601 |
|
|
602 |
private boolean hsTriggered() {
|
|
603 |
synchronized(writeList) {
|
|
604 |
for (ByteBuffer b : writeList)
|
|
605 |
if (b == HS_TRIGGER)
|
|
606 |
return true;
|
|
607 |
return false;
|
|
608 |
}
|
|
609 |
}
|
|
610 |
|
49944
|
611 |
void triggerWrite() {
|
|
612 |
synchronized (writeList) {
|
|
613 |
if (writeList.isEmpty()) {
|
|
614 |
writeList.add(HS_TRIGGER);
|
|
615 |
}
|
|
616 |
}
|
|
617 |
scheduler.runOrSchedule();
|
|
618 |
}
|
|
619 |
|
48083
|
620 |
private void processData() {
|
|
621 |
boolean completing = isCompleting();
|
|
622 |
|
|
623 |
try {
|
49765
|
624 |
if (debugw.on())
|
|
625 |
debugw.log("processData, writeList remaining:"
|
|
626 |
+ Utils.remaining(writeList) + ", hsTriggered:"
|
|
627 |
+ hsTriggered() + ", needWrap:" + needWrap());
|
|
628 |
|
|
629 |
while (Utils.remaining(writeList) > 0 || hsTriggered() || needWrap()) {
|
48083
|
630 |
ByteBuffer[] outbufs = writeList.toArray(Utils.EMPTY_BB_ARRAY);
|
|
631 |
EngineResult result = wrapBuffers(outbufs);
|
49765
|
632 |
if (debugw.on())
|
|
633 |
debugw.log("wrapBuffer returned %s", result.result);
|
48083
|
634 |
|
|
635 |
if (result.status() == Status.CLOSED) {
|
49765
|
636 |
if (!upstreamCompleted) {
|
|
637 |
upstreamCompleted = true;
|
|
638 |
upstreamSubscription.cancel();
|
|
639 |
}
|
48083
|
640 |
if (result.bytesProduced() <= 0)
|
|
641 |
return;
|
|
642 |
|
|
643 |
if (!completing && !completed) {
|
|
644 |
completing = this.completing = true;
|
|
645 |
// There could still be some outgoing data in outbufs.
|
|
646 |
writeList.add(SENTINEL);
|
|
647 |
}
|
|
648 |
}
|
|
649 |
|
|
650 |
boolean handshaking = false;
|
|
651 |
if (result.handshaking()) {
|
49765
|
652 |
if (debugw.on()) debugw.log("handshaking");
|
|
653 |
doHandshake(result, WRITER); // ok to ignore return
|
48083
|
654 |
handshaking = true;
|
|
655 |
} else {
|
|
656 |
if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) {
|
49944
|
657 |
applicationBufferSize = engine.getSession().getApplicationBufferSize();
|
|
658 |
packetBufferSize = engine.getSession().getPacketBufferSize();
|
48083
|
659 |
setALPN();
|
|
660 |
resumeActivity();
|
|
661 |
}
|
|
662 |
}
|
|
663 |
cleanList(writeList); // tidy up the source list
|
|
664 |
sendResultBytes(result);
|
|
665 |
if (handshaking && !completing) {
|
49765
|
666 |
if (needWrap()) {
|
|
667 |
continue;
|
|
668 |
} else {
|
|
669 |
return;
|
48083
|
670 |
}
|
|
671 |
}
|
|
672 |
}
|
|
673 |
if (completing && Utils.remaining(writeList) == 0) {
|
|
674 |
if (!completed) {
|
|
675 |
completed = true;
|
|
676 |
writeList.clear();
|
|
677 |
outgoing(Utils.EMPTY_BB_LIST, true);
|
|
678 |
}
|
|
679 |
return;
|
|
680 |
}
|
|
681 |
if (writeList.isEmpty() && needWrap()) {
|
|
682 |
writer.addData(HS_TRIGGER);
|
|
683 |
}
|
|
684 |
} catch (Throwable ex) {
|
49765
|
685 |
errorCommon(ex);
|
48083
|
686 |
handleError(ex);
|
|
687 |
}
|
|
688 |
}
|
|
689 |
|
49765
|
690 |
@SuppressWarnings("fallthrough")
|
|
691 |
EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException {
|
|
692 |
if (debugw.on())
|
|
693 |
debugw.log("wrapping " + Utils.remaining(src) + " bytes");
|
|
694 |
ByteBuffer dst = getNetBuffer();
|
|
695 |
while (true) {
|
|
696 |
SSLEngineResult sslResult = engine.wrap(src, dst);
|
|
697 |
if (debugw.on()) debugw.log("SSLResult: " + sslResult);
|
|
698 |
switch (sslResult.getStatus()) {
|
|
699 |
case BUFFER_OVERFLOW:
|
|
700 |
// Shouldn't happen. We allocated buffer with packet size
|
|
701 |
// get it again if net buffer size was changed
|
|
702 |
if (debugw.on()) debugw.log("BUFFER_OVERFLOW");
|
49944
|
703 |
int netSize = packetBufferSize
|
|
704 |
= engine.getSession().getPacketBufferSize();
|
|
705 |
ByteBuffer b = ByteBuffer.allocate(netSize + dst.position());
|
49765
|
706 |
dst.flip();
|
|
707 |
b.put(dst);
|
|
708 |
dst = b;
|
|
709 |
break; // try again
|
|
710 |
case CLOSED:
|
|
711 |
if (debugw.on()) debugw.log("CLOSED");
|
|
712 |
// fallthrough. There could be some remaining data in dst.
|
|
713 |
// CLOSED will be handled by the caller.
|
|
714 |
case OK:
|
|
715 |
dst.flip();
|
|
716 |
final ByteBuffer dest = dst;
|
|
717 |
if (debugw.on())
|
|
718 |
debugw.log("OK => produced: %d, not wrapped: %d",
|
|
719 |
dest.remaining(), Utils.remaining(src));
|
|
720 |
return new EngineResult(sslResult, dest);
|
|
721 |
case BUFFER_UNDERFLOW:
|
|
722 |
// Shouldn't happen. Doesn't returns when wrap()
|
|
723 |
// underflow handled externally
|
|
724 |
// assert false : "Buffer Underflow";
|
|
725 |
if (debug.on()) debug.log("BUFFER_UNDERFLOW");
|
|
726 |
return new EngineResult(sslResult);
|
|
727 |
default:
|
|
728 |
if (debugw.on())
|
|
729 |
debugw.log("result: %s", sslResult.getStatus());
|
|
730 |
assert false : "result:" + sslResult.getStatus();
|
|
731 |
}
|
|
732 |
}
|
|
733 |
}
|
|
734 |
|
48083
|
735 |
private boolean needWrap() {
|
|
736 |
return engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP;
|
|
737 |
}
|
|
738 |
|
|
739 |
private void sendResultBytes(EngineResult result) {
|
|
740 |
if (result.bytesProduced() > 0) {
|
49765
|
741 |
if (debugw.on())
|
|
742 |
debugw.log("Sending %d bytes downstream",
|
|
743 |
result.bytesProduced());
|
48083
|
744 |
outgoing(result.destBuffer, false);
|
|
745 |
}
|
|
746 |
}
|
|
747 |
|
|
748 |
@Override
|
|
749 |
public String toString() {
|
|
750 |
return "WRITER: " + super.toString() +
|
|
751 |
" writeList size " + Integer.toString(writeList.size());
|
|
752 |
//" writeList: " + writeList.toString();
|
|
753 |
}
|
|
754 |
}
|
|
755 |
|
|
756 |
private void handleError(Throwable t) {
|
49765
|
757 |
if (debug.on()) debug.log("handleError", t);
|
|
758 |
readerCF.completeExceptionally(t);
|
|
759 |
writerCF.completeExceptionally(t);
|
48083
|
760 |
// no-op if already completed
|
|
761 |
alpnCF.completeExceptionally(t);
|
|
762 |
reader.stop();
|
|
763 |
writer.stop();
|
|
764 |
}
|
|
765 |
|
49765
|
766 |
boolean stopped;
|
|
767 |
|
|
768 |
private synchronized void normalStop() {
|
|
769 |
if (stopped)
|
|
770 |
return;
|
|
771 |
stopped = true;
|
48083
|
772 |
reader.stop();
|
|
773 |
writer.stop();
|
|
774 |
}
|
|
775 |
|
49765
|
776 |
private Void stopOnError(Throwable currentlyUnused) {
|
|
777 |
// maybe log, etc
|
|
778 |
normalStop();
|
|
779 |
return null;
|
|
780 |
}
|
|
781 |
|
48083
|
782 |
private void cleanList(List<ByteBuffer> l) {
|
|
783 |
synchronized (l) {
|
|
784 |
Iterator<ByteBuffer> iter = l.iterator();
|
|
785 |
while (iter.hasNext()) {
|
|
786 |
ByteBuffer b = iter.next();
|
|
787 |
if (!b.hasRemaining() && b != SENTINEL) {
|
|
788 |
iter.remove();
|
|
789 |
}
|
|
790 |
}
|
|
791 |
}
|
|
792 |
}
|
|
793 |
|
|
794 |
/**
|
|
795 |
* States for handshake. We avoid races when accessing/updating the AtomicInt
|
|
796 |
* because updates always schedule an additional call to both the read()
|
|
797 |
* and write() functions.
|
|
798 |
*/
|
|
799 |
private static final int NOT_HANDSHAKING = 0;
|
|
800 |
private static final int HANDSHAKING = 1;
|
49765
|
801 |
|
48083
|
802 |
private static final int DOING_TASKS = 4; // bit added to above state
|
|
803 |
private static final ByteBuffer HS_TRIGGER = ByteBuffer.allocate(0);
|
|
804 |
|
|
805 |
private static final int READER = 1;
|
|
806 |
private static final int WRITER = 2;
|
|
807 |
|
|
808 |
private static String states(AtomicInteger state) {
|
|
809 |
int s = state.get();
|
|
810 |
StringBuilder sb = new StringBuilder();
|
|
811 |
int x = s & ~DOING_TASKS;
|
|
812 |
switch (x) {
|
|
813 |
case NOT_HANDSHAKING:
|
|
814 |
sb.append(" NOT_HANDSHAKING ");
|
|
815 |
break;
|
|
816 |
case HANDSHAKING:
|
|
817 |
sb.append(" HANDSHAKING ");
|
|
818 |
break;
|
|
819 |
default:
|
|
820 |
throw new InternalError();
|
|
821 |
}
|
|
822 |
if ((s & DOING_TASKS) > 0)
|
|
823 |
sb.append("|DOING_TASKS");
|
|
824 |
return sb.toString();
|
|
825 |
}
|
|
826 |
|
|
827 |
private void resumeActivity() {
|
|
828 |
reader.schedule();
|
|
829 |
writer.schedule();
|
|
830 |
}
|
|
831 |
|
|
832 |
final AtomicInteger handshakeState;
|
49944
|
833 |
final ConcurrentLinkedQueue<String> stateList =
|
|
834 |
debug.on() ? new ConcurrentLinkedQueue<>() : null;
|
48083
|
835 |
|
49765
|
836 |
private boolean doHandshake(EngineResult r, int caller) {
|
|
837 |
// unconditionally sets the HANDSHAKING bit, while preserving DOING_TASKS
|
|
838 |
handshakeState.getAndAccumulate(HANDSHAKING, (current, update) -> update | (current & DOING_TASKS));
|
49944
|
839 |
if (stateList != null && debug.on()) {
|
|
840 |
stateList.add(r.handshakeStatus().toString());
|
|
841 |
stateList.add(Integer.toString(caller));
|
|
842 |
}
|
48083
|
843 |
switch (r.handshakeStatus()) {
|
|
844 |
case NEED_TASK:
|
49765
|
845 |
int s = handshakeState.getAndUpdate((current) -> current | DOING_TASKS);
|
48083
|
846 |
if ((s & DOING_TASKS) > 0) // someone else was doing tasks
|
49765
|
847 |
return false;
|
|
848 |
|
|
849 |
if (debug.on()) debug.log("obtaining and initiating task execution");
|
48083
|
850 |
List<Runnable> tasks = obtainTasks();
|
|
851 |
executeTasks(tasks);
|
49765
|
852 |
return false; // executeTasks will resume activity
|
48083
|
853 |
case NEED_WRAP:
|
49765
|
854 |
if (caller == READER) {
|
49944
|
855 |
writer.triggerWrite();
|
49765
|
856 |
return false;
|
|
857 |
}
|
48083
|
858 |
break;
|
|
859 |
case NEED_UNWRAP:
|
|
860 |
case NEED_UNWRAP_AGAIN:
|
|
861 |
// do nothing else
|
49765
|
862 |
// receiving-side data will trigger unwrap
|
48083
|
863 |
break;
|
|
864 |
default:
|
|
865 |
throw new InternalError("Unexpected handshake status:"
|
|
866 |
+ r.handshakeStatus());
|
|
867 |
}
|
49765
|
868 |
return true;
|
48083
|
869 |
}
|
|
870 |
|
|
871 |
private List<Runnable> obtainTasks() {
|
|
872 |
List<Runnable> l = new ArrayList<>();
|
|
873 |
Runnable r;
|
|
874 |
while ((r = engine.getDelegatedTask()) != null) {
|
|
875 |
l.add(r);
|
|
876 |
}
|
|
877 |
return l;
|
|
878 |
}
|
|
879 |
|
|
880 |
private void executeTasks(List<Runnable> tasks) {
|
49765
|
881 |
if (tasks.isEmpty())
|
|
882 |
return;
|
48083
|
883 |
exec.execute(() -> {
|
49765
|
884 |
try {
|
|
885 |
List<Runnable> nextTasks = tasks;
|
|
886 |
do {
|
|
887 |
nextTasks.forEach(Runnable::run);
|
48083
|
888 |
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
|
|
889 |
nextTasks = obtainTasks();
|
49765
|
890 |
} else {
|
|
891 |
break;
|
|
892 |
}
|
|
893 |
} while (true);
|
|
894 |
handshakeState.getAndUpdate((current) -> current & ~DOING_TASKS);
|
|
895 |
resumeActivity();
|
|
896 |
} catch (Throwable t) {
|
|
897 |
handleError(t);
|
|
898 |
}
|
48083
|
899 |
});
|
|
900 |
}
|
|
901 |
|
|
902 |
// FIXME: acknowledge a received CLOSE request from peer
|
|
903 |
EngineResult doClosure(EngineResult r) throws IOException {
|
49765
|
904 |
if (debug.on())
|
|
905 |
debug.log("doClosure(%s): %s [isOutboundDone: %s, isInboundDone: %s]",
|
|
906 |
r.result, engine.getHandshakeStatus(),
|
|
907 |
engine.isOutboundDone(), engine.isInboundDone());
|
48083
|
908 |
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
|
|
909 |
// we have received TLS close_notify and need to send
|
|
910 |
// an acknowledgement back. We're calling doHandshake
|
|
911 |
// to finish the close handshake.
|
|
912 |
if (engine.isInboundDone() && !engine.isOutboundDone()) {
|
49765
|
913 |
if (debug.on()) debug.log("doClosure: close_notify received");
|
48083
|
914 |
close_notify_received = true;
|
49944
|
915 |
if (!writer.scheduler.isStopped()) {
|
|
916 |
doHandshake(r, READER);
|
|
917 |
} else {
|
|
918 |
// We have received closed notify, but we
|
|
919 |
// won't be able to send the acknowledgement.
|
|
920 |
// Nothing more will come from the socket either,
|
|
921 |
// so mark the reader as completed.
|
|
922 |
synchronized (reader.readBufferLock) {
|
|
923 |
reader.completing = true;
|
|
924 |
}
|
|
925 |
}
|
48083
|
926 |
}
|
|
927 |
}
|
|
928 |
return r;
|
|
929 |
}
|
|
930 |
|
|
931 |
/**
|
|
932 |
* Returns the upstream Flow.Subscriber of the reading (incoming) side.
|
|
933 |
* This flow must be given the encrypted data read from upstream (eg socket)
|
|
934 |
* before it is decrypted.
|
|
935 |
*/
|
|
936 |
public Flow.Subscriber<List<ByteBuffer>> upstreamReader() {
|
|
937 |
return reader;
|
|
938 |
}
|
|
939 |
|
|
940 |
/**
|
|
941 |
* Returns the upstream Flow.Subscriber of the writing (outgoing) side.
|
|
942 |
* This flow contains the plaintext data before it is encrypted.
|
|
943 |
*/
|
|
944 |
public Flow.Subscriber<List<ByteBuffer>> upstreamWriter() {
|
|
945 |
return writer;
|
|
946 |
}
|
|
947 |
|
|
948 |
public boolean resumeReader() {
|
|
949 |
return reader.signalScheduling();
|
|
950 |
}
|
|
951 |
|
|
952 |
public void resetReaderDemand() {
|
|
953 |
reader.resetDownstreamDemand();
|
|
954 |
}
|
|
955 |
|
|
956 |
static class EngineResult {
|
|
957 |
final SSLEngineResult result;
|
|
958 |
final ByteBuffer destBuffer;
|
|
959 |
|
|
960 |
// normal result
|
|
961 |
EngineResult(SSLEngineResult result) {
|
|
962 |
this(result, null);
|
|
963 |
}
|
|
964 |
|
|
965 |
EngineResult(SSLEngineResult result, ByteBuffer destBuffer) {
|
|
966 |
this.result = result;
|
|
967 |
this.destBuffer = destBuffer;
|
|
968 |
}
|
|
969 |
|
|
970 |
boolean handshaking() {
|
|
971 |
HandshakeStatus s = result.getHandshakeStatus();
|
|
972 |
return s != HandshakeStatus.FINISHED
|
|
973 |
&& s != HandshakeStatus.NOT_HANDSHAKING
|
|
974 |
&& result.getStatus() != Status.CLOSED;
|
|
975 |
}
|
|
976 |
|
|
977 |
boolean needUnwrap() {
|
|
978 |
HandshakeStatus s = result.getHandshakeStatus();
|
|
979 |
return s == HandshakeStatus.NEED_UNWRAP;
|
|
980 |
}
|
|
981 |
|
|
982 |
|
|
983 |
int bytesConsumed() {
|
|
984 |
return result.bytesConsumed();
|
|
985 |
}
|
|
986 |
|
|
987 |
int bytesProduced() {
|
|
988 |
return result.bytesProduced();
|
|
989 |
}
|
|
990 |
|
|
991 |
SSLEngineResult.HandshakeStatus handshakeStatus() {
|
|
992 |
return result.getHandshakeStatus();
|
|
993 |
}
|
|
994 |
|
|
995 |
SSLEngineResult.Status status() {
|
|
996 |
return result.getStatus();
|
|
997 |
}
|
|
998 |
}
|
|
999 |
|
49944
|
1000 |
volatile int packetBufferSize;
|
|
1001 |
final ByteBuffer getNetBuffer() {
|
|
1002 |
int netSize = packetBufferSize;
|
|
1003 |
if (netSize <= 0) {
|
|
1004 |
packetBufferSize = netSize = engine.getSession().getPacketBufferSize();
|
|
1005 |
}
|
|
1006 |
return ByteBuffer.allocate(netSize);
|
48083
|
1007 |
}
|
|
1008 |
|
49944
|
1009 |
volatile int applicationBufferSize;
|
|
1010 |
final ByteBuffer getAppBuffer() {
|
|
1011 |
int appSize = applicationBufferSize;
|
|
1012 |
if (appSize <= 0) {
|
|
1013 |
applicationBufferSize = appSize = engine.getSession().getApplicationBufferSize();
|
|
1014 |
}
|
|
1015 |
return ByteBuffer.allocate(appSize);
|
48083
|
1016 |
}
|
|
1017 |
|
|
1018 |
final String dbgString() {
|
|
1019 |
return "SSLFlowDelegate(" + tubeName + ")";
|
|
1020 |
}
|
|
1021 |
}
|