void reqReader_HTTPRequestCaptured(object sender, HTTPReaderEventArgs e)
        {
            TCPStreamMonitorStack tsStack = GetStackForMonitor((NetworkStreamMonitor)sender);

            if (tsStack == null)
            {
                InvokeExceptionThrown(new InvalidOperationException("HTTP monitor captured a HTTP response from a stack which was already destroyed."));
                return;
            }

            HTTPConversation hcConversation = dictConversations[tsStack];

            hcConversation.AddRequest((HTTPRequest)e.HTTPMessage);

            if (dictResponses[tsStack].Count > 0)
            {
                HTTPRequest htqr = GetNextRequest(hcConversation);

                if (htqr != null)
                {
                    htqr.Response = dictResponses[tsStack].Dequeue();
                }
            }

            InvokeExternalAsync(HTTPSessionInformationChanged, new HTTPMonitorEventArgs(hcConversation));
        }
Beispiel #2
0
        private void ShutdownStack(TCPStreamMonitorStack tsmsStack)
        {
            tsmsStack.StackAlice.TCPSocket.StateChange -= new EventHandler <TCPListenerSocketEventArgs>(TCPSocket_StateChange);
            tsmsStack.StackBob.TCPSocket.StateChange   -= new EventHandler <TCPListenerSocketEventArgs>(TCPSocket_StateChange);

            if (tsmsStack.StackAlice.TCPSocket.TCPState != TCPSocketState.Closed)
            {
                tsmsStack.StackAlice.Close();
            }

            if (tsmsStack.StackBob.TCPSocket.TCPState != TCPSocketState.Closed)
            {
                tsmsStack.StackBob.Close();
            }

            foreach (NetworkStreamMonitor nsMonitor in tsmsStack.Monitors)
            {
                if (nsMonitor.IsRunning)
                {
                    nsMonitor.LoopError  -= new ExceptionEventHandler(nsMonitor_LoopError);
                    nsMonitor.LoopClosed -= new EventHandler(nsMonitor_LoopClosed);

                    nsMonitor.StopAsync();
                }
            }
        }
        void respReader_HTTPResponseCaptured(object sender, HTTPReaderEventArgs e)
        {
            TCPStreamMonitorStack tsStack = GetStackForMonitor((NetworkStreamMonitor)sender);

            if (tsStack == null)
            {
                InvokeExceptionThrown(new InvalidOperationException(this.Name + "captured a HTTP response from a stack which was already destroyed."));
                return;
            }
            HTTPConversation hcConversation = dictConversations[tsStack];

            HTTPRequest htqr = GetNextRequest(hcConversation);

            if (htqr == null)
            {
                //Request missing for response
                dictResponses[tsStack].Enqueue((HTTPResponse)e.HTTPMessage);
            }
            else
            {
                htqr.Response = (HTTPResponse)e.HTTPMessage;
            }

            InvokeExternalAsync(HTTPSessionInformationChanged, new HTTPMonitorEventArgs(hcConversation));
        }
Beispiel #4
0
        void TCPSocket_StateChange(object sender, TCPListenerSocketEventArgs e)
        {
            if (e.Sender.TCPState == TCPSocketState.Closed)
            {
                TCPStreamMonitorStack tsmsStack = GetStackForSocket(e.Sender);
                if (tsmsStack == null)
                {
                    throw new InvalidOperationException("TCP stream monitor caught an event of a socket which was not created by it. This should never happen.");
                }
                TCPListenerSocket sAlice = tsmsStack.StackAlice.TCPSocket;
                TCPListenerSocket sBob   = tsmsStack.StackBob.TCPSocket;
                if (sAlice.TCPState == TCPSocketState.Closed && sBob.TCPState == TCPSocketState.Closed)
                {
                    ShutdownStack(tsmsStack);

                    if (tsmsStack.IsClosed)
                    {
                        lock (lStacks)
                        {
                            lStacks.Remove(tsmsStack);
                            InvokeExternal(StackDestroyed, new TCPStreamMonitorEventArgs(tsmsStack));
                        }
                    }
                }
            }
        }
