Exemple #1
0
        /// <summary>
        /// Close connection.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
        /// <exception cref="Exception">Thrown if TCP Status is CLOSED.</exception>
        public void Close()
        {
            if (StateMachine.Status == Status.CLOSED)
            {
                throw new Exception("Client already closed.");
            }
            if (StateMachine.Status == Status.ESTABLISHED)
            {
                var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)StateMachine.destinationPort, StateMachine.SequenceNumber, StateMachine.AckNumber, 20, (byte)(Flags.FIN | Flags.ACK), 0xFAF0, 0);
                OutgoingBuffer.AddPacket(packet);
                NetworkStack.Update();

                StateMachine.SequenceNumber++;

                StateMachine.Status = Status.FIN_WAIT1;

                if (StateMachine.WaitStatus(Status.CLOSED, 5000) == false)
                {
                    throw new Exception("Failed to close TCP connection!");
                }
            }

            if (clients.ContainsKey((uint)StateMachine.localPort))
            {
                clients.Remove((uint)StateMachine.localPort);
            }
        }
Exemple #2
0
        /// <summary>
        /// Received reply.
        /// </summary>

        /// <summary>
        /// Create new instance of the <see cref="ICMPPacket"/> class.
        /// </summary>
        /// <param name="packetData">Packet data.</param>
        /// <exception cref="ArgumentException">Thrown if packetData is invalid.</exception>
        internal static void ICMPHandler(byte[] packetData)
        {
            Global.mDebugger.Send("ICMP Handler called");
            var icmp_packet = new ICMPPacket(packetData);

            switch (icmp_packet.ICMPType)
            {
            case 0:
                var receiver = ICMPClient.GetClient(icmp_packet.SourceIP.Hash);
                if (receiver != null)
                {
                    receiver.ReceiveData(icmp_packet);
                }
                Global.mDebugger.Send("Received ICMP Echo reply from " + icmp_packet.SourceIP.ToString());
                break;

            case 8:
                var request = new ICMPEchoRequest(packetData);
                var reply   = new ICMPEchoReply(request);
                Global.mDebugger.Send("Sending ICMP Echo reply to " + reply.DestinationIP.ToString());
                OutgoingBuffer.AddPacket(reply);
                NetworkStack.Update();
                break;
            }
        }
Exemple #3
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!");
            }
        }
Exemple #4
0
 protected override void BeforeRun()
 {
     try
     {
         using (var xClient = new DHCPClient())
         {
             /** Send a DHCP Discover packet **/
             //This will automatically set the IP config after DHCP response
             NetworkStack.Update();
             int r = xClient.SendDiscoverPacket();
             if (r == -1)
             {
                 Console.WriteLine("Failure while configuring DHCP: timeout");
                 ManualConfig();
             }
             else
             {
                 Console.WriteLine("DHCP Configure: result: " + r);
             }
             xClient.Close();  //don't forget to close!
         }
         ipconfig();
     }
     catch (Exception x)
     {
         Console.WriteLine("err: " + x.Message);
     }
 }
