Example #1
0
        /// <summary>
        /// Send data.
        /// </summary>
        /// <param name="data">Data array.</param>
        /// <param name="dest">Destination address.</param>
        /// <param name="destPort">Destination port.</param>
        /// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception>
        /// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception>
        /// <exception cref="Sys.IO.IOException">Thrown on IO error.</exception>
        public void Send(byte[] data, Address dest, int destPort)
        {
            Address source = IPConfig.FindNetwork(dest);
            var     packet = new UDPPacket(source, dest, (ushort)localPort, (ushort)destPort, data);

            OutgoingBuffer.AddPacket(packet);
        }
Example #2
0
        /// <summary>
        /// Connect to client.
        /// </summary>
        /// <param name="dest">Destination address.</param>
        /// <param name="destPort">Destination port.</param>
        /// <exception cref="Exception">Thrown if TCP Status is not CLOSED.</exception>
        public void Connect(Address dest, int destPort, int timeout = 5000)
        {
            if (StateMachine.Status != Status.CLOSED)
            {
                throw new Exception("Client must be closed before setting a new connection.");
            }

            StateMachine.destination     = dest;
            StateMachine.destinationPort = destPort;

            StateMachine.source = IPConfig.FindNetwork(dest);

            //Generate Random Sequence Number
            var rnd = new Random();

            StateMachine.SequenceNumber = (uint)((rnd.Next(0, Int32.MaxValue)) << 32) | (uint)(rnd.Next(0, Int32.MaxValue));

            // Flags=0x02 -> Syn
            var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)destPort, StateMachine.SequenceNumber, 0, 20, (byte)Flags.SYN, 0xFAF0, 0);

            OutgoingBuffer.AddPacket(packet);
            NetworkStack.Update();

            StateMachine.Status = Status.SYN_SENT;

            if (StateMachine.WaitStatus(Status.ESTABLISHED, timeout) == false)
            {
                throw new Exception("Failed to open TCP connection!");
            }
        }
Example #3
0
        /// <summary>
        /// Send ICMP Echo
        /// </summary>
        public void SendEcho()
        {
            Address         source  = IPConfig.FindNetwork(destination);
            ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working

            OutgoingBuffer.AddPacket(request);                                                //Aura doesn't work when this is called.
            NetworkStack.Update();
        }
Example #4
0
        /// <summary>
        /// Send DNS Ask for Domain Name string
        /// </summary>
        /// <param name="url">Domain Name string.</param>
        public void SendAsk(string url)
        {
            Address source = IPConfig.FindNetwork(destination);

            queryurl = url;

            var askpacket = new DNSPacketAsk(source, destination, url);

            OutgoingBuffer.AddPacket(askpacket);

            NetworkStack.Update();
        }
Example #5
0
        /// <summary>
        /// Process LISTEN Status.
        /// </summary>
        /// <param name="packet">Packet to receive.</param>
        public void ProcessListen(TCPPacket packet)
        {
            if (packet.RST)
            {
                Global.mDebugger.Send("RST received at LISTEN state, packet passed.");

                return;
            }
            else if (packet.FIN)
            {
                Status = Status.CLOSED;

                Global.mDebugger.Send("TCP connection closed! (FIN received on LISTEN state)");
            }
            else if (packet.ACK)
            {
                TCB.RcvNxt = packet.SequenceNumber;
                TCB.SndNxt = packet.AckNumber;

                Status = Status.ESTABLISHED;
            }
            else if (packet.SYN)
            {
                LocalEndPoint.Address  = IPConfig.FindNetwork(packet.SourceIP);
                RemoteEndPoint.Address = packet.SourceIP;
                RemoteEndPoint.Port    = packet.SourcePort;

                var rnd            = new Random();
                var sequenceNumber = (uint)((rnd.Next(0, Int32.MaxValue)) << 32) | (uint)(rnd.Next(0, Int32.MaxValue));

                //Fill TCB
                TCB.SndUna = sequenceNumber;
                TCB.SndNxt = sequenceNumber;
                TCB.SndWnd = Tcp.TcpWindowSize;
                TCB.SndUp  = 0;
                TCB.SndWl1 = packet.SequenceNumber - 1;
                TCB.SndWl2 = 0;
                TCB.ISS    = sequenceNumber;

                TCB.RcvNxt = packet.SequenceNumber + 1;
                TCB.RcvWnd = Tcp.TcpWindowSize;
                TCB.RcvUp  = 0;
                TCB.IRS    = packet.SequenceNumber;

                SendEmptyPacket(Flags.SYN | Flags.ACK);

                Status = Status.SYN_RECEIVED;
            }
        }
Example #6
0
        /// <summary>
        /// Send a packet to the DHCP server to make the address available again
        /// </summary>
        public void SendReleasePacket()
        {
            foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
            {
                Address source       = IPConfig.FindNetwork(DHCPServerAddress(networkDevice));
                var     dhcp_release = new DHCPRelease(source, DHCPServerAddress(networkDevice), networkDevice.MACAddress);

                OutgoingBuffer.AddPacket(dhcp_release);
                NetworkStack.Update();

                NetworkStack.RemoveAllConfigIP();

                IPConfig.Enable(networkDevice, new Address(0, 0, 0, 0), new Address(0, 0, 0, 0), new Address(0, 0, 0, 0));
            }
            Close();
        }
