Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feedback
  • Loading branch information
wfurt committed Aug 7, 2023
commit 9e1340567440eff8eb48396389edc236398ef722
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ internal static partial class Interop
internal static partial class Sys
{
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_Bind")]
private static partial Error Bind(SafeHandle socket, int socketProtocolType, ReadOnlySpan<byte> socketAddress, int socketAddressLen);
private static partial Error Bind(SafeHandle socket, ProtocolType socketProtocolType, ReadOnlySpan<byte> socketAddress, int socketAddressLen);

internal static Error Bind(
SafeHandle socket, ProtocolType socketProtocolType, ReadOnlySpan<byte> socketAddress)
=> Bind(socket, (int)socketProtocolType, socketAddress, socketAddress.Length);
=> Bind(socket, socketProtocolType, socketAddress, socketAddress.Length);
}
}

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions src/libraries/Common/src/System/Net/Internals/readme.md

This file was deleted.

43 changes: 3 additions & 40 deletions src/libraries/Common/src/System/Net/SocketAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Net
{
// This class is used when subclassing EndPoint, and provides indication
// on how to format the memory buffers that the platform uses for network addresses.
public class SocketAddress : System.IEquatable<SocketAddress>
public class SocketAddress : IEquatable<SocketAddress>
{
#pragma warning disable CA1802 // these could be const on Windows but need to be static readonly for Unix
internal static readonly int IPv6AddressSize = SocketAddressPal.IPv6AddressSize;
Expand Down Expand Up @@ -40,7 +40,7 @@ public int Size
set
{
ArgumentOutOfRangeException.ThrowIfGreaterThan(value, _buffer.Length);
ArgumentOutOfRangeException.ThrowIfLessThan(value, MinSize);
ArgumentOutOfRangeException.ThrowIfLessThan(value, 0);
_size = value;
}
}
Expand Down Expand Up @@ -125,13 +125,6 @@ internal SocketAddress(IPAddress ipaddress, int port)
SocketAddressPal.SetPort(_buffer, unchecked((ushort)port));
}

internal SocketAddress(AddressFamily addressFamily, ReadOnlySpan<byte> buffer)
{
_buffer = buffer.ToArray();
_size = _buffer.Length;
SocketAddressPal.SetAddressFamily(_buffer, addressFamily);
}

/// <summary>This represents underlying memory that can be passed to native OS calls.</summary>
/// <remarks>
/// Content of the memory can be invalidated if <see cref="Size"/> is changed or if the SocketAddress is used in another receive call.
Expand All @@ -140,40 +133,10 @@ public Memory<byte> Buffer
{
get
{
return new Memory<byte>(_buffer, 0, _size);
}
}

internal IPAddress GetIPAddress()
{
if (Family == AddressFamily.InterNetworkV6)
{
Debug.Assert(Size >= IPv6AddressSize);

Span<byte> address = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
uint scope;
SocketAddressPal.GetIPv6Address(_buffer, address, out scope);

return new IPAddress(address, (long)scope);
}
else if (Family == AddressFamily.InterNetwork)
{
Debug.Assert(Size >= IPv4AddressSize);
long address = (long)SocketAddressPal.GetIPv4Address(_buffer) & 0x0FFFFFFFF;
return new IPAddress(address);
}
else
{
throw new SocketException(SocketError.AddressFamilyNotSupported);
return new Memory<byte>(_buffer);
}
}

internal int GetPort() => (int)SocketAddressPal.GetPort(_buffer);

internal IPEndPoint GetIPEndPoint()
{
return new IPEndPoint(GetIPAddress(), GetPort());
}

public override bool Equals(object? comparand) =>
comparand is SocketAddress other && Equals(other);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public static IPEndPoint GetIPEndPoint(this SocketAddress socketAddress)

