Exemple #1
0
        /// <summary>
        /// Initialize the Network Stack to prepare it for operation.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
        public static void Init()
        {
            AddressMap = new TempDictionary <NetworkDevice>();

            // VMT Scanner issue workaround
            ARPPacket.VMTInclude();
            ARPPacket_Ethernet.VMTInclude();
            ARPReply_Ethernet.VMTInclude();
            ARPRequest_Ethernet.VMTInclude();
            ICMPPacket.VMTInclude();
            ICMPEchoReply.VMTInclude();
            ICMPEchoRequest.VMTInclude();
            UDPPacket.VMTInclude();
            //TCPPacket.VMTInclude();
        }
Exemple #2
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 #3
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)
        {
            if (Misc.IsIpv4Address(arg))
            {
                string[] items = arg.Split('.');

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

                int PercentLoss = 0;

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

                    int _deltaT = 0;
                    int second;

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

                        System.Utilities.WaitSeconds(1);

                        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)
                        {
                            Console.WriteLine("Error on ping " + i + ": " + ex);
                        }

                        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
                {
                    Console.WriteLine("Internal error.");
                }
                finally
                {
                    PercentLoss = 25 * PacketLost;

                    Console.WriteLine();
                    Console.WriteLine("Ping statistics for " + arg + ":");
                    Console.WriteLine("    Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)");
                }
            }
            else
            {
                Network.IPV4.UDP.DNS.DNSClient DNSRequest = new Network.IPV4.UDP.DNS.DNSClient(53);
                DNSRequest.Ask(arg);
                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 #4
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));
        }
Exemple #5
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());
            }
        }