Skip to content

Commit

Permalink
Add graceful TCP termination and remove useless TCP window full check
Browse files Browse the repository at this point in the history
  • Loading branch information
LipiLee committed Apr 30, 2018
1 parent 9704cc2 commit a511e0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
30 changes: 20 additions & 10 deletions app/src/main/java/com/lipisoft/toyshark/SessionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ private void handleTCPPacket(ByteBuffer clientPacketData, IPv4Header ipHeader, T
Session session = SessionManager.INSTANCE.getSessionByKey(key);

if(session == null) {
Log.e(TAG,"**** ==> Session not found: " + key);
if(!tcpheader.isRST() && !tcpheader.isFIN()){
if (tcpheader.isFIN()) {
sendLastAck(ipHeader, tcpheader);
} else if (!tcpheader.isRST()) {
sendRstPacket(ipHeader, tcpheader, dataLength);
}
else {
Log.e(TAG,"**** ==> Session not found: " + key);
}
return;
}

Expand Down Expand Up @@ -208,6 +212,20 @@ private void sendRstPacket(IPv4Header ip, TCPHeader tcp, int dataLength){
Log.e(TAG,"failed to send RST packet: " + e.getMessage());
}
}

private void sendLastAck(IPv4Header ip, TCPHeader tcp){
byte[] data = TCPPacketFactory.createResponseAckData(ip, tcp, tcp.getSequenceNumber()+1);
try {
writer.write(data);
packetData.addData(data);
Log.d(TAG,"Sent RST Packet to client with dest => " +
PacketUtil.intToIPAddress(ip.getDestinationIP()) + ":" +
tcp.getDestinationPort());
} catch (IOException e) {
Log.e(TAG,"failed to send RST packet: " + e.getMessage());
}
}

private void ackFinAck(IPv4Header ip, TCPHeader tcp, Session session){
//TODO: check if client send only FIN without ACK
long ack = tcp.getSequenceNumber() + 1;
Expand Down Expand Up @@ -331,14 +349,6 @@ private void acceptAck(IPv4Header ipHeader, TCPHeader tcpHeader, Session session
if(tcpHeader.getWindowSize() > 0){
session.setSendWindowSizeAndScale(tcpHeader.getWindowSize(), session.getSendWindowScale());
}
long byteReceived = tcpHeader.getAckNumber() - session.getSendUnack();
if(byteReceived > 0){
session.decreaseAmountSentSinceLastAck(byteReceived);
}
if(session.isClientWindowFull()){
Log.d(TAG,"window: "+session.getSendWindow()+" is full? "+session.isClientWindowFull() + " for "+PacketUtil.intToIPAddress(ipHeader.getDestinationIP())
+":"+tcpHeader.getDestinationPort()+"-"+PacketUtil.intToIPAddress(ipHeader.getSourceIP())+":"+tcpHeader.getSourcePort());
}
session.setSendUnack(tcpHeader.getAckNumber());
session.setRecSequence(tcpHeader.getSequenceNumber());
session.setTimestampReplyto(tcpHeader.getTimeStampSender());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static byte[] createFinData(IPv4Header ip, TCPHeader tcp, long ackNumber,
ip.setIdentification(PacketUtil.getPacketId());

tcp.setIsRST(false);
tcp.setIsACK(false);
tcp.setIsACK(true);
tcp.setIsSYN(false);
tcp.setIsPSH(false);
tcp.setIsCWR(false);
Expand Down Expand Up @@ -247,6 +247,7 @@ public static byte[] createResponseAckData(IPv4Header ipHeader, TCPHeader tcphea
tcp.setIsACK(true);
tcp.setIsSYN(false);
tcp.setIsPSH(false);
tcp.setIsFIN(false);

//set response timestamps in options fields
tcp.setTimeStampReplyTo(tcp.getTimeStampSender());
Expand Down

1 comment on commit a511e0e

@LipiLee
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2

Please sign in to comment.