private IPAddressCollection GetWinsServerAddresses() { InternalIPAddressCollection collection = new InternalIPAddressCollection(); try { string fileContents = File.ReadAllText(NetworkFiles.SmbConfFile); string label = "wins server = "; int labelIndex = fileContents.IndexOf(label); int labelLineStart = fileContents.LastIndexOf(Environment.NewLine, labelIndex); if (labelLineStart < labelIndex) { int commentIndex = fileContents.IndexOf(';', labelLineStart, labelIndex - labelLineStart); if (commentIndex != -1) { return(collection); } } int endOfLine = fileContents.IndexOf(Environment.NewLine, labelIndex); string addressString = fileContents.Substring(labelIndex + label.Length, endOfLine - (labelIndex + label.Length)); IPAddress address = IPAddress.Parse(addressString); collection.InternalAdd(address); } catch { // If any parsing or file reading exception occurs, just ignore it and return the collection. } return(collection); }
private IPAddressCollection GetDhcpServerAddresses() { // Parse the /var/lib/dhcp/dhclient.leases file, if it exists. // If any errors occur, like the file not existing or being // improperly formatted, just bail and return an empty collection. InternalIPAddressCollection collection = new InternalIPAddressCollection(); try { string fileContents = File.ReadAllText(NetworkFiles.DHClientLeasesFile); int leaseIndex = -1; int secondBrace = -1; while ((leaseIndex = fileContents.IndexOf("lease", leaseIndex + 1)) != -1) { int firstBrace = fileContents.IndexOf("{", leaseIndex); secondBrace = fileContents.IndexOf("}", leaseIndex); int blockLength = secondBrace - firstBrace; int interfaceIndex = fileContents.IndexOf("interface", firstBrace, blockLength); int afterName = fileContents.IndexOf(';', interfaceIndex); int beforeName = fileContents.LastIndexOf(' ', afterName); string interfaceName = fileContents.Substring(beforeName + 2, afterName - beforeName - 3); if (interfaceName != _linuxNetworkInterface.Name) { continue; } int indexOfDhcp = fileContents.IndexOf("dhcp-server-identifier", firstBrace, blockLength); int afterAddress = fileContents.IndexOf(";", indexOfDhcp); int beforeAddress = fileContents.LastIndexOf(' ', afterAddress); string dhcpAddressString = fileContents.Substring(beforeAddress + 1, afterAddress - beforeAddress - 1); IPAddress dhcpAddress; if (IPAddress.TryParse(dhcpAddressString, out dhcpAddress)) { collection.InternalAdd(dhcpAddress); } } } catch { // If any parsing or file reading exception occurs, just ignore it and return the collection. } return(collection); }
internal SystemIPInterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) { _adapterFlags = ipAdapterAddresses.flags; _dnsSuffix = ipAdapterAddresses.dnsSuffix; _dnsEnabled = fixedInfo.enableDns; _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0); _multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); _dnsAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); _anycastAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); _unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); _winsServersAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); _gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); _dhcpServers = new InternalIPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); } if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv4Enabled) != 0) { _ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0) { _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
internal SystemIPInterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) { _adapterFlags = ipAdapterAddresses.flags; _dnsSuffix = ipAdapterAddresses.dnsSuffix; _dnsEnabled = fixedInfo.enableDns; _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0); _multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); _dnsAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); _anycastAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); _unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); _winsServersAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); _gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); _dhcpServers = new InternalIPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); } if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv4Enabled) != 0) { _ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0) { _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
private static IPAddressCollection GetDnsAddresses() { // Parse /etc/resolv.conf for all of the "nameserver" entries. // These are the DNS servers the machine is configured to use. // On OSX, this file is not directly used by most processes for DNS // queries/routing, but it is automatically generated instead, with // the machine's DNS servers listed in it. string data = File.ReadAllText(NetworkFiles.EtcResolvConfFile); RowConfigReader rcr = new RowConfigReader(data); InternalIPAddressCollection addresses = new InternalIPAddressCollection(); string addressString = null; while (rcr.TryGetNextValue("nameserver", out addressString)) { IPAddress parsedAddress; if (IPAddress.TryParse(addressString, out parsedAddress)) { addresses.InternalAdd(parsedAddress); } } return(addresses); }
private static IPAddressCollection GetDnsAddresses() { // Parse /etc/resolv.conf for all of the "nameserver" entries. // These are the DNS servers the machine is configured to use. // On OSX, this file is not directly used by most processes for DNS // queries/routing, but it is automatically generated instead, with // the machine's DNS servers listed in it. string data = File.ReadAllText(NetworkFiles.EtcResolvConfFile); RowConfigReader rcr = new RowConfigReader(data); InternalIPAddressCollection addresses = new InternalIPAddressCollection(); string addressString = null; while (rcr.TryGetNextValue("nameserver", out addressString)) { IPAddress parsedAddress; if (IPAddress.TryParse(addressString, out parsedAddress)) { addresses.InternalAdd(parsedAddress); } } return addresses; }
private IPAddressCollection GetDhcpServerAddresses() { // Parse the /var/lib/dhcp/dhclient.leases file, if it exists. // If any errors occur, like the file not existing or being // improperly formatted, just bail and return an empty collection. InternalIPAddressCollection collection = new InternalIPAddressCollection(); try { string fileContents = File.ReadAllText(NetworkFiles.DHClientLeasesFile); int leaseIndex = -1; int secondBrace = -1; while ((leaseIndex = fileContents.IndexOf("lease", leaseIndex + 1)) != -1) { int firstBrace = fileContents.IndexOf("{", leaseIndex); secondBrace = fileContents.IndexOf("}", leaseIndex); int blockLength = secondBrace - firstBrace; int interfaceIndex = fileContents.IndexOf("interface", firstBrace, blockLength); int afterName = fileContents.IndexOf(';', interfaceIndex); int beforeName = fileContents.LastIndexOf(' ', afterName); string interfaceName = fileContents.Substring(beforeName + 2, afterName - beforeName - 3); if (interfaceName != _linuxNetworkInterface.Name) { continue; } int indexOfDhcp = fileContents.IndexOf("dhcp-server-identifier", firstBrace, blockLength); int afterAddress = fileContents.IndexOf(";", indexOfDhcp); int beforeAddress = fileContents.LastIndexOf(' ', afterAddress); string dhcpAddressString = fileContents.Substring(beforeAddress + 1, afterAddress - beforeAddress - 1); IPAddress dhcpAddress; if (IPAddress.TryParse(dhcpAddressString, out dhcpAddress)) { collection.InternalAdd(dhcpAddress); } } } catch { // If any parsing or file reading exception occurs, just ignore it and return the collection. } return collection; }
private IPAddressCollection GetWinsServerAddresses() { InternalIPAddressCollection collection = new InternalIPAddressCollection(); try { string fileContents = File.ReadAllText(NetworkFiles.SmbConfFile); string label = "wins server = "; int labelIndex = fileContents.IndexOf(label); int labelLineStart = fileContents.LastIndexOf(Environment.NewLine, labelIndex); if (labelLineStart < labelIndex) { int commentIndex = fileContents.IndexOf(';', labelLineStart, labelIndex - labelLineStart); if (commentIndex != -1) { return collection; } } int endOfLine = fileContents.IndexOf(Environment.NewLine, labelIndex); string addressString = fileContents.Substring(labelIndex + label.Length, endOfLine - (labelIndex + label.Length)); IPAddress address = IPAddress.Parse(addressString); collection.InternalAdd(address); } catch { // If any parsing or file reading exception occurs, just ignore it and return the collection. } return collection; }
internal static InternalIPAddressCollection MarshalIpAddressCollection(IntPtr ptr) { InternalIPAddressCollection addressList = new InternalIPAddressCollection(); while (ptr != IntPtr.Zero) { IpAdapterAddress addressStructure = Marshal.PtrToStructure<IpAdapterAddress>(ptr); IPAddress address = addressStructure.address.MarshalIPAddress(); addressList.InternalAdd(address); ptr = addressStructure.next; } return addressList; }