Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public override bool SendMessage(string message, string mobileMessage)
        {
            if (ClientType == ClientType.Android)
            {
                if (mobileMessage.Length > 0)
                {
                    if (mobileMessage == "dupe")
                    {
                        OutgoingBuffer.Enqueue("t=output~txt=" + message);
                    }
                    else
                    {
                        OutgoingBuffer.Enqueue(mobileMessage);
                    }
                }
            }
            else
            {
                if (message.Length > 0)
                {
                    OutgoingBuffer.Enqueue(message);
                }
            }

            return(true);
        }
Ejemplo n.º 3
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;
            }
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 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.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;
        }
Ejemplo n.º 6
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!");
            }
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
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();
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Flushes the entire outbound buffer to the Player's connection.
        /// If the Player is not connected, nothing is done.
        /// </summary>
        /// <returns></returns>
        public bool SendOutgoingBuffer()
        {
            if (OutgoingBuffer.Count == 0)
            {
                return(false);
            }

            string message;

            while (true)
            {
                if (Connection == null)
                {
                    return(false);
                }

                if (Connection.Writer == null)
                {
                    return(false);
                }

                if (OutgoingBuffer.Count == 0)
                {
                    break;
                }

                message = OutgoingBuffer.Dequeue();

                try
                {
                    // save bandwidth by clearing color codes only on newlines
                    //TODO: check CPU usage
                    if (message.EndsWith("\n\r"))
                    {
                        Connection.Writer.Write(message + Colors.CLEAR);
                    }
                    else
                    {
                        Connection.Writer.Write(message);
                    }
                }
                catch (Exception e)
                {
                    Connection.Writer = null;
                    return(false);
                }
            }

            if (ClientType != ClientType.Android)
            {
                Connection.Writer.Write("\n\r");
                Connection.Writer.Write(GetPrompt());
            }

            return(true);
        }
Ejemplo n.º 10
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());
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Send TCP packet.
        /// </summary>
        private void SendPacket(TCPPacket packet)
        {
            OutgoingBuffer.AddPacket(packet);
            NetworkStack.Update();

            if (packet.SYN || packet.FIN)
            {
                TCB.SndNxt++;
            }
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
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;
            }
        }
Ejemplo n.º 14
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();
        }
Ejemplo n.º 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()
        {
            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;
            }
        }
Ejemplo n.º 16
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();
        }
Ejemplo n.º 17
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);
 }
Ejemplo n.º 18
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());
        }
Ejemplo n.º 19
0
        internal static void ARPHandler(byte[] packetData)
        {
            ARPPacket arp_packet = new ARPPacket(packetData);

            //Sys.Console.WriteLine("Received ARP Packet");
            //Sys.Console.WriteLine(arp_packet.ToString());
            if (arp_packet.Operation == 0x01)
            {
                if ((arp_packet.HardwareType == 1) && (arp_packet.ProtocolType == 0x0800))
                {
                    ARPRequest_Ethernet arp_request = new ARPRequest_Ethernet(packetData);
                    if (arp_request.SenderIP == null)
                    {
                        NetworkStack.debugger.Send("SenderIP null in ARPHandler!");
                    }
                    arp_request = new ARPRequest_Ethernet(packetData);

                    ARPCache.Update(arp_request.SenderIP, arp_request.SenderMAC);

                    if (NetworkStack.AddressMap.ContainsKey(arp_request.TargetIP.Hash) == true)
                    {
                        NetworkStack.debugger.Send("ARP Request Recvd from " + arp_request.SenderIP.ToString());
                        NetworkDevice nic = NetworkStack.AddressMap[arp_request.TargetIP.Hash];

                        ARPReply_Ethernet reply =
                            new ARPReply_Ethernet(nic.MACAddress, arp_request.TargetIP, arp_request.SenderMAC, arp_request.SenderIP);

                        nic.QueueBytes(reply.RawData);
                    }
                }
            }
            else if (arp_packet.Operation == 0x02)
            {
                if ((arp_packet.HardwareType == 1) && (arp_packet.ProtocolType == 0x0800))
                {
                    ARPReply_Ethernet arp_reply = new ARPReply_Ethernet(packetData);
                    NetworkStack.debugger.Send("Received ARP Reply");
                    NetworkStack.debugger.Send(arp_reply.ToString());
                    NetworkStack.debugger.Send("ARP Reply Recvd from " + arp_reply.SenderIP.ToString());
                    ARPCache.Update(arp_reply.SenderIP, arp_reply.SenderMAC);

                    OutgoingBuffer.ARPCache_Update(arp_reply);
                }
            }
        }
Ejemplo n.º 20
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();
            }
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Called continously to keep the Network Stack going.
 /// </summary>
 /// <exception cref="ArgumentException">Thrown on fatal error (contact support).</exception>
 /// <exception cref="ArgumentOutOfRangeException">Thrown on memory error.</exception>
 /// <exception cref="OverflowException">Thrown if data length of any packet in the queue is bigger than Int32.MaxValue.</exception>
 public static void Update()
 {
     OutgoingBuffer.Send();
 }
Ejemplo n.º 22
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());
            }
        }
Ejemplo n.º 23
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));
        }
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 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)
        {
            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());
            }
        }