IP address data type

This is an abstract data type and cannot be used directly with standard C unary or binary operators. Only local or global variables of type ip_addr_t are supported. A variable of this type can also be stored in an associate array either as a key or as a value.

Vue supports the following characteristics and operations for the IP address type variables:

Declaration of IP address variable
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.

The qualifiers signed, unsigned, register, static, thread and kernel are not supported for the ip_addr_t type variables.

Assignment operation

The assignment (=) operator allows a ip_addr_t type variable to be assigned to another ip_addr_t type variable and also it allows constant IP address or hostname to be assigned to ip_addr_t type variable. The original values of the variable is overwritten. No type casting is allowed from or to the ip_addr_t variable types.

In the following example, the content of the ip_addr_t i1 is assigned to 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.

Comparison operation

Only equality (==) and inequality (! =) operators are allowed for ip_addr_t types variables. The comparison allowed only between two ip_addr_t type variables and with constant string type (IP address or hostnames are provided in double quotes “192.168.1.1” or “example.com").

The result of the equality operator is True (1) if both contains the same IP address type (IPV4 or IPV6) and values. or False (0) otherwise. The inequality operator is the exact compliment of that. No other comparison operators (>=, >, < or =<) are allowed for the ip_addr_t type variables.

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”);
 
Printing IP address type variables

A ip_addr_t type variable can be printed with “%I” format specifier to print IP address in dotted decimal or hex format and “%H” format specifier to print hostname in the printf() function of Vue. This printing hostname involves a time consuming dns lookup operation. Hence it should be judiciously used in VUE scripts.

Note: When user uses the format specifier “%H” to print host name for IP address which may not exists in dns, for those IP addresses, it prints the IP addresses in dotted decimal/hex format instead of hostname.
An associative array that has ip_addr_t type variables as key or value (or both) can be printed using the print() function.
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); 
Limitations for IP address type variable
  • The array of the ip_addr_t variable cannot be declared.
  • Pointer to ip_addr_t variable is not allowed.
  • Typecasting of ip_addr_t variable to any other type or typecasting any other type to ip_addr_t type is not allowed.
  • No arithmetic operator (+, -, *, /, ++, -- etc) can be used with ip_addr_t type variable.