123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using SMBLibrary.RPC;
- namespace SMBLibrary.Services
- {
- public class RPCPipeStream : Stream
- {
- private RemoteService m_service;
- private List<MemoryStream> m_outputStreams;
- private int? m_maxTransmitFragmentSize;
- public RPCPipeStream(RemoteService service)
- {
- m_service = service;
- m_outputStreams = new List<MemoryStream>();
- }
- public override int Read(byte[] buffer, int offset, int count)
- {
- if (m_outputStreams.Count > 0)
- {
- int result = m_outputStreams[0].Read(buffer, offset, count);
- if (m_outputStreams[0].Position == m_outputStreams[0].Length)
- {
- m_outputStreams.RemoveAt(0);
- }
- return result;
- }
- else
- {
- return 0;
- }
- }
- public override void Write(byte[] buffer, int offset, int count)
- {
- int lengthOfPDUs = 0;
- do
- {
- RPCPDU rpcRequest = RPCPDU.GetPDU(buffer, offset);
- ProcessRPCRequest(rpcRequest);
- lengthOfPDUs += rpcRequest.FragmentLength;
- }
- while (lengthOfPDUs < count);
- }
- private void ProcessRPCRequest(RPCPDU rpcRequest)
- {
- if (rpcRequest is BindPDU)
- {
- BindAckPDU bindAckPDU = RemoteServiceHelper.GetRPCBindResponse((BindPDU)rpcRequest, m_service);
- m_maxTransmitFragmentSize = bindAckPDU.MaxTransmitFragmentSize;
- Append(bindAckPDU.GetBytes());
- }
- else if (rpcRequest is RequestPDU)
- {
-
- if (m_maxTransmitFragmentSize.HasValue)
- {
- List<ResponsePDU> responsePDUs = RemoteServiceHelper.GetRPCResponse((RequestPDU)rpcRequest, m_service, m_maxTransmitFragmentSize.Value);
- foreach (ResponsePDU responsePDU in responsePDUs)
- {
- Append(responsePDU.GetBytes());
- }
- }
- }
- else
- {
- throw new NotImplementedException("Unsupported RPC Packet Type");
- }
- }
- private void Append(byte[] buffer)
- {
- MemoryStream stream = new MemoryStream(buffer);
- m_outputStreams.Add(stream);
- }
- public override void Flush()
- {
- }
- public override void Close()
- {
- }
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException();
- }
- public override void SetLength(long value)
- {
- throw new NotSupportedException();
- }
- public override bool CanSeek
- {
- get
- {
- return false;
- }
- }
- public override bool CanRead
- {
- get
- {
- return true;
- }
- }
- public override bool CanWrite
- {
- get
- {
- return true;
- }
- }
- public override long Length
- {
- get
- {
-
- throw new NotSupportedException();
- }
- }
- public override long Position
- {
- get
- {
- throw new NotSupportedException();
- }
- set
- {
- throw new NotSupportedException();
- }
- }
-
-
-
- public int MessageLength
- {
- get
- {
- if (m_outputStreams.Count > 0)
- {
- return (int)m_outputStreams[0].Length;
- }
- else
- {
- return 0;
- }
- }
- }
- }
- }
|