/// <summary> /// adds a new HoatNIC to the arraylist /// </summary> /// <param name="nic">interface as HostNIC</param> public void AddInterface(HostNIC nic) { this.interfaces.Add(nic); IOController.Log(this, "Added Interface at " + (this.interfaces.Count - 1) + ":\n" + nic.ToString() + "\n", IOController.Flag.status); if (InterfaceAddedEvt != null) { InterfaceAddedEvt(this.interfaces, new PropertyChangedEventArgs("InterfaceAdded")); } }
// region for interface selection #region interface selection //################################################################### interface selection /// <summary> /// selects the inface with the given index /// </summary> /// <param name="i">index of the interface</param> public void SelectInterface(int i) { if (i < Interfaces.Count) { IOController.Log(this, "SelectInterface called index:" + i, IOController.Flag.debug); HostNIC nic = (HostNIC)Interfaces[i]; this.HostIP = nic.IPAddress; this.HostSubnetMask = nic.SubnetMask; this.HostSubnet = nic.SubnetIdentifier; this.HostHasStaticIp = nic.StaticIPAddress; this.HostGateway = nic.Gateway; this.SelectedInterfaceIndex = i; this.SelectedInterfaceID = nic.Id; IOController.Log(this, "SelectInterface ID" + nic.Id, IOController.Flag.debug); this.OverviewSelectedInterfaceName = nic.Name; IOController.Log(this, nic.ToString(), IOController.Flag.status); } else { IOController.Log(this, "SelectInterface IndexOutOfRange index:" + i, IOController.Flag.error); } }
/// <summary> /// retrieves information about the host computer such as network interfaces, IP addresses etc. and stores the information in settings /// </summary> public void GetHostInfo() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); NetCalcTool nct = settings.HostNetCalcTool; // loop through all network interfaces foreach (NetworkInterface adapter in nics) { //check if interface is loopback if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback) { //skip if loopback continue; } //check if adapter is up if (adapter.OperationalStatus != OperationalStatus.Up) { //if not up skip this interface continue; } //create new HostNIC HostNIC nic = new HostNIC(); //get name, id, hardware address, interface type nic.Name = adapter.Name; nic.Id = adapter.Id; nic.MacAddress = adapter.GetPhysicalAddress().ToString(); nic.Type = adapter.NetworkInterfaceType.ToString(); //get ipv4 status nic.Ipv4Enabled = adapter.Supports(NetworkInterfaceComponent.IPv4); if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { nic.StaticIPAddress = !adapter.GetIPProperties().GetIPv4Properties().IsDhcpEnabled; // get gateway addresses and set first if present GatewayIPAddressInformationCollection gateways = adapter.GetIPProperties().GatewayAddresses; if (gateways.Count > 0) { nic.Gateway = gateways[0].Address.ToString(); } // get unicast addresses foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) { // check if ipv4 address if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) { // set ip adress nic.IPAddress = unicastIPAddressInformation.Address.ToString(); // get subnet mask String snetMask; try { // IPv4Mask not implemented in mono at current state snetMask = unicastIPAddressInformation.IPv4Mask.ToString(); } catch (NotImplementedException e) { IOController.Log(this, "failed to get subnetmask with unicastIPAddressInformation.IPv4Mask, using workaround", Flag.status); // workaround to get subnetmask in unix environment with mono using the custom SubnetMask class snetMask = ""; snetMask = SubnetMask.GetIPv4Mask(adapter.Name); } nic.SubnetMask = snetMask; } } //try to get the dns addresses of adapter try { IPAddressCollection dnsAddresses = adapter.GetIPProperties().DnsAddresses; if (dnsAddresses.Count >= 1) { nic.PrimaryDNS = dnsAddresses[0].ToString(); } if (dnsAddresses.Count >= 2) { nic.SecondaryDNS = dnsAddresses[0].ToString(); } } catch (Exception e) { IOController.Log(this, "failed to get DNS server addresses " + e, Flag.error); } //calculate network id try { nct.calculate(nic.IPAddress, nic.SubnetMask); nic.SubnetIdentifier = nct.NetworkId; } catch (Exception e) { IOController.Log(this, "failed calculate network address " + e, Flag.error); } } //add NIC to settings settings.AddInterface(nic); } }