예제 #1
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);
     }
 }
예제 #2
0
        /// <summary>
        /// Get network time, in UTC.
        /// </summary>
        /// <returns>network time, in UTC</returns>

        public static void Init()
        {
            using (var xClient = new DHCPClient())
            {
                xClient.SendDiscoverPacket();

                xClient.Close();
            }
        }
예제 #3
0
        /// <summary>
        /// CommandIPConfig
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            var xClient = new DHCPClient();

            if (arguments[0] == "/release")
            {
                xClient.SendReleasePacket();
            }
            else if (arguments[0] == "/ask")
            {
                if (xClient.SendDiscoverPacket() != -1)
                {
                    Console.WriteLine("Configuration applied! Your local IPv4 Address is " + NetworkConfig.CurrentConfig.Value.IPAddress.ToString() + ".");
                }
                else
                {
                    return(new ReturnInfo(this, ReturnCode.ERROR, "DHCP Discover failed. Can't apply dynamic IPv4 address."));
                }
            }
            else if (arguments[0] == "/listnic")
            {
                foreach (var device in NetworkDevice.Devices)
                {
                    switch (device.CardType)
                    {
                    case CardType.Ethernet:
                        Console.WriteLine("Ethernet Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")");
                        break;

                    case CardType.Wireless:
                        Console.WriteLine("Wireless Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")");
                        break;
                    }
                }
            }
            else if (arguments[0] == "/set")
            {
                if ((arguments.Count == 3) || (arguments.Count == 4)) // ipconfig /set eth0 192.168.1.2/24 {gw}
                {
                    string[]      adrnetwork = arguments[2].Split('/');
                    Address       ip         = Address.Parse(adrnetwork[0]);
                    NetworkDevice nic        = NetworkDevice.GetDeviceByName(arguments[1]);
                    Address       gw         = null;
                    if (arguments.Count == 4)
                    {
                        gw = Address.Parse(arguments[3]);
                    }

                    int cidr;
                    try
                    {
                        cidr = int.Parse(adrnetwork[1]);
                    }
                    catch (Exception ex)
                    {
                        return(new ReturnInfo(this, ReturnCode.ERROR, ex.Message));
                    }
                    Address subnet = Address.CIDRToAddress(cidr);

                    if (nic == null)
                    {
                        return(new ReturnInfo(this, ReturnCode.ERROR, "Couldn't find network device: " + arguments[1]));
                    }

                    if (ip != null && subnet != null && gw != null)
                    {
                        IPConfig.Enable(nic, ip, subnet, gw);
                        Console.WriteLine("Config OK!");
                    }
                    else if (ip != null && subnet != null)
                    {
                        IPConfig.Enable(nic, ip, subnet, ip);
                        Console.WriteLine("Config OK!");
                    }
                    else
                    {
                        return(new ReturnInfo(this, ReturnCode.ERROR, "Can't parse IP addresses (make sure they are well formated)."));
                    }
                }
                else
                {
                    return(new ReturnInfo(this, ReturnCode.ERROR, "Usage : ipconfig /set {device} {IPv4/CIDR} {Gateway|null}"));
                }
            }
            else if (arguments[0] == "/nameserver")
            {
                if (arguments[1] == "-add")
                {
                    DNSConfig.Add(Address.Parse(arguments[2]));
                    Console.WriteLine(arguments[2] + " has been added to nameservers.");
                }
                else if (arguments[1] == "-rem")
                {
                    DNSConfig.Remove(Address.Parse(arguments[2]));
                    Console.WriteLine(arguments[2] + " has been removed from nameservers list.");
                }
                else
                {
                    return(new ReturnInfo(this, ReturnCode.ERROR, "Usage : ipconfig /nameserver {add|remove} {IP}"));
                }
            }
            else
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "Wrong usage, please type: ipconfig /help"));
            }
            return(new ReturnInfo(this, ReturnCode.OK));
        }
예제 #4
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);
        }
예제 #5
0
        /// <summary>
        /// CommandIPConfig
        /// </summary>
        /// <param name="arguments">Arguments</param>
        public override ReturnInfo Execute(List <string> arguments)
        {
            if (arguments[0] == "/release")
            {
                DHCPClient.SendReleasePacket();
            }
            else if (arguments[0] == "/ask")
            {
                DHCPClient.SendDiscoverPacket();
            }
            else if (arguments[0] == "/listnic")
            {
                foreach (var device in NetworkDevice.Devices)
                {
                    switch (device.CardType)
                    {
                    case CardType.Ethernet:
                        Console.WriteLine("Ethernet Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")");
                        break;

                    case CardType.Wireless:
                        Console.WriteLine("Wireless Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")");
                        break;
                    }
                }
            }
            else if (arguments[0] == "/set")
            {
                if (arguments.Count < 5)
                {
                    return(new ReturnInfo(this, ReturnCode.ERROR, "Usage : ipconfig /set {device} {IPv4} {Subnet} {Gateway}"));
                }
                else
                {
                    Address       ip     = Address.Parse(arguments[2]);
                    Address       subnet = Address.Parse(arguments[3]);
                    Address       gw     = Address.Parse(arguments[4]);
                    NetworkDevice nic    = NetworkDevice.GetDeviceByName(arguments[1]);

                    if (nic == null)
                    {
                        return(new ReturnInfo(this, ReturnCode.ERROR, "Couldn't find network device: " + arguments[1]));
                    }
                    if (ip != null && subnet != null && gw != null)
                    {
                        NetworkInit.Enable(nic, ip, subnet, gw, gw);
                        Console.WriteLine("Config OK!");
                    }
                    else
                    {
                        return(new ReturnInfo(this, ReturnCode.ERROR, "Can't parse IP addresses (make sure they are well formated)."));
                    }

                    /*if (NetworkInterfaces.Interface(arguments[1]) != "null")
                     * {
                     *  Utils.Settings settings = new Utils.Settings(@"0:\System\" + NetworkInterfaces.Interface(arguments[1]) + ".conf");
                     *  NetworkStack.RemoveAllConfigIP();
                     *  ApplyIP(arguments.ToArray(), settings); //TODO Fix that (arguments)
                     *  settings.Push();
                     * }
                     * else
                     * {
                     *  Console.WriteLine("This interface doesn't exists.");
                     * } */
                }
            }
            else
            {
                return(new ReturnInfo(this, ReturnCode.ERROR, "Wrong usage, please type: ipconfig /help"));
            }
            return(new ReturnInfo(this, ReturnCode.OK));
        }