Skip to content

Commit 072fd17

Browse files
authored
use approved SocketAddress API instead of direct internal access (#89841)
* use approved SocketAddress API instead of internal access * feedback * cleanup
1 parent e07d1ee commit 072fd17

File tree

20 files changed

+102
-98
lines changed

20 files changed

+102
-98
lines changed

src/libraries/Common/src/Interop/Unix/System.Native/Interop.Bind.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ internal static partial class Interop
1010
internal static partial class Sys
1111
{
1212
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_Bind")]
13-
internal static unsafe partial Error Bind(SafeHandle socket, ProtocolType socketProtocolType, byte* socketAddress, int socketAddressLen);
13+
private static partial Error Bind(SafeHandle socket, ProtocolType socketProtocolType, ReadOnlySpan<byte> socketAddress, int socketAddressLen);
14+
15+
internal static Error Bind(
16+
SafeHandle socket, ProtocolType socketProtocolType, ReadOnlySpan<byte> socketAddress)
17+
=> Bind(socket, socketProtocolType, socketAddress, socketAddress.Length);
1418
}
1519
}

src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSASocketW.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using System;
55
using System.Net.Sockets;
66
using System.Runtime.InteropServices;
7-
#if !SYSTEM_NET_SOCKETS_DLL
8-
using SocketType = System.Net.Internals.SocketType;
9-
#endif
107

