private void saveNetBuf(TlsContext tctx) { int rem = tctx.getSslNetBuf().remaining(); byte[] b = new byte[rem]; tctx.getSslNetBuf().get(b); tctx.setSslReadChunk(b); if (rem == tctx.getSslNetBuf().capacity()) { // Did not consume anything; extend buffer by 1kB tctx.setSslNetBuf(ByteBuffer.allocate(tctx.getSslNetBuf().capacity() + 1024)); } }
if (tctx.getSslNetBuf() == null) { tctx.setSslNetBuf(ByteBuffer.allocate(tctx.getSslEngine().getSession().getPacketBufferSize())); tctx.getSslNetBuf().clear(); // clear for read if (tctx.getSslReadChunk() != null) { tctx.getSslNetBuf().put(tctx.getSslReadChunk()); tctx.setSslReadChunk(null); int read = ch.read(tctx.getSslNetBuf()); if (read > 0) { tctx.getSslNetBuf().flip(); while (tctx.getSslNetBuf().hasRemaining()) { buffer.clear(); log.traceNio(key, "sslDataRead()->unwrap()", "BEFORE"); SSLEngineResult result = tctx.getSslEngine().unwrap(tctx.getSslNetBuf(), buffer); log.traceNio(key, "sslDataRead()->unwrap()", result.getStatus().toString()); switch (result.getStatus()) { tctx.getSslNetBuf().clear(); } else {
if (tctx.getSslNetBuf() == null) { tctx.setSslNetBuf(ByteBuffer.allocate(engine.getSession().getPacketBufferSize())); tctx.getSslNetBuf().clear(); case NEED_UNWRAP: try { int rb = ch.read(tctx.getSslNetBuf()); if (rb < 0) { forceClose(key, atta, tctx, engine); return; tctx.getSslNetBuf().flip(); try { log.traceNio(key, "sslHandshake()->unwrap()", "BEFORE"); result = engine.unwrap(tctx.getSslNetBuf(), tctx.getSslAppBuf()); log.traceNio(key, "sslHandshake()->unwrap()", "AFTER"); tctx.getSslNetBuf().compact(); handshakeStatus = result.getHandshakeStatus(); } catch (SSLException e) { break; case BUFFER_UNDERFLOW: { tctx.setSslNetBuf(extendBuf(tctx.getSslNetBuf(), engine.getSession().getPacketBufferSize(), 0, true)); log.traceNio(key, "sslHandshake->unwrap()", "UNDERFLOW"); return; tctx.getSslNetBuf().clear(); try { log.traceNio(key, "sslHandshake()->wrap()", "BEFORE");
public void traceNio(SelectionKey key, String tag, String action) { if (isTraceEnabled()) { NetCtx atta = (NetCtx)key.attachment(); if (atta != null) { TlsContext tctx = atta.getTlsContext(); if (tctx != null) { trace(keyToString(key) + " " + tag + " " + action + ", state=" + tctx.getSslState() + ", netBuf=" + tctx.getSslNetBuf() + ", appBuf=" + tctx.getSslAppBuf()); } else { trace(keyToString(key) + " " + tag + " " + action + ", (no attachment)"); } } else { trace(keyToString(key) + " " + tag + " " + action + ", (no attachment)"); } } } }