IP アドレス・データ型

これは抽象データ型であり、標準 C の単項演算子および 2 項演算子と直接使用することはできません。 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.

signed、unsigned、register、static、thread、および kernel の各修飾子は、ip_addr_t 型変数ではサポートされていません。

割り当て演算

代入 (=) 演算子では、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 型変数では、等価 (==) 演算子と不等価 (! =) 演算子のみが許可されます。 この比較では、2 つの 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 アドレス型変数の表示

ip_addr_t 型変数は、IP アドレスをドット 10 進形式または 16 進形式で表示する場合は「%I」フォーマット指定子を使用して表示でき、Vue の printf() 関数でホスト名を表示する場合は「%H」フォーマット指定子を使用して表示できます。 このホスト名の表示には、時間のかかる DNS ルックアップ操作が含まれます。 そのため、VUE スクリプトで慎重に使用する必要があります。

注: ユーザーがフォーマット指定子「%H」を使用して、DNS に存在しない可能性がある IP アドレスのホスト名を出力する場合、それらの IP アドレスについては、ホスト名ではなくドット 10 進/16 進形式で IP アドレスを出力します。
ip_addr_t 型変数をキーまたは値 (もしくはその両方) として持つ連想配列は、print() 関数を使用して表示できます。
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 型変数と一緒に使用することはできません。