예제 #1
0
파일: connection.py 프로젝트: CivBase/oppy
    def dataReceived(self, data):
        '''We received data from the remote connection.

        Extract cells from the data stream and send them along to be
        processed.

        :param str data: data received from remote end
        '''
        self._buffer += data

        while Cell.enoughDataForCell(self._buffer):
            try:
                cell = Cell.parse(self._buffer, encrypted=True)
                self._deliverCell(cell)
                self._buffer = self._buffer[len(cell):]
            # this shouldn't happen and if it does, it's probably a bug
            except NotEnoughBytes as e:
                logging.debug(str(e))
                break
            # XXX remove len(unimplemented cell bytes) from buffer
            except NotImplementedError:
                logging.debug("Received a cell we can't handle yet.")
                logging.debug('buffer contents:\n')
                logging.debug([ord(i) for i in self._buffer])
                raise
            except (BadHandshakeState, HandshakeFailed, UnexpectedCell) as e:
                logging.warning(e)
                self.closeConnection()
                self._buffer = ''
                break
예제 #2
0
    def dataReceived(self, data):
        '''We received data from the remote connection.

        Extract cells from the data stream and send them along to be
        processed.

        :param str data: data received from remote end
        '''
        self._buffer += data

        while Cell.enoughDataForCell(self._buffer):
            try:
                cell = Cell.parse(self._buffer, encrypted=True)
                self._deliverCell(cell)
                self._buffer = self._buffer[len(cell):]
            # this shouldn't happen and if it does, it's probably a bug
            except NotEnoughBytes as e:
                logging.debug(str(e))
                break
            # XXX remove len(unimplemented cell bytes) from buffer
            except NotImplementedError:
                logging.debug("Received a cell we can't handle yet.")
                logging.debug('buffer contents:\n')
                logging.debug([ord(i) for i in self._buffer])
                raise
            except (BadHandshakeState, HandshakeFailed, UnexpectedCell) as e:
                logging.warning(e)
                self.closeConnection()
                self._buffer = ''
                break
예제 #3
0
파일: test_cell.py 프로젝트: CivBase/oppy
    def test_fixed_length_command_shorter_than_required(self):
        self.mock_struct.unpack.return_value = (None, 0)

        result = AbstractCell.enoughDataForCell(
            self.gen_data(509), link_version=4)

        self.assertFalse(result)
        self.mock_struct.unpack.assert_called_once_with('!IB', 'abcde')
예제 #4
0
파일: test_cell.py 프로젝트: CivBase/oppy
    def test_fixed_length_command_shorter_than_required(self):
        self.mock_struct.unpack.return_value = (None, 0)

        result = AbstractCell.enoughDataForCell(self.gen_data(509),
                                                link_version=4)

        self.assertFalse(result)
        self.mock_struct.unpack.assert_called_once_with('!IB', 'abcde')
예제 #5
0
파일: test_cell.py 프로젝트: CivBase/oppy
    def test_variable_length_command_shorter_than_required(self):
        self.mock_struct.unpack.side_effect = [(None, 7), (8, None)]

        result = AbstractCell.enoughDataForCell(self.gen_data(7),
                                                link_version=4)

        self.assertFalse(result)
        self.mock_struct.unpack.assert_has_calls(
            [call('!IB', 'abcde'), call('!H', 'fg')])
예제 #6
0
파일: test_cell.py 프로젝트: CivBase/oppy
    def test_variable_length_command_shorter_than_required(self):
        self.mock_struct.unpack.side_effect = [(None, 7), (8, None)]

        result = AbstractCell.enoughDataForCell(
            self.gen_data(7), link_version=4)

        self.assertFalse(result)
        self.mock_struct.unpack.assert_has_calls([
            call('!IB', 'abcde'),
            call('!H', 'fg')])
예제 #7
0
    def dataReceived(self, data):
        self._buffer += data

        # TODO: fix underlying cell parsing code. current code does not
        #       handle malicious inputs properly and can't recover from
        #       weird/broken inputs
        while Cell.enoughDataForCell(self._buffer):
            try:
                cell = Cell.parse(self._buffer)
                self._read_queue.put(cell)
                self._buffer = self._buffer[len(cell):]
            # TODO: catch all exceptions and remove that length
            #       from the buffer
            # TODO: remove len(NotImplementedBytes) from buffer
            except NotImplementedError as e:
                msg = ("Connection to {} received an unexpected cell. {}."
                       .format(self.micro_status_entry.fingerprint, e))
                self._current_task.errback(msg)
                break
예제 #8
0
    def dataReceived(self, data):
        self._buffer += data

        # TODO: fix underlying cell parsing code. current code does not
        #       handle malicious inputs properly and can't recover from
        #       weird/broken inputs
        while Cell.enoughDataForCell(self._buffer):
            try:
                cell = Cell.parse(self._buffer)
                self._read_queue.put(cell)
                self._buffer = self._buffer[len(cell):]
            # TODO: catch all exceptions and remove that length
            #       from the buffer
            # TODO: remove len(NotImplementedBytes) from buffer
            except NotImplementedError as e:
                msg = (
                    "Connection to {} received an unexpected cell. {}.".format(
                        self.micro_status_entry.fingerprint, e))
                self._current_task.errback(msg)
                break
예제 #9
0
    def dataReceived(self, data):
        '''We received data from the remote connection.

        Extract cells from the data stream and send them along to be
        processed.

        :param str data: data received from remote end
        '''
        self._buffer += data

        while Cell.enoughDataForCell(self._buffer):
            try:
                cell = Cell.parse(self._buffer, encrypted=True)
                self._recv(cell)
                self._buffer = self._buffer[len(cell):]
            except NotEnoughBytes as e:
                logging.debug(e)
                break
            # TODO: remove len(unimplemented cell bytes) from buffer
            except NotImplementedError:
                logging.debug("Received a cell we can't handle yet.")
                logging.debug('buffer contents:\n')
                logging.debug([ord(i) for i in self._buffer])
                raise
예제 #10
0
파일: connection.py 프로젝트: nskinkel/oppy
    def dataReceived(self, data):
        '''We received data from the remote connection.

        Extract cells from the data stream and send them along to be
        processed.

        :param str data: data received from remote end
        '''
        self._buffer += data

        while Cell.enoughDataForCell(self._buffer):
            try:
                cell = Cell.parse(self._buffer, encrypted=True)
                self._recv(cell)
                self._buffer = self._buffer[len(cell):]
            except NotEnoughBytes as e:
                logging.debug(e)
                break
            # TODO: remove len(unimplemented cell bytes) from buffer
            except NotImplementedError:
                logging.debug("Received a cell we can't handle yet.")
                logging.debug('buffer contents:\n')
                logging.debug([ord(i) for i in self._buffer])
                raise
예제 #11
0
파일: test_cell.py 프로젝트: CivBase/oppy
    def test_data_shorter_than_header(self):
        result = AbstractCell.enoughDataForCell(
            self.gen_data(2), link_version=1)

        self.assertFalse(result)
        self.assertFalse(self.mock_struct.unpack.called)
예제 #12
0
파일: test_cell.py 프로젝트: CivBase/oppy
    def test_data_shorter_than_header(self):
        result = AbstractCell.enoughDataForCell(self.gen_data(2),
                                                link_version=1)

        self.assertFalse(result)
        self.assertFalse(self.mock_struct.unpack.called)