Example #1
0
    def test_append_VspAvp(self):
        print "test_append_VspAvp\n"

        vsp_avp_attrType1 = 10
        vsp_avp_attrType2 = 20
        vsp_avp1 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType1,
                                         attrValue="11111111111")
        vsp_avp2 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType2,
                                         notifyVendor=1,
                                         attrValue="222222222222")

        # vsp_avp3 has OutCtrlPktList set to L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP, and it will be
        # appended to the avps list, and vsp_avp1 will not.
        ctrlIdsList = []
        ctrlIdsList.append(L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP)
        vsp_avp_attrType3 = 30
        vsp_avp3_attrValue = "3333333333"
        vsp_avp3 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType3,
                                         OutCtrlIds=ctrlIdsList,
                                         attrValue=vsp_avp3_attrValue)

        vsp_avps = l2tpv3VspAvps()
        self.assertTrue(vsp_avps is not None)
        vsp_avps.add_VspAvp(vsp_avp1)
        vsp_avps.add_VspAvp(vsp_avp2)
        vsp_avps.add_VspAvp(vsp_avp3)

        avps = list()
        avps.append(
            L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP))
        avps.append(
            L2tpv3RFC3931AVPs.SbfdVccv(L2tpv3RFC3931AVPs.SbfdVccv.VccvValue))

        vsp_avps.append_VspAvp(avps, L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP)
        # vsp_avp3 should be in avps list now
        found_vsp_avp_in_icrp = 0
        for avp in avps:
            if (isinstance(avp, l2tpv3SampleVendorAvp)
                    and avp.attrValue == vsp_avp3_attrValue):
                found_vsp_avp_in_icrp = 1
        self.assertEqual(found_vsp_avp_in_icrp, 1)
Example #2
0
    def ReceiveICRQ(self, pkt):
        """Receive a ICRQ from remote, if it is a good ICRQ, will send a ICRP.

        :param pkt: The ICRQ control packet, has been decoded.
        :return: ICRP packet or None

        """

        if L2tpv3GlobalSettings.L2tpv3GlobalSettings.MustAvpsCheck is True:
            ret = self.connection.checkMustAvps(
                L2tpv3ControlPacket.L2tpv3ControlPacket.ICRQMandatoryAVPs,
                pkt.avps)
            if ret is not True:
                self.fsm.recvBadICRQ()
                return L2tpv3ControlPacket.L2tpv3CDN(
                    self, 2, 4, "Avp cannot be handled correctly")

        self.logger.debug("L2tp session[%d, %d] receives a ICRQ message.",
                          self.localSessionId, self.remoteSessionId)

        avps = list()
        avps.append(
            L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP))
        avps.append(
            L2tpv3RFC3931AVPs.DataSequencing(
                L2tpv3RFC3931AVPs.DataSequencing.AllSeq))
        # TODO  add sbfd support for ipv6
        if Convert.is_valid_ipv4_address(self.connection.localAddr):
            avps.append(
                L2tpv3RFC3931AVPs.SbfdDiscriminator(
                    int(
                        socket.inet_aton(
                            self.connection.localAddr).encode('hex'), 16)))
            avps.append(
                L2tpv3RFC3931AVPs.SbfdVccv(
                    L2tpv3RFC3931AVPs.SbfdVccv.VccvValue))

        # Need add some Cable labs avp
        self.logger.debug(
            "Session [%d, %d]sends a ICRP packet to remote, connection:%d",
            self.localSessionId, self.remoteSessionId,
            pkt.Connection.remoteConnID)

        icrp = L2tpv3ControlPacket.L2tpv3ControlPacket(
            pkt.Connection.remoteConnID, 0, 0, avps)
        del self.avps_icrq[:]
        del self.mcast[:]
        if len(pkt.avps) > 1:
            for i in xrange(1, len(pkt.avps)):
                avp = pkt.avps[i]
                if isinstance(
                        avp,
                        L2tpv3CableLabsAvps.DepiL2SpecificSublayerSubtype):
                    self.session_l2Sublayer = avp.pw_type
                self.avps_icrq.append(avp)
                # We got a bad ICRQ, we should send a CDN
                if not avp.handleAvp(pkt, icrp):
                    self.fsm.recvBadICRQ()
                    return L2tpv3ControlPacket.L2tpv3CDN(
                        self, 2, 4, "Avp cannot be handled correctly")
        self.logger.debug("We got a good ICRQ, send to fsm")
        self.fsm.recvGoodICRQ()
        return icrp