Exemple #5
0
        /// <summary>
        /// Send data to client.
        /// </summary>
        /// <param name="data">Data array to send.</param>
        /// <exception cref="Exception">Thrown if destination is null or destinationPort is 0.</exception>
        /// <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>
        /// <exception cref="Exception">Thrown if TCP Status is not ESTABLISHED.</exception>
        public void Send(byte[] data)
        {
            if ((StateMachine.RemoteEndPoint.Address == null) || (StateMachine.RemoteEndPoint.Port == 0))
            {
                throw new InvalidOperationException("Must establish a default remote host by calling Connect() before using this Send() overload");
            }
            if (StateMachine.Status != Status.ESTABLISHED)
            {
                throw new Exception("Client must be connected before sending data.");
            }
            if (data.Length > 536)
            {
                var chunks = ArrayHelper.ArraySplit(data, 536);

                for (int i = 0; i < chunks.Length; i++)
                {
                    var packet = new TCPPacket(StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address, StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, i == chunks.Length - 2 ? (byte)(Flags.PSH | Flags.ACK) : (byte)(Flags.ACK), StateMachine.TCB.SndWnd, 0, chunks[i]);
                    OutgoingBuffer.AddPacket(packet);
                    NetworkStack.Update();

                    StateMachine.TCB.SndNxt += (uint)chunks[i].Length;
                }
            }
            else
            {
                var packet = new TCPPacket(StateMachine.LocalEndPoint.Address, StateMachine.RemoteEndPoint.Address, StateMachine.LocalEndPoint.Port, StateMachine.RemoteEndPoint.Port, StateMachine.TCB.SndNxt, StateMachine.TCB.RcvNxt, 20, (byte)(Flags.PSH | Flags.ACK), StateMachine.TCB.SndWnd, 0, data);
                OutgoingBuffer.AddPacket(packet);
                NetworkStack.Update();

                StateMachine.TCB.SndNxt += (uint)data.Length;
            }
        }
Exemple #6
0
        public void Send(byte[] data)
        {
            if ((this.destination == null) ||
                (this.destinationPort == 0))
            {
                throw new Exception("Must establish a default remote host by calling Connect() before using this Send() overload");
            }

            Send(data, this.destination, this.destinationPort);
            NetworkStack.Update();

            int _deltaT = 0;
            int second  = 0;

            while (!readytosend)
            {
                if (_deltaT != Cosmos.HAL.RTC.Second)
                {
                    second++;
                    _deltaT = Cosmos.HAL.RTC.Second;
                }

                if (second >= 4)
                {
                    Console.WriteLine("No response in 4 secondes...");
                    break;
                }
            }
            //Console.WriteLine("Done!");
        }
Exemple #7
0
        /// <summary>
        /// Send data to client.
        /// </summary>
        /// <param name="data">Data array to send.</param>
        /// <exception cref="Exception">Thrown if destination is null or destinationPort is 0.</exception>
        /// <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>
        /// <exception cref="Exception">Thrown if TCP Status is not ESTABLISHED.</exception>
        public void Send(byte[] data)
        {
            if ((StateMachine.destination == null) || (StateMachine.destinationPort == 0))
            {
                throw new InvalidOperationException("Must establish a default remote host by calling Connect() before using this Send() overload");
            }
            if (StateMachine.Status != Status.ESTABLISHED)
            {
                throw new Exception("Client must be connected before sending data.");
            }
            if (data.Length > 536)
            {
                var chunks = ArrayHelper.ArraySplit(data, 536);

                for (int i = 0; i < chunks.Length; i++)
                {
                    var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)StateMachine.destinationPort, StateMachine.SequenceNumber, StateMachine.AckNumber, 20, i == chunks.Length - 2 ? (byte)(Flags.PSH | Flags.ACK) : (byte)(Flags.ACK), 0xFAF0, 0, chunks[i]);
                    OutgoingBuffer.AddPacket(packet);
                    NetworkStack.Update();

                    StateMachine.SequenceNumber += (uint)chunks[i].Length;
                }
            }
            else
            {
                var packet = new TCPPacket(StateMachine.source, StateMachine.destination, (ushort)StateMachine.localPort, (ushort)StateMachine.destinationPort, StateMachine.SequenceNumber, StateMachine.AckNumber, 20, (byte)(Flags.PSH | Flags.ACK), 0xFAF0, 0, data);
                OutgoingBuffer.AddPacket(packet);
                NetworkStack.Update();

                StateMachine.SequenceNumber += (uint)data.Length;
            }
            StateMachine.WaitingAck = true;
        }
Exemple #8
0
        /// <summary>
        /// Send acknowledgement packet
        /// </summary>
        private void SendEmptyPacket(Flags flag)
        {
            var packet = new TCPPacket(source, destination, (ushort)localPort, (ushort)destinationPort, SequenceNumber, AckNumber, 20, (byte)flag, 0xFAF0, 0);

            OutgoingBuffer.AddPacket(packet);
            NetworkStack.Update();
        }
