diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Unix.cs b/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Unix.cs deleted file mode 100644 index 3b512f0427..0000000000 --- a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Unix.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; - -namespace Microsoft.Data -{ - internal static partial class SafeNativeMethods - { - internal static IntPtr GetProcAddress(IntPtr HModule, string funcName) - { - throw new PlatformNotSupportedException("SafeNativeMethods.GetProcAddress is not supported on non-Windows platforms"); - } - } -} - -namespace Microsoft.Data -{ - internal static class Win32NativeMethods - { - internal static bool IsTokenRestrictedWrapper(IntPtr token) - { - throw new PlatformNotSupportedException("Win32NativeMethods.IsTokenRestrictedWrapper is not supported on non-Windows platforms"); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs deleted file mode 100644 index 41708b7d8f..0000000000 --- a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs +++ /dev/null @@ -1,359 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; -using System.Text; -using Interop.Windows.Sni; -using Microsoft.Data.Common; -using Microsoft.Data.SqlClient; - -namespace Microsoft.Data.SqlClient -{ - internal static partial class SNINativeMethodWrapper - { - private const string SNI = "Microsoft.Data.SqlClient.SNI.dll"; - - private static int s_sniMaxComposedSpnLength = -1; - - private const int SniOpenTimeOut = -1; // infinite - - internal const int SniIP6AddrStringBufferLength = 48; // from SNI layer - - internal static int SniMaxComposedSpnLength - { - get - { - if (s_sniMaxComposedSpnLength == -1) - { - s_sniMaxComposedSpnLength = checked((int)GetSniMaxComposedSpnLength()); - } - return s_sniMaxComposedSpnLength; - } - } - - #region DLL Imports - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref uint pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref AuthProviderInfo pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICheckConnectionWrapper")] - internal static extern uint SNICheckConnection([In] SNIHandle pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICloseWrapper")] - internal static extern uint SNIClose(IntPtr pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIGetLastError(out SniError pErrorStruct); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIPacketRelease(IntPtr pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIPacketResetWrapper")] - internal static extern void SNIPacketReset([In] SNIHandle pConn, IoType IOType, SNIPacket pPacket, ConsumerNumber ConsNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref IntPtr pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIReadAsyncWrapper")] - internal static extern uint SNIReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIRemoveProviderWrapper")] - internal static extern uint SNIRemoveProvider(SNIHandle pConn, Provider ProvNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNISecInitPackage(ref uint pcbMaxToken); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISetInfoWrapper")] - internal static extern uint SNISetInfo(SNIHandle pConn, QueryType QType, [In] ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNITerminate(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIWaitForSSLHandshakeToCompleteWrapper")] - internal static extern uint SNIWaitForSSLHandshakeToComplete([In] SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint UnmanagedIsTokenRestricted([In] IntPtr token, [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint GetSniMaxComposedSpnLength(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Guid pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out ushort portNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] - private static extern uint SNIGetPeerAddrStrWrapper([In] SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLen); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Provider provNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIInitialize([In] IntPtr pmo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIOpenWrapper( - [In] ref SniConsumerInfo pConsumerInfo, - [MarshalAs(UnmanagedType.LPWStr)] string szConnect, - [In] SNIHandle pConn, - out IntPtr ppConn, - [MarshalAs(UnmanagedType.Bool)] bool fSync, - SqlConnectionIPAddressPreference ipPreference, - [In] ref SniDnsCacheInfo pDNSCachedInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType IOType); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIPacketGetDataWrapper([In] IntPtr packet, [In, Out] byte[] readBuffer, uint readBufferLength, out uint dataSize); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern unsafe uint SNISecGenClientContextWrapper( - [In] SNIHandle pConn, - [In, Out] byte* pIn, - uint cbIn, - [In, Out] byte[] pOut, - [In] ref uint pcbOut, - [MarshalAsAttribute(UnmanagedType.Bool)] out bool pfDone, - byte* szServerInfo, - uint cbServerInfo, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszUserName, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszPassword); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumOpenWrapper")] - internal static extern IntPtr SNIServerEnumOpen(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumCloseWrapper")] - internal static extern void SNIServerEnumClose([In] IntPtr packet); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumReadWrapper", CharSet = CharSet.Unicode)] - internal static extern int SNIServerEnumRead([In] IntPtr packet, - [In][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, - [In] int bufferLength, - [MarshalAs(UnmanagedType.Bool)] out bool more); - #endregion - - internal static uint SniGetConnectionId(SNIHandle pConn, ref Guid connId) - { - return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_CONNID, out connId); - } - - internal static uint SniGetProviderNumber(SNIHandle pConn, ref Provider provNum) - { - return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_PROVIDERNUM, out provNum); - } - - internal static uint SniGetConnectionPort(SNIHandle pConn, ref ushort portNum) - { - return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_PEERPORT, out portNum); - } - - internal static uint SniGetConnectionIPString(SNIHandle pConn, ref string connIPStr) - { - UInt32 ret; - uint connIPLen = 0; - - int bufferSize = SniIP6AddrStringBufferLength; - StringBuilder addrBuffer = new StringBuilder(bufferSize); - - ret = SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out connIPLen); - - connIPStr = addrBuffer.ToString(0, Convert.ToInt32(connIPLen)); - - return ret; - } - - internal static uint SNIInitialize() - { - return SNIInitialize(IntPtr.Zero); - } - - internal static unsafe uint SNIOpenMarsSession(ConsumerInfo consumerInfo, SNIHandle parent, ref IntPtr pConn, bool fSync, SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo) - { - // initialize consumer info for MARS - SniConsumerInfo native_consumerInfo = new SniConsumerInfo(); - MarshalConsumerInfo(consumerInfo, ref native_consumerInfo); - - SniDnsCacheInfo native_cachedDNSInfo = new SniDnsCacheInfo(); - native_cachedDNSInfo.wszCachedFQDN = cachedDNSInfo?.FQDN; - native_cachedDNSInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4; - native_cachedDNSInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6; - native_cachedDNSInfo.wszCachedTcpPort = cachedDNSInfo?.Port; - - return SNIOpenWrapper(ref native_consumerInfo, "session:", parent, out pConn, fSync, ipPreference, ref native_cachedDNSInfo); - } - - internal static unsafe uint SNIOpenSyncEx( - ConsumerInfo consumerInfo, - string constring, - ref IntPtr pConn, - byte[] spnBuffer, - byte[] instanceName, - bool fOverrideCache, - bool fSync, - int timeout, - bool fParallel, - SqlConnectionIPAddressPreference ipPreference, - SQLDNSInfo cachedDNSInfo, - string hostNameInCertificate) - { - - fixed (byte* pin_instanceName = &instanceName[0]) - { - SniClientConsumerInfo clientConsumerInfo = new SniClientConsumerInfo(); - - // initialize client ConsumerInfo part first - MarshalConsumerInfo(consumerInfo, ref clientConsumerInfo.ConsumerInfo); - - clientConsumerInfo.wszConnectionString = constring; - clientConsumerInfo.HostNameInCertificate = hostNameInCertificate; - clientConsumerInfo.networkLibrary = Prefix.UNKNOWN_PREFIX; - clientConsumerInfo.szInstanceName = pin_instanceName; - clientConsumerInfo.cchInstanceName = (uint)instanceName.Length; - clientConsumerInfo.fOverrideLastConnectCache = fOverrideCache; - clientConsumerInfo.fSynchronousConnection = fSync; - clientConsumerInfo.timeout = timeout; - clientConsumerInfo.fParallel = fParallel; - - clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.DisabledMode; - clientConsumerInfo.totalTimeout = SniOpenTimeOut; - clientConsumerInfo.isAzureSqlServerEndpoint = ADP.IsAzureSqlServerEndpoint(constring); - - clientConsumerInfo.ipAddressPreference = ipPreference; - clientConsumerInfo.DNSCacheInfo.wszCachedFQDN = cachedDNSInfo?.FQDN; - clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4; - clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6; - clientConsumerInfo.DNSCacheInfo.wszCachedTcpPort = cachedDNSInfo?.Port; - - if (spnBuffer != null) - { - fixed (byte* pin_spnBuffer = &spnBuffer[0]) - { - clientConsumerInfo.szSPN = pin_spnBuffer; - clientConsumerInfo.cchSPN = (uint)spnBuffer.Length; - return SNIOpenSyncExWrapper(ref clientConsumerInfo, out pConn); - } - } - else - { - // else leave szSPN null (SQL Auth) - return SNIOpenSyncExWrapper(ref clientConsumerInfo, out pConn); - } - } - } - - internal static void SNIPacketAllocate(SafeHandle pConn, IoType IOType, ref IntPtr pPacket) - { - pPacket = SNIPacketAllocateWrapper(pConn, IOType); - } - - internal static unsafe uint SNIPacketGetData(IntPtr packet, byte[] readBuffer, ref uint dataSize) - { - return SNIPacketGetDataWrapper(packet, readBuffer, (uint)readBuffer.Length, out dataSize); - } - - internal static unsafe void SNIPacketSetData(SNIPacket packet, byte[] data, int length) - { - fixed (byte* pin_data = &data[0]) - { - SNIPacketSetData(packet, pin_data, (uint)length); - } - } - - internal static unsafe uint SNISecGenClientContext(SNIHandle pConnectionObject, ReadOnlySpan inBuff, byte[] OutBuff, ref uint sendLength, byte[] serverUserName) - { - fixed (byte* pin_serverUserName = &serverUserName[0]) - fixed (byte* pInBuff = inBuff) - { - return SNISecGenClientContextWrapper( - pConnectionObject, - pInBuff, - (uint)inBuff.Length, - OutBuff, - ref sendLength, - out _, - pin_serverUserName, - (uint)serverUserName.Length, - null, - null); - } - } - - internal static uint SNIWritePacket(SNIHandle pConn, SNIPacket packet, bool sync) - { - if (sync) - { - return SNIWriteSyncOverAsync(pConn, packet); - } - else - { - return SNIWriteAsyncWrapper(pConn, packet); - } - } - - private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref SniConsumerInfo native_consumerInfo) - { - native_consumerInfo.DefaultUserDataLength = consumerInfo.defaultBufferSize; - native_consumerInfo.fnReadComp = consumerInfo.readDelegate != null - ? Marshal.GetFunctionPointerForDelegate(consumerInfo.readDelegate) - : IntPtr.Zero; - native_consumerInfo.fnWriteComp = consumerInfo.writeDelegate != null - ? Marshal.GetFunctionPointerForDelegate(consumerInfo.writeDelegate) - : IntPtr.Zero; - native_consumerInfo.ConsumerKey = consumerInfo.key; - } - } -} - -namespace Microsoft.Data -{ - internal static partial class SafeNativeMethods - { - [DllImport("kernel32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)] - internal static extern IntPtr GetProcAddress(IntPtr HModule, [MarshalAs(UnmanagedType.LPStr), In] string funcName); - } -} - -namespace Microsoft.Data -{ - internal static class Win32NativeMethods - { - internal static bool IsTokenRestrictedWrapper(IntPtr token) - { - bool isRestricted; - uint result = SNINativeMethodWrapper.UnmanagedIsTokenRestricted(token, out isRestricted); - - if (result != 0) - { - Marshal.ThrowExceptionForHR(unchecked((int)result)); - } - - return isRestricted; - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 79690c780b..6246fc590d 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -731,6 +731,9 @@ Interop\Windows\Sni\IoType.cs + + Interop\Windows\Sni\ISniNativeMethods.cs + Interop\Windows\Sni\Prefix.cs @@ -752,6 +755,12 @@ Interop\Windows\Sni\SniError.cs + + Interop\Windows\Sni\SniNativeMethods.netcore.cs + + + Interop\Windows\Sni\SniNativeWrapper.cs + Interop\Windows\Sni\TransparentNetworkResolutionMode.cs @@ -788,8 +797,7 @@ Microsoft\Data\SqlTypes\SqlFileStream.Windows.cs - - + @@ -808,8 +816,7 @@ Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.Unix.cs - - + diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs index cd51f9ca82..e6b9a685dc 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/LocalDBAPI.Windows.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using Interop.Windows.Kernel32; using Microsoft.Data.SqlClient; using Interop.Windows.Sni; @@ -10,7 +11,8 @@ namespace Microsoft.Data { internal static partial class LocalDBAPI { - private static IntPtr LoadProcAddress() => SafeNativeMethods.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage"); + private static IntPtr LoadProcAddress() => + Kernel32.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage"); private static IntPtr UserInstanceDLLHandle { @@ -22,15 +24,15 @@ private static IntPtr UserInstanceDLLHandle { if (s_userInstanceDLLHandle == IntPtr.Zero) { - SNINativeMethodWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle); + SniNativeWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle); if (s_userInstanceDLLHandle != IntPtr.Zero) { SqlClientEventSource.Log.TryTraceEvent("LocalDBAPI.UserInstanceDLLHandle | LocalDB - handle obtained"); } else { - SNINativeMethodWrapper.SNIGetLastError(out SniError sniError); - throw CreateLocalDBException(StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError.sniError); + SniNativeWrapper.SNIGetLastError(out SniError sniError); + throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: sniError.sniError); } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs index 480eb8dc97..e2d86dc210 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs @@ -75,7 +75,7 @@ private SNIErrorDetails GetSniErrorDetails() } else { - SNINativeMethodWrapper.SNIGetLastError(out SniError sniError); + SniNativeWrapper.SNIGetLastError(out SniError sniError); details.sniErrorNumber = sniError.sniError; details.errorMessage = sniError.errorMessage; details.nativeError = sniError.nativeError; diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs index 62cc0e9bd4..3534b61740 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectNative.cs @@ -84,7 +84,7 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache if (string.IsNullOrEmpty(userProtocol)) { - result = SNINativeMethodWrapper.SniGetProviderNumber(Handle, ref providerNumber); + result = SniNativeWrapper.SniGetProviderNumber(Handle, ref providerNumber); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber"); _parser.isTcpProtocol = (providerNumber == Provider.TCP_PROV); } @@ -96,10 +96,10 @@ internal override void AssignPendingDNSInfo(string userProtocol, string DNSCache // serverInfo.UserProtocol could be empty if (_parser.isTcpProtocol) { - result = SNINativeMethodWrapper.SniGetConnectionPort(Handle, ref portFromSNI); + result = SniNativeWrapper.SniGetConnectionPort(Handle, ref portFromSNI); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort"); - result = SNINativeMethodWrapper.SniGetConnectionIPString(Handle, ref IPStringFromSNI); + result = SniNativeWrapper.SniGetConnectionIPString(Handle, ref IPStringFromSNI); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString"); pendingDNSInfo = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString()); @@ -166,13 +166,13 @@ internal override void CreatePhysicalSNIHandle( { // Native SNI requires the Unicode encoding and any other encoding like UTF8 breaks the code. byte[] srvSPN = Encoding.Unicode.GetBytes(serverSPN); - Trace.Assert(srvSPN.Length <= SNINativeMethodWrapper.SniMaxComposedSpnLength, "Length of the provided SPN exceeded the buffer size."); + Trace.Assert(srvSPN.Length <= SniNativeWrapper.SniMaxComposedSpnLength, "Length of the provided SPN exceeded the buffer size."); spnBuffer[0] = srvSPN; SqlClientEventSource.Log.TryTraceEvent("<{0}.{1}|SEC> Server SPN `{2}` from the connection string is used.", nameof(TdsParserStateObjectNative), nameof(CreatePhysicalSNIHandle), serverSPN); } else { - spnBuffer[0] = new byte[SNINativeMethodWrapper.SniMaxComposedSpnLength]; + spnBuffer[0] = new byte[SniNativeWrapper.SniMaxComposedSpnLength]; } } @@ -187,7 +187,7 @@ internal override void CreatePhysicalSNIHandle( protected override uint SNIPacketGetData(PacketHandle packet, byte[] _inBuff, ref uint dataSize) { Debug.Assert(packet.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer"); - return SNINativeMethodWrapper.SNIPacketGetData(packet.NativePointer, _inBuff, ref dataSize); + return SniNativeWrapper.SNIPacketGetData(packet.NativePointer, _inBuff, ref dataSize); } protected override bool CheckPacket(PacketHandle packet, TaskCompletionSource source) @@ -267,7 +267,7 @@ internal override PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint throw ADP.ClosedConnectionError(); } IntPtr readPacketPtr = IntPtr.Zero; - error = SNINativeMethodWrapper.SNIReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining()); + error = SniNativeWrapper.SNIReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining()); return PacketHandle.FromNativePointer(readPacketPtr); } @@ -284,20 +284,20 @@ internal override bool IsPacketEmpty(PacketHandle readPacket) internal override void ReleasePacket(PacketHandle syncReadPacket) { Debug.Assert(syncReadPacket.Type == PacketHandle.NativePointerType, "unexpected packet type when requiring NativePointer"); - SNINativeMethodWrapper.SNIPacketRelease(syncReadPacket.NativePointer); + SniNativeWrapper.SNIPacketRelease(syncReadPacket.NativePointer); } internal override uint CheckConnection() { SNIHandle handle = Handle; - return handle == null ? TdsEnums.SNI_SUCCESS : SNINativeMethodWrapper.SNICheckConnection(handle); + return handle == null ? TdsEnums.SNI_SUCCESS : SniNativeWrapper.SNICheckConnection(handle); } internal override PacketHandle ReadAsync(SessionHandle handle, out uint error) { Debug.Assert(handle.Type == SessionHandle.NativeHandleType, "unexpected handle type when requiring NativePointer"); IntPtr readPacketPtr = IntPtr.Zero; - error = SNINativeMethodWrapper.SNIReadAsync(handle.NativeHandle, ref readPacketPtr); + error = SniNativeWrapper.SNIReadAsync(handle.NativeHandle, ref readPacketPtr); return PacketHandle.FromNativePointer(readPacketPtr); } @@ -313,7 +313,7 @@ internal override PacketHandle CreateAndSetAttentionPacket() internal override uint WritePacket(PacketHandle packet, bool sync) { Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket"); - return SNINativeMethodWrapper.SNIWritePacket(Handle, packet.NativePacket, sync); + return SniNativeWrapper.SNIWritePacket(Handle, packet.NativePacket, sync); } internal override PacketHandle AddPacketToPendingList(PacketHandle packetToAdd) @@ -346,7 +346,7 @@ internal override PacketHandle GetResetWritePacket(int dataSize) { if (_sniPacket != null) { - SNINativeMethodWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI); + SniNativeWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI); } else { @@ -375,17 +375,17 @@ internal override void ClearAllWritePackets() internal override void SetPacketData(PacketHandle packet, byte[] buffer, int bytesUsed) { Debug.Assert(packet.Type == PacketHandle.NativePacketType, "unexpected packet type when requiring NativePacket"); - SNINativeMethodWrapper.SNIPacketSetData(packet.NativePacket, buffer, bytesUsed); + SniNativeWrapper.SNIPacketSetData(packet.NativePacket, buffer, bytesUsed); } internal override uint SniGetConnectionId(ref Guid clientConnectionId) - => SNINativeMethodWrapper.SniGetConnectionId(Handle, ref clientConnectionId); + => SniNativeWrapper.SniGetConnectionId(Handle, ref clientConnectionId); internal override uint DisableSsl() - => SNINativeMethodWrapper.SNIRemoveProvider(Handle, Provider.SSL_PROV); + => SniNativeWrapper.SNIRemoveProvider(Handle, Provider.SSL_PROV); internal override uint EnableMars(ref uint info) - => SNINativeMethodWrapper.SNIAddProvider(Handle, Provider.SMUX_PROV, ref info); + => SniNativeWrapper.SNIAddProvider(Handle, Provider.SMUX_PROV, ref info); internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCertificateFilename) { @@ -395,15 +395,15 @@ internal override uint EnableSsl(ref uint info, bool tlsFirst, string serverCert authInfo.serverCertFileName = serverCertificateFilename; // Add SSL (Encryption) SNI provider. - return SNINativeMethodWrapper.SNIAddProvider(Handle, Provider.SSL_PROV, ref authInfo); + return SniNativeWrapper.SNIAddProvider(Handle, Provider.SSL_PROV, ref authInfo); } internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize) - => SNINativeMethodWrapper.SNISetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize); + => SniNativeWrapper.SNISetInfo(Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize); internal override uint WaitForSSLHandShakeToComplete(out int protocolVersion) { - uint returnValue = SNINativeMethodWrapper.SNIWaitForSSLHandshakeToComplete(Handle, GetTimeoutRemaining(), out uint nativeProtocolVersion); + uint returnValue = SniNativeWrapper.SNIWaitForSSLHandshakeToComplete(Handle, GetTimeoutRemaining(), out uint nativeProtocolVersion); var nativeProtocol = (NativeProtocols)nativeProtocolVersion; #pragma warning disable CA5398 // Avoid hardcoded SslProtocols values @@ -472,7 +472,7 @@ public SNIPacket Take(SNIHandle sniHandle) { // Success - reset the packet packet = _packets.Pop(); - SNINativeMethodWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI); + SniNativeWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI); } else { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index bb0d68011e..591977ef2d 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -172,6 +172,9 @@ Interop\Windows\Sni\IoType.cs + + Interop\Windows\Sni\ISniNativeMethods.cs + Interop\Windows\Sni\Prefix.cs @@ -193,6 +196,21 @@ Interop\Windows\Sni\SniError.cs + + Interop\Windows\Sni\SniNativeMethodsArm64.netfx.cs + + + Interop\Windows\Sni\SniNativeMethodsNotSupported.netfx.cs + + + Interop\Windows\Sni\SniNativeMethodsX64.netfx.cs + + + Interop\Windows\Sni\SniNativeMethodsX86.netfx.cs + + + Interop\Windows\Sni\SniNativeWrapper.cs + Interop\Windows\Sni\SqlDependencyProcessDispatcherStorage.netfx.cs @@ -808,10 +826,6 @@ - - - - @@ -895,12 +909,12 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + - - + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperARM64.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperARM64.cs deleted file mode 100644 index 3c836ebeb1..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperARM64.cs +++ /dev/null @@ -1,141 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; -using System.Text; -using Interop.Windows.Sni; - -namespace Microsoft.Data.SqlClient -{ - internal static class SNINativeManagedWrapperARM64 - { - private const string SNI = "Microsoft.Data.SqlClient.SNI.arm64.dll"; - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref uint pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider ProvNum, [In] ref AuthProviderInfo pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICheckConnectionWrapper")] - internal static extern uint SNICheckConnection([In] SNIHandle pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICloseWrapper")] - internal static extern uint SNIClose(IntPtr pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIGetLastError(out SniError pErrorStruct); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIPacketRelease(IntPtr pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIPacketResetWrapper")] - internal static extern void SNIPacketReset([In] SNIHandle pConn, IoType IOType, SNIPacket pPacket, ConsumerNumber ConsNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref IntPtr pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIReadAsyncWrapper")] - internal static extern uint SNIReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIRemoveProviderWrapper")] - internal static extern uint SNIRemoveProvider(SNIHandle pConn, Provider ProvNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNISecInitPackage(ref uint pcbMaxToken); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISetInfoWrapper")] - internal static extern uint SNISetInfo(SNIHandle pConn, QueryType QType, [In] ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNITerminate(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIWaitForSSLHandshakeToCompleteWrapper")] - internal static extern uint SNIWaitForSSLHandshakeToComplete([In] SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint UnmanagedIsTokenRestricted([In] IntPtr token, [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint GetSniMaxComposedSpnLength(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Guid pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out ushort portNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] - internal static extern uint SNIGetPeerAddrStrWrapper([In] SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLen); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Provider provNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIInitialize")] - internal static extern uint SNIInitialize([In] IntPtr pmo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIOpenWrapper( - [In] ref SniConsumerInfo pConsumerInfo, - [MarshalAs(UnmanagedType.LPWStr)] string szConnect, - [In] SNIHandle pConn, - out IntPtr ppConn, - [MarshalAs(UnmanagedType.Bool)] bool fSync, - SqlConnectionIPAddressPreference ipPreference, - [In] ref SniDnsCacheInfo pDNSCachedInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType IOType); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIPacketGetDataWrapper([In] IntPtr packet, [In, Out] byte[] readBuffer, uint readBufferLength, out uint dataSize); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISecGenClientContextWrapper")] - internal static extern unsafe uint SNISecGenClientContextWrapper( - [In] SNIHandle pConn, - [In, Out] byte* pIn, - uint cbIn, - [In, Out] byte[] pOut, - [In] ref uint pcbOut, - [MarshalAsAttribute(UnmanagedType.Bool)] out bool pfDone, - byte* szServerInfo, - uint cbServerInfo, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszUserName, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszPassword); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumOpenWrapper")] - internal static extern IntPtr SNIServerEnumOpen(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumCloseWrapper")] - internal static extern void SNIServerEnumClose([In] IntPtr packet); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumReadWrapper", CharSet = CharSet.Unicode)] - internal static extern int SNIServerEnumRead([In] IntPtr packet, - [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, - [In] int bufferLength, - [MarshalAs(UnmanagedType.Bool)] out bool more); - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs deleted file mode 100644 index 149f1d2f3a..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs +++ /dev/null @@ -1,142 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; -using System.Text; -using Interop.Windows.Sni; -using static Microsoft.Data.SqlClient.SNINativeMethodWrapper; - -namespace Microsoft.Data.SqlClient -{ - internal static class SNINativeManagedWrapperX64 - { - private const string SNI = "Microsoft.Data.SqlClient.SNI.x64.dll"; - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref uint pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider ProvNum, [In] ref AuthProviderInfo pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICheckConnectionWrapper")] - internal static extern uint SNICheckConnection([In] SNIHandle pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICloseWrapper")] - internal static extern uint SNIClose(IntPtr pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIGetLastError(out SniError pErrorStruct); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIPacketRelease(IntPtr pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIPacketResetWrapper")] - internal static extern void SNIPacketReset([In] SNIHandle pConn, IoType IOType, SNIPacket pPacket, ConsumerNumber ConsNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref IntPtr pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIReadAsyncWrapper")] - internal static extern uint SNIReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIRemoveProviderWrapper")] - internal static extern uint SNIRemoveProvider(SNIHandle pConn, Provider ProvNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNISecInitPackage(ref uint pcbMaxToken); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISetInfoWrapper")] - internal static extern uint SNISetInfo(SNIHandle pConn, QueryType QType, [In] ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNITerminate(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIWaitForSSLHandshakeToCompleteWrapper")] - internal static extern uint SNIWaitForSSLHandshakeToComplete([In] SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint UnmanagedIsTokenRestricted([In] IntPtr token, [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint GetSniMaxComposedSpnLength(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Guid pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out ushort portNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] - internal static extern uint SNIGetPeerAddrStrWrapper([In] SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLen); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Provider provNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIInitialize")] - internal static extern uint SNIInitialize([In] IntPtr pmo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIOpenWrapper( - [In] ref SniConsumerInfo pConsumerInfo, - [MarshalAs(UnmanagedType.LPWStr)] string szConnect, - [In] SNIHandle pConn, - out IntPtr ppConn, - [MarshalAs(UnmanagedType.Bool)] bool fSync, - SqlConnectionIPAddressPreference ipPreference, - [In] ref SniDnsCacheInfo pDNSCachedInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType IOType); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIPacketGetDataWrapper([In] IntPtr packet, [In, Out] byte[] readBuffer, uint readBufferLength, out uint dataSize); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISecGenClientContextWrapper")] - internal static extern unsafe uint SNISecGenClientContextWrapper( - [In] SNIHandle pConn, - [In, Out] byte* pIn, - uint cbIn, - [In, Out] byte[] pOut, - [In] ref uint pcbOut, - [MarshalAsAttribute(UnmanagedType.Bool)] out bool pfDone, - byte* szServerInfo, - uint cbServerInfo, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszUserName, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszPassword); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumOpenWrapper")] - internal static extern IntPtr SNIServerEnumOpen(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumCloseWrapper")] - internal static extern void SNIServerEnumClose([In] IntPtr packet); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumReadWrapper", CharSet = CharSet.Unicode)] - internal static extern int SNIServerEnumRead([In] IntPtr packet, - [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, - [In] int bufferLength, - [MarshalAs(UnmanagedType.Bool)] out bool more); - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs deleted file mode 100644 index 834355b488..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs +++ /dev/null @@ -1,142 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; -using System.Text; -using Interop.Windows.Sni; -using static Microsoft.Data.SqlClient.SNINativeMethodWrapper; - -namespace Microsoft.Data.SqlClient -{ - internal static class SNINativeManagedWrapperX86 - { - private const string SNI = "Microsoft.Data.SqlClient.SNI.x86.dll"; - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref uint pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIAddProviderWrapper")] - internal static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider ProvNum, [In] ref AuthProviderInfo pInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICheckConnectionWrapper")] - internal static extern uint SNICheckConnection([In] SNIHandle pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNICloseWrapper")] - internal static extern uint SNIClose(IntPtr pConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIGetLastError(out SniError pErrorStruct); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern void SNIPacketRelease(IntPtr pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIPacketResetWrapper")] - internal static extern void SNIPacketReset([In] SNIHandle pConn, IoType IOType, SNIPacket pPacket, ConsumerNumber ConsNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIQueryInfo(QueryType QType, ref IntPtr pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIReadAsyncWrapper")] - internal static extern uint SNIReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIRemoveProviderWrapper")] - internal static extern uint SNIRemoveProvider(SNIHandle pConn, Provider ProvNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNISecInitPackage(ref uint pcbMaxToken); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISetInfoWrapper")] - internal static extern uint SNISetInfo(SNIHandle pConn, QueryType QType, [In] ref uint pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNITerminate(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIWaitForSSLHandshakeToCompleteWrapper")] - internal static extern uint SNIWaitForSSLHandshakeToComplete([In] SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint UnmanagedIsTokenRestricted([In] IntPtr token, [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint GetSniMaxComposedSpnLength(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Guid pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out ushort portNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] - internal static extern uint SNIGetPeerAddrStrWrapper([In] SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLen); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Provider provNum); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIInitialize")] - internal static extern uint SNIInitialize([In] IntPtr pmo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIOpenWrapper( - [In] ref SniConsumerInfo pConsumerInfo, - [MarshalAs(UnmanagedType.LPWStr)] string szConnect, - [In] SNIHandle pConn, - out IntPtr ppConn, - [MarshalAs(UnmanagedType.Bool)] bool fSync, - SqlConnectionIPAddressPreference ipPreference, - [In] ref SniDnsCacheInfo pDNSCachedInfo); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType IOType); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIPacketGetDataWrapper([In] IntPtr packet, [In, Out] byte[] readBuffer, uint readBufferLength, out uint dataSize); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNISecGenClientContextWrapper")] - internal static extern unsafe uint SNISecGenClientContextWrapper( - [In] SNIHandle pConn, - [In, Out] byte* pIn, - uint cbIn, - [In, Out] byte[] pOut, - [In] ref uint pcbOut, - [MarshalAsAttribute(UnmanagedType.Bool)] out bool pfDone, - byte* szServerInfo, - uint cbServerInfo, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszUserName, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszPassword); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)] - internal static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumOpenWrapper")] - internal static extern IntPtr SNIServerEnumOpen(); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumCloseWrapper")] - internal static extern void SNIServerEnumClose([In] IntPtr packet); - - [DllImport(SNI, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SNIServerEnumReadWrapper", CharSet = CharSet.Unicode)] - internal static extern int SNIServerEnumRead([In] IntPtr packet, - [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, - [In] int bufferLength, - [MarshalAs(UnmanagedType.Bool)] out bool more); - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs deleted file mode 100644 index de4e344ff0..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs +++ /dev/null @@ -1,986 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.Versioning; -using System.Security; -using System.Text; -using Interop.Windows.Sni; -using Microsoft.Data.Common; -using Microsoft.Data.SqlClient; - -namespace Microsoft.Data.SqlClient -{ - internal static class SNINativeMethodWrapper - { - private static int s_sniMaxComposedSpnLength = -1; - private static readonly System.Runtime.InteropServices.Architecture s_architecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture; - - private const int SniOpenTimeOut = -1; // infinite - - internal const int SniIP6AddrStringBufferLength = 48; // from SNI layer - - internal static int SniMaxComposedSpnLength - { - get - { - if (s_sniMaxComposedSpnLength == -1) - { - s_sniMaxComposedSpnLength = checked((int)GetSniMaxComposedSpnLength()); - } - return s_sniMaxComposedSpnLength; - } - } - - static AppDomain GetDefaultAppDomainInternal() - { - return AppDomain.CurrentDomain; - } - - internal static _AppDomain GetDefaultAppDomain() - { - return GetDefaultAppDomainInternal(); - } - - [ResourceExposure(ResourceScope.Process)] // SxS: there is no way to set scope = Instance, using Process which is wider - [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] - internal unsafe static byte[] GetData() - { - int size; - IntPtr ptr = (IntPtr)(SqlDependencyProcessDispatcherStorage.NativeGetData(out size)); - byte[] result = null; - - if (ptr != IntPtr.Zero) - { - result = new byte[size]; - Marshal.Copy(ptr, result, 0, size); - } - - return result; - } - - [ResourceExposure(ResourceScope.Process)] // SxS: there is no way to set scope = Instance, using Process which is wider - [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] - internal unsafe static void SetData(Byte[] data) - { - //cli::pin_ptr pin_dispatcher = &data[0]; - fixed (byte* pin_dispatcher = &data[0]) - { - SqlDependencyProcessDispatcherStorage.NativeSetData(pin_dispatcher, data.Length); - } - } - - #region DLL Imports - internal static uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref uint pInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIAddProvider(pConn, ProvNum, ref pInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIAddProvider(pConn, ProvNum, ref pInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIAddProvider(pConn, ProvNum, ref pInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIAddProviderWrapper(SNIHandle pConn, Provider ProvNum, [In] ref AuthProviderInfo pInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIAddProviderWrapper(pConn, ProvNum, ref pInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIAddProviderWrapper(pConn, ProvNum, ref pInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIAddProviderWrapper(pConn, ProvNum, ref pInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNICheckConnection([In] SNIHandle pConn) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNICheckConnection(pConn); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNICheckConnection(pConn); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNICheckConnection(pConn); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIClose(IntPtr pConn) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIClose(pConn); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIClose(pConn); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIClose(pConn); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static void SNIGetLastError(out SniError pErrorStruct) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - SNINativeManagedWrapperARM64.SNIGetLastError(out pErrorStruct); - break; - case System.Runtime.InteropServices.Architecture.X64: - SNINativeManagedWrapperX64.SNIGetLastError(out pErrorStruct); - break; - case System.Runtime.InteropServices.Architecture.X86: - SNINativeManagedWrapperX86.SNIGetLastError(out pErrorStruct); - break; - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static void SNIPacketRelease(IntPtr pPacket) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - SNINativeManagedWrapperARM64.SNIPacketRelease(pPacket); - break; - case System.Runtime.InteropServices.Architecture.X64: - SNINativeManagedWrapperX64.SNIPacketRelease(pPacket); - break; - case System.Runtime.InteropServices.Architecture.X86: - SNINativeManagedWrapperX86.SNIPacketRelease(pPacket); - break; - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static void SNIPacketReset([In] SNIHandle pConn, IoType IOType, SNIPacket pPacket, ConsumerNumber ConsNum) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - SNINativeManagedWrapperARM64.SNIPacketReset(pConn, IOType, pPacket, ConsNum); - break; - case System.Runtime.InteropServices.Architecture.X64: - SNINativeManagedWrapperX64.SNIPacketReset(pConn, IOType, pPacket, ConsNum); - break; - case System.Runtime.InteropServices.Architecture.X86: - SNINativeManagedWrapperX86.SNIPacketReset(pConn, IOType, pPacket, ConsNum); - break; - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIQueryInfo(QueryType QType, ref uint pbQInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIQueryInfo(QType, ref pbQInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIQueryInfo(QType, ref pbQInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIQueryInfo(QType, ref pbQInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIQueryInfo(QueryType QType, ref IntPtr pbQInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIQueryInfo(QType, ref pbQInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIQueryInfo(QType, ref pbQInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIQueryInfo(QType, ref pbQInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIReadAsync(pConn, ref ppNewPacket); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIReadAsync(pConn, ref ppNewPacket); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIReadAsync(pConn, ref ppNewPacket); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIRemoveProvider(SNIHandle pConn, Provider ProvNum) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIRemoveProvider(pConn, ProvNum); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIRemoveProvider(pConn, ProvNum); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIRemoveProvider(pConn, ProvNum); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNISecInitPackage(ref uint pcbMaxToken) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNISecInitPackage(ref pcbMaxToken); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNISecInitPackage(ref pcbMaxToken); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNISecInitPackage(ref pcbMaxToken); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNISetInfo(SNIHandle pConn, QueryType QType, [In] ref uint pbQInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNISetInfo(pConn, QType, ref pbQInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNISetInfo(pConn, QType, ref pbQInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNISetInfo(pConn, QType, ref pbQInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNITerminate() - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNITerminate(); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNITerminate(); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNITerminate(); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint SNIWaitForSSLHandshakeToComplete([In] SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIWaitForSSLHandshakeToComplete(pConn, dwMilliseconds, out pProtocolVersion); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIWaitForSSLHandshakeToComplete(pConn, dwMilliseconds, out pProtocolVersion); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIWaitForSSLHandshakeToComplete(pConn, dwMilliseconds, out pProtocolVersion); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static uint UnmanagedIsTokenRestricted([In] IntPtr token, [MarshalAs(UnmanagedType.Bool)] out bool isRestricted) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.UnmanagedIsTokenRestricted(token, out isRestricted); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.UnmanagedIsTokenRestricted(token, out isRestricted); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.UnmanagedIsTokenRestricted(token, out isRestricted); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint GetSniMaxComposedSpnLength() - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.GetSniMaxComposedSpnLength(); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.GetSniMaxComposedSpnLength(); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.GetSniMaxComposedSpnLength(); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Guid pbQInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIGetInfoWrapper(pConn, QType, out pbQInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIGetInfoWrapper(pConn, QType, out pbQInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIGetInfoWrapper(pConn, QType, out pbQInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIGetInfoWrapper(pConn, QType, out pbQInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIGetInfoWrapper(pConn, QType, out pbQInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIGetInfoWrapper(pConn, QType, out pbQInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out ushort portNum) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIGetInfoWrapper(pConn, QType, out portNum); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIGetInfoWrapper(pConn, QType, out portNum); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIGetInfoWrapper(pConn, QType, out portNum); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIGetPeerAddrStrWrapper([In] SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLen) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLen); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLen); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLen); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Provider provNum) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIGetInfoWrapper(pConn, QType, out provNum); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIGetInfoWrapper(pConn, QType, out provNum); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIGetInfoWrapper(pConn, QType, out provNum); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIInitialize([In] IntPtr pmo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIInitialize(pmo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIInitialize(pmo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIInitialize(pmo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIOpenWrapper( - [In] ref SniConsumerInfo pConsumerInfo, - [MarshalAs(UnmanagedType.LPWStr)] string szConnect, - [In] SNIHandle pConn, - out IntPtr ppConn, - [MarshalAs(UnmanagedType.Bool)] bool fSync, - SqlConnectionIPAddressPreference ipPreference, - [In] ref SniDnsCacheInfo pDNSCachedInfo) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIOpenWrapper(ref pConsumerInfo, szConnect, pConn, out ppConn, fSync, ipPreference, ref pDNSCachedInfo); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIOpenWrapper(ref pConsumerInfo, szConnect, pConn, out ppConn, fSync, ipPreference, ref pDNSCachedInfo); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIOpenWrapper(ref pConsumerInfo, szConnect, pConn, out ppConn, fSync, ipPreference, ref pDNSCachedInfo); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType IOType) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIPacketAllocateWrapper(pConn, IOType); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIPacketAllocateWrapper(pConn, IOType); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIPacketAllocateWrapper(pConn, IOType); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIPacketGetDataWrapper([In] IntPtr packet, [In, Out] byte[] readBuffer, uint readBufferLength, out uint dataSize) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - SNINativeManagedWrapperARM64.SNIPacketSetData(pPacket, pbBuf, cbBuf); - break; - case System.Runtime.InteropServices.Architecture.X64: - SNINativeManagedWrapperX64.SNIPacketSetData(pPacket, pbBuf, cbBuf); - break; - case System.Runtime.InteropServices.Architecture.X86: - SNINativeManagedWrapperX86.SNIPacketSetData(pPacket, pbBuf, cbBuf); - break; - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static unsafe uint SNISecGenClientContextWrapper( - [In] SNIHandle pConn, - [In, Out] ReadOnlySpan pIn, - [In, Out] byte[] pOut, - [In] ref uint pcbOut, - [MarshalAsAttribute(UnmanagedType.Bool)] out bool pfDone, - byte* szServerInfo, - uint cbServerInfo, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszUserName, - [MarshalAsAttribute(UnmanagedType.LPWStr)] string pwszPassword) - { - fixed (byte* pInPtr = pIn) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNISecGenClientContextWrapper(pConn, pInPtr, (uint)pIn.Length, pOut, ref pcbOut, out pfDone, szServerInfo, cbServerInfo, pwszUserName, pwszPassword); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNISecGenClientContextWrapper(pConn, pInPtr, (uint)pIn.Length, pOut, ref pcbOut, out pfDone, szServerInfo, cbServerInfo, pwszUserName, pwszPassword); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNISecGenClientContextWrapper(pConn, pInPtr, (uint)pIn.Length, pOut, ref pcbOut, out pfDone, szServerInfo, cbServerInfo, pwszUserName, pwszPassword); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - } - - private static uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIWriteAsyncWrapper(pConn, pPacket); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIWriteAsyncWrapper(pConn, pPacket); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIWriteAsyncWrapper(pConn, pPacket); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - private static uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIWriteSyncOverAsync(pConn, pPacket); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIWriteSyncOverAsync(pConn, pPacket); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIWriteSyncOverAsync(pConn, pPacket); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - #endregion - internal static uint SniGetConnectionId(SNIHandle pConn, ref Guid connId) - { - return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_CONNID, out connId); - } - - internal static uint SniGetProviderNumber(SNIHandle pConn, ref Provider provNum) - { - return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_PROVIDERNUM, out provNum); - } - - internal static uint SniGetConnectionPort(SNIHandle pConn, ref ushort portNum) - { - return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_PEERPORT, out portNum); - } - - internal static uint SniGetConnectionIPString(SNIHandle pConn, ref string connIPStr) - { - UInt32 ret; - uint ERROR_SUCCESS = 0; - uint connIPLen = 0; - - int bufferSize = SniIP6AddrStringBufferLength; - StringBuilder addrBuffer = new StringBuilder(bufferSize); - - ret = SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out connIPLen); - Debug.Assert(ret == ERROR_SUCCESS, "SNIGetPeerAddrStrWrapper fail"); - - connIPStr = addrBuffer.ToString(0, Convert.ToInt32(connIPLen)); - - return ret; - } - - internal static uint SNIInitialize() - { - return SNIInitialize(IntPtr.Zero); - } - - internal static IntPtr SNIServerEnumOpen() - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIServerEnumOpen(); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIServerEnumOpen(); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIServerEnumOpen(); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - internal static int SNIServerEnumRead([In] IntPtr packet, [In, Out] char[] readbuffer, int bufferLength, out bool more) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - return SNINativeManagedWrapperARM64.SNIServerEnumRead(packet, readbuffer, bufferLength, out more); - case System.Runtime.InteropServices.Architecture.X64: - return SNINativeManagedWrapperX64.SNIServerEnumRead(packet, readbuffer, bufferLength, out more); - case System.Runtime.InteropServices.Architecture.X86: - return SNINativeManagedWrapperX86.SNIServerEnumRead(packet, readbuffer, bufferLength, out more); - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static void SNIServerEnumClose([In] IntPtr packet) - { - switch (s_architecture) - { - case System.Runtime.InteropServices.Architecture.Arm64: - SNINativeManagedWrapperARM64.SNIServerEnumClose(packet); - break; - case System.Runtime.InteropServices.Architecture.X64: - SNINativeManagedWrapperX64.SNIServerEnumClose(packet); - break; - case System.Runtime.InteropServices.Architecture.X86: - SNINativeManagedWrapperX86.SNIServerEnumClose(packet); - break; - default: - throw ADP.SNIPlatformNotSupported(s_architecture.ToString()); - } - } - - internal static unsafe uint SNIOpenMarsSession(ConsumerInfo consumerInfo, SNIHandle parent, ref IntPtr pConn, bool fSync, SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo) - { - // initialize consumer info for MARS - SniConsumerInfo native_consumerInfo = new SniConsumerInfo(); - MarshalConsumerInfo(consumerInfo, ref native_consumerInfo); - - SniDnsCacheInfo native_cachedDNSInfo = new SniDnsCacheInfo(); - native_cachedDNSInfo.wszCachedFQDN = cachedDNSInfo?.FQDN; - native_cachedDNSInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4; - native_cachedDNSInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6; - native_cachedDNSInfo.wszCachedTcpPort = cachedDNSInfo?.Port; - - return SNIOpenWrapper(ref native_consumerInfo, "session:", parent, out pConn, fSync, ipPreference, ref native_cachedDNSInfo); - } - - internal static unsafe uint SNIOpenSyncEx( - ConsumerInfo consumerInfo, - string constring, - ref IntPtr pConn, - byte[] spnBuffer, - byte[] instanceName, - bool fOverrideCache, - bool fSync, - int timeout, - bool fParallel, - Int32 transparentNetworkResolutionStateNo, - Int32 totalTimeout, - Boolean isAzureSqlServerEndpoint, - SqlConnectionIPAddressPreference ipPreference, - SQLDNSInfo cachedDNSInfo, - string hostNameInCertificate) - { - fixed (byte* pin_instanceName = &instanceName[0]) - { - SniClientConsumerInfo clientConsumerInfo = new SniClientConsumerInfo(); - - // initialize client ConsumerInfo part first - MarshalConsumerInfo(consumerInfo, ref clientConsumerInfo.ConsumerInfo); - - clientConsumerInfo.wszConnectionString = constring; - clientConsumerInfo.HostNameInCertificate = hostNameInCertificate; - clientConsumerInfo.networkLibrary = Prefix.UNKNOWN_PREFIX; - clientConsumerInfo.szInstanceName = pin_instanceName; - clientConsumerInfo.cchInstanceName = (uint)instanceName.Length; - clientConsumerInfo.fOverrideLastConnectCache = fOverrideCache; - clientConsumerInfo.fSynchronousConnection = fSync; - clientConsumerInfo.timeout = timeout; - clientConsumerInfo.fParallel = fParallel; - - clientConsumerInfo.isAzureSqlServerEndpoint = ADP.IsAzureSqlServerEndpoint(constring); - - switch (transparentNetworkResolutionStateNo) - { - case (0): - clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.DisabledMode; - break; - case (1): - clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.SequentialMode; - break; - case (2): - clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.ParallelMode; - break; - }; - clientConsumerInfo.totalTimeout = totalTimeout; - - clientConsumerInfo.ipAddressPreference = ipPreference; - clientConsumerInfo.DNSCacheInfo.wszCachedFQDN = cachedDNSInfo?.FQDN; - clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4; - clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6; - clientConsumerInfo.DNSCacheInfo.wszCachedTcpPort = cachedDNSInfo?.Port; - - if (spnBuffer != null) - { - fixed (byte* pin_spnBuffer = &spnBuffer[0]) - { - clientConsumerInfo.szSPN = pin_spnBuffer; - clientConsumerInfo.cchSPN = (uint)spnBuffer.Length; - return SNIOpenSyncExWrapper(ref clientConsumerInfo, out pConn); - } - } - else - { - // else leave szSPN null (SQL Auth) - return SNIOpenSyncExWrapper(ref clientConsumerInfo, out pConn); - } - } - } - - [ResourceExposure(ResourceScope.None)] - [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)] - internal static uint SNIAddProvider(SNIHandle pConn, - Provider providerEnum, - AuthProviderInfo authInfo) - { - UInt32 ret; - uint ERROR_SUCCESS = 0; - - Debug.Assert(authInfo.clientCertificateCallback == null, "CTAIP support has been removed"); - - ret = SNIAddProviderWrapper(pConn, providerEnum, ref authInfo); - - if (ret == ERROR_SUCCESS) - { - // added a provider, need to requery for sync over async support - ret = SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_SUPPORTS_SYNC_OVER_ASYNC, out bool _); - Debug.Assert(ret == ERROR_SUCCESS, "SNIGetInfo cannot fail with this QType"); - } - - return ret; - } - - internal static void SNIPacketAllocate(SafeHandle pConn, IoType IOType, ref IntPtr pPacket) - { - pPacket = SNIPacketAllocateWrapper(pConn, IOType); - } - - internal static unsafe uint SNIPacketGetData(IntPtr packet, byte[] readBuffer, ref uint dataSize) - { - return SNIPacketGetDataWrapper(packet, readBuffer, (uint)readBuffer.Length, out dataSize); - } - - internal static unsafe void SNIPacketSetData(SNIPacket packet, byte[] data, int length) - { - fixed (byte* pin_data = &data[0]) - { - SNIPacketSetData(packet, pin_data, (uint)length); - } - } - - //[ResourceExposure(ResourceScope::None)] - // - // Notes on SecureString: Writing out security sensitive information to managed buffer should be avoid as these can be moved - // around by GC. There are two set of information which falls into this category: passwords and new changed password which - // are passed in as SecureString by a user. Writing out clear passwords information is delayed until this layer to ensure that - // the information is written out to buffer which is pinned in this method already. This also ensures that processing a clear password - // is done right before it is written out to SNI_Packet where gets encrypted properly. - // TdsParserStaticMethods.EncryptPassword operation is also done here to minimize the time the clear password is held in memory. Any changes - // to loose encryption algorithm is changed it should be done in both in this method as well as TdsParserStaticMethods.EncryptPassword. - // Up to current release, it is also guaranteed that both password and new change password will fit into a single login packet whose size is fixed to 4096 - // So, there is no splitting logic is needed. - internal static void SNIPacketSetData(SNIPacket packet, - Byte[] data, - Int32 length, - SecureString[] passwords, // pointer to the passwords which need to be written out to SNI Packet - Int32[] passwordOffsets // Offset into data buffer where the password to be written out to - ) - { - Debug.Assert(passwords == null || (passwordOffsets != null && passwords.Length == passwordOffsets.Length), "The number of passwords does not match the number of password offsets"); - - bool mustRelease = false; - bool mustClearBuffer = false; - IntPtr clearPassword = IntPtr.Zero; - - // provides a guaranteed finally block – without this it isn’t guaranteed – non interruptable by fatal exceptions - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - unsafe - { - - fixed (byte* pin_data = &data[0]) - { } - if (passwords != null) - { - // Process SecureString - for (int i = 0; i < passwords.Length; ++i) - { - // SecureString is used - if (passwords[i] != null) - { - // provides a guaranteed finally block – without this it isn’t guaranteed – non interruptable by fatal exceptions - RuntimeHelpers.PrepareConstrainedRegions(); - try - { - // ========================================================================== - // Get the clear text of secure string without converting it to String type - // ========================================================================== - clearPassword = Marshal.SecureStringToCoTaskMemUnicode(passwords[i]); - - // ========================================================================================================================== - // Losely encrypt the clear text - The encryption algorithm should exactly match the TdsParserStaticMethods.EncryptPassword - // ========================================================================================================================== - - unsafe - { - - char* pwChar = (char*)clearPassword.ToPointer(); - byte* pByte = (byte*)(clearPassword.ToPointer()); - - - - - int s; - byte bLo; - byte bHi; - int passwordsLength = passwords[i].Length; - for (int j = 0; j < passwordsLength; ++j) - { - s = (int)*pwChar; - bLo = (byte)(s & 0xff); - bHi = (byte)((s >> 8) & 0xff); - *(pByte++) = (Byte)((((bLo & 0x0f) << 4) | (bLo >> 4)) ^ 0xa5); - *(pByte++) = (Byte)((((bHi & 0x0f) << 4) | (bHi >> 4)) ^ 0xa5); - ++pwChar; - } - - // =========================================================== - // Write out the losely encrypted passwords to data buffer - // =========================================================== - mustClearBuffer = true; - Marshal.Copy(clearPassword, data, passwordOffsets[i], passwordsLength * 2); - } - } - finally - { - // Make sure that we clear the security sensitive information - if (clearPassword != IntPtr.Zero) - { - Marshal.ZeroFreeCoTaskMemUnicode(clearPassword); - } - } - } - } - } - - packet.DangerousAddRef(ref mustRelease); - Debug.Assert(mustRelease, "AddRef Failed!"); - - fixed (byte* pin_data = &data[0]) - { - SNIPacketSetData(packet, pin_data, (uint)length); - } - } - } - finally - { - if (mustRelease) - { - packet.DangerousRelease(); - } - - // Make sure that we clear the security sensitive information - // data->Initialize() is not safe to call under CER - if (mustClearBuffer) - { - for (int i = 0; i < data.Length; ++i) - { - data[i] = 0; - } - } - } - } - - internal static unsafe uint SNISecGenClientContext(SNIHandle pConnectionObject, ReadOnlySpan inBuff, byte[] OutBuff, ref uint sendLength, byte[] serverUserName) - { - fixed (byte* pin_serverUserName = &serverUserName[0]) - { - return SNISecGenClientContextWrapper( - pConnectionObject, - inBuff, - OutBuff, - ref sendLength, - out bool _, - pin_serverUserName, - (uint)serverUserName.Length, - null, - null); - } - } - - internal static uint SNIWritePacket(SNIHandle pConn, SNIPacket packet, bool sync) - { - if (sync) - { - return SNIWriteSyncOverAsync(pConn, packet); - } - else - { - return SNIWriteAsyncWrapper(pConn, packet); - } - } - - private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref SniConsumerInfo native_consumerInfo) - { - native_consumerInfo.DefaultUserDataLength = consumerInfo.defaultBufferSize; - native_consumerInfo.fnReadComp = consumerInfo.readDelegate != null - ? Marshal.GetFunctionPointerForDelegate(consumerInfo.readDelegate) - : IntPtr.Zero; - native_consumerInfo.fnWriteComp = consumerInfo.writeDelegate != null - ? Marshal.GetFunctionPointerForDelegate(consumerInfo.writeDelegate) - : IntPtr.Zero; - native_consumerInfo.ConsumerKey = consumerInfo.key; - } - } -} - -namespace Microsoft.Data -{ - internal static class Win32NativeMethods - { - internal static bool IsTokenRestrictedWrapper(IntPtr token) - { - bool isRestricted; - uint result = SNINativeMethodWrapper.UnmanagedIsTokenRestricted(token, out isRestricted); - - if (result != 0) - { - Marshal.ThrowExceptionForHR(unchecked((int)result)); - } - - return isRestricted; - } - } -} diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBAPI.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBAPI.cs index 43e73ab69b..2d6192f6bf 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBAPI.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBAPI.cs @@ -81,7 +81,7 @@ static IntPtr UserInstanceDLLHandle Monitor.Enter(s_dllLock, ref lockTaken); if (s_userInstanceDLLHandle == IntPtr.Zero) { - SNINativeMethodWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle); + SniNativeWrapper.SNIQueryInfo(QueryType.SNI_QUERY_LOCALDB_HMODULE, ref s_userInstanceDLLHandle); if (s_userInstanceDLLHandle != IntPtr.Zero) { SqlClientEventSource.Log.TryTraceEvent(" LocalDB - handle obtained"); @@ -89,7 +89,7 @@ static IntPtr UserInstanceDLLHandle else { SniError sniError = new SniError(); - SNINativeMethodWrapper.SNIGetLastError(out sniError); + SniNativeWrapper.SNIGetLastError(out sniError); throw CreateLocalDBException(errorMessage: StringsHelper.GetString("LocalDB_FailedGetDLLHandle"), sniError: sniError.sniError); } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index b21d458f41..e845f1bdb9 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -436,14 +436,14 @@ internal void Connect(ServerInfo serverInfo, { // Native SNI requires the Unicode encoding and any other encoding like UTF8 breaks the code. byte[] srvSPN = Encoding.Unicode.GetBytes(serverInfo.ServerSPN); - Trace.Assert(srvSPN.Length <= SNINativeMethodWrapper.SniMaxComposedSpnLength, "The provided SPN length exceeded the buffer size."); + Trace.Assert(srvSPN.Length <= SniNativeWrapper.SniMaxComposedSpnLength, "The provided SPN length exceeded the buffer size."); _sniSpnBuffer = srvSPN; SqlClientEventSource.Log.TryTraceEvent(" Server SPN `{0}` from the connection string is used.", serverInfo.ServerSPN); } else { // now allocate proper length of buffer - _sniSpnBuffer = new byte[SNINativeMethodWrapper.SniMaxComposedSpnLength]; + _sniSpnBuffer = new byte[SniNativeWrapper.SniMaxComposedSpnLength]; } SqlClientEventSource.Log.TryTraceEvent(" SSPI or Active Directory Authentication Library for SQL Server based integrated authentication"); } @@ -576,7 +576,7 @@ internal void Connect(ServerInfo serverInfo, _connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.InitializeConnection); _connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.SendPreLoginHandshake); - uint result = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); + uint result = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId"); // for DNS Caching phase 1 @@ -648,7 +648,7 @@ internal void Connect(ServerInfo serverInfo, ThrowExceptionAndWarning(_physicalStateObj); } - uint retCode = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); + uint retCode = SniNativeWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId"); SqlClientEventSource.Log.TryTraceEvent(" Sending prelogin handshake"); @@ -700,7 +700,7 @@ internal void RemoveEncryption() uint error = 0; // Remove SSL (Encryption) SNI provider since we only wanted to encrypt login. - error = SNINativeMethodWrapper.SNIRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV); + error = SniNativeWrapper.SNIRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV); if (error != TdsEnums.SNI_SUCCESS) { _physicalStateObj.AddError(ProcessSNIError(_physicalStateObj)); @@ -727,7 +727,7 @@ internal void EnableMars() uint info = 0; // Add SMUX (MARS) SNI provider. - error = SNINativeMethodWrapper.SNIAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info); + error = SniNativeWrapper.SNIAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info); if (error != TdsEnums.SNI_SUCCESS) { @@ -748,12 +748,12 @@ internal void EnableMars() { _pMarsPhysicalConObj.IncrementPendingCallbacks(); - error = SNINativeMethodWrapper.SNIReadAsync(_pMarsPhysicalConObj.Handle, ref temp); + error = SniNativeWrapper.SNIReadAsync(_pMarsPhysicalConObj.Handle, ref temp); if (temp != IntPtr.Zero) { // Be sure to release packet, otherwise it will be leaked by native. - SNINativeMethodWrapper.SNIPacketRelease(temp); + SniNativeWrapper.SNIPacketRelease(temp); } } Debug.Assert(IntPtr.Zero == temp, "unexpected syncReadPacket without corresponding SNIPacketRelease"); @@ -1026,7 +1026,7 @@ private void EnableSsl(uint info, SqlConnectionEncryptOption encrypt, bool integ Debug.Assert((_encryptionOption & EncryptionOptions.CLIENT_CERT) == 0, "Client certificate authentication support has been removed"); - error = SNINativeMethodWrapper.SNIAddProvider(_physicalStateObj.Handle, Provider.SSL_PROV, authInfo); + error = SniNativeWrapper.SNIAddProvider(_physicalStateObj.Handle, Provider.SSL_PROV, authInfo); if (error != TdsEnums.SNI_SUCCESS) { @@ -1038,7 +1038,7 @@ private void EnableSsl(uint info, SqlConnectionEncryptOption encrypt, bool integ // wait for SSL handshake to complete, so that the SSL context is fully negotiated before we try to use its // Channel Bindings as part of the Windows Authentication context build (SSL handshake must complete // before calling SNISecGenClientContext). - error = SNINativeMethodWrapper.SNIWaitForSSLHandshakeToComplete(_physicalStateObj.Handle, _physicalStateObj.GetTimeoutRemaining(), out uint protocolVersion); + error = SniNativeWrapper.SNIWaitForSSLHandshakeToComplete(_physicalStateObj.Handle, _physicalStateObj.GetTimeoutRemaining(), out uint protocolVersion); if (error != TdsEnums.SNI_SUCCESS) { @@ -1592,7 +1592,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj) Debug.Assert(SniContext.Undefined != stateObj.DebugOnlyCopyOfSniContext || ((_fMARS) && ((_state == TdsParserState.Closed) || (_state == TdsParserState.Broken))), "SniContext must not be None"); #endif SniError sniError = new SniError(); - SNINativeMethodWrapper.SNIGetLastError(out sniError); + SniNativeWrapper.SNIGetLastError(out sniError); if (sniError.sniError != 0) { @@ -2915,7 +2915,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb // Update SNI ConsumerInfo value to be resulting packet size uint unsignedPacketSize = (uint)packetSize; - uint bufferSizeResult = SNINativeMethodWrapper.SNISetInfo(_physicalStateObj.Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize); + uint bufferSizeResult = SniNativeWrapper.SNISetInfo(_physicalStateObj.Handle, QueryType.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize); Debug.Assert(bufferSizeResult == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SNISetInfo"); } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.netfx.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.netfx.cs index 98101a2bc4..32d9b091ce 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.netfx.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.netfx.cs @@ -141,7 +141,7 @@ internal void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey) if (string.IsNullOrEmpty(userProtocol)) { - result = SNINativeMethodWrapper.SniGetProviderNumber(_physicalStateObj.Handle, ref providerNumber); + result = SniNativeWrapper.SniGetProviderNumber(_physicalStateObj.Handle, ref providerNumber); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetProviderNumber"); isTcpProtocol = (providerNumber == Provider.TCP_PROV); } @@ -153,11 +153,11 @@ internal void AssignPendingDNSInfo(string userProtocol, string DNSCacheKey) // serverInfo.UserProtocol could be empty if (isTcpProtocol) { - result = SNINativeMethodWrapper.SniGetConnectionPort(_physicalStateObj.Handle, ref portFromSNI); + result = SniNativeWrapper.SniGetConnectionPort(_physicalStateObj.Handle, ref portFromSNI); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionPort"); - result = SNINativeMethodWrapper.SniGetConnectionIPString(_physicalStateObj.Handle, ref IPStringFromSNI); + result = SniNativeWrapper.SniGetConnectionIPString(_physicalStateObj.Handle, ref IPStringFromSNI); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionIPString"); _connHandler.pendingSQLDNSObject = new SQLDNSInfo(DNSCacheKey, null, null, portFromSNI.ToString()); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs index 0544d38766..0ce58d120a 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.netfx.cs @@ -285,20 +285,20 @@ internal PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint error) { SNIHandle handle = Handle ?? throw ADP.ClosedConnectionError(); PacketHandle readPacket = default; - error = SNINativeMethodWrapper.SNIReadSyncOverAsync(handle, ref readPacket, timeoutRemaining); + error = SniNativeWrapper.SNIReadSyncOverAsync(handle, ref readPacket, timeoutRemaining); return readPacket; } internal PacketHandle ReadAsync(SessionHandle handle, out uint error) { PacketHandle readPacket = default; - error = SNINativeMethodWrapper.SNIReadAsync(handle.NativeHandle, ref readPacket); + error = SniNativeWrapper.SNIReadAsync(handle.NativeHandle, ref readPacket); return readPacket; } - internal uint CheckConnection() => SNINativeMethodWrapper.SNICheckConnection(Handle); + internal uint CheckConnection() => SniNativeWrapper.SNICheckConnection(Handle); - internal void ReleasePacket(PacketHandle syncReadPacket) => SNINativeMethodWrapper.SNIPacketRelease(syncReadPacket); + internal void ReleasePacket(PacketHandle syncReadPacket) => SniNativeWrapper.SNIPacketRelease(syncReadPacket); [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal int DecrementPendingCallbacks(bool release) @@ -416,7 +416,7 @@ internal bool ValidateSNIConnection() SNIHandle handle = Handle; if (handle != null) { - error = SNINativeMethodWrapper.SNICheckConnection(handle); + error = SniNativeWrapper.SNICheckConnection(handle); } } finally @@ -543,7 +543,7 @@ public void ProcessSniPacket(PacketHandle packet, uint error) { uint dataSize = 0; - uint getDataError = SNINativeMethodWrapper.SNIPacketGetData(packet, _inBuff, ref dataSize); + uint getDataError = SniNativeWrapper.SNIPacketGetData(packet, _inBuff, ref dataSize); if (getDataError == TdsEnums.SNI_SUCCESS) { @@ -1169,7 +1169,7 @@ private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out uint sniErro } finally { - sniError = SNINativeMethodWrapper.SNIWritePacket(handle, packet, sync); + sniError = SniNativeWrapper.SNIWritePacket(handle, packet, sync); } if (sniError == TdsEnums.SNI_SUCCESS_IO_PENDING) @@ -1281,7 +1281,7 @@ internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = fa SNIPacket attnPacket = new SNIPacket(Handle); _sniAsyncAttnPacket = attnPacket; - SNINativeMethodWrapper.SNIPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null); + SniNativeWrapper.SNIPacketSetData(attnPacket, SQL.AttentionHeader, TdsEnums.HEADER_LEN, null, null); RuntimeHelpers.PrepareConstrainedRegions(); try @@ -1345,7 +1345,7 @@ private Task WriteSni(bool canAccumulate) { // Prepare packet, and write to packet. SNIPacket packet = GetResetWritePacket(); - SNINativeMethodWrapper.SNIPacketSetData(packet, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer); + SniNativeWrapper.SNIPacketSetData(packet, _outBuff, _outBytesUsed, _securePasswords, _securePasswordOffsetsInBuffer); Debug.Assert(Parser.Connection._parserLock.ThreadMayHaveLock(), "Thread is writing without taking the connection lock"); Task task = SNIWritePacket(Handle, packet, out _, canAccumulate, callerHasConnectionLock: true); @@ -1400,7 +1400,7 @@ internal SNIPacket GetResetWritePacket() { if (_sniPacket != null) { - SNINativeMethodWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI); + SniNativeWrapper.SNIPacketReset(Handle, IoType.WRITE, _sniPacket, ConsumerNumber.SNI_Consumer_SNI); } else { diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/ISniNativeMethods.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/ISniNativeMethods.cs new file mode 100644 index 0000000000..9be1c90087 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/ISniNativeMethods.cs @@ -0,0 +1,101 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Data.SqlClient; + +namespace Interop.Windows.Sni +{ + internal interface ISniNativeMethods + { + uint SniAddProvider(SNIHandle pConn, Provider provider, ref AuthProviderInfo pInfo); + + uint SniAddProvider(SNIHandle pConn, Provider provider, ref uint pInfo); + + uint SniCheckConnection(SNIHandle pConn); + + uint SniClose(IntPtr pConn); + + uint SniGetMaxComposedSpnLength(); + + uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out bool pbQueryInfo); + + uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo); + + uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Provider provider); + + uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out ushort portNumber); + + void SniGetLastError(out SniError pLastError); + + uint SniGetPeerAddrStrWrapper(SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLength); + + uint SniInitialize(IntPtr pmo); + + uint SniIsTokenRestricted(IntPtr token, out bool isRestricted); + + uint SniOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn); + + uint SniOpenWrapper( + ref SniConsumerInfo pConsumerInfo, + string connect, + SNIHandle pConn, + out IntPtr ppConn, + bool fSync, + SqlConnectionIPAddressPreference ipPreference, + ref SniDnsCacheInfo pDnsCacheInfo); + + IntPtr SniPacketAllocateWrapper(SafeHandle pConn, IoType ioType); + + uint SniPacketGetDataWrapper(IntPtr packet, byte[] readBuffer, uint readBufferLength, out uint dataSize); + + void SniPacketReset(SNIHandle pConn, IoType ioType, SNIPacket pPacket, ConsumerNumber consumer); + + void SniPacketRelease(IntPtr pPacket); + + unsafe void SniPacketSetData(SNIPacket pPacket, byte* pbBuffer, uint cbBuffer); + + uint SniQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo); + + uint SniQueryInfo(QueryType queryType, ref uint pbQueryInfo); + + uint SniReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket); + + uint SniReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); + + uint SniRemoveProvider(SNIHandle pConn, Provider provider); + + unsafe uint SniSecGenClientContextWrapper( + SNIHandle pConn, + byte* pIn, + uint cbIn, + byte[] pOut, + ref uint pcbOut, + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + string pwszUserName, + string pwszPassword); + + uint SniSecInitPackage(ref uint pcbMaxToken); + + void SniServerEnumClose(IntPtr packet); + + IntPtr SniServerEnumOpen(); + + int SniServerEnumRead(IntPtr packet, char[] readBuffer, int bufferLength, out bool more); + + uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInfo); + + uint SniTerminate(); + + uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion); + + uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket); + + uint SniWriteSyncOverAsync(SNIHandle pConn, SNIPacket pPacket); + } +} diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethods.netcore.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethods.netcore.cs new file mode 100644 index 0000000000..c4dfdb87e9 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethods.netcore.cs @@ -0,0 +1,319 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if NET + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Data.SqlClient; + +namespace Interop.Windows.Sni +{ + internal sealed class SniNativeMethods : ISniNativeMethods + { + private const string DllName = "Microsoft.Data.SqlClient.SNI.dll"; + + #region Interface Implementation + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref AuthProviderInfo pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref uint pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniCheckConnection(SNIHandle pConn) => + SNICheckConnectionWrapper(pConn); + + public uint SniClose(IntPtr pConn) => + SNICloseWrapper(pConn); + + public uint SniGetMaxComposedSpnLength() => + GetSniMaxComposedSpnLength(); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out bool pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Provider provider) => + SNIGetInfoWrapper(pConn, queryType, out provider); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out ushort portNumber) => + SNIGetInfoWrapper(pConn, queryType, out portNumber); + + public void SniGetLastError(out SniError pLastError) => + SNIGetLastError(out pLastError); + + public uint SniGetPeerAddrStrWrapper( + SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength) => + SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLength); + + public uint SniInitialize(IntPtr pmo) => + SNIInitialize(pmo); + + public uint SniIsTokenRestricted(IntPtr token, out bool isRestricted) => + UnmanagedIsTokenRestricted(token, out isRestricted); + + public uint SniOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) => + SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); + + public uint SniOpenWrapper( + ref SniConsumerInfo pConsumerInfo, + string connect, + SNIHandle pConn, + out IntPtr ppConn, + bool fSync, + SqlConnectionIPAddressPreference ipPreference, + ref SniDnsCacheInfo pDnsCacheInfo) => + SNIOpenWrapper(ref pConsumerInfo, connect, pConn, out ppConn, fSync, ipPreference, ref pDnsCacheInfo); + + public IntPtr SniPacketAllocateWrapper(SafeHandle pConn, IoType ioType) => + SNIPacketAllocateWrapper(pConn, ioType); + + public uint SniPacketGetDataWrapper( + IntPtr packet, + byte[] readBuffer, + uint readBufferLength, + out uint dataSize) => + SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); + + public void SniPacketReset(SNIHandle pConn, IoType ioType, SNIPacket pPacket, ConsumerNumber consumer) => + SNIPacketResetWrapper(pConn, ioType, pPacket, consumer); + + public void SniPacketRelease(IntPtr pPacket) => + SNIPacketRelease(pPacket); + + public unsafe void SniPacketSetData(SNIPacket pPacket, byte* pbBuffer, uint cbBuffer) => + SNIPacketSetData(pPacket, pbBuffer, cbBuffer); + + public uint SniQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniQueryInfo(QueryType queryType, ref uint pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) => + SNIReadAsyncWrapper(pConn, ref ppNewPacket); + + public uint SniReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) => + SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); + + public uint SniRemoveProvider(SNIHandle pConn, Provider provider) => + SNIRemoveProviderWrapper(pConn, provider); + + public unsafe uint SniSecGenClientContextWrapper( + SNIHandle pConn, + byte* pIn, + uint cbIn, + byte[] pOut, + ref uint pcbOut, + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + string pwszUserName, + string pwszPassword) => + SNISecGenClientContextWrapper( + pConn, + pIn, + cbIn, + pOut, + ref pcbOut, + out pfDone, + szServerInfo, + cbServerInfo, + pwszUserName, + pwszPassword); + + public uint SniSecInitPackage(ref uint pcbMaxToken) => + SNISecInitPackage(ref pcbMaxToken); + + public void SniServerEnumClose(IntPtr packet) => + SNIServerEnumCloseWrapper(packet); + + public IntPtr SniServerEnumOpen() => + SNIServerEnumOpenWrapper(); + + public int SniServerEnumRead(IntPtr packet, char[] readBuffer, int bufferLength, out bool more) => + SNIServerEnumReadWrapper(packet, readBuffer, bufferLength, out more); + + public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInfo) => + SNISetInfoWrapper(pConn, queryType, ref pbQueryInfo); + + public uint SniTerminate() => + SNITerminate(); + + public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) => + SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion); + + public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteAsyncWrapper(pConn, pPacket); + + public uint SniWriteSyncOverAsync(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteSyncOverAsync(pConn, pPacket); + + #endregion + + #region DllImports + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint GetSniMaxComposedSpnLength(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper( + SNIHandle pConn, + Provider provider, + [In] ref AuthProviderInfo pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider provider, [In] ref uint pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICheckConnectionWrapper([In] SNIHandle pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICloseWrapper(IntPtr pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper( + [In] SNIHandle pConn, + QueryType queryType, + [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out ushort portNum); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIGetLastError(out SniError pErrorStruct); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern uint SNIGetPeerAddrStrWrapper( + [In] SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIInitialize([In] IntPtr pmo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenSyncExWrapper( + ref SniClientConsumerInfo pClientConsumerInfo, + out IntPtr ppConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenWrapper( + [In] ref SniConsumerInfo pConsumerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string szConnect, + [In] SNIHandle pConn, + out IntPtr ppConn, + [MarshalAs(UnmanagedType.Bool)] bool fSync, + SqlConnectionIPAddressPreference ipPreference, + [In] ref SniDnsCacheInfo pDnsCacheInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType ioType); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIPacketGetDataWrapper( + [In] IntPtr packet, + [In, Out] byte[] readBuffer, + uint readBufferLength, + out uint dataSize); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketRelease(IntPtr pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketResetWrapper( + [In] SNIHandle pConn, + IoType ioType, + SNIPacket pPacket, + ConsumerNumber consumer); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadAsyncWrapper(SNIHandle pConn, ref IntPtr ppNewPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIRemoveProviderWrapper(SNIHandle pConn, Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe uint SNISecGenClientContextWrapper( + [In] SNIHandle pConn, + [In, Out] byte* pIn, + uint cbIn, + [In, Out] byte[] pOut, + [In] ref uint pcbOut, + [MarshalAs(UnmanagedType.Bool)] out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, + [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISecInitPackage(ref uint pcbMaxToken); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIServerEnumCloseWrapper([In] IntPtr packet); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIServerEnumOpenWrapper(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern int SNIServerEnumReadWrapper( + [In] IntPtr packet, + [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, + [In] int bufferLength, + [MarshalAs(UnmanagedType.Bool)] out bool more); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISetInfoWrapper(SNIHandle pConn, QueryType queryType, [In] ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNITerminate(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper( + [In] SNIHandle pConn, + int dwMilliseconds, + out uint pProtocolVersion); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint UnmanagedIsTokenRestricted( + [In] IntPtr token, + [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); + + #endregion + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsArm64.netfx.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsArm64.netfx.cs new file mode 100644 index 0000000000..e3e087a7ca --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsArm64.netfx.cs @@ -0,0 +1,319 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if NETFRAMEWORK + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Data.SqlClient; + +namespace Interop.Windows.Sni +{ + internal sealed class SniNativeMethodsArm64 : ISniNativeMethods + { + private const string DllName = "Microsoft.Data.SqlClient.SNI.arm64.dll"; + + #region Interface Implementation + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref AuthProviderInfo pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref uint pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniCheckConnection(SNIHandle pConn) => + SNICheckConnectionWrapper(pConn); + + public uint SniClose(IntPtr pConn) => + SNICloseWrapper(pConn); + + public uint SniGetMaxComposedSpnLength() => + GetSniMaxComposedSpnLength(); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out bool pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Provider provider) => + SNIGetInfoWrapper(pConn, queryType, out provider); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out ushort portNumber) => + SNIGetInfoWrapper(pConn, queryType, out portNumber); + + public void SniGetLastError(out SniError pLastError) => + SNIGetLastError(out pLastError); + + public uint SniGetPeerAddrStrWrapper( + SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength) => + SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLength); + + public uint SniInitialize(IntPtr pmo) => + SNIInitialize(pmo); + + public uint SniIsTokenRestricted(IntPtr token, out bool isRestricted) => + UnmanagedIsTokenRestricted(token, out isRestricted); + + public uint SniOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) => + SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); + + public uint SniOpenWrapper( + ref SniConsumerInfo pConsumerInfo, + string connect, + SNIHandle pConn, + out IntPtr ppConn, + bool fSync, + SqlConnectionIPAddressPreference ipPreference, + ref SniDnsCacheInfo pDnsCacheInfo) => + SNIOpenWrapper(ref pConsumerInfo, connect, pConn, out ppConn, fSync, ipPreference, ref pDnsCacheInfo); + + public IntPtr SniPacketAllocateWrapper(SafeHandle pConn, IoType ioType) => + SNIPacketAllocateWrapper(pConn, ioType); + + public uint SniPacketGetDataWrapper( + IntPtr packet, + byte[] readBuffer, + uint readBufferLength, + out uint dataSize) => + SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); + + public void SniPacketReset(SNIHandle pConn, IoType ioType, SNIPacket pPacket, ConsumerNumber consumer) => + SNIPacketResetWrapper(pConn, ioType, pPacket, consumer); + + public void SniPacketRelease(IntPtr pPacket) => + SNIPacketRelease(pPacket); + + public unsafe void SniPacketSetData(SNIPacket pPacket, byte* pbBuffer, uint cbBuffer) => + SNIPacketSetData(pPacket, pbBuffer, cbBuffer); + + public uint SniQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniQueryInfo(QueryType queryType, ref uint pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) => + SNIReadAsyncWrapper(pConn, ref ppNewPacket); + + public uint SniReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) => + SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); + + public uint SniRemoveProvider(SNIHandle pConn, Provider provider) => + SNIRemoveProviderWrapper(pConn, provider); + + public unsafe uint SniSecGenClientContextWrapper( + SNIHandle pConn, + byte* pIn, + uint cbIn, + byte[] pOut, + ref uint pcbOut, + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + string pwszUserName, + string pwszPassword) => + SNISecGenClientContextWrapper( + pConn, + pIn, + cbIn, + pOut, + ref pcbOut, + out pfDone, + szServerInfo, + cbServerInfo, + pwszUserName, + pwszPassword); + + public uint SniSecInitPackage(ref uint pcbMaxToken) => + SNISecInitPackage(ref pcbMaxToken); + + public void SniServerEnumClose(IntPtr packet) => + SNIServerEnumCloseWrapper(packet); + + public IntPtr SniServerEnumOpen() => + SNIServerEnumOpenWrapper(); + + public int SniServerEnumRead(IntPtr packet, char[] readBuffer, int bufferLength, out bool more) => + SNIServerEnumReadWrapper(packet, readBuffer, bufferLength, out more); + + public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInfo) => + SNISetInfoWrapper(pConn, queryType, ref pbQueryInfo); + + public uint SniTerminate() => + SNITerminate(); + + public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) => + SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion); + + public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteAsyncWrapper(pConn, pPacket); + + public uint SniWriteSyncOverAsync(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteSyncOverAsync(pConn, pPacket); + + #endregion + + #region DllImports + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint GetSniMaxComposedSpnLength(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper( + SNIHandle pConn, + Provider provider, + [In] ref AuthProviderInfo pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider provider, [In] ref uint pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICheckConnectionWrapper([In] SNIHandle pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICloseWrapper(IntPtr pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper( + [In] SNIHandle pConn, + QueryType queryType, + [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out ushort portNum); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIGetLastError(out SniError pErrorStruct); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern uint SNIGetPeerAddrStrWrapper( + [In] SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIInitialize([In] IntPtr pmo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenSyncExWrapper( + ref SniClientConsumerInfo pClientConsumerInfo, + out IntPtr ppConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenWrapper( + [In] ref SniConsumerInfo pConsumerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string szConnect, + [In] SNIHandle pConn, + out IntPtr ppConn, + [MarshalAs(UnmanagedType.Bool)] bool fSync, + SqlConnectionIPAddressPreference ipPreference, + [In] ref SniDnsCacheInfo pDnsCacheInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType ioType); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIPacketGetDataWrapper( + [In] IntPtr packet, + [In, Out] byte[] readBuffer, + uint readBufferLength, + out uint dataSize); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketRelease(IntPtr pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketResetWrapper( + [In] SNIHandle pConn, + IoType ioType, + SNIPacket pPacket, + ConsumerNumber consumer); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadAsyncWrapper(SNIHandle pConn, ref IntPtr ppNewPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIRemoveProviderWrapper(SNIHandle pConn, Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe uint SNISecGenClientContextWrapper( + [In] SNIHandle pConn, + [In, Out] byte* pIn, + uint cbIn, + [In, Out] byte[] pOut, + [In] ref uint pcbOut, + [MarshalAs(UnmanagedType.Bool)] out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, + [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISecInitPackage(ref uint pcbMaxToken); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIServerEnumCloseWrapper([In] IntPtr packet); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIServerEnumOpenWrapper(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern int SNIServerEnumReadWrapper( + [In] IntPtr packet, + [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, + [In] int bufferLength, + [MarshalAs(UnmanagedType.Bool)] out bool more); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISetInfoWrapper(SNIHandle pConn, QueryType queryType, [In] ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNITerminate(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper( + [In] SNIHandle pConn, + int dwMilliseconds, + out uint pProtocolVersion); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint UnmanagedIsTokenRestricted( + [In] IntPtr token, + [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); + + #endregion + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsNotSupported.netfx.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsNotSupported.netfx.cs new file mode 100644 index 0000000000..4b2622e57f --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsNotSupported.netfx.cs @@ -0,0 +1,156 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if NETFRAMEWORK + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Data.Common; +using Microsoft.Data.SqlClient; + +namespace Interop.Windows.Sni +{ + internal sealed class SniNativeMethodsNotSupported : ISniNativeMethods + { + private readonly string _architecture; + + public SniNativeMethodsNotSupported(Architecture architecture) + { + _architecture = architecture.ToString(); + } + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref AuthProviderInfo pInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref uint pInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniCheckConnection(SNIHandle pConn) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniClose(IntPtr pConn) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniGetMaxComposedSpnLength() => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out bool pbQueryInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Provider provider) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out ushort portNumber) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public void SniGetLastError(out SniError pLastError) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniGetPeerAddrStrWrapper( + SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniInitialize(IntPtr pmo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniIsTokenRestricted(IntPtr token, out bool isRestricted) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniOpenWrapper( + ref SniConsumerInfo pConsumerInfo, + string connect, + SNIHandle pConn, + out IntPtr ppConn, + bool fSync, + SqlConnectionIPAddressPreference ipPreference, + ref SniDnsCacheInfo pDnsCacheInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public IntPtr SniPacketAllocateWrapper(SafeHandle pConn, IoType ioType) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniPacketGetDataWrapper( + IntPtr packet, + byte[] readBuffer, + uint readBufferLength, + out uint dataSize) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public void SniPacketReset(SNIHandle pConn, IoType ioType, SNIPacket pPacket, ConsumerNumber consumer) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public void SniPacketRelease(IntPtr pPacket) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public unsafe void SniPacketSetData(SNIPacket pPacket, byte* pbBuffer, uint cbBuffer) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniQueryInfo(QueryType queryType, ref uint pbQueryInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniRemoveProvider(SNIHandle pConn, Provider provider) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public unsafe uint SniSecGenClientContextWrapper( + SNIHandle pConn, + byte* pIn, + uint cbIn, + byte[] pOut, + ref uint pcbOut, + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + string pwszUserName, + string pwszPassword) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniSecInitPackage(ref uint pcbMaxToken) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public void SniServerEnumClose(IntPtr packet) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public IntPtr SniServerEnumOpen() => + throw ADP.SNIPlatformNotSupported(_architecture); + + public int SniServerEnumRead(IntPtr packet, char[] readBuffer, int bufferLength, out bool more) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInfo) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniTerminate() => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) => + throw ADP.SNIPlatformNotSupported(_architecture); + + public uint SniWriteSyncOverAsync(SNIHandle pConn, SNIPacket pPacket) => + throw ADP.SNIPlatformNotSupported(_architecture); + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX64.netfx.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX64.netfx.cs new file mode 100644 index 0000000000..1d80ee30d0 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX64.netfx.cs @@ -0,0 +1,319 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if NETFRAMEWORK + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Data.SqlClient; + +namespace Interop.Windows.Sni +{ + internal sealed class SniNativeMethodsX64 : ISniNativeMethods + { + private const string DllName = "Microsoft.Data.SqlClient.SNI.x64.dll"; + + #region Interface Implementation + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref AuthProviderInfo pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref uint pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniCheckConnection(SNIHandle pConn) => + SNICheckConnectionWrapper(pConn); + + public uint SniClose(IntPtr pConn) => + SNICloseWrapper(pConn); + + public uint SniGetMaxComposedSpnLength() => + GetSniMaxComposedSpnLength(); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out bool pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Provider provider) => + SNIGetInfoWrapper(pConn, queryType, out provider); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out ushort portNumber) => + SNIGetInfoWrapper(pConn, queryType, out portNumber); + + public void SniGetLastError(out SniError pLastError) => + SNIGetLastError(out pLastError); + + public uint SniGetPeerAddrStrWrapper( + SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength) => + SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLength); + + public uint SniInitialize(IntPtr pmo) => + SNIInitialize(pmo); + + public uint SniIsTokenRestricted(IntPtr token, out bool isRestricted) => + UnmanagedIsTokenRestricted(token, out isRestricted); + + public uint SniOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) => + SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); + + public uint SniOpenWrapper( + ref SniConsumerInfo pConsumerInfo, + string connect, + SNIHandle pConn, + out IntPtr ppConn, + bool fSync, + SqlConnectionIPAddressPreference ipPreference, + ref SniDnsCacheInfo pDnsCacheInfo) => + SNIOpenWrapper(ref pConsumerInfo, connect, pConn, out ppConn, fSync, ipPreference, ref pDnsCacheInfo); + + public IntPtr SniPacketAllocateWrapper(SafeHandle pConn, IoType ioType) => + SNIPacketAllocateWrapper(pConn, ioType); + + public uint SniPacketGetDataWrapper( + IntPtr packet, + byte[] readBuffer, + uint readBufferLength, + out uint dataSize) => + SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); + + public void SniPacketReset(SNIHandle pConn, IoType ioType, SNIPacket pPacket, ConsumerNumber consumer) => + SNIPacketResetWrapper(pConn, ioType, pPacket, consumer); + + public void SniPacketRelease(IntPtr pPacket) => + SNIPacketRelease(pPacket); + + public unsafe void SniPacketSetData(SNIPacket pPacket, byte* pbBuffer, uint cbBuffer) => + SNIPacketSetData(pPacket, pbBuffer, cbBuffer); + + public uint SniQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniQueryInfo(QueryType queryType, ref uint pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) => + SNIReadAsyncWrapper(pConn, ref ppNewPacket); + + public uint SniReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) => + SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); + + public uint SniRemoveProvider(SNIHandle pConn, Provider provider) => + SNIRemoveProviderWrapper(pConn, provider); + + public unsafe uint SniSecGenClientContextWrapper( + SNIHandle pConn, + byte* pIn, + uint cbIn, + byte[] pOut, + ref uint pcbOut, + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + string pwszUserName, + string pwszPassword) => + SNISecGenClientContextWrapper( + pConn, + pIn, + cbIn, + pOut, + ref pcbOut, + out pfDone, + szServerInfo, + cbServerInfo, + pwszUserName, + pwszPassword); + + public uint SniSecInitPackage(ref uint pcbMaxToken) => + SNISecInitPackage(ref pcbMaxToken); + + public void SniServerEnumClose(IntPtr packet) => + SNIServerEnumCloseWrapper(packet); + + public IntPtr SniServerEnumOpen() => + SNIServerEnumOpenWrapper(); + + public int SniServerEnumRead(IntPtr packet, char[] readBuffer, int bufferLength, out bool more) => + SNIServerEnumReadWrapper(packet, readBuffer, bufferLength, out more); + + public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInfo) => + SNISetInfoWrapper(pConn, queryType, ref pbQueryInfo); + + public uint SniTerminate() => + SNITerminate(); + + public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) => + SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion); + + public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteAsyncWrapper(pConn, pPacket); + + public uint SniWriteSyncOverAsync(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteSyncOverAsync(pConn, pPacket); + + #endregion + + #region DllImports + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint GetSniMaxComposedSpnLength(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper( + SNIHandle pConn, + Provider provider, + [In] ref AuthProviderInfo pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider provider, [In] ref uint pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICheckConnectionWrapper([In] SNIHandle pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICloseWrapper(IntPtr pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper( + [In] SNIHandle pConn, + QueryType queryType, + [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out ushort portNum); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIGetLastError(out SniError pErrorStruct); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern uint SNIGetPeerAddrStrWrapper( + [In] SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIInitialize([In] IntPtr pmo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenSyncExWrapper( + ref SniClientConsumerInfo pClientConsumerInfo, + out IntPtr ppConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenWrapper( + [In] ref SniConsumerInfo pConsumerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string szConnect, + [In] SNIHandle pConn, + out IntPtr ppConn, + [MarshalAs(UnmanagedType.Bool)] bool fSync, + SqlConnectionIPAddressPreference ipPreference, + [In] ref SniDnsCacheInfo pDnsCacheInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType ioType); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIPacketGetDataWrapper( + [In] IntPtr packet, + [In, Out] byte[] readBuffer, + uint readBufferLength, + out uint dataSize); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketRelease(IntPtr pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketResetWrapper( + [In] SNIHandle pConn, + IoType ioType, + SNIPacket pPacket, + ConsumerNumber consumer); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadAsyncWrapper(SNIHandle pConn, ref IntPtr ppNewPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIRemoveProviderWrapper(SNIHandle pConn, Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe uint SNISecGenClientContextWrapper( + [In] SNIHandle pConn, + [In, Out] byte* pIn, + uint cbIn, + [In, Out] byte[] pOut, + [In] ref uint pcbOut, + [MarshalAs(UnmanagedType.Bool)] out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, + [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISecInitPackage(ref uint pcbMaxToken); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIServerEnumCloseWrapper([In] IntPtr packet); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIServerEnumOpenWrapper(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern int SNIServerEnumReadWrapper( + [In] IntPtr packet, + [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, + [In] int bufferLength, + [MarshalAs(UnmanagedType.Bool)] out bool more); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISetInfoWrapper(SNIHandle pConn, QueryType queryType, [In] ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNITerminate(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper( + [In] SNIHandle pConn, + int dwMilliseconds, + out uint pProtocolVersion); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint UnmanagedIsTokenRestricted( + [In] IntPtr token, + [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); + + #endregion + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX86.netfx.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX86.netfx.cs new file mode 100644 index 0000000000..70aa77ae4b --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeMethodsX86.netfx.cs @@ -0,0 +1,319 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if NETFRAMEWORK + +using System; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Data.SqlClient; + +namespace Interop.Windows.Sni +{ + internal sealed class SniNativeMethodsX86 : ISniNativeMethods + { + private const string DllName = "Microsoft.Data.SqlClient.SNI.x86.dll"; + + #region Interface Implementation + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref AuthProviderInfo pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniAddProvider(SNIHandle pConn, Provider provider, ref uint pInfo) => + SNIAddProviderWrapper(pConn, provider, ref pInfo); + + public uint SniCheckConnection(SNIHandle pConn) => + SNICheckConnectionWrapper(pConn); + + public uint SniClose(IntPtr pConn) => + SNICloseWrapper(pConn); + + public uint SniGetMaxComposedSpnLength() => + GetSniMaxComposedSpnLength(); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out bool pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo) => + SNIGetInfoWrapper(pConn, queryType, out pbQueryInfo); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out Provider provider) => + SNIGetInfoWrapper(pConn, queryType, out provider); + + public uint SniGetInfoWrapper(SNIHandle pConn, QueryType queryType, out ushort portNumber) => + SNIGetInfoWrapper(pConn, queryType, out portNumber); + + public void SniGetLastError(out SniError pLastError) => + SNIGetLastError(out pLastError); + + public uint SniGetPeerAddrStrWrapper( + SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength) => + SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLength); + + public uint SniInitialize(IntPtr pmo) => + SNIInitialize(pmo); + + public uint SniIsTokenRestricted(IntPtr token, out bool isRestricted) => + UnmanagedIsTokenRestricted(token, out isRestricted); + + public uint SniOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) => + SNIOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); + + public uint SniOpenWrapper( + ref SniConsumerInfo pConsumerInfo, + string connect, + SNIHandle pConn, + out IntPtr ppConn, + bool fSync, + SqlConnectionIPAddressPreference ipPreference, + ref SniDnsCacheInfo pDnsCacheInfo) => + SNIOpenWrapper(ref pConsumerInfo, connect, pConn, out ppConn, fSync, ipPreference, ref pDnsCacheInfo); + + public IntPtr SniPacketAllocateWrapper(SafeHandle pConn, IoType ioType) => + SNIPacketAllocateWrapper(pConn, ioType); + + public uint SniPacketGetDataWrapper( + IntPtr packet, + byte[] readBuffer, + uint readBufferLength, + out uint dataSize) => + SNIPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); + + public void SniPacketReset(SNIHandle pConn, IoType ioType, SNIPacket pPacket, ConsumerNumber consumer) => + SNIPacketResetWrapper(pConn, ioType, pPacket, consumer); + + public void SniPacketRelease(IntPtr pPacket) => + SNIPacketRelease(pPacket); + + public unsafe void SniPacketSetData(SNIPacket pPacket, byte* pbBuffer, uint cbBuffer) => + SNIPacketSetData(pPacket, pbBuffer, cbBuffer); + + public uint SniQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniQueryInfo(QueryType queryType, ref uint pbQueryInfo) => + SNIQueryInfo(queryType, ref pbQueryInfo); + + public uint SniReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) => + SNIReadAsyncWrapper(pConn, ref ppNewPacket); + + public uint SniReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) => + SNIReadSyncOverAsync(pConn, ref ppNewPacket, timeout); + + public uint SniRemoveProvider(SNIHandle pConn, Provider provider) => + SNIRemoveProviderWrapper(pConn, provider); + + public unsafe uint SniSecGenClientContextWrapper( + SNIHandle pConn, + byte* pIn, + uint cbIn, + byte[] pOut, + ref uint pcbOut, + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + string pwszUserName, + string pwszPassword) => + SNISecGenClientContextWrapper( + pConn, + pIn, + cbIn, + pOut, + ref pcbOut, + out pfDone, + szServerInfo, + cbServerInfo, + pwszUserName, + pwszPassword); + + public uint SniSecInitPackage(ref uint pcbMaxToken) => + SNISecInitPackage(ref pcbMaxToken); + + public void SniServerEnumClose(IntPtr packet) => + SNIServerEnumCloseWrapper(packet); + + public IntPtr SniServerEnumOpen() => + SNIServerEnumOpenWrapper(); + + public int SniServerEnumRead(IntPtr packet, char[] readBuffer, int bufferLength, out bool more) => + SNIServerEnumReadWrapper(packet, readBuffer, bufferLength, out more); + + public uint SniSetInfo(SNIHandle pConn, QueryType queryType, ref uint pbQueryInfo) => + SNISetInfoWrapper(pConn, queryType, ref pbQueryInfo); + + public uint SniTerminate() => + SNITerminate(); + + public uint SniWaitForSslHandshakeToComplete(SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) => + SNIWaitForSSLHandshakeToCompleteWrapper(pConn, dwMilliseconds, out pProtocolVersion); + + public uint SniWriteAsyncWrapper(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteAsyncWrapper(pConn, pPacket); + + public uint SniWriteSyncOverAsync(SNIHandle pConn, SNIPacket pPacket) => + SNIWriteSyncOverAsync(pConn, pPacket); + + #endregion + + #region DllImports + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint GetSniMaxComposedSpnLength(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper( + SNIHandle pConn, + Provider provider, + [In] ref AuthProviderInfo pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIAddProviderWrapper(SNIHandle pConn, Provider provider, [In] ref uint pInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICheckConnectionWrapper([In] SNIHandle pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNICloseWrapper(IntPtr pConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper( + [In] SNIHandle pConn, + QueryType queryType, + [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Guid pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType queryType, out ushort portNum); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIGetLastError(out SniError pErrorStruct); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern uint SNIGetPeerAddrStrWrapper( + [In] SNIHandle pConn, + int bufferSize, + StringBuilder addrBuffer, + out uint addrLength); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIInitialize([In] IntPtr pmo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenSyncExWrapper( + ref SniClientConsumerInfo pClientConsumerInfo, + out IntPtr ppConn); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIOpenWrapper( + [In] ref SniConsumerInfo pConsumerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string szConnect, + [In] SNIHandle pConn, + out IntPtr ppConn, + [MarshalAs(UnmanagedType.Bool)] bool fSync, + SqlConnectionIPAddressPreference ipPreference, + [In] ref SniDnsCacheInfo pDnsCacheInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType ioType); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIPacketGetDataWrapper( + [In] IntPtr packet, + [In, Out] byte[] readBuffer, + uint readBufferLength, + out uint dataSize); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketRelease(IntPtr pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIPacketResetWrapper( + [In] SNIHandle pConn, + IoType ioType, + SNIPacket pPacket, + ConsumerNumber consumer); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref IntPtr pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIQueryInfo(QueryType queryType, ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadAsyncWrapper(SNIHandle pConn, ref IntPtr ppNewPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIRemoveProviderWrapper(SNIHandle pConn, Provider provider); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe uint SNISecGenClientContextWrapper( + [In] SNIHandle pConn, + [In, Out] byte* pIn, + uint cbIn, + [In, Out] byte[] pOut, + [In] ref uint pcbOut, + [MarshalAs(UnmanagedType.Bool)] out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, + [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISecInitPackage(ref uint pcbMaxToken); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern void SNIServerEnumCloseWrapper([In] IntPtr packet); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr SNIServerEnumOpenWrapper(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] + private static extern int SNIServerEnumReadWrapper( + [In] IntPtr packet, + [In, Out][MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, + [In] int bufferLength, + [MarshalAs(UnmanagedType.Bool)] out bool more); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNISetInfoWrapper(SNIHandle pConn, QueryType queryType, [In] ref uint pbQueryInfo); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNITerminate(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWaitForSSLHandshakeToCompleteWrapper( + [In] SNIHandle pConn, + int dwMilliseconds, + out uint pProtocolVersion); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + private static extern uint UnmanagedIsTokenRestricted( + [In] IntPtr token, + [MarshalAs(UnmanagedType.Bool)] out bool isRestricted); + + #endregion + } +} + +#endif diff --git a/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeWrapper.cs b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeWrapper.cs new file mode 100644 index 0000000000..faa34928a9 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeWrapper.cs @@ -0,0 +1,608 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Security; +using System.Text; +using Interop.Windows.Sni; +using Microsoft.Data.Common; +using Microsoft.Data.SqlClient; + +namespace Microsoft.Data.SqlClient +{ + internal static class SniNativeWrapper + { + #if NETFRAMEWORK + private static readonly ISniNativeMethods NativeMethods = RuntimeInformation.ProcessArchitecture switch + { + Architecture.Arm64 => new SniNativeMethodsArm64(), + Architecture.X64 => new SniNativeMethodsX64(), + Architecture.X86 => new SniNativeMethodsX86(), + _ => new SniNativeMethodsNotSupported(RuntimeInformation.ProcessArchitecture) + }; + #else + private static readonly ISniNativeMethods NativeMethods = new SniNativeMethods(); + #endif + + private static int s_sniMaxComposedSpnLength = -1; + + private const int SniOpenTimeOut = -1; // infinite + + internal const int SniIP6AddrStringBufferLength = 48; // from SNI layer + + internal static int SniMaxComposedSpnLength + { + get + { + if (s_sniMaxComposedSpnLength == -1) + { + s_sniMaxComposedSpnLength = checked((int)GetSniMaxComposedSpnLength()); + } + return s_sniMaxComposedSpnLength; + } + } + + #if NETFRAMEWORK + static AppDomain GetDefaultAppDomainInternal() + { + return AppDomain.CurrentDomain; + } + + internal static _AppDomain GetDefaultAppDomain() + { + return GetDefaultAppDomainInternal(); + } + + [ResourceExposure(ResourceScope.Process)] // SxS: there is no way to set scope = Instance, using Process which is wider + [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] + internal unsafe static byte[] GetData() + { + int size; + IntPtr ptr = (IntPtr)(SqlDependencyProcessDispatcherStorage.NativeGetData(out size)); + byte[] result = null; + + if (ptr != IntPtr.Zero) + { + result = new byte[size]; + Marshal.Copy(ptr, result, 0, size); + } + + return result; + } + + [ResourceExposure(ResourceScope.Process)] // SxS: there is no way to set scope = Instance, using Process which is wider + [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] + internal unsafe static void SetData(Byte[] data) + { + //cli::pin_ptr pin_dispatcher = &data[0]; + fixed (byte* pin_dispatcher = &data[0]) + { + SqlDependencyProcessDispatcherStorage.NativeSetData(pin_dispatcher, data.Length); + } + } + #endif + + #region DLL Imports + + internal static uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref uint pInfo) => + NativeMethods.SniAddProvider(pConn, ProvNum, ref pInfo); + + internal static uint SNIAddProvider(SNIHandle pConn, Provider ProvNum, [In] ref AuthProviderInfo pInfo) => + NativeMethods.SniAddProvider(pConn, ProvNum, ref pInfo); + + internal static uint SNICheckConnection([In] SNIHandle pConn) => + NativeMethods.SniCheckConnection(pConn); + + internal static uint SNIClose(IntPtr pConn) => + NativeMethods.SniClose(pConn); + + internal static void SNIGetLastError(out SniError pErrorStruct) => + NativeMethods.SniGetLastError(out pErrorStruct); + + internal static void SNIPacketRelease(IntPtr pPacket) => + NativeMethods.SniPacketRelease(pPacket); + + internal static void SNIPacketReset([In] SNIHandle pConn, IoType IOType, SNIPacket pPacket, ConsumerNumber ConsNum) => + NativeMethods.SniPacketReset(pConn, IOType, pPacket, ConsNum); + + internal static uint SNIQueryInfo(QueryType QType, ref uint pbQInfo) => + NativeMethods.SniQueryInfo(QType, ref pbQInfo); + + internal static uint SNIQueryInfo(QueryType QType, ref IntPtr pbQInfo) => + NativeMethods.SniQueryInfo(QType, ref pbQInfo); + + internal static uint SNIReadAsync(SNIHandle pConn, ref IntPtr ppNewPacket) => + NativeMethods.SniReadAsync(pConn, ref ppNewPacket); + + internal static uint SNIReadSyncOverAsync(SNIHandle pConn, ref IntPtr ppNewPacket, int timeout) => + NativeMethods.SniReadSyncOverAsync(pConn, ref ppNewPacket, timeout); + + internal static uint SNIRemoveProvider(SNIHandle pConn, Provider ProvNum) => + NativeMethods.SniRemoveProvider(pConn, ProvNum); + + internal static uint SNISecInitPackage(ref uint pcbMaxToken) => + NativeMethods.SniSecInitPackage(ref pcbMaxToken); + + internal static uint SNISetInfo(SNIHandle pConn, QueryType QType, [In] ref uint pbQInfo) => + NativeMethods.SniSetInfo(pConn, QType, ref pbQInfo); + + internal static uint SNITerminate() => + NativeMethods.SniTerminate(); + + internal static uint SNIWaitForSSLHandshakeToComplete([In] SNIHandle pConn, int dwMilliseconds, out uint pProtocolVersion) => + NativeMethods.SniWaitForSslHandshakeToComplete(pConn, dwMilliseconds, out pProtocolVersion); + + internal static uint UnmanagedIsTokenRestricted([In] IntPtr token, [MarshalAs(UnmanagedType.Bool)] out bool isRestricted) => + NativeMethods.SniIsTokenRestricted(token, out isRestricted); + + private static uint GetSniMaxComposedSpnLength() => + NativeMethods.SniGetMaxComposedSpnLength(); + + private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Guid pbQInfo) => + NativeMethods.SniGetInfoWrapper(pConn, QType, out pbQInfo); + + #if NETFRAMEWORK + private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, [MarshalAs(UnmanagedType.Bool)] out bool pbQInfo) => + NativeMethods.SniGetInfoWrapper(pConn, QType, out pbQInfo); + #endif + + private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out ushort portNum) => + NativeMethods.SniGetInfoWrapper(pConn, QType, out portNum); + + private static uint SNIGetPeerAddrStrWrapper([In] SNIHandle pConn, int bufferSize, StringBuilder addrBuffer, out uint addrLen) => + NativeMethods.SniGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out addrLen); + + private static uint SNIGetInfoWrapper([In] SNIHandle pConn, QueryType QType, out Provider provNum) => + NativeMethods.SniGetInfoWrapper(pConn, QType, out provNum); + + private static uint SNIInitialize([In] IntPtr pmo) => + NativeMethods.SniInitialize(pmo); + + private static uint SNIOpenSyncExWrapper(ref SniClientConsumerInfo pClientConsumerInfo, out IntPtr ppConn) => + NativeMethods.SniOpenSyncExWrapper(ref pClientConsumerInfo, out ppConn); + + private static uint SNIOpenWrapper( + [In] ref SniConsumerInfo pConsumerInfo, + [MarshalAs(UnmanagedType.LPWStr)] string szConnect, + [In] SNIHandle pConn, + out IntPtr ppConn, + [MarshalAs(UnmanagedType.Bool)] bool fSync, + SqlConnectionIPAddressPreference ipPreference, + [In] ref SniDnsCacheInfo pDNSCachedInfo) => + NativeMethods.SniOpenWrapper( + ref pConsumerInfo, + szConnect, + pConn, + out ppConn, + fSync, + ipPreference, + ref pDNSCachedInfo); + + private static IntPtr SNIPacketAllocateWrapper([In] SafeHandle pConn, IoType IOType) => + NativeMethods.SniPacketAllocateWrapper(pConn, IOType); + + private static uint SNIPacketGetDataWrapper([In] IntPtr packet, [In, Out] byte[] readBuffer, uint readBufferLength, out uint dataSize) => + NativeMethods.SniPacketGetDataWrapper(packet, readBuffer, readBufferLength, out dataSize); + + private static unsafe void SNIPacketSetData(SNIPacket pPacket, [In] byte* pbBuf, uint cbBuf) => + NativeMethods.SniPacketSetData(pPacket, pbBuf, cbBuf); + + private static unsafe uint SNISecGenClientContextWrapper( + [In] SNIHandle pConn, + [In, Out] ReadOnlySpan pIn, + [In, Out] byte[] pOut, + [In] ref uint pcbOut, + [MarshalAsAttribute(UnmanagedType.Bool)] + out bool pfDone, + byte* szServerInfo, + uint cbServerInfo, + [MarshalAsAttribute(UnmanagedType.LPWStr)] + string pwszUserName, + [MarshalAsAttribute(UnmanagedType.LPWStr)] + string pwszPassword) + { + fixed (byte* pInPtr = pIn) + { + return NativeMethods.SniSecGenClientContextWrapper( + pConn, + pInPtr, + (uint)pIn.Length, + pOut, + ref pcbOut, + out pfDone, + szServerInfo, + cbServerInfo, + pwszUserName, + pwszPassword); + } + } + + private static uint SNIWriteAsyncWrapper(SNIHandle pConn, [In] SNIPacket pPacket) => + NativeMethods.SniWriteAsyncWrapper(pConn, pPacket); + + private static uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket) => + NativeMethods.SniWriteSyncOverAsync(pConn, pPacket); + + internal static IntPtr SNIServerEnumOpen() => + NativeMethods.SniServerEnumOpen(); + + internal static void SNIServerEnumClose([In] IntPtr packet) => + NativeMethods.SniServerEnumClose(packet); + + internal static int SNIServerEnumRead( + [In] IntPtr packet, + [In] [MarshalAs(UnmanagedType.LPArray)] char[] readBuffer, + [In] int bufferLength, + [MarshalAs(UnmanagedType.Bool)] out bool more) => + NativeMethods.SniServerEnumRead(packet, readBuffer, bufferLength, out more); + + #endregion + + internal static uint SniGetConnectionId(SNIHandle pConn, ref Guid connId) + { + return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_CONNID, out connId); + } + + internal static uint SniGetProviderNumber(SNIHandle pConn, ref Provider provNum) + { + return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_PROVIDERNUM, out provNum); + } + + internal static uint SniGetConnectionPort(SNIHandle pConn, ref ushort portNum) + { + return SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_PEERPORT, out portNum); + } + + internal static uint SniGetConnectionIPString(SNIHandle pConn, ref string connIPStr) + { + UInt32 ret; + uint connIPLen = 0; + + int bufferSize = SniIP6AddrStringBufferLength; + StringBuilder addrBuffer = new StringBuilder(bufferSize); + + ret = SNIGetPeerAddrStrWrapper(pConn, bufferSize, addrBuffer, out connIPLen); + + connIPStr = addrBuffer.ToString(0, Convert.ToInt32(connIPLen)); + + return ret; + } + + internal static uint SNIInitialize() + { + return SNIInitialize(IntPtr.Zero); + } + + internal static unsafe uint SNIOpenMarsSession(ConsumerInfo consumerInfo, SNIHandle parent, ref IntPtr pConn, bool fSync, SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo) + { + // initialize consumer info for MARS + SniConsumerInfo native_consumerInfo = new SniConsumerInfo(); + MarshalConsumerInfo(consumerInfo, ref native_consumerInfo); + + SniDnsCacheInfo native_cachedDNSInfo = new SniDnsCacheInfo(); + native_cachedDNSInfo.wszCachedFQDN = cachedDNSInfo?.FQDN; + native_cachedDNSInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4; + native_cachedDNSInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6; + native_cachedDNSInfo.wszCachedTcpPort = cachedDNSInfo?.Port; + + return SNIOpenWrapper(ref native_consumerInfo, "session:", parent, out pConn, fSync, ipPreference, ref native_cachedDNSInfo); + } + + internal static unsafe uint SNIOpenSyncEx( + ConsumerInfo consumerInfo, + string constring, + ref IntPtr pConn, + byte[] spnBuffer, + byte[] instanceName, + bool fOverrideCache, + bool fSync, + int timeout, + bool fParallel, + + #if NETFRAMEWORK + Int32 transparentNetworkResolutionStateNo, + Int32 totalTimeout, + #endif + + SqlConnectionIPAddressPreference ipPreference, + SQLDNSInfo cachedDNSInfo, + string hostNameInCertificate) + { + fixed (byte* pin_instanceName = &instanceName[0]) + { + SniClientConsumerInfo clientConsumerInfo = new SniClientConsumerInfo(); + + // initialize client ConsumerInfo part first + MarshalConsumerInfo(consumerInfo, ref clientConsumerInfo.ConsumerInfo); + + clientConsumerInfo.wszConnectionString = constring; + clientConsumerInfo.HostNameInCertificate = hostNameInCertificate; + clientConsumerInfo.networkLibrary = Prefix.UNKNOWN_PREFIX; + clientConsumerInfo.szInstanceName = pin_instanceName; + clientConsumerInfo.cchInstanceName = (uint)instanceName.Length; + clientConsumerInfo.fOverrideLastConnectCache = fOverrideCache; + clientConsumerInfo.fSynchronousConnection = fSync; + clientConsumerInfo.timeout = timeout; + clientConsumerInfo.fParallel = fParallel; + + #if NETFRAMEWORK + switch (transparentNetworkResolutionStateNo) + { + case (0): + clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.DisabledMode; + break; + case (1): + clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.SequentialMode; + break; + case (2): + clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.ParallelMode; + break; + }; + clientConsumerInfo.totalTimeout = totalTimeout; + #else + clientConsumerInfo.transparentNetworkResolution = TransparentNetworkResolutionMode.DisabledMode; + clientConsumerInfo.totalTimeout = SniOpenTimeOut; + #endif + + clientConsumerInfo.isAzureSqlServerEndpoint = ADP.IsAzureSqlServerEndpoint(constring); + + clientConsumerInfo.ipAddressPreference = ipPreference; + clientConsumerInfo.DNSCacheInfo.wszCachedFQDN = cachedDNSInfo?.FQDN; + clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4; + clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6; + clientConsumerInfo.DNSCacheInfo.wszCachedTcpPort = cachedDNSInfo?.Port; + + if (spnBuffer != null) + { + fixed (byte* pin_spnBuffer = &spnBuffer[0]) + { + clientConsumerInfo.szSPN = pin_spnBuffer; + clientConsumerInfo.cchSPN = (uint)spnBuffer.Length; + return SNIOpenSyncExWrapper(ref clientConsumerInfo, out pConn); + } + } + else + { + // else leave szSPN null (SQL Auth) + return SNIOpenSyncExWrapper(ref clientConsumerInfo, out pConn); + } + } + } + + #if NETFRAMEWORK + [ResourceExposure(ResourceScope.None)] + [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)] + internal static uint SNIAddProvider(SNIHandle pConn, + Provider providerEnum, + AuthProviderInfo authInfo) + { + UInt32 ret; + uint ERROR_SUCCESS = 0; + + Debug.Assert(authInfo.clientCertificateCallback == null, "CTAIP support has been removed"); + + ret = SNIAddProvider(pConn, providerEnum, ref authInfo); + + if (ret == ERROR_SUCCESS) + { + // added a provider, need to requery for sync over async support + ret = SNIGetInfoWrapper(pConn, QueryType.SNI_QUERY_CONN_SUPPORTS_SYNC_OVER_ASYNC, out bool _); + Debug.Assert(ret == ERROR_SUCCESS, "SNIGetInfo cannot fail with this QType"); + } + + return ret; + } + #endif + + internal static void SNIPacketAllocate(SafeHandle pConn, IoType IOType, ref IntPtr pPacket) + { + pPacket = SNIPacketAllocateWrapper(pConn, IOType); + } + + internal static unsafe uint SNIPacketGetData(IntPtr packet, byte[] readBuffer, ref uint dataSize) + { + return SNIPacketGetDataWrapper(packet, readBuffer, (uint)readBuffer.Length, out dataSize); + } + + internal static unsafe void SNIPacketSetData(SNIPacket packet, byte[] data, int length) + { + fixed (byte* pin_data = &data[0]) + { + SNIPacketSetData(packet, pin_data, (uint)length); + } + } + + #if NETFRAMEWORK + //[ResourceExposure(ResourceScope::None)] + // + // Notes on SecureString: Writing out security sensitive information to managed buffer should be avoid as these can be moved + // around by GC. There are two set of information which falls into this category: passwords and new changed password which + // are passed in as SecureString by a user. Writing out clear passwords information is delayed until this layer to ensure that + // the information is written out to buffer which is pinned in this method already. This also ensures that processing a clear password + // is done right before it is written out to SNI_Packet where gets encrypted properly. + // TdsParserStaticMethods.EncryptPassword operation is also done here to minimize the time the clear password is held in memory. Any changes + // to loose encryption algorithm is changed it should be done in both in this method as well as TdsParserStaticMethods.EncryptPassword. + // Up to current release, it is also guaranteed that both password and new change password will fit into a single login packet whose size is fixed to 4096 + // So, there is no splitting logic is needed. + internal static void SNIPacketSetData(SNIPacket packet, + Byte[] data, + Int32 length, + SecureString[] passwords, // pointer to the passwords which need to be written out to SNI Packet + Int32[] passwordOffsets // Offset into data buffer where the password to be written out to + ) + { + Debug.Assert(passwords == null || (passwordOffsets != null && passwords.Length == passwordOffsets.Length), "The number of passwords does not match the number of password offsets"); + + bool mustRelease = false; + bool mustClearBuffer = false; + IntPtr clearPassword = IntPtr.Zero; + + // provides a guaranteed finally block – without this it isn’t guaranteed – non interruptable by fatal exceptions + RuntimeHelpers.PrepareConstrainedRegions(); + try + { + unsafe + { + + fixed (byte* pin_data = &data[0]) + { } + if (passwords != null) + { + // Process SecureString + for (int i = 0; i < passwords.Length; ++i) + { + // SecureString is used + if (passwords[i] != null) + { + // provides a guaranteed finally block – without this it isn’t guaranteed – non interruptable by fatal exceptions + RuntimeHelpers.PrepareConstrainedRegions(); + try + { + // ========================================================================== + // Get the clear text of secure string without converting it to String type + // ========================================================================== + clearPassword = Marshal.SecureStringToCoTaskMemUnicode(passwords[i]); + + // ========================================================================================================================== + // Losely encrypt the clear text - The encryption algorithm should exactly match the TdsParserStaticMethods.EncryptPassword + // ========================================================================================================================== + + unsafe + { + + char* pwChar = (char*)clearPassword.ToPointer(); + byte* pByte = (byte*)(clearPassword.ToPointer()); + + + + + int s; + byte bLo; + byte bHi; + int passwordsLength = passwords[i].Length; + for (int j = 0; j < passwordsLength; ++j) + { + s = (int)*pwChar; + bLo = (byte)(s & 0xff); + bHi = (byte)((s >> 8) & 0xff); + *(pByte++) = (Byte)((((bLo & 0x0f) << 4) | (bLo >> 4)) ^ 0xa5); + *(pByte++) = (Byte)((((bHi & 0x0f) << 4) | (bHi >> 4)) ^ 0xa5); + ++pwChar; + } + + // =========================================================== + // Write out the losely encrypted passwords to data buffer + // =========================================================== + mustClearBuffer = true; + Marshal.Copy(clearPassword, data, passwordOffsets[i], passwordsLength * 2); + } + } + finally + { + // Make sure that we clear the security sensitive information + if (clearPassword != IntPtr.Zero) + { + Marshal.ZeroFreeCoTaskMemUnicode(clearPassword); + } + } + } + } + } + + packet.DangerousAddRef(ref mustRelease); + Debug.Assert(mustRelease, "AddRef Failed!"); + + fixed (byte* pin_data = &data[0]) + { + SNIPacketSetData(packet, pin_data, (uint)length); + } + } + } + finally + { + if (mustRelease) + { + packet.DangerousRelease(); + } + + // Make sure that we clear the security sensitive information + // data->Initialize() is not safe to call under CER + if (mustClearBuffer) + { + for (int i = 0; i < data.Length; ++i) + { + data[i] = 0; + } + } + } + } + #endif + + + internal static unsafe uint SNISecGenClientContext(SNIHandle pConnectionObject, ReadOnlySpan inBuff, byte[] OutBuff, ref uint sendLength, byte[] serverUserName) + { + fixed (byte* pin_serverUserName = &serverUserName[0]) + //netcore fixed (byte* pInBuff = inBuff) + { + return SNISecGenClientContextWrapper( + pConnectionObject, + inBuff, + OutBuff, + ref sendLength, + out _, + pin_serverUserName, + (uint)serverUserName.Length, + null, + null); + } + } + + internal static uint SNIWritePacket(SNIHandle pConn, SNIPacket packet, bool sync) + { + if (sync) + { + return SNIWriteSyncOverAsync(pConn, packet); + } + else + { + return SNIWriteAsyncWrapper(pConn, packet); + } + } + + private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref SniConsumerInfo native_consumerInfo) + { + native_consumerInfo.DefaultUserDataLength = consumerInfo.defaultBufferSize; + native_consumerInfo.fnReadComp = consumerInfo.readDelegate != null + ? Marshal.GetFunctionPointerForDelegate(consumerInfo.readDelegate) + : IntPtr.Zero; + native_consumerInfo.fnWriteComp = consumerInfo.writeDelegate != null + ? Marshal.GetFunctionPointerForDelegate(consumerInfo.writeDelegate) + : IntPtr.Zero; + native_consumerInfo.ConsumerKey = consumerInfo.key; + } + } +} + +namespace Microsoft.Data +{ + internal static class Win32NativeMethods + { + internal static bool IsTokenRestrictedWrapper(IntPtr token) + { + bool isRestricted; + uint result = SniNativeWrapper.UnmanagedIsTokenRestricted(token, out isRestricted); + + if (result != 0) + { + Marshal.ThrowExceptionForHR(unchecked((int)result)); + } + + return isRestricted; + } + } +} diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj index 6f5595b591..c092b35a98 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj @@ -4,14 +4,16 @@ true - + + - - - + + + + diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorNativeHelper.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorNativeHelper.cs index 65a7898c68..b99e91414a 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorNativeHelper.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorNativeHelper.cs @@ -50,23 +50,23 @@ internal static DataTable GetDataSources() { } finally { - handle = SNINativeMethodWrapper.SNIServerEnumOpen(); + handle = SniNativeWrapper.SNIServerEnumOpen(); SqlClientEventSource.Log.TryTraceEvent(" {2} returned handle = {3}.", nameof(SqlDataSourceEnumeratorNativeHelper), nameof(GetDataSources), - nameof(SNINativeMethodWrapper.SNIServerEnumOpen), handle); + nameof(SniNativeWrapper.SNIServerEnumOpen), handle); } if (handle != ADP.s_ptrZero) { while (more && !TdsParserStaticMethods.TimeoutHasExpired(s_timeoutTime)) { - readLength = SNINativeMethodWrapper.SNIServerEnumRead(handle, buffer, bufferSize, out more); + readLength = SniNativeWrapper.SNIServerEnumRead(handle, buffer, bufferSize, out more); SqlClientEventSource.Log.TryTraceEvent(" {2} returned 'readlength':{3}, and 'more':{4} with 'bufferSize' of {5}", nameof(SqlDataSourceEnumeratorNativeHelper), nameof(GetDataSources), - nameof(SNINativeMethodWrapper.SNIServerEnumRead), + nameof(SniNativeWrapper.SNIServerEnumRead), readLength, more, bufferSize); if (readLength > bufferSize) { @@ -84,21 +84,21 @@ internal static DataTable GetDataSources() { if (handle != ADP.s_ptrZero) { - SNINativeMethodWrapper.SNIServerEnumClose(handle); + SniNativeWrapper.SNIServerEnumClose(handle); SqlClientEventSource.Log.TryTraceEvent(" {2} called.", nameof(SqlDataSourceEnumeratorNativeHelper), nameof(GetDataSources), - nameof(SNINativeMethodWrapper.SNIServerEnumClose)); + nameof(SniNativeWrapper.SNIServerEnumClose)); } } if (failure) { - Debug.Assert(false, $"{nameof(GetDataSources)}:{nameof(SNINativeMethodWrapper.SNIServerEnumRead)} returned bad length"); + Debug.Assert(false, $"{nameof(GetDataSources)}:{nameof(SniNativeWrapper.SNIServerEnumRead)} returned bad length"); SqlClientEventSource.Log.TryTraceEvent(" {2} returned bad length, requested buffer {3}, received {4}", nameof(SqlDataSourceEnumeratorNativeHelper), nameof(GetDataSources), - nameof(SNINativeMethodWrapper.SNIServerEnumRead), + nameof(SniNativeWrapper.SNIServerEnumRead), bufferSize, readLength); throw ADP.ArgumentOutOfRange(StringsHelper.GetString(Strings.ADP_ParameterValueOutOfRange, readLength), nameof(readLength)); diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SSPI/NativeSSPIContextProvider.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SSPI/NativeSSPIContextProvider.cs index 067682a617..26145134d4 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SSPI/NativeSSPIContextProvider.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SSPI/NativeSSPIContextProvider.cs @@ -35,7 +35,7 @@ private void LoadSSPILibrary() // use local for ref param to defer setting s_maxSSPILength until we know the call succeeded. uint maxLength = 0; - if (0 != SNINativeMethodWrapper.SNISecInitPackage(ref maxLength)) + if (0 != SniNativeWrapper.SNISecInitPackage(ref maxLength)) SSPIError(SQLMessage.SSPIInitializeError(), TdsEnums.INIT_SSPI_PACKAGE); s_maxSSPILength = maxLength; @@ -58,7 +58,7 @@ internal override void GenerateSspiClientContext(ReadOnlyMemory receivedBu Debug.Assert(_physicalStateObj.SessionHandle.Type == SessionHandle.NativeHandleType); SNIHandle handle = _physicalStateObj.SessionHandle.NativeHandle; #endif - if (0 != SNINativeMethodWrapper.SNISecGenClientContext(handle, receivedBuff.Span, sendBuff, ref sendLength, _sniSpnBuffer[0])) + if (0 != SniNativeWrapper.SNISecGenClientContext(handle, receivedBuff.Span, sendBuff, ref sendLength, _sniSpnBuffer[0])) { throw new InvalidOperationException(SQLMessage.SSPIGenerateError()); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs index e8d474b3da..534d42f866 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs @@ -462,7 +462,7 @@ public void AddCommandDependency(SqlCommand command) [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)] private static void ObtainProcessDispatcher() { - byte[] nativeStorage = SNINativeMethodWrapper.GetData(); + byte[] nativeStorage = SniNativeWrapper.GetData(); if (nativeStorage == null) { @@ -472,7 +472,7 @@ private static void ObtainProcessDispatcher() SqlClientEventSource.Log.TryNotificationTraceEvent(" AppDomain.CurrentDomain.FriendlyName: {0}", AppDomain.CurrentDomain.FriendlyName); #endif // DEBUG - _AppDomain masterDomain = SNINativeMethodWrapper.GetDefaultAppDomain(); + _AppDomain masterDomain = SniNativeWrapper.GetDefaultAppDomain(); if (masterDomain != null) { @@ -492,7 +492,7 @@ private static void ObtainProcessDispatcher() SqlClientObjRef objRef = new(s_processDispatcher); DataContractSerializer serializer = new(objRef.GetType()); GetSerializedObject(objRef, serializer, stream); - SNINativeMethodWrapper.SetData(stream.ToArray()); // Native will be forced to synchronize and not overwrite. + SniNativeWrapper.SetData(stream.ToArray()); // Native will be forced to synchronize and not overwrite. } } else diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs index 3b1b668733..bf5871c57e 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs @@ -35,7 +35,7 @@ private SNILoadHandle() : base(IntPtr.Zero, true) { } finally { - _sniStatus = SNINativeMethodWrapper.SNIInitialize(); + _sniStatus = SniNativeWrapper.SNIInitialize(); base.handle = (IntPtr)1; // Initialize to non-zero dummy variable. } } @@ -56,7 +56,7 @@ public bool ClientOSEncryptionSupport { uint value = 0; // Query OS to find out whether encryption is supported. - SNINativeMethodWrapper.SNIQueryInfo(QueryType.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value); + SniNativeWrapper.SNIQueryInfo(QueryType.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value); _clientOSEncryptionSupport = value != 0; } catch (Exception e) @@ -78,7 +78,7 @@ override protected bool ReleaseHandle() if (TdsEnums.SNI_SUCCESS == _sniStatus) { LocalDBAPI.ReleaseDLLHandles(); - SNINativeMethodWrapper.SNITerminate(); + SniNativeWrapper.SNITerminate(); } base.handle = IntPtr.Zero; } @@ -183,15 +183,38 @@ internal SNIHandle( // timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE //} -#if NETFRAMEWORK + #if NETFRAMEWORK int transparentNetworkResolutionStateNo = (int)transparentNetworkResolutionState; - _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle, - spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, transparentNetworkResolutionStateNo, totalTimeout, - ADP.IsAzureSqlServerEndpoint(serverName), ipPreference, cachedDNSInfo, hostNameInCertificate); -#else - _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle, - spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, ipPreference, cachedDNSInfo, hostNameInCertificate); -#endif // NETFRAMEWORK + _status = SniNativeWrapper.SNIOpenSyncEx( + myInfo, + serverName, + ref base.handle, + spnBuffer, + instanceName, + flushCache, + fSync, + timeout, + fParallel, + transparentNetworkResolutionStateNo, + totalTimeout, + ipPreference, + cachedDNSInfo, + hostNameInCertificate); + #else + _status = SniNativeWrapper.SNIOpenSyncEx( + myInfo, + serverName, + ref base.handle, + spnBuffer, + instanceName, + flushCache, + fSync, + timeout, + fParallel, + ipPreference, + cachedDNSInfo, + hostNameInCertificate); + #endif } } @@ -202,7 +225,7 @@ internal SNIHandle(ConsumerInfo myInfo, SNIHandle parent, SqlConnectionIPAddress { } finally { - _status = SNINativeMethodWrapper.SNIOpenMarsSession(myInfo, parent, ref base.handle, parent._fSync, ipPreference, cachedDNSInfo); + _status = SniNativeWrapper.SNIOpenMarsSession(myInfo, parent, ref base.handle, parent._fSync, ipPreference, cachedDNSInfo); } } @@ -221,7 +244,7 @@ override protected bool ReleaseHandle() base.handle = IntPtr.Zero; if (IntPtr.Zero != ptr) { - if (0 != SNINativeMethodWrapper.SNIClose(ptr)) + if (0 != SniNativeWrapper.SNIClose(ptr)) { return false; // SNIClose should never fail. } @@ -242,7 +265,7 @@ internal sealed class SNIPacket : SafeHandle { internal SNIPacket(SafeHandle sniHandle) : base(IntPtr.Zero, true) { - SNINativeMethodWrapper.SNIPacketAllocate(sniHandle, IoType.WRITE, ref base.handle); + SniNativeWrapper.SNIPacketAllocate(sniHandle, IoType.WRITE, ref base.handle); if (IntPtr.Zero == base.handle) { throw SQL.SNIPacketAllocationFailure(); @@ -264,7 +287,7 @@ override protected bool ReleaseHandle() base.handle = IntPtr.Zero; if (IntPtr.Zero != ptr) { - SNINativeMethodWrapper.SNIPacketRelease(ptr); + SniNativeWrapper.SNIPacketRelease(ptr); } return true; } @@ -288,7 +311,7 @@ public SNIPacket Take(SNIHandle sniHandle) { // Success - reset the packet packet = _packets.Pop(); - SNINativeMethodWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI); + SniNativeWrapper.SNIPacketReset(sniHandle, IoType.WRITE, packet, ConsumerNumber.SNI_Consumer_SNI); } else { diff --git a/tools/props/Versions.props b/tools/props/Versions.props index b2e08d3531..12df4b45a1 100644 --- a/tools/props/Versions.props +++ b/tools/props/Versions.props @@ -32,11 +32,12 @@ 1.11.4 + 9.0.0 9.0.0 7.5.0 7.5.0 + 4.3.0 9.0.0 - 9.0.0