public static IPHostEntry GetHostByAddress(string address) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null, address); } NameResolutionPal.EnsureSocketsAreInitialized(); if (address == null) { throw new ArgumentNullException(nameof(address)); } IPHostEntry ipHostEntry = InternalGetHostByAddress(IPAddress.Parse(address)); if (NetEventSource.IsEnabled) { NetEventSource.Exit(null, ipHostEntry); } return(ipHostEntry); } // GetHostByAddress
public static IPHostEntry GetHostByAddress(IPAddress address) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(address, address); } NameResolutionPal.EnsureSocketsAreInitialized(); if (address is null) { throw new ArgumentNullException(nameof(address)); } IPHostEntry ipHostEntry = GetHostEntryCore(address); if (NetEventSource.IsEnabled) { NetEventSource.Exit(address, ipHostEntry); } return(ipHostEntry); }
public static IPHostEntry GetHostEntry(string hostNameOrAddress) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress); } NameResolutionPal.EnsureSocketsAreInitialized(); if (hostNameOrAddress is null) { throw new ArgumentNullException(nameof(hostNameOrAddress)); } // See if it's an IP Address. IPHostEntry ipHostEntry; if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address)) { if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)) { if (NetEventSource.IsEnabled) { NetEventSource.Error(address, $"Invalid address '{address}'"); } throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress))); } ipHostEntry = GetHostEntryCore(address); } else { ipHostEntry = GetHostEntryCore(hostNameOrAddress); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(hostNameOrAddress, $"{ipHostEntry} with {ipHostEntry.AddressList.Length} entries"); } return(ipHostEntry); }
public void Abort() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } lock (_internalLock) { try { if (_state == State.Closed) { return; } // Just detach and free resources. Don't call Stop (which may throw). if (_state == State.Started) { Close(true); } } catch (Exception exception) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, $"Abort {exception}"); } throw; } finally { _state = State.Closed; if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } } }
public static IPAddress[] GetHostAddresses(string hostNameOrAddress) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress); } NameResolutionPal.EnsureSocketsAreInitialized(); if (hostNameOrAddress is null) { throw new ArgumentNullException(nameof(hostNameOrAddress)); } // See if it's an IP Address. IPAddress[] addresses; if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address)) { if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)) { if (NetEventSource.IsEnabled) { NetEventSource.Error(address, $"Invalid address '{address}'"); } throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress))); } addresses = new IPAddress[] { address }; } else { addresses = GetHostAddressesCore(hostNameOrAddress); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(hostNameOrAddress, addresses); } return(addresses); }
public static IPHostEntry GetHostByAddress(IPAddress address) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostByAddress", ""); } NameResolutionPal.EnsureSocketsAreInitialized(); if (address == null) { throw new ArgumentNullException(nameof(address)); } IPHostEntry ipHostEntry = InternalGetHostByAddress(address, false); if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostByAddress", ipHostEntry); } return(ipHostEntry); } // GetHostByAddress
public void Close() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { if (NetEventSource.IsEnabled) { NetEventSource.Info(this); } ((IDisposable)this).Dispose(); } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } }
public X509Certificate2 EndGetClientCertificate(IAsyncResult asyncResult) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } X509Certificate2 clientCertificate = null; try { if (asyncResult == null) { throw new ArgumentNullException(nameof(asyncResult)); } ListenerClientCertAsyncResult clientCertAsyncResult = asyncResult as ListenerClientCertAsyncResult; if (clientCertAsyncResult == null || clientCertAsyncResult.AsyncObject != this) { throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult)); } if (clientCertAsyncResult.EndCalled) { throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, nameof(EndGetClientCertificate))); } clientCertAsyncResult.EndCalled = true; clientCertificate = clientCertAsyncResult.InternalWaitForCompletion() as X509Certificate2; if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"_clientCertificate:{_clientCertificate}"); } } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } return(clientCertificate); }
} // GetHostEntry public static IPAddress[] GetHostAddresses(string hostNameOrAddress) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostAddresses", hostNameOrAddress); } NameResolutionPal.EnsureSocketsAreInitialized(); if (hostNameOrAddress == null) { throw new ArgumentNullException(nameof(hostNameOrAddress)); } // See if it's an IP Address. IPAddress address; IPAddress[] addresses; if (IPAddress.TryParse(hostNameOrAddress, out address)) { if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)) { throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress))); } addresses = new IPAddress[] { address }; } else { // InternalGetHostByName works with IP addresses (and avoids a reverse-lookup), but we need // explicit handling in order to do the ArgumentException and guarantee the behavior. addresses = InternalGetHostByName(hostNameOrAddress, true).AddressList; } if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostAddresses", addresses); } return(addresses); }
public void Start() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } lock (_internalLock) { try { CheckDisposed(); if (_state == State.Started) { return; } HttpEndPointManager.AddListener(this); _state = State.Started; } catch (Exception exception) { _state = State.Closed; if (NetEventSource.IsEnabled) { NetEventSource.Error(this, $"Start {exception}"); } throw; } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } } }
} // EndGetHostByName() public static IPHostEntry GetHostEntry(string hostNameOrAddress) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostEntry", hostNameOrAddress); } NameResolutionPal.EnsureSocketsAreInitialized(); if (hostNameOrAddress == null) { throw new ArgumentNullException(nameof(hostNameOrAddress)); } // See if it's an IP Address. IPAddress address; IPHostEntry ipHostEntry; if (IPAddress.TryParse(hostNameOrAddress, out address)) { if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)) { throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress))); } ipHostEntry = InternalGetHostByAddress(address, true); } else { ipHostEntry = InternalGetHostByName(hostNameOrAddress, true); } if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostEntry", ipHostEntry); } return(ipHostEntry); }
public static IPHostEntry EndResolve(IAsyncResult asyncResult) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null, asyncResult); } IPHostEntry ipHostEntry; try { ipHostEntry = TaskToApm.End <IPHostEntry>(asyncResult); } catch (SocketException ex) { IPAddress?address = asyncResult switch { Task t => t.AsyncState as IPAddress, TaskToApm.TaskAsyncResult twar => twar._task.AsyncState as IPAddress, _ => null }; if (address is null) { throw; // BeginResolve was called with a HostName, not an IPAddress } if (NetEventSource.IsEnabled) { NetEventSource.Error(null, ex); } ipHostEntry = CreateHostEntryForAddress(address); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(null, ipHostEntry); } return(ipHostEntry); }
protected override void Dispose(bool disposing) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "_closed:" + _closed); } _closed = true; } finally { base.Dispose(disposing); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } }
public HttpListener() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } _state = State.Stopped; _internalLock = new object(); _defaultServiceNames = new ServiceNameStore(); _timeoutManager = new HttpListenerTimeoutManager(this); _prefixes = new HttpListenerPrefixCollection(this); // default: no CBT checks on any platform (appcompat reasons); applies also to PolicyEnforcement // config element _extendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Never); if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } }
public X509Certificate2 GetClientCertificate() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { ProcessClientCertificate(); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"_clientCertificate:{_clientCertificate}"); } } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } return(_clientCertificate); }
public void Stop() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { lock (_internalLock) { CheckDisposed(); if (_state == State.Stopped) { return; } Close(false); _state = State.Stopped; } } catch (Exception exception) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, $"Stop {exception}"); } throw; } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } }
private void Dispose() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } lock (_internalLock) { try { if (_state == State.Closed) { return; } Close(true); } catch (Exception exception) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, $"Dispose {exception}"); } throw; } finally { _state = State.Closed; if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } } }
private static IPHostEntry InternalGetHostByName(string hostName) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null, hostName); } IPHostEntry ipHostEntry = null; ValidateHostName(hostName); int nativeErrorCode; SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, out ipHostEntry, out nativeErrorCode); if (errorCode != SocketError.Success) { throw SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(null, ipHostEntry); } return(ipHostEntry); } // GetHostByName
public void Abort() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { if (_responseState >= ResponseState.Closed) { return; } _responseState = ResponseState.Closed; HttpListenerContext.Abort(); } finally { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } } }
private static X509Certificate2?GetRemoteCertificate(SafeDeleteContext?securityContext, X509Certificate2Collection?remoteCertificateStore) { bool gotReference = false; if (securityContext == null) { return(null); } if (NetEventSource.IsEnabled) { NetEventSource.Enter(securityContext); } X509Certificate2? result = null; SafeFreeCertContext?remoteContext = null; try { int errorCode = QueryContextRemoteCertificate(securityContext, out remoteContext); if (remoteContext != null && !remoteContext.IsInvalid) { remoteContext.DangerousAddRef(ref gotReference); result = new X509Certificate2(remoteContext.DangerousGetHandle()); } if (remoteCertificateStore != null) { using (SafeSharedX509StackHandle chainStack = Interop.OpenSsl.GetPeerCertificateChain(((SafeDeleteSslContext)securityContext).SslContext)) { if (!chainStack.IsInvalid) { int count = Interop.Crypto.GetX509StackFieldCount(chainStack); for (int i = 0; i < count; i++) { IntPtr certPtr = Interop.Crypto.GetX509StackField(chainStack, i); if (certPtr != IntPtr.Zero) { // X509Certificate2(IntPtr) calls X509_dup, so the reference is appropriately tracked. X509Certificate2 chainCert = new X509Certificate2(certPtr); remoteCertificateStore.Add(chainCert); } } } } } } catch { result?.Dispose(); throw; } finally { if (remoteContext != null) { if (gotReference) { remoteContext.DangerousRelease(); } remoteContext.Dispose(); } } if (NetEventSource.IsEnabled) { NetEventSource.Log.RemoteCertificate(result); NetEventSource.Exit(securityContext, result); } return(result); }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException(nameof(size)); } if (size == 0 || _closed) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } HttpRequestStreamAsyncResult result = new HttpRequestStreamAsyncResult(this, state, callback); result.InvokeCallback((uint)0); return(result); } HttpRequestStreamAsyncResult asyncResult = null; uint dataRead = 0; if (_dataChunkIndex != -1) { dataRead = Interop.HttpApi.GetChunks(_httpContext.Request.RequestBuffer, _httpContext.Request.OriginalBlobAddress, ref _dataChunkIndex, ref _dataChunkOffset, buffer, offset, size); if (_dataChunkIndex != -1 && dataRead == size) { asyncResult = new HttpRequestStreamAsyncResult(_httpContext.RequestQueueBoundHandle, this, state, callback, buffer, offset, (uint)size, 0); asyncResult.InvokeCallback(dataRead); } } if (_dataChunkIndex == -1 && dataRead < size) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "size:" + size + " offset:" + offset); } uint statusCode = 0; offset += (int)dataRead; size -= (int)dataRead; //the http.sys team recommends that we limit the size to 128kb if (size > MaxReadSize) { size = MaxReadSize; } asyncResult = new HttpRequestStreamAsyncResult(_httpContext.RequestQueueBoundHandle, this, state, callback, buffer, offset, (uint)size, dataRead); uint bytesReturned; try { fixed(byte *pBuffer = buffer) { // issue unmanaged blocking call if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Calling Interop.HttpApi.HttpReceiveRequestEntityBody"); } uint flags = 0; if (!_inOpaqueMode) { flags = (uint)Interop.HttpApi.HTTP_FLAGS.HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY; } statusCode = Interop.HttpApi.HttpReceiveRequestEntityBody( _httpContext.RequestQueueHandle, _httpContext.RequestId, flags, asyncResult._pPinnedBuffer, (uint)size, out bytesReturned, asyncResult._pOverlapped); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Call to Interop.HttpApi.HttpReceiveRequestEntityBody returned:" + statusCode + " dataRead:" + dataRead); } } } catch (Exception e) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, e.ToString()); } asyncResult.InternalCleanup(); throw; } if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_IO_PENDING) { asyncResult.InternalCleanup(); if (statusCode == Interop.HttpApi.ERROR_HANDLE_EOF) { asyncResult = new HttpRequestStreamAsyncResult(this, state, callback, dataRead); asyncResult.InvokeCallback((uint)0); } else { Exception exception = new HttpListenerException((int)statusCode); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, exception.ToString()); } asyncResult.InternalCleanup(); throw exception; } } else if (statusCode == Interop.HttpApi.ERROR_SUCCESS && HttpListener.SkipIOCPCallbackOnSuccess) { // IO operation completed synchronously - callback won't be called to signal completion. asyncResult.IOCompleted(statusCode, bytesReturned); } } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } return(asyncResult); }
public override int Read([In, Out] byte[] buffer, int offset, int size) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); NetEventSource.Info(this, "size:" + size + " offset:" + offset); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException(nameof(size)); } if (size == 0 || _closed) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this, "dataRead:0"); } return(0); } uint dataRead = 0; if (_dataChunkIndex != -1) { dataRead = Interop.HttpApi.GetChunks(_httpContext.Request.RequestBuffer, _httpContext.Request.OriginalBlobAddress, ref _dataChunkIndex, ref _dataChunkOffset, buffer, offset, size); } if (_dataChunkIndex == -1 && dataRead < size) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "size:" + size + " offset:" + offset); } uint statusCode = 0; uint extraDataRead = 0; offset += (int)dataRead; size -= (int)dataRead; //the http.sys team recommends that we limit the size to 128kb if (size > MaxReadSize) { size = MaxReadSize; } fixed(byte *pBuffer = buffer) { // issue unmanaged blocking call if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Calling Interop.HttpApi.HttpReceiveRequestEntityBody"); } uint flags = 0; if (!_inOpaqueMode) { flags = (uint)Interop.HttpApi.HTTP_FLAGS.HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY; } statusCode = Interop.HttpApi.HttpReceiveRequestEntityBody( _httpContext.RequestQueueHandle, _httpContext.RequestId, flags, (void *)(pBuffer + offset), (uint)size, out extraDataRead, null); dataRead += extraDataRead; if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Call to Interop.HttpApi.HttpReceiveRequestEntityBody returned:" + statusCode + " dataRead:" + dataRead); } } if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_HANDLE_EOF) { Exception exception = new HttpListenerException((int)statusCode); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, exception.ToString()); } throw exception; } UpdateAfterRead(statusCode, dataRead); } if (NetEventSource.IsEnabled) { NetEventSource.DumpBuffer(this, buffer, offset, (int)dataRead); NetEventSource.Info(this, "returning dataRead:" + dataRead); NetEventSource.Exit(this, "dataRead:" + dataRead); } return((int)dataRead); }
// Accepts an incoming binary security blob and returns an outgoing binary security blob. internal byte[] GetOutgoingBlob(byte[] incomingBlob, bool throwOnError, out SecurityStatusPal statusCode) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this, incomingBlob); } SecurityBuffer[] inSecurityBufferArray = null; if (incomingBlob != null && _channelBinding != null) { inSecurityBufferArray = new SecurityBuffer[2] { new SecurityBuffer(incomingBlob, SecurityBufferType.SECBUFFER_TOKEN), new SecurityBuffer(_channelBinding) }; } else if (incomingBlob != null) { inSecurityBufferArray = new SecurityBuffer[1] { new SecurityBuffer(incomingBlob, SecurityBufferType.SECBUFFER_TOKEN) }; } else if (_channelBinding != null) { inSecurityBufferArray = new SecurityBuffer[1] { new SecurityBuffer(_channelBinding) }; } var outSecurityBuffer = new SecurityBuffer(_tokenSize, SecurityBufferType.SECBUFFER_TOKEN); bool firstTime = _securityContext == null; try { if (!_isServer) { // client session statusCode = NegotiateStreamPal.InitializeSecurityContext( _credentialsHandle, ref _securityContext, _spn, _requestedContextFlags, inSecurityBufferArray, outSecurityBuffer, ref _contextFlags); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"SSPIWrapper.InitializeSecurityContext() returns statusCode:0x{((int)statusCode.ErrorCode):x8} ({statusCode})"); } if (statusCode.ErrorCode == SecurityStatusPalErrorCode.CompleteNeeded) { var inSecurityBuffers = new SecurityBuffer[1]; inSecurityBuffers[0] = outSecurityBuffer; statusCode = NegotiateStreamPal.CompleteAuthToken(ref _securityContext, inSecurityBuffers); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"SSPIWrapper.CompleteAuthToken() returns statusCode:0x{((int)statusCode.ErrorCode):x8} ({statusCode})"); } outSecurityBuffer.token = null; } } else { // Server session. statusCode = NegotiateStreamPal.AcceptSecurityContext( _credentialsHandle, ref _securityContext, _requestedContextFlags, inSecurityBufferArray, outSecurityBuffer, ref _contextFlags); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"SSPIWrapper.AcceptSecurityContext() returns statusCode:0x{((int)statusCode.ErrorCode):x8} ({statusCode})"); } } } finally { // // Assuming the ISC or ASC has referenced the credential on the first successful call, // we want to decrement the effective ref count by "disposing" it. // The real dispose will happen when the security context is closed. // Note if the first call was not successful the handle is physically destroyed here. // if (firstTime && _credentialsHandle != null) { _credentialsHandle.Dispose(); } } if (((int)statusCode.ErrorCode >= (int)SecurityStatusPalErrorCode.OutOfMemory)) { CloseContext(); _isCompleted = true; if (throwOnError) { Exception exception = NegotiateStreamPal.CreateExceptionFromError(statusCode); if (NetEventSource.IsEnabled) { NetEventSource.Exit(this, exception); } throw exception; } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this, $"null statusCode:0x{((int)statusCode.ErrorCode):x8} ({statusCode})"); } return(null); } else if (firstTime && _credentialsHandle != null) { // Cache until it is pushed out by newly incoming handles. SSPIHandleCache.CacheCredential(_credentialsHandle); } // The return value will tell us correctly if the handshake is over or not if (statusCode.ErrorCode == SecurityStatusPalErrorCode.OK) { // Success. _isCompleted = true; } else if (NetEventSource.IsEnabled) { // We need to continue. if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"need continue statusCode:0x{((int)statusCode.ErrorCode):x8} ({statusCode}) _securityContext:{_securityContext}"); } } if (NetEventSource.IsEnabled) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this, $"IsCompleted: {IsCompleted}"); } } return(outSecurityBuffer.token); }
public override void Write(byte[] buffer, int offset, int size) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); NetEventSource.Info(this, "buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException(nameof(size)); } Interop.HttpApi.HTTP_FLAGS flags = ComputeLeftToWrite(); if (_closed || (size == 0 && _leftToWrite != 0)) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } return; } if (_leftToWrite >= 0 && size > _leftToWrite) { throw new ProtocolViolationException(SR.net_entitytoobig); } uint statusCode; uint dataToWrite = (uint)size; SafeLocalAllocHandle bufferAsIntPtr = null; IntPtr pBufferAsIntPtr = IntPtr.Zero; bool sentHeaders = _httpContext.Response.SentHeaders; try { if (size == 0) { statusCode = _httpContext.Response.SendHeaders(null, null, flags, false); } else { fixed(byte *pDataBuffer = buffer) { byte *pBuffer = pDataBuffer; if (_httpContext.Response.BoundaryType == BoundaryType.Chunked) { string chunkHeader = size.ToString("x", CultureInfo.InvariantCulture); dataToWrite = dataToWrite + (uint)(chunkHeader.Length + 4); bufferAsIntPtr = SafeLocalAllocHandle.LocalAlloc((int)dataToWrite); pBufferAsIntPtr = bufferAsIntPtr.DangerousGetHandle(); for (int i = 0; i < chunkHeader.Length; i++) { Marshal.WriteByte(pBufferAsIntPtr, i, (byte)chunkHeader[i]); } Marshal.WriteInt16(pBufferAsIntPtr, chunkHeader.Length, 0x0A0D); Marshal.Copy(buffer, offset, pBufferAsIntPtr + chunkHeader.Length + 2, size); Marshal.WriteInt16(pBufferAsIntPtr, (int)(dataToWrite - 2), 0x0A0D); pBuffer = (byte *)pBufferAsIntPtr; offset = 0; } Interop.HttpApi.HTTP_DATA_CHUNK dataChunk = new Interop.HttpApi.HTTP_DATA_CHUNK(); dataChunk.DataChunkType = Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory; dataChunk.pBuffer = (byte *)(pBuffer + offset); dataChunk.BufferLength = dataToWrite; flags |= _leftToWrite == size ? Interop.HttpApi.HTTP_FLAGS.NONE : Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA; if (!sentHeaders) { statusCode = _httpContext.Response.SendHeaders(&dataChunk, null, flags, false); } else { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Calling Interop.HttpApi.HttpSendResponseEntityBody"); } statusCode = Interop.HttpApi.HttpSendResponseEntityBody( _httpContext.RequestQueueHandle, _httpContext.RequestId, (uint)flags, 1, &dataChunk, null, SafeLocalAllocHandle.Zero, 0, null, null); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Call to Interop.HttpApi.HttpSendResponseEntityBody returned:" + statusCode); } if (_httpContext.Listener.IgnoreWriteExceptions) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Write() suppressing error"); } statusCode = Interop.HttpApi.ERROR_SUCCESS; } } } } } finally { // free unmanaged buffer bufferAsIntPtr?.Close(); } if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_HANDLE_EOF) { Exception exception = new HttpListenerException((int)statusCode); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, exception.ToString()); } _closed = true; _httpContext.Abort(); throw exception; } UpdateAfterWrite(dataToWrite); if (NetEventSource.IsEnabled) { NetEventSource.DumpBuffer(this, buffer, offset, (int)dataToWrite); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } }
public static object QueryContextAttributes(SSPIInterface secModule, SafeDeleteContext securityContext, Interop.SspiCli.ContextAttribute contextAttribute, out int errorCode) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null, contextAttribute); } int nativeBlockSize = IntPtr.Size; Type handleType = null; switch (contextAttribute) { case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES: nativeBlockSize = SecPkgContext_Sizes.SizeOf; break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_STREAM_SIZES: nativeBlockSize = SecPkgContext_StreamSizes.SizeOf; break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES: handleType = typeof(SafeFreeContextBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_PACKAGE_INFO: handleType = typeof(SafeFreeContextBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO: handleType = typeof(SafeFreeContextBuffer); unsafe { nativeBlockSize = sizeof(SecPkgContext_NegotiationInfoW); } break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET: handleType = typeof(SafeFreeContextBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT: handleType = typeof(SafeFreeCertContext); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_CERT_CONTEXT: handleType = typeof(SafeFreeCertContext); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX: nativeBlockSize = Marshal.SizeOf <Interop.SspiCli.SecPkgContext_IssuerListInfoEx>(); handleType = typeof(SafeFreeContextBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CONNECTION_INFO: nativeBlockSize = Marshal.SizeOf <SecPkgContext_ConnectionInfo>(); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_APPLICATION_PROTOCOL: nativeBlockSize = Marshal.SizeOf <Interop.SecPkgContext_ApplicationProtocol>(); break; default: throw new ArgumentException(System.StringsHelper.Format(Strings.net_invalid_enum, nameof(contextAttribute)), nameof(contextAttribute)); } SafeHandle sspiHandle = null; object attribute = null; try { var nativeBuffer = new byte[nativeBlockSize]; errorCode = secModule.QueryContextAttributes(securityContext, contextAttribute, nativeBuffer, handleType, out sspiHandle); if (errorCode != 0) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(null, $"ERROR = {ErrorDescription(errorCode)}"); } return(null); } switch (contextAttribute) { case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES: attribute = new SecPkgContext_Sizes(nativeBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_STREAM_SIZES: attribute = new SecPkgContext_StreamSizes(nativeBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES: attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle()); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_PACKAGE_INFO: attribute = new SecurityPackageInfoClass(sspiHandle, 0); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO: unsafe { fixed(void *ptr = &nativeBuffer[0]) { attribute = new NegotiationInfoClass(sspiHandle, (int)((SecPkgContext_NegotiationInfoW *)ptr)->NegotiationState); } } break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET: attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle()); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_CERT_CONTEXT: // Fall-through to RemoteCertificate is intentional. case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT: attribute = sspiHandle; sspiHandle = null; break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX: attribute = new Interop.SspiCli.SecPkgContext_IssuerListInfoEx(sspiHandle, nativeBuffer); sspiHandle = null; break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CONNECTION_INFO: attribute = new SecPkgContext_ConnectionInfo(nativeBuffer); break; case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_APPLICATION_PROTOCOL: unsafe { fixed(void *ptr = nativeBuffer) { attribute = Marshal.PtrToStructure <Interop.SecPkgContext_ApplicationProtocol>(new IntPtr(ptr)); } } break; default: // Will return null. break; } } finally { if (sspiHandle != null) { sspiHandle.Dispose(); } } if (NetEventSource.IsEnabled) { NetEventSource.Exit(null, attribute); } return(attribute); }
private static byte[] GetChunkHeader(int size, out int offset) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null, $"size:{size}"); } uint Mask = 0xf0000000; byte[] Header = new byte[10]; int i; offset = -1; // // Loop through the size, looking at each nibble. If it's not 0 // convert it to hex. Save the index of the first non-zero // byte. // for (i = 0; i < 8; i++, size <<= 4) { // // offset == -1 means that we haven't found a non-zero nibble // yet. If we haven't found one, and the current one is zero, // don't do anything. // if (offset == -1) { if ((size & Mask) == 0) { continue; } } // // Either we have a non-zero nibble or we're no longer skipping // leading zeros. Convert this nibble to ASCII and save it. // uint Temp = (uint)size >> 28; if (Temp < 10) { Header[i] = (byte)(Temp + '0'); } else { Header[i] = (byte)((Temp - 10) + 'A'); } // // If we haven't found a non-zero nibble yet, we've found one // now, so remember that. // if (offset == -1) { offset = i; } } Header[8] = (byte)'\r'; Header[9] = (byte)'\n'; if (NetEventSource.IsEnabled) { NetEventSource.Exit(null); } return(Header); }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "buffer.Length:" + buffer.Length + " size:" + size + " offset:" + offset); } if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException(nameof(size)); } Interop.HttpApi.HTTP_FLAGS flags = ComputeLeftToWrite(); if (_closed || (size == 0 && _leftToWrite != 0)) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } HttpResponseStreamAsyncResult result = new HttpResponseStreamAsyncResult(this, state, callback); result.InvokeCallback((uint)0); return(result); } if (_leftToWrite >= 0 && size > _leftToWrite) { throw new ProtocolViolationException(SR.net_entitytoobig); } uint statusCode; uint bytesSent = 0; flags |= _leftToWrite == size ? Interop.HttpApi.HTTP_FLAGS.NONE : Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA; bool sentHeaders = _httpContext.Response.SentHeaders; HttpResponseStreamAsyncResult asyncResult = new HttpResponseStreamAsyncResult(this, state, callback, buffer, offset, size, _httpContext.Response.BoundaryType == BoundaryType.Chunked, sentHeaders, _httpContext.RequestQueueBoundHandle); // Update m_LeftToWrite now so we can queue up additional BeginWrite's without waiting for EndWrite. UpdateAfterWrite((uint)((_httpContext.Response.BoundaryType == BoundaryType.Chunked) ? 0 : size)); try { if (!sentHeaders) { statusCode = _httpContext.Response.SendHeaders(null, asyncResult, flags, false); } else { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Calling Interop.HttpApi.HttpSendResponseEntityBody"); } statusCode = Interop.HttpApi.HttpSendResponseEntityBody( _httpContext.RequestQueueHandle, _httpContext.RequestId, (uint)flags, asyncResult.dataChunkCount, asyncResult.pDataChunks, &bytesSent, SafeLocalAllocHandle.Zero, 0, asyncResult._pOverlapped, null); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Call to Interop.HttpApi.HttpSendResponseEntityBody returned:" + statusCode); } } } catch (Exception e) { if (NetEventSource.IsEnabled) { NetEventSource.Error(this, e.ToString()); } asyncResult.InternalCleanup(); _closed = true; _httpContext.Abort(); throw; } if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_IO_PENDING) { asyncResult.InternalCleanup(); if (_httpContext.Listener.IgnoreWriteExceptions && sentHeaders) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "BeginWrite() Suppressing error"); } } else { Exception exception = new HttpListenerException((int)statusCode); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, exception.ToString()); } _closed = true; _httpContext.Abort(); throw exception; } } if (statusCode == Interop.HttpApi.ERROR_SUCCESS && HttpListener.SkipIOCPCallbackOnSuccess) { // IO operation completed synchronously - callback won't be called to signal completion. asyncResult.IOCompleted(statusCode, bytesSent); } // Last write, cache it for special cancelation handling. if ((flags & Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA) == 0) { _lastWrite = asyncResult; } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } return(asyncResult); }
// Create a WebRequest. // // This is the main creation routine. We take a Uri object, look // up the Uri in the prefix match table, and invoke the appropriate // handler to create the object. We also have a parameter that // tells us whether or not to use the whole Uri or just the // scheme portion of it. // // Input: // requestUri - Uri object for request. // useUriBase - True if we're only to look at the scheme portion of the Uri. // // Returns: // Newly created WebRequest. private static WebRequest Create(Uri requestUri, bool useUriBase) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Enter(NetEventSource.ComponentType.Requests, "WebRequest", "Create", requestUri.ToString()); } string LookupUri; WebRequestPrefixElement Current = null; bool Found = false; if (!useUriBase) { LookupUri = requestUri.AbsoluteUri; } else { // schemes are registered as <schemeName>":", so add the separator // to the string returned from the Uri object LookupUri = requestUri.Scheme + ':'; } int LookupLength = LookupUri.Length; // Copy the prefix list so that if it is updated it will // not affect us on this thread. List <WebRequestPrefixElement> prefixList = PrefixList; // Look for the longest matching prefix. // Walk down the list of prefixes. The prefixes are kept longest // first. When we find a prefix that is shorter or the same size // as this Uri, we'll do a compare to see if they match. If they // do we'll break out of the loop and call the creator. for (int i = 0; i < prefixList.Count; i++) { Current = prefixList[i]; // See if this prefix is short enough. if (LookupLength >= Current.Prefix.Length) { // It is. See if these match. if (String.Compare(Current.Prefix, 0, LookupUri, 0, Current.Prefix.Length, StringComparison.OrdinalIgnoreCase) == 0) { // These match. Remember that we found it and break // out. Found = true; break; } } } WebRequest webRequest = null; if (Found) { // We found a match, so just call the creator and return what it does. webRequest = Current.Creator.Create(requestUri); if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exit(NetEventSource.ComponentType.Requests, "WebRequest", "Create", webRequest); } return(webRequest); } if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exit(NetEventSource.ComponentType.Requests, "WebRequest", "Create", null); } // Otherwise no match, throw an exception. throw new NotSupportedException(SR.net_unknown_prefix); }
private object WaitForCompletion(bool snap) { ManualResetEvent waitHandle = null; bool createdByMe = false; bool complete = snap ? IsCompleted : InternalPeekCompleted; if (!complete) { // Not done yet, so wait: waitHandle = (ManualResetEvent)_event; if (waitHandle == null) { createdByMe = LazilyCreateEvent(out waitHandle); } } if (waitHandle != null) { try { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, $"Waiting for completion event {waitHandle}"); } waitHandle.WaitOne(Timeout.Infinite); } catch (ObjectDisposedException) { // This can occur if this method is called from two different threads. // This possibility is the trade-off for not locking. } finally { // We also want to dispose the event although we can't unless we did wait on it here. if (createdByMe && !_userEvent) { // Does _userEvent need to be volatile (or _event set via Interlocked) in order // to avoid giving a user a disposed event? ManualResetEvent oldEvent = (ManualResetEvent)_event; _event = null; if (!_userEvent) { oldEvent.Dispose(); } } } } // A race condition exists because InvokeCallback sets _intCompleted before _result (so that _result // can benefit from the synchronization of _intCompleted). That means you can get here before _result got // set (although rarely - once every eight hours of stress). Handle that case with a spin-lock. SpinWait sw = new SpinWait(); while (_result == DBNull.Value) { sw.SpinOnce(); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this, _result); } return(_result); }
protected override void Dispose(bool disposing) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(this); } try { if (disposing) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "_closed:" + _closed); } if (_closed) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } return; } _closed = true; Interop.HttpApi.HTTP_FLAGS flags = ComputeLeftToWrite(); if (_leftToWrite > 0 && !_inOpaqueMode) { throw new InvalidOperationException(SR.net_io_notenoughbyteswritten); } bool sentHeaders = _httpContext.Response.SentHeaders; if (sentHeaders && _leftToWrite == 0) { if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } return; } uint statusCode = 0; if ((_httpContext.Response.BoundaryType == BoundaryType.Chunked || _httpContext.Response.BoundaryType == BoundaryType.None) && (String.Compare(_httpContext.Request.HttpMethod, "HEAD", StringComparison.OrdinalIgnoreCase) != 0)) { if (_httpContext.Response.BoundaryType == BoundaryType.None) { flags |= Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_DISCONNECT; } fixed(void *pBuffer = ChunkTerminator) { Interop.HttpApi.HTTP_DATA_CHUNK *pDataChunk = null; if (_httpContext.Response.BoundaryType == BoundaryType.Chunked) { Interop.HttpApi.HTTP_DATA_CHUNK dataChunk = new Interop.HttpApi.HTTP_DATA_CHUNK(); dataChunk.DataChunkType = Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory; dataChunk.pBuffer = (byte *)pBuffer; dataChunk.BufferLength = (uint)ChunkTerminator.Length; pDataChunk = &dataChunk; } if (!sentHeaders) { statusCode = _httpContext.Response.SendHeaders(pDataChunk, null, flags, false); } else { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Calling Interop.HttpApi.HttpSendResponseEntityBody"); } statusCode = Interop.HttpApi.HttpSendResponseEntityBody( _httpContext.RequestQueueHandle, _httpContext.RequestId, (uint)flags, pDataChunk != null ? (ushort)1 : (ushort)0, pDataChunk, null, SafeLocalAllocHandle.Zero, 0, null, null); if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Call to Interop.HttpApi.HttpSendResponseEntityBody returned:" + statusCode); } if (_httpContext.Listener.IgnoreWriteExceptions) { if (NetEventSource.IsEnabled) { NetEventSource.Info(this, "Suppressing error"); } statusCode = Interop.HttpApi.ERROR_SUCCESS; } } } } else { if (!sentHeaders) { statusCode = _httpContext.Response.SendHeaders(null, null, flags, false); } } if (statusCode != Interop.HttpApi.ERROR_SUCCESS && statusCode != Interop.HttpApi.ERROR_HANDLE_EOF) { Exception exception = new HttpListenerException((int)statusCode); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, exception.ToString()); } _httpContext.Abort(); throw exception; } _leftToWrite = 0; } } finally { base.Dispose(disposing); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(this); } }