Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal static void ParseDhcpServerAddressesFromLeasesFile(List<IPAddress> coll
{
if (File.Exists(filePath)) // avoid an exception in most cases if path doesn't already exist
{
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
int leaseIndex = -1;
int secondBrace = -1;
while ((leaseIndex = fileContents.IndexOf("lease", leaseIndex + 1, StringComparison.Ordinal)) != -1)
Expand Down Expand Up @@ -128,7 +128,7 @@ internal static List<IPAddress> ParseWinsServerAddressesFromSmbConfFile(string s
{
if (File.Exists(smbConfFilePath)) // avoid an exception in most cases if path doesn't already exist
{
string fileContents = File.ReadAllText(smbConfFilePath);
string fileContents = ReadAllText(smbConfFilePath);
string label = "wins server = ";
int labelIndex = fileContents.IndexOf(label);
int labelLineStart = fileContents.LastIndexOf(Environment.NewLine, labelIndex, StringComparison.Ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ internal static partial class StringParsingHelpers

internal static int ParseNumSocketConnections(string filePath, string protocolName)
{
if (!File.Exists(filePath))
{
throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
}

// Parse the number of active connections out of /proc/net/sockstat
string sockstatFile = File.ReadAllText(filePath);
string sockstatFile = ReadAllText(filePath);
int indexOfTcp = sockstatFile.IndexOf(protocolName, StringComparison.Ordinal);
int endOfTcpLine = sockstatFile.IndexOf(Environment.NewLine, indexOfTcp + 1, StringComparison.Ordinal);
string tcpLineData = sockstatFile.Substring(indexOfTcp, endOfTcpLine - indexOfTcp);
Expand All @@ -31,15 +26,10 @@ internal static int ParseNumSocketConnections(string filePath, string protocolNa

internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
{
if (!File.Exists(tcp4ConnectionsFile) || !File.Exists(tcp6ConnectionsFile))
{
throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
}

string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);
string tcp4FileContents = ReadAllText(tcp4ConnectionsFile);
string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);
string tcp6FileContents = ReadAllText(tcp6ConnectionsFile);
string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

// First line is header in each file. On WSL, this file may be empty.
Expand Down Expand Up @@ -99,15 +89,10 @@ internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(st

internal static IPEndPoint[] ParseActiveTcpListenersFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
{
if (!File.Exists(tcp4ConnectionsFile) || !File.Exists(tcp6ConnectionsFile))
{
throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
}

string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);
string tcp4FileContents = ReadAllText(tcp4ConnectionsFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we getting rid of the file existence checks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason was to change the type of exception thrown to something more appropriate. PNSE is not mentioned by the docs and feels more appropriate for cases when there is no (implemented?) way of accessing the information in question.

Omitting the file existence checks will still make the code throw, but it will throw NetworkInformationException instead (which is mentioned in the docs), and its inner exception will provide more info to the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the exception type is technically breaking change. But same argument as above could be applicable here. It feels it would be better and perhaps make the helper nullable and deal with lack of the file more gracefully.

string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);
string tcp6FileContents = ReadAllText(tcp6ConnectionsFile);
string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

// First line is header in each file. On WSL, this file may be empty.
Expand Down Expand Up @@ -167,15 +152,10 @@ internal static IPEndPoint[] ParseActiveTcpListenersFromFiles(string tcp4Connect

public static IPEndPoint[] ParseActiveUdpListenersFromFiles(string udp4File, string udp6File)
{
if (!File.Exists(udp4File) || !File.Exists(udp6File))
{
throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
}

string udp4FileContents = File.ReadAllText(udp4File);
string udp4FileContents = ReadAllText(udp4File);
string[] v4connections = udp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

string udp6FileContents = File.ReadAllText(udp6File);
string udp6FileContents = ReadAllText(udp6File);
string[] v6connections = udp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

// First line is header in each file. On WSL, this file may be empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,61 @@ namespace System.Net.NetworkInformation
{
internal static partial class StringParsingHelpers
{
// in some environments (restricted docker container, shared hosting etc.),
// procfs is not accessible and we get UnauthorizedAccessException while the
// inner exception is set to IOException.

internal static string[] ReadAllLines(string filePath)
{
try
{
return File.ReadAllLines(filePath);
}
catch (Exception e)
{
throw CreateNetworkInformationException(e);
}
}

internal static string ReadAllText(string filePath)
{
try
{
return File.ReadAllText(filePath);
}
catch (Exception e)
{
throw CreateNetworkInformationException(e);
}
}


internal static StreamReader OpenStreamReader(string filePath)
{
try
{
return new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false));
}
catch (Exception e)
{
throw CreateNetworkInformationException(e);
}
}

internal static NetworkInformationException CreateNetworkInformationException(Exception inner)
{
// Overload accepting message and inner exception is internal and thus inaccessible in
// the unit test project
#if NETWORKINFORMATION_TEST
return new NetworkInformationException();
#else
return new NetworkInformationException(SR.net_PInvokeError, inner);
#endif
}

internal static int ParseNumRoutesFromRouteFile(string filePath)
{
string routeFile = File.ReadAllText(filePath);
string routeFile = ReadAllText(filePath);
return CountOccurrences(Environment.NewLine, routeFile) - 1; // File includes one-line header
}

Expand All @@ -34,7 +86,7 @@ internal static int ParseNumIPInterfaces(string folderPath)
internal static int ParseDefaultTtlFromFile(string filePath)
{
// snmp6 does not include Default TTL info. Read it from snmp.
string snmp4FileContents = File.ReadAllText(filePath);
string snmp4FileContents = ReadAllText(filePath);
int firstIpHeader = snmp4FileContents.IndexOf("Ip:", StringComparison.Ordinal);
int secondIpHeader = snmp4FileContents.IndexOf("Ip:", firstIpHeader + 1, StringComparison.Ordinal);
int endOfSecondLine = snmp4FileContents.IndexOf(Environment.NewLine, secondIpHeader, StringComparison.Ordinal);
Expand All @@ -49,7 +101,7 @@ internal static int ParseDefaultTtlFromFile(string filePath)
internal static int ParseRawIntFile(string filePath)
{
int ret;
if (!int.TryParse(File.ReadAllText(filePath).Trim(), out ret))
if (!int.TryParse(ReadAllText(filePath).Trim(), out ret))
{
throw ExceptionHelper.CreateForParseFailure();
}
Expand All @@ -60,7 +112,7 @@ internal static int ParseRawIntFile(string filePath)
internal static long ParseRawLongFile(string filePath)
{
long ret;
if (!long.TryParse(File.ReadAllText(filePath).Trim(), out ret))
if (!long.TryParse(ReadAllText(filePath).Trim(), out ret))
{
throw ExceptionHelper.CreateForParseFailure();
}
Expand All @@ -70,7 +122,7 @@ internal static long ParseRawLongFile(string filePath)

internal static int ParseRawHexFileAsInt(string filePath)
{
return Convert.ToInt32(File.ReadAllText(filePath).Trim(), 16);
return Convert.ToInt32(ReadAllText(filePath).Trim(), 16);
}

private static int CountOccurrences(string value, string candidate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ internal static partial class StringParsingHelpers
// Parses ICMP v4 statistics from /proc/net/snmp
public static Icmpv4StatisticsTable ParseIcmpv4FromSnmpFile(string filePath)
{
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
int firstIpHeader = fileContents.IndexOf("Icmp:", StringComparison.Ordinal);
int secondIpHeader = fileContents.IndexOf("Icmp:", firstIpHeader + 1, StringComparison.Ordinal);
int inCsumErrorsIdx = fileContents.IndexOf("InCsumErrors", firstIpHeader + 1, StringComparison.Ordinal);
Expand Down Expand Up @@ -219,7 +219,7 @@ public static Icmpv4StatisticsTable ParseIcmpv4FromSnmpFile(string filePath)

public static Icmpv6StatisticsTable ParseIcmpv6FromSnmp6File(string filePath)
{
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
RowConfigReader reader = new RowConfigReader(fileContents);
bool hasIcmp6OutErrors = fileContents.Contains("Icmp6OutErrors");

Expand Down Expand Up @@ -262,7 +262,7 @@ public static Icmpv6StatisticsTable ParseIcmpv6FromSnmp6File(string filePath)

public static IPGlobalStatisticsTable ParseIPv4GlobalStatisticsFromSnmpFile(string filePath)
{
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);

int firstIpHeader = fileContents.IndexOf("Ip:", StringComparison.Ordinal);
int secondIpHeader = fileContents.IndexOf("Ip:", firstIpHeader + 1, StringComparison.Ordinal);
Expand Down Expand Up @@ -300,7 +300,7 @@ public static IPGlobalStatisticsTable ParseIPv4GlobalStatisticsFromSnmpFile(stri
internal static IPGlobalStatisticsTable ParseIPv6GlobalStatisticsFromSnmp6File(string filePath)
{
// Read the remainder of statistics from snmp6.
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
RowConfigReader reader = new RowConfigReader(fileContents);

return new IPGlobalStatisticsTable()
Expand Down Expand Up @@ -329,7 +329,7 @@ internal static TcpGlobalStatisticsTable ParseTcpGlobalStatisticsFromSnmpFile(st
{
// NOTE: There is no information in the snmp6 file regarding TCP statistics,
// so the statistics are always pulled from /proc/net/snmp.
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
int firstTcpHeader = fileContents.IndexOf("Tcp:", StringComparison.Ordinal);
int secondTcpHeader = fileContents.IndexOf("Tcp:", firstTcpHeader + 1, StringComparison.Ordinal);
int inCsumErrorsIdx = fileContents.IndexOf("InCsumErrors", firstTcpHeader + 1, StringComparison.Ordinal);
Expand Down Expand Up @@ -361,7 +361,7 @@ internal static TcpGlobalStatisticsTable ParseTcpGlobalStatisticsFromSnmpFile(st

internal static UdpGlobalStatisticsTable ParseUdpv4GlobalStatisticsFromSnmpFile(string filePath)
{
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
int firstUdpHeader = fileContents.IndexOf("Udp:", StringComparison.Ordinal);
int secondUdpHeader = fileContents.IndexOf("Udp:", firstUdpHeader + 1, StringComparison.Ordinal);
int inCsumErrorsIdx = fileContents.IndexOf("InCsumErrors", firstUdpHeader + 1, StringComparison.Ordinal);
Expand All @@ -385,7 +385,7 @@ internal static UdpGlobalStatisticsTable ParseUdpv4GlobalStatisticsFromSnmpFile(

internal static UdpGlobalStatisticsTable ParseUdpv6GlobalStatisticsFromSnmp6File(string filePath)
{
string fileContents = File.ReadAllText(filePath);
string fileContents = ReadAllText(filePath);
RowConfigReader reader = new RowConfigReader(fileContents);
bool hasUdp6Errors = fileContents.Contains("Udp6SndbufErrors");

Expand All @@ -403,7 +403,7 @@ internal static UdpGlobalStatisticsTable ParseUdpv6GlobalStatisticsFromSnmp6File

internal static IPInterfaceStatisticsTable ParseInterfaceStatisticsTableFromFile(string filePath, string name)
{
using (StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false)))
using (StreamReader sr = OpenStreamReader(filePath))
{
sr.ReadLine();
sr.ReadLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<IgnoreForCI Condition="'$(TargetOS)' == 'Browser'">true</IgnoreForCI>
<DefineConstants>$(DefineConstants);NETWORKINFORMATION_TEST</DefineConstants>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should name it UnitTest?

</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
Expand Down Expand Up @@ -70,4 +71,4 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
</Project>