118
internal static partial class Interop
129
{
@@ -15,7 +12,7 @@ internal static partial class Winsock
1512
[LibraryImport(Interop.Libraries.Ws2_32, SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
1613
internal static partial IntPtr WSASocketW(
1714
AddressFamily addressFamily,
18-
SocketType socketType,
15+
int socketType,
1916
int protocolType,
2017
IntPtr protocolInfo,
2118
int group,
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Runtime.InteropServices;
56
using System.Net.Sockets;
67

@@ -9,9 +10,13 @@ internal static partial class Interop
910
internal static partial class Winsock
1011
{
1112
[LibraryImport(Interop.Libraries.Ws2_32, SetLastError = true)]
12-
internal static partial SocketError bind(
13+
private static partial SocketError bind(
1314
SafeSocketHandle socketHandle,
14-
byte[] socketAddress,
15+
ReadOnlySpan<byte> socketAddress,
1516
int socketAddressSize);
17+
18+
internal static SocketError bind(
19+
SafeSocketHandle socketHandle,
20+
ReadOnlySpan<byte> socketAddress) => bind(socketHandle, socketAddress, socketAddress.Length);
1621
}
1722
}

src/libraries/Common/src/System/Net/IPEndPointExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace System.Net.Sockets
88
{
9-
internal static class IPEndPointExtensions
9+
internal static partial class IPEndPointExtensions
1010
{
1111
public static IPAddress GetIPAddress(ReadOnlySpan<byte> socketAddressBuffer)
1212
{

src/libraries/Common/src/System/Net/Internals/IPEndPointExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace System.Net.Sockets
77
{
8-
internal static class IPEndPointExtensions
8+
internal static partial class IPEndPointExtensions
99
{
1010
public static Internals.SocketAddress Serialize(EndPoint endpoint)
1111
{

src/libraries/Common/src/System/Net/SocketAddress.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class SocketAddress : System.IEquatable<SocketAddress>
2929
internal static readonly int MaxAddressSize = SocketAddressPal.MaxAddressSize;
3030
#pragma warning restore CA1802
3131

32-
internal int InternalSize;
33-
internal byte[] InternalBuffer;
32+
private int _size;
33+
private byte[] _buffer;
3434

3535
private const int MinSize = 2;
3636
private const int DataOffset = 2;
@@ -39,21 +39,21 @@ public AddressFamily Family
3939
{
4040
get
4141
{
42-
return SocketAddressPal.GetAddressFamily(InternalBuffer);
42+
return SocketAddressPal.GetAddressFamily(_buffer);
4343
}
4444
}
4545

4646
public int Size
4747
{
4848
get
4949
{
50-
return InternalSize;
50+
return _size;
5151
}
5252
set
5353
{
54-
ArgumentOutOfRangeException.ThrowIfGreaterThan(value, InternalBuffer.Length);
54+
ArgumentOutOfRangeException.ThrowIfGreaterThan(value, _buffer.Length);
5555
ArgumentOutOfRangeException.ThrowIfLessThan(value, MinSize);
56-
InternalSize = value;
56+
_size = value;
5757
}
5858
}
5959

@@ -69,15 +69,15 @@ public byte this[int offset]
6969
{
7070
throw new IndexOutOfRangeException();
7171
}
72-
return InternalBuffer[offset];
72+
return _buffer[offset];
7373
}
7474
set
7575
{
7676
if ((uint)offset >= (uint)Size)
7777
{
7878
throw new IndexOutOfRangeException();
7979
}
80-
InternalBuffer[offset] = value;
80+
_buffer[offset] = value;
8181
}
8282
}
8383

@@ -97,11 +97,11 @@ public SocketAddress(AddressFamily family, int size)
9797
{
9898
ArgumentOutOfRangeException.ThrowIfLessThan(size, MinSize);
9999

100-
InternalSize = size;
101-
InternalBuffer = new byte[size];
102-
InternalBuffer[0] = (byte)InternalSize;
100+
_size = size;
101+
_buffer = new byte[size];
102+
_buffer[0] = (byte)_size;
103103

104-
SocketAddressPal.SetAddressFamily(InternalBuffer, family);
104+
SocketAddressPal.SetAddressFamily(_buffer, family);
105105
}
106106

107107
internal SocketAddress(IPAddress ipAddress)
@@ -110,15 +110,15 @@ internal SocketAddress(IPAddress ipAddress)
110110
{
111111

112112
// No Port.
113-
SocketAddressPal.SetPort(InternalBuffer, 0);
113+
SocketAddressPal.SetPort(_buffer, 0);
114114

115115
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
116116
{
117117
Span<byte> addressBytes = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
118118
ipAddress.TryWriteBytes(addressBytes, out int bytesWritten);
119119
Debug.Assert(bytesWritten == IPAddressParserStatics.IPv6AddressBytes);
120120

121-
SocketAddressPal.SetIPv6Address(InternalBuffer, addressBytes, (uint)ipAddress.ScopeId);
121+
SocketAddressPal.SetIPv6Address(_buffer, addressBytes, (uint)ipAddress.ScopeId);
122122
}
123123
else
124124
{
@@ -127,21 +127,21 @@ internal SocketAddress(IPAddress ipAddress)
127127
#pragma warning restore CS0618
128128

129129
Debug.Assert(ipAddress.AddressFamily == AddressFamily.InterNetwork);
130-
SocketAddressPal.SetIPv4Address(InternalBuffer, address);
130+
SocketAddressPal.SetIPv4Address(_buffer, address);
131131
}
132132
}
133133

134134
internal SocketAddress(IPAddress ipaddress, int port)
135135
: this(ipaddress)
136136
{
137-
SocketAddressPal.SetPort(InternalBuffer, unchecked((ushort)port));
137+
SocketAddressPal.SetPort(_buffer, unchecked((ushort)port));
138138
}
139139

140140
internal SocketAddress(AddressFamily addressFamily, ReadOnlySpan<byte> buffer)
141141
{
142-
InternalBuffer = buffer.ToArray();
143-
InternalSize = InternalBuffer.Length;
144-
SocketAddressPal.SetAddressFamily(InternalBuffer, addressFamily);
142+
_buffer = buffer.ToArray();
143+
_size = _buffer.Length;
144+
SocketAddressPal.SetAddressFamily(_buffer, addressFamily);
145145
}
146146

147147
/// <summary>This represents underlying memory that can be passed to native OS calls.</summary>
@@ -152,7 +152,7 @@ public Memory<byte> Buffer
152152
{
153153
get
154154
{
155-
return new Memory<byte>(InternalBuffer, 0, InternalSize);
155+
return new Memory<byte>(_buffer, 0, _size);
156156
}
157157
}
158158

@@ -164,14 +164,14 @@ internal IPAddress GetIPAddress()
164164

165165
Span<byte> address = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
166166
uint scope;
167-
SocketAddressPal.GetIPv6Address(InternalBuffer, address, out scope);
167+
SocketAddressPal.GetIPv6Address(_buffer, address, out scope);
168168

169169
return new IPAddress(address, (long)scope);
170170
}
171171
else if (Family == AddressFamily.InterNetwork)
172172
{
173173
Debug.Assert(Size >= IPv4AddressSize);
174-
long address = (long)SocketAddressPal.GetIPv4Address(InternalBuffer) & 0x0FFFFFFFF;
174+
long address = (long)SocketAddressPal.GetIPv4Address(_buffer) & 0x0FFFFFFFF;
175175
return new IPAddress(address);
176176
}
177177
else
@@ -184,7 +184,7 @@ internal IPAddress GetIPAddress()
184184
}
185185
}
186186

187-
internal int GetPort() => (int)SocketAddressPal.GetPort(InternalBuffer);
187+
internal int GetPort() => (int)SocketAddressPal.GetPort(_buffer);
188188

189189
internal IPEndPoint GetIPEndPoint()
190190
{
@@ -199,7 +199,7 @@ public override bool Equals(object? comparand) =>
199199
public override int GetHashCode()
200200
{
201201
HashCode hash = default;
202-
hash.AddBytes(new ReadOnlySpan<byte>(InternalBuffer, 0, InternalSize));
202+
hash.AddBytes(new ReadOnlySpan<byte>(_buffer, 0, _size));
203203
return hash.ToHashCode();
204204
}
205205

@@ -234,7 +234,7 @@ public override string ToString()
234234
result[length++] = ':';
235235
result[length++] = '{';
236236

237-
byte[] buffer = InternalBuffer;
237+
byte[] buffer = _buffer;
238238
for (int i = DataOffset; i < Size; i++)
239239
{
240240
if (i > DataOffset)

src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Unix.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace System.Net
88
{
99
internal static partial class SocketProtocolSupportPal
1010
{
11-
private const int DgramSocketType = 2;
1211
private static unsafe bool IsSupported(AddressFamily af)
1312
{
1413
// Check for AF_UNIX on iOS/tvOS. The OS claims to support this, but returns EPERM on bind.

src/libraries/Common/src/System/Net/SocketProtocolSupportPal.Windows.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
using System.Net.Sockets;
55
using System.Runtime.InteropServices;
6-
#if !SYSTEM_NET_SOCKETS_DLL
7-
using SocketType = System.Net.Internals.SocketType;
8-
#endif
96

107
namespace System.Net
118
{
@@ -19,7 +16,7 @@ private static bool IsSupported(AddressFamily af)
1916
IntPtr socket = INVALID_SOCKET;
2017
try
2118
{
22-
socket = Interop.Winsock.WSASocketW(af, SocketType.Stream, 0, IntPtr.Zero, 0, (int)Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
19+
socket = Interop.Winsock.WSASocketW(af, DgramSocketType, 0, IntPtr.Zero, 0, (int)Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
2320
return
2421
socket != INVALID_SOCKET ||
2522
(SocketError)Marshal.GetLastPInvokeError() != SocketError.AddressFamilyNotSupported;

src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ internal static partial class SocketProtocolSupportPal
1414
public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
1515
public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);
1616

17+
private const int DgramSocketType = 2;
18+
1719
private static bool IsIPv6Disabled()
1820
{
1921
// First check for the AppContext switch, giving it priority over the environment variable.

src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@
2121
<Compile Include="$(CommonPath)System\Net\InternalException.cs"
2222
Link="Common\System\Net\InternalException.cs" />
2323
<!-- System.Net common -->
24-
<Compile Include="$(CommonPath)System\Net\Sockets\ProtocolType.cs"
25-
Link="Common\System\Net\Sockets\ProtocolType.cs" />
26-
<Compile Include="$(CommonPath)System\Net\Sockets\SocketType.cs"
27-
Link="Common\System\Net\Sockets\SocketType.cs" />
2824
<Compile Include="$(CommonPath)System\Net\IPAddressParserStatics.cs"
2925
Link="Common\System\Net\IPAddressParserStatics.cs" />
30-
<Compile Include="$(CommonPath)System\Net\IPEndPointStatics.cs"
31-
Link="Common\System\Net\IPEndPointStatics.cs" />
3226
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
3327
Link="Common\System\Net\SocketProtocolSupportPal.cs" />
3428
</ItemGroup>
@@ -37,9 +31,7 @@
3731
<!-- Debug only -->
3832
<Compile Include="$(CommonPath)System\Net\DebugSafeHandle.cs"
3933
Link="Common\System\Net\DebugSafeHandle.cs" />
40-
<!-- System.Net.Internals -->
41-
<Compile Include="$(CommonPath)System\Net\Internals\IPAddressExtensions.cs"
42-
Link="Common\System\Net\Internals\IPAddressExtensions.cs" />
34+
<!-- System.Net common -->
4335
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.Windows.cs"
4436
Link="Common\System\Net\SocketProtocolSupportPal.Windows" />
4537
<Compile Include="$(CommonPath)System\Net\SocketAddressPal.Windows.cs"

0 commit comments

Comments
 (0)