Beispiel #5
0
        void nsMonitor_LoopError(object sender, ExceptionEventArgs args)
        {
            TCPStreamMonitorStack tsmsStack = GetStackForMonitor((NetworkStreamMonitor)sender);

            if (tsmsStack != null)
            {
                InvokeExceptionThrown(new TCPStreamMonitorException("An exception was thrown by a TCP stream monitor with the binding " + tsmsStack.ToString(), args.Exception));
            }
            else
            {
                InvokeExceptionThrown(new TCPStreamMonitorException("An exception was thrown by a TCP stream monitor which was already destroyed.", args.Exception));
            }
        }
Beispiel #6
0
        void nsMonitor_LoopClosed(object sender, EventArgs e)
        {
            NetworkStreamMonitor nsMonitor = (NetworkStreamMonitor)sender;


            nsMonitor.LoopError  -= new ExceptionEventHandler(nsMonitor_LoopError);
            nsMonitor.LoopClosed -= new EventHandler(nsMonitor_LoopClosed);

            TCPStreamMonitorStack tsmsStack = GetStackForMonitor(nsMonitor);

            if (tsmsStack != null && tsmsStack.IsClosed)
            {
                lock (lStacks)
                {
                    lStacks.Remove(tsmsStack);
                    InvokeExternal(StackDestroyed, new TCPStreamMonitorEventArgs(tsmsStack));
                }
            }
        }
Beispiel #7
0
        protected override void HandleTraffic(Frame fInputFrame)
        {
            if (!bIsShuttingDown)
            {
                IP.IPFrame   ipFrame  = GetIPFrame(fInputFrame);
                TCP.TCPFrame tcpFrame = GetTCPFrame(fInputFrame);

                if (ipFrame == null || tcpFrame == null)
                {
                    return;
                }

                if (!ShouldIntercept(ipFrame.SourceAddress, ipFrame.DestinationAddress, tcpFrame.SourcePort, tcpFrame.DestinationPort))
                {
                    return;
                }

                bool bFound = false;

                lock (lStacks)
                {
                    foreach (TCPStreamMonitorStack 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...

                        TCPIPListenerStack     sAlice;
                        TCPIPListenerStack     sBob;
                        NetworkStreamMonitor[] arMonitors;

                        sBob = new TCPIPListenerStack(ipFrame.DestinationAddress, ipFrame.SourceAddress, tcpFrame.DestinationPort, tcpFrame.SourcePort);
                        sBob.ProtocolParser = this.ProtocolParser;
                        sAlice = new TCPIPListenerStack(ipFrame.SourceAddress, ipFrame.DestinationAddress, tcpFrame.SourcePort, tcpFrame.DestinationPort);
                        sAlice.ProtocolParser = this.ProtocolParser;

                        sAlice.TCPSocket.StateChange += new EventHandler <TCPListenerSocketEventArgs>(TCPSocket_StateChange);
                        sBob.TCPSocket.StateChange   += new EventHandler <TCPListenerSocketEventArgs>(TCPSocket_StateChange);

                        arMonitors = CreateAndLinkStreamMonitors(new SocketNetworkStream(sAlice), new SocketNetworkStream(sBob));

                        TCPStreamMonitorStack tsmsStack = new TCPStreamMonitorStack(sAlice, sBob, arMonitors);

                        foreach (NetworkStreamMonitor nsMonitor in tsmsStack.Monitors)
                        {
                            nsMonitor.Start();
                            nsMonitor.LoopError  += new ExceptionEventHandler(nsMonitor_LoopError);
                            nsMonitor.LoopClosed += new EventHandler(nsMonitor_LoopClosed);
                        }

                        tsmsStack.StackBob.Listen();
                        tsmsStack.StackBob.PushUp(ipFrame, false);

                        tsmsStack.StackAlice.Connect();

                        lStacks.Add(tsmsStack);

                        //Notify Created
                        InvokeExternal(StackCreated, new TCPStreamMonitorEventArgs(tsmsStack));
                    }
                }
            }
        }
Beispiel #8
0
 public TCPStreamMonitorEventArgs(TCPStreamMonitorStack tsmStack)
 {
     Stack = tsmStack;
 }