/* * Method called to applied the differents options received in the DHCP packet ACK **/ /// <summary> /// Apply the new IP configuration received. /// </summary> /// <param name="Options">DHCPOption class using the packetData from the received dhcp packet.</param> /// <param name="message">Enable/Disable the displaying of messages about DHCP applying and conf. Disabled by default. /// </param> private void Apply(DHCPAck packet, bool message = false) { NetworkStack.RemoveAllConfigIP(); //cf. Roadmap. (have to change this, because some network interfaces are not configured in dhcp mode) [have to be done in 0.5.x] foreach (NetworkDevice networkDevice in NetworkDevice.Devices) { if (packet.Client.ToString() == null || packet.Client.ToString() == null || packet.Client.ToString() == null || packet.Client.ToString() == null) { throw new Exception("Parsing DHCP ACK Packet failed, can't apply network configuration."); } else { if (message) { Global.mDebugger.Send("[DHCP ACK][" + networkDevice.Name + "] Packet received, applying IP configuration..."); Global.mDebugger.Send(" IP Address : " + packet.Client.ToString()); Global.mDebugger.Send(" Subnet mask : " + packet.Subnet.ToString()); Global.mDebugger.Send(" Gateway : " + packet.Server.ToString()); Global.mDebugger.Send(" DNS server : " + packet.DNS.ToString()); } IPConfig.Enable(networkDevice, packet.Client, packet.Subnet, packet.Server); DNSConfig.Add(packet.DNS); if (message) { Global.mDebugger.Send("[DHCP CONFIG][" + networkDevice.Name + "] IP configuration applied."); asked = false; } } } Close(); }
/// <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)); }