22 #ifndef ETCPAL_INET_H_
23 #define ETCPAL_INET_H_
28 #include "etcpal/error.h"
29 #include "etcpal/os_inet.h"
63 #define ETCPAL_IPV6_BYTES 16
110 #define ETCPAL_IP_INVALID_INIT_VALUES \
111 kEtcPalIpTypeInvalid, { 0 }
121 #define ETCPAL_IP_INVALID_INIT \
123 ETCPAL_IP_INVALID_INIT_VALUES \
138 #define ETCPAL_IPV4_INIT_VALUES(v4_val) \
139 kEtcPalIpTypeV4, { .v4 = v4_val }
151 #define ETCPAL_IPV4_INIT(addr_val) \
153 ETCPAL_IPV4_INIT_VALUES(addr_val) \
174 #define ETCPAL_IPV6_INIT_VALUES(...) \
177 .v6 = {.addr_buf = {__VA_ARGS__}, .scope_id = 0 } \
195 #define ETCPAL_IPV6_INIT(...) \
197 ETCPAL_IPV6_INIT_VALUES(__VA_ARGS__) \
205 #define ETCPAL_IP_IS_V4(etcpal_ip_ptr) ((etcpal_ip_ptr)->type == kEtcPalIpTypeV4)
212 #define ETCPAL_IP_IS_V6(etcpal_ip_ptr) ((etcpal_ip_ptr)->type == kEtcPalIpTypeV6)
219 #define ETCPAL_IP_IS_INVALID(etcpal_ip_ptr) ((etcpal_ip_ptr)->type == kEtcPalIpTypeInvalid)
230 #define ETCPAL_IP_V4_ADDRESS(etcpal_ip_ptr) ((etcpal_ip_ptr)->addr.v4)
241 #define ETCPAL_IP_V6_ADDRESS(etcpal_ip_ptr) ((etcpal_ip_ptr)->addr.v6.addr_buf)
252 #define ETCPAL_IP_V6_SCOPE_ID(etcpal_ip_ptr) ((etcpal_ip_ptr)->addr.v6.scope_id)
262 #define ETCPAL_IP_SET_V4_ADDRESS(etcpal_ip_ptr, val) \
265 (etcpal_ip_ptr)->type = kEtcPalIpTypeV4; \
266 (etcpal_ip_ptr)->addr.v4 = val; \
278 #define ETCPAL_IP_SET_V6_ADDRESS(etcpal_ip_ptr, addr_val) \
279 ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(etcpal_ip_ptr, addr_val, 0u)
291 #define ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(etcpal_ip_ptr, addr_val, scope_id_val) \
294 (etcpal_ip_ptr)->type = kEtcPalIpTypeV6; \
295 memcpy((etcpal_ip_ptr)->addr.v6.addr_buf, (addr_val), ETCPAL_IPV6_BYTES); \
296 (etcpal_ip_ptr)->addr.v6.scope_id = (scope_id_val); \
303 #define ETCPAL_IP_SET_INVALID(etcpal_ip_ptr) ((etcpal_ip_ptr)->type = kEtcPalIpTypeInvalid)
317 #define ETCPAL_MAC_BYTES 6
333 #define ETCPAL_MAC_CMP(mac1ptr, mac2ptr) memcmp((mac1ptr)->data, (mac2ptr)->data, ETCPAL_MAC_BYTES)
346 #define ETCPAL_MAC_IS_NULL(macptr) (memcmp((macptr)->data, kEtcPalNullMacAddr.data, ETCPAL_MAC_BYTES) == 0)
349 #define ETCPAL_NETINTINFO_ID_LEN 64
351 #define ETCPAL_NETINTINFO_FRIENDLY_NAME_LEN 64
394 #define ETCPAL_IP_STRING_BYTES 46
396 #define ETCPAL_MAC_STRING_BYTES 18
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
struct EtcPalNetintInfo EtcPalNetintInfo
A description of a single address assigned to a network interface.
bool sockaddr_os_to_etcpal(const etcpal_os_sockaddr_t *os_sa, EtcPalSockAddr *sa)
Convert a platform-specific socket address to an EtcPalSockAddr.
EtcPalIpAddr etcpal_ip_mask_from_length(etcpal_iptype_t type, unsigned int mask_length)
Create a netmask given a length in bits.
Definition: inet.c:351
int etcpal_netint_info_cmp(const EtcPalNetintInfo *i1, const EtcPalNetintInfo *i2)
Compare two EtcPalNetintInfos.
Definition: inet.c:254
bool etcpal_ip_network_portions_equal(const EtcPalIpAddr *ip1, const EtcPalIpAddr *ip2, const EtcPalIpAddr *netmask)
Compare the network portions of two IP addresses using a netmask.
Definition: inet.c:419
#define ETCPAL_MAC_BYTES
The number of bytes in a MAC address.
Definition: inet.h:317
#define ETCPAL_MAC_CMP(mac1ptr, mac2ptr)
Compare two EtcPalMacAddrs numerically.
Definition: inet.h:333
void etcpal_ip_set_wildcard(etcpal_iptype_t type, EtcPalIpAddr *ip)
Initialize a EtcPalIpAddr with a wildcard address.
Definition: inet.c:176
bool etcpal_ip_is_loopback(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a loopback address.
Definition: inet.c:90
struct EtcPalMcastNetintId EtcPalMcastNetintId
A set of identifying information for a network interface, for multicast purposes.
bool etcpal_ip_is_multicast(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a multicast address.
Definition: inet.c:117
#define ETCPAL_NETINTINFO_ID_LEN
The maximum length of a network interface id.
Definition: inet.h:349
#define ETCPAL_NETINTINFO_FRIENDLY_NAME_LEN
The maximum length of a user-friendly network interface name.
Definition: inet.h:351
struct EtcPalIpAddr EtcPalIpAddr
An IP address.
etcpal_iptype_t
Used to identify the type of IP address contained in a EtcPalIpAddr.
Definition: inet.h:53
etcpal_error_t etcpal_string_to_ip(etcpal_iptype_t type, const char *src, EtcPalIpAddr *dest)
Convert IPv4 and IPv6 addresses from text to binary form.
unsigned int etcpal_ip_mask_length(const EtcPalIpAddr *netmask)
Get the length in bits of a netmask.
Definition: inet.c:296
bool etcpal_ip_is_link_local(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a link-local address.
Definition: inet.c:62
size_t sockaddr_etcpal_to_os(const EtcPalSockAddr *sa, etcpal_os_sockaddr_t *os_sa)
Convert an EtcPalSockAddr to a platform-specific socket address.
struct EtcPalSockAddr EtcPalSockAddr
An IP address with associated interface and port.
etcpal_error_t etcpal_string_to_mac(const char *src, EtcPalMacAddr *dest)
Create a MAC address from a string representation.
Definition: inet.c:564
bool ip_os_to_etcpal(const etcpal_os_ipaddr_t *os_ip, EtcPalIpAddr *ip)
Convert a platform-specific IP address to an EtcPalIpAddr.
#define ETCPAL_IPV6_BYTES
The number of bytes in an IPv6 address.
Definition: inet.h:63
const EtcPalMacAddr kEtcPalNullMacAddr
A null (all 0's) MAC address, used by ETCPAL_MAC_IS_NULL() for comparison.
Definition: inet.c:26
bool etcpal_ip_is_wildcard(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a wildcard address.
Definition: inet.c:148
etcpal_error_t etcpal_mac_to_string(const EtcPalMacAddr *src, char *dest)
Create a string representation of a MAC address.
Definition: inet.c:542
struct EtcPalMacAddr EtcPalMacAddr
A MAC address.
int etcpal_ip_cmp(const EtcPalIpAddr *ip1, const EtcPalIpAddr *ip2)
Compare two EtcPalIpAddrs.
Definition: inet.c:210
bool etcpal_ip_and_port_equal(const EtcPalSockAddr *sock1, const EtcPalSockAddr *sock2)
Determine whether two instances of EtcPalSockAddr contain identical IP addresses and ports.
Definition: inet.c:238
etcpal_error_t etcpal_ip_to_string(const EtcPalIpAddr *src, char *dest)
Convert IPv4 and IPv6 addresses from binary to text form.
size_t ip_etcpal_to_os(const EtcPalIpAddr *ip, etcpal_os_ipaddr_t *os_ip)
Convert an EtcPalIpAddr to a platform-specific IP address.
@ kEtcPalIpTypeInvalid
This EtcPalIpAddr is not valid.
Definition: inet.h:55
@ kEtcPalIpTypeV4
This EtcPalIpAddr contains an IPv4 address.
Definition: inet.h:57
@ kEtcPalIpTypeV6
This EtcPalIpAddr contains an IPv6 address.
Definition: inet.h:59
An IP address.
Definition: inet.h:75
union EtcPalIpAddr::@0 addr
A union containing either the IPv4 or IPv6 address.
uint8_t addr_buf[ETCPAL_IPV6_BYTES]
The IPv6 address.
Definition: inet.h:86
uint32_t v4
The IPv4 address value in host byte order.
Definition: inet.h:82
unsigned long scope_id
The IPv6 scope ID.
Definition: inet.h:87
etcpal_iptype_t type
The IP address type (IPv4 or IPv6)
Definition: inet.h:77
A MAC address.
Definition: inet.h:321
uint8_t data[ETCPAL_MAC_BYTES]
The 6-byte address data.
Definition: inet.h:322
A set of identifying information for a network interface, for multicast purposes.
Definition: inet.h:388
unsigned int index
The OS index of the network interface.
Definition: inet.h:390
etcpal_iptype_t ip_type
The IP protocol used on the network interface.
Definition: inet.h:389
A description of a single address assigned to a network interface.
Definition: inet.h:355
bool is_default
Whether this is the default network interface.
Definition: inet.h:381
EtcPalIpAddr mask
The subnet mask for this interface.
Definition: inet.h:365
EtcPalMacAddr mac
The adapter MAC address.
Definition: inet.h:367
char friendly_name[ETCPAL_NETINTINFO_FRIENDLY_NAME_LEN]
A user-friendly name for the interface.
Definition: inet.h:378
unsigned int index
The OS-specific network interface number.
Definition: inet.h:361
EtcPalIpAddr addr
The interface ip address.
Definition: inet.h:363
An IP address with associated interface and port.
Definition: inet.h:311
EtcPalIpAddr ip
IP address.
Definition: inet.h:313
uint16_t port
TCP or UDP port.
Definition: inet.h:312