Ejemplo n.º 1
0
        internal override object ReadAsync(out uint error, ref object handle)
        {
            IntPtr readPacketPtr = IntPtr.Zero;

            error = SNINativeMethodWrapper.SNIReadAsync((SNIHandle)handle, ref readPacketPtr);
            return(readPacketPtr);
        }
Ejemplo n.º 2
0
        private void LoadSSPILibrary()
        {
            if (TdsParserStateObjectFactory.UseManagedSNI)
            {
                return;
            }
            // Outer check so we don't acquire lock once it's loaded.
            if (!s_fSSPILoaded)
            {
                lock (s_tdsParserLock)
                {
                    // re-check inside lock
                    if (!s_fSSPILoaded)
                    {
                        // 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))
                        {
                            SSPIError(SQLMessage.SSPIInitializeError(), TdsEnums.INIT_SSPI_PACKAGE);
                        }

                        s_maxSSPILength = maxLength;
                        s_fSSPILoaded   = true;
                    }
                }
            }

            if (s_maxSSPILength > int.MaxValue)
            {
                throw SQL.InvalidSSPIPacketSize();   // SqlBu 332503
            }
        }
Ejemplo n.º 3
0
        // creates a physical connection
        internal SNIHandle(
            SNINativeMethodWrapper.ConsumerInfo myInfo,
            string serverName,
            byte[] spnBuffer,
            bool ignoreSniOpenTimeout,
            int timeout,
            out byte[] instanceName,
            bool flushCache,
            bool fSync,
            bool fParallel)
            : base(IntPtr.Zero, true)
        {
            try { }
            finally
            {
                _fSync       = fSync;
                instanceName = new byte[256]; // Size as specified by netlibs.
                if (ignoreSniOpenTimeout)
                {
                    timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE
                }

                _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
                                                               spnBuffer, instanceName, flushCache, fSync, timeout, fParallel);
            }
        }
Ejemplo n.º 4
0
 // constructs SNI Handle for MARS session
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, SNIHandle parent) : base(IntPtr.Zero, true)
 {
     RuntimeHelpers.PrepareConstrainedRegions();
     try {} finally {
         _status = SNINativeMethodWrapper.SNIOpenMarsSession(myInfo, parent, ref base.handle, parent._fSync);
     }
 }
Ejemplo n.º 5
0
        private SNIErrorDetails GetSniErrorDetails()
        {
            SNIErrorDetails details = new SNIErrorDetails();

            if (TdsParserStateObjectFactory.UseManagedSNI)
            {
                SNIError sniError = SNIProxy.Singleton.GetLastError();
                details.sniErrorNumber = sniError.sniError;
                details.errorMessage   = sniError.errorMessage;
                details.nativeError    = sniError.nativeError;
                details.provider       = (int)sniError.provider;
                details.lineNumber     = sniError.lineNumber;
                details.function       = sniError.function;
                details.exception      = sniError.exception;
            }
            else
            {
                SNINativeMethodWrapper.SNI_Error sniError;
                SNINativeMethodWrapper.SNIGetLastError(out sniError);
                details.sniErrorNumber = sniError.sniError;
                details.errorMessage   = sniError.errorMessage;
                details.nativeError    = sniError.nativeError;
                details.provider       = (int)sniError.provider;
                details.lineNumber     = sniError.lineNumber;
                details.function       = sniError.function;
            }
            return(details);
        }
Ejemplo n.º 6
0
        // creates a physical connection
        internal SNIHandle(
            SNINativeMethodWrapper.ConsumerInfo myInfo,
            string serverName,
            byte[] spnBuffer,
            bool ignoreSniOpenTimeout,
            int timeout,
            out byte[] instanceName,
            bool flushCache,
            bool fSync,
            bool fParallel,
            TransparentNetworkResolutionState transparentNetworkResolutionState,
            int totalTimeout)
            : base(IntPtr.Zero, true)
        {
            RuntimeHelpers.PrepareConstrainedRegions();
            try {} finally {
                _fSync       = fSync;
                instanceName = new byte[256]; // Size as specified by netlibs.
                if (ignoreSniOpenTimeout)
                {
                    //


                    timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE
                }

                int transparentNetworkResolutionStateNo = (int)transparentNetworkResolutionState;
                _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
                                                               spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, transparentNetworkResolutionStateNo, totalTimeout,
                                                               ADP.IsAzureSqlServerEndpoint(serverName));
            }
        }
