Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

fix TCP network info on OSX and add tests for that area - #32830

Merged
wfurt merged 5 commits into
dotnet:masterfrom
wfurt:fix2_32727
Nov 13, 2018
Merged

wfurt merged 5 commits into
dotnet:masterfrom
wfurt:fix2_32727

Conversation

@wfurt

@wfurt wfurt commented Oct 15, 2018

Copy link
Copy Markdown
Member

fixes #32727, related to #32816

The root cause for not finding expected port is fact that kernel returns port in network byte order.
The other problem was that GetActiveTcpListeners() had wrong condition with "tci.State != TcpState.Listen" and got everything but listeners.

Documentation for GetActiveTcpConnections() states that it returns all TCP connections in state other than Listen. That make sense as the listener is really not a connection.

Since there were no tests for the touched functions I added few tests.

@wfurt wfurt added this to the 3.0 milestone Oct 15, 2018
@wfurt wfurt self-assigned this Oct 15, 2018
}

TcpConnectionInformation[] connectionInformations = new TcpConnectionInformation[infoCount];
int skip = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why manage this as skip rather than managing the number of items copied, i.e. have a:

int nextResultIndex = 0;

that you then just increment as part of:

connectionInformations[nextResultIndex++] = ...;

and then you know if you need to resize if nextResultIndex != connectionInformations.Length at the end. I realize it's similar, but it seems cleaner, and you only need to increment in one place.

}
public unsafe override TcpConnectionInformation[] GetActiveTcpConnections()
{
return GetTcpConnections(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit:

return GetTcpConnections(listeners: false);

{
TcpConnectionInformation[] allConnections = GetActiveTcpConnections();
return allConnections.Where(tci => tci.State != TcpState.Listen).Select(tci => tci.RemoteEndPoint).ToArray();
TcpConnectionInformation[] allConnections = GetTcpConnections(true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar nit as above.

{
// OSX does not provide separated TCP-IPv4 and TCP-IPv6 stats.
throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
return new OsxTcpStatistics();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe I missed it... why was this throwing previously if we already had the implementation for it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OSX (and FreeBSD) does not track transport (TCP/UDP) statistic per network protocol (IPv4/6)
For UDP we return same values for both IPv4 and IPv6. For TCP we did return common stats for IPv6 but not for IPv4. That popped when I added basic tests and it looked wrong.

This change makes it consistent even if not 100% correct. Alternatively we can throw for all four cases but that does not seems productive.

TcpStatistics v4TcpSTat = gp.GetTcpIPv4Statistics();
TcpStatistics v6TcpStat = gp.GetTcpIPv6Statistics();
UdpStatistics v4UdpStat = gp.GetUdpIPv4Statistics();
UdpStatistics v6UdpStat = gp.GetUdpIPv6Statistics();

@stephentoub stephentoub Oct 15, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could at least Assert.NotNull for all of these, access properties that won't throw, etc.

foreach (TcpConnectionInformation ti in tcpCconnections)
{
if (ti.LocalEndPoint.Equals(client.LocalEndPoint) && ti.RemoteEndPoint.Equals(client.RemoteEndPoint) &&
(ti.State == TcpState.Established))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Indentation

{
server.Bind(new IPEndPoint(address, 0));
server.Listen(1);
_log.WriteLine("listening on {0}", server.LocalEndPoint);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Use interpolated string format instead?

_log.WriteLine($"listening on {server.LocalEndPoint}");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was commented on before when using interpolated. Is there some advantage or is that just personal or team preference?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In product src it's more questionable, unless it's in some debug-only or trace-only code, as it can lead to extra expenses. But it's also often more readable/maintainable, so in test code it can be preferred.

@wfurt
wfurt requested a review from a team October 16, 2018 02:52
@wfurt

wfurt commented Oct 16, 2018

Copy link
Copy Markdown
Member Author

@dotnet-bot test Outerloop Windows x64 Debug Build
@dotnet-bot test Outerloop Linux x64 Debug Build
@dotnet-bot test Outerloop OSX x64 Debug Build

<Compile Include="IPInterfacePropertiesTest_Linux.cs" />
<Compile Include="IPInterfacePropertiesTest_OSX.cs" />
<Compile Include="IPInterfacePropertiesTest_Windows.cs" />
<Compile Include="IPGlobalPropertiesTest.cs" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this line should come before IPInterfacePropertiesTest_Linux.cs" to keep alphabetical ordering.

@wfurt

wfurt commented Oct 18, 2018

Copy link
Copy Markdown
Member Author

new tests are failing on some platforms. I'll investigate.

@wfurt

wfurt commented Nov 5, 2018

Copy link
Copy Markdown
Member Author

@dotnet-bot test Outerloop Windows x64 Debug Build
@dotnet-bot test Outerloop Linux x64 Debug Build
@dotnet-bot test Outerloop OSX x64 Debug Build

@wfurt

wfurt commented Nov 5, 2018

Copy link
Copy Markdown
Member Author

It looks like we have parsing bug for /proc/net/tcp6. Tracked in #33257
On Windows, all tests were failing with "Access denied" error.

@wfurt

wfurt commented Nov 6, 2018

Copy link
Copy Markdown
Member Author

@dotnet-bot test Outerloop Windows x64 Debug Build
@dotnet-bot test Outerloop Linux x64 Debug Build
@dotnet-bot test Outerloop OSX x64 Debug Build

@wfurt

wfurt commented Nov 6, 2018

Copy link
Copy Markdown
Member Author

Fedora.28 test failures are unrelated.

@wfurt
wfurt merged commit ee0f9aa into dotnet:master Nov 13, 2018
@wfurt
wfurt deleted the fix2_32727 branch November 13, 2018 00:57
jlennox pushed a commit to jlennox/corefx that referenced this pull request Dec 16, 2018
* fix TCP netowrkinfo on OSX and add tests for that area

* feedback from reviews

* feedback from reviews

* disable tests on Linux & Windows

* fix WriteLine format
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…x#32830)

* fix TCP netowrkinfo on OSX and add tests for that area

* feedback from reviews

* feedback from reviews

* disable tests on Linux & Windows

* fix WriteLine format


Commit migrated from dotnet/corefx@ee0f9aa
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mac: IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners() does not list NodeJs listener

4 participants