Exemple #9
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();
        }
Exemple #10
0
 /// <summary>
 /// Send a request to apply the new IP configuration
 /// </summary>
 public static void SendRequestPacket(Address RequestedAddress, Address DHCPServerAddress)
 {
     foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
     {
         DHCPRequest dhcp_request = new DHCPRequest(networkDevice.MACAddress, RequestedAddress, DHCPServerAddress);
         OutgoingBuffer.AddPacket(dhcp_request);
         NetworkStack.Update();
     }
 }
Exemple #11
0
        /// <summary>
        /// Send TCP packet.
        /// </summary>
        private void SendPacket(TCPPacket packet)
        {
            OutgoingBuffer.AddPacket(packet);
            NetworkStack.Update();

            if (packet.SYN || packet.FIN)
            {
                TCB.SndNxt++;
            }
        }
Exemple #12
0
        /// <summary>
        /// Send data to client.
        /// </summary>
        /// <param name="data">Data array to send.</param>
        /// <exception cref="Exception">Thrown if destination is null or destinationPort is 0.</exception>
        /// <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)
        {
            if ((destination == null) || (destinationPort == 0))
            {
                throw new InvalidOperationException("Must establish a default remote host by calling Connect() before using this Send() overload");
            }

            Send(data, destination, destinationPort);
            NetworkStack.Update();
        }
Exemple #13
0
 /// <summary>
 /// Send a request to apply the new IP configuration
 /// </summary>
 /// <returns>time value (-1 = timeout)</returns>
 private int SendRequestPacket(Address RequestedAddress)
 {
     foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
     {
         var dhcp_request = new DHCPRequest(networkDevice.MACAddress, RequestedAddress);
         OutgoingBuffer.AddPacket(dhcp_request);
         NetworkStack.Update();
     }
     return(Receive());
 }
Exemple #14
0
        public void Ask(string url)
        {
            Settings settings = new Settings(@"0:\System\" + NetworkInterfaces.Interface("eth0") + ".conf");
            Address  gateway  = Address.Parse(settings.Get("dns01"));
            Address  source   = Config.FindNetwork(gateway);

            askpacket = new DNSPacketAsk(source, gateway, 0x1234, 0x0100, 1, url);

            OutgoingBuffer.AddPacket(askpacket);
            NetworkStack.Update();
        }
Exemple #15
0
        /// <summary>
        /// Send a packet to find the DHCP server and tell that we want a new IP address
        /// </summary>
        public static void SendDiscoverPacket()
        {
            foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
            {
                DHCPDiscover dhcp_discover = new DHCPDiscover(networkDevice.MACAddress);
                OutgoingBuffer.AddPacket(dhcp_discover);
                NetworkStack.Update();

                DHCPAsked = true;
            }
        }
Exemple #16
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();
        }
        /// <summary>
        /// Send a packet to find the DHCP server and tell that we want a new IP address
        /// </summary>
        public static void SendDiscoverPacket()
        {
            NetworkStack.RemoveAllConfigIP();

            foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
            {
                NetworkInit.Enable(networkDevice, new Network.IPV4.Address(0, 0, 0, 0), new Network.IPV4.Address(0, 0, 0, 0), new Network.IPV4.Address(0, 0, 0, 0));

                DHCPDiscover dhcp_discover = new DHCPDiscover(networkDevice.MACAddress);
                OutgoingBuffer.AddPacket(dhcp_discover);
                NetworkStack.Update();

                DHCPAsked = true;
            }
        }
Exemple #18
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();
        }
