def __get_filter_key(self, filter_param): key_items = OrderedDict() if Tc.Param.HANDLE in filter_param: handle = filter_param.get(Tc.Param.HANDLE) typepy.Integer(handle).validate() handle = int(handle) for mangle in self.__iptables_ctrl.parse(): if mangle.mark_id != handle: continue key_items[Tc.Param.DST_NETWORK] = mangle.destination if typepy.is_not_null_string(mangle.source): key_items[Tc.Param.SRC_NETWORK] = mangle.source key_items[Tc.Param.PROTOCOL] = mangle.protocol break else: raise ValueError("mangle mark not found: {}".format(mangle)) else: src_network = filter_param.get(Tc.Param.SRC_NETWORK) if typepy.is_not_null_string( src_network) and not is_anywhere_network( src_network, self.__ip_version): key_items[Tc.Param.SRC_NETWORK] = src_network dst_network = filter_param.get(Tc.Param.DST_NETWORK) if typepy.is_not_null_string( dst_network) and not is_anywhere_network( dst_network, self.__ip_version): key_items[Tc.Param.DST_NETWORK] = dst_network src_port = filter_param.get(Tc.Param.SRC_PORT) if typepy.Integer(src_port).is_type(): key_items[Tc.Param.SRC_PORT] = "{}".format(src_port) elif src_port is not None: self.__logger.warning( "expected a integer value for {}, actual {}: {}".format( Tc.Param.SRC_PORT, type(src_port), src_port, )) dst_port = filter_param.get(Tc.Param.DST_PORT) if typepy.Integer(dst_port).is_type(): key_items[Tc.Param.DST_PORT] = "{}".format(dst_port) elif src_port is not None: self.__logger.warning( "expected a integer value for {}, actual {}".format( Tc.Param.DST_PORT, type(dst_port))) protocol = filter_param.get(Tc.Param.PROTOCOL) if typepy.is_not_null_string(protocol): key_items[Tc.Param.PROTOCOL] = protocol key = ", ".join( ["{}={}".format(key, value) for key, value in key_items.items()]) return key, key_items
def __set_pre_network_filter(self): if self._is_use_iptables(): return 0 if all( [ typepy.is_null_string(self._tc_obj.dst_network), not typepy.Integer(self._tc_obj.dst_port).is_type(), ] ): flowid = "{:s}:{:d}".format(self._tc_obj.qdisc_major_id_str, self._get_qdisc_minor_id()) else: flowid = "{:s}:2".format(self._tc_obj.qdisc_major_id_str) return SubprocessRunner( " ".join( [ self._tc_obj.get_tc_command(TcSubCommand.FILTER), self._dev, "protocol {:s}".format(self._tc_obj.protocol), "parent {:s}:".format(self._tc_obj.qdisc_major_id_str), "prio 2 u32 match {:s} {:s} {:s}".format( self._tc_obj.protocol, self._get_network_direction_str(), get_anywhere_network(self._tc_obj.ip_version), ), "flowid {:s}".format(flowid), ] ) ).run()
def get_value(self): self.__preprocess() value_fmt = "{:f}{:s}" if typepy.Integer(self.__number, strict_level=typepy.StrictLevel.MAX).is_type(): value_fmt = "{:d}{:s}" return value_fmt.format(self.__number, self.__unit)
def get_anywhere_network(ip_version): ip_version_n = typepy.Integer(ip_version).try_convert() if ip_version_n == 4: return Network.Ipv4.ANYWHERE if ip_version_n == 6: return Network.Ipv6.ANYWHERE raise ValueError("unknown ip version: {}".format(ip_version))
def __modify_item(data): try: return typepy.Integer(data).convert() except typepy.TypeConversionError: pass try: return typepy.RealNumber(data).convert() except typepy.TypeConversionError: pass return MultiByteStrDecoder(data).unicode_str
def __normalize(self): number = typepy.Integer(self.__number).try_convert() if number is not None: self.__number = number if self.__unit in self.__VALID_SEC_UNIT_LIST: self.__unit = "sec" elif self.__unit in self.__VALID_MSEC_UNIT_LIST: self.__unit = "ms" elif self.__unit in self.__VALID_USEC_UNIT_LIST: self.__unit = "us" elif self.__unit in self.__VALID_MINUTE_UNIT_LIST: self.__number *= 60 self.__unit = "sec"
def to_query(self) -> str: value = self._value if value is None: return "NULL" if typepy.Integer(value).is_type() or typepy.RealNumber( value).is_type(): return str(value) try: if value.find("'") != -1: return f'"{value}"' except (TypeError, AttributeError): pass return f"'{value}'"
def to_query(self): value = self._value if value is None: return "NULL" if typepy.Integer(value).is_type() or typepy.RealNumber( value).is_type(): return six.text_type(value) try: if value.find("'") != -1: return '"{}"'.format(value) except (TypeError, AttributeError): pass return "'{}'".format(value)
def __get_unique_qdisc_minor_id(self): if not is_execute_tc_command(self._tc_obj.tc_command_output): return (int( self._tc_obj.netem_param.calc_hash( self._tc_obj.make_srcdst_text())[-2:], 16) + 1) if self._tc_obj.is_change_shaping_rule: self.__qdisc_minor_id_count += 1 return self.__DEFAULT_CLASS_MINOR_ID + self.__qdisc_minor_id_count exist_class_item_list = re.findall( "class {algorithm:s} {qdisc_major_id:s}:[0-9]+".format( algorithm=ShapingAlgorithm.HTB, qdisc_major_id=self._tc_obj.qdisc_major_id_str), run_tc_show(TcSubCommand.CLASS, self._tc_device, self._tc_obj.tc_command_output), re.MULTILINE, ) exist_class_minor_id_list = [] for class_item in exist_class_item_list: try: exist_class_minor_id_list.append( typepy.Integer(class_item.split(":")[1]).convert()) except typepy.TypeConversionError: continue logger.debug("existing class list with {:s}: {}".format( self._dev, exist_class_item_list)) logger.debug("existing minor classid list with {:s}: {}".format( self._dev, exist_class_minor_id_list)) next_minor_id = self.__DEFAULT_CLASS_MINOR_ID while True: if next_minor_id not in exist_class_minor_id_list: break next_minor_id += 1 return next_minor_id
def __get_unique_qdisc_minor_id(self): if (self._tc_obj.tc_command_output != TcCommandOutput.NOT_SET or self._tc_obj.is_change_shaping_rule): self.__qdisc_minor_id_count += 1 return self.__DEFAULT_CLASS_MINOR_ID + self.__qdisc_minor_id_count exist_class_item_list = re.findall( "class {algorithm:s} {qdisc_major_id:s}[\:][0-9]+".format( algorithm=ShapingAlgorithm.HTB, qdisc_major_id=self._tc_obj.qdisc_major_id_str), run_tc_show(TcSubCommand.CLASS, self._tc_device), re.MULTILINE, ) exist_class_minor_id_list = [] for class_item in exist_class_item_list: try: exist_class_minor_id_list.append( typepy.Integer(class_item.split(":")[1]).convert()) except typepy.TypeConversionError: continue logger.debug("existing class list with {:s}: {}".format( self._dev, exist_class_item_list)) logger.debug("existing minor classid list with {:s}: {}".format( self._dev, exist_class_minor_id_list)) next_minor_id = self.__DEFAULT_CLASS_MINOR_ID while True: if next_minor_id not in exist_class_minor_id_list: break next_minor_id += 1 return next_minor_id
def test_normal(self, method, strict_level, value, expected): assert convert_wrapper(typepy.Integer(value, strict_level), method) == expected