Esempio n. 1
0
    def get_ippkt2sLan_from_cLan(self, session_id, mbuf):
        if self.__is_ipv6:
            offset = 8
            size = 16
        else:
            offset = 12
            size = 4

        mbuf.offset = offset

        clan_saddr = mbuf.get_part(size)
        slan_saddr = self.find_sLanAddr_by_cLanAddr(session_id, clan_saddr)

        if not slan_saddr:
            try:
                slan_saddr = self.__ip_alloc.get_addr()
            except ipaddr.IpaddrNoEnoughErr:
                return False
            self.add2Lan(session_id, clan_saddr, slan_saddr)

        if not self.__is_ipv6:
            ippkts.modify_ip4address(slan_saddr, mbuf, flags=0)
        else:
            ippkts.modify_ip6address(slan_saddr, mbuf, flags=0)
        self.__timer.set_timeout(slan_saddr, self.__VALID_TIME)

        return True
Esempio n. 2
0
    def get_ippkt2cLan_from_sLan(self, mbuf):
        if self.__is_ipv6:
            offset = 24
            size = 16
        else:
            offset = 16
            size = 4

        mbuf.offset = offset

        slan_daddr = mbuf.get_part(size)
        rs = self.find_cLanAddr_by_sLanAddr(slan_daddr)

        if not rs: return (
                False,
                None,
        )

        if not self.__is_ipv6:
            ippkts.modify_ip4address(rs["clan_addr"], mbuf, flags=1)
        else:
            ippkts.modify_ip6address(rs["clan_addr"], mbuf, flags=1)
        self.__timer.set_timeout(slan_daddr, self.__VALID_TIME)

        return (
            True,
            rs["session_id"],
        )
Esempio n. 3
0
    def get_ippkt2sLan_from_cLan(self, session_id, mbuf):
        if self.__is_ipv6:
            offset = 8
            size = 16
        else:
            offset = 12
            size = 4

        mbuf.offset = offset

        clan_saddr = mbuf.get_part(size)
        slan_saddr = self.find_sLanAddr_by_cLanAddr(session_id, clan_saddr)

        if not slan_saddr:
            try:
                slan_saddr = self.__ip_alloc.get_addr()
            except ipaddr.IpaddrNoEnoughErr:
                return False
            self.add2Lan(session_id, clan_saddr, slan_saddr)

        if not self.__is_ipv6:
            ippkts.modify_ip4address(slan_saddr, mbuf, flags=0)
        else:
            ippkts.modify_ip6address(slan_saddr, mbuf, flags=0)
        self.__timer.set_timeout(slan_saddr, self.__VALID_TIME)

        return True
Esempio n. 4
0
    def __handle_ipv6_data_from_tunnel(self):
        self.__mbuf.offset = 4
        payload_length = utils.bytes2number(self.__mbuf.get_part(2))
        if payload_length + 40 != self.__mbuf.payload_size: return

        self.__mbuf.offset = 6
        nexthdr = self.__mbuf.get_part(1)

        if nexthdr not in self.__support_protocols: return

        self.__mbuf.offset = 24
        byte_dst_addr = self.__mbuf.get_part(16)
        self.__mbuf.offset = 42
        byte_dst_port = self.__mbuf.get_part(2)
        dst_port = utils.bytes2number(byte_dst_port)

        rule = self.__port_mapv6.find_rule_for_in(byte_dst_addr, nexthdr,
                                                  dst_port)
        if not rule: return

        self.__mbuf.offset = 8
        byte_src_addr = self.__mbuf.get_part(16)
        src_addr = socket.inet_ntop(socket.AF_INET6, byte_src_addr)
        k = "%s/132" % src_addr

        if k not in self.__routes:
            self.set_route(src_addr, prefix=132, is_ipv6=True)
        # 此处重写IP地址
        byte_rewrite, extra_data = rule
        ippkts.modify_ip6address(byte_rewrite, self.__mbuf, flags=1)
        self.__mbuf.offset = 0
        byte_data = self.__mbuf.get_data()
        self.get_handler(self.__tundev_fileno).msg_from_tunnel(byte_data)
Esempio n. 5
0
    def __handle_ipv6_msg_from_tundev(self):
        self.__mbuf.offset = 6
        nexthdr = self.__mbuf.get_part(1)
        self.__mbuf.offset = 8
        byte_saddr = self.__mbuf.get_part(16)

        if nexthdr not in self.__support_protocols: return

        self.__mbuf.offset = 40

        byte_sport = self.__mbuf.get_part(2)
        src_port = utils.bytes2number(byte_sport)

        rules = self.__port_mapv6.find_rule_for_out(byte_saddr, nexthdr,
                                                    src_port)
        if not rules: return

        in_key, byte_src_ip, p, port = rules
        ippkts.modify_ip6address(byte_saddr, self.__mbuf, flags=0)
        ippkts.modify_port(port, self.__mbuf, flags=0)

        self.__mbuf.offset = 0
        message = self.__mbuf.get_data()

        self.send_msg_to_tunnel(proto_utils.ACT_IPDATA, message)
Esempio n. 6
0
    def get_ippkt2cLan_from_sLan(self, mbuf):
        if self.__is_ipv6:
            offset = 24
            size = 16
        else:
            offset = 16
            size = 4

        mbuf.offset = offset

        slan_daddr = mbuf.get_part(size)
        rs = self.find_cLanAddr_by_sLanAddr(slan_daddr)

        if not rs: return (False, None,)

        if not self.__is_ipv6:
            ippkts.modify_ip4address(rs["clan_addr"], mbuf, flags=1)
        else:
            ippkts.modify_ip6address(rs["clan_addr"], mbuf, flags=1)

        return (True, rs["session_id"],)