Bug 6057

Summary: UdpClient IPv6 Receive throws ArgumentException
Product: [Mono] Class Libraries Reporter: mab
Component: SystemAssignee: marcos.henrich
Status: RESOLVED FIXED    
Severity: normal CC: janv, john.miller, kumpera, miguel, mono-bugs+mono, patrick.suzuki
Priority: ---    
Version: 2.10.x   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description mab 2012-07-10 05:31:46 UTC
Description of Problem:
UdpClient's Receive() function throws ArgumentException when using IPv6 endpoints.

Steps to reproduce the problem:

Code:
UdpClient udpClient = new UdpClient(PORT, AddressFamily.InterNetworkV6);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
byte[] receive_byte_array;
receive_byte_array = udpClient.Receive(ref remoteEP);

Send data to an IPv6 endpoint, which sends data back.

Actual Results:
System.ArgumentException: The IPEndPoint was created using InterNetwork AddressFamily but SocketAddress contains InterNetworkV6 instead, please use the same type.
  at System.Net.IPEndPoint.Create (System.Net.SocketAddress socketAddress) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.Socket.ReceiveFrom_nochecks_exc (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags, System.Net.EndPoint& remote_end, Boolean throwOnError, System.Int32& error) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.Socket.ReceiveFrom_nochecks (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags, System.Net.EndPoint& remote_end) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.Socket.ReceiveFrom (System.Byte[] buffer, System.Net.EndPoint& remoteEP) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.UdpClient.Receive (System.Net.IPEndPoint& remoteEP) [0x00000] in <filename unknown>:0 

Expected Results:
Should receive data.

How often does this happen? 
Always.

Additional Information:

Can be fixed by replacing:

EndPoint endPoint = new IPEndPoint (IPAddress.Any, 0);

With:

EndPoint endPoint;
if (remoteEP.AddressFamily == AddressFamily.InterNetwork) {
  endPoint = new IPEndPoint (IPAddress.Any, 0);
} else {
  endPoint = new IPEndPoint (IPAddress.IPv6Any, 0);
}
Comment 1 Rodrigo Kumpera 2013-01-11 16:25:48 UTC
Please provide a complete test case, I don't see how exactly to build one of your explanation that will match what you're experiencing.
Comment 3 John Miller [MSFT] 2015-11-27 18:16:32 UTC
@Rodrigo,

What do you need for this bug? Also, what is missing from the description that does not help reproduce this? I've filed https://bugzilla.xamarin.com/show_bug.cgi?id=36315 which might be a duplicate.
Comment 4 Jan Vilhuber 2015-11-30 15:29:19 UTC
The code in the description is more than adequate to reproduce the problem. Failing that, try using DnDns (available in a few locations; ours is here: https://github.com/nachocove/DnDns) on a IPv6-only network to do DNS lookups. I suspect it would be much easier to just use the code in the description, though. Some knowledge of networking is required.
Comment 5 Rodrigo Kumpera 2015-12-01 13:29:48 UTC
Hey Marcos,

Could you take a look at this one?
Comment 6 marcos.henrich 2015-12-01 15:47:09 UTC
This should be fixed by PR 2274
https://github.com/mono/mono/pull/2274
Comment 7 marcos.henrich 2015-12-01 20:12:43 UTC
*** Bug 36315 has been marked as a duplicate of this bug. ***
Comment 8 marcos.henrich 2016-01-13 10:19:35 UTC
Fixed in master b21767e.
https://github.com/mono/mono/commit/b21767e01caefe05ad063ddc28afecd0b5b78b04