mirror of
https://github.com/netchx/netch.git
synced 2026-03-18 18:13:21 +08:00
47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
|
|
// TODO: Remove if fixed
|
|
// This code is from https://github.com/dotnet/corefx
|
|
// Will be removed whenever the bugs reading network information on linux are fixed and
|
|
// I can use the Managed version.
|
|
|
|
namespace DnsClient.Linux
|
|
{
|
|
internal static partial class StringParsingHelpers
|
|
{
|
|
internal static string ParseDnsSuffixFromResolvConfFile(string filePath)
|
|
{
|
|
string data = File.ReadAllText(filePath);
|
|
RowConfigReader rcr = new RowConfigReader(data);
|
|
string dnsSuffix;
|
|
|
|
return rcr.TryGetNextValue("search", out dnsSuffix) ? dnsSuffix : string.Empty;
|
|
}
|
|
|
|
internal static List<IPAddress> ParseDnsAddressesFromResolvConfFile(string filePath)
|
|
{
|
|
// 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(filePath);
|
|
RowConfigReader rcr = new RowConfigReader(data);
|
|
List<IPAddress> addresses = new List<IPAddress>();
|
|
|
|
string addressString = null;
|
|
while (rcr.TryGetNextValue("nameserver", out addressString))
|
|
{
|
|
IPAddress parsedAddress;
|
|
if (IPAddress.TryParse(addressString, out parsedAddress))
|
|
{
|
|
addresses.Add(parsedAddress);
|
|
}
|
|
}
|
|
|
|
return addresses;
|
|
}
|
|
}
|
|
} |