diff --git a/rdpy/protocol/common/error.py b/rdpy/protocol/common/error.py new file mode 100644 index 0000000..04f7072 --- /dev/null +++ b/rdpy/protocol/common/error.py @@ -0,0 +1,20 @@ +''' +@author: sylvain +''' + +class InvalidExpectedDataException(Exception): + ''' + raise when expected data on network is invalid + ''' + def __init__(self, message): + ''' + constructor with message + ''' + self._message = message + + def __str__(self): + ''' + return string representation of exception + ''' + return "%s"%self._message + diff --git a/rdpy/protocol/rdp/tpdu.py b/rdpy/protocol/rdp/tpdu.py index f7a99b1..cee2fad 100644 --- a/rdpy/protocol/rdp/tpdu.py +++ b/rdpy/protocol/rdp/tpdu.py @@ -5,6 +5,7 @@ Created on 5 sept. 2013 ''' from rdpy.protocol.common.layer import Layer from rdpy.protocol.common.stream import Stream +from rdpy.protocol.common.error import InvalidExpectedDataException class TPDU(Layer): ''' classdocs @@ -23,8 +24,35 @@ class TPDU(Layer): def connect(self): self.writeMessage(TPDU.X224_TPDU_CONNECTION_REQUEST) + + def recv(self, data): + ''' + main receive function + layer complexity doesn't need automata + ''' + #unused length + len = data.read_uint8() + code = data.read_uint8() + + if code == TPDU.X224_TPDU_DATA: + data.read_uint8() + self._presentation.recv(data) + else: + #padding + data.read_leuint32() + if code == TPDU.X224_TPDU_CONNECTION_CONFIRM: + self._presentation.connect() + elif code == TPDU.X224_TPDU_CONNECTION_REQUEST: + self.writeMessage(TPDU.X224_TPDU_CONNECTION_CONFIRM) + self._presentation.connect() + else: + raise InvalidExpectedDataException("invalid TPDU header code %d"%code) def write(self, data): + ''' + write message packet for TPDU layer + add TPDU header + ''' s = Stream() s.write_uint8(2) s.write_uint8(TPDU.X224_TPDU_DATA) @@ -33,10 +61,14 @@ class TPDU(Layer): self._transport.write(data) def writeMessage(self, code): + ''' + special write function + that packet TPDU message + ''' s = Stream() - s.write_uint8(6); - s.write_uint8(code); - s.write_beuint16(0); - s.write_beuint16(0); - s.write_uint8(0); + s.write_uint8(6) + s.write_uint8(code) + s.write_beuint16(0) + s.write_beuint16(0) + s.write_uint8(0) self.write(s) \ No newline at end of file diff --git a/rdpy/protocol/rdp/tpkt.py b/rdpy/protocol/rdp/tpkt.py index 9b8bc90..16d4f57 100644 --- a/rdpy/protocol/rdp/tpkt.py +++ b/rdpy/protocol/rdp/tpkt.py @@ -78,7 +78,7 @@ class TPKT(RawLayer): read classic TPKT packet ''' #next state is pass to - self.recv(data) + self._presentation.recv(data) self.expect(2, self.readHeader) def write(self, data):