lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_inet

Overview

POSIX-like identifiers for IP addresses and network interfaces.

#include "lwpa_inet.h"

Data Structures

struct  LwpaIpAddr
 An IP address. More...
 
struct  LwpaSockaddr
 An IP address with associated interface and port. More...
 
struct  LwpaNetintInfo
 A description of a network interface. More...
 

Macros

#define IPV6_BYTES   16
 The number of bytes in an IPv6 address.
 
#define lwpasock_ip_port_equal(sockptr1, sockptr2)    (lwpaip_equal(&(sockptr1)->ip, &(sockptr2)->ip) && ((sockptr1)->port == (sockptr2)->port))
 Determine whether two instances of LwpaSockaddr contain identical IP addresses and ports. More...
 
#define NETINTINFO_MAC_LEN   6
 
#define NETINTINFO_NAME_LEN   64
 

Typedefs

typedef struct LwpaIpAddr LwpaIpAddr
 An IP address. More...
 
typedef struct LwpaSockaddr LwpaSockaddr
 An IP address with associated interface and port. More...
 
typedef struct LwpaNetintInfo LwpaNetintInfo
 A description of a network interface.
 

Enumerations

enum  lwpa_iptype_t { LWPA_IP_INVALID = 0 , LWPA_IPV4 = 1 , LWPA_IPV6 = 2 }
 Used to identify the type of IP address contained in a LwpaIpAddr. More...
 

lwpa_ip macros

A set of macros for interacting with a LwpaIpAddr.

It is recommended to use these macros where possible rather than accessing the structure members directly; otherwise, it's easy to forget to fill in a field.

#define lwpaip_is_v4(lwpa_ip_ptr)   ((lwpa_ip_ptr)->type == LWPA_IPV4)
 Determine whether a LwpaIpAddr contains an IPv4 address. More...
 
#define lwpaip_is_v6(lwpa_ip_ptr)   ((lwpa_ip_ptr)->type == LWPA_IPV6)
 Determine whether a LwpaIpAddr contains an IPv6 address. More...
 
#define lwpaip_is_invalid(lwpa_ip_ptr)   ((lwpa_ip_ptr)->type == LWPA_IP_INVALID)
 Determine whether a LwpaIpAddr contains an invalid address. More...
 
#define lwpaip_v4_address(lwpa_ip_ptr)   ((lwpa_ip_ptr)->addr.v4)
 Get the IPv4 address from a LwpaIpAddr. More...
 
#define lwpaip_v6_address(lwpa_ip_ptr)   ((lwpa_ip_ptr)->addr.v6)
 Get the IPv6 address from a LwpaIpAddr. More...
 
#define lwpaip_set_v4_address(lwpa_ip_ptr, val)
 Set the IPv4 address in a LwpaIpAddr. More...
 
#define lwpaip_set_v6_address(lwpa_ip_ptr, val)
 Set the IPv6 address in a LwpaIpAddr. More...
 
#define lwpaip_set_invalid(lwpa_ip_ptr)   ((lwpa_ip_ptr)->type = LWPA_IP_INVALID)
 Set the type field in a LwpaIpAddr to indicate that it does not contain a valid address. More...
 
#define lwpaip_is_multicast(lwpa_ip_ptr)
 Determine whether a LwpaIpAddr contains a multicast address. More...
 
#define lwpaip_equal(ipptr1, ipptr2)
 Determine whether two instances of LwpaIpAddr contain identical addresses. More...
 
#define lwpaip_cmp(ipptr1, ipptr2)
 A comparison algorithm for lwpa_ip addresses. More...
 
#define LWPA_INADDR_ANY   0
 
#define lwpaip_make_any_v4(lwpa_ip_ptr)   lwpaip_set_v4_address(lwpa_ip_ptr, LWPA_INADDR_ANY)
 Set a LwpaIpAddr to the IPv4 wildcard address. More...
 
#define lwpaip_make_any_v6(lwpa_ip_ptr)
 Set a LwpaIpAddr to the IPv6 wildcard address. More...
 

