Skip to content

Commit

Permalink
SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
tywo45 committed Sep 28, 2023
1 parent 63cf4a4 commit ac2f1dd
Show file tree
Hide file tree
Showing 21 changed files with 428 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,14 @@ recommend that a file or class name and description of purpose be included on
import org.slf4j.LoggerFactory;
import org.tio.client.intf.TioClientListener;
import org.tio.core.ChannelContext.CloseCode;
import org.tio.core.TioConfig;
import org.tio.core.Node;
import org.tio.core.ReadCompletionHandler;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.core.ssl.SslFacadeContext;
import org.tio.core.ssl.SslUtils;
import org.tio.core.stat.IpStat;
import org.tio.utils.SystemTimer;
import org.tio.utils.hutool.CollUtil;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ recommend that a file or class name and description of purpose be included on
import org.tio.core.utils.ByteBufferUtils;
import org.tio.core.utils.TioUtils;
import org.tio.utils.SystemTimer;
import org.tio.utils.hutool.CollUtil;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ recommend that a file or class name and description of purpose be included on
import org.tio.core.intf.Packet.Meta;
import org.tio.core.stat.IpStat;
import org.tio.utils.SystemTimer;
import org.tio.utils.hutool.CollUtil;

/**
*
Expand Down
1 change: 0 additions & 1 deletion src/core/src/main/java/org/tio/core/intf/TioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ recommend that a file or class name and description of purpose be included on
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.exception.TioDecodeException;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,30 +214,31 @@ public ByteBuffer append(ByteBuffer data) {
return nb;
}

/**
* 把
* @param byteBuffer
*/
public void set(ByteBuffer byteBuffer) {
if (byteBuffer.hasRemaining()) {
b = ByteBuffer.allocate(byteBuffer.remaining());
b.put(byteBuffer);
b.rewind();
}
}

public void clear() {
b = null;
}

public ByteBuffer get() {
return b;
}

public boolean hasRemaining() {
if (b != null) {
return b.hasRemaining();
}
return false;
}

public ByteBuffer get() {
return b;
/**
* 把
*
* @param byteBuffer
*/
public void set(ByteBuffer byteBuffer) {
if (byteBuffer.hasRemaining()) {
b = ByteBuffer.allocate(byteBuffer.remaining());
b.put(byteBuffer);
b.rewind();
}
}
}
138 changes: 69 additions & 69 deletions src/core/src/main/java/org/tio/core/ssl/facade/Buffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,53 @@ public Buffers(SSLSession ssLSession, ChannelContext channelContext) {
waitUnwrapBuffer = new AppendableBuffer();
}

// private void debug(final String msg) {
// System.out.println("[Buffers]" + msg);
// System.out.flush();
// }
// private void debug(final String msg) {
// System.out.println("[Buffers]" + msg);
// System.out.flush();
// }

/* private */
private void allocate() {
int applicationBufferSize = ssLSession.getApplicationBufferSize();
int packetBufferSize = ssLSession.getPacketBufferSize();
_peerApp = ByteBuffer.allocate(applicationBufferSize);
_myApp = ByteBuffer.allocate(applicationBufferSize);
_peerNet = ByteBuffer.allocate(packetBufferSize);
_myNet = ByteBuffer.allocate(packetBufferSize);
}

private void assign(BufferType t, ByteBuffer b) {
switch (t) {

case IN_PLAIN:
_peerApp = b;
break;
case IN_CIPHER:
_peerNet = b;
break;
case OUT_PLAIN:
_myApp = b;
break;
case OUT_CIPHER:
_myNet = b;
break;
}
}

void cache(ByteBuffer data) {
if (data != null) {
waitUnwrapBuffer.set(data);
}
}

private void clear(BufferType source, BufferType destination) {
get(source).clear();
get(destination).clear();
}

void clearCache() {
waitUnwrapBuffer.clear();
}

