23 #ifndef ETCPAL_CPP_INET_H_
24 #define ETCPAL_CPP_INET_H_
31 #include "etcpal/inet.h"
72 constexpr uint32_t
v4_data()
const noexcept;
73 constexpr
const uint8_t*
v6_data()
const noexcept;
74 std::array<uint8_t, ETCPAL_IPV6_BYTES>
ToV6Array()
const;
75 constexpr
unsigned long scope_id()
const noexcept;
77 constexpr
bool IsValid()
const noexcept;
79 constexpr
bool IsV4()
const noexcept;
80 constexpr
bool IsV6()
const noexcept;
164 std::array<char, ETCPAL_IP_STRING_BYTES> str_buf;
168 return {str_buf.data()};
198 std::array<uint8_t, ETCPAL_IPV6_BYTES> arr;
324 return FromString(ip_str.c_str());
358 return Netmask(IpAddrType::kV4, mask_length);
366 return Netmask(IpAddrType::kV6, mask_length);
399 constexpr
IpAddr ip()
const noexcept;
400 constexpr uint16_t
port()
const noexcept;
402 constexpr uint32_t
v4_data()
const noexcept;
403 constexpr
const uint8_t*
v6_data()
const noexcept;
404 std::array<uint8_t, ETCPAL_IPV6_BYTES>
ToV6Array()
const;
405 constexpr
unsigned long scope_id()
const noexcept;
407 constexpr
bool IsValid()
const noexcept;
409 constexpr
bool IsV4()
const noexcept;
410 constexpr
bool IsV6()
const noexcept;
548 std::array<uint8_t, ETCPAL_IPV6_BYTES> arr;
675 explicit MacAddr(
const uint8_t* mac_data) noexcept;
680 constexpr const uint8_t*
data() const noexcept;
683 bool IsNull() const noexcept;
728 std::array<char, ETCPAL_MAC_STRING_BYTES> str_buf;
730 return {str_buf.data()};
746 std::array<uint8_t, ETCPAL_MAC_BYTES> arr;
772 return FromString(mac_str.c_str());
789 return c_ip == ip.get();
792 inline bool operator!=(
const EtcPalIpAddr& c_ip,
const IpAddr& ip) noexcept
794 return !(c_ip == ip);
797 inline bool operator==(
const IpAddr& ip,
const EtcPalIpAddr& c_ip) noexcept
799 return ip.get() == c_ip;
802 inline bool operator!=(
const IpAddr& ip,
const EtcPalIpAddr& c_ip) noexcept
804 return !(ip == c_ip);
807 inline bool operator==(
const EtcPalSockAddr& c_ip,
const SockAddr& ip) noexcept
809 return c_ip == ip.get();
812 inline bool operator!=(
const EtcPalSockAddr& c_ip,
const SockAddr& ip) noexcept
814 return !(c_ip == ip);
817 inline bool operator==(
const SockAddr& ip,
const EtcPalSockAddr& c_ip) noexcept
819 return ip.get() == c_ip;
822 inline bool operator!=(
const SockAddr& ip,
const EtcPalSockAddr& c_ip) noexcept
824 return !(ip == c_ip);
827 inline bool operator==(
const EtcPalMacAddr& c_mac,
const MacAddr& mac) noexcept
829 return c_mac == mac.get();
832 inline bool operator!=(
const EtcPalMacAddr& c_mac,
const MacAddr& mac) noexcept
834 return !(c_mac == mac);
837 inline bool operator==(
const MacAddr& mac,
const EtcPalMacAddr& c_mac) noexcept
839 return mac.get() == c_mac;
842 inline bool operator!=(
const MacAddr& mac,
const EtcPalMacAddr& c_mac) noexcept
844 return !(mac == c_mac);
849 inline bool operator==(
const IpAddr& a,
const IpAddr& b) noexcept
851 return a.get() == b.get();
854 inline bool operator!=(
const IpAddr& a,
const IpAddr& b) noexcept
859 inline bool operator<(
const IpAddr& a,
const IpAddr& b) noexcept
861 return a.get() < b.get();
864 inline bool operator>(
const IpAddr& a,
const IpAddr& b) noexcept
869 inline bool operator<=(
const IpAddr& a,
const IpAddr& b) noexcept
874 inline bool operator>=(
const IpAddr& a,
const IpAddr& b) noexcept
879 inline bool operator==(
const SockAddr& a,
const SockAddr& b) noexcept
881 return a.get() == b.get();
884 inline bool operator!=(
const SockAddr& a,
const SockAddr& b) noexcept
889 inline bool operator<(
const SockAddr& a,
const SockAddr& b) noexcept
891 return a.get() < b.get();
894 inline bool operator>(
const SockAddr& a,
const SockAddr& b) noexcept
899 inline bool operator<=(
const SockAddr& a,
const SockAddr& b) noexcept
904 inline bool operator>=(
const SockAddr& a,
const SockAddr& b) noexcept
909 inline bool operator==(
const MacAddr& a,
const MacAddr& b) noexcept
911 return a.get() == b.get();
914 inline bool operator!=(
const MacAddr& a,
const MacAddr& b) noexcept
919 inline bool operator<(
const MacAddr& a,
const MacAddr& b) noexcept
921 return a.get() < b.get();
924 inline bool operator>(
const MacAddr& a,
const MacAddr& b) noexcept
929 inline bool operator<=(
const MacAddr& a,
const MacAddr& b) noexcept
934 inline bool operator>=(
const MacAddr& a,
const MacAddr& b) noexcept
A wrapper class for the EtcPal IP address type.
Definition: inet.h:57
unsigned int MaskLength() const noexcept
The number of consecutive set bits in a netmask.
Definition: inet.h:270
constexpr bool IsV6() const noexcept
Whether an IpAddr contains a valid IPv6 address.
Definition: inet.h:236
static IpAddr NetmaskV4(unsigned int mask_length) noexcept
Construct an IPv4 netmask given a length in bits.
Definition: inet.h:356
constexpr unsigned long scope_id() const noexcept
Get the scope ID of an IPv6 address.
Definition: inet.h:212
std::string ToString() const
Convert the IP address to a string representation.
Definition: inet.h:162
bool IsLinkLocal() const noexcept
Whether an IpAddr contains a link-local address.
Definition: inet.h:242
static IpAddr WildcardV6() noexcept
Construct a wildcard IPv6 address.
Definition: inet.h:338
void SetAddress(uint32_t v4_data) noexcept
Set the IPv4 address data.
Definition: inet.h:280
bool IsMulticast() const noexcept
Whether an IpAddr contains a multicast address.
Definition: inet.h:254
constexpr bool IsV4() const noexcept
Whether an IpAddr contains a valid IPv4 address.
Definition: inet.h:230
constexpr const EtcPalIpAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:148
static IpAddr FromString(const char *ip_str) noexcept
Construct an IpAddr from a string representation.
Definition: inet.h:309
bool IsLoopback() const noexcept
Whether an IpAddr contains a loopback address.
Definition: inet.h:248
static IpAddr Netmask(IpAddrType type, unsigned int mask_length) noexcept
Construct a netmask of the type specifed given a length in bits.
Definition: inet.h:372
ETCPAL_CONSTEXPR_14 IpAddr() noexcept
Constructs an invalid IP address by default.
Definition: inet.h:105
constexpr IpAddrType type() const noexcept
Get the type of the IP address.
Definition: inet.h:224
static IpAddr NetmaskV6(unsigned int mask_length) noexcept
Construct an IPv6 netmask given a length in bits.
Definition: inet.h:364
constexpr bool IsValid() const noexcept
Whether an IpAddr contains a valid IPv4 or IPv6 address.
Definition: inet.h:218
static IpAddr Wildcard(IpAddrType type) noexcept
Construct a wildcard address of the type specified.
Definition: inet.h:346
static IpAddr WildcardV4() noexcept
Construct a wildcard IPv4 address.
Definition: inet.h:330
std::array< uint8_t, ETCPAL_IPV6_BYTES > ToV6Array() const
Get a 16-byte std::array representation of an IPv6 address.
Definition: inet.h:195
bool IsWildcard() const noexcept
Whether an IpAddr contains a wildcard address.
Definition: inet.h:262
constexpr uint32_t v4_data() const noexcept
Get the raw 32-bit representation of an IPv4 address.
Definition: inet.h:177
IpAddr & operator=(const EtcPalIpAddr &c_ip) noexcept
Assign an instance of the C EtcPalIpAddr type to an instance of this class.
Definition: inet.h:116
constexpr const uint8_t * v6_data() const noexcept
Get the raw 16-byte array representation of an IPv6 address.
Definition: inet.h:186
A wrapper for the EtcPal MAC address type.
Definition: inet.h:667
std::string ToString() const
Convert the MAC address to a string representation.
Definition: inet.h:726
bool IsNull() const noexcept
Whether this MacAddr represents a null (all 0's) MAC address.
Definition: inet.h:752
static MacAddr FromString(const char *mac_str) noexcept
Construct a MacAddr from a string representation.
Definition: inet.h:760
constexpr const uint8_t * data() const noexcept
Get the raw 6-byte array representation of a MAC address.
Definition: inet.h:735
std::array< uint8_t, ETCPAL_MAC_BYTES > ToArray() const noexcept
Get a 6-byte std::array representation of a MAC address.
Definition: inet.h:743
constexpr const EtcPalMacAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:712
MacAddr & operator=(const EtcPalMacAddr &c_mac) noexcept
Assign an instance of the C EtcPalMacAddr type to an instance of this class.
Definition: inet.h:698
MacAddr()=default
Constructs a null MAC address by default.
A wrapper for the EtcPal socket address type.
Definition: inet.h:383
constexpr bool IsV6() const noexcept
Whether a SockAddr contains a valid IPv6 address.
Definition: inet.h:586
constexpr unsigned long scope_id() const noexcept
Get the scope ID of the SockAddr's IPv6 address.
Definition: inet.h:562
std::string ToString() const
Convert the IP address and port to a string representation.
Definition: inet.h:501
bool IsLinkLocal() const noexcept
Whether a SockAddr contains a link-local address.
Definition: inet.h:592
void SetAddress(uint32_t v4_data) noexcept
Set the IPv4 address data.
Definition: inet.h:622
bool IsMulticast() const noexcept
Whether a SockAddr contains a multicast address.
Definition: inet.h:604
constexpr IpAddr ip() const noexcept
Get the IP address from the SockAddr.
Definition: inet.h:511
constexpr bool IsV4() const noexcept
Whether a SockAddr contains a valid IPv4 address.
Definition: inet.h:580
bool IsLoopback() const noexcept
Whether a SockAddr contains a loopback address.
Definition: inet.h:598
constexpr const EtcPalSockAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:485
constexpr IpAddrType type() const noexcept
Get the type of the SockAddr's IP address.
Definition: inet.h:574
constexpr bool IsValid() const noexcept
Whether a SockAddr contains a valid IPv4 or IPv6 address.
Definition: inet.h:568
void SetPort(uint16_t port) noexcept
Set the port.
Definition: inet.h:657
std::array< uint8_t, ETCPAL_IPV6_BYTES > ToV6Array() const
Get a 16-byte std::array representation of the SockAddr's IPv6 address.
Definition: inet.h:545
SockAddr & operator=(const EtcPalSockAddr &c_sa) noexcept
Assign an instance of the C EtcPalSockAddr type to an instance of this class.
Definition: inet.h:438
constexpr uint16_t port() const noexcept
Get the port number from the SockAddr.
Definition: inet.h:517
bool IsWildcard() const noexcept
Whether a SockAddr contains a wildcard address.
Definition: inet.h:612
ETCPAL_CONSTEXPR_14 SockAddr() noexcept
Constructs an invalid SockAddr by default.
Definition: inet.h:427
constexpr uint32_t v4_data() const noexcept
Get the raw 32-bit representation of the SockAddr's IPv4 address.
Definition: inet.h:527
constexpr const uint8_t * v6_data() const noexcept
Get the raw 16-byte array representation of the SockAddr's IPv6 address.
Definition: inet.h:536
Common definitions used by EtcPal C++ wrappers.
IpAddrType
Indicates an IP address family, or an invalid IP address.
Definition: inet.h:46
#define ETCPAL_CONSTEXPR_14
Stand-in for "constexpr" on entities that can only be defined "constexpr" in C++14 or later.
Definition: common.h:53
#define ETCPAL_CONSTEXPR_14_OR_INLINE
Defined to "constexpr" in C++14 or later, "inline" earlier.
Definition: common.h:54
@ kEtcPalErrOk
The call was successful, no error occurred.
Definition: error.h:51
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:311
#define ETCPAL_MAC_BYTES
The number of bytes in a MAC address.
Definition: inet.h:317
#define ETCPAL_IP_V4_ADDRESS(etcpal_ip_ptr)
Get the IPv4 address from a EtcPalIpAddr.
Definition: inet.h:230
#define ETCPAL_IP_IS_V4(etcpal_ip_ptr)
Determine whether a EtcPalIpAddr contains an IPv4 address.
Definition: inet.h:205
#define ETCPAL_IP_IS_INVALID(etcpal_ip_ptr)
Determine whether a EtcPalIpAddr contains an invalid address.
Definition: inet.h:219
void etcpal_ip_set_wildcard(etcpal_iptype_t type, EtcPalIpAddr *ip)
Initialize a EtcPalIpAddr with a wildcard address.
Definition: inet.c:174
#define ETCPAL_IP_SET_V4_ADDRESS(etcpal_ip_ptr, val)
Set the IPv4 address in a EtcPalIpAddr.
Definition: inet.h:262
bool etcpal_ip_is_loopback(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a loopback address.
Definition: inet.c:88
bool etcpal_ip_is_multicast(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a multicast address.
Definition: inet.c:115
etcpal_iptype_t
Used to identify the type of IP address contained in a EtcPalIpAddr.
Definition: inet.h:53
#define ETCPAL_MAC_IS_NULL(macptr)
Determine if a MAC address is null.
Definition: inet.h:346
#define ETCPAL_IP_IS_V6(etcpal_ip_ptr)
Determine whether a EtcPalIpAddr contains an IPv6 address.
Definition: inet.h:212
#define ETCPAL_IP_V6_SCOPE_ID(etcpal_ip_ptr)
Get the IPv6 scope ID from an EtcPalIpAddr.
Definition: inet.h:252
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:256
bool etcpal_ip_is_link_local(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a link-local address.
Definition: inet.c:60
#define ETCPAL_IP_V6_ADDRESS(etcpal_ip_ptr)
Get the IPv6 address from a EtcPalIpAddr.
Definition: inet.h:241
#define ETCPAL_IP_SET_INVALID(etcpal_ip_ptr)
Set the type field in a EtcPalIpAddr to indicate that it does not contain a valid address.
Definition: inet.h:303
etcpal_error_t etcpal_string_to_mac(const char *src, EtcPalMacAddr *dest)
Create a MAC address from a string representation.
Definition: inet.c:524
#define ETCPAL_IPV6_BYTES
The number of bytes in an IPv6 address.
Definition: inet.h:63
#define ETCPAL_IP_INVALID_INIT
An initializer for an invalid EtcPalIpAddr.
Definition: inet.h:121
#define ETCPAL_IP_SET_V6_ADDRESS(etcpal_ip_ptr, addr_val)
Set the IPv6 address in a EtcPalIpAddr.
Definition: inet.h:278
#define ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(etcpal_ip_ptr, addr_val, scope_id_val)
Set an IPv6 address with an explicit scope ID in a EtcPalIpAddr.
Definition: inet.h:291
bool etcpal_ip_is_wildcard(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a wildcard address.
Definition: inet.c:146
#define ETCPAL_IP_INVALID_INIT_VALUES
A set of initializer values for an invalid EtcPalIpAddr.
Definition: inet.h:110
etcpal_error_t etcpal_mac_to_string(const EtcPalMacAddr *src, char *dest)
Create a string representation of a MAC address.
Definition: inet.c:502
etcpal_error_t etcpal_ip_to_string(const EtcPalIpAddr *src, char *dest)
Convert IPv4 and IPv6 addresses from binary to text form.
@ 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
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
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