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

add ReceiveFromAsync and SendToAsync with SocketAddress overload #90086

Merged
merged 11 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions src/libraries/Common/src/System/Net/IPEndPointExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,34 @@ public static void Serialize(this IPEndPoint endPoint, Span<byte> destination)
SetIPAddress(destination, endPoint.Address);
SocketAddressPal.SetPort(destination, (ushort)endPoint.Port);
}

public static bool Equals(this IPEndPoint endPoint, ReadOnlySpan<byte> socketAddressBuffer)
{
if (socketAddressBuffer.Length >= SocketAddress.GetMaximumAddressSize(endPoint.AddressFamily) &&
endPoint.AddressFamily == SocketAddressPal.GetAddressFamily(socketAddressBuffer) &&
endPoint.Port == (int)SocketAddressPal.GetPort(socketAddressBuffer))
{
if (endPoint.AddressFamily == AddressFamily.InterNetwork)
{
#pragma warning disable CS0618
return endPoint.Address.Address == (long)SocketAddressPal.GetIPv4Address(socketAddressBuffer);
#pragma warning restore CS0618
}
else
{
Span<byte> addressBuffer1 = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
Span<byte> addressBuffer2 = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
SocketAddressPal.GetIPv6Address(socketAddressBuffer, addressBuffer1, out uint scopeid);
if (endPoint.Address.ScopeId != (long)scopeid)
{
return false;
}
endPoint.Address.TryWriteBytes(addressBuffer2, out _);
return addressBuffer1.SequenceEqual(addressBuffer2);
}
}

return false;
}
}
}

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.

59 changes: 3 additions & 56 deletions src/libraries/Common/src/System/Net/SocketAddress.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Globalization;
using System.Net.Sockets;
using System.Text;

#if SYSTEM_NET_PRIMITIVES_DLL
namespace System.Net
#else
namespace System.Net.Internals
#endif
{
// This class is used when subclassing EndPoint, and provides indication
// on how to format the memory buffers that the platform uses for network addresses.
#if SYSTEM_NET_PRIMITIVES_DLL
public
#else
internal sealed
#endif
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 @@ -52,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 @@ -137,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 @@ -152,44 +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
{
#if SYSTEM_NET_PRIMITIVES_DLL
throw new SocketException(SocketError.AddressFamilyNotSupported);
#else
throw new SocketException((int)SocketError.AddressFamilyNotSupported);
#endif
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
37 changes: 37 additions & 0 deletions src/libraries/Common/src/System/Net/SocketAddressExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Net;

namespace System.Net.Sockets
{
internal static partial class SocketAddressExtensions
{
public static IPAddress GetIPAddress(this SocketAddress socketAddress) => IPEndPointExtensions.GetIPAddress(socketAddress.Buffer.Span);
public static int GetPort(this SocketAddress socketAddress)
{
Debug.Assert(socketAddress.Family == AddressFamily.InterNetwork || socketAddress.Family == AddressFamily.InterNetworkV6);
return (int)SocketAddressPal.GetPort(socketAddress.Buffer.Span);
}

public static IPEndPoint GetIPEndPoint(this SocketAddress socketAddress)
{
return new IPEndPoint(socketAddress.GetIPAddress(), socketAddress.GetPort());
}

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

// We could serialize other EndPoints and compare socket addresses.
// But that would do two allocations and is probably as expensive as
// allocating new EndPoint.
// This may change if https://github.com/dotnet/runtime/issues/78993 is done
return false;
}
}
}
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
6 changes: 0 additions & 6 deletions src/libraries/Common/src/System/Net/Sockets/ProtocolType.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if SYSTEM_NET_SOCKETS_DLL
namespace System.Net.Sockets
{
public
#else
namespace System.Net.Internals
{
internal
#endif
// Specifies the protocols that the Socket class supports.
enum ProtocolType
{
Expand Down
6 changes: 0 additions & 6 deletions src/libraries/Common/src/System/Net/Sockets/SocketType.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if SYSTEM_NET_SOCKETS_DLL
namespace System.Net.Sockets
{
public
#else
namespace System.Net.Internals
{
internal
#endif
// Specifies the type of socket an instance of the System.Net.Sockets.Socket class represents.
enum SocketType
{
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 @@ -39,6 +39,10 @@
Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CommonPath)System\Net\IPEndPointExtensions.cs"
Link="Common\System\Net\IPEndPointExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddressExtensions.cs"
Link="Common\System\Net\SocketAddressExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="HostInformationPalTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CommonPath)System\Net\IPEndPointExtensions.cs"
Link="Common\System\Net\IPEndPointExtensions.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddressExtensions.cs"
Link="Common\System\Net\SocketAddressExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="CookieCollectionTest.cs" />
Expand Down
Loading
Loading