Skip to content

Commit

Permalink
look for dhcp lease file in multiple locations (dotnet#41016)
Browse files Browse the repository at this point in the history
  • Loading branch information
wfurt authored Sep 3, 2020
1 parent 171082e commit 341b8b2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ public GatewayIPAddressInformationCollection GetGatewayAddresses(LinuxNetworkInt

private IPAddressCollection GetDhcpServerAddresses()
{
List<IPAddress> internalCollection
= StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(NetworkFiles.DHClientLeasesFile, _linuxNetworkInterface.Name);
List<IPAddress> internalCollection = new List<IPAddress>();

StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, NetworkFiles.DHClientLeasesFile, _linuxNetworkInterface.Name);
StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, string.Format(NetworkFiles.DHClientInterfaceLeasesFile, _linuxNetworkInterface.Name), _linuxNetworkInterface.Name);
StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, string.Format(NetworkFiles.DHClientSecondaryInterfaceLeasesFile, _linuxNetworkInterface.Name), _linuxNetworkInterface.Name);

return new InternalIPAddressCollection(internalCollection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal static class NetworkFiles
public const string Udp4ConnectionsFile = "/proc/net/udp";
public const string Udp6ConnectionsFile = "/proc/net/udp6";
public const string DHClientLeasesFile = "/var/lib/dhcp/dhclient.leases";
public const string DHClientInterfaceLeasesFile = "/var/lib/dhcp/dhclient.{0}.leases";
public const string DHClientSecondaryInterfaceLeasesFile = "/var/lib/dhcp/dhclient6.{0}.leases";
public const string SmbConfFile = "/etc/samba/smb.conf";

// Individual file names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ internal static void ParseIPv6GatewayAddressesFromRouteFile(List<GatewayIPAddres
}
}

internal static List<IPAddress> ParseDhcpServerAddressesFromLeasesFile(string filePath, string name)
internal static void ParseDhcpServerAddressesFromLeasesFile(List<IPAddress> collection, string filePath, string name)
{
// 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.
List<IPAddress> collection = new List<IPAddress>();
try
{
if (File.Exists(filePath)) // avoid an exception in most cases if path doesn't already exist
Expand Down Expand Up @@ -123,8 +122,6 @@ internal static List<IPAddress> ParseDhcpServerAddressesFromLeasesFile(string fi
{
// If any parsing or file reading exception occurs, just ignore it and return the collection.
}

return collection;
}

internal static List<IPAddress> ParseWinsServerAddressesFromSmbConfFile(string smbConfFilePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void DhcpServerAddressParsing()
{
string fileName = GetTestFilePath();
FileUtil.NormalizeLineEndings("NetworkFiles/dhclient.leases", fileName);
List<IPAddress> dhcpServerAddresses = StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(fileName, "wlan0");
List<IPAddress> dhcpServerAddresses = new List<IPAddress>();
StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(dhcpServerAddresses, fileName, "wlan0");
Assert.Equal(1, dhcpServerAddresses.Count);
Assert.Equal(IPAddress.Parse("10.105.128.4"), dhcpServerAddresses[0]);
}
Expand Down

0 comments on commit 341b8b2

Please sign in to comment.