ipv6_compare()ipv6_compare()

比较两个 IPv6 或 IPv4 网络地址字符串。Compares two IPv6 or IPv4 network address strings. 分析并比较两个 IPv6 字符串,同时考虑根据参数前缀和可选的 PrefixMask 参数计算出的组合 IP 前缀掩码。The two IPv6 strings are parsed and compared while accounting for the combined IP-prefix mask calculated from argument prefixes, and the optional PrefixMask argument.

ipv6_compare('::ffff:7f00:1', '127.0.0.1') == 0
ipv6_compare('fe80::85d:e82c:9446:7994', 'fe80::85d:e82c:9446:7995')  < 0
ipv6_compare('192.168.1.1/24', '192.168.1.255/24') == 0
ipv6_compare('fe80::85d:e82c:9446:7994/127', 'fe80::85d:e82c:9446:7995/127') == 0
ipv6_compare('fe80::85d:e82c:9446:7994', 'fe80::85d:e82c:9446:7995', 127) == 0

语法Syntax

ipv6_compare(Expr1, Expr2[ ,PrefixMask])ipv6_compare(Expr1, Expr2[ ,PrefixMask])

参数Arguments

  • Expr1、Expr2:表示 IPv6 或 IPv4 地址的字符串表达式。Expr1, Expr2: A string expression representing an IPv6 or IPv4 address. 可以使用 IP 前缀表示法(请参见注释)对 IPv6 和 IPv4 字符串进行掩码操作。IPv6 and IPv4 strings can be masked using IP-prefix notation (see note).
  • PrefixMask:从 0 到 128 的整数,表示所考虑的最有效位的数目。PrefixMask: An integer from 0 to 128 representing the number of most significant bits that are taken into account.

备注

IP 前缀表示法IP-prefix notation

常见的做法是通过斜杠 (/) 字符使用 IP-prefix notation 定义 IP 地址。It's common practice to define IP addresses with IP-prefix notation using a slash (/) character. 斜杠 (/) 左侧的 IP 地址是基本 IP 地址,斜杠 (/) 右侧的数字(1 到 127)是网络掩码中连续位的数目。The IP address to the LEFT of the slash (/) is the base IP address, and the number (1 to 127) to the RIGHT of the slash (/) is the number of contiguous 1 bits in the netmask.

示例:fe80::85d:e82c:9446:7994/120 将有一个包含 120 个连续位的关联网络/子网掩码。Example: fe80::85d:e82c:9446:7994/120 will have an associated net/subnetmask containing 120 contiguous bits.

返回Returns

  • 0:如果第一个 IPv6 字符串参数的长表示形式等于第二个 IPv6 字符串参数。0: If the long representation of the first IPv6 string argument is equal to the second IPv6 string argument.
  • 1:如果第一个 IPv6 字符串参数的长表示形式大于第二个 IPv6 字符串参数。1: If the long representation of the first IPv6 string argument is greater than the second IPv6 string argument.
  • -1:如果第一个 IPv6 字符串参数的长表示形式小于第二个 IPv6 字符串参数。-1: If the long representation of the first IPv6 string argument is less than the second IPv6 string argument.
  • null:如果两个 IPv6 字符串之一转换不成功。null: If conversion for one of the two IPv6 strings wasn't successful.

备注

此函数可以接受并比较表示 IPv6 和 IPv4 网络地址的参数。The function can accept and compare arguments representing both IPv6 and IPv4 network addresses. 但是,如果调用方知道参数采用 IPv4 格式,请使用 ipv4_is_compare() 函数。However, if the caller knows that arguments are in IPv4 format, use ipv4_is_compare() function. 此函数将产生更好的运行时性能。This function will result in better runtime performance.

示例:IPv6/IPv4 比较相等的情况Examples: IPv6/IPv4 comparison equality cases

使用 IPv6/IPv4 字符串中指定的 IP 前缀表示法比较 IPCompare IPs using the IP-prefix notation specified inside the IPv6/IPv4 strings

