Skip to content

Commit

Permalink
Finalizers and cleanup around (DNET-698)
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Feb 21, 2017
1 parent 011e555 commit adebcb2
Show file tree
Hide file tree
Showing 59 changed files with 1,816 additions and 3,197 deletions.
Expand Up @@ -23,35 +23,13 @@ namespace FirebirdSql.Data.Client.Managed
{
internal class FetchResponse : IResponse
{
#region Fields

private int _status;
private int _count;

#endregion

#region Properties

public int Status
{
get { return _status; }
}

public int Count
{
get { return _count; }
}

#endregion

#region Constructors
public int Status { get; }
public int Count { get; }

public FetchResponse(int status, int count)
{
_status = status;
_count = count;
Status = status;
Count = count;
}

#endregion
}
}
Expand Up @@ -30,8 +30,8 @@ namespace FirebirdSql.Data.Client.Managed
{
internal class GdsConnection
{
const ulong KeepAliveTime = 1800000; //30min
const ulong KeepAliveInterval = 1800000; //30min
const ulong KeepAliveTime = 1800000; // 30min
const ulong KeepAliveInterval = 1800000; // 30min

#region Fields

Expand Down Expand Up @@ -271,7 +271,7 @@ private IPAddress GetIPAddress(string dataSource, AddressFamily addressFamily)
IPAddress[] addresses = Dns.GetHostEntry(dataSource).AddressList;
#endif

// Try to avoid problems with IPV6 addresses
// try to avoid problems with IPv6 addresses
foreach (IPAddress address in addresses)
{
if (address.AddressFamily == addressFamily)
Expand Down
Expand Up @@ -23,49 +23,17 @@ namespace FirebirdSql.Data.Client.Managed
{
internal sealed class GenericResponse : IResponse
{
#region Fields

private int _objectHandle;
private long _blobId;
private byte[] _data;
private IscException _exception;

#endregion

#region Properties

public int ObjectHandle
{
get { return _objectHandle; }
}

public long BlobId
{
get { return _blobId; }
}

public byte[] Data
{
get { return _data; }
}

public IscException Exception
{
get { return _exception; }
}

#endregion

#region Constructors
public int ObjectHandle { get; }
public long BlobId { get; }
public byte[] Data { get; }
public IscException Exception { get; }

public GenericResponse(int objectHandle, long blobId, byte[] data, IscException exception)
{
_objectHandle = objectHandle;
_blobId = blobId;
_data = data;
_exception = exception;
ObjectHandle = objectHandle;
BlobId = blobId;
Data = data;
Exception = exception;
}

#endregion
}
}
Expand Up @@ -23,28 +23,11 @@ namespace FirebirdSql.Data.Client.Managed
{
internal class SqlResponse : IResponse
{
#region Fields

private int _count;

#endregion

#region Properties

public int Count
{
get { return _count; }
}

#endregion

#region Constructors
public int Count { get; }

public SqlResponse(int count)
{
_count = count;
Count = count;
}

#endregion
}
}
Expand Up @@ -88,19 +88,15 @@ public SecBuffer(int bufferSize)
}

public SecBuffer(byte[] secBufferBytes)
: this(secBufferBytes.Length)
{
cbBuffer = secBufferBytes.Length;
bufferType = (int)SecBufferType.SECBUFFER_TOKEN;
pvBuffer = Marshal.AllocHGlobal(cbBuffer);
Marshal.Copy(secBufferBytes, 0, pvBuffer, cbBuffer);
}

public SecBuffer(byte[] secBufferBytes, SecBufferType bufferType)
: this(secBufferBytes)
{
cbBuffer = secBufferBytes.Length;
this.bufferType = (int)bufferType;
pvBuffer = Marshal.AllocHGlobal(cbBuffer);
Marshal.Copy(secBufferBytes, 0, pvBuffer, cbBuffer);
}

public void Dispose()
Expand Down Expand Up @@ -129,7 +125,7 @@ private struct SecBufferDesc : IDisposable
{
public int ulVersion;
public int cBuffers;
public IntPtr pBuffers; //Point to SecBuffer
public IntPtr pBuffers;

public SecBufferDesc(int bufferSize)
{
Expand Down Expand Up @@ -272,9 +268,9 @@ public byte[] GetSecBufferBytes()

#region Private members

private SecHandle _clientCredentials = new SecHandle();
private SecHandle _clientContext = new SecHandle();
private bool _disposed = false;
private SecHandle _clientCredentials;
private SecHandle _clientContext;
private bool _disposed;

private string _securPackage;
private string _remotePrincipal;
Expand Down Expand Up @@ -309,10 +305,12 @@ public SspiHelper(string securityPackage, string remotePrincipal)
{
_securPackage = securityPackage;
_remotePrincipal = remotePrincipal;
_clientCredentials = new SecHandle();
SecInteger expiry = new SecInteger();
if (AcquireCredentialsHandle(null, securityPackage, SECPKG_CRED_OUTBOUND,
IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero,
out _clientCredentials, out expiry) != SEC_E_OK)
int resCode = AcquireCredentialsHandle(null, securityPackage, SECPKG_CRED_OUTBOUND,
IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero,
out _clientCredentials, out expiry);
if (resCode != SEC_E_OK)
throw new Exception($"{nameof(AcquireCredentialsHandle)} failed");
}

Expand All @@ -326,27 +324,27 @@ public SspiHelper(string securityPackage, string remotePrincipal)
/// <returns>Client authentication data to be sent to server</returns>
public byte[] InitializeClientSecurity()
{
if (_disposed)
throw new ObjectDisposedException(nameof(SspiHelper));
EnsureDisposed();
CloseClientContext();
SecInteger expiry = new SecInteger(0);
_clientContext = new SecHandle();
SecInteger expiry = new SecInteger();
uint contextAttributes;
SecBufferDesc clientTokenBuf = new SecBufferDesc(MAX_TOKEN_SIZE);
try
{
int resCode = InitializeSecurityContext(
ref _clientCredentials,
IntPtr.Zero,
_remotePrincipal,// null string pszTargetName,
_remotePrincipal,
STANDARD_CONTEXT_ATTRIBUTES,
0,//int Reserved1,
SECURITY_NATIVE_DREP,//int TargetDataRep
IntPtr.Zero, //Always zero first time around...
0, //int Reserved2,
out _clientContext, //pHandle CtxtHandle = SecHandle
ref clientTokenBuf,//ref SecBufferDesc pOutput, //PSecBufferDesc
out contextAttributes,//ref int pfContextAttr,
out expiry); //ref IntPtr ptsExpiry ); //PTimeStamp
0,
SECURITY_NATIVE_DREP,
IntPtr.Zero,
0,
out _clientContext,
ref clientTokenBuf,
out contextAttributes,
out expiry);
if (resCode != SEC_E_OK && resCode != SEC_I_CONTINUE_NEEDED)
throw new Exception($"{nameof(InitializeSecurityContext)} failed");
return clientTokenBuf.GetSecBufferBytes();
Expand All @@ -366,8 +364,7 @@ public byte[] InitializeClientSecurity()
/// <returns>Client authentication data to be sent to server</returns>
public byte[] GetClientSecurity(byte[] serverToken)
{
if (_disposed)
throw new ObjectDisposedException(nameof(SspiHelper));
EnsureDisposed();
if (_clientContext.IsInvalid)
throw new InvalidOperationException($"{nameof(InitializeClientSecurity)} not called");
SecInteger expiry = new SecInteger();
Expand All @@ -381,16 +378,16 @@ public byte[] GetClientSecurity(byte[] serverToken)
int resCode = InitializeSecurityContext(
ref _clientCredentials,
ref _clientContext,
_remotePrincipal,// null string pszTargetName,
_remotePrincipal,
STANDARD_CONTEXT_ATTRIBUTES,
0,//int Reserved1,
SECURITY_NATIVE_DREP,//int TargetDataRep
ref serverTokenBuf, // server token must be ref because it is struct
0, //int Reserved2,
out _clientContext, //pHandle CtxtHandle = SecHandle
ref clientTokenBuf,//ref SecBufferDesc pOutput, //PSecBufferDesc
out contextAttributes,//ref int pfContextAttr,
out expiry); //ref IntPtr ptsExpiry ); //PTimeStamp
0,
SECURITY_NATIVE_DREP,
ref serverTokenBuf,
0,
out _clientContext,
ref clientTokenBuf,
out contextAttributes,
out expiry);
if (resCode != SEC_E_OK && resCode != SEC_I_CONTINUE_NEEDED)
throw new Exception($"{nameof(InitializeSecurityContext)} failed");
return clientTokenBuf.GetSecBufferBytes();
Expand Down Expand Up @@ -431,37 +428,30 @@ public void Dispose()

private void Dispose(bool disposing)
{
lock (this)
if (!_disposed)
{
if (!_disposed)
{
CloseClientContext();
CloseClientCredentials();

if (disposing)
{ }

_disposed = true;
}
_disposed = true;
CloseClientContext();
CloseClientCredentials();
}
}

private void CloseClientContext()
{
if (!_clientContext.IsInvalid)
{
DeleteSecurityContext(ref _clientContext);
_clientContext = new SecHandle();
}
}

private void CloseClientCredentials()
{
if (!_clientCredentials.IsInvalid)
{
FreeCredentialsHandle(ref _clientCredentials);
_clientCredentials = new SecHandle();
}
}

private void EnsureDisposed()
{
if (_disposed)
throw new ObjectDisposedException(nameof(SspiHelper));
}

#endregion
Expand Down

0 comments on commit adebcb2

Please sign in to comment.