private void BeginShutdownStack(TCPStreamModifierStack tsmsStack) { tsmsStack.StackAlice.TCPSocket.StateChange -= new EventHandler <TCPSocketEventArgs>(TCPSocket_StateChange); tsmsStack.StackBob.TCPSocket.StateChange -= new EventHandler <TCPSocketEventArgs>(TCPSocket_StateChange); tsmsStack.StackAlice.FrameEncapsulated -= new FrameProcessedEventHandler(Stack_FrameEncapsulated); tsmsStack.StackBob.FrameEncapsulated -= new FrameProcessedEventHandler(Stack_FrameEncapsulated); if (tsmsStack.StackAlice.TCPSocket.TCPState != TCPSocketState.Closed && tsmsStack.StackAlice.TCPSocket.TCPState != TCPSocketState.TimeWait) { tsmsStack.StackAlice.BeginClose(); } if (tsmsStack.StackBob.TCPSocket.TCPState != TCPSocketState.Closed && tsmsStack.StackBob.TCPSocket.TCPState != TCPSocketState.TimeWait) { tsmsStack.StackBob.BeginClose(); } foreach (NetworkStreamModifier sModifier in tsmsStack.Modifiers) { if (sModifier.IsRunning) { sModifier.AliceLoopError -= new ExceptionEventHandler(sModifier_AliceLoopError); sModifier.BobLoopError -= new ExceptionEventHandler(sModifier_BobLoopError); sModifier.StopAsync(); } } }
private void HandleOperatorException(object sender, ExceptionEventArgs args) { TCPStreamModifierStack tsmsStack = GetStackForOperator((NetworkStreamModifier)sender); if (tsmsStack == null) { throw new InvalidOperationException("TCP stream modifier caught an event of a socket which was not created by it. This should never happen."); } InvokeExceptionThrown(new TCPStreamModifierException("An exception was thrown by the TCP modifier stack with the binding " + tsmsStack.ToString(), args.Exception)); }
/// <summary> /// Checks whether all worker threads exited and all resources have been disposed. If true, removes the stack. /// </summary> /// <param name="tsmsStack">The stack to check</param> /// <returns>A bool indicating whether all worker threads exited and all resources have been disposed.</returns> private bool RemoveIfClosed(TCPStreamModifierStack tsmsStack) { if (tsmsStack.IsClosed) { lock (lStacks) { lStacks.Remove(tsmsStack); } InvokeExternalAsync(StackDestroyed, new TCPStreamModifierEventArgs(tsmsStack)); return(true); } return(false); }
void TCPSocket_StateChange(object sender, TCPSocketEventArgs e) { if (e.Sender.TCPState == TCPSocketState.Closed || e.Sender.TCPState == TCPSocketState.TimeWait) { TCPStreamModifierStack tsmsStack = GetStackForSocket(e.Sender); if (tsmsStack == null) { throw new InvalidOperationException("TCP stream modifier caught an event of a socket which was not created by it. This should never happen."); } TCPSocket sAlice = tsmsStack.StackAlice.TCPSocket; TCPSocket sBob = tsmsStack.StackBob.TCPSocket; if (sAlice.TCPState == TCPSocketState.Closed && sBob.TCPState == TCPSocketState.Closed) { ShutdownStack(tsmsStack); RemoveIfClosed(tsmsStack); } } }
protected override Frame ModifyTraffic(Frame fInputFrame) { if (!bIsShuttingDown) { IP.IPFrame ipFrame = GetIPv4Frame(fInputFrame); TCP.TCPFrame tcpFrame = GetTCPFrame(fInputFrame); if (ipFrame == null || tcpFrame == null) { return(fInputFrame); } if (IsLocal(ipFrame)) { return(fInputFrame); } if (!ShouldIntercept(ipFrame.SourceAddress, ipFrame.DestinationAddress, tcpFrame.SourcePort, tcpFrame.DestinationPort)) { return(fInputFrame); } bool bFound = false; lock (lStacks) { foreach (TCPStreamModifierStack tcpStack in lStacks) { if (tcpStack.StackAlice.PushUp(fInputFrame, false) || tcpStack.StackBob.PushUp(fInputFrame, false)) { bFound = true; break; } } if (!bFound) { //We have to create a new stack... TCPIPStack sAlice; TCPIPStack sBob; NetworkStreamModifier[] arMods; sAlice = new TCPIPStack(ipFrame.DestinationAddress, ipFrame.SourceAddress, tcpFrame.DestinationPort, tcpFrame.SourcePort); sAlice.ProtocolParser = this.ProtocolParser; sBob = new TCPIPStack(ipFrame.SourceAddress, ipFrame.DestinationAddress, tcpFrame.SourcePort, tcpFrame.DestinationPort); sBob.ProtocolParser = this.ProtocolParser; sAlice.FrameEncapsulated += new FrameProcessedEventHandler(Stack_FrameEncapsulated); sBob.FrameEncapsulated += new FrameProcessedEventHandler(Stack_FrameEncapsulated); sAlice.TCPSocket.StateChange += new EventHandler <TCPSocketEventArgs>(TCPSocket_StateChange); sBob.TCPSocket.StateChange += new EventHandler <TCPSocketEventArgs>(TCPSocket_StateChange); arMods = CreateAndLinkStreamOperators(new SocketNetworkStream(sAlice), new SocketNetworkStream(sBob)); TCPStreamModifierStack tsmsStack = new TCPStreamModifierStack(sAlice, sBob, arMods); foreach (NetworkStreamModifier sModifier in tsmsStack.Modifiers) { sModifier.Start(); //sModifier.AliceLoopClosed += new EventHandler(sModifier_AliceLoopClosed); //sModifier.BobLoopClosed += new EventHandler(sModifier_BobLoopClosed); sModifier.AliceLoopError += new ExceptionEventHandler(sModifier_AliceLoopError); sModifier.BobLoopError += new ExceptionEventHandler(sModifier_BobLoopError); } tsmsStack.StackAlice.Listen(); tsmsStack.StackAlice.PushUp(fInputFrame, false); tsmsStack.StackBob.ConnectAsync(); lStacks.Add(tsmsStack); //Notify Created InvokeExternalAsync(StackCreated, new TCPStreamModifierEventArgs(tsmsStack)); } } return(null); } return(fInputFrame); }
public TCPStreamModifierEventArgs(TCPStreamModifierStack tsmStack) { Stack = tsmStack; }
private void EndShutdownStack(TCPStreamModifierStack tsmsStack) { tsmsStack.StackAlice.EndClose(); tsmsStack.StackBob.EndClose(); }
private void ShutdownStack(TCPStreamModifierStack tsmsStack) { BeginShutdownStack(tsmsStack); EndShutdownStack(tsmsStack); }