Macro Definition Documentation

◆ lwpaip_cmp

#define lwpaip_cmp (   ipptr1,
  ipptr2 
)
Value:
(((ipptr1)->type != (ipptr2)->type) \
? ((ipptr1)->type - (ipptr2)->type) \
: (((ipptr1)->type == LWPA_IPV4) ? ((int)(ipptr1)->addr.v4 - (int)(ipptr2)->addr.v4) \
: memcmp((ipptr1)->addr.v6, (ipptr2)->addr.v6, IPV6_BYTES)))
#define IPV6_BYTES
The number of bytes in an IPv6 address.
Definition: lwpa_inet.h:50
@ LWPA_IPV4
This lwpa_ip contains an IPv4 address.
Definition: lwpa_inet.h:44

A comparison algorithm for lwpa_ip addresses.

All IPv4 addresses are considered to be < all IPv6 addresses. For matching types, the numerical address value is compared.

Parameters
ipptr1Pointer to the first LwpaIpAddr to compare.
ipptr2Pointer to the second LwpaIpAddr to compare.
Returns
< 0 (ipptr1 < ipptr2), 0 (ipptr1 == ipptr2), > 0 (ipptr1 > ipptr2)

◆ lwpaip_equal

#define lwpaip_equal (   ipptr1,
  ipptr2 
)
Value:
(((ipptr1)->type == (ipptr2)->type) \
? (((ipptr1)->type == LWPA_IPV4) ? ((ipptr1)->addr.v4 == (ipptr2)->addr.v4) \
: (0 == memcmp((ipptr1)->addr.v6, (ipptr2)->addr.v6, IPV6_BYTES))) \
: false)

Determine whether two instances of LwpaIpAddr contain identical addresses.

The type (IPv4 or IPv6) must be the same, as well as the value of the relevant address.

Parameters
ipptr1Pointer to the first LwpaIpAddr to compare.
ipptr2Pointer to the second LwpaIpAddr to compare.
Returns
true (IPs are identical) or false (IPs are not identical).

◆ lwpaip_is_invalid

#define lwpaip_is_invalid (   lwpa_ip_ptr)    ((lwpa_ip_ptr)->type == LWPA_IP_INVALID)

Determine whether a LwpaIpAddr contains an invalid address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
Returns
true (is invalid) or false (is not invalid).

◆ lwpaip_is_multicast

#define lwpaip_is_multicast (   lwpa_ip_ptr)
Value:
(((lwpa_ip_ptr)->type == LWPA_IPV4) \
? (((lwpa_ip_ptr)->addr.v4 > 0xe0000000) && ((lwpa_ip_ptr)->addr.v4 < 0xefffffff)) \
: ((lwpa_ip_ptr)->addr.v6[0] == 0xff))

Determine whether a LwpaIpAddr contains a multicast address.

Works for either an IPv4 or an IPv6 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
Returns
true (contains a multicast address) or false (does not contain a multicast address).

◆ lwpaip_is_v4

#define lwpaip_is_v4 (   lwpa_ip_ptr)    ((lwpa_ip_ptr)->type == LWPA_IPV4)

Determine whether a LwpaIpAddr contains an IPv4 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
Returns
true (contains an IPv4 address) or false (does not contain an IPv4 address).

◆ lwpaip_is_v6

#define lwpaip_is_v6 (   lwpa_ip_ptr)    ((lwpa_ip_ptr)->type == LWPA_IPV6)

Determine whether a LwpaIpAddr contains an IPv6 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
Returns
true (contains an IPv6 address) or false (does not contain an IPv6 address).

◆ lwpaip_make_any_v4

#define lwpaip_make_any_v4 (   lwpa_ip_ptr)    lwpaip_set_v4_address(lwpa_ip_ptr, LWPA_INADDR_ANY)

Set a LwpaIpAddr to the IPv4 wildcard address.

Use this macro; do not use LWPA_INADDR_ANY directly.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.

◆ lwpaip_make_any_v6