public static bool Equals(this SocketAddress socketAddress, EndPoint? endPoint)
{
if (endPoint is IPEndPoint ipe)
if (socketAddress.Family == endPoint?.AddressFamily && endPoint is IPEndPoint ipe)
{
if (socketAddress.Family == endPoint.AddressFamily)
{
return ipe.Equals(socketAddress.Buffer.Span);
}
return ipe.Equals(socketAddress.Buffer.Span);
}

// We could serialize other EndPoints and compare socket addresses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace System.Net
{
internal static partial class SocketProtocolSupportPal
{
private const int DgramSocketType = 2;

private static unsafe bool IsSupported(AddressFamily af)
{
// Check for AF_UNIX on iOS/tvOS. The OS claims to support this, but returns EPERM on bind.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ internal static partial class SocketProtocolSupportPal
{
private static bool IsSupported(AddressFamily af)
{
const int StreamSocketType = 1;
Interop.Winsock.EnsureInitialized();

IntPtr INVALID_SOCKET = (IntPtr)(-1);
IntPtr socket = INVALID_SOCKET;
try
{
socket = Interop.Winsock.WSASocketW(af, DgramSocketType, 0, IntPtr.Zero, 0, (int)Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke Socket.OSSupportsUnixDomainSockets as Windows only support Stream for UDS.

socket = Interop.Winsock.WSASocketW(af, StreamSocketType, 0, IntPtr.Zero, 0, (int)Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
return
socket != INVALID_SOCKET ||
(SocketError)Marshal.GetLastPInvokeError() != SocketError.AddressFamilyNotSupported;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ internal static partial class SocketProtocolSupportPal
public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);

private const int DgramSocketType = 2;

private static bool IsIPv6Disabled()
{
// First check for the AppContext switch, giving it priority over the environment variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
Link="Common\System\Net\CookieParser.cs" />
<Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
Link="Common\System\Net\IPAddressParserStatics.cs" />
<Compile Include="$(CommonPath)System\Net\IPEndPointExtensions.cs"
Link="Common\System\Net\IPEndPointExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\HttpKnownHeaderNames.cs"
Link="Common\System\Net\HttpKnownHeaderNames.cs" />
<Compile Include="$(CommonPath)System\Net\TcpValidationHelpers.cs"
Expand All @@ -68,6 +70,8 @@
Link="Common\System\Net\UriScheme.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddress.cs"
Link="Common\System\Net\SocketAddress.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddressExtensions.cs"
Link="Common\System\Net\SocketAddressExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\NegotiationInfoClass.cs"
Link="Common\System\Net\NegotiationInfoClass.cs" />
<Compile Include="$(CommonPath)System\Net\NetworkInformation\HostInformation.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public static void AddressFamily_Size_Correct()
{
SocketAddress sa = new SocketAddress(AddressFamily.InterNetwork);
Assert.Throws<ArgumentOutOfRangeException>(() => sa.Size = sa.Size + 1);

sa.Size = 4;
Assert.Equal(4, sa.Buffer.Length);
sa.Size = 0;
Assert.Throws<ArgumentOutOfRangeException>(() => sa.Size = - 1);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public ValueTask<int> ReceiveFromAsync(Memory<byte> buffer, SocketFlags socketFl
saea.RemoteEndPoint = null;
saea._socketAddress = receivedAddress;
saea.WrapExceptionsForNetworkStream = false;
return saea.ReceiveFromSaAsync(this, cancellationToken);
return saea.ReceiveFromSocketAddressAsync(this, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -688,6 +688,7 @@ public ValueTask<int> SendToAsync(ReadOnlyMemory<byte> buffer, SocketFlags socke
/// <returns>An asynchronous task that completes with the number of bytes sent.</returns>
public ValueTask<int> SendToAsync(ReadOnlyMemory<byte> buffer, SocketFlags socketFlags, SocketAddress socketAddress, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
ArgumentNullException.ThrowIfNull(socketAddress);

if (cancellationToken.IsCancellationRequested)
Expand Down Expand Up @@ -1085,7 +1086,7 @@ public ValueTask<SocketReceiveFromResult> ReceiveFromAsync(Socket socket, Cancel
ValueTask.FromException<SocketReceiveFromResult>(CreateException(error));
}

internal ValueTask<int> ReceiveFromSaAsync(Socket socket, CancellationToken cancellationToken)
internal ValueTask<int> ReceiveFromSocketAddressAsync(Socket socket, CancellationToken cancellationToken)
{
if (socket.ReceiveFromAsync(this, cancellationToken))
{
Expand Down
18 changes: 7 additions & 11 deletions src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ private unsafe Socket(SafeSocketHandle handle, bool loadPropertiesFromHandle)
break;

case AddressFamily.Unix:
//socketAddress = new SocketAddress(AddressFamily.Unix, buffer.Slice(0, bufferLength));
//_remoteEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
_remoteEndPoint = new UnixDomainSocketEndPoint(buffer.Slice(0, bufferLength));
break;
}
Expand Down Expand Up @@ -779,7 +777,7 @@ private void DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
SocketError errorCode = SocketPal.Bind(
_handle,
_protocolType,
socketAddress.Buffer.Span);
socketAddress.Buffer.Span.Slice(0, socketAddress.Size));

// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
Expand Down Expand Up @@ -1285,7 +1283,7 @@ public int SendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags,
SocketAddress socketAddress = Serialize(ref remoteEP);

int bytesTransferred;
SocketError errorCode = SocketPal.SendTo(_handle, buffer, offset, size, socketFlags, socketAddress.Buffer, out bytesTransferred);
SocketError errorCode = SocketPal.SendTo(_handle, buffer, offset, size, socketFlags, socketAddress.Buffer.Slice(0, socketAddress.Size), out bytesTransferred);

// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
Expand Down Expand Up @@ -1357,7 +1355,7 @@ public int SendTo(ReadOnlySpan<byte> buffer, SocketFlags socketFlags, EndPoint r
SocketAddress socketAddress = Serialize(ref remoteEP);

int bytesTransferred;
SocketError errorCode = SocketPal.SendTo(_handle, buffer, socketFlags, socketAddress.Buffer, out bytesTransferred);
SocketError errorCode = SocketPal.SendTo(_handle, buffer, socketFlags, socketAddress.Buffer.Slice(0, socketAddress.Size), out bytesTransferred);

// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
Expand Down Expand Up @@ -1396,7 +1394,7 @@ public int SendTo(ReadOnlySpan<byte> buffer, SocketFlags socketFlags, SocketAddr
ValidateBlockingMode();

int bytesTransferred;
SocketError errorCode = SocketPal.SendTo(_handle, buffer, socketFlags, socketAddress.Buffer, out bytesTransferred);
SocketError errorCode = SocketPal.SendTo(_handle, buffer, socketFlags, socketAddress.Buffer.Slice(0, socketAddress.Size), out bytesTransferred);

// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
Expand Down Expand Up @@ -1898,10 +1896,8 @@ public int ReceiveFrom(Span<byte> buffer, SocketFlags socketFlags, SocketAddress

int bytesTransferred;
SocketError errorCode = SocketPal.ReceiveFrom(_handle, buffer, socketFlags, receivedAddress.Buffer, out int socketAddressSize, out bytesTransferred);
if (socketAddressSize > 0)
{
receivedAddress.Size = socketAddressSize;
}
receivedAddress.Size = socketAddressSize;

UpdateReceiveSocketErrorForDisposed(ref errorCode, bytesTransferred);
// If the native call fails we'll throw a SocketException.
if (errorCode != SocketError.Success)
Expand Down Expand Up @@ -3167,7 +3163,7 @@ private void DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
SocketError errorCode;
try
{
errorCode = SocketPal.Connect(_handle, socketAddress.Buffer);
errorCode = SocketPal.Connect(_handle, socketAddress.Buffer.Slice(0, socketAddress.Size));
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public void MulticastInterface_Set_InvalidIndex_Throws()

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
[SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.FreeBSD, "Expected behavior is different on OSX or FreeBSD")]
[ActiveIssue("AAA")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52124", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public async Task MulticastInterface_Set_IPv6_AnyInterface_Succeeds()
{
Expand Down Expand Up @@ -181,7 +180,6 @@ public void Ttl_Set_Succeeds(AddressFamily af)

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))] // Skip on Nano: https://github.com/dotnet/runtime/issues/26286
[PlatformSpecific(TestPlatforms.Windows)]
[ActiveIssue(" AA")]
public async Task MulticastInterface_Set_IPv6_Loopback_Succeeds()
{
// On Windows, we can apparently assume interface 1 is "loopback." On other platforms, this is not a
Expand Down