Exemple #19
0
        /// <summary>
        /// Send a packet to find the DHCP server and tell that we want a new IP address
        /// </summary>
        /// <returns>time value (-1 = timeout)</returns>
        public int SendDiscoverPacket()
        {
            NetworkStack.RemoveAllConfigIP();

            foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
            {
                IPConfig.Enable(networkDevice, new Address(0, 0, 0, 0), new Address(0, 0, 0, 0), new Address(0, 0, 0, 0));

                var dhcp_discover = new DHCPDiscover(networkDevice.MACAddress);
                OutgoingBuffer.AddPacket(dhcp_discover);
                NetworkStack.Update();

                asked = true;
            }

            return(Receive());
        }
Exemple #20
0
 public bool Send(bool isdata)
 {
     Apps.System.Debugger.debugger.Send("Sending TCP packet...");
     if (isdata)
     {
         TCPPacket packet = new TCPPacket(source, dest, localPort, destPort, data, sequencenumber, acknowledgmentnb, 0x50, Flags, WSValue, 0x0000, false, false);
         OutgoingBuffer.AddPacket(packet);
     }
     else
     {
         TCPPacket packet = new TCPPacket(source, dest, localPort, destPort, data, sequencenumber, acknowledgmentnb, 0x50, Flags, WSValue, 0x0000, true, true);
         OutgoingBuffer.AddPacket(packet);
     }
     NetworkStack.Update();
     Apps.System.Debugger.debugger.Send("Sent!");
     return(true);
 }