Ejemplo n.º 7
0
 // constructs SNI Handle for MARS session
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, SNIHandle parent) : base(IntPtr.Zero, true)
 {
     try { }
     finally
     {
         _status = SNINativeMethodWrapper.SNIOpenMarsSession(myInfo, parent, ref base.handle, parent._fSync);
     }
 }
Ejemplo n.º 8
0
 internal SNIPacket(SafeHandle sniHandle) : base(IntPtr.Zero, true)
 {
     SNINativeMethodWrapper.SNIPacketAllocate(sniHandle, SNINativeMethodWrapper.IOType.WRITE, ref base.handle);
     if (IntPtr.Zero == base.handle)
     {
         throw SQL.SNIPacketAllocationFailure();
     }
 }
Ejemplo n.º 9
0
        protected override bool ReleaseHandle()
        {
            IntPtr handle = base.handle;

            base.handle = IntPtr.Zero;
            if (IntPtr.Zero != handle)
            {
                SNINativeMethodWrapper.SNIPacketRelease(handle);
            }
            return(true);
        }
Ejemplo n.º 10
0
        protected override bool ReleaseHandle()
        {
            IntPtr handle = base.handle;

            base.handle = IntPtr.Zero;
            if ((IntPtr.Zero != handle) && (SNINativeMethodWrapper.SNIClose(handle) != 0))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 11
0
        override protected bool ReleaseHandle()
        {
            // NOTE: The SafeHandle class guarantees this will be called exactly once.
            IntPtr ptr = base.handle;

            base.handle = IntPtr.Zero;
            if (IntPtr.Zero != ptr)
            {
                SNINativeMethodWrapper.SNIPacketRelease(ptr);
            }
            return(true);
        }
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, SNIHandle parent) : base(IntPtr.Zero, true)
 {
     this._status = uint.MaxValue;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this._status = SNINativeMethodWrapper.SNIOpen(myInfo, serverName, parent, ref this.handle, parent._fSync);
     }
 }
Ejemplo n.º 13
0
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, SNIHandle parent) : base(IntPtr.Zero, true)
 {
     this._status = uint.MaxValue;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this._status = SNINativeMethodWrapper.SNIOpen(myInfo, serverName, parent, ref this.handle, parent._fSync);
     }
 }
Ejemplo n.º 14
0
        internal override PacketHandle ReadSyncOverAsync(int timeoutRemaining, out uint error)
        {
            SNIHandle handle = Handle;

            if (handle == null)
            {
                throw ADP.ClosedConnectionError();
            }
            IntPtr readPacketPtr = IntPtr.Zero;

            error = SNINativeMethodWrapper.SNIReadSyncOverAsync(handle, ref readPacketPtr, GetTimeoutRemaining());
            return(PacketHandle.FromNativePointer(readPacketPtr));
        }
 protected override bool ReleaseHandle()
 {
     if (base.handle != IntPtr.Zero)
     {
         if (this._sniStatus == 0)
         {
             LocalDBAPI.ReleaseDLLHandles();
             SNINativeMethodWrapper.SNITerminate();
         }
         base.handle = IntPtr.Zero;
     }
     return(true);
 }
Ejemplo n.º 16
0
        override protected bool ReleaseHandle()
        {
            if (base.handle != IntPtr.Zero)
            {
                if (TdsEnums.SNI_SUCCESS == _sniStatus)
                {
                    LocalDBAPI.ReleaseDLLHandles();
                    SNINativeMethodWrapper.SNITerminate();
                }
                base.handle = IntPtr.Zero;
            }

            return(true);
        }
Ejemplo n.º 17
0
 internal override object GetResetWritePacket()
 {
     if (_sniPacket != null)
     {
         SNINativeMethodWrapper.SNIPacketReset(Handle, SNINativeMethodWrapper.IOType.WRITE, _sniPacket, SNINativeMethodWrapper.ConsumerNumber.SNI_Consumer_SNI);
     }
     else
     {
         lock (_writePacketLockObject)
         {
             _sniPacket = _writePacketCache.Take(Handle);
         }
     }
     return(_sniPacket);
 }
Ejemplo n.º 18
0
        override protected bool ReleaseHandle()
        {
            // NOTE: The SafeHandle class guarantees this will be called exactly once.
            IntPtr ptr = base.handle;

            base.handle = IntPtr.Zero;
            if (IntPtr.Zero != ptr)
            {
                if (0 != SNINativeMethodWrapper.SNIClose(ptr))
                {
                    return(false);   // SNIClose should never fail.
                }
            }
            return(true);
        }
