UniRec  2.9.3
Data Structures | Functions
IP addresses API

Data Structures

union  ip_addr_t
 

Functions

INLINE int ip_is4 (const ip_addr_t *addr)
 
INLINE int ip_is6 (const ip_addr_t *addr)
 
INLINE uint32_t ip_get_v4_as_int (ip_addr_t *addr)
 
INLINE char * ip_get_v4_as_bytes (const ip_addr_t *addr)
 
INLINE ip_addr_t ip_from_int (uint32_t i)
 
INLINE ip_addr_t ip_from_4_bytes_be (char b[4])
 
INLINE ip_addr_t ip_from_4_bytes_le (char b[4])
 
INLINE ip_addr_t ip_from_16_bytes_be (char b[16])
 
INLINE ip_addr_t ip_from_16_bytes_le (char b[16])
 
INLINE int ip_cmp (const ip_addr_t *addr1, const ip_addr_t *addr2)
 
INLINE int ip_is_null (const ip_addr_t *addr)
 
INLINE int ip_from_str (const char *str, ip_addr_t *addr)
 
INLINE void ip_to_str (const ip_addr_t *addr, char *str)
 

Detailed Description

Structure and functions to handle generic IP addresses (IPv4 or IPv6). IP addresses are stored on 128 bits. IPv6 addresses are stored directly. IPv4 addresses are converted to 128 bit is this way:

0000:0000:0000:0000:<ipv4_addr>:ffff:ffff

No valid IPv6 address should look like this so it's possible to determine IP address version without explicitly storing any flag.Addresses are stored in big endian (network byte order).This implementation assumes the platform uses little endian (true for x86 architectures).

Layout of ip_addr_t union:
MSB LSB
xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
| i64[0] | i64[1] |
| i32[0] | i32[1] | i32[2] | i32[3] |
|bytes[0] ... ... bytes[15]|

Data Structure Documentation

◆ ip_addr_t

union ip_addr_t

IP address data type.

It is 128b data type. User can access to parts of the IP address in several ways.

Definition at line 101 of file ipaddr.h.

Data Fields
uint8_t bytes[16]

16x 8b numbers

uint16_t ui16[8]

8x 16b numbers

uint32_t ui32[4]

4x 32b numbers

uint64_t ui64[2]

2x 64b numbers

uint8_t ui8[16]

same as bytes

Function Documentation

◆ ip_cmp()

INLINE int ip_cmp ( const ip_addr_t addr1,
const ip_addr_t addr2 
)

Compare two IP addresses.

Parameters
[in]addr1IP address as ip_addr_t
[in]addr2IP address as ip_addr_t
Returns
Positive number (>0) if addr1 > addr2, negative number (<0) if addr1 < addr2, and zero (=0) if addr1 == addr2.

Definition at line 266 of file ipaddr.h.

◆ ip_from_16_bytes_be()

INLINE ip_addr_t ip_from_16_bytes_be ( char  b[16])

Convert 16B array (in network byte order) into IPv6 address encoded as ip_addr_t.

Parameters
[in]b16 bytes array in network byte order for conversion.
Returns
IPv6 address stored as ip_addr_t.

Definition at line 236 of file ipaddr.h.

◆ ip_from_16_bytes_le()

INLINE ip_addr_t ip_from_16_bytes_le ( char  b[16])

Convert 16B array (in little-endian byte order) into IPv6 address encoded as ip_addr_t.

Parameters
[in]b16 bytes array in little-endian byte order for conversion.
Returns
IPv6 address stored as ip_addr_t.

Definition at line 249 of file ipaddr.h.

◆ ip_from_4_bytes_be()

INLINE ip_addr_t ip_from_4_bytes_be ( char  b[4])

Convert 4B array (in network byte order) into IPv4 address encoded as ip_addr_t.

Parameters
[in]b4 bytes array in network byte order for conversion.
Returns
IPv4 address stored as ip_addr_t.
See also
ip_from_int()

Definition at line 199 of file ipaddr.h.

◆ ip_from_4_bytes_le()

INLINE ip_addr_t ip_from_4_bytes_le ( char  b[4])

Convert 4B array (in little-endian byte order) into IPv4 address encoded as ip_addr_t.

Parameters
[in]b4 bytes array in little-endian byte order for conversion.
Returns
IPv4 address stored as ip_addr_t.
See also
ip_from_int()

Definition at line 218 of file ipaddr.h.

◆ ip_from_int()

INLINE ip_addr_t ip_from_int ( uint32_t  i)

Convert 32b number into IPv4 address encoded as ip_addr_t.

Parameters
[in]i32b number in host byte order for conversion.
Returns
IPv4 address stored as ip_addr_t.

Definition at line 183 of file ipaddr.h.

◆ ip_from_str()

INLINE int ip_from_str ( const char *  str,
ip_addr_t addr 
)

Convert string into ip_addr_t.

Parameters
[in]strString for conversion.
[out]addrPointer to memory where to store converted IP address.
Returns
1 on success, 0 on error i.e. string is not a valid IP address.

Definition at line 301 of file ipaddr.h.

◆ ip_get_v4_as_bytes()

INLINE char* ip_get_v4_as_bytes ( const ip_addr_t addr)

Get pointer to byte array of IPv4 address.

The function returns pointer to the first byte of IP address data structure. Data is in network byte order.

Parameters
[in]addrIP address for conversion.
Returns
Pointer to the first byte of IPv4 address.

Definition at line 171 of file ipaddr.h.

◆ ip_get_v4_as_int()

INLINE uint32_t ip_get_v4_as_int ( ip_addr_t addr)

Get IPv4 address as one 32b number.

The function takes the appropriate 32b part of ip_addr_t and converts it into host byte order.

Parameters
[in]addrIP address for conversion.
Returns
IP address as one 32b number in host byte order.

Definition at line 157 of file ipaddr.h.

◆ ip_is4()

INLINE int ip_is4 ( const ip_addr_t addr)

Check if the address is IPv4.

Parameters
[in]addrIP address that is checked.
Returns
1 if the address is IPv4, 0 otherwise.

Definition at line 131 of file ipaddr.h.

◆ ip_is6()

INLINE int ip_is6 ( const ip_addr_t addr)

Check if the address is IPv6.

Parameters
[in]addrIP address that is checked.
Returns
1 if the address is IPv6, 0 otherwise.
Note
It is a negation of ip_is4().

Definition at line 143 of file ipaddr.h.

◆ ip_is_null()

INLINE int ip_is_null ( const ip_addr_t addr)

Check if IP address is NULL/invalid/unspecified?

There are two variants being used as invalid/unspecified address:

00000000 00000000 00000000 ffffffff (i.e. "0.0.0.0", zero IPv4)
00000000 00000000 00000000 00000000 (i.e. "::", zero IPv6)
Parameters
[in]addrIP address to check.
Returns
1 if given address matches any of the ones above, otherwise returns 0.

Definition at line 283 of file ipaddr.h.

◆ ip_to_str()

INLINE void ip_to_str ( const ip_addr_t addr,
char *  str 
)

Convert ip_addr_t into string.

Parameters
[in]addrPointer to IP address.
[out]strPointer to memory where to store converted IP address. It must be of at least INET6_ADDRSTRLEN size.

Definition at line 325 of file ipaddr.h.