Skip to content

Commit 46e8714

Browse files
benrr101Ben Russell
andauthored
Merge | SniNativeWrapper Interface (#3015)
* Introducing ISniNativeMethods and porting X64 implementation to it * Adopting usage of SniNativeMethodsX64 in the netfx SniNativeMethodWrapper * Migrating x86 implementation of native methods * Migrating arm implementation of native methods * Introduce a NotSupported implementation of ISniMethods * Rewrite SniNativeMethodWrapper to select native method provider at static construction * Create a netcore implementation of ISniNativeMethods * Create merge file * Resolve merge conflicts ... I think 😅 * Migrating to the merged version * Cleaning up derelict code for unix * Make SniNativeMethod implementations sealed * Fix unix project --------- Co-authored-by: Ben Russell <ben@holycrapitsbenrussell>
1 parent fca9c27 commit 46e8714

28 files changed

+2286
-1893
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Unix.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs

Lines changed: 0 additions & 359 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@
731731
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\IoType.cs">
732732
<Link>Interop\Windows\Sni\IoType.cs</Link>
733733
</Compile>
734+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\ISniNativeMethods.cs">
735+
<Link>Interop\Windows\Sni\ISniNativeMethods.cs</Link>
736+
</Compile>
734737
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\Prefix.cs">
735738
<Link>Interop\Windows\Sni\Prefix.cs</Link>
736739
</Compile>
@@ -752,6 +755,12 @@
752755
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniError.cs">
753756
<Link>Interop\Windows\Sni\SniError.cs</Link>
754757
</Compile>
758+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeMethods.netcore.cs">
759+
<Link>Interop\Windows\Sni\SniNativeMethods.netcore.cs</Link>
760+
</Compile>
761+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeWrapper.cs">
762+
<Link>Interop\Windows\Sni\SniNativeWrapper.cs</Link>
763+
</Compile>
755764
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\TransparentNetworkResolutionMode.cs">
756765
<Link>Interop\Windows\Sni\TransparentNetworkResolutionMode.cs</Link>
757766
</Compile>
@@ -788,8 +797,7 @@
788797
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs">
789798
<Link>Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs</Link>
790799
</Compile>
791-
792-
<Compile Include="Interop\SNINativeMethodWrapper.Windows.cs" />
800+
793801
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Common.cs" />
794802
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Windows.cs" />
795803
<Compile Include="Microsoft\Data\SqlClient\PacketHandle.Windows.cs" />
@@ -808,8 +816,7 @@
808816
<Compile Include="$(CommonSourceRoot)Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Unix.cs">
809817
<Link>Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Unix.cs</Link>
810818
</Compile>
811-
812-
<Compile Include="Interop\SNINativeMethodWrapper.Unix.cs" />
819+
813820
<Compile Include="Microsoft\Data\Sql\SqlDataSourceEnumerator.Unix.cs" />
814821
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.Unix.cs" />
815822
<Compile Include="Microsoft\Data\SqlClient\PacketHandle.Unix.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using Interop.Windows.Kernel32;
67
using Microsoft.Data.SqlClient;
78
using Interop.Windows.Sni;
89

910
namespace Microsoft.Data
1011
{
1112
internal static partial class LocalDBAPI
1213
{
13-
private static IntPtr LoadProcAddress() => SafeNativeMethods.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage");
14+
private static IntPtr LoadProcAddress() =>
15+
Kernel32.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage");
1416

1517
private static IntPtr UserInstanceDLLHandle
1618
{
@@ -22,15 +24,15 @@ private static IntPtr UserInstanceDLLHandle
2224
{
2325
if (s_userInstanceDLLHandle == IntPtr.Zero)
2426
{
25-
SNINativeMethodWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle);
27+
SniNativeWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle);
2628
if (s_userInstanceDLLHandle != IntPtr.Zero)
2729
{
2830
SqlClientEventSource.Log.TryTraceEvent("LocalDBAPI.UserInstanceDLLHandle | LocalDB - handle obtained");
2931
}
3032
else
3133
{
32-
SNINativeMethodWrapper.SNIGetLastError(out SniError sniError);
33-
throw CreateLocalDBException(StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError.sniError);
34+
SniNativeWrapper.SNIGetLastError(out SniError sniError);
35+
throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: sniError.sniError);
3436
}
3537
}
3638
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private SNIErrorDetails GetSniErrorDetails()
7575
}
7676
else
7777
{
78-
SNINativeMethodWrapper.SNIGetLastError(out SniError sniError);
78+
SniNativeWrapper.SNIGetLastError(out SniError sniError);
7979
details.sniErrorNumber = sniError.sniError;
8080
details.errorMessage = sniError.errorMessage;
8181
details.nativeError = sniError.nativeError;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache
8484
if (string.IsNullOrEmpty(userProtocol))
8585
{
8686

87-
result = SNINativeMethodWrapper.SniGetProviderNumber(Handle, ref providerNumber);
87+
result = SniNativeWrapper.SniGetProviderNumber(Handle, ref providerNumber);
8888
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber");
8989
_parser.isTcpProtocol = (providerNumber == Provider.TCP_PROV);
9090
}
@@ -96,10 +96,10 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache
9696
// serverInfo.UserProtocol could be empty
9797
if (_parser.isTcpProtocol)
9898
{
99-
result = SNINativeMethodWrapper.SniGetConnectionPort(Handle, ref portFromSNI);
99+
result = SniNativeWrapper.SniGetConnectionPort(Handle, ref portFromSNI);
100100
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort");
101101

102-
result = SNINativeMethodWrapper.SniGetConnectionIPString(Handle, ref IPStringFromSNI);
102+
result = SniNativeWrapper.SniGetConnectionIPString(Handle, ref IPStringFromSNI);
103103
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString");
104104

105105
pendingDNSInfo = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString());
@@ -166,13 +166,13 @@ internal override void CreatePhysicalSNIHandle(
166166
{
167167
// Native SNI requires the Unicode encoding and any other encoding like UTF8 breaks the code.
168168
byte[] srvSPN = Encoding.Unicode.GetBytes(serverSPN);
169-
Trace.Assert(srvSPN.Length <= SNINativeMethodWrapper.SniMaxComposedSpnLength, "Length of the provided SPN exceeded the buffer size.");
169+
Trace.Assert(srvSPN.Length <= SniNativeWrapper.SniMaxComposedSpnLength, "Length of the provided SPN exceeded the buffer size.");
170170
spnBuffer[0] = srvSPN;
171171
SqlClientEventSource.Log.TryTraceEvent("<{0}.{1}|SEC> Server SPN `{2}` from the connection string is used.", nameof(TdsParserStateObjectNative), nameof(CreatePhysicalSNIHandle), serverSPN);
172172
}
173173
else
174174
{
175-
spnBuffer[0] = new byte[SNINativeMethodWrapper.SniMaxComposedSpnLength];
175+
spnBuffer[0] = new byte[SniNativeWrapper.SniMaxComposedSpnLength];
176176
}
177177
}
178178

@@ -187,7 +187,7 @@ internal override void CreatePhysicalSNIHandle(
187187
protected override uint SNIPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize)
188188
{
189189
Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
190-
return SNINativeMethodWrapper.SNIPacketGetData(packet.NativePointer, _inBuff, ref dataSize);
190+
return SniNativeWrapper.SNIPacketGetData(packet.NativePointer, _inBuff, ref dataSize);
191191
}
192192

193193
protected override bool CheckPacket(PacketHandle packet, TaskCompletionSource<object> source)
@@ -267,7 +267,7 @@ internal override PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint
267267
throw ADP.ClosedConnectionError();
268268
}
269269
IntPtr readPacketPtr = IntPtr.Zero;
270-
error = SNINativeMethodWrapper.SNIReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining());
270+
error = SniNativeWrapper.SNIReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining());
271271
return PacketHandle.FromNativePointer(readPacketPtr);
272272
}
273273