Ejemplo n.º 19
0
        public SNIPacket Take(SNIHandle sniHandle)
        {
            SNIPacket packet;

            if (_packets.Count > 0)
            {
                // Success - reset the packet
                packet = _packets.Pop();
                SNINativeMethodWrapper.SNIPacketReset(sniHandle, SNINativeMethodWrapper.IOType.WRITE, packet, SNINativeMethodWrapper.ConsumerNumber.SNI_Consumer_SNI);
            }
            else
            {
                // Failed to take a packet - create a new one
                packet = new SNIPacket(sniHandle);
            }
            return(packet);
        }
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, byte[] spnBuffer, bool ignoreSniOpenTimeout, int timeout, out byte[] instanceName, bool flushCache, bool fSync, bool fParallel) : base(IntPtr.Zero, true)
 {
     this._status = uint.MaxValue;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this._fSync = fSync;
         instanceName = new byte[0x100];
         if (ignoreSniOpenTimeout)
         {
             timeout = -1;
         }
         this._status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref this.handle, spnBuffer, instanceName, flushCache, fSync, timeout, fParallel);
     }
 }
Ejemplo n.º 21
0
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, byte[] spnBuffer, bool ignoreSniOpenTimeout, int timeout, out byte[] instanceName, bool flushCache, bool fSync, bool fParallel) : base(IntPtr.Zero, true)
 {
     this._status = uint.MaxValue;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this._fSync  = fSync;
         instanceName = new byte[0x100];
         if (ignoreSniOpenTimeout)
         {
             timeout = -1;
         }
         this._status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref this.handle, spnBuffer, instanceName, flushCache, fSync, timeout, fParallel);
     }
 }
 private SNILoadHandle() : base(IntPtr.Zero, true)
 {
     this.ReadAsyncCallbackDispatcher  = new SNINativeMethodWrapper.SqlAsyncCallbackDelegate(SNILoadHandle.ReadDispatcher);
     this.WriteAsyncCallbackDispatcher = new SNINativeMethodWrapper.SqlAsyncCallbackDelegate(SNILoadHandle.WriteDispatcher);
     this._sniStatus = uint.MaxValue;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this._sniStatus = SNINativeMethodWrapper.SNIInitialize();
         uint qInfo = 0;
         if (this._sniStatus == 0)
         {
             SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref qInfo);
         }
         this._encryptionOption = (qInfo == 0) ? EncryptionOptions.NOT_SUP : EncryptionOptions.OFF;
         base.handle            = (IntPtr)1;
     }
 }
        private static void ObtainProcessDispatcher()
        {
            byte[] data = SNINativeMethodWrapper.GetData();
            if (data == null)
            {
                Bid.NotificationsTrace("<sc.SqlDependency.ObtainProcessDispatcher|DEP> nativeStorage null, obtaining dispatcher AppDomain and creating ProcessDispatcher.\n");
                _AppDomain defaultAppDomain = SNINativeMethodWrapper.GetDefaultAppDomain();
                if (defaultAppDomain != null)
                {
                    ObjectHandle handle = CreateProcessDispatcher(defaultAppDomain);
                    if (handle != null)
                    {
                        SqlDependencyProcessDispatcher dispatcher = (SqlDependencyProcessDispatcher)handle.Unwrap();
                        if (dispatcher != null)
                        {
                            _processDispatcher = dispatcher.SingletonProcessDispatcher;
                            ObjRef          objRef     = GetObjRef(_processDispatcher);
                            BinaryFormatter formatter2 = new BinaryFormatter();
                            MemoryStream    stream     = new MemoryStream();
                            GetSerializedObject(objRef, formatter2, stream);
                            SNINativeMethodWrapper.SetData(stream.GetBuffer());
                            return;
                        }
                        Bid.NotificationsTrace("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - ObjectHandle.Unwrap returned null!\n");
                        throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyObtainProcessDispatcherFailureObjectHandle);
                    }
                    Bid.NotificationsTrace("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - AppDomain.CreateInstance returned null!\n");
                    throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyProcessDispatcherFailureCreateInstance);
                }
                Bid.NotificationsTrace("<sc.SqlDependency.ObtainProcessDispatcher|DEP|ERR> ERROR - unable to obtain default AppDomain!\n");
                throw ADP.InternalError(ADP.InternalErrorCode.SqlDependencyProcessDispatcherFailureAppDomain);
            }
            Bid.NotificationsTrace("<sc.SqlDependency.ObtainProcessDispatcher|DEP> nativeStorage not null, obtaining existing dispatcher AppDomain and ProcessDispatcher.\n");
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream2   = new MemoryStream(data);

            _processDispatcher = GetDeserializedObject(formatter, stream2);
            Bid.NotificationsTrace("<sc.SqlDependency.ObtainProcessDispatcher|DEP> processDispatcher obtained, ID: %d\n", _processDispatcher.ObjectID);
        }