Exemple #21
0
        /// <summary>
        /// Send a packet to the DHCP server to make the address available again
        /// </summary>
        public static void SendReleasePacket()
        {
            foreach (NetworkDevice networkDevice in NetworkDevice.Devices)
            {
                Address     source       = Config.FindNetwork(DHCPServerAddress(networkDevice));
                DHCPRelease dhcp_release = new DHCPRelease(source, DHCPServerAddress(networkDevice));
                OutgoingBuffer.AddPacket(dhcp_release);
                NetworkStack.Update();

                NetworkStack.RemoveAllConfigIP();

                Settings settings = new Settings(@"0:\" + networkDevice.Name + ".conf");
                settings.EditValue("ipaddress", "0.0.0.0");
                settings.EditValue("subnet", "0.0.0.0");
                settings.EditValue("gateway", "0.0.0.0");
                settings.EditValue("dns01", "0.0.0.0");
                settings.PushValues();

                NetworkInit.Enable();
            }
        }
Exemple #22
0
        internal static void ICMPHandler(byte[] packetData)
        {
            NetworkStack.debugger.Send("ICMP Handler called");
            ICMPPacket icmp_packet = new ICMPPacket(packetData);

            switch (icmp_packet.ICMP_Type)
            {
            case 0:
                recvd_reply = new ICMPEchoReply(packetData);
                NetworkStack.debugger.Send("Received ICMP Echo reply from " + recvd_reply.SourceIP.ToString());
                break;

            case 8:
                ICMPEchoRequest request = new ICMPEchoRequest(packetData);
                NetworkStack.debugger.Send("Received " + request.ToString());
                ICMPEchoReply reply = new ICMPEchoReply(request);
                NetworkStack.debugger.Send("Sending ICMP Echo reply to " + reply.DestinationIP.ToString());
                OutgoingBuffer.AddPacket(reply);
                NetworkStack.Update();
                break;
            }
        }
Exemple #23
0
        protected override void Run()
        {
            NetworkStack.Update();
            Console.Write("> ");
            var input = Console.ReadLine();

            if (input.ToLower().StartsWith("sendpkt"))
            {
                var x = new UdpClient(128);
                x.Connect(Address.Zero, 128);
                x.Send(Encoding.ASCII.GetBytes("Hello from cosmos!"));
                x.Close();
            }
            else if (input.ToLower().StartsWith("getip"))
            {
                Console.WriteLine("Connecting to DNS Server");
                xClient.Connect(new Address(8, 8, 4, 4));

                Console.WriteLine("Asking IP for github.com");
                xClient.SendAsk("github.com");

                Console.WriteLine("Waiting for data");

                var addr = xClient.Receive();
                if (addr == null)
                {
                    Console.WriteLine("Error: connection timed out");
                }
                else
                {
                    Console.WriteLine("Got data: " + addr.ToString());
                }
            }
            else if (input.ToLower().StartsWith("gettime"))
            {
                NTPClient client = new NTPClient();
                var       t      = client.GetNetworkTime();
                if (t == null)
                {
                    Console.WriteLine("NTPClient.GetNetworkTime() Returned null!");
                }
                else
                {
                    Console.WriteLine("NTPClient.GetNetworkTime() Returned " + t);
                }
            }
            else if (input.ToLower().StartsWith("ping"))
            {
                string s = input.Replace("ping ", "");

                if (input.ToLower() == "ping" | string.IsNullOrEmpty(s))
                {
                    Console.WriteLine("Invaild synax. Usage: ping <ip address or site>");
                    return;
                }

                Address dest = Address.Parse(s);

                if (dest == null)
                {
                    //make a DNS request
                    xClient.Connect(DNSConfig.Server(0));
                    xClient.SendAsk(s);
                    dest = xClient.Receive();
                    xClient.Close();

                    if (dest == null)
                    {
                        Console.WriteLine("ERROR: Cannot find " + s);
                        return;
                    }
                }
                int PacketSent     = 0;
                int PacketReceived = 0;
                int PacketLost     = 0;
                int PercentLoss;
                try
                {
                    Console.WriteLine("Sending ping to " + dest.ToString());

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

                    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 (Exception x)
                {
                    Console.WriteLine("Ping error: " + x.Message);
                }

                PercentLoss = 25 * PacketLost;

                Console.WriteLine();
                Console.WriteLine("Ping statistics for " + dest.ToString() + ":");
                Console.WriteLine("    Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)");
            }
            else if (input.ToLower().StartsWith("ipconfig"))
            {
                ipconfig();
            }
            else if (input.ToLower().StartsWith("help"))
            {
                Console.WriteLine("MishaNetworkDemoOS Commands");
                Console.WriteLine("ipconfig - Gets current IP config");
                Console.WriteLine("getip - Gets IP address of github.com");
                Console.WriteLine("sendpkt - Sends a packet");
                Console.WriteLine("gettime - Get UTC time from time.windows.com");
            }
            else if (string.IsNullOrEmpty(input))
            {
            }
            else
            {
                Console.WriteLine("Unknown command.");
            }
            NetworkStack.Update();
        }
Exemple #24
0
        /// <summary>
        /// CommandEcho
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            if (NetworkStack.ConfigEmpty())
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "No network configuration detected! Use ipconfig /set."));
            }

            int PacketSent     = 0;
            int PacketReceived = 0;
            int PacketLost     = 0;
            int PercentLoss    = 0;

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

            string IPdest = "";

            if (destination == null)
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "Can't parse IP addresses (make sure they are well formated)."));
            }
            else
            {
                try
                {
                    IPdest = destination.ToString();

                    int _deltaT = 0;
                    int second;

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

                    for (int i = 0; i < 4; i++)
                    {
                        second = 0;

                        try
                        {
                            ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working
                            OutgoingBuffer.AddPacket(request);                                                //Aura doesn't work when this is called.
                            NetworkStack.Update();
                        }
                        catch (Exception ex)
                        {
                            return(new ReturnInfo(this, ReturnCode.ERROR, ex.ToString()));
                        }

                        PacketSent++;

                        while (true)
                        {
                            if (ICMPPacket.recvd_reply != null)
                            {
                                if (second < 1)
                                {
                                    Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s");
                                }
                                else if (second >= 1)
                                {
                                    Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s");
                                }

                                PacketReceived++;

                                ICMPPacket.recvd_reply = null;
                                break;
                            }

                            if (second >= 5)
                            {
                                Console.WriteLine("Destination host unreachable.");
                                PacketLost++;
                                break;
                            }

                            if (_deltaT != Cosmos.HAL.RTC.Second)
                            {
                                second++;
                                _deltaT = Cosmos.HAL.RTC.Second;
                            }
                        }
                    }
                }
                catch
                {
                    return(new ReturnInfo(this, ReturnCode.ERROR, "Ping process error."));
                }

                PercentLoss = 25 * PacketLost;

                Console.WriteLine();
                Console.WriteLine("Ping statistics for " + IPdest + ":");
                Console.WriteLine("    Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)");
                return(new ReturnInfo(this, ReturnCode.OK));
            }
        }
