Exemple #1
0
        public static void Visit(
            IVisitorQuicReceiveLoop host, ITransport server,
            QuicStream stream, ThreadManager thread, int bufferSize)
        {
            byte[] receivedData = new byte[bufferSize];

            while (!thread.ShouldExit)
            {
                int receivedLength;

                try
                {
                    receivedLength = stream.Read(receivedData, 0, bufferSize);

                    if (receivedLength == 0)
                    {
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, null));
                        break;
                    }
                    else
                    {
                        host.VisitorAddReceivedData(ArrayUtility.SubArray <byte>(receivedData, 0, receivedLength));
                    }
                }
                catch (QuicException ex)
                {
                    server.AddEvent(new TransportEvent(EventType.Disconnected, null, ex));
                    break;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// the received loop for tcp connection.
        /// </summary>
        /// <param name="host">
        /// an IVisitorTcpReceiveLoop interface that specifies the host of visitor.
        /// </param>
        /// <param name="server">
        /// an ITransport object that provides AddEvent, it must be TcpClient or TcpServerConnection.
        /// </param>
        /// <param name="stream">
        /// a Stream object that specifies the underlayer transport stream.<para/>
        /// if DirectTcp, it's the stream of TcpClient.GetStream().<para/>
        /// if Tcp over Ssl, it's SslStream.
        /// </param>
        /// <param name="thread">
        /// a ThreadManager object that specifies the received thread.
        /// </param>
        /// <param name="bufferSize">
        /// an int value that specifies the max buffer size.
        /// </param>
        public static void Visit(
            IVisitorTcpReceiveLoop host, ITransport server,
            Stream stream, ThreadManager thread, int bufferSize)
        {
            byte[] data = new byte[bufferSize];

            while (!thread.ShouldExit)
            {
                int receivedLength = 0;

                try
                {
                    // received data from server.
                    receivedLength = stream.Read(data, 0, data.Length);

                    // if the server close the socket, return.
                    if (receivedLength == 0)
                    {
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, null));

                        break;
                    }

                    host.VisitorAddReceivedData(ArrayUtility.SubArray <byte>(data, 0, receivedLength));
                }
                catch (IOException ex)
                {
                    SocketException socketException = ex.InnerException as SocketException;

                    // if the server disconnect the socket.
                    if (socketException != null &&
                        (socketException.SocketErrorCode == SocketError.ConnectionReset ||
                         socketException.SocketErrorCode == SocketError.Interrupted ||
                         socketException.SocketErrorCode == SocketError.ConnectionAborted))
                    {
                        // handle the disconnected event, return.
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, ex));
                    }
                    else
                    {
                        // handle exception event, return.
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Exception, ex));
                    }

                    break;
                }
            }
        }
        public static void Visit(
            IVisitorNetbiosReceiveLoop host, ITransport server,
            NetbiosTransport transport, int localEP, int remoteEP, ThreadManager thread)
        {
            byte[] data = null;

            while (!thread.ShouldExit)
            {
                try
                {
                    // received data from server.
                    data = transport.Receive(remoteEP);

                    // if the server close the socket, return.
                    if (data == null)
                    {
                        server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, null));

                        break;
                    }

                    host.VisitorAddReceivedData(ArrayUtility.SubArray <byte>(data, 0));
                }
                // the connection is disconnected.
                catch (InvalidOperationException ex)
                {
                    server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, ex));

                    break;
                }
                catch (Exception ex)
                {
                    // handle exception event, return.
                    server.AddEvent(new TransportEvent(EventType.Exception, remoteEP, localEP, ex));

                    break;
                }
            }
        }
        public static void Visit(
            IVisitorNetbiosReceiveLoop host, ITransport server,
            NetbiosTransport transport, int localEP, int remoteEP, ThreadManager thread)
        {
            byte[] data = null;

            while (!thread.ShouldExit)
            {
                try
                {
                    // received data from server.
                    data = transport.Receive(remoteEP);

                    // if the server close the socket, return.
                    if (data == null)
                    {
                        server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, null));

                        break;
                    }

                    host.VisitorAddReceivedData(ArrayUtility.SubArray<byte>(data, 0));
                }
                // the connection is disconnected.
                catch (InvalidOperationException ex)
                {
                    server.AddEvent(new TransportEvent(EventType.Disconnected, remoteEP, localEP, ex));

                    break;
                }
                catch (Exception ex)
                {
                    // handle exception event, return.
                    server.AddEvent(new TransportEvent(EventType.Exception, remoteEP, localEP, ex));

                    break;
                }
            }
        }
        /// <summary>
        /// the received loop for tcp connection.
        /// </summary>
        /// <param name="host">
        /// an IVisitorTcpReceiveLoop interface that specifies the host of visitor.
        /// </param>
        /// <param name="server">
        /// an ITransport object that provides AddEvent, it must be TcpClient or TcpServerConnection.
        /// </param>
        /// <param name="stream">
        /// a Stream object that specifies the underlayer transport stream.<para/>
        /// if DirectTcp, it's the stream of TcpClient.GetStream().<para/>
        /// if Tcp over Ssl, it's SslStream.
        /// </param>
        /// <param name="thread">
        /// a ThreadManager object that specifies the received thread.
        /// </param>
        /// <param name="bufferSize">
        /// an int value that specifies the max buffer size.
        /// </param>
        public static void Visit(
            IVisitorTcpReceiveLoop host, ITransport server,
            Stream stream, ThreadManager thread, int bufferSize)
        {
            byte[] data = new byte[bufferSize];

            while (!thread.ShouldExit)
            {
                int receivedLength = 0;

                try
                {
                    // received data from server.
                    receivedLength = stream.Read(data, 0, data.Length);

                    // if the server close the socket, return.
                    if (receivedLength == 0)
                    {
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, null));

                        break;
                    }

                    host.VisitorAddReceivedData(ArrayUtility.SubArray<byte>(data, 0, receivedLength));
                }
                catch (IOException ex)
                {
                    SocketException socketException = ex.InnerException as SocketException;

                    // if the server disconnect the socket.
                    if (socketException != null
                        && (socketException.SocketErrorCode == SocketError.ConnectionReset
                        || socketException.SocketErrorCode == SocketError.Interrupted
                        || socketException.SocketErrorCode == SocketError.ConnectionAborted))
                    {
                        // handle the disconnected event, return.
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Disconnected, ex));
                    }
                    else
                    {
                        // handle exception event, return.
                        server.AddEvent(host.VisitorCreateTransportEvent(EventType.Exception, ex));
                    }

                    break;
                }
            }
        }
        public static void Visit(
            IVisitorUdpReceiveLoop host, ITransport server,
            UdpClient udpClient, ThreadManager thread, bool isLspHooked)
        {
            byte[] data = null;

            // get the endpoint of tcp client.
            IPEndPoint localEP = host.LspHookedLocalEP;

            while (!thread.ShouldExit)
            {
                try
                {
                    // received data from server.
                    IPEndPoint remoteEP = null;

                    data = udpClient.Receive(ref remoteEP);

                    if (data == null)
                    {
                        break;
                    }

                    if (isLspHooked)
                    {
                        int numBytesReceived = data.Length;

                        if (numBytesReceived < Marshal.SizeOf(typeof(LspUdpHeader)))
                        {
                            throw new FormatException("Invalid LSP udp packet received");
                        }

                        //Get udp header
                        byte[] udpHeaderBuffer = ArrayUtility.SubArray(data, 0, Marshal.SizeOf(typeof(LspUdpHeader)));

                        IntPtr p = Marshal.AllocHGlobal(udpHeaderBuffer.Length);
                        Marshal.Copy(udpHeaderBuffer, 0, p, udpHeaderBuffer.Length);

                        LspUdpHeader udpHeader = (LspUdpHeader)Marshal.PtrToStructure(p, typeof(LspUdpHeader));
                        Marshal.FreeHGlobal(p);

                        //get address
                        byte[] srcAddressArray = ArrayUtility.SubArray <byte>(data,
                                                                              Marshal.SizeOf(typeof(LspUdpHeader)), udpHeader.HeaderLength -
                                                                              Marshal.SizeOf(typeof(LspUdpHeader)));
                        string srcAddress = Encoding.ASCII.GetString(srcAddressArray);

                        //replacement
                        numBytesReceived = numBytesReceived - udpHeader.HeaderLength;
                        byte[] msgBody = new byte[numBytesReceived];
                        Array.Copy(data, udpHeader.HeaderLength, msgBody, 0, numBytesReceived);

                        //endPoint is real remote client endpoint, remoteEP is LspDll's endpoint
                        IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(srcAddress), udpHeader.Port);

                        //map from the real endpoint to the lsp dll endpoint
                        //when sending response to the real endpoint, response will be sent to the lsp dll endpoint
                        //and the lsp dll will relay it to the real endpoint
                        LspConsole.Instance.SetMappedIPEndPoint(
                            (IPEndPoint)udpClient.Client.LocalEndPoint, (IPEndPoint)endPoint,
                            (IPEndPoint)remoteEP, StackTransportType.Udp);

                        host.VisitorAddReceivedData(msgBody, localEP, (IPEndPoint)endPoint);
                    }
                    else
                    {
                        host.VisitorAddReceivedData(data, localEP, remoteEP);
                    }
                }
                catch (Exception ex)
                {
                    // handle exception event, return.
                    server.AddEvent(new TransportEvent(EventType.Exception, localEP, ex));

                    break;
                }
            }
        }
        public static void Visit(
            IVisitorUdpReceiveLoop host, ITransport server,
            UdpClient udpClient, ThreadManager thread, bool isLspHooked)
        {
            byte[] data = null;

            // get the endpoint of tcp client.
            IPEndPoint localEP = host.LspHookedLocalEP;

            while (!thread.ShouldExit)
            {
                try
                {
                    // received data from server.
                    IPEndPoint remoteEP = null;

                    data = udpClient.Receive(ref remoteEP);

                    if (data == null)
                    {
                        break;
                    }

                    if(isLspHooked)
                    {
                        int numBytesReceived = data.Length;

                        if (numBytesReceived < Marshal.SizeOf(typeof(LspUdpHeader)))
                        {
                            throw new FormatException("Invalid LSP udp packet received");
                        }

                        //Get udp header
                        byte[] udpHeaderBuffer = ArrayUtility.SubArray(data, 0, Marshal.SizeOf(typeof(LspUdpHeader)));

                        IntPtr p = Marshal.AllocHGlobal(udpHeaderBuffer.Length);
                        Marshal.Copy(udpHeaderBuffer, 0, p, udpHeaderBuffer.Length);

                        LspUdpHeader udpHeader = (LspUdpHeader)Marshal.PtrToStructure(p, typeof(LspUdpHeader));
                        Marshal.FreeHGlobal(p);

                        //get address
                        byte[] srcAddressArray = ArrayUtility.SubArray<byte>(data,
                            Marshal.SizeOf(typeof(LspUdpHeader)), udpHeader.HeaderLength -
                            Marshal.SizeOf(typeof(LspUdpHeader)));
                        string srcAddress = Encoding.ASCII.GetString(srcAddressArray);

                        //replacement
                        numBytesReceived = numBytesReceived - udpHeader.HeaderLength;
                        byte[] msgBody = new byte[numBytesReceived];
                        Array.Copy(data, udpHeader.HeaderLength, msgBody, 0, numBytesReceived);

                        //endPoint is real remote client endpoint, remoteEP is LspDll's endpoint
                        IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(srcAddress), udpHeader.Port);

                        //map from the real endpoint to the lsp dll endpoint
                        //when sending response to the real endpoint, response will be sent to the lsp dll endpoint
                        //and the lsp dll will relay it to the real endpoint
                        LspConsole.Instance.SetMappedIPEndPoint(
                            (IPEndPoint)udpClient.Client.LocalEndPoint, (IPEndPoint)endPoint,
                            (IPEndPoint)remoteEP, StackTransportType.Udp);

                        host.VisitorAddReceivedData(msgBody, localEP, (IPEndPoint)endPoint);
                    }
                    else
                    {
                        host.VisitorAddReceivedData(data, localEP, remoteEP);
                    }
                }
                catch (Exception ex)
                {
                    // handle exception event, return.
                    server.AddEvent(new TransportEvent(EventType.Exception, localEP, ex));

                    break;
                }
            }
        }