public void ManualConfig() { Console.WriteLine("Preforming network Manual configuration"); NetworkDevice nic = NetworkDevice.GetDeviceByName("eth0"); //get network device by name IPConfig.Enable(nic, new Address(192, 168, 1, 70), //IP new Address(255, 255, 255, 252), //SUBNET new Address(192, 168, 2, 1) //GATEWAY ); //enable IPv4 configuration Console.WriteLine("Manual config complete"); }
/// <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(); }
/// <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()); }
/* * 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)); }