#define lwpaip_make_any_v6 (   lwpa_ip_ptr)
Value:
do \
{ \
(lwpa_ip_ptr)->type = LWPA_IPV6; \
memset(lwpa_ip_v6_address(lwpa_ip_ptr), 0, IPV6_BYTES); \
} while (0)
@ LWPA_IPV6
This lwpa_ip contains an IPv6 address.
Definition: lwpa_inet.h:46

Set a LwpaIpAddr to the IPv6 wildcard address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.

◆ lwpaip_set_invalid

#define lwpaip_set_invalid (   lwpa_ip_ptr)    ((lwpa_ip_ptr)->type = LWPA_IP_INVALID)

Set the type field in a LwpaIpAddr to indicate that it does not contain a valid address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.

◆ lwpaip_set_v4_address

#define lwpaip_set_v4_address (   lwpa_ip_ptr,
  val 
)
Value:
do \
{ \
(lwpa_ip_ptr)->type = LWPA_IPV4; \
(lwpa_ip_ptr)->addr.v4 = val; \
} while (0)

Set the IPv4 address in a LwpaIpAddr.

Also sets the type field to indicate that this LwpaIpAddr contains an IPv4 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
valIPv4 address to set (uint32_t).

◆ lwpaip_set_v6_address

#define lwpaip_set_v6_address (   lwpa_ip_ptr,
  val 
)
Value:
do \
{ \
(lwpa_ip_ptr)->type = LWPA_IPV6; \
memcpy((lwpa_ip_ptr)->addr.v6, val, IPV6_BYTES); \
} while (0)

Set the IPv6 address in a LwpaIpAddr.

Also sets the type field to indicate that this LwpaIpAddr contains an IPv6 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
valIPv6 address to set (uint8_t[]). Must be at least of length IPV6_BYTES. Gets copied into the struct.

◆ lwpaip_v4_address

#define lwpaip_v4_address (   lwpa_ip_ptr)    ((lwpa_ip_ptr)->addr.v4)

Get the IPv4 address from a LwpaIpAddr.

It is recommended to first use lwpaip_is_v4() to make sure this LwpaIpAddr contains a valid IPv4 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
Returns
The IPv4 address (uint32_t).

◆ lwpaip_v6_address

#define lwpaip_v6_address (   lwpa_ip_ptr)    ((lwpa_ip_ptr)->addr.v6)

Get the IPv6 address from a LwpaIpAddr.

It is recommended to first use lwpaip_is_v6() to make sure this LwpaIpAddr contains a valid IPv6 address.

Parameters
lwpa_ip_ptrPointer to a LwpaIpAddr.
Returns
The IPv6 address (uint8_t[]).

◆ lwpasock_ip_port_equal

#define lwpasock_ip_port_equal (   sockptr1,
  sockptr2 
)     (lwpaip_equal(&(sockptr1)->ip, &(sockptr2)->ip) && ((sockptr1)->port == (sockptr2)->port))

Determine whether two instances of LwpaSockaddr contain identical IP addresses and ports.

Parameters
sockptr1Pointer to first LwpaSockaddr to compare.
sockptr2Pointer to second LwpaSockaddr to compare.
Returns
true (the IP address and port are identical) or false (the IP address and port are not identical).

Typedef Documentation

◆ LwpaIpAddr

typedef struct LwpaIpAddr LwpaIpAddr

An IP address.

Can hold either an IPv4 or IPv6 address. IPv4 addresses are in host byte order.

◆ LwpaSockaddr

typedef struct LwpaSockaddr LwpaSockaddr

An IP address with associated interface and port.

Ports are in host byte order.

Enumeration Type Documentation

◆ lwpa_iptype_t

Used to identify the type of IP address contained in a LwpaIpAddr.

Enumerator
LWPA_IP_INVALID 

This lwpa_ip is not valid.

LWPA_IPV4 

This lwpa_ip contains an IPv4 address.

LWPA_IPV6 

This lwpa_ip contains an IPv6 address.