Python RelayExtend2Cell Example

Introduction

The python relayextend2cell example is extracted from the most popular open source projects, you can refer to the following example for usage.

Programming language: Python

Namespace/package name: oppycellrelay

Example#1
File: circuitbuildtask.pyProject: nskinkel/oppy

 def _sendExtend2Cell(self, _, path_node):
     lspecs = [LinkSpecifier(path_node), LinkSpecifier(path_node, legacy=True)]
     self._hs_state = ntor.NTorState(path_node.microdescriptor)
     onion_skin = ntor.createOnionSkin(self._hs_state)
     extend2 = RelayExtend2Cell.make(self.circuit_id, nspec=len(lspecs), lspecs=lspecs, hdata=onion_skin)
     crypt_cell = crypto.encryptCell(extend2, self._crypt_path, early=True)
     self._conn.send(crypt_cell)

Example#2
File: ntorfsm.pyProject: CivBase/oppy

    def _processCreated2(self, cell):
        '''Called when this ntor fsm is receives a cell and is expecting
        an Created2Cell.

            - verify we did in fact receive a valid Created2Cell
            - derive crypto keys
            - create a RelayCrypto object and add to crypt_path
            - create an Extend2 cell and encrypt it
            - advance this ntor fsm's state
            - return the encrypted Extend2 cell

        Fail if we received an invalid or unexpected cell.

        :param cell cell: the received cell
        :returns: **oppy.cell.relay.RelayExtend2Cell**
        '''
        NTorFSM._verifyCellCmd(cell.header.cmd, CREATED2_CMD)

        entry_crypto = self._ntor_handshakes[0].deriveRelayCrypto(cell)
        self._crypt_path.append(entry_crypto)

        relay = self._path.middle
        lspecs = [LinkSpecifier(relay), LinkSpecifier(relay, legacy=True)]
        hdata = self._ntor_handshakes[1].createOnionSkin()

        cell = RelayExtend2Cell.make(self.circuit_id,
                                     nspec=len(lspecs),
                                     lspecs=lspecs,
                                     hdata=hdata)

        response = crypto.encryptCellToTarget(cell, self._crypt_path,
                                              target=0, early=True)
        self._state = State.EXPECT_FIRST_EXTENDED2
        return response

Example#3
File: ntorfsm.pyProject: CivBase/oppy

    def _processFstExtended2(self, cell):
        '''Called when this ntor fsm is receives a cell and is expecting
        its first Extended2 cell.

            - decrypt the incoming cell
            - verify we did in fact receive a valid Extended2 cell
            - derive crypto keys
            - create a RelayCrypto object and add to crypt_path
            - create an Extend2 cell and encrypt it
            - advance this ntor fsm's state
            - return the encrypted Extend2 cell

        Fail if we received an invalid or unexpected cell.

        :param cell cell: the received cell
        :returns: **oppy.cell.relay.RelayExtend2Cell**
        '''
        try:
            cell, origin = crypto.decryptCellUntilRecognized(cell,
                                                             self._crypt_path,
                                                             origin=0)
        except UnrecognizedCell:
            raise HandshakeFailed()

        cmd = cell.header.cmd
        NTorFSM._verifyCellCmd(cmd, RELAY_CMD)
        rcmd = cell.rheader.cmd
        NTorFSM._verifyCellCmd(rcmd, RELAY_EXTENDED2_CMD)

        # Generate crypto material from the received cell.
        middle_crypto = self._ntor_handshakes[1].deriveRelayCrypto(cell)
        self._crypt_path.append(middle_crypto)

        relay = self._path.exit
        lspecs = [LinkSpecifier(relay), LinkSpecifier(relay, legacy=True)]

        hdata = self._ntor_handshakes[2].createOnionSkin()

        cell = RelayExtend2Cell.make(self.circuit_id,
                                     nspec=len(lspecs),
                                     lspecs=lspecs,
                                     hdata=hdata)
        response = crypto.encryptCellToTarget(cell, self._crypt_path,
                                              target=1, early=True)

        self._state = State.EXPECT_SECOND_EXTENDED2
        return response

Example#4
File: test_relay.pyProject: CivBase/oppy

    def test_make(self):
        hdata = self.gen_data(84)
        lspecs = [self.gen_data(8), self.gen_data(20)]

        result = RelayExtend2Cell.make(
            1, hdata=hdata, lspecs=lspecs, early=False)

        self.assertEqual(result, self.mock_relay_extend_2_cell.return_value)
        self.mock_fixedlen_header.assert_called_once_with(
            circ_id=1, cmd=3, link_version=3)

        self.mock_relay_header.assert_called_once_with(
            cmd=14, recognized="\x00\x00", stream_id=0,
            digest="\x00\x00\x00\x00", rpayload_len=117)

        self.mock_relay_extend_2_cell.assert_called_once_with(
            self.mock_fixedlen_header.return_value,
            rheader=self.mock_relay_header.return_value, nspec=2,
            lspecs=lspecs, htype=2, hlen=84, hdata=hdata)