ByteBuffer get(BufferType t) {
ByteBuffer result = null;
Expand Down Expand Up @@ -294,7 +337,7 @@ void grow(BufferType t) {
assign(t, grow(t, ssLSession.getPacketBufferSize()));
break;
case OUT_PLAIN:
//No known reason for this case to occur
// No known reason for this case to occur
break;
case OUT_CIPHER:
assign(t, grow(t, ssLSession.getPacketBufferSize()));
Expand All @@ -308,14 +351,30 @@ ByteBuffer grow(BufferType b, int recommendedBufferSize) {
ByteBuffer newBuffer = ByteBuffer.allocate(recommendedBufferSize);

try {
//debug("grow buffer " + originalBuffer + " to " + newBuffer);
// debug("grow buffer " + originalBuffer + " to " + newBuffer);
BufferUtils.copy(originalBuffer, newBuffer);
} catch (BufferOverflowException e) {
throw e;
}
return newBuffer;
}

private ByteBuffer growIfNecessary(BufferType t, int size) {
// grow if not enough space
ByteBuffer b = get(t);
// log.info("growIfNecessary {}, size:{}", b, size);
// System.out.println("Grow " + t + " : " + b + " size=" + size);
if (b.position() + size > b.capacity()) {
// System.out.println("Grow");
resetSize(t, b.limit() + size);
}
return get(t);
}

boolean isCacheEmpty() {
return !waitUnwrapBuffer.hasRemaining();
}

void prepareForUnwrap(ByteBuffer data) {
clear(BufferType.IN_CIPHER, BufferType.IN_PLAIN);
if (data != null) {
Expand All @@ -324,7 +383,7 @@ void prepareForUnwrap(ByteBuffer data) {
newBuffer.put(data);
newBuffer.flip();
} catch (Exception e) {
log.error(e.toString() + ", data: " + data + ", BufferType.IN_CIPHER:" + get(BufferType.IN_CIPHER), e);
log.error("" + ", data: " + data + ", BufferType.IN_CIPHER:" + get(BufferType.IN_CIPHER), e);
throw new RuntimeException(e);
}
}
Expand All @@ -335,7 +394,7 @@ void prepareForUnwrap(ByteBuffer data) {
* @param plainData 待加密的ByteBuffer
*/
void prepareForWrap(ByteBuffer plainData) {
//Avoid buffer overflow when loading plain data and clear buffers
// Avoid buffer overflow when loading plain data and clear buffers
clear(BufferType.OUT_PLAIN, BufferType.OUT_CIPHER);
if (plainData != null) {
ByteBuffer newBuffer = growIfNecessary(BufferType.OUT_PLAIN, plainData.limit());
Expand All @@ -355,74 +414,15 @@ ByteBuffer prependCached(ByteBuffer data) {
}
}

void cache(ByteBuffer data) {
if (data != null) {
waitUnwrapBuffer.set(data);
}
}

void clearCache() {
waitUnwrapBuffer.clear();
}

boolean isCacheEmpty() {
return !waitUnwrapBuffer.hasRemaining();
}

/* private */
private void allocate() {
int applicationBufferSize = ssLSession.getApplicationBufferSize();
int packetBufferSize = ssLSession.getPacketBufferSize();
_peerApp = ByteBuffer.allocate(applicationBufferSize);
_myApp = ByteBuffer.allocate(applicationBufferSize);
_peerNet = ByteBuffer.allocate(packetBufferSize);
_myNet = ByteBuffer.allocate(packetBufferSize);
}

private void clear(BufferType source, BufferType destination) {
get(source).clear();
get(destination).clear();
}

private void assign(BufferType t, ByteBuffer b) {
switch (t) {

case IN_PLAIN:
_peerApp = b;
break;
case IN_CIPHER:
_peerNet = b;
break;
case OUT_PLAIN:
_myApp = b;
break;
case OUT_CIPHER:
_myNet = b;
break;
}
}

private void resetSize(BufferType t, int size) {
ByteBuffer newBuffer = ByteBuffer.allocate(size);
try {
BufferUtils.copy(get(t), newBuffer);
assign(t, newBuffer);
// ByteBuffer ss = get(t);
// log.error("size:{}, newbytebuffer:{}", size, ss);
// ByteBuffer ss = get(t);
// log.error("size:{}, newbytebuffer:{}", size, ss);
} catch (BufferOverflowException e) {
throw e;
}
}

private ByteBuffer growIfNecessary(BufferType t, int size) {
//grow if not enough space
ByteBuffer b = get(t);
// log.info("growIfNecessary {}, size:{}", b, size);
// System.out.println("Grow " + t + " : " + b + " size=" + size);
if (b.position() + size > b.capacity()) {
// System.out.println("Grow");
resetSize(t, b.limit() + size);
}
return get(t);
}
}
Loading

0 comments on commit ac2f1dd

Please sign in to comment.