@@ -284,20 +284,20 @@ internal override bool IsPacketEmpty(PacketHandle readPacket)
284284
internal override void ReleasePacket(PacketHandle syncReadPacket)
285285
{
286286
Debug.Assert(syncReadPacket.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer");
287-
SNINativeMethodWrapper.SNIPacketRelease(syncReadPacket.NativePointer);
287+
SniNativeWrapper.SNIPacketRelease(syncReadPacket.NativePointer);
288288
}
289289

290290
internal override uint CheckConnection()
291291
{
292292
SNIHandle handle = Handle;
293-
return handle == null ? TdsEnums.SNI_SUCCESS : SNINativeMethodWrapper.SNICheckConnection(handle);
293+
return handle == null ? TdsEnums.SNI_SUCCESS : SniNativeWrapper.SNICheckConnection(handle);
294294
}
295295

296296
internal override PacketHandle ReadAsync(SessionHandle handle, out uint error)
297297
{
298298
Debug.Assert(handle.Type == SessionHandle.NativeHandleType, "unexpected handle type when requiring NativePointer");
299299
IntPtr readPacketPtr = IntPtr.Zero;
300-
error = SNINativeMethodWrapper.SNIReadAsync(handle.NativeHandle, ref readPacketPtr);
300+
error = SniNativeWrapper.SNIReadAsync(handle.NativeHandle, ref readPacketPtr);
301301
return PacketHandle.FromNativePointer(readPacketPtr);
302302
}
303303

@@ -313,7 +313,7 @@ internal override PacketHandle CreateAndSetAttentionPacket()
313313
internal override uint WritePacket(PacketHandle packet, bool sync)
314314
{
315315
Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
316-
return SNINativeMethodWrapper.SNIWritePacket(Handle, packet.NativePacket, sync);
316+
return SniNativeWrapper.SNIWritePacket(Handle, packet.NativePacket, sync);
317317
}
318318

319319
internal override PacketHandle AddPacketToPendingList(PacketHandle packetToAdd)
@@ -346,7 +346,7 @@ internal override PacketHandle GetResetWritePacket(int dataSize)
346346
{
347347
if (_sniPacket != null)
348348
{
349-
SNINativeMethodWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
349+
SniNativeWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI);
350350
}
351351
else
352352
{
@@ -375,17 +375,17 @@ internal override void ClearAllWritePackets()
375375
internal override void SetPacketData(PacketHandle packet, byte[] buffer, int bytesUsed)
376376
{
377377
Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket");
378-
SNINativeMethodWrapper.SNIPacketSetData(packet.NativePacket, buffer, bytesUsed);
378+
SniNativeWrapper.SNIPacketSetData(packet.NativePacket, buffer, bytesUsed);
379379
}
380380

381381
internal override uint SniGetConnectionId(ref Guid clientConnectionId)
382-
=> SNINativeMethodWrapper.SniGetConnectionId(Handle, ref clientConnectionId);
382+
=> SniNativeWrapper.SniGetConnectionId(Handle, ref clientConnectionId);
383383

384384
internal override uint DisableSsl()
385-
=> SNINativeMethodWrapper.SNIRemoveProvider(Handle, Provider.SSL_PROV);
385+
=> SniNativeWrapper.SNIRemoveProvider(Handle, Provider.SSL_PROV);
386386

387387
internal override uint EnableMars(ref uint info)
388-
=> SNINativeMethodWrapper.SNIAddProvider(Handle, Provider.SMUX_PROV, ref info);
388+
=> SniNativeWrapper.SNIAddProvider(Handle, Provider.SMUX_PROV, ref info);
389389

390390
internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename)
391391
{
@@ -395,15 +395,15 @@ internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCert
395395
authInfo.serverCertFileName = serverCertificateFilename;
396396

397397
// Add SSL (Encryption) SNI provider.
398-
return SNINativeMethodWrapper.SNIAddProvider(Handle, Provider.SSL_PROV, ref authInfo);
398+
return SniNativeWrapper.SNIAddProvider(Handle, Provider.SSL_PROV, ref authInfo);
399399
}
400400

401401
internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
402-
=> SNINativeMethodWrapper.SNISetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
402+
=> SniNativeWrapper.SNISetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
403403

404404
internal override uint WaitForSSLHandShakeToComplete(out int protocolVersion)
405405
{
406-
uint returnValue = SNINativeMethodWrapper.SNIWaitForSSLHandshakeToComplete(Handle, GetTimeoutRemaining(), out uint nativeProtocolVersion);
406+
uint returnValue = SniNativeWrapper.SNIWaitForSSLHandshakeToComplete(Handle, GetTimeoutRemaining(), out uint nativeProtocolVersion);
407407
var nativeProtocol = (NativeProtocols)nativeProtocolVersion;
408408

409409
#pragma warning disable CA5398 // Avoid hardcoded SslProtocols values
@@ -472,7 +472,7 @@ public SNIPacket Take(SNIHandle sniHandle)
472472
{
473473
// Success - reset the packet
474474
packet = _packets.Pop();
475-
SNINativeMethodWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI);
475+
SniNativeWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI);
476476
}
477477
else
478478
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\IoType.cs">
173173
<Link>Interop\Windows\Sni\IoType.cs</Link>
174174
</Compile>
175+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\ISniNativeMethods.cs">
176+
<Link>Interop\Windows\Sni\ISniNativeMethods.cs</Link>
177+
</Compile>
175178
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\Prefix.cs">
176179
<Link>Interop\Windows\Sni\Prefix.cs</Link>
177180
</Compile>
@@ -193,6 +196,21 @@
193196
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniError.cs">
194197
<Link>Interop\Windows\Sni\SniError.cs</Link>
195198
</Compile>
199+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeMethodsArm64.netfx.cs">
200+
<Link>Interop\Windows\Sni\SniNativeMethodsArm64.netfx.cs</Link>
201+
</Compile>
202+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeMethodsNotSupported.netfx.cs">
203+
<Link>Interop\Windows\Sni\SniNativeMethodsNotSupported.netfx.cs</Link>
204+
</Compile>
205+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeMethodsX64.netfx.cs">
206+
<Link>Interop\Windows\Sni\SniNativeMethodsX64.netfx.cs</Link>
207+
</Compile>
208+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeMethodsX86.netfx.cs">
209+
<Link>Interop\Windows\Sni\SniNativeMethodsX86.netfx.cs</Link>
210+
</Compile>
211+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SniNativeWrapper.cs">
212+
<Link>Interop\Windows\Sni\SniNativeWrapper.cs</Link>
213+
</Compile>
196214
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Sni\SqlDependencyProcessDispatcherStorage.netfx.cs">
197215
<Link>Interop\Windows\Sni\SqlDependencyProcessDispatcherStorage.netfx.cs</Link>
198216
</Compile>
@@ -808,10 +826,6 @@
808826
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
809827
<Compile Include="Microsoft\Data\Common\DbConnectionString.cs" />
810828
<Compile Include="Microsoft\Data\Common\GreenMethods.cs" />
811-
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperARM64.cs" />
812-
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX64.cs" />
813-
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX86.cs" />
814-
<Compile Include="Microsoft\Data\Interop\SNINativeMethodWrapper.cs" />
815829
<Compile Include="Microsoft\Data\SqlClient\assemblycache.cs" />
816830
<Compile Include="Microsoft\Data\SqlClient\LocalDBAPI.cs" />
817831
<Compile Include="Microsoft\Data\SqlClient\LocalDBConfig.cs" />
@@ -895,12 +909,12 @@
895909
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
896910
</PackageReference>
897911
<PackageReference Include="Azure.Identity" Version="$(AzureIdentityVersion)" />
912+
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="$(MicrosoftBclCryptographyVersion)" />
898913
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="$(MicrosoftIdentityModelProtocolsOpenIdConnectVersion)" />
899914
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="$(MicrosoftIdentityModelJsonWebTokensVersion)" />
900915
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
901-
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
902916
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="$(SystemSecurityCryptographyPkcsVersion)" />
903-
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="$(MicrosoftBclCryptographyVersion)" />
917+
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
904918
</ItemGroup>
905919
<Import Project="$(CommonSourceRoot)tools\targets\GenerateResourceStringsSource.targets" />
906920
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />

0 commit comments

Comments
 (0)