internal void WriteByteArray(Byte[] b, int len, int offsetBuffer, TdsParserStateObject stateObj) { int offset = offsetBuffer; while (len > 0) { if ((stateObj._outBytesUsed + len) > stateObj._outBuff.Length) { int remainder = stateObj._outBuff.Length - stateObj._outBytesUsed; Buffer.BlockCopy(b, offset, stateObj._outBuff, stateObj._outBytesUsed, remainder); offset += remainder; stateObj._outBytesUsed += remainder; if (stateObj._outBytesUsed == stateObj._outBuff.Length) { stateObj.WritePacket(0); } len -= remainder; } else { Buffer.BlockCopy(b, offset, stateObj._outBuff, stateObj._outBytesUsed, len); stateObj._outBytesUsed += len; break; } } }
internal void WriteByte(byte b, TdsParserStateObject stateObj) { if (stateObj._outBytesUsed == stateObj._outBuff.Length) { stateObj.WritePacket(0); } stateObj._outBuff[stateObj._outBytesUsed++] = b; }
internal void TdsLogin(SqlLogin rec) { _physicalStateObj.SetTimeoutSeconds(rec.timeout); byte[] encryptedPassword = null; encryptedPassword = TdsParserStaticMethods.EncryptPassword(rec.password); byte[] encryptedChangePassword = null; encryptedChangePassword = TdsParserStaticMethods.EncryptPassword(rec.newPassword); _physicalStateObj._outputMessageType = 16; int length = 0x5e; string clientInterfaceName = ".Net SqlClient Data Provider"; checked { length += (rec.hostName.Length + rec.applicationName.Length + rec.serverName.Length + clientInterfaceName.Length + rec.language.Length + rec.database.Length + rec.attachDBFilename.Length) * 2; } byte[] outSSPIBuff = null; UInt32 outSSPILength = 0; if (!rec.useSSPI) { checked { length += (rec.userName.Length * 2) + encryptedPassword.Length + encryptedChangePassword.Length; } } else { if (rec.useSSPI) { outSSPIBuff = new byte[s_maxSSPILength]; outSSPILength = s_maxSSPILength; _physicalStateObj.SniContext = SniContext.Snix_LoginSspi; SSPIData(null, 0, outSSPIBuff, ref outSSPILength); if (outSSPILength > Int32.MaxValue) { throw SQL.InvalidSSPIPacketSize(); // SqlBu 332503 } _physicalStateObj.SniContext = SniContext.Snix_Login; checked { length += (Int32)outSSPILength; } } } try { WriteInt(length, _physicalStateObj); WriteInt(0x730a0003, _physicalStateObj); WriteInt(rec.packetSize, _physicalStateObj); WriteInt(0x06000000, _physicalStateObj); WriteInt(0xa10, _physicalStateObj); WriteInt(0, _physicalStateObj); WriteByte(0xe0, _physicalStateObj); WriteByte(0x3, _physicalStateObj); WriteByte(0, _physicalStateObj); WriteByte(0, _physicalStateObj); WriteInt(0, _physicalStateObj); WriteInt(0, _physicalStateObj); int offset = 0x5e; WriteShort(offset, _physicalStateObj); WriteShort(rec.hostName.Length, _physicalStateObj); offset += rec.hostName.Length * 2; if (rec.useSSPI == false) { WriteShort(offset, _physicalStateObj); WriteShort(rec.userName.Length, _physicalStateObj); offset += rec.userName.Length * 2; WriteShort(offset, _physicalStateObj); WriteShort(encryptedPassword.Length / 2, _physicalStateObj); offset += encryptedPassword.Length; } else { WriteShort(0, _physicalStateObj); WriteShort(0, _physicalStateObj); WriteShort(0, _physicalStateObj); WriteShort(0, _physicalStateObj); } WriteShort(offset, _physicalStateObj); WriteShort(rec.applicationName.Length, _physicalStateObj); offset += rec.applicationName.Length * 2; WriteShort(offset, _physicalStateObj); WriteShort(rec.serverName.Length, _physicalStateObj); offset += rec.serverName.Length * 2; WriteShort(offset, _physicalStateObj); WriteShort(0, _physicalStateObj); WriteShort(offset, _physicalStateObj); WriteShort(clientInterfaceName.Length, _physicalStateObj); offset += clientInterfaceName.Length * 2; WriteShort(offset, _physicalStateObj); WriteShort(rec.language.Length, _physicalStateObj); offset += rec.language.Length * 2; WriteShort(offset, _physicalStateObj); WriteShort(rec.database.Length, _physicalStateObj); offset += rec.database.Length * 2; if (null == s_nicAddress) { s_nicAddress = TdsParserStaticMethods.GetNetworkPhysicalAddressForTdsLoginOnly(); } WriteByteArray(s_nicAddress, s_nicAddress.Length, 0, _physicalStateObj); WriteShort(offset, _physicalStateObj); if (rec.useSSPI) { WriteShort((int)outSSPILength, _physicalStateObj); offset += (int)outSSPILength; } else { WriteShort(0, _physicalStateObj); } WriteShort(offset, _physicalStateObj); WriteShort(rec.attachDBFilename.Length, _physicalStateObj); offset += rec.attachDBFilename.Length * 2; WriteShort(offset, _physicalStateObj); WriteShort(encryptedChangePassword.Length / 2, _physicalStateObj); WriteInt(0, _physicalStateObj); WriteString(rec.hostName, _physicalStateObj); if (!rec.useSSPI) { WriteString(rec.userName, _physicalStateObj); _physicalStateObj._tracePasswordOffset = _physicalStateObj._outBytesUsed; _physicalStateObj._tracePasswordLength = encryptedPassword.Length; WriteByteArray(encryptedPassword, encryptedPassword.Length, 0, _physicalStateObj); } WriteString(rec.applicationName, _physicalStateObj); WriteString(rec.serverName, _physicalStateObj); WriteString(clientInterfaceName, _physicalStateObj); WriteString(rec.language, _physicalStateObj); WriteString(rec.database, _physicalStateObj); if (rec.useSSPI) { WriteByteArray(outSSPIBuff, (int)outSSPILength, 0, _physicalStateObj); } WriteString(rec.attachDBFilename, _physicalStateObj); if (!rec.useSSPI) { _physicalStateObj._traceChangePasswordOffset = _physicalStateObj._outBytesUsed; _physicalStateObj._traceChangePasswordLength = encryptedChangePassword.Length; WriteByteArray(encryptedChangePassword, encryptedChangePassword.Length, 0, _physicalStateObj); } } catch (Exception e) { if (ADP.IsCatchableExceptionType(e)) { _physicalStateObj._outputPacketNumber = 1; _physicalStateObj.ResetBuffer(); } throw; } _physicalStateObj.WritePacket(1); _physicalStateObj._pendingData = true; return; }