-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type and content inconsistency between TcpClient.Client.LocalEndPoint/RemoteEndPoint and TcpConnectionInformation.LocalEndPoint/RemoteEndPoint #91137
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionWhen attempting to identify a TCP connection from On a Windows 11 host with both IPv4 and IPv6 enabled,
Whereas after retrieving the connection information, it returns something different:
It appears Is there a way to identify the connection from Reproduction StepsDocumented here: https://stackoverflow.com/questions/76979491/matching-tcp-connections-between-tcpclient-and-tcpconnectioninformation and also above Expected behavior
Actual behavior
Regression?Unknown Known WorkaroundsFor Configuration.NET 7 Other informationNA
|
That depends on how you create the |
What if the TcpClient was retrieved from a server listener? E.g. TcpClient tcpClient = await _Listener.AcceptTcpClientAsync().ConfigureAwait(false); |
I would expect the socket The TCP list just dumps what ever connections are out there. If you cannot control the listener you would need to convert the addresses as needed. |
Sorry to keep bugging you with this; I'm trying to find a way to match connections in a reliable way across platforms, regardless of whether the address is IPv4 or IPv6, and hopefully without having to do a bunch of I have control over the TcpListener(int port);
TcpListener(IPEndPoint localEP);
TcpListener(IPAddress localaddr, int port); I don't see any way to influence whether the listener is IPv4 or IPv6 (directly), unless the What about situations where you want to listen on multiple interfaces and choose a listener of There has to be some means of accomplishing this :) |
yes
IPAddress.Any means IPv4. IPAddress.Ipv6Any means IPV6. Do note that in dualmode all IPAddresses you will get back from dotnet will be IPv6, which means for IPv4 clients they will be IsIPv4MappedToIPv6=true. If you want your application to be dualmode yet still have pretty IPv4 addresses+endpoints, your application code should normalize mapped IPs back to ipv4. Another option is to have two distinct listeners, one on |
Hi @fbrosseau sorry if this is a dumb question, but |
That is property of |
The
How about for the |
This is because IPEndPoint lep = tcpClient.Client.LocalEndPoint as IPEndPoint;
IPEndPoint rep = tcpClient.Client.RemoteEndPoint as IPEndPoint;
Console.WriteLine("Client = {0} {1}", tcpClient.Client.LocalEndPoint, tcpClient.Client.RemoteEndPoint);
Console.WriteLine("Client = {0} {1}", lep.Address.IsIPv4MappedToIPv6 ?
new IPEndPoint(lep.Address.MapToIPv4(), lep.Port) : lep,
rep.Address.IsIPv4MappedToIPv6 ?
new IPEndPoint(rep.Address.MapToIPv4(), lep.Port) : rep); and it would produce something like
I hope this make some sense @jchristn. |
Thank you @wfurt this makes it perfectly clear. Is there any effort underway to ensure that |
@jchristn what kind of effort do you expect here? |
Hi @karelz unfortunately IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
var state = connections.FirstOrDefault(x =>
x.LocalEndPoint.Equals(client.Client.LocalEndPoint)
&& x.RemoteEndPoint.Equals(client.Client.RemoteEndPoint)); |
@jchristn can you try to debug why it does not work? |
Hi @karelz yes I have tried. It seems that when the e.g. if I use the Linq statement above, it returns null. If I iterate the array of |
There is no effort to make them same and I feel there should not be. They are different for good reasons. As I mentioned above, the endpoints on Only one hope for the future would be #63162 IMHO. That already uses |
Description
When attempting to identify a TCP connection from
TcpClient.Client.LocalEndPoint
orRemoteEndPoint
using theTcpConnectionInformation[]
returned fromIPGlobalProperties.GetIPGlobalProperties.GetActiveTcpConnections()
, there is a material difference between the contents ofLocalEndPoint
andRemoteEndPoint
.On a Windows 11 host with both IPv4 and IPv6 enabled,
.ToString()
for theTcpClient
returns this:Whereas after retrieving the connection information, it returns something different:
It appears
TcpClient
contains a hybrid IPv6/IPv4 address whereasTcpConnectionInformation
only contains IPv4.Is there a way to identify the connection from
TcpConnectionInformation
in a consistent manner, preferably across runtimes and across platforms?Reproduction Steps
Documented here: https://stackoverflow.com/questions/76979491/matching-tcp-connections-between-tcpclient-and-tcpconnectioninformation and also above
Expected behavior
LocalEndPoint
andRemoteEndPoint
would contain values of similar structure and be comparable, betweenTcpClient
andTcpConnectionInformation
Actual behavior
TcpClient
contains a hybrid IPv6/IPv4 address whereasTcpConnectionInformation
contains only an IPv4 address.Regression?
Unknown
Known Workarounds
For
localhost
connections, just comparing by source port, which must be unique.Configuration
.NET 7
Windows 11
x64
Unknown
No Blazor
Other information
NA
The text was updated successfully, but these errors were encountered: