Пример #1
0
    def testHandlePkt_FirstAvpIsNotControlAvp(self):
        """Handle control packet: the first avp is not control avp.

        :return:

        """
        conn_address = '127.11.2.4'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.6'
        conn = L2tpConnection(5, 5, conn_address, local_addr)
        avp1 = Hostname("TestAVP")
        avp2 = ControlMessageAVP(ControlMessageAVP.SCCRQ)
        avp3 = RouterID(0)
        avp4 = AssignedControlConnectionID(1)
        avp5 = PseudowireCapList(())
        avps = [avp1, avp2, avp3, avp4, avp5]
        sccrq = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        ret = conn.HandlePkt(sccrq)
        self.assertIsNone(ret)
        conn.CloseConnection()
        self.assertIsNone(ret)
Пример #2
0
    def test_recvICRP(self):
        conn_address = '127.11.2.15'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.17'
        conn = L2tpConnection(16, 16, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)
        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)

        avp = ControlMessageAVP(ControlMessageAVP.ICRP)
        avps = [avp]
        avps.append(localAvp)
        icrp = L2tpv3ControlPacket(0, 0, 1, avps)
        self.assertEqual(icrp.length, 30)
        self.assertEqual(icrp.avps[0].messageType, 11)

        ret = conn.HandlePkt(icrp)
        self.assertIsNone(ret)
        conn.CloseConnection()
Пример #3
0
    def test_recvFSR(self):
        """Handle control packet: FSR.

        Function: recvFSR.

        :return:

        """
        conn_address = '127.11.2.29'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.31'
        conn = L2tpConnection(18, 18, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)

        avp1 = ControlMessageAVP(ControlMessageAVP.FSR)
        avp2 = FailoverSessionState(session.remoteSessionId, session.localSessionId)
        avps = [avp1, avp2]
        fsr = L2tpv3ControlPacket(0, 0, 1, avps)

        ret = conn.HandlePkt(fsr)
        self.assertIsNone(ret)
        conn.CloseConnection()
Пример #4
0
def demoL2tp():
    print "demoL2tp thread start!"
    setup_logging('HAL', filename="hal.log", logging_level=logging.DEBUG)
    drv_logger = logging.getLogger("demoL2tp")
    drv_logger.info("hello demoL2tp Log")

    dispatcher = Dispatcher()
    l2tp_dispatcher = L2tpv3Dispatcher(
        dispatcher,
        local_addr=None,
        # since we don't create global listen, set it to None
        create_global_listen=False)
    L2tpv3GlobalSettings.Dispatcher = l2tp_dispatcher

    # setup the halclient
    SupportedCfgMsg = list()
    SupportedCfgMsg.append(MsgTypeVspAvpExchange)
    notificationMsg = list()
    notificationMsg.append(MsgTypeL2tpv3SessionStatusNotification)
    hal_client = L2tpHalClient("L2TP_HAL_CLIENT",
                               "the HAL client of L2TP feature", "1.0",
                               tuple(notificationMsg), dispatcher,
                               SupportedCfgMsg)
    L2tpv3GlobalSettings.l2tp_hal_client = hal_client
    hal_client.start(l2tp_dispatcher.receive_hal_message)
    if L2tpv3GlobalSettings.l2tp_hal_client:
        print("setup l2tp hal client successfully")

    if L2tpv3GlobalSettings.Dispatcher:
        print("l2tp_dispatcher is OK")
        print l2tp_dispatcher

    l2tp_dispatcher.dispatcher.loop()
    print "demoL2tp thread done!"
Пример #5
0
    def testStopConnection(self):
        conn_address = '127.11.2.26'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False
        L2tpv3GlobalSettings.l2tp_hal_client = fake_hal()

        local_addr = '127.11.2.27'
        L2tpConnection.ConnectionDb.clear()
        conn = L2tpConnection(3, 3, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)

        conn.ReceiveHalMsg(msg=None)
        msg = L2tpv3Hal_pb2.t_l2tpLcceAssignmentRsp()
        msg.result = False
        try:
            conn.ReceiveHalMsg(msg)
        except Exception as e:
            pass
        msg.result = True
        conn.ReceiveHalMsg(msg)
        conn.SendHalMsg(msg_type=L2tpConnection.ADD_LCCE)
        conn.fsmEventrecvBadSCCRP(event=None)
        conn.fsmEventrecvSCCRQLoseTieGood(event=None)
        conn.fsmEventrecvSCCRQLoseTieBad(event=None)
        conn.fsmEventrecvSCCRQWinSCCRQ(event=None)
        conn.fsmEventcloseRequest(event=None)
        conn.fsmEventHalError(event=None)
        conn.StopConnection()
        conn.StopConnection()
        self.assertEqual(len(conn.ConnectionDb), 0)
        L2tpv3GlobalSettings.l2tp_hal_client = None
Пример #6
0
    def test_recvFSQ(self):
        """Handle control packet: FSQ.

        Function: recvFSQ.

        :return:

        """
        conn_address = '127.11.2.28'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = True

        local_addr = '127.11.2.30'
        conn = L2tpConnection(18, 18, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)

        avp1 = ControlMessageAVP(ControlMessageAVP.FSQ)
        avp2 = FailoverSessionState(session.remoteSessionId, session.localSessionId)
        avp3 = FailoverSessionState(4, 5)
        avps = [avp1, avp2, avp3]
        fsq = L2tpv3ControlPacket(0, 0, 1, avps)

        fsr = conn.HandlePkt(fsq)
        self.assertIsNotNone(fsr)
        self.assertEqual(fsr.avps[1].sessionID, 1)
        self.assertEqual(fsr.avps[1].remoteSessionID, 2)
        self.assertEqual(fsr.avps[2].sessionID, 0)
        self.assertEqual(fsr.avps[2].remoteSessionID, 4)
        conn.CloseConnection()
Пример #7
0
    def test_add_remove_find_Session(self):
        conn_address = '127.11.2.21'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.23'
        conn = L2tpConnection(22, 22, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'sender', conn)
        session1 = None
        conn.addSession(session)
        conn.addSession(session1)
        self.assertEqual(conn.sessions.keys(), [1])
        self.assertEqual(conn.sessionsByRemoteSessionId.keys(), [2])
        self.assertIsNotNone(
            conn.findSessionByLocalSessionID(session.localSessionId))
        self.assertIsNotNone(
            conn.findSessionByRemoteSessionID(session.remoteSessionId))

        conn.removeSession(session)
        self.assertEqual(conn.sessions.keys(), [])
        self.assertEqual(conn.sessionsByRemoteSessionId.keys(), [])
        self.assertIsNone(
            conn.findSessionByLocalSessionID(session.localSessionId))
        self.assertIsNone(
            conn.findSessionByRemoteSessionID(session.remoteSessionId))
        conn.CloseConnection()
Пример #8
0
    def test_recvHELLO(self):
        conn_address = '127.11.2.16'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.18'
        conn = L2tpConnection(17, 17, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)
        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)

        avp = ControlMessageAVP(ControlMessageAVP.HELLO)
        avps = [avp]
        avps.append(localAvp)
        hello = L2tpv3ControlPacket(0, 0, 1, avps)
        self.assertEqual(hello.length, 30)
        self.assertEqual(hello.avps[0].messageType, 6)

        zlb = conn.HandlePkt(hello)
        self.assertEqual(zlb.length, 12)
        conn.CloseConnection()
Пример #9
0
    def testConnection_init(self):
        conn_address = '127.11.2.1'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.2'
        # localConnectionID not equal to zero
        conn1 = L2tpConnection(1, 1, conn_address, local_addr)
        session1 = L2tpv3Session(1, 2, 'sender', conn1)
        conn1.addSession(session1)
        self.assertEqual(conn1.sessions.keys(), [1])
        self.assertEqual(conn1.sessionsByRemoteSessionId.keys(), [2])
        self.assertEqual(conn1.localConnID, 1)
        self.assertIsNotNone(conn1.ConnectionDb)

        # localConnectionID equal to zero
        local_addr = '127.11.2.3'
        conn2 = L2tpConnection(0, 2, conn_address, local_addr)
        session2 = L2tpv3Session(1, 2, 'sender', conn2)
        conn2.addSession(session2)
        self.assertEqual(conn2.sessions.keys(), [1])
        self.assertEqual(conn2.sessionsByRemoteSessionId.keys(), [2])
        self.assertIsNotNone(conn2.localConnID)
        self.assertIsNotNone(conn2.ConnectionDb)
        conn1.CloseConnection()
        conn2.CloseConnection()
Пример #10
0
    def test_recvSLI(self):
        conn_address = '127.11.2.17'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.19'
        conn = L2tpConnection(18, 18, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)
        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)

        avp1 = ControlMessageAVP(ControlMessageAVP.SLI)
        avp2 = CallSerialNumber()
        avps = [avp1, avp2]
        avps.append(localAvp)
        sli = L2tpv3ControlPacket(0, 0, 1, avps)
        self.assertEqual(sli.length, 40)
        self.assertEqual(sli.avps[0].messageType, ControlMessageAVP.SLI)

        ret = conn.recvSLI(sli)
        self.assertIsNone(ret)
        # Abnormal SLI message
        avps = list()
        sli = L2tpv3ControlPacket(0, 0, 1, avps)
        ret = conn.HandlePkt(sli)
        self.assertIsNone(ret)
        conn.CloseConnection()
Пример #11
0
    def test_recvCDN(self):
        conn_address = '127.11.2.14'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.16'
        conn = L2tpConnection(15, 15, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)
        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)

        avp = ControlMessageAVP(ControlMessageAVP.CDN)
        avps = [avp]
        avps.append(localAvp)
        cdn = L2tpv3ControlPacket(0, 0, 1, avps)
        self.assertEqual(cdn.length, 30)
        self.assertEqual(cdn.avps[0].messageType, 14)

        conn.HandlePkt(cdn)
        self.assertEqual(conn.fsm.current, 'idle')
        conn.CloseConnection()
Пример #12
0
    def test_resetTransport(self):
        conn_address = '127.11.2.33'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.35'
        conn = L2tpConnection(18, 18, conn_address, local_addr)
        avp = ControlMessageAVP(ControlMessageAVP.HELLO)
        avps = [avp]
        hello = L2tpv3ControlPacket(0, 0, 1, avps)
        conn.transport.sendList.append({
            "time": 1,
            "pkt": hello,
            "sendTimes": 0,
        })
        conn.transport.receiveWindow.add(hello)
        conn.transport.ackNr = 105
        conn.transport.ns = 56
        conn.resetTransport()
        self.assertFalse(conn.transport.sendList)
        self.assertFalse(conn.transport.receiveWindow)
        self.assertEqual(conn.transport.ackNr, 0)
        self.assertEqual(conn.transport.ackNr, 0)
        conn.CloseConnection()
Пример #13
0
    def test_recvICCN(self):
        conn_address = '127.11.2.13'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.15'
        conn = L2tpConnection(14, 14, conn_address, local_addr)
        session = L2tpv3Session(3, 4, 'receive', conn)
        conn.addSession(session)

        # localSessionID = session.remoteSessionId
        remoteSessionID = session.localSessionId
        # localAvp = LocalSessionID(localSessionID)
        remoteAvp = RemoteSessionID(remoteSessionID)

        # Normal case, session is not none
        avp = ControlMessageAVP(ControlMessageAVP.ICCN)
        avps = [avp]
        avps.append(remoteAvp)
        iccn = L2tpv3ControlPacket(0, 0, 1, avps)
        self.assertEqual(iccn.length, 30)
        self.assertEqual(iccn.avps[0].messageType, 12)
        ret = conn.HandlePkt(iccn)
        self.assertIsNotNone(ret)
        self.assertIsInstance(ret, L2tpv3ACK)

        conn.CloseConnection()
Пример #14
0
    def test_recvICRQ_normal_1(self):
        """Handle control packet: ICRQ, session exist.

        :return: none

        """
        conn_address = '127.11.2.11'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.13'
        conn = L2tpConnection(12, 12, conn_address, local_addr)
        icrq = L2tpv3ControlPacket.decode(self.ircq_buf)
        icrp = conn.HandlePkt(icrq)
        print icrp

        self.assertEqual(icrp.connectionID, 12)
        ret = conn.checkMustAvps(
            L2tpv3ControlPacket.ICRPMandatoryAVPs, icrp.avps)
        self.assertTrue(ret)
        self.assertEqual(icrp.avps[0].messageType, ControlMessageAVP.ICRP)
        session = conn.findSessionByRemoteSessionID(0x40000001)
        self.assertIsNotNone(session)

        conn.CloseConnection()
Пример #15
0
    def setUp(self):
        setup_test_redis()
        self.local_addr = "127.0.0.1"
        self.global_dispatcher = Dispatcher()
        self.l2tp_dispatcher = L2tpv3Dispatcher(
            dispatcher=self.global_dispatcher,
            create_global_listen=False,
            local_addr=self.local_addr)
        L2tpv3GlobalSettings.Dispatcher = self.l2tp_dispatcher

        self.local_addr = "::1"
        self.global_dispatcher = Dispatcher()
        self.l2tp_dispatcher = L2tpv3Dispatcher(
            dispatcher=self.global_dispatcher,
            create_global_listen=False,
            local_addr=self.local_addr)
        L2tpv3GlobalSettings.Dispatcher = self.l2tp_dispatcher
Пример #16
0
 def test_init(self):
     local_addr = "test"
     try:
         dispatcher = L2tpv3Dispatcher(dispatcher=self.global_dispatcher,
                                       create_global_listen=True,
                                       local_addr=local_addr)
     except L2tpv3DispatcherError as e:
         pass
Пример #17
0
    def testHandlePkt_SCCCN(self):
        """Handle control packet: SCCCN.

        Function: recvSCCCN.

        :return:

        """
        conn_address = '127.11.2.9'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.11'
        conn = L2tpConnection(10, 10, conn_address, local_addr)

        # Receive a good SCCCN, return NULL
        avp1 = ControlMessageAVP(ControlMessageAVP.SCCCN)
        avp2 = AssignedControlConnectionID(10)
        avps = [avp1, avp2]
        scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        ret = conn.HandlePkt(scccn)
        self.assertIsNotNone(ret)
        self.assertIsInstance(ret, L2tpv3ACK)

        # Receive a bad SCCCN, return stopCCN
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)
        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)
        avp3 = CallSerialNumber()
        avps.append(localAvp)
        avps.append(avp3)
        scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        ret = conn.HandlePkt(scccn)
        self.assertIsNotNone(ret)
        self.assertIsInstance(ret, L2tpv3ACK)

        # Recovery tunnel receive SCCCN
        recover_conn = L2tpConnection(100, 100, conn_address, local_addr)
        recover_conn.isInRecovery = True
        conn.recoverConnection = recover_conn
        conn.isRecoveryTunnel = True
        avp1 = ControlMessageAVP(ControlMessageAVP.SCCCN)
        avp2 = AssignedControlConnectionID(10)
        avps = [avp1, avp2]
        scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        ret = conn.HandlePkt(scccn)
        self.assertFalse(recover_conn.isInRecovery)

        conn.CloseConnection()
Пример #18
0
 def setUpClass(cls):
     cls.conn_address = '127.0.0.1'
     setup_test_redis()
     global_dispatcher = Dispatcher()
     dispatcher = L2tpv3Dispatcher(global_dispatcher, cls.conn_address, False, None)
     L2tpv3GlobalSettings.Dispatcher = dispatcher
     # setup the halclient
     cls.hal_client = L2tpHalClient("L2TP_HAL_CLIENT",
                                    "the HAL client of L2TP feature",
                                    "1.0", tuple(L2tpHalClient.notification_list.keys()), global_dispatcher)
     cls.hal_client.handler = dispatcher.receive_hal_message
     L2tpv3GlobalSettings.l2tp_hal_client = cls.hal_client
     pass
Пример #19
0
    def testHandlePkt_SCCRQ(self):
        """Handle control packet: SCCRQ.

        Function: recvSCCRQ.

        :return:

        """
        conn_address = '127.11.2.7'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.9'
        conn = L2tpConnection(8, 8, conn_address, local_addr)

        # Receive a good SCCRQ, return SCCRP
        avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ)
        avp2 = Hostname("TestAVP")
        avp3 = RouterID(0)
        avp4 = AssignedControlConnectionID(1)
        avp5 = PseudowireCapList(())
        avps = [avp1, avp2, avp3, avp4, avp5]
        sccrq = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        sccrp = conn.HandlePkt(sccrq)

        self.assertEqual(sccrp.connectionID, 8)
        self.assertEqual(sccrp.length, 28)
        self.assertEqual(sccrp.avps[0].length, 8)
        self.assertEqual(conn.fsm.current,
                         L2tpv3ConnectionFsm.StateWaitCtlConn)
        # self.assertEqual(sccrp.avps[0].messageType, ControlMessageAVP.SCCRP)

        # Receive a bad SCCRQ, return stopCCN
        session = L2tpv3Session(1, 2, 'receiver', conn)
        conn.addSession(session)

        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)
        avps.append(localAvp)
        sccrp = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        stopCCN = conn.HandlePkt(sccrp)
        self.assertEqual(stopCCN.length, 58)
        self.assertEqual(stopCCN.connectionID, 8)
        self.assertEqual(stopCCN.avps[0].length, 8)
        self.assertEqual(
            stopCCN.avps[0].messageType, ControlMessageAVP.StopCCN)
Пример #20
0
    def test_allocate_local_session_id(self):
        conn_address = '127.11.2.25'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.24'
        conn = L2tpConnection(12, 12, conn_address, local_addr)
        pkt = L2tpv3ControlPacket()
        ret = conn._findSession(pkt=pkt)
        self.assertIsNone(ret)
        conn.recvICCN(pkt)
        conn.recvCDN(pkt)
        conn.recvICRP(pkt)
        conn.recvSLI(pkt)
        L2tpv3GlobalSettings.MustAvpsCheck = True
        ret = conn.recvSCCRQ(pkt)
        self.assertIsInstance(ret, L2tpv3StopCCN)
        ret = conn.recvSCCRP(pkt)
        ret = conn.recvSCCCN(pkt)
        ret = conn.recvStopCCN(pkt)

        ret = conn.allocate_local_session_id(pkt=pkt)
        self.assertEqual(ret, 0)
        remoteEndId = RemoteEndID()
        localsession = LocalSessionID(0x112)
        pkt.avps.append(remoteEndId)
        pkt.avps.append(localsession)
        ret = conn.allocate_local_session_id(pkt=pkt)
        self.assertEqual(ret, 0)
        sublayer = DepiL2SpecificSublayerSubtype(17)
        pkt.avps.append(sublayer)
        ret = conn.allocate_local_session_id(pkt=pkt)
        self.assertEqual(ret, 0x1100000)

        icrq = L2tpv3ControlPacket.decode(self.ircq_buf)
        ret = conn.allocate_local_session_id(pkt=icrq)
        self.assertEqual(ret, 0x10030000)
        for avp in icrq.avps:
            if isinstance(avp, RemoteSessionID):
                icrq.avps.remove(avp)
        avp_remote = RemoteSessionID(0x80000001)
        icrq.avps.append(avp_remote)
        ret = conn.allocate_local_session_id(pkt=icrq)
        self.assertEqual(ret, 0x80000001)
        conn.CloseConnection()
        conn.CloseConnection()
Пример #21
0
 def testL2tpv3StopCCN(self):
     conn_address = '127.13.3.5'
     global_dispatcher = Dispatcher()
     dispatcher = L2tpv3Dispatcher(global_dispatcher, conn_address, False,
                                   None)
     L2tpv3GlobalSettings.Dispatcher = dispatcher
     L2tpv3GlobalSettings.LocalIPAddress = '127.13.3.5'
     conn = L2tpConnection(4, 4, conn_address)
     pkt = L2tpv3StopCCN(conn, 0, 0, "CCN")
     self.assertEqual(pkt.avps[0].messageType, ControlMessageAVP.StopCCN)
     self.assertIsNotNone(pkt.avpStr)
     self.assertIsNone(pkt.Session)
     self.assertIsNone(pkt.Connection)
     self.assertIsNone(pkt.transport)
     conn.CloseConnection()
