2008-11-17 18:29:00 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2008-03-05 09:52:00 -05:00
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
2008-11-17 18:29:00 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// #define DEBUG_TCP
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2008-11-17 18:29:00 -05:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Threading;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
#if !SINGULARITY
|
|
|
|
using System.Net;
|
|
|
|
#endif
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
using Microsoft.Contracts;
|
|
|
|
|
|
|
|
using NetStack.Common;
|
|
|
|
using TcpError = NetStack.Contracts.TcpError;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
namespace NetStack.Runtime
|
|
|
|
{
|
|
|
|
using Protocols;
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
public enum TcpStateEnum
|
|
|
|
{
|
|
|
|
Undefined = 0,
|
|
|
|
Closed = 1,
|
|
|
|
Listen = 2,
|
|
|
|
SynRecvd = 3,
|
|
|
|
SynSent = 4,
|
|
|
|
Established = 5,
|
|
|
|
CloseWait = 6,
|
|
|
|
LastAck = 7,
|
|
|
|
FinWait1 = 8,
|
|
|
|
FinWait2 = 9,
|
|
|
|
Closing = 10,
|
|
|
|
}
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
internal class TcpState
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Names corresponding to Enums, above (reflection missing).
|
|
|
|
/// </summary>
|
|
|
|
internal static readonly string[] TcpStateNames = new string[] {
|
|
|
|
"Undefined",
|
|
|
|
"Closed ",
|
|
|
|
"Listen ",
|
|
|
|
"SynRecvd ",
|
|
|
|
"SynSent ",
|
|
|
|
"Establish",
|
|
|
|
"CloseWait",
|
|
|
|
"LastAck ",
|
|
|
|
"FinWait1 ",
|
|
|
|
"FinWait2 ",
|
|
|
|
"Closing ",
|
|
|
|
};
|
|
|
|
|
|
|
|
private static TcpState! instance = new TcpState();
|
|
|
|
|
|
|
|
internal static TcpState! InstanceOfUndefined()
|
|
|
|
{
|
|
|
|
return TcpState.instance;
|
|
|
|
}
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// ctor
|
|
|
|
internal TcpState()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a state transition
|
2008-11-17 18:29:00 -05:00
|
|
|
protected void ChangeState(TcpSession! tcpSession, TcpState newState)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ChangeState((!)newState);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// enter the new state
|
2008-11-17 18:29:00 -05:00
|
|
|
virtual internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// a new packet(=event) received, should handle it
|
|
|
|
// according to the current state.
|
2008-11-17 18:29:00 -05:00
|
|
|
virtual internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// leave the current state to the new one
|
|
|
|
virtual internal void OnStateLeave(TcpSession! tcpSession, TcpState newState)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// Handle timeout events.
|
2008-03-05 09:52:00 -05:00
|
|
|
// args is a session argument which is session specific
|
2008-11-17 18:29:00 -05:00
|
|
|
virtual internal NetStatus OnTimeout(TcpSession! tcpSession, Dispatcher.CallbackArgs args)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSessionEventsSource.EventLog.LogTimeout(
|
|
|
|
tcpSession.Uid,
|
|
|
|
tcpSession.currentState.StateEnum,
|
|
|
|
TcpSession.TcpTimeoutType.Unknown);
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// Get the state name for debugging
|
|
|
|
internal string GetStateName()
|
|
|
|
{
|
|
|
|
return TcpState.TcpStateNames[(int)this.StateEnum];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get state enum for eventing (tracing)
|
|
|
|
virtual internal TcpStateEnum GetStateEnum()
|
|
|
|
{
|
|
|
|
return TcpStateEnum.Undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// State's Enumeration as a Property
|
|
|
|
internal TcpStateEnum StateEnum
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
get { return this.GetStateEnum(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// State's Name as a Property
|
|
|
|
internal string StateName
|
|
|
|
{
|
|
|
|
get { return TcpState.TcpStateNames[(int)this.StateEnum]; }
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[ Conditional("DEBUG_TCP") ]
|
|
|
|
internal static void DebugPrint(string format, params object [] args)
|
|
|
|
{
|
|
|
|
Core.Log(format, args);
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
|
|
|
[ Conditional("DEBUG_TCP") ]
|
|
|
|
internal static void DebugPrintLine(string format, params object [] args)
|
|
|
|
{
|
|
|
|
TcpState.DebugPrint(format, args);
|
|
|
|
}
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
///
|
|
|
|
// This the TCP Finite State Machine Implementation
|
|
|
|
// NOTICE: This is just a quick implementation...
|
|
|
|
// should handle FIN, RST, window management, slow start, double acks...
|
|
|
|
//
|
2008-03-05 09:52:00 -05:00
|
|
|
internal class TCPFSM
|
|
|
|
{
|
|
|
|
|
|
|
|
// Definition of the TCP states
|
|
|
|
internal static TcpState! CLOSED { get {return TCPStateClosed.Instance();}}
|
|
|
|
|
|
|
|
internal static TcpState! LISTEN { get {return TCPStateListen.Instance();}}
|
|
|
|
internal static TcpState! SYN_RECVD { get {return TCPStateSynRcv.Instance();}}
|
|
|
|
internal static TcpState! SYN_SENT { get {return TCPStateSynSent.Instance();}}
|
|
|
|
internal static TcpState! ESTABLISHED { get {return TCPStateEstab.Instance();}}
|
|
|
|
internal static TcpState! CLOSE_WAIT { get {return TCPCloseWait.Instance();}}
|
|
|
|
internal static TcpState! LAST_ACK { get {return TCPLastAck.Instance();}}
|
|
|
|
|
|
|
|
internal static TcpState! FIN_WAIT1 { get {return TCPFINWait1.Instance();}}
|
|
|
|
internal static TcpState! FIN_WAIT2 { get {return TCPFINWait2.Instance();}}
|
|
|
|
internal static TcpState! CLOSING { get {return TCPClosing.Instance();}}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// Compare two TCP sequence numbers:
|
|
|
|
// - means A < B, 0 means A == B and + means A > B
|
2008-03-05 09:52:00 -05:00
|
|
|
[Pure]
|
2008-11-17 18:29:00 -05:00
|
|
|
[Inline]
|
|
|
|
private static int TcpSequenceNumberCompare(uint seqA, uint seqB)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
// Exploit integer underflow to compare correctly even in the case
|
2008-11-17 18:29:00 -05:00
|
|
|
// of sequence number wraparound. This assumes the two numbers are
|
|
|
|
// always in the same half of the numberspace.
|
|
|
|
|
|
|
|
return unchecked((int)(unchecked(seqA - seqB)));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Pure]
|
|
|
|
internal static bool TCPSeqEQ(uint seqA, uint seqB)
|
|
|
|
{
|
|
|
|
return TCPFSM.TcpSequenceNumberCompare(seqA, seqB) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Pure]
|
|
|
|
internal static bool TCPSeqNEQ(uint seqA, uint seqB)
|
|
|
|
{
|
|
|
|
return TCPFSM.TcpSequenceNumberCompare(seqA, seqB) != 0;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Pure]
|
|
|
|
internal static bool TCPSeqLEQ(uint seqA, uint seqB)
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TCPFSM.TcpSequenceNumberCompare(seqA, seqB) <= 0;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Pure]
|
|
|
|
internal static bool TCPSeqLess(uint seqA, uint seqB)
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TCPFSM.TcpSequenceNumberCompare(seqA, seqB) < 0;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Pure]
|
|
|
|
internal static bool TCPSeqGEQ(uint seqA, uint seqB)
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TCPFSM.TcpSequenceNumberCompare(seqA, seqB) >= 0;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Pure]
|
|
|
|
internal static bool TCPSeqGreater(uint seqA, uint seqB)
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TCPFSM.TcpSequenceNumberCompare(seqA, seqB) > 0;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
internal sealed class TCPStateClosed : TcpState
|
|
|
|
{
|
|
|
|
static TCPStateClosed! instance = new TCPStateClosed();
|
|
|
|
static TCPStateClosed() {}
|
|
|
|
|
|
|
|
internal static TCPStateClosed! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum for eventing (tracing) and debugging.
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.Closed;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// enter the Closed state
|
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.DestroyRetransmitTimer();
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// leave the state
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateLeave(TcpSession! tcpSession, TcpState newState)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
assert newState != null;
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateLeave(tcpSession, newState);
|
|
|
|
switch (newState.StateEnum) {
|
|
|
|
case TcpStateEnum.Listen:
|
|
|
|
break;
|
|
|
|
case TcpStateEnum.SynSent:
|
|
|
|
if (!TCPFSM.SendSYN(tcpSession, TcpFormat.TCP_MSS)) {
|
|
|
|
// This should never happen; our outbound
|
|
|
|
// packet queue should never be empty
|
|
|
|
// while we're setting up a session
|
|
|
|
Debug.Assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TcpStateEnum.SynRecvd:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Core.Panic("TCP: Ses{0,3} ({1}) state machine error!",
|
|
|
|
tcpSession.Uid, this.StateName);
|
|
|
|
break;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnTimeout(TcpSession! tcpSession, Dispatcher.CallbackArgs args)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnTimeout(tcpSession, args);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
// A Closed session cannot handle any packets.
|
|
|
|
// BUGBUG: The parent class should make this the default behavior. // TODO
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// a passive connection listen state
|
|
|
|
internal sealed class TCPStateListen : TcpState
|
|
|
|
{
|
|
|
|
static TCPStateListen! instance = new TCPStateListen();
|
|
|
|
static TCPStateListen() {}
|
|
|
|
|
|
|
|
internal static TCPStateListen! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum for eventing (tracing) and debugging.
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.Listen;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession) {
|
|
|
|
DebugPrintLine("TCP: Ses{0,3} ({1}) Waiting for connections.",
|
|
|
|
tcpSession.Uid, this.StateName);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR; // we ignore it
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsAck(ref tcpHeader)) {
|
|
|
|
SendReset(tcpSession, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// we received a syn!
|
2008-11-17 18:29:00 -05:00
|
|
|
// create a new TCP session and initialize it (only if
|
|
|
|
// we have room) TBC: should use listen param for bound)
|
|
|
|
// new session's new state (OnEnter) will send syn-ack
|
|
|
|
// (this is the result of the ChangeState call).
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
TcpSession client =
|
2008-11-17 18:29:00 -05:00
|
|
|
(TcpSession!)tcpSession.Protocol.CreateSession();
|
|
|
|
client.passiveSession = tcpSession; // this is our "owner"
|
|
|
|
client.SetLocalEndPoint(tcpSession.LocalAddress,
|
|
|
|
tcpSession.LocalPort);
|
|
|
|
client.InitializeServerSession(tcpHeader.seq,
|
|
|
|
tcpHeader.window);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// we have access to the IP header from the packet overlapped
|
|
|
|
// context (TCPModule put it there)
|
2008-11-17 18:29:00 -05:00
|
|
|
IPFormat.IPHeader ipHeader =
|
|
|
|
packet.OverlapContext as IPFormat.IPHeader;
|
2008-03-05 09:52:00 -05:00
|
|
|
assert ipHeader != null;
|
|
|
|
|
|
|
|
client.SetRemoteEndPoint(ipHeader.Source,
|
|
|
|
tcpHeader.sourcePort);
|
|
|
|
|
|
|
|
// Save ourselves work if we should reject the session right away
|
2008-11-17 18:29:00 -05:00
|
|
|
if (tcpSession.AcceptQueueIsFull()) {
|
2008-03-05 09:52:00 -05:00
|
|
|
TcpSegment reset = CreateResetSegment(client, true);
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.Protocol.OnProtocolSend(reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// New session starts at syn-rcv. It immediately (from
|
|
|
|
// OnEntry) sends a SYN-Ack and starts the connect timer.
|
2008-03-05 09:52:00 -05:00
|
|
|
client.ChangeState(SYN_RECVD);
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// WE are left at the SAME state!
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
internal sealed class TCPStateSynRcv : TcpState
|
|
|
|
{
|
|
|
|
static TCPStateSynRcv! instance = new TCPStateSynRcv();
|
|
|
|
static TCPStateSynRcv() {}
|
|
|
|
|
|
|
|
internal static TCPStateSynRcv! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum for eventing (tracing)
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.SynRecvd;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
// send syn-ack
|
|
|
|
if (!SendSYNAck(tcpSession)) {
|
|
|
|
// This should never happen; our outbound packet queues
|
|
|
|
// should never be full while we're setting up a session.
|
|
|
|
Debug.Assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start timer to limit the time to establishing the new session
|
|
|
|
tcpSession.StartConnectTimer();
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
2008-03-05 09:52:00 -05:00
|
|
|
// all TCP states get the tcpHeader context
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
bool packetIsAcceptable =
|
|
|
|
IsSegmentAcceptable(tcpSession, ref tcpHeader, segmentSize);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!packetIsAcceptable) {
|
|
|
|
if (!TcpFormat.IsReset(ref tcpHeader)) {
|
|
|
|
// send an ACK; don't care about whether this works
|
|
|
|
// or not since we're discarding the packet anyhow.
|
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we accept the segment
|
2008-11-17 18:29:00 -05:00
|
|
|
Debug.Assert(tcpHeader.seq == tcpSession.sessionTCB.RCV.NXT);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// if we get a reset
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
|
|
|
NetStatus.Code result;
|
|
|
|
switch (tcpSession.oldStateEnum) {
|
|
|
|
case TcpStateEnum.Listen:
|
|
|
|
// if we came from the listen state (passive open)
|
|
|
|
// the reset will return us to the Listen state
|
|
|
|
tcpSession.FlushRetransmissions();
|
|
|
|
ChangeState(tcpSession, LISTEN);
|
|
|
|
result = NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
break;
|
|
|
|
case TcpStateEnum.SynSent:
|
|
|
|
// we came from SYN_SENT (active open)
|
|
|
|
// the connection was refused, close it!
|
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.Refused);
|
|
|
|
result = NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
break;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
return result;
|
|
|
|
}
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession,
|
|
|
|
CreateResetSegment(tcpSession, false),
|
|
|
|
TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsAck(ref tcpHeader)) {
|
|
|
|
if (TCPSeqLEQ(tcpSession.sessionTCB.SND.UNA, tcpHeader.ackSeq) &&
|
|
|
|
TCPSeqGEQ(tcpSession.sessionTCB.SND.NXT, tcpHeader.ackSeq))
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
// enter established state
|
|
|
|
|
|
|
|
// we get an Ack for our syn-ack
|
|
|
|
// remove the SYN-ACK from the retransmit Q
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.HandleTCPAck(tcpSession, ref tcpHeader);
|
|
|
|
AttemptToEnterESTABLISHED(tcpSession, ref tcpHeader);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
// reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession,
|
|
|
|
CreateResetSegment(tcpSession, false),
|
|
|
|
TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsFIN(ref tcpHeader)) {
|
|
|
|
DebugPrintLine("TCP: Ses{0,3} ({1)} TCPStateSynRcv: Received FIN.",
|
|
|
|
tcpSession.Uid, this.StateName);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// RCV.NXT should reflect the data that we processed out of
|
|
|
|
// this packet, but not the FIN. Advance over the FIN.
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.RCV.NXT += 1;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ack the FIN. Don't worry if this doesn't actually work;
|
|
|
|
// the remote side will think the ACK got lost and retransmit,
|
|
|
|
// which we will hopefully be able to successfully ACK later.
|
2008-11-17 18:29:00 -05:00
|
|
|
SendAck(tcpSession);
|
|
|
|
ChangeState(tcpSession, CLOSE_WAIT);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
internal sealed class TCPStateSynSent : TcpState
|
|
|
|
{
|
|
|
|
static TCPStateSynSent! instance = new TCPStateSynSent();
|
|
|
|
static TCPStateSynSent() {}
|
|
|
|
|
|
|
|
internal static TCPStateSynSent! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum for eventing (tracing) and debugging.
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.SynSent;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
DebugPrintLine("TCP: Ses{0,3} ({1}) Sending SYN.",
|
|
|
|
tcpSession.Uid, this.StateName);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// we sent a syn we wait for syn-ack
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
2008-03-05 09:52:00 -05:00
|
|
|
// all TCP states get the tcpHeader context
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
2008-03-05 09:52:00 -05:00
|
|
|
bool ackAcceptable=false;
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsAck(ref tcpHeader)) {
|
|
|
|
if (TCPSeqLEQ(tcpHeader.ackSeq, tcpSession.sessionTCB.SND.ISS) ||
|
|
|
|
TCPSeqGreater(tcpHeader.ackSeq, tcpSession.sessionTCB.SND.NXT))
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
SendReset(tcpSession, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLEQ(tcpSession.sessionTCB.SND.UNA, tcpHeader.ackSeq) &&
|
|
|
|
TCPSeqLEQ(tcpHeader.ackSeq, tcpSession.sessionTCB.SND.NXT))
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
ackAcceptable=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
|
|
|
if (ackAcceptable) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// connection was reset, drop the segment
|
|
|
|
// and close the connection.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// check syn
|
|
|
|
// ack is is ok or there is no ack and no RST
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
|
|
|
if (ackAcceptable) {
|
|
|
|
DebugPrintLine("TCP: Ses{0,3} ({1}) SYNSENT, Received SYN-ACK",
|
|
|
|
tcpSession.Uid, this.StateName);
|
|
|
|
|
|
|
|
// use the ack parameters to complete the session's data
|
|
|
|
tcpSession.sessionTCB.RCV.NXT = tcpHeader.seq+1;
|
|
|
|
tcpSession.sessionTCB.RCV.IRS = tcpHeader.seq;
|
|
|
|
tcpSession.sessionTCB.SND.UNA = tcpHeader.ackSeq;
|
|
|
|
tcpSession.sessionTCB.SND.WND = tcpHeader.window;
|
|
|
|
tcpSession.sessionTCB.RCV.WND = TcpFormat.TCP_MSS;
|
|
|
|
|
|
|
|
// now remove the SYN from the retransmit Q
|
|
|
|
// (so we don't retransmit it again)
|
|
|
|
TCPFSM.HandleTCPAck(tcpSession, ref tcpHeader);
|
|
|
|
|
|
|
|
if (tcpSession.sessionTCB.SND.UNA > tcpSession.sessionTCB.SND.ISS) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// our syn has been acked.
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// change the state to established!
|
2008-11-17 18:29:00 -05:00
|
|
|
AttemptToEnterESTABLISHED(tcpSession, ref tcpHeader);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
// received a new SYN (overlap connection)
|
|
|
|
// (see SYN_RECVD for more details)
|
|
|
|
|
|
|
|
// Create a new object to track the new session
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSession client =
|
|
|
|
(TcpSession!) (tcpSession.Protocol.CreateSession());
|
|
|
|
client.passiveSession = tcpSession; // this is our "owner"
|
|
|
|
client.SetLocalEndPoint(tcpSession.LocalAddress,
|
|
|
|
tcpSession.LocalPort);
|
|
|
|
client.InitializeServerSession(tcpHeader.seq,
|
|
|
|
tcpHeader.window);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// we have access to the IP header from the packet overlapped
|
|
|
|
// context (TCPModule put it there)
|
2008-11-17 18:29:00 -05:00
|
|
|
IPFormat.IPHeader ipHeader = packet.OverlapContext as IPFormat.IPHeader;
|
2008-03-05 09:52:00 -05:00
|
|
|
assert ipHeader!=null;
|
|
|
|
client.SetRemoteEndPoint(ipHeader.Source,
|
|
|
|
tcpHeader.sourcePort);
|
|
|
|
|
|
|
|
// Save ourselves work if we should reject the session right away
|
2008-11-17 18:29:00 -05:00
|
|
|
if (tcpSession.AcceptQueueIsFull()) {
|
2008-03-05 09:52:00 -05:00
|
|
|
TcpSegment reset = CreateResetSegment(client, true);
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.Protocol.OnProtocolSend(reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// New session starts at syn-rcv. It immediately (from
|
|
|
|
// OnEntry) sends a SYN-Ack and starts the connect timer.
|
2008-03-05 09:52:00 -05:00
|
|
|
client.ChangeState(SYN_RECVD);
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
} // TCPStateSynSent
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// This is the working mode state
|
|
|
|
internal sealed class TCPStateEstab : TcpState
|
|
|
|
{
|
|
|
|
static TCPStateEstab! instance = new TCPStateEstab();
|
|
|
|
static TCPStateEstab() {}
|
|
|
|
|
|
|
|
internal static TCPStateEstab! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum for eventing (tracing) and debugging.
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.Established;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
// When becoming Established, stop the connect timer
|
|
|
|
// if there is one ticking.
|
|
|
|
tcpSession.DestroyConnectTimer();
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// Wake up anyone waiting for the connection to complete
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.setupCompleteEvent.Set();
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available; // TCPModule already shrinked it for us
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
NetStatus res = NetStatus.Code.PROTOCOL_DROP_ERROR;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
bool accept=IsSegmentAcceptable(tcpSession, ref tcpHeader, segmentSize);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!accept) {
|
|
|
|
if (!TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send an ACK and return
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// for simplicity, just close the connection.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check syn
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send a reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, CreateResetSegment(tcpSession, false), TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
res = HandleTCPData(ref tcpHeader, packet, tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// our peer wants to end the relationship...
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsFIN(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// RCV.NXT should reflect any data in this packet, but not the FIN.
|
|
|
|
// Advance over the FIN.
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.RCV.NXT += 1;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ack the FIN
|
2008-11-17 18:29:00 -05:00
|
|
|
SendAck(tcpSession);
|
|
|
|
ChangeState(tcpSession, CLOSE_WAIT);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// CLOSE_WAIT is entered when we receive (and ACK) a FIN from the
|
|
|
|
// remote side. There is no further data to receive, but we can
|
|
|
|
// continue sending if we like.
|
|
|
|
internal sealed class TCPCloseWait : TcpState
|
|
|
|
{
|
|
|
|
static TCPCloseWait! instance = new TCPCloseWait();
|
|
|
|
static TCPCloseWait() {}
|
|
|
|
|
|
|
|
internal static TCPCloseWait! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum (debug)
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.CloseWait;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateEnter(tcpSession);
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// Signal that there is nothing more to read on this
|
|
|
|
// connection
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ValidForRead = false;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!IsSegmentAcceptable(tcpSession, ref tcpHeader, segmentSize)) {
|
|
|
|
if (!TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send an ACK and return
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TcpFormat.IsReset(ref tcpHeader) ||
|
|
|
|
TcpFormat.IsSync(ref tcpHeader) ||
|
|
|
|
(!TcpFormat.IsAck(ref tcpHeader)))
|
|
|
|
{
|
|
|
|
// Abort
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.HandleTCPAck(tcpSession, ref tcpHeader);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_OK;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// LAST_ACK is entered when we close our side of the duplex
|
|
|
|
// connection and the remote site had sent us its FIN
|
|
|
|
// previously. We send out our own FIN and just hang around
|
|
|
|
// to make sure it was received properly before shutting down.
|
|
|
|
//
|
|
|
|
// CALLER SHOULD ARRANGE TO TRANSMIT THE FIN PACKET BEFORE
|
|
|
|
// ENTERING THIS STATE!
|
|
|
|
internal sealed class TCPLastAck : TcpState
|
|
|
|
{
|
|
|
|
static TCPLastAck! instance = new TCPLastAck();
|
|
|
|
static TCPLastAck() {}
|
|
|
|
|
|
|
|
internal static TCPLastAck! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum for eventing (tracing) and debugging.
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.LastAck;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateEnter(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// Flag that writing is now disallowed on this session
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ValidForWrite = false;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcbSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available; // TCPModule already shrinked it for us
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// RST is illegal at this point
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// for simplicity, just close the connection.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcbSession, null, TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SYNC causes us to issue a RST and abort
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send a reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcbSession,
|
|
|
|
CreateResetSegment(tcbSession, false),
|
|
|
|
TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!IsSegmentAcceptable(tcbSession, ref tcpHeader, segmentSize)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// Just drop it
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do ACK housekeeping
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLess(tcbSession.sessionTCB.SND.UNA, tcpHeader.ackSeq) &&
|
|
|
|
TCPSeqLEQ(tcpHeader.ackSeq, tcbSession.sessionTCB.SND.NXT))
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
// This appears to be ACKing something we sent.
|
2008-11-17 18:29:00 -05:00
|
|
|
tcbSession.sessionTCB.SND.UNA = tcpHeader.ackSeq;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// remove the packet(s) from the retransmit queue
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.HandleTCPAck(tcbSession, ref tcpHeader);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note the weirdness here; because we prepacketize
|
|
|
|
// data, we don't want to compare to SND.NXT
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqGEQ(tcbSession.sessionTCB.SND.UNA, tcbSession.sessionTCB.SND.NextSeq)) {
|
|
|
|
// The remote site has acknowledged everything we sent.
|
|
|
|
// We're done.
|
|
|
|
HandleTerminateSession(tcbSession, null, TcpError.Reset);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// FIN_WAIT1 is entered when we close our side of the duplex
|
|
|
|
// connection. We don't send any more data, but we continue to
|
|
|
|
// receive data from the remote side.
|
|
|
|
//
|
|
|
|
// CALLER SHOULD ARRANGE TO SEND THE FIN PACKET BEFORE ENTERING
|
|
|
|
// THIS STATE!
|
|
|
|
internal sealed class TCPFINWait1 : TcpState
|
|
|
|
{
|
|
|
|
static TCPFINWait1! instance = new TCPFINWait1();
|
|
|
|
static TCPFINWait1() {}
|
|
|
|
|
|
|
|
internal static TCPFINWait1! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum (debug)
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.FinWait1;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateEnter(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// Flag that writing is now disallowed on this session
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ValidForWrite = false;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// leave the state
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateLeave(TcpSession! tcpSession, TcpState newState)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
assert newState != null;
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateLeave(tcpSession, newState);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession, NetPacket! packet, object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available; // TCPModule already shrinked it for us
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// RST is illegal at this point
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// for simplicity, just close the connection.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SYNC causes us to issue a RST and abort
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send a reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, CreateResetSegment(tcpSession, false), TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!IsSegmentAcceptable(tcpSession, ref tcpHeader, segmentSize)) {
|
|
|
|
if (!TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send an ACK and return
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chew on the payload...
|
2008-11-17 18:29:00 -05:00
|
|
|
NetStatus retval = HandleTCPData(ref tcpHeader, packet, tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLEQ(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT) &&
|
2008-03-05 09:52:00 -05:00
|
|
|
(TcpFormat.IsFIN(ref tcpHeader)))
|
|
|
|
{
|
|
|
|
// This is the remote side's FIN, and we have
|
|
|
|
// received all data preceding it, too!
|
|
|
|
// advance RCV.NXT over the FIN sequence
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.RCV.NXT += 1;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// Note the weirdness here; because we
|
2008-03-05 09:52:00 -05:00
|
|
|
// prepacketize data, we don't want to test
|
|
|
|
// against SND.NXT
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLess(tcpSession.sessionTCB.SND.UNA,
|
|
|
|
tcpSession.sessionTCB.SND.NextSeq)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// ...but the remote side hasn't received
|
|
|
|
// everything we have said, yet.
|
|
|
|
|
|
|
|
// ack the FIN
|
2008-11-17 18:29:00 -05:00
|
|
|
SendAck(tcpSession);
|
|
|
|
ChangeState(tcpSession, CLOSING);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
// The remote side has heard everything we
|
|
|
|
// have to say. We're done.
|
|
|
|
// ACK the FIN and shut down
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment ackSeg = CreateAckSegment(tcpSession);
|
|
|
|
HandleTerminateSession(tcpSession, ackSeg, TcpError.Closed);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
// Note the weirdness here; because we
|
|
|
|
// prepacketize data, we do not want to test
|
|
|
|
// against SND.NXT, but rather SND.NextSeq.
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqGEQ(tcpSession.sessionTCB.SND.UNA,
|
|
|
|
tcpSession.sessionTCB.SND.NextSeq)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// The remote side has ACKed everything we have
|
|
|
|
// said, but they haven't FINed themselves.
|
2008-11-17 18:29:00 -05:00
|
|
|
ChangeState(tcpSession, FIN_WAIT2);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// FIN_WAIT2 is entered when the other side has received our FIN, but has
|
|
|
|
// more data to send. We wait for them to signal FIN as well.
|
|
|
|
internal sealed class TCPFINWait2 : TcpState
|
|
|
|
{
|
|
|
|
static TCPFINWait2! instance = new TCPFINWait2();
|
|
|
|
static TCPFINWait2() {}
|
|
|
|
|
|
|
|
internal static TCPFINWait2! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum (debug)
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.FinWait2;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// leave the state
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateLeave(TcpSession! tcpSession, TcpState newState)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateLeave(tcpSession, newState);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available; // TCPModule already shrinked it for us
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// RST is illegal at this point
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// for simplicity, just close the connection.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession,
|
|
|
|
null,
|
|
|
|
TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SYNC causes us to issue a RST and abort
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send a reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession,
|
|
|
|
CreateResetSegment(tcpSession, false),
|
|
|
|
TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!IsSegmentAcceptable(tcpSession, ref tcpHeader, segmentSize)) {
|
|
|
|
if (!TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send an ACK and return
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chew on the payload...
|
2008-11-17 18:29:00 -05:00
|
|
|
NetStatus retval = HandleTCPData(ref tcpHeader, packet, tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// Only check for FIN if we have previously received
|
|
|
|
// all data.
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLEQ(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT) &&
|
2008-03-05 09:52:00 -05:00
|
|
|
(TcpFormat.IsFIN(ref tcpHeader)))
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
// RCV.NXT should reflect the data in this packet, but not
|
|
|
|
// the FIN. Advance over the FIN...
|
|
|
|
tcpSession.sessionTCB.RCV.NXT += 1;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ack the FIN and shut down
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment ackSeg = CreateAckSegment(tcpSession);
|
|
|
|
HandleTerminateSession(tcpSession, ackSeg, TcpError.Closed);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// CLOSING is entered when both sides have sent a FIN, but we're not sure
|
|
|
|
// the other side has received all our data yet. We hang around processing
|
|
|
|
// ACKs until we're sure the other side has heard everything we have to say.
|
|
|
|
internal sealed class TCPClosing : TcpState
|
|
|
|
{
|
|
|
|
static TCPClosing! instance = new TCPClosing();
|
|
|
|
static TCPClosing() {}
|
|
|
|
|
|
|
|
internal static TCPClosing! Instance()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// get state enum (debug)
|
|
|
|
override internal TcpStateEnum GetStateEnum()
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
return TcpStateEnum.Closing;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateEnter(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateEnter(tcpSession);
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// Signal that there's nothing more to read on this session
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ValidForRead = false;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// leave the state
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal void OnStateLeave(TcpSession! tcpSession, TcpState newState)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
assert newState != null;
|
2008-11-17 18:29:00 -05:00
|
|
|
base.OnStateLeave(tcpSession, newState);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
override internal NetStatus OnPacketReceive(TcpSession! tcpSession,
|
|
|
|
NetPacket! packet,
|
|
|
|
object context)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
assert context != null;
|
|
|
|
TcpFormat.TcpHeader tcpHeader = (TcpFormat.TcpHeader)context;
|
|
|
|
uint segmentSize = (uint)packet.Available; // TCPModule already shrinked it for us
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// RST is illegal at this point
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsReset(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// for simplicity, just close the connection.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SYNC causes us to issue a RST and abort
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsSync(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// send a reset
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession,
|
|
|
|
CreateResetSegment(tcpSession, false),
|
|
|
|
TcpError.ProtocolViolation);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!IsSegmentAcceptable(tcpSession, ref tcpHeader, segmentSize)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// Just drop it
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do ACK housekeeping
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLess(tcpSession.sessionTCB.SND.UNA, tcpHeader.ackSeq) &&
|
|
|
|
TCPSeqLEQ(tcpHeader.ackSeq, tcpSession.sessionTCB.SND.NXT))
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
// This appears to be ACKing something we sent.
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.SND.UNA = tcpHeader.ackSeq;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// remove the packet(s) from the retransmit queue
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.HandleTCPAck(tcpSession, ref tcpHeader);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note the weirdness here; because we
|
|
|
|
// prepacketize data, we don't want to compare against
|
|
|
|
// SND.NXT
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqGEQ(tcpSession.sessionTCB.SND.UNA, tcpSession.sessionTCB.SND.NextSeq)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// The remote side has now heard everything we said.
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession, null, TcpError.Closed);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Create an ACK for data we have received to date
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static TcpSegment! CreateAckSegment(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
byte[] ackBuffer = new byte[EthernetFormat.Size+IPFormat.Size+TcpFormat.Size];
|
|
|
|
|
|
|
|
TcpFormat.WriteTcpSegment(
|
2008-11-17 18:29:00 -05:00
|
|
|
ackBuffer, tcpSession.LocalPort, tcpSession.RemotePort,
|
|
|
|
tcpSession.sessionTCB.RCV.NXT, tcpSession.sessionTCB.SND.NextSeq,
|
|
|
|
TcpFormat.TCP_MSS, tcpSession.LocalAddress,
|
|
|
|
tcpSession.RemoteAddress, 0,
|
|
|
|
true, false, false, false, false);
|
|
|
|
|
|
|
|
return new TcpSegment(ackBuffer, tcpSession,
|
|
|
|
tcpSession.sessionTCB.SND.NextSeq, true);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// sends an ack
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static bool SendAck(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment ackSeg = CreateAckSegment(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// put it on this session outgoing queue
|
2008-11-17 18:29:00 -05:00
|
|
|
return tcpSession.PutPacket(tcpSession.outQueue, ackSeg, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends a FIN
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static bool SendFin(TcpSession! tcpSession, bool canBlock)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
byte[] finBuffer = new byte[EthernetFormat.Size+IPFormat.Size+TcpFormat.Size];
|
|
|
|
|
|
|
|
TcpFormat.WriteTcpSegment(
|
2008-11-17 18:29:00 -05:00
|
|
|
finBuffer, tcpSession.LocalPort, tcpSession.RemotePort, tcpSession.sessionTCB.RCV.NXT,
|
|
|
|
tcpSession.sessionTCB.SND.NextSeq, TcpFormat.TCP_MSS, tcpSession.LocalAddress, tcpSession.RemoteAddress, 0,
|
|
|
|
true, false, false, true, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ok, we have a ready segment.
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment syn = new TcpSegment(finBuffer, tcpSession, tcpSession.sessionTCB.SND.NextSeq, true);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
bool retVal = tcpSession.PutPacket(tcpSession.outQueue, syn, canBlock);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// Only advance the segment counter if we successfully queued the
|
|
|
|
// outbound packet
|
2008-11-17 18:29:00 -05:00
|
|
|
if (retVal) {
|
|
|
|
tcpSession.sessionTCB.SND.NextSeq++;
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sends a syn ack packet
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static bool SendSYNAck(TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
byte[] synackBuffer = new byte[EthernetFormat.Size+IPFormat.Size+TcpFormat.Size];
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpFormat.WriteTcpSegment(synackBuffer, tcpSession.LocalPort, tcpSession.RemotePort,
|
|
|
|
tcpSession.sessionTCB.RCV.NXT,
|
|
|
|
tcpSession.sessionTCB.SND.ISS,
|
|
|
|
(ushort)tcpSession.sessionTCB.SND.WND,
|
|
|
|
tcpSession.LocalAddress, tcpSession.RemoteAddress,
|
|
|
|
0, true, true, false, false, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ok, we have a ready segment.
|
|
|
|
// (SYN is regular segment)
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment syn = new TcpSegment(synackBuffer, tcpSession, tcpSession.sessionTCB.SND.ISS, true);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// put it on this session's outgoing queue
|
|
|
|
return tcpSession.PutPacket(tcpSession.outQueue, syn, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// sends a syn packet, with MSS options
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static bool SendSYN(TcpSession! tcpSession, ushort MSS)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
byte[] synBuffer = new byte[EthernetFormat.Size+IPFormat.Size+TcpFormat.Size+4];
|
|
|
|
|
|
|
|
// setup the session
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.SND.ISS=(uint)DateTime.UtcNow.Ticks;
|
|
|
|
tcpSession.sessionTCB.SND.UNA=tcpSession.sessionTCB.SND.ISS;
|
|
|
|
tcpSession.sessionTCB.SND.NXT=tcpSession.sessionTCB.SND.ISS+1; // next packet sequence
|
|
|
|
tcpSession.sessionTCB.SND.NextSeq = tcpSession.sessionTCB.SND.NXT;
|
|
|
|
tcpSession.sessionTCB.SND.WND=TcpFormat.TCP_MSS;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// we must first write the data...
|
2008-11-17 18:29:00 -05:00
|
|
|
byte[] options=new byte[] {2, 4, ((byte)(MSS>>8)), (byte)MSS};
|
|
|
|
Array.Copy(options, 0, synBuffer, EthernetFormat.Size+IPFormat.Size+TcpFormat.Size, 4);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// now create the segment+checksum
|
|
|
|
TcpFormat.WriteTcpSegment(
|
2008-11-17 18:29:00 -05:00
|
|
|
synBuffer, tcpSession.LocalPort, tcpSession.RemotePort, 0,
|
|
|
|
tcpSession.sessionTCB.SND.ISS, MSS, tcpSession.LocalAddress, tcpSession.RemoteAddress, 4,
|
|
|
|
false, true, false, false, true);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ok, we have a ready segment.
|
|
|
|
// (SYN is regular segment)
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment syn = new TcpSegment(synBuffer, tcpSession, tcpSession.sessionTCB.SND.ISS, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// put it on this session outgoing queue
|
2008-11-17 18:29:00 -05:00
|
|
|
return tcpSession.PutPacket(tcpSession.outQueue, syn, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a TCP reset
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static bool SendReset(TcpSession! tcpSession, bool isAck)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
// we won't retransmit it (it is like an Ack)
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment syn = CreateResetSegment(tcpSession, true);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// put it on this session outgoing queue
|
2008-11-17 18:29:00 -05:00
|
|
|
return tcpSession.PutPacket(tcpSession.outQueue, syn, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a TCP reset
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static TcpSegment! CreateResetSegment(TcpSession! tcpSession, bool isAck)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
byte[] rstBuffer = new byte[EthernetFormat.Size+IPFormat.Size+TcpFormat.Size];
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// NOTE: use SND.NXT as the sequence number here instead of nextSeq,
|
2008-03-05 09:52:00 -05:00
|
|
|
// otherwise the sequence number may be far ahead (if there's lots of queued
|
|
|
|
// data) and the receiving host may ignore it as outside its window.
|
|
|
|
TcpFormat.WriteTcpSegment(
|
2008-11-17 18:29:00 -05:00
|
|
|
rstBuffer, tcpSession.LocalPort, tcpSession.RemotePort, tcpSession.sessionTCB.RCV.NXT,
|
|
|
|
tcpSession.sessionTCB.SND.NXT, TcpFormat.TCP_MSS, tcpSession.LocalAddress,
|
|
|
|
tcpSession.RemoteAddress, 0, isAck, false, true, false, false);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// we won't retransmit it
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSegment syn = new TcpSegment(rstBuffer, tcpSession, tcpSession.sessionTCB.SND.NXT, true);
|
2008-03-05 09:52:00 -05:00
|
|
|
return syn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle received TCP data
|
|
|
|
// Returns the appropriate NetStatus.Code
|
|
|
|
internal static NetStatus HandleTCPData(ref TcpFormat.TcpHeader tcpHeader,
|
2008-11-17 18:29:00 -05:00
|
|
|
NetPacket! packet, TcpSession! tcpSession)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
uint segmentSize = (uint)packet.Available; // TCPModule already shrinked it for us
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// TBC: We assume the segment start with
|
|
|
|
// the RCV.NXT !!! (otherwise we can buffer them)
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqGreater(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// we missed one or few, send ack again
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!TcpFormat.IsAck(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, deal with the window advertised in this packet.
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.SND.WND = tcpHeader.window;
|
|
|
|
tcpSession.HandleWindowUpdate();
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// ack is set and it is relevant (ack something we sent)
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TCPSeqLess(tcpSession.sessionTCB.SND.UNA, tcpHeader.ackSeq) &&
|
|
|
|
TCPSeqLEQ(tcpHeader.ackSeq, tcpSession.sessionTCB.SND.NXT))
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.SND.UNA = tcpHeader.ackSeq;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// remove the packet(s) from the retransmit queue
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.HandleTCPAck(tcpSession, ref tcpHeader);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else if (tcpSession.sessionTCB.SND.UNA > tcpHeader.ackSeq) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// a duplicate ack
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else if (TCPSeqGreater(tcpHeader.ackSeq, tcpSession.sessionTCB.SND.NXT)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// ack for future data..
|
2008-11-17 18:29:00 -05:00
|
|
|
TCPFSM.SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check URG bit
|
2008-11-17 18:29:00 -05:00
|
|
|
if (TcpFormat.IsUrg(ref tcpHeader)) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// TBC
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// check PUSH bit
|
|
|
|
if (TcpFormat.IsPush(ref tcpHeader)) {
|
|
|
|
// TBC
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// at last, process the segment data!!!
|
|
|
|
// at the end send an ACK (TBC: change to accumulate acks)
|
2008-11-17 18:29:00 -05:00
|
|
|
if (segmentSize > 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// put the data in the session's inQ
|
2008-11-17 18:29:00 -05:00
|
|
|
if (tcpSession.PutPacket(tcpSession.inQueue, packet, false)) {
|
|
|
|
tcpSession.sessionTCB.RCV.NXT+=segmentSize; // we expect the next segment seq
|
|
|
|
tcpSession.sessionTCB.RCV.WND=TcpFormat.TCP_MSS; // TBC: change according to buffer
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// send our ack if we ack data
|
2008-11-17 18:29:00 -05:00
|
|
|
SendAck(tcpSession);
|
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// don't release the packet
|
|
|
|
return NetStatus.Code.PROTOCOL_PROCESSING;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
// packet was dropped...
|
|
|
|
// send our ack if we ack data
|
2008-11-17 18:29:00 -05:00
|
|
|
SendAck(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
// Payload was empty...
|
|
|
|
return NetStatus.Code.PROTOCOL_DROP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle a TCP received ack
|
|
|
|
// the ack is already checked for validity (by the actual state)
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static void HandleTCPAck(TcpSession! tcpSession,
|
|
|
|
ref TcpFormat.TcpHeader tcpHdr)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ACKThrough(tcpHdr.ackSeq);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if we can accept the segment
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static bool IsSegmentAcceptable(TcpSession! tcpSession,
|
|
|
|
ref TcpFormat.TcpHeader tcpHeader,
|
|
|
|
uint segmentSize)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
|
|
|
bool accept=false;
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
// first check sequence number
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((segmentSize == 0) && (tcpSession.sessionTCB.RCV.WND == 0)) {
|
|
|
|
accept = (tcpHeader.seq == tcpSession.sessionTCB.RCV.NXT);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else if ((segmentSize == 0) && (tcpSession.sessionTCB.RCV.WND > 0)) {
|
|
|
|
accept=(TCPSeqGEQ(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT) &&
|
|
|
|
TCPSeqLess(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT+tcpSession.sessionTCB.RCV.WND));
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
else if ((segmentSize > 0) && (tcpSession.sessionTCB.RCV.WND > 0)) {
|
|
|
|
accept=(TCPSeqGEQ(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT) &&
|
|
|
|
TCPSeqLess(tcpHeader.seq, tcpSession.sessionTCB.RCV.NXT+tcpSession.sessionTCB.RCV.WND));
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
accept = accept || (TCPSeqLEQ(tcpSession.sessionTCB.RCV.NXT, tcpHeader.seq+segmentSize-1) &&
|
2008-03-05 09:52:00 -05:00
|
|
|
TCPSeqLess(tcpHeader.seq +segmentSize - 1,
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.sessionTCB.RCV.NXT + tcpSession.sessionTCB.RCV.WND));
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
|
2008-03-05 09:52:00 -05:00
|
|
|
return accept;
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
private static void AttemptToEnterESTABLISHED(TcpSession! tcpSession,
|
2008-03-05 09:52:00 -05:00
|
|
|
ref TcpFormat.TcpHeader tcpHeader)
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
TcpSession passiveOwner = tcpSession.passiveSession;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (passiveOwner != null) {
|
|
|
|
bool success = passiveOwner.AddAcceptedSession(tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (!success) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// Oops; while this connection was being established
|
|
|
|
// we ran out of room in the accept queue. Abort
|
|
|
|
// the connection!
|
2008-11-17 18:29:00 -05:00
|
|
|
HandleTerminateSession(tcpSession,
|
|
|
|
CreateResetSegment(tcpSession, false),
|
|
|
|
TcpError.ResourcesExhausted);
|
2008-03-05 09:52:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ChangeState(ESTABLISHED);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// this method terminated the given session
|
|
|
|
// if nextSegment is not null than it is sent before final
|
|
|
|
// removal.
|
2008-11-17 18:29:00 -05:00
|
|
|
internal static void HandleTerminateSession(TcpSession! tcpSession,
|
|
|
|
TcpSegment nextSegment,
|
|
|
|
TcpError connectError)
|
2008-03-05 09:52:00 -05:00
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.StopPersistTimer();
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// 1. first clear the retransmit queue
|
|
|
|
// 2. make the session not valid for users!
|
|
|
|
// 3. release any user waiting for read/write on the session
|
|
|
|
// 4. clear the out/in queues
|
|
|
|
// 5. remove the session from TCP if it is no longer needed
|
|
|
|
// (when it considered to be closed)
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.FlushRetransmissions();
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// some user may wait on the q to write/read data
|
|
|
|
// we will release them by trigger the monitors.
|
|
|
|
// they will fail since Valid is false and they are users.
|
|
|
|
// only TCP can still use this session.
|
2008-11-17 18:29:00 -05:00
|
|
|
lock (tcpSession.outQueue.SyncRoot) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// from now on, every user thread will
|
|
|
|
// fail to read/write data
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ValidForRead = false;
|
|
|
|
tcpSession.ValidForWrite = false;
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.DrainQueue(tcpSession.outQueue);
|
2008-03-05 09:52:00 -05:00
|
|
|
if (nextSegment != null)
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.outQueue.Add(nextSegment);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// Don't forget these!
|
2008-11-17 18:29:00 -05:00
|
|
|
Monitor.PulseAll(tcpSession.outQueue.SyncRoot);
|
2008-03-05 09:52:00 -05:00
|
|
|
Core.Instance().SignalOutboundPackets();
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
lock (tcpSession.inQueue.SyncRoot) {
|
2008-03-05 09:52:00 -05:00
|
|
|
// Pulse anyone waiting to read data so they can notice
|
|
|
|
// they are unlikely to succeed, but don't clear the
|
|
|
|
// queue; if there is data on it, we may want to drain
|
|
|
|
// it, still.
|
2008-11-17 18:29:00 -05:00
|
|
|
Monitor.PulseAll(tcpSession.inQueue.SyncRoot);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if (nextSegment == null) {
|
|
|
|
IProtocol tcpProtocol = tcpSession.Protocol;
|
|
|
|
Core.Instance().DeregisterSession(tcpProtocol, tcpSession);
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// change the state to close
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.ChangeState(TCPFSM.CLOSED);
|
2008-03-05 09:52:00 -05:00
|
|
|
|
|
|
|
// Signal anyone who was waiting for this session to begin or end
|
2008-11-17 18:29:00 -05:00
|
|
|
tcpSession.connectError = connectError;
|
|
|
|
tcpSession.setupCompleteEvent.Set();
|
2008-03-05 09:52:00 -05:00
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
// NOTE: If we are transmitting a last-gasp packet,
|
2008-03-05 09:52:00 -05:00
|
|
|
// don't signal the session as closed just yet; wait for the packet
|
|
|
|
// to actually get transmitted.
|
2008-11-17 18:29:00 -05:00
|
|
|
if (nextSegment == null) {
|
|
|
|
tcpSession.closedEvent.Set();
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|