Exemple #25
0
        /// <summary>
        /// c = command, c_Ping
        /// </summary>
        /// <param name="arg">IP Address</param>
        /// /// <param name="startIndex">The start index for remove.</param>
        /// <param name="count">The count index for remove.</param>
        public static void c_Ping(string arg, short startIndex = 0, short count = 5)
        {
            string str = arg.Remove(startIndex, count);

            string[] items = str.Split('.');

            if (System.Utils.Misc.IsIpv4Address(items))
            {
                string IPdest = "";

                int PacketSent     = 0;
                int PacketReceived = 0;
                int PacketLost     = 0;

                int PercentLoss = 0;

                try
                {
                    Address destination = new Address((byte)(Int32.Parse(items[0])), (byte)(Int32.Parse(items[1])), (byte)(Int32.Parse(items[2])), (byte)(Int32.Parse(items[3])));
                    Address source      = Config.FindNetwork(destination);


                    IPdest = destination.ToString();

                    int _deltaT = 0;
                    int second;

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

                    for (int i = 0; i < 4; i++)
                    {
                        second = 0;
                        //CustomConsole.WriteLineInfo("Sending ping to " + destination.ToString() + "...");

                        try
                        {
                            //replace address by source
                            //System.Network.IPV4.Address address = new System.Network.IPV4.Address(192, 168, 1, 70);
                            ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working
                            OutgoingBuffer.AddPacket(request);                                                //Aura doesn't work when this is called.
                            NetworkStack.Update();
                        }
                        catch (Exception ex)
                        {
                            CustomConsole.WriteLineError(ex.ToString());
                        }

                        PacketSent++;

                        while (true)
                        {
                            if (ICMPPacket.recvd_reply != null)
                            {
                                //if (ICMPPacket.recvd_reply.SourceIP == destination)
                                //{

                                if (second < 1)
                                {
                                    Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s");
                                }
                                else if (second >= 1)
                                {
                                    Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s");
                                }

                                PacketReceived++;

                                ICMPPacket.recvd_reply = null;
                                break;
                                //}
                            }

                            if (second >= 5)
                            {
                                Console.WriteLine("Destination host unreachable.");
                                PacketLost++;
                                break;
                            }

                            if (_deltaT != Cosmos.HAL.RTC.Second)
                            {
                                second++;
                                _deltaT = Cosmos.HAL.RTC.Second;
                            }
                        }
                    }
                }
                catch
                {
                    L.Text.Display("notcorrectaddress");
                }
                finally
                {
                    PercentLoss = 25 * PacketLost;

                    Console.WriteLine();
                    Console.WriteLine("Ping statistics for " + IPdest + ":");
                    Console.WriteLine("    Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)");
                }
            }
            else
            {
                System.Network.IPV4.UDP.DNS.DNSClient DNSRequest = new System.Network.IPV4.UDP.DNS.DNSClient(53);
                DNSRequest.Ask(str);
                int _deltaT = 0;
                int second  = 0;
                while (!DNSRequest.ReceivedResponse)
                {
                    if (_deltaT != Cosmos.HAL.RTC.Second)
                    {
                        second++;
                        _deltaT = Cosmos.HAL.RTC.Second;
                    }

                    if (second >= 4)
                    {
                        Apps.System.Debugger.debugger.Send("No response in 4 secondes...");
                        break;
                    }
                }
                DNSRequest.Close();
                c_Ping("     " + DNSRequest.address.ToString());
            }
        }
