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; } } }