예제 #1
0
        public static string Get(string url)
        {
            request +=
                "GET " + GetResource(url) + " HTTP/1.1\n" +
                "Host: " + GetHost(url) + "\n" +
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0\n" +
                "";
            using (var xClient = new DnsClient())
            {
                xClient.Connect(dns); //DNS Server address

                /** Send DNS ask for a single domain name **/
                xClient.SendAsk(GetHost(url));

                /** Receive DNS Response **/
                Address destination = xClient.Receive(); //can set a timeout value

                xClient.Close();
                tcpc.Connect(destination, 80);
                tcpc.Send(Encoding.ASCII.GetBytes(request));
                endPoint.address = destination;
                endPoint.port    = 80;
                return(Encoding.ASCII.GetString(tcpc.Receive(ref endPoint)));
            }
        }
예제 #2
0
        /// <summary>
        /// CommandDns
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            try
            {
                var dnsClient = new DnsClient();
                var tcpClient = new TcpClient(80);

                //Uri uri = new Uri(arguments[0]); Missing plugs

                dnsClient.Connect(DNSConfig.Server(0));
                dnsClient.SendAsk(arguments[0]);
                Address address = dnsClient.Receive();
                dnsClient.Close();

                tcpClient.Connect(address, 80);

                string httpget = "GET / HTTP/1.1\r\n" +
                                 "User-Agent: Wget (CosmosOS)\r\n" +
                                 "Accept: */*\r\n" +
                                 "Accept-Encoding: identity\r\n" +
                                 "Host: " + arguments[0] + "\r\n" +
                                 "Connection: Keep-Alive\r\n\r\n";

                tcpClient.Send(Encoding.ASCII.GetBytes(httpget));

                var ep   = new EndPoint(Address.Zero, 0);
                var data = tcpClient.Receive(ref ep);
                tcpClient.Close();

                string httpresponse = Encoding.ASCII.GetString(data);

                Kernel.console.WriteLine(httpresponse);
            }
            catch (Exception ex)
            {
                Kernel.console.WriteLine("Exception: " + ex);
            }

            return(new ReturnInfo(this, ReturnCode.OK));
        }
예제 #3
0
        /// <summary>
        /// CommandDns
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            var    xClient = new DnsClient();
            string domainname;

            if (arguments.Count < 1 || arguments.Count > 2)
            {
                return(new ReturnInfo(this, ReturnCode.ERROR_ARG));
            }
            else if (arguments.Count == 1)
            {
                xClient.Connect(DNSConfig.Server(0));
                Kernel.console.WriteLine("DNS used : " + DNSConfig.Server(0).ToString());
                xClient.SendAsk(arguments[0]);
                domainname = arguments[0];
            }
            else
            {
                xClient.Connect(Address.Parse(arguments[0]));
                xClient.SendAsk(arguments[1]);
                domainname = arguments[1];
            }

            Address address = xClient.Receive();

            xClient.Close();

            if (address == null)
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "Unable to find " + arguments[0]));
            }
            else
            {
                Kernel.console.WriteLine(domainname + " is " + address.ToString());
            }

            return(new ReturnInfo(this, ReturnCode.OK));
        }
예제 #4
0
        /// <summary>
        /// CommandDns
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            var    xClient = new DnsClient();
            string domainname;

            if (arguments.Count < 1 || arguments.Count > 2)
            {
                return(new ReturnInfo(this, ReturnCode.ERROR_ARG));
            }
            else if (arguments.Count == 1)
            {
                xClient.Connect(NetworkConfig.CurrentConfig.Value.DefaultDNSServer);
                xClient.SendAsk(arguments[0]);
                domainname = arguments[0];
            }
            else
            {
                xClient.Connect(Address.Parse(arguments[0]));
                xClient.SendAsk(arguments[1]);
                domainname = arguments[1];
            }

            Address address = xClient.Receive();

            xClient.Close();

            if (address == null)
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "Unable to get URL for " + arguments[0]));
            }
            else
            {
                Console.WriteLine(domainname + " is " + address.ToString());
            }

            return(new ReturnInfo(this, ReturnCode.OK));
        }
예제 #5
0
        /// <summary>
        /// Gets IP address of a domain. The parameter can be also an IP address
        /// </summary>
        /// <param name="s">A domain or an IP. For example: google.com or 8.8.8.8</param>
        /// <returns>An IP address</returns>
        public static Address GetAddressFromName(string s)
        {
            Address a = null;

            try
            {
                a = Address.Parse(s);
            }
            catch
            {
            }
            if (a != null)
            {
                return(a);
            }

            dnsClient.Connect(new Address(8, 8, 8, 8)); //DNS Server address
            /** Send DNS ask for a single domain name **/
            dnsClient.SendAsk(s);
            /** Receive DNS Response **/
            Address destination = dnsClient.Receive(); //can set a timeout value

            return(destination);
        }
예제 #6
0
        /// <summary>
        /// CommandEcho
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            int PacketSent     = 0;
            int PacketReceived = 0;
            int PacketLost     = 0;
            int PercentLoss;

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

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

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

                source = IPConfig.FindNetwork(destination);
            }

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

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

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

                    PacketSent++;

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

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

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

                        PacketReceived++;
                    }
                }

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

            PercentLoss = 25 * PacketLost;

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

            return(new ReturnInfo(this, ReturnCode.OK));
        }
예제 #7
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));
        }
예제 #8
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();
        }