Skip to content

Commit e09ab04

Browse files
committed
Report the proper socket error
When a connection fails, the socket error was being incorrectly reported as an address family not supported error, instead of connection refused. It looks like this happened between c4e698c (Unity 2018.1b5), where the proper error was reported, and ec98a4e (Unity 2018.1b6), there the incorrect error is reported. Why did this happen? Between those two changesets (there are about 180 intervening changesets), the order of addresses returned from `Dns.GetHostAddresses` changed. The newer code returns the IPv6 address first, and the IPv4 address second. While `GetCheckedIPs` mentioned in a comment that it skips addresses that don't match the address family, it did not. The code using the addresses returned by `GetCheckedIPs` will use the first address in the array. If this address happens to match the address family of the socket, things will work "properly" (the correct socket error will occur). If, however the first entry has the wrong address family, then the socket error will be an address family not supported error, since the socket was created with different address family. This change modifies `GetCheckedIPs` to filter the addresses, removing those which do not match the address family. This corrects Unity case 1012875.
1 parent 83e3daa commit e09ab04

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mcs/class/System/System.Net.Sockets/Socket.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,19 @@ bool GetCheckedIPs (SocketAsyncEventArgs e, out IPAddress [] addresses)
11601160
// while skipping entries that do not match the address family
11611161
DnsEndPoint dep = e.RemoteEndPoint as DnsEndPoint;
11621162
if (dep != null) {
1163-
addresses = Dns.GetHostAddresses (dep.Host);
1163+
var possibleAddresses = Dns.GetHostAddresses (dep.Host);
1164+
var numberOfAddresses = 0;
1165+
int[] addressIndices = new int[possibleAddresses.Length];
1166+
for (var i = 0; i < possibleAddresses.Length; i++) {
1167+
if (possibleAddresses[i].AddressFamily == dep.AddressFamily) {
1168+
addressIndices[numberOfAddresses] = i;
1169+
numberOfAddresses++;
1170+
}
1171+
}
1172+
addresses = new IPAddress[numberOfAddresses];
1173+
for (var i = 0; i < numberOfAddresses; i++)
1174+
addresses[i] = possibleAddresses[addressIndices[i]];
1175+
11641176
return true;
11651177
} else {
11661178
e.ConnectByNameError = null;

0 commit comments

Comments
 (0)