runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Starting from .NET 6 IPv6Any and IPv6None now point to same object, which they probably should not.

Open riku76 opened this issue 5 years ago • 11 comments

From https://source.dot.net/#System.Net.Primitives/System/Net/IPAddress.cs,e28302f14f72daac I can see that System.Net.IPAddress.IPv6Any and System.Net.IPAddress.IPv6None now point to the same object. That did not use to be the case, as we can see from eg. .NET Framework 4.8 sources (https://referencesource.microsoft.com/#system/net/system/Net/IPAddress.cs).

Documentation regarding IPv6None (https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.ipv6none?view=net-6.0) states that:

"The Socket.Bind method uses the IPv6None field to indicate that a Socket must not listen for client activity. The IPv6None field is equivalent to 0:0:0:0:0:0:0:0 in colon-hexadecimal notation, or to ::0 in compact notation."

So it sounds very much that IPv6Any and IPv6None should not be the same object. I did not check is the Socket.Bind really broken now (as it probably is if documentation is correct), but at least OPC UA SDK breaks due to this change and that is probably not the only thing that breaks.

riku76 avatar Nov 26 '21 07:11 riku76

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

Issue Details

From https://source.dot.net/#System.Net.Primitives/System/Net/IPAddress.cs,e28302f14f72daac I can see that System.Net.IPAddress.IPv6Any and System.Net.IPAddress.IPv6None now point to the same object. That did not use to be the case, as we can see from eg. .NET Framework 4.8 sources (https://referencesource.microsoft.com/#system/net/system/Net/IPAddress.cs).

Documentation regarding IPv6None (https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.ipv6none?view=net-6.0) states that:

"The Socket.Bind method uses the IPv6None field to indicate that a Socket must not listen for client activity. The IPv6None field is equivalent to 0:0:0:0:0:0:0:0 in colon-hexadecimal notation, or to ::0 in compact notation."

So it sounds very much that IPv6Any and IPv6None should not be the same object. I did not check is the Socket.Bind really broken now (as it probably is if documentation is correct), but at least OPC UA SDK breaks due to this change and that is probably not the only thing that breaks.

Author: riku76
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

ghost avatar Nov 26 '21 07:11 ghost

IPv6None and IPv6Any both point to 0:0:0:0:0:0:0:0, therefore they are identical. The fact that they are backed by different object instances in older .NET versions doesn't make any difference here.

The Socket.Bind method uses the IPv6None field to indicate that a Socket must not listen for client activity.

(From the IPV4 IPAddress.None docs:)

The Socket.Bind method uses the None field to indicate that a Socket must not listen for client activity.

TBH, I don't really understand the purpose of IPAddress.IPv6None, and also don't understand what do the docs mean by binding IPv4 sockets to IPAddress.None in order to " not listen for client activity". The IPV4 version IPAddress.None is 255.255.255.255, which is actually the "Limited Broadcast" address according to rfc6890 and rfc919. It can be used for broadcasting, but as far as I am aware, winsock doesn't allow to bind to it.

@wfurt any thoughts? I think we have a documentation issue here.

antonfirsov avatar Nov 26 '21 14:11 antonfirsov

Oops, I was the one who aliased the two objects.

This behavior of IPv6None is indeed weird. It represents the same IP address with IPv6Any, yet is expected to be compared by reference to trigger some special case? And what does "a Socket must not listen for client activity" mean? If you don't want to listen for client activity, just don't.

BTW, IPv6None is referenced only once in the both the BCL and ASP.NET Core, twice in the .NET Framework's BCL, and once in WCF. In none of these cases is it compared by reference. So I guess the documentation is wrong.

teo-tsirpanis avatar Nov 26 '21 16:11 teo-tsirpanis

@teo-tsirpanis no code should rely on by-ref comparison of IPAddress, I would be surprised if BCL code ever does it, if yes, it's likely a bug.

@riku76 can you provide more details on how exactly is OPC UA SDK broken?

antonfirsov avatar Nov 26 '21 17:11 antonfirsov

@antonfirsov OPC UA SDK has a configuration for network interface to listen. If you omit it, UA internally initializes it to Any/IPv6Any, then it later compares that to IPv6None and thinks that it does not need to listen anything. As a workaround specifying the IPv6Any like address manually (bitwise equal, not reference equal) it starts working again.

@teo-tsirpanis I agree that by reference compare is not a good idea, but I think that if you only look at the docs and API one could easily overlook the fact that it is a reference comparison you are making with == operator and the thing that Any & None are exactly the same, thus I believe the same kind of issue could easily exist somewhere else than in OPC UA SDK as well.

riku76 avatar Nov 29 '21 06:11 riku76

Triage: We believe we just need to update documentation. It is ok to have the same instance for these 2 fields.

karelz avatar Nov 30 '21 18:11 karelz

then it later compares that to IPv6None

@riku76 I believe an issue has to be raised against the OPC UA SDK. They should not compare the address against IPAdress.IPv6None by reference. I will think how to clean-up our docs so they are less confusing.

If we see more reports on customers hitting this issue, we may reconsider this.

antonfirsov avatar Nov 30 '21 18:11 antonfirsov

@antonfirsov I think the inconsistent behavior between IPv4 None and Any vs. IPv6 None and Any is an issue, but whatever. In case you stick to this decision you could consider implementing == operator to prevent future issues.

.NET 6 itself is using == operator (reference compare) at least in:

https://source.dot.net/#Microsoft.AspNetCore.Server.Kestrel.Transport.Quic/Internal/QuicConnectionListener.cs,47 https://source.dot.net/#Microsoft.AspNetCore.Server.Kestrel.Transport.Quic/Internal/QuicConnectionListener.cs,51 https://source.dot.net/#Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets/SocketTransportOptions.cs,119

...so if even you are making the mistake it is quite clear that others may fall to it too. (these are comparison to Any, but it is reference comparison and now for two latter cases they match to IPv6None too).

riku76 avatar Dec 01 '21 06:12 riku76

~~@riku76 Equals compares IP addresses by value; it's not the same with ==.~~

Forget about it, didn't read the whole sources.

teo-tsirpanis avatar Dec 01 '21 07:12 teo-tsirpanis

@riku76 thank you for pointing me to those cases! Opened dotnet/aspnetcore#38761 to address them.

We also made a mistake in our own code, see #57250. I would re-consider adding == overload to IPAddress, naively it feels like it may fix more problems than it would create. Related: #38040. @geoffkizer @scalablecory thoughts?

Someone should really untangle the historical intention behind naming and docs of IPAddress.None and IPAddress.IPv6None, these look like nonsense fields to me.

Putting this back to untriaged, since the topic could use another discussion.

antonfirsov avatar Dec 01 '21 14:12 antonfirsov

Triage:

  • It is not super-important, so let's do minimal work - leave it as it is, until we get more customer reports
    • No obsoletion now
  • operator == sounds like a good thing in general to help customers. Alternatively we could create Roslyn analyzer to detect usage of reference comparison and flag it.
  • Fix docs

karelz avatar Dec 02 '21 17:12 karelz