datatable(ip1_string:string, ip2_string:string)
[
 // IPv4 are compared as IPv6 addresses
 '192.168.1.1',    '192.168.1.1',       // Equal IPs
 '192.168.1.1/24', '192.168.1.255',     // 24 bit IP4-prefix is used for comparison
 '192.168.1.1',    '192.168.1.255/24',  // 24 bit IP4-prefix is used for comparison
 '192.168.1.1/30', '192.168.1.255/24',  // 24 bit IP4-prefix is used for comparison
  // IPv6 cases
 'fe80::85d:e82c:9446:7994', 'fe80::85d:e82c:9446:7994',         // Equal IPs
 'fe80::85d:e82c:9446:7994/120', 'fe80::85d:e82c:9446:7998',     // 120 bit IP6-prefix is used for comparison
 'fe80::85d:e82c:9446:7994', 'fe80::85d:e82c:9446:7998/120',     // 120 bit IP6-prefix is used for comparison
 'fe80::85d:e82c:9446:7994/120', 'fe80::85d:e82c:9446:7998/120', // 120 bit IP6-prefix is used for comparison
 // Mixed case of IPv4 and IPv6
 '192.168.1.1',      '::ffff:c0a8:0101', // Equal IPs
 '192.168.1.1/24',   '::ffff:c0a8:01ff', // 24 bit IP-prefix is used for comparison
 '::ffff:c0a8:0101', '192.168.1.255/24', // 24 bit IP-prefix is used for comparison
 '::192.168.1.1/30', '192.168.1.255/24', // 24 bit IP-prefix is used for comparison
]
| extend result = ipv6_compare(ip1_string, ip2_string)
ip1_stringip1_string ip2_stringip2_string resultresult
192.168.1.1192.168.1.1 192.168.1.1192.168.1.1 00
192.168.1.1/24192.168.1.1/24 192.168.1.255192.168.1.255 00
192.168.1.1192.168.1.1 192.168.1.255/24192.168.1.255/24 00
192.168.1.1/30192.168.1.1/30 192.168.1.255/24192.168.1.255/24 00
fe80::85d:e82c:9446:7994fe80::85d:e82c:9446:7994 fe80::85d:e82c:9446:7994fe80::85d:e82c:9446:7994 00
fe80::85d:e82c:9446:7994/120fe80::85d:e82c:9446:7994/120 fe80::85d:e82c:9446:7998fe80::85d:e82c:9446:7998 00
fe80::85d:e82c:9446:7994fe80::85d:e82c:9446:7994 fe80::85d:e82c:9446:7998/120fe80::85d:e82c:9446:7998/120 00
fe80::85d:e82c:9446:7994/120fe80::85d:e82c:9446:7994/120 fe80::85d:e82c:9446:7998/120fe80::85d:e82c:9446:7998/120 00
192.168.1.1192.168.1.1 ::ffff:c0a8:0101::ffff:c0a8:0101 00
192.168.1.1/24192.168.1.1/24 ::ffff:c0a8:01ff::ffff:c0a8:01ff 00
::ffff:c0a8:0101::ffff:c0a8:0101 192.168.1.255/24192.168.1.255/24 00
::192.168.1.1/30::192.168.1.1/30 192.168.1.255/24192.168.1.255/24 00

使用 IPv6/IPv4 字符串中指定的 IP 前缀表示法作为 ipv6_compare() 函数的附加参数比较 IPCompare IPs using IP-prefix notation specified inside the IPv6/IPv4 strings and as additional argument of the ipv6_compare() function

datatable(ip1_string:string, ip2_string:string, prefix:long)
[
 // IPv4 are compared as IPv6 addresses 
 '192.168.1.1',    '192.168.1.0',   31, // 31 bit IP4-prefix is used for comparison
 '192.168.1.1/24', '192.168.1.255', 31, // 24 bit IP4-prefix is used for comparison
 '192.168.1.1',    '192.168.1.255', 24, // 24 bit IP4-prefix is used for comparison
   // IPv6 cases
 'fe80::85d:e82c:9446:7994', 'fe80::85d:e82c:9446:7995',     127, // 127 bit IP6-prefix is used for comparison
 'fe80::85d:e82c:9446:7994/127', 'fe80::85d:e82c:9446:7998', 120, // 120 bit IP6-prefix is used for comparison
 'fe80::85d:e82c:9446:7994/120', 'fe80::85d:e82c:9446:7998', 127, // 120 bit IP6-prefix is used for comparison
 // Mixed case of IPv4 and IPv6
 '192.168.1.1/24',   '::ffff:c0a8:01ff', 127, // 127 bit IP6-prefix is used for comparison
 '::ffff:c0a8:0101', '192.168.1.255',    120, // 120 bit IP6-prefix is used for comparison
 '::192.168.1.1/30', '192.168.1.255/24', 127, // 120 bit IP6-prefix is used for comparison
]
| extend result = ipv6_compare(ip1_string, ip2_string, prefix)
ip1_stringip1_string ip2_stringip2_string 前缀prefix resultresult
192.168.1.1192.168.1.1 192.168.1.0192.168.1.0 3131 00
192.168.1.1/24192.168.1.1/24 192.168.1.255192.168.1.255 3131 00
192.168.1.1192.168.1.1 192.168.1.255192.168.1.255 2424 00
fe80::85d:e82c:9446:7994fe80::85d:e82c:9446:7994 fe80::85d:e82c:9446:7995fe80::85d:e82c:9446:7995 127127 00
fe80::85d:e82c:9446:7994/127fe80::85d:e82c:9446:7994/127 fe80::85d:e82c:9446:7998fe80::85d:e82c:9446:7998 120120 00
fe80::85d:e82c:9446:7994/120fe80::85d:e82c:9446:7994/120 fe80::85d:e82c:9446:7998fe80::85d:e82c:9446:7998 127127 00
192.168.1.1/24192.168.1.1/24 ::ffff:c0a8:01ff::ffff:c0a8:01ff 127127 00
::ffff:c0a8:0101::ffff:c0a8:0101 192.168.1.255192.168.1.255 120120 00
::192.168.1.1/30::192.168.1.1/30 192.168.1.255/24192.168.1.255/24 127127 00