Exemple #26
0
        protected override void BeforeRun()
        {
            Console.Clear();
            #region Register additional network cards
            int i = 1;
            foreach (PCIDevice device in PCI.Devices)
            {
                if ((device.ClassCode == 0x02) && (device.Subclass == 0x00) && // is Ethernet Controller
                    device == PCI.GetDevice(device.bus, device.slot, device.function))
                {
                    Console.WriteLine("Found " + PCIDevice.DeviceClass.GetDeviceString(device) + " on PCI " + device.bus + ":" + device.slot + ":" + device.function);


                    if (device.VendorID == 0x10EC && device.DeviceID == 0x8168)
                    {
                        Console.WriteLine("RTL8168 NIC IRQ: " + device.InterruptLine);

                        var RTL8168Device = new RTL8168(device);

                        RTL8168Device.NameID = "eth" + i;

                        Console.WriteLine("Registered at " + RTL8168Device.NameID + " (" + RTL8168Device.MACAddress.ToString() + ")");

                        RTL8168Device.Enable();
                        i++;
                    }
                }
            }
            foreach (var item in Intel8254X.FindAll())
            {
                item.NameID = "eth" + i;
                item.Enable();
                Console.WriteLine("Registered at " + item.NameID + " (" + item.MACAddress.ToString() + ")");
                i++;
            }
            #endregion
            try
            {
                using (var xClient = new DHCPClient())
                {
                    /** Send a DHCP Discover packet **/
                    //This will automatically set the IP config after DHCP response
                    NetworkStack.Update();
                    int r = xClient.SendDiscoverPacket();
                    if (r == -1)
                    {
                        Console.WriteLine("Failure while configuring DHCP: timeout");
                        xClient.Close();
                        return;
                    }
                    else
                    {
                        Console.WriteLine("DHCP Configure: result: " + r);
                    }
                    xClient.Close();  //don't forget to close!
                }
                ipconfig();
            }
            catch (Exception x)
            {
                Console.WriteLine("err: " + x.Message);
            }

            NTPClient client = new NTPClient();
            var       t      = client.GetNetworkTime();
            Console.WriteLine("Curent time: " + t);

            HTTPClient http     = new HTTPClient("github.com");
            var        responce = http.GET("/test.html");
            Console.WriteLine("====");
            Console.WriteLine(responce);
        }
Exemple #27
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 = Config.FindNetwork(destination);
            }
            else //Make a DNS request if it's not an IP
            {
                var xClient = new DnsClient();
                xClient.Connect(NetworkConfig.CurrentConfig.Value.DefaultDNSServer);
                xClient.SendAsk(arguments[0]);
                destination = xClient.Receive();
                xClient.Close();

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

                source = Config.FindNetwork(destination);
            }

            try
            {
                int _deltaT = 0;
                int second;

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

                for (int i = 0; i < 4; i++)
                {
                    second = 0;

                    try
                    {
                        ICMPEchoRequest request = new ICMPEchoRequest(source, destination, 0x0001, 0x50); //this is working
                        OutgoingBuffer.AddPacket(request);                                                //Aura doesn't work when this is called.
                        NetworkStack.Update();
                    }
                    catch (Exception ex)
                    {
                        return(new ReturnInfo(this, ReturnCode.ERROR, ex.ToString()));
                    }

                    PacketSent++;

                    while (true)
                    {
                        if (ICMPPacket.recvd_reply != null)
                        {
                            if (second < 1)
                            {
                                Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time < 1s");
                            }
                            else if (second >= 1)
                            {
                                Console.WriteLine("Reply received from " + ICMPPacket.recvd_reply.SourceIP.ToString() + " time " + second + "s");
                            }

                            PacketReceived++;

                            ICMPPacket.recvd_reply = null;
                            break;
                        }

                        if (second >= 5)
                        {
                            Console.WriteLine("Destination host unreachable.");
                            PacketLost++;
                            break;
                        }

                        if (_deltaT != Cosmos.HAL.RTC.Second)
                        {
                            second++;
                            _deltaT = Cosmos.HAL.RTC.Second;
                        }
                    }
                }
            }
            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));
        }