Skip to content
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

Socket: don't disconnect Socket for unknown/unsupported socket options. #59925

Merged
merged 8 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName opti
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}
}

Expand Down Expand Up @@ -2015,7 +2015,7 @@ public void SetRawSocketOption(int optionLevel, int optionName, ReadOnlySpan<byt

if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}
}

Expand Down Expand Up @@ -2051,7 +2051,7 @@ public void SetRawSocketOption(int optionLevel, int optionName, ReadOnlySpan<byt
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}

return optionValue;
Expand All @@ -2076,7 +2076,7 @@ public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName opti
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}
}

Expand All @@ -2100,7 +2100,7 @@ public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName op
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}

if (optionLength != realOptionLength)
Expand Down Expand Up @@ -2136,7 +2136,7 @@ public int GetRawSocketOption(int optionLevel, int optionName, Span<byte> option

if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}

return realOptionLength;
Expand Down Expand Up @@ -3432,7 +3432,7 @@ internal unsafe void SetSocketOption(SocketOptionLevel optionLevel, SocketOption
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}
}

Expand All @@ -3445,7 +3445,7 @@ private void SetMulticastOption(SocketOptionName optionName, MulticastOption MR)
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}
}

Expand All @@ -3472,7 +3472,7 @@ private void SetLingerOption(LingerOption lref)
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}
}

Expand All @@ -3486,7 +3486,7 @@ private void SetLingerOption(LingerOption lref)
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}

return lingerOption;
Expand All @@ -3502,7 +3502,7 @@ private void SetLingerOption(LingerOption lref)
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}

return multicastOption;
Expand All @@ -3519,7 +3519,7 @@ private void SetLingerOption(LingerOption lref)
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketErrorAndThrowException(errorCode);
UpdateStatusAfterSocketOptionErrorAndThrowException(errorCode);
}

return multicastOption;
Expand Down Expand Up @@ -3690,30 +3690,38 @@ internal void SetToDisconnected()
}
}

private void UpdateStatusAfterSocketErrorAndThrowException(SocketError error, [CallerMemberName] string? callerName = null)
private void UpdateStatusAfterSocketOptionErrorAndThrowException(SocketError error, [CallerMemberName] string? callerName = null)
{
// Don't disconnect socket for unknown options.
bool noDisconnect = error == SocketError.ProtocolOption ||
error == SocketError.OperationNotSupported;
UpdateStatusAfterSocketErrorAndThrowException(error, callerName, noDisconnect);
}

private void UpdateStatusAfterSocketErrorAndThrowException(SocketError error, [CallerMemberName] string? callerName = null, bool noDisconnect = false)
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
// Update the internal state of this socket according to the error before throwing.
var socketException = new SocketException((int)error);
UpdateStatusAfterSocketError(socketException);
UpdateStatusAfterSocketError(socketException, noDisconnect);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, socketException, memberName: callerName);
throw socketException;
}

// UpdateStatusAfterSocketError(socketException) - updates the status of a connected socket
// on which a failure occurred. it'll go to winsock and check if the connection
// is still open and if it needs to update our internal state.
internal void UpdateStatusAfterSocketError(SocketException socketException)
internal void UpdateStatusAfterSocketError(SocketException socketException, bool noDisconnect = false)
{
UpdateStatusAfterSocketError(socketException.SocketErrorCode);
UpdateStatusAfterSocketError(socketException.SocketErrorCode, noDisconnect);
}

internal void UpdateStatusAfterSocketError(SocketError errorCode)
internal void UpdateStatusAfterSocketError(SocketError errorCode, bool noDisconnect = false)
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
// If we already know the socket is disconnected
// we don't need to do anything else.
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"errorCode:{errorCode}");
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"errorCode:{errorCode},noDisconnect:{noDisconnect}");
tmds marked this conversation as resolved.
Show resolved Hide resolved

if (_isConnected && (_handle.IsInvalid || (errorCode != SocketError.WouldBlock &&
if (!noDisconnect && _isConnected && (_handle.IsInvalid || (errorCode != SocketError.WouldBlock &&
errorCode != SocketError.IOPending && errorCode != SocketError.NoBufferSpaceAvailable &&
errorCode != SocketError.TimedOut)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void ReuseUnicastPort_CreateSocketGetOption()
}
else
{
Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
SocketException se = Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
tmds marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand All @@ -45,7 +45,7 @@ public void ReuseUnicastPort_CreateSocketSetOption()
}
else
{
Assert.Throws<SocketException>(() => socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
SocketException se = Assert.Throws<SocketException>(() => socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
}
}
}
Expand All @@ -60,7 +60,7 @@ public void MulticastOption_CreateSocketSetGetOption_GroupAndInterfaceIndex_SetS
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(groupIp, interfaceIndex));

Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership));
SocketException se = Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership));
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public void MulticastInterface_Set_InvalidIndex_Throws()
int interfaceIndex = 31415;
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Throws<SocketException>(() =>
SocketException se = Assert.Throws<SocketException>(() =>
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(interfaceIndex)));
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public void MulticastInterface_Set_IPv6_InvalidIndex_Throws()
int interfaceIndex = 31415;
using (Socket s = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
{
Assert.Throws<SocketException>(() =>
SocketException se = Assert.Throws<SocketException>(() =>
s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, interfaceIndex));
}
}
Expand Down Expand Up @@ -562,6 +562,132 @@ public void Get_AcceptConnection_Succeeds()
}
}

[Fact]
public void GetUnsupportedSocketOption_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
SocketException se = Assert.Throws<SocketException>(() => socket1.GetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1)));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void GetUnsupportedSocketOptionBytesArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.GetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void GetUnsupportedSocketOptionLengthArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
SocketException se = Assert.Throws<SocketException>(() => socket1.GetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionLength: 4));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void SetUnsupportedSocketOptionIntArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
SocketException se = Assert.Throws<SocketException>(() => socket1.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue: 1));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void SetUnsupportedSocketOptionBytesArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void SetUnsupportedSocketOptionBoolArg_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
bool optionValue = true;
SocketException se = Assert.Throws<SocketException>(() => socket1.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)(-1), optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void GetUnsupportedRawSocketOption_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.GetRawSocketOption(SOL_SOCKET, -1, optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

[Fact]
public void SetUnsupportedRawSocketOption_DoesNotDisconnectSocket()
{
(Socket socket1, Socket socket2) = SocketTestExtensions.CreateConnectedSocketPair();
using (socket1)
using (socket2)
{
var optionValue = new byte[4];
SocketException se = Assert.Throws<SocketException>(() => socket1.SetRawSocketOption(SOL_SOCKET, -1, optionValue));
Assert.True(se.SocketErrorCode == SocketError.ProtocolOption ||
se.SocketErrorCode == SocketError.OperationNotSupported, $"SocketError: {se.SocketErrorCode}");

Assert.True(socket1.Connected, "Connected");
}
}

private static int SOL_SOCKET = OperatingSystem.IsLinux() ? 1 : (int)SocketOptionLevel.Socket;
}

[Collection("NoParallelTests")]
Expand Down