Example #7
0
        /// <summary>
        /// Connect to client.
        /// </summary>
        /// <param name="dest">Destination address.</param>
        /// <param name="destPort">Destination port.</param>
        /// <exception cref="Exception">Thrown if TCP Status is not CLOSED.</exception>
        public void Connect(Address dest, int destPort, int timeout = 5000)
        {
            if (StateMachine.Status == Status.ESTABLISHED)
            {
                throw new Exception("Client must be closed before setting a new connection.");
            }

            StateMachine.RemoteEndPoint.Address = dest;
            StateMachine.LocalEndPoint.Address  = IPConfig.FindNetwork(dest);
            StateMachine.RemoteEndPoint.Port    = (ushort)destPort;

            //Generate Random Sequence Number
            var rnd            = new Random();
            var SequenceNumber = (uint)((rnd.Next(0, Int32.MaxValue)) << 32) | (uint)(rnd.Next(0, Int32.MaxValue));

            //Fill TCB
            StateMachine.TCB.SndUna = SequenceNumber;
            StateMachine.TCB.SndNxt = SequenceNumber;
            StateMachine.TCB.SndWnd = Tcp.TcpWindowSize;
            StateMachine.TCB.SndUp  = 0;
            StateMachine.TCB.SndWl1 = 0;
            StateMachine.TCB.SndWl2 = 0;
            StateMachine.TCB.ISS    = SequenceNumber;

            StateMachine.TCB.RcvNxt = 0;
            StateMachine.TCB.RcvWnd = Tcp.TcpWindowSize;
            StateMachine.TCB.RcvUp  = 0;
            StateMachine.TCB.IRS    = 0;

            Tcp.Connections.Add(StateMachine);

            StateMachine.SendEmptyPacket(Flags.SYN);

            StateMachine.Status = Status.SYN_SENT;

            if (StateMachine.WaitStatus(Status.ESTABLISHED, timeout) == false)
            {
                throw new Exception("Failed to open TCP connection!");
            }
        }
Example #8
0
        /// <summary>
        /// Process LISTEN Status.
        /// </summary>
        /// <param name="packet">Packet to receive.</param>
        public void ProcessListen(TCPPacket packet)
        {
            if (packet.RST)
            {
                Status = Status.CLOSED;

                throw new Exception("TCP connection resetted! (RST received on LISTEN state)");
            }
            else if (packet.FIN)
            {
                Status = Status.CLOSED;

                throw new Exception("TCP connection closed! (FIN received on LISTEN state)");
            }
            else if (packet.ACK)
            {
                AckNumber      = packet.SequenceNumber;
                SequenceNumber = packet.AckNumber;

                Status = Status.ESTABLISHED;
            }
            else if (packet.SYN)
            {
                Status = Status.SYN_RECEIVED;

                source = IPConfig.FindNetwork(packet.SourceIP);

                AckNumber = packet.SequenceNumber + 1;

                var rnd = new Random();
                SequenceNumber = (uint)((rnd.Next(0, Int32.MaxValue)) << 32) | (uint)(rnd.Next(0, Int32.MaxValue));

                destination     = packet.SourceIP;
                destinationPort = packet.SourcePort;

                SendEmptyPacket(Flags.SYN | Flags.ACK);
            }
        }
Example #9
0
        /// <summary>
        /// CommandEcho
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            int PacketSent     = 0;
            int PacketReceived = 0;
            int PacketLost     = 0;
            int PercentLoss;

            Address source;
            Address destination = Address.Parse(arguments[0]);

            if (destination != null)
            {
                source = IPConfig.FindNetwork(destination);
            }
            else //Make a DNS request if it's not an IP
            {
                var xClient = new DnsClient();
                xClient.Connect(DNSConfig.Server(0));
                xClient.SendAsk(arguments[0]);
                destination = xClient.Receive();
                xClient.Close();

                if (destination == null)
                {
                    return(new ReturnInfo(this, ReturnCode.ERROR, "Failed to get DNS response for " + arguments[0]));
                }

                source = IPConfig.FindNetwork(destination);
            }

            try
            {
                Console.WriteLine("Sending ping to " + destination.ToString());

                var xClient = new ICMPClient();
                xClient.Connect(destination);

                for (int i = 0; i < 4; i++)
                {
                    xClient.SendEcho();

                    PacketSent++;

                    var endpoint = new EndPoint(Address.Zero, 0);

                    int second = xClient.Receive(ref endpoint, 4000);

                    if (second == -1)
                    {
                        Console.WriteLine("Destination host unreachable.");
                        PacketLost++;
                    }
                    else
                    {
                        if (second < 1)
                        {
                            Console.WriteLine("Reply received from " + endpoint.address.ToString() + " time < 1s");
                        }
                        else if (second >= 1)
                        {
                            Console.WriteLine("Reply received from " + endpoint.address.ToString() + " time " + second + "s");
                        }

                        PacketReceived++;
                    }
                }

                xClient.Close();
            }
            catch
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "Ping process error."));
            }

            PercentLoss = 25 * PacketLost;

            Console.WriteLine();
            Console.WriteLine("Ping statistics for " + destination.ToString() + ":");
            Console.WriteLine("    Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)");

            return(new ReturnInfo(this, ReturnCode.OK));
        }