static void Main(string[] args) { Device device = new Device("TUNTEST"); device.setMediaStatusAsConnected(); device.setTunMode("10.111.111.1", "255.0.0.0"); var mtu = device.getMTU(); logger.Debug("MTU: {0}", mtu); Tap = device.getStream(); var udpForwarder = new UDPForwarder(Tap); var tcpForwarder = new TCPForwarder(Tap); object state = new int(); WaitObject = new EventWaitHandle(false, EventResetMode.AutoReset); IAsyncResult res; while (true) { byte[] buf = new byte[mtu]; BytesRead = Tap.Read(buf, 0, buf.Length); if (BytesRead <= 0) { break; } logger.Debug("Read " + BytesRead.ToString()); try { var frame = IPFrame.Create(buf.Take(BytesRead).ToArray()); logger.Debug("Version: " + frame.Version); logger.Debug("Protocol Type: " + frame.Protocol); logger.Debug("Source Address: " + frame.SourceAddress.ToString()); logger.Debug("Dest Address: " + frame.DestinationAddress.ToString()); var parser = new ProtocolParser(); parser.ParseCompleteFrame(frame); if (frame.EncapsulatedFrame.FrameType == FrameTypes.UDP) { udpForwarder.forwardFrame(frame); } else if (frame.EncapsulatedFrame.FrameType == FrameTypes.TCP) { tcpForwarder.forwardFrame(frame); } } catch (Exception e) { logger.Error(e); } // // Reverse IPv4 addresses and send back to tun // //for (int i = 0; i< 4; ++i) //{ // byte tmp = buf[12+i]; buf[12+i] = buf[16+i]; buf[16+i] = tmp; //} //res2 = Tap.BeginWrite(buf, 0, BytesRead, ar => { // Tap.EndWrite(ar); // WaitObject2.Set(); //}, state2); //WaitObject2.WaitOne(); } }
static void ListenTest(GpioPin pin) { PinPulse(pin, 2000); using (Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4)) { IPEndPoint ep = new IPEndPoint(IPAddress.Any, 6501); soc.Bind(ep); soc.Listen(1); while (true) { Socket client = null; int BytesRead; try { Console.WriteLine(DateTime.UtcNow.ToString() + "Accept client"); client = soc.Accept(); Console.WriteLine(DateTime.UtcNow.ToString() + "Accepted"); PinPulse(pin, 500); client.ReceiveTimeout = 10000; NetworkStream stream = new NetworkStream(client); byte[] buffer = new byte[100]; // Echo back everything we receive and toggle Led while ((BytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) { stream.Write(buffer, 0, BytesRead); Console.WriteLine(DateTime.UtcNow.ToString() + "Read/Write " + BytesRead.ToString()); pin.Toggle(); } Console.WriteLine(DateTime.UtcNow.ToString() + "connection ending"); } catch (Exception ex) { Console.WriteLine(DateTime.UtcNow.ToString() + "Ex " + ex.Message); }; if (client != null) { client.Close(); } break; } } }