23 #ifndef ETCPAL_CPP_INET_H_
24 #define ETCPAL_CPP_INET_H_
31 #include "etcpal/inet.h"
33 #include "etcpal/cpp/hash.h"
74 constexpr uint32_t
v4_data()
const noexcept;
75 constexpr
const uint8_t*
v6_data()
const noexcept;
76 std::array<uint8_t, ETCPAL_IPV6_BYTES>
ToV6Array()
const;
77 constexpr
unsigned long scope_id()
const noexcept;
79 constexpr
bool IsValid()
const noexcept;
82 constexpr
bool IsV4()
const noexcept;
83 constexpr
bool IsV6()
const noexcept;
167 std::array<char, ETCPAL_IP_STRING_BYTES> str_buf;
171 return {str_buf.data()};
201 std::array<uint8_t, ETCPAL_IPV6_BYTES> arr;
333 return FromString(ip_str.c_str());
367 return Netmask(IpAddrType::kV4, mask_length);
375 return Netmask(IpAddrType::kV6, mask_length);
408 constexpr
IpAddr ip()
const noexcept;
409 constexpr uint16_t
port()
const noexcept;
410 constexpr uint32_t
v4_data()
const noexcept;
411 constexpr
const uint8_t*
v6_data()
const noexcept;
412 std::array<uint8_t, ETCPAL_IPV6_BYTES>
ToV6Array()
const;
413 constexpr
unsigned long scope_id()
const noexcept;
415 constexpr
bool IsValid()
const noexcept;
418 constexpr
bool IsV4()
const noexcept;
419 constexpr
bool IsV6()
const noexcept;
557 std::array<uint8_t, ETCPAL_IPV6_BYTES> arr;
690 explicit MacAddr(
const uint8_t* mac_data) noexcept;
695 constexpr const uint8_t*
data() const noexcept;
698 bool IsNull() const noexcept;
743 std::array<char, ETCPAL_MAC_STRING_BYTES> str_buf;
745 return {str_buf.data()};
761 std::array<uint8_t, ETCPAL_MAC_BYTES> arr;
787 return FromString(mac_str.c_str());
794 class NetintIndexType
822 std::string
id()
const noexcept;
826 constexpr
bool IsValid()
const noexcept;
937 return c_ip == ip.get();
940 inline bool operator!=(
const EtcPalIpAddr& c_ip,
const IpAddr& ip) noexcept
942 return !(c_ip == ip);
945 inline bool operator==(
const IpAddr& ip,
const EtcPalIpAddr& c_ip) noexcept
947 return ip.get() == c_ip;
950 inline bool operator!=(
const IpAddr& ip,
const EtcPalIpAddr& c_ip) noexcept
952 return !(ip == c_ip);
955 inline bool operator==(
const EtcPalSockAddr& c_ip,
const SockAddr& ip) noexcept
957 return c_ip == ip.get();
960 inline bool operator!=(
const EtcPalSockAddr& c_ip,
const SockAddr& ip) noexcept
962 return !(c_ip == ip);
965 inline bool operator==(
const SockAddr& ip,
const EtcPalSockAddr& c_ip) noexcept
967 return ip.get() == c_ip;
970 inline bool operator!=(
const SockAddr& ip,
const EtcPalSockAddr& c_ip) noexcept
972 return !(ip == c_ip);
975 inline bool operator==(
const EtcPalMacAddr& c_mac,
const MacAddr& mac) noexcept
977 return c_mac == mac.get();
980 inline bool operator!=(
const EtcPalMacAddr& c_mac,
const MacAddr& mac) noexcept
982 return !(c_mac == mac);
985 inline bool operator==(
const MacAddr& mac,
const EtcPalMacAddr& c_mac) noexcept
987 return mac.get() == c_mac;
990 inline bool operator!=(
const MacAddr& mac,
const EtcPalMacAddr& c_mac) noexcept
992 return !(mac == c_mac);
995 inline bool operator==(
const EtcPalNetintInfo& c_info,
const NetintInfo& info) noexcept
997 return c_info == info.get();
1000 inline bool operator!=(
const EtcPalNetintInfo& c_info,
const NetintInfo& info) noexcept
1002 return !(c_info == info);
1005 inline bool operator==(
const NetintInfo& info,
const EtcPalNetintInfo& c_info) noexcept
1007 return info.get() == c_info;
1010 inline bool operator!=(
const NetintInfo& info,
const EtcPalNetintInfo& c_info) noexcept
1012 return !(info == c_info);
1017 inline bool operator==(
const IpAddr& a,
const IpAddr& b) noexcept
1019 return a.get() == b.get();
1022 inline bool operator!=(
const IpAddr& a,
const IpAddr& b) noexcept
1027 inline bool operator<(
const IpAddr& a,
const IpAddr& b) noexcept
1029 return a.get() < b.get();
1032 inline bool operator>(
const IpAddr& a,
const IpAddr& b) noexcept
1037 inline bool operator<=(
const IpAddr& a,
const IpAddr& b) noexcept
1042 inline bool operator>=(
const IpAddr& a,
const IpAddr& b) noexcept
1047 inline bool operator==(
const SockAddr& a,
const SockAddr& b) noexcept
1049 return a.get() == b.get();
1052 inline bool operator!=(
const SockAddr& a,
const SockAddr& b) noexcept
1057 inline bool operator<(
const SockAddr& a,
const SockAddr& b) noexcept
1059 return a.get() < b.get();
1062 inline bool operator>(
const SockAddr& a,
const SockAddr& b) noexcept
1067 inline bool operator<=(
const SockAddr& a,
const SockAddr& b) noexcept
1072 inline bool operator>=(
const SockAddr& a,
const SockAddr& b) noexcept
1077 inline bool operator==(
const MacAddr& a,
const MacAddr& b) noexcept
1079 return a.get() == b.get();
1082 inline bool operator!=(
const MacAddr& a,
const MacAddr& b) noexcept
1087 inline bool operator<(
const MacAddr& a,
const MacAddr& b) noexcept
1089 return a.get() < b.get();
1092 inline bool operator>(
const MacAddr& a,
const MacAddr& b) noexcept
1097 inline bool operator<=(
const MacAddr& a,
const MacAddr& b) noexcept
1102 inline bool operator>=(
const MacAddr& a,
const MacAddr& b) noexcept
1107 inline bool operator==(
const NetintInfo& a,
const NetintInfo& b) noexcept
1109 return a.get() == b.get();
1112 inline bool operator!=(
const NetintInfo& a,
const NetintInfo& b) noexcept
1117 inline bool operator<(
const NetintInfo& a,
const NetintInfo& b) noexcept
1119 return a.get() < b.get();
1122 inline bool operator>(
const NetintInfo& a,
const NetintInfo& b) noexcept
1127 inline bool operator<=(
const NetintInfo& a,
const NetintInfo& b) noexcept
1132 inline bool operator>=(
const NetintInfo& a,
const NetintInfo& b) noexcept
1153 std::size_t operator()(const ::etcpal::IpAddr& addr)
const noexcept
1164 else if (addr.IsV4())
1178 std::size_t operator()(const ::etcpal::SockAddr& addr)
const noexcept
1191 std::size_t operator()(const ::etcpal::MacAddr& addr)
const noexcept
A wrapper class for the EtcPal IP address type.
Definition: inet.h:59
unsigned int MaskLength() const noexcept
The number of consecutive set bits in a netmask.
Definition: inet.h:279
constexpr bool IsV6() const noexcept
Whether an IpAddr contains a valid IPv6 address.
Definition: inet.h:245
static IpAddr NetmaskV4(unsigned int mask_length) noexcept
Construct an IPv4 netmask given a length in bits.
Definition: inet.h:365
constexpr unsigned long scope_id() const noexcept
Get the scope ID of an IPv6 address.
Definition: inet.h:215
std::string ToString() const
Convert the IP address to a string representation.
Definition: inet.h:165
bool IsLinkLocal() const noexcept
Whether an IpAddr contains a link-local address.
Definition: inet.h:251
static IpAddr WildcardV6() noexcept
Construct a wildcard IPv6 address.
Definition: inet.h:347
void SetAddress(uint32_t v4_data) noexcept
Set the IPv4 address data.
Definition: inet.h:289
bool IsMulticast() const noexcept
Whether an IpAddr contains a multicast address.
Definition: inet.h:263
constexpr bool IsV4() const noexcept
Whether an IpAddr contains a valid IPv4 address.
Definition: inet.h:239
constexpr const EtcPalIpAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:151
static IpAddr FromString(const char *ip_str) noexcept
Construct an IpAddr from a string representation.
Definition: inet.h:318
bool IsLoopback() const noexcept
Whether an IpAddr contains a loopback address.
Definition: inet.h:257
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:381
ETCPAL_CONSTEXPR_14 IpAddr() noexcept
Constructs an invalid IP address by default.
Definition: inet.h:108
constexpr IpAddrType type() const noexcept
Get the type of the IP address.
Definition: inet.h:227
static IpAddr NetmaskV6(unsigned int mask_length) noexcept
Construct an IPv6 netmask given a length in bits.
Definition: inet.h:373
constexpr bool IsValid() const noexcept
Whether an IpAddr contains a valid IPv4 or IPv6 address.
Definition: inet.h:221
static IpAddr Wildcard(IpAddrType type) noexcept
Construct a wildcard address of the type specified.
Definition: inet.h:355
static IpAddr WildcardV4() noexcept
Construct a wildcard IPv4 address.
Definition: inet.h:339
std::array< uint8_t, ETCPAL_IPV6_BYTES > ToV6Array() const
Get a 16-byte std::array representation of an IPv6 address.
Definition: inet.h:198
bool IsWildcard() const noexcept
Whether an IpAddr contains a wildcard address.
Definition: inet.h:271
constexpr etcpal_iptype_t raw_type() const noexcept
Get the underlying etcpal C type of the IP address.
Definition: inet.h:233
constexpr uint32_t v4_data() const noexcept
Get the raw 32-bit representation of an IPv4 address.
Definition: inet.h:180
IpAddr & operator=(const EtcPalIpAddr &c_ip) noexcept
Assign an instance of the C EtcPalIpAddr type to an instance of this class.
Definition: inet.h:119
constexpr const uint8_t * v6_data() const noexcept
Get the raw 16-byte array representation of an IPv6 address.
Definition: inet.h:189
A wrapper for the EtcPal MAC address type.
Definition: inet.h:682
std::string ToString() const
Convert the MAC address to a string representation.
Definition: inet.h:741
bool IsNull() const noexcept
Whether this MacAddr represents a null (all 0's) MAC address.
Definition: inet.h:767
static MacAddr FromString(const char *mac_str) noexcept
Construct a MacAddr from a string representation.
Definition: inet.h:775
constexpr const uint8_t * data() const noexcept
Get the raw 6-byte array representation of a MAC address.
Definition: inet.h:750
std::array< uint8_t, ETCPAL_MAC_BYTES > ToArray() const noexcept
Get a 6-byte std::array representation of a MAC address.
Definition: inet.h:758
constexpr const EtcPalMacAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:727
MacAddr & operator=(const EtcPalMacAddr &c_mac) noexcept
Assign an instance of the C EtcPalMacAddr type to an instance of this class.
Definition: inet.h:713
MacAddr()=default
Constructs a null MAC address by default.
A wrapper class for the EtcPal netint info type.
Definition: inet.h:810
constexpr IpAddr mask() const noexcept
Get the subnet mask for this interface.
Definition: inet.h:869
constexpr bool is_default() const noexcept
Determine whether this is the default network interface.
Definition: inet.h:906
constexpr const EtcPalNetintInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:846
constexpr bool IsValid() const noexcept
Determine whether this netint info is valid.
Definition: inet.h:912
constexpr IpAddr addr() const noexcept
Get the interface ip address.
Definition: inet.h:863
constexpr NetintIndex index() const noexcept
Get the OS-specific network interface number.
Definition: inet.h:857
std::string friendly_name() const noexcept
Get a user-friendly name for the interface.
Definition: inet.h:896
std::string id() const noexcept
Get the system name for the interface.
Definition: inet.h:886
NetintInfo & operator=(const EtcPalNetintInfo &c_info) noexcept
Assign an instance of the C EtcPalNetintInfo type to an instance of this class.
Definition: inet.h:839
constexpr MacAddr mac() const noexcept
Get the adapter MAC address.
Definition: inet.h:875
A strongly-typed ID with arbitrary internal representation.
Definition: opaque_id.h:118
constexpr bool IsValid() const
Explicitly determine whether an ID is valid.
Definition: opaque_id.h:166
A wrapper for the EtcPal socket address type.
Definition: inet.h:392
constexpr bool IsV6() const noexcept
Whether a SockAddr contains a valid IPv6 address.
Definition: inet.h:601
constexpr unsigned long scope_id() const noexcept
Get the scope ID of the SockAddr's IPv6 address.
Definition: inet.h:571
std::string ToString() const
Convert the IP address and port to a string representation.
Definition: inet.h:510
bool IsLinkLocal() const noexcept
Whether a SockAddr contains a link-local address.
Definition: inet.h:607
void SetAddress(uint32_t v4_data) noexcept
Set the IPv4 address data.
Definition: inet.h:637
bool IsMulticast() const noexcept
Whether a SockAddr contains a multicast address.
Definition: inet.h:619
constexpr IpAddr ip() const noexcept
Get the IP address from the SockAddr.
Definition: inet.h:520
constexpr bool IsV4() const noexcept
Whether a SockAddr contains a valid IPv4 address.
Definition: inet.h:595
bool IsLoopback() const noexcept
Whether a SockAddr contains a loopback address.
Definition: inet.h:613
constexpr const EtcPalSockAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:494
constexpr IpAddrType type() const noexcept
Get the type of the SockAddr's IP address.
Definition: inet.h:583
constexpr bool IsValid() const noexcept
Whether a SockAddr contains a valid IPv4 or IPv6 address.
Definition: inet.h:577
void SetPort(uint16_t port) noexcept
Set the port.
Definition: inet.h:672
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:554
SockAddr & operator=(const EtcPalSockAddr &c_sa) noexcept
Assign an instance of the C EtcPalSockAddr type to an instance of this class.
Definition: inet.h:447
constexpr uint16_t port() const noexcept
Get the port number from the SockAddr.
Definition: inet.h:526
bool IsWildcard() const noexcept
Whether a SockAddr contains a wildcard address.
Definition: inet.h:627
constexpr etcpal_iptype_t raw_type() const noexcept
Get the underlying etcpal C type of the SockAddr's IP address.
Definition: inet.h:589
ETCPAL_CONSTEXPR_14 SockAddr() noexcept
Constructs an invalid SockAddr by default.
Definition: inet.h:436
constexpr uint32_t v4_data() const noexcept
Get the raw 32-bit representation of the SockAddr's IPv4 address.
Definition: inet.h:536
constexpr const uint8_t * v6_data() const noexcept
Get the raw 16-byte array representation of the SockAddr's IPv6 address.
Definition: inet.h:545
Common definitions used by EtcPal C++ wrappers.
void HashCombine(size_t &seed, const T &val)
A function that combines the hash of a new value into an existing seed, based on boost::hash_combine.
Definition: hash.h:52
etcpal::OpaqueId< detail::NetintIndexType, unsigned int, 0 > NetintIndex
A handle that represents a network interface index.
Definition: inet.h:805
IpAddrType
Indicates an IP address family, or an invalid IP address.
Definition: inet.h:48
#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:351
#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:176
#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:90
bool etcpal_ip_is_multicast(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a multicast address.
Definition: inet.c:117
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:296
bool etcpal_ip_is_link_local(const EtcPalIpAddr *ip)
Determine whether a EtcPalIpAddr contains a link-local address.
Definition: inet.c:62
#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:564
#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:148
#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:542
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
Provides the OpaqueId template type and function definitions.
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
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 id[ETCPAL_NETINTINFO_ID_LEN]
The system name for the interface.
Definition: inet.h:373
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