IP 地址数据类型
它是抽象数据类型,不能直接与标准 C 一元运算符或二目运算符配合使用。 仅类型为 ip_addr_t 的局部变量或全局变量受支持。 此类型的变量还可作为键或值存储在关联数组中。
Vue 支持 IP 地址类型变量的下列特征和操作:
IP 地址变量的声明
ip_addr_t i1; // global variable of type ip_addr_t
__auto ip_addr_t i2; // auto variable of type
ip_addr_t i2 = __ip4hdr->src_addr; // store source IP address in a local ip_addr_t variable.
ip_aso[“src_ip_addr”] = __ip4hdr->src_addr; // store in an associative array.
ip_addr_t 类型变量不支持 signed、unsigned、register、static、thread 和 kernel 限定符。
分配操作
赋值运算符 (=) 允许将 ip_addr_t 类型变量赋值给另一个 ip_addr_t 类型变量,它还允许将常量 IP 地址或主机名赋值给 ip_addr_t 类型变量。 该变量的原始值被覆盖。 不允许在 ip_addr_t 变量类型与其他类型之间进行强制类型转换。
在以下示例中,ip_addr_t i1 的内容被赋值给 i2。
ip_addr_t i1, i2; // Declares two IP address variables.
ip_addr_t i3, i4, i5; // Declares three IP address variables.
i1 = __ip4hdr->src_addr; // Get the source IP address of the packet in i1.
i2 = i1 ; // Get the content of i1 into i2.
i3 = “10.10.10.1”; // Assign the constant IPv4 address to i3 variable.
i4 = “fe80::2c0c:33ff:fe48:f903”; // Assign the Ipv6 address to i4 variable.
i5 = “example.com”; // Assign the hostname to i5 variable.
// Get the content of i1 into i2.
比较操作
对于 ip_addr_t 类型变量,仅允许使用等于 (==) 和不等于 (! =) 运算符。 仅允许在两个 ip_addr_t 类型变量之间或与常量字符串类型(用双引号引起来的 IP 地址或主机名,例如,“192.168.1.1”或“example.com”)进行此比较。
如果这两个运算符都包含相同的 IP 地址类型 (IPV4 或 IPV6) 和值,那么等于运算符的结果为 True (1)。 否则为 False (0)。 不等于运算符是此行为的确切补充。 ip_addr_t 类型变量不允许使用其他比较操作符(>=、>、< 或 =<)。
if( i1 == i2) // comparing two ip_addr_t type variables.
//IP address string
printf(“IP addresses are equal”);
else printf(“IP addresses are not equal”);
or
if( i1 == “192.168.1.1”) // comparing ip_addr_t type variable and constant string.
printf(“IP addresses are equal”);
else printf(“IP addresses are not equal”);
or
if (i1 = “example.com”) // comparing ip_addr_t type variable and constant
//IP address string
printf(“IP addresses are equal”);
else printf(“IP addresses are not equal”);
显示 IP 地址类型变量在 Vue 的 printf() 函数中,可使用“%I”格式说明符显示 ip_addr_t 类型变量来以点分十进制或十六进制格式显示 IP 地址,并可使用“%H”格式说明符显示主机名。 显示主机名涉及 DNS 查找操作,此操作非常耗时。 因此,在 Vue 脚本中使用时应非常谨慎。
注: 当用户使用格式说明符 "%H" 为 dns 中可能不存在的 IP 地址打印主机名时,对于这些 IP 地址,将以点分十进制/十六进制格式而不是主机名打印 IP 地址。
可使用 print() 函数显示将 ip_addr_t 类型变量用作键和/或值的关联数组。
printf(“ Source IP address=[%I]\n”, __ip4hdr->src_addr);
ip_aso[“src_ip_address”] = __ip4hdr->src_addr ; // Store source IP address as value in an associative array
print(ip_aso);
IP 地址类型变量的限制
- 无法声明 ip_addr_t 变量的数组。
- 不允许使用指向 ip_addr_t 变量的指针。
- 不允许将 ip_addr_t 变量强制转换为任何其他类型或将任何其他类型强制转换为 ip_addr_t 类型。
- 不能将算术运算符(+、-、*、/、++、-- 等等)与 ip_addr_t 类型变量配合使用。