public AttachBuffer ( ArraySegment |
||
buffer | ArraySegment |
|
Résultat | void |
private bool TurnServer_Received(ServersManager<TurnConnection> s, TurnConnection c, ref ServerAsyncEventArgs e) { if (e.LocalEndPoint.Protocol == ServerProtocol.Udp) { if (TurnMessage.IsTurnMessage(e.Buffer, e.Offset, e.BytesTransferred)) TurnServer_TurnDataReceived(ref e); else TurnServer_PeerDataReceived(ref e); } else if (e.LocalEndPoint.Protocol == ServerProtocol.Tcp) { if (c.Buffer.IsValid) { c.Buffer.Resize(Math.Max(4096, c.BytesExpected)); if (c.Buffer.CopyTransferredFrom(e, 0) == false) return false; if (c.Buffer.Count < c.BytesExpected) return true; } else { if (e.BytesTransferred < c.BytesExpected) return c.Buffer.CopyTransferredFrom(e, 0); } int proccessed = 0; for (; ; ) { if (c.Buffer.IsValid) { if (e == null) { e = EventArgsManager.Get(); e.CopyAddressesFrom(c); } e.AttachBuffer(c.Buffer); proccessed = 0; } if (e.BytesTransferred - proccessed < c.BytesExpected) return c.Buffer.CopyTransferredFrom(e, proccessed); switch (c.Phase) { case TcpPhase.WaitingFirstXpacket: if (pseudoTlsMessage.IsBeginOfClientHello(e.Buffer, e.Offset, FirstXpacketLength)) { c.Phase = TcpPhase.WaitingClientHello; c.BytesExpected = PseudoTlsMessage.ClientHelloLength; } else { c.Phase = TcpPhase.WaitingTcpFrame; c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength; if (FirstXpacketLength <= TcpFramingHeader.TcpFramingHeaderLength) goto case TcpPhase.WaitingTcpFrame; } break; case TcpPhase.WaitingClientHello: if (pseudoTlsMessage.IsClientHello(e.Buffer, e.Offset) == false) return false; var x = EventArgsManager.Get(); x.CopyAddressesFrom(e); x.Count = PseudoTlsMessage.ServerHelloHelloDoneLength; pseudoTlsMessage.GetServerHelloHelloDoneBytes(x.Buffer, x.Offset); s.SendAsync(x); proccessed += c.BytesExpected; c.Phase = TcpPhase.WaitingTcpFrame; c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength; break; case TcpPhase.WaitingTcpFrame: TcpFramingHeader tcpHeader; if (TcpFramingHeader.TryParse(e.Buffer, e.Offset, out tcpHeader) == false) return false; proccessed += c.BytesExpected; c.Phase = (tcpHeader.Type == TcpFrameType.ControlMessage) ? TcpPhase.WaitingTurnControlMessage : TcpPhase.WaitingTurnEndToEndData; c.BytesExpected = tcpHeader.Length; break; case TcpPhase.WaitingTurnEndToEndData: case TcpPhase.WaitingTurnControlMessage: if (e.BytesTransferred - proccessed < c.BytesExpected) if (c.Buffer.CopyTransferredFrom(e, proccessed + c.BytesExpected) == false) return false; e.Count -= proccessed; e.Offset += proccessed; e.BytesTransferred = c.BytesExpected; if (c.Phase == TcpPhase.WaitingTurnEndToEndData) TurnServer_PeerDataReceived(ref e); else TurnServer_TurnDataReceived(ref e); proccessed = e.BytesTransferred; c.Phase = TcpPhase.WaitingTcpFrame; c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength; break; default: throw new NotImplementedException(); } } } else throw new NotImplementedException(); return true; }
private void SendWriter(ServerAsyncEventArgs r, HttpMessageWriter writer) { r.Count = writer.Count; r.OffsetOffset = writer.OffsetOffset; r.AttachBuffer(writer.Detach()); SendAsync(r, false); }
private bool DecryptData(ref ServerAsyncEventArgs e, Server <C> .Connection <C> connection) { SspiContext sspiContext = connection.SspiContext; SecBufferDescEx secBufferDesc = sspiContext.SecBufferDesc5; if (sspiContext.Buffer.IsValid && e != null && !sspiContext.Buffer.CopyTransferredFrom(e, 0)) { return(false); } SecurityStatus securityStatus2; while (true) { secBufferDesc.Buffers[0].BufferType = BufferType.SECBUFFER_DATA; if (sspiContext.Buffer.IsValid) { this.SetSecBuffer(ref secBufferDesc.Buffers[0], sspiContext); } else { this.SetSecBuffer(ref secBufferDesc.Buffers[0], e); } secBufferDesc.Buffers[1].SetBufferEmpty(); secBufferDesc.Buffers[2].SetBufferEmpty(); secBufferDesc.Buffers[3].SetBufferEmpty(); secBufferDesc.Buffers[4].SetBufferEmpty(); SecurityStatus securityStatus = Sspi.SafeDecryptMessage(ref sspiContext.Handle, ref secBufferDesc, 0u, null); int bufferIndex = secBufferDesc.GetBufferIndex(BufferType.SECBUFFER_EXTRA, 0); int bufferIndex2 = secBufferDesc.GetBufferIndex(BufferType.SECBUFFER_DATA, 0); securityStatus2 = securityStatus; if (securityStatus2 != SecurityStatus.SEC_E_OK) { break; } if (bufferIndex2 < 0) { return(false); } if (sspiContext.Buffer.IsInvalid) { if (bufferIndex >= 0 && !sspiContext.Buffer.CopyFrom(secBufferDesc.Buffers[bufferIndex])) { return(false); } e.Offset = secBufferDesc.Buffers[bufferIndex2].Offset; e.BytesTransferred = secBufferDesc.Buffers[bufferIndex2].Size; e.SetMaxCount(); if (!this.OnReceived(connection, ref e)) { return(false); } } else { ArraySegment <byte> buffer = sspiContext.Buffer.Detach(); if (bufferIndex >= 0 && !sspiContext.Buffer.CopyFrom(secBufferDesc.Buffers[bufferIndex])) { return(false); } ServerAsyncEventArgs serverAsyncEventArgs = EventArgsManager.Get(); base.PrepareEventArgs(connection, serverAsyncEventArgs); serverAsyncEventArgs.AttachBuffer(buffer); serverAsyncEventArgs.Offset = secBufferDesc.Buffers[bufferIndex2].Offset; serverAsyncEventArgs.BytesTransferred = secBufferDesc.Buffers[bufferIndex2].Size; serverAsyncEventArgs.SetMaxCount(); bool flag = this.OnReceived(connection, ref serverAsyncEventArgs); if (serverAsyncEventArgs != null) { EventArgsManager.Put(serverAsyncEventArgs); } if (!flag) { return(false); } } if (bufferIndex < 0) { return(true); } } return(securityStatus2 != SecurityStatus.SEC_I_RENEGOTIATE && securityStatus2 == (SecurityStatus)2148074264u && (!sspiContext.Buffer.IsInvalid || sspiContext.Buffer.CopyTransferredFrom(e, 0))); }