Ejemplo n.º 24
0
        private SNILoadHandle() : base(IntPtr.Zero, true)
        {
            // SQL BU DT 346588 - from security review - SafeHandle guarantees this is only called once.
            // The reason for the safehandle is guaranteed initialization and termination of SNI to
            // ensure SNI terminates and cleans up properly.
            RuntimeHelpers.PrepareConstrainedRegions();
            try {} finally {
                _sniStatus = SNINativeMethodWrapper.SNIInitialize();

                UInt32 value = 0;

                // VSDevDiv 479597: If initialize fails, don't call QueryInfo.
                if (TdsEnums.SNI_SUCCESS == _sniStatus)
                {
                    // Query OS to find out whether encryption is supported.
                    SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value);
                }

                _encryptionOption = (value == 0) ? EncryptionOptions.NOT_SUP : EncryptionOptions.OFF;

                base.handle = (IntPtr)1;  // Initialize to non-zero dummy variable.
            }
        }
Ejemplo n.º 25
0
 internal override uint WaitForSSLHandShakeToComplete()
 => SNINativeMethodWrapper.SNIWaitForSSLHandshakeToComplete(Handle, GetTimeoutRemaining());
Ejemplo n.º 26
0
 internal override uint SniGetConnectionId(ref Guid clientConnectionId)
 => SNINativeMethodWrapper.SniGetConnectionId(Handle, ref clientConnectionId);
Ejemplo n.º 27
0
 // constructs SNI Handle for MARS session
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, SNIHandle parent) : base(IntPtr.Zero, true)
 {
     try { }
     finally
     {
         _status = SNINativeMethodWrapper.SNIOpenMarsSession(myInfo, parent, ref base.handle, parent._fSync);
     }
 }
Ejemplo n.º 28
0
 internal override uint EnableMars(ref uint info)
 => SNINativeMethodWrapper.SNIAddProvider(Handle, SNINativeMethodWrapper.ProviderEnum.SMUX_PROV, ref info);
Ejemplo n.º 29
0
 internal override uint DisabeSsl()
 => SNINativeMethodWrapper.SNIRemoveProvider(Handle, SNINativeMethodWrapper.ProviderEnum.SSL_PROV);
 // constructs SNI Handle for MARS session
 internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, SNIHandle parent) : base(IntPtr.Zero, true) {
     RuntimeHelpers.PrepareConstrainedRegions();
     try {} finally {          
         _status = SNINativeMethodWrapper.SNIOpenMarsSession(myInfo, parent, ref base.handle, parent._fSync);
     }
 }
Ejemplo n.º 31
0
 internal override uint EnableSsl(ref uint info)
 {
     // Add SSL (Encryption) SNI provider.
     return(SNINativeMethodWrapper.SNIAddProvider(Handle, SNINativeMethodWrapper.ProviderEnum.SSL_PROV, ref info));
 }
        // creates a physical connection
        internal SNIHandle(
            SNINativeMethodWrapper.ConsumerInfo myInfo, 
            string serverName,
            byte[] spnBuffer, 
            bool ignoreSniOpenTimeout, 
            int timeout, 
            out byte[] instanceName, 
            bool flushCache, 
            bool fSync,
            bool fParallel)
            : base(IntPtr.Zero, true) {

            RuntimeHelpers.PrepareConstrainedRegions();
            try {} finally {
                _fSync = fSync;
                instanceName = new byte[256]; // Size as specified by netlibs.
                if (ignoreSniOpenTimeout) {
                    // 


                    timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE
                }

                _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle,
                            spnBuffer, instanceName, flushCache, fSync, timeout, fParallel);
            }
        }
Ejemplo n.º 33
0
 internal override uint SetConnectionBufferSize(ref uint unsignedPacketSize)
 => SNINativeMethodWrapper.SNISetInfo(Handle, SNINativeMethodWrapper.QTypes.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
Ejemplo n.º 34
0
 internal override uint GenerateSspiClientContext(byte[] receivedBuff, uint receivedLength, ref byte[] sendBuff, ref uint sendLength, byte[] _sniSpnBuffer)
 => SNINativeMethodWrapper.SNISecGenClientContext(Handle, receivedBuff, receivedLength, sendBuff, ref sendLength, _sniSpnBuffer);
 private static extern uint SNIGetInfoWrapper([In] SNIHandle pConn, SNINativeMethodWrapper.QTypes QType, out Guid pbQInfo);
Ejemplo n.º 36
0
 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));
 }