public int BlindlyRelayData( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, SniffedDataChunk sniffedDataChunk = null) { int noBytesTransferred = 0; int bytesRead = 0; int chunkCounter = 0; byte[] buffer = new byte[MaxBufferSize]; while ((bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length)) > 0) { // Forward received data packets to peer outputStreamWriter.Write(buffer, 0, bytesRead); noBytesTransferred += bytesRead; // If a sniffer data chunk object is defined write application data to it if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } chunkCounter++; Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "BlindlyRelayData(): FragmentNo:{0} bytesTransferred:{1}", chunkCounter, bytesRead); } return(noBytesTransferred); }
string GetResourceName(int index, out int dataOffset) { long pos = nameSectionPosition + namePositions[index]; byte[] bytes; lock (reader) { reader.Seek(pos, SeekOrigin.Begin); // Can't use reader.ReadString, since it's using UTF-8! int byteLen = reader.Read7BitEncodedInt(); if (byteLen < 0) { throw new BadImageFormatException("Resource name has negative length"); } bytes = new byte[byteLen]; // We must read byteLen bytes, or we have a corrupted file. // Use a blocking read in case the stream doesn't give us back // everything immediately. int count = byteLen; while (count > 0) { int n = reader.Read(bytes, byteLen - count, count); if (n == 0) { throw new BadImageFormatException("End of stream within a resource name"); } count -= n; } dataOffset = reader.ReadInt32(); if (dataOffset < 0) { throw new BadImageFormatException("Negative data offset"); } } return(Encoding.Unicode.GetString(bytes)); }
public byte [] ReadSizedChunk() { int length = reader.ReadVariableInt(); if (length == 0) { return(empty_bytes); } byte [] buffer = new byte [length]; for (int readSize = 0; readSize < length;) { readSize += reader.Read(buffer, readSize, length - readSize); } return(buffer); }
public byte [] ReadSizedChunk() { int length = reader.ReadVariableInt(); if (length > 65536) { throw new InvalidOperationException("The message is too large."); } byte [] buffer = new byte [length]; for (int readSize = 0; readSize < length;) { readSize += reader.Read(buffer, readSize, length - readSize); } return(buffer); }
private byte[] ReceiveChunk(int totalBytesToRead, MyBinaryReader dataSenderStream) { int bytesRead = 0; int totalBytesReceived = 0; int chunkFragmentCounter = 0; MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[totalBytesToRead]; int maxInputLen = totalBytesToRead; while (totalBytesReceived < totalBytesToRead && (bytesRead = dataSenderStream.Read(buffer, 0, maxInputLen)) > 0) { if (totalBytesReceived + bytesRead >= totalBytesToRead) { int newPacketSize = bytesRead - ((totalBytesReceived + bytesRead) - totalBytesToRead); totalBytesReceived += newPacketSize; memStream.Write(buffer, 0, newPacketSize); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "Chunked.ReceiveChunk(0:MaxLen:{0}): Receiving fragment {1} from: Client -> Server ({2} buffer, totalBytesReceived:{3}, totalBytesToRead:{4})", buffer.Length, chunkFragmentCounter, bytesRead, totalBytesReceived, totalBytesToRead); break; } else { totalBytesReceived += bytesRead; memStream.Write(buffer, 0, bytesRead); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "Chunked.ReceiveChunk(1:MaxLen:{0}): Receiving fragment {1} from: Client -> Server ({2} buffer, totalBytesReceived:{3}, totalBytesToRead:{4})", buffer.Length, chunkFragmentCounter, bytesRead, totalBytesReceived, totalBytesToRead); } maxInputLen -= bytesRead; chunkFragmentCounter++; } byte[] fullDataChunk = memStream.ToArray(); return(fullDataChunk); }
public int ForwardNonchunkedDataToPeerChunked( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, Encoding contentCharsetEncoding, int transferredContentLength, byte[] serverNewlineBytes, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; byte[] buffer = new byte[MaxBufferSize]; int totalTransferredBytes = 0; int bytesRead = 0; while (totalTransferredBytes < transferredContentLength) { // Read data from peer 1 int maxDataToTransfer = transferredContentLength - totalTransferredBytes; bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(2:DATA), No data to transfer"); break; } // if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } // Encode received bytes to the announced format DataChunk serverDataChunk = new DataChunk(buffer, bytesRead, this.requestObj.ServerResponseObj.ContentTypeEncoding.ContentCharsetEncoding); if (mustBeProcessed == true) { Lib.PluginCalls.ServerDataTransfer(this.requestObj, serverDataChunk); } // Send chunk size to recepient string chunkSizeHexStringTmp = serverDataChunk.ContentDataLength.ToString("x"); byte[] chunkSizeDeclaration = contentCharsetEncoding.GetBytes(chunkSizeHexStringTmp); outputStreamWriter.Write(chunkSizeDeclaration, 0, chunkSizeDeclaration.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); // Send data packet to recipient outputStreamWriter.Write(serverDataChunk.ContentData, 0, serverDataChunk.ContentDataLength); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); outputStreamWriter.Flush(); noBytesTransferred += serverDataChunk.ContentDataLength; Logging.Instance.LogMessage( this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(): Data successfully relayed to client. ContentDataLength:{0} chunkSizeHexStringTmp={1} noBytesTransferred={2}", serverDataChunk.ContentDataLength, chunkSizeHexStringTmp, noBytesTransferred); totalTransferredBytes += bytesRead; } // Send trailing "0 length" chunk string chunkSizeZeroHexString = 0.ToString("x"); byte[] chunkSizeZeroDeclaration = contentCharsetEncoding.GetBytes(chunkSizeZeroHexString); outputStreamWriter.Write(chunkSizeZeroDeclaration, 0, chunkSizeZeroDeclaration.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(2:DATA): Total amount of transferred data={0}", totalTransferredBytes); return(noBytesTransferred); }
public int ForwardNonchunkedDataToPeerNonChunked( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, Encoding contentCharsetEncoding, int transferredContentLength, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[MaxBufferSize]; int totalTransferredBytes = 0; int bytesRead = 0; while (totalTransferredBytes < transferredContentLength) { // Read data from peer 1 int maxDataToTransfer = transferredContentLength - totalTransferredBytes; bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA), No data to transfer"); break; } // Write data to memory stream memStream.Write(buffer, 0, bytesRead); if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } totalTransferredBytes += bytesRead; Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA, TotalDataToTransfer:{0}): Relaying data from: Client -> Server (bytesRead:{1} totalTransferredBytes:{2})", buffer.Length, bytesRead, totalTransferredBytes); } byte[] dataPacket = memStream.ToArray(); //if (dataPacket.Length != MaxBufferSize) //{ // throw new Exception("The announced content length and the amount of received data are not the same"); //} // Encode received bytes to the announced format DataChunk serverDataChunk = new DataChunk(dataPacket, dataPacket.Length, this.requestObj.ServerResponseObj.ContentTypeEncoding.ContentCharsetEncoding); if (mustBeProcessed == true) { Lib.PluginCalls.ServerDataTransfer(this.requestObj, serverDataChunk); } // Send data packet to recipient outputStreamWriter.Write(serverDataChunk.ContentData, 0, serverDataChunk.ContentDataLength); outputStreamWriter.Flush(); noBytesTransferred += serverDataChunk.ContentDataLength; Logging.Instance.LogMessage( this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(): Data successfully relayed to client. ContentDataSize:{0})", serverDataChunk.ContentDataLength); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA): Total amount of transferred data={0}", totalTransferredBytes); return(noBytesTransferred); }