Пример #22
0
    def testL2tpv3CDN(self):
        conn_address = '127.13.3.6'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(global_dispatcher, conn_address, False,
                                      None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.LocalIPAddress = '127.13.3.6'
        conn = L2tpConnection(5, 5, conn_address)

        session = L2tpv3Session(1, 2, 'receiver', conn)
        cdn = L2tpv3CDN(session, 2, 4, "Avp cannot be handled correctly")
        self.assertEqual(cdn.avps[0].messageType, ControlMessageAVP.CDN)
        self.assertEqual(cdn.avps[1].messageType, ControlMessageAVP.StopCCN)
        self.assertEqual(cdn.avps[2].attrType, 63)
        self.assertEqual(cdn.avps[3].attrType, 64)
Пример #23
0
    def test_queryInactSessions(self):
        conn_address = '127.11.2.30'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.32'
        conn = L2tpConnection(18, 18, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        session.local_circuit_status = L2tpv3Session.CIRCUIT_STATUS_DOWN
        conn.addSession(session)
        conn.queryInactSessions()
        conn.CloseConnection()
Пример #24
0
    def test_queryStaleSessions(self):
        conn_address = '127.11.2.31'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.33'
        conn = L2tpConnection(18, 18, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        session.stale = True
        conn.addSession(session)
        conn.queryStaleSessions()
        conn.CloseConnection()
Пример #25
0
    def setUpClass(cls):
        # open logger
        setup_logging("L2TP")
        setup_test_redis()

        cls.conn_address = '127.0.0.1'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, cls.conn_address, False, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        # setup the halclient

        notification_list = list()
        notification_list.append(
            HalConfigMsg.MsgTypeL2tpv3SessionStatusNotification)

        cls.hal_client = L2tpHalClient("L2TP_HAL_CLIENT",
                                       "the HAL client of L2TP feature",
                                       "1.0", tuple(L2tpHalClient.notification_list.keys()), global_dispatcher)

        cls.hal_client.handler = dispatcher.receive_hal_message
        cls.conn = L2tpConnection(
            6661, 6662, cls.conn_address, cls.conn_address)
        cls.session = L2tpv3Session(6661, 6662, 'receiver', cls.conn)
        cls.conn.addSession(cls.session)
        localSessionId = L2tpv3RFC3931AVPs.LocalSessionID(6661)
        remoteSessionId = L2tpv3RFC3931AVPs.RemoteSessionID(6662)
        remoteEnd = L2tpv3RFC3931AVPs.RemoteEndID(
            (((0, 3, 0), 0), ((0, 3, 1), 1), ((0, 3, 2), 2)))
        remoteEnd_1 = L2tpv3RFC3931AVPs.RemoteEndID(
            (((0, 3, 3), 3), ((0, 3, 4), 4), ((0, 3, 5), 5)))
        pw_type = L2tpv3RFC3931AVPs.L2SpecificSublayer(3)
        DepiL2SpecificSublayerSubtype = L2tpv3CableLabsAvps.DepiL2SpecificSublayerSubtype(3)
        LocalMTUCableLabs = L2tpv3CableLabsAvps.LocalMTUCableLabs(1500)
        DepiRemoteMulticastJoin = L2tpv3CableLabsAvps.DepiRemoteMulticastJoin(("5.5.5.1", "229.1.1.255"))
        DepiResourceAllocReq = L2tpv3CableLabsAvps.DepiResourceAllocReq(((0, 1), (1, 2)))
        UpstreamFlow = L2tpv3CableLabsAvps.UpstreamFlow(((0, 1), (1, 2)))

        cls.session.avps_icrq.append(localSessionId)
        cls.session.avps_icrq.append(remoteSessionId)
        cls.session.avps_icrq.append(remoteEnd)
        cls.session.avps_icrq.append(remoteEnd_1)
        cls.session.avps_icrq.append(DepiL2SpecificSublayerSubtype)
        cls.session.avps_icrq.append(LocalMTUCableLabs)
        cls.session.avps_icrq.append(pw_type)
        cls.session.avps_icrq.append(DepiRemoteMulticastJoin)
        cls.session.avps_icrq.append(DepiResourceAllocReq)
        cls.session.avps_icrq.append(UpstreamFlow)
Пример #26
0
    def test_localRequest(self):
        conn_address = '127.11.2.18'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.20'
        conn = L2tpConnection(19, 19, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)

        conn.localRequest('127.0.0.1')
        self.assertEqual(
            conn.fsm.current, L2tpv3ConnectionFsm.StateWaitCtlReply)
        conn.CloseConnection()
Пример #27
0
 def test_SetPacketTransport(self):
     conn_address = '127.13.3.3'
     global_dispatcher = Dispatcher()
     dispatcher = L2tpv3Dispatcher(global_dispatcher, conn_address, False,
                                   None)
     L2tpv3GlobalSettings.Dispatcher = dispatcher
     L2tpv3GlobalSettings.LocalIPAddress = '127.13.3.4'
     avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ)
     avp2 = Hostname("TestAVP")
     avps = [avp1, avp2]
     pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps,
                               "testL2tpv3ControlPacket")
     self.assertIsNone(pkt.transport)
     conn = L2tpConnection(3, 3, conn_address)
     transport = L2tpv3Transport(conn, '127.0.1.1', '127.0.1.2')
     pkt.SetPacketTransport(transport)
     self.assertIsNotNone(pkt.transport)
     conn.CloseConnection()
Пример #28
0
 def test_SetPktSession(self):
     conn_address = '127.13.1.4'
     global_dispatcher = Dispatcher()
     dispatcher = L2tpv3Dispatcher(global_dispatcher, conn_address, False,
                                   None)
     L2tpv3GlobalSettings.Dispatcher = dispatcher
     L2tpv3GlobalSettings.LocalIPAddress = '127.13.2.4'
     avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ)
     avp2 = Hostname("TestAVP")
     avps = [avp1, avp2]
     pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps,
                               "testL2tpv3ControlPacket")
     self.assertIsNone(pkt.Session)
     conn = L2tpConnection(2, 2, conn_address)
     session = L2tpv3Session(1, 2, 'receiver', conn)
     pkt.SetPktSession(session)
     self.assertIsNotNone(pkt.Session)
     conn.CloseConnection()
Пример #29
0
    def test_recvStopCCN(self):
        conn_address = '127.11.2.19'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.21'
        conn = L2tpConnection(20, 20, conn_address, local_addr)
        session = L2tpv3Session(1, 2, 'receive', conn)
        conn.addSession(session)
        # receive sccrq, new state->wait-ctl-conn
        avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ)
        avp2 = AssignedControlConnectionID(10)
        avps = [avp1, avp2]
        sccrq = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        sccrp = conn.HandlePkt(sccrq)
        self.assertEqual(
            conn.fsm.current, L2tpv3ConnectionFsm.StateWaitCtlConn)

        # receive SCCCN, new state->established
        avp1 = ControlMessageAVP(ControlMessageAVP.SCCCN)
        avp2 = AssignedControlConnectionID(10)
        avps = [avp1, avp2]
        scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        ret = conn.HandlePkt(scccn)
        self.assertEqual(
            conn.fsm.current, L2tpv3ConnectionFsm.StateEstablished)

        # receive a stopCCN, new state -> idle
        localSessionID = session.remoteSessionId
        localAvp = LocalSessionID(localSessionID)

        avp = ControlMessageAVP(ControlMessageAVP.StopCCN)
        avps = [avp]
        avps.append(localAvp)
        stopCCN = L2tpv3ControlPacket(0, 0, 1, avps)
        self.assertEqual(stopCCN.length, 30)
        self.assertEqual(stopCCN.avps[0].messageType, 4)

        ret = conn.HandlePkt(stopCCN)
        self.assertEqual(conn.fsm.current, L2tpv3ConnectionFsm.StateIdle)
Пример #30
0
    def testHandlePkt_WithoutAvps(self):
        """Handle control packet: without avps.

        :return:

        """
        conn_address = '127.11.2.5'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, conn_address, True, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        L2tpv3GlobalSettings.MustAvpsCheck = False

        local_addr = '127.11.2.7'
        conn = L2tpConnection(6, 6, conn_address, local_addr)
        avps = list()
        pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps)
        ret = conn.HandlePkt(pkt)
        self.assertIsNone(ret)
        conn.CloseConnection()