EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
inet.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_INET_H_
24 #define ETCPAL_CPP_INET_H_
25 
26 #include <algorithm>
27 #include <array>
28 #include <cstring>
29 #include <iterator>
30 #include <string>
31 #include "etcpal/inet.h"
32 #include "etcpal/cpp/common.h"
33 
34 namespace etcpal
35 {
42 
45 enum class IpAddrType
46 {
47  kInvalid = kEtcPalIpTypeInvalid,
48  kV4 = kEtcPalIpTypeV4,
49  kV6 = kEtcPalIpTypeV6
50 };
51 
56 class IpAddr
57 {
58 public:
59  ETCPAL_CONSTEXPR_14 IpAddr() noexcept;
60  // Note: this constructor is not explicit by design, to allow implicit conversions e.g.
61  // etcpal::IpAddr ip = etcpal_c_function_that_returns_ip();
62  constexpr IpAddr(const EtcPalIpAddr& c_ip) noexcept;
63  IpAddr& operator=(const EtcPalIpAddr& c_ip) noexcept;
64 
65  ETCPAL_CONSTEXPR_14 IpAddr(uint32_t v4_data) noexcept;
66  explicit IpAddr(const uint8_t* v6_data) noexcept;
67  IpAddr(const uint8_t* v6_data, unsigned long scope_id) noexcept;
68 
69  constexpr const EtcPalIpAddr& get() const noexcept;
71  std::string ToString() const;
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;
76 
77  constexpr bool IsValid() const noexcept;
78  constexpr IpAddrType type() const noexcept;
79  constexpr bool IsV4() const noexcept;
80  constexpr bool IsV6() const noexcept;
81  bool IsLinkLocal() const noexcept;
82  bool IsLoopback() const noexcept;
83  bool IsMulticast() const noexcept;
84  bool IsWildcard() const noexcept;
85  unsigned int MaskLength() const noexcept;
86 
87  void SetAddress(uint32_t v4_data) noexcept;
88  void SetAddress(const uint8_t* v6_data) noexcept;
89  void SetAddress(const uint8_t* v6_data, unsigned long scope_id) noexcept;
90 
91  static IpAddr FromString(const char* ip_str) noexcept;
92  static IpAddr FromString(const std::string& ip_str) noexcept;
93  static IpAddr WildcardV4() noexcept;
94  static IpAddr WildcardV6() noexcept;
95  static IpAddr Wildcard(IpAddrType type) noexcept;
96  static IpAddr NetmaskV4(unsigned int mask_length) noexcept;
97  static IpAddr NetmaskV6(unsigned int mask_length) noexcept;
98  static IpAddr Netmask(IpAddrType type, unsigned int mask_length) noexcept;
99 
100 private:
102 };
103 
106 {
107  ETCPAL_IP_SET_INVALID(&addr_);
108 }
109 
111 constexpr IpAddr::IpAddr(const EtcPalIpAddr& c_ip) noexcept : addr_(c_ip)
112 {
113 }
114 
116 inline IpAddr& IpAddr::operator=(const EtcPalIpAddr& c_ip) noexcept
117 {
118  addr_ = c_ip;
119  return *this;
120 }
121 
125 {
126  ETCPAL_IP_SET_V4_ADDRESS(&addr_, v4_data);
127 }
128 
131 inline IpAddr::IpAddr(const uint8_t* v6_data) noexcept
132 {
133  ETCPAL_IP_SET_V6_ADDRESS(&addr_, v6_data);
134 }
135 
142 inline IpAddr::IpAddr(const uint8_t* v6_data, unsigned long scope_id) noexcept
143 {
144  ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(&addr_, v6_data, scope_id);
145 }
146 
148 constexpr const EtcPalIpAddr& IpAddr::get() const noexcept
149 {
150  return addr_;
151 }
152 
155 {
156  return addr_;
157 }
158 
162 inline std::string IpAddr::ToString() const
163 {
164  std::array<char, ETCPAL_IP_STRING_BYTES> str_buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
165 
166  auto result = etcpal_ip_to_string(&addr_, str_buf.data());
167  if (result == kEtcPalErrOk)
168  return {str_buf.data()};
169  return {};
170 }
171 
177 constexpr uint32_t IpAddr::v4_data() const noexcept
178 {
179  return ETCPAL_IP_V4_ADDRESS(&addr_);
180 }
181 
186 constexpr const uint8_t* IpAddr::v6_data() const noexcept
187 {
188  return ETCPAL_IP_V6_ADDRESS(&addr_);
189 }
190 
195 inline std::array<uint8_t, ETCPAL_IPV6_BYTES> IpAddr::ToV6Array() const
196 {
197  // RVO should hopefully make this only a single copy
198  std::array<uint8_t, ETCPAL_IPV6_BYTES> arr; // NOLINT(cppcoreguidelines-pro-type-member-init)
199  std::memcpy(arr.data(), ETCPAL_IP_V6_ADDRESS(&addr_), ETCPAL_IPV6_BYTES);
200  return arr;
201 }
202 
212 constexpr unsigned long IpAddr::scope_id() const noexcept
213 {
214  return ETCPAL_IP_V6_SCOPE_ID(&addr_);
215 }
216 
218 constexpr bool IpAddr::IsValid() const noexcept
219 {
220  return !ETCPAL_IP_IS_INVALID(&addr_);
221 }
222 
224 constexpr IpAddrType IpAddr::type() const noexcept
225 {
226  return static_cast<IpAddrType>(addr_.type);
227 }
228 
230 constexpr bool IpAddr::IsV4() const noexcept
231 {
232  return ETCPAL_IP_IS_V4(&addr_);
233 }
234 
236 constexpr bool IpAddr::IsV6() const noexcept
237 {
238  return ETCPAL_IP_IS_V6(&addr_);
239 }
240 
242 inline bool IpAddr::IsLinkLocal() const noexcept
243 {
244  return etcpal_ip_is_link_local(&addr_);
245 }
246 
248 inline bool IpAddr::IsLoopback() const noexcept
249 {
250  return etcpal_ip_is_loopback(&addr_);
251 }
252 
254 inline bool IpAddr::IsMulticast() const noexcept
255 {
256  return etcpal_ip_is_multicast(&addr_);
257 }
258 
262 inline bool IpAddr::IsWildcard() const noexcept
263 {
264  return etcpal_ip_is_wildcard(&addr_);
265 }
266 
270 inline unsigned int IpAddr::MaskLength() const noexcept
271 {
272  return etcpal_ip_mask_length(&addr_);
273 }
274 
280 inline void IpAddr::SetAddress(uint32_t v4_data) noexcept
281 {
282  ETCPAL_IP_SET_V4_ADDRESS(&addr_, v4_data);
283 }
284 
290 inline void IpAddr::SetAddress(const uint8_t* v6_data) noexcept
291 {
292  ETCPAL_IP_SET_V6_ADDRESS(&addr_, v6_data);
293 }
294 
301 inline void IpAddr::SetAddress(const uint8_t* v6_data, unsigned long scope_id) noexcept
302 {
303  ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(&addr_, v6_data, scope_id);
304 }
305 
309 inline IpAddr IpAddr::FromString(const char* ip_str) noexcept
310 {
311  IpAddr result;
312  if (etcpal_string_to_ip(kEtcPalIpTypeV4, ip_str, &result.addr_) != kEtcPalErrOk)
313  {
314  etcpal_string_to_ip(kEtcPalIpTypeV6, ip_str, &result.addr_);
315  }
316  return result;
317 }
318 
322 inline IpAddr IpAddr::FromString(const std::string& ip_str) noexcept
323 {
324  return FromString(ip_str.c_str());
325 }
326 
330 inline IpAddr IpAddr::WildcardV4() noexcept
331 {
332  return Wildcard(IpAddrType::kV4);
333 }
334 
338 inline IpAddr IpAddr::WildcardV6() noexcept
339 {
340  return Wildcard(IpAddrType::kV6);
341 }
342 
346 inline IpAddr IpAddr::Wildcard(IpAddrType type) noexcept
347 {
348  IpAddr result;
349  etcpal_ip_set_wildcard(static_cast<etcpal_iptype_t>(type), &result.addr_);
350  return result;
351 }
352 
356 inline IpAddr IpAddr::NetmaskV4(unsigned int mask_length) noexcept
357 {
358  return Netmask(IpAddrType::kV4, mask_length);
359 }
360 
364 inline IpAddr IpAddr::NetmaskV6(unsigned int mask_length) noexcept
365 {
366  return Netmask(IpAddrType::kV6, mask_length);
367 }
368 
372 inline IpAddr IpAddr::Netmask(IpAddrType type, unsigned int mask_length) noexcept
373 {
374  return etcpal_ip_mask_from_length(static_cast<etcpal_iptype_t>(type), mask_length);
375 }
376 
382 class SockAddr
383 {
384 public:
385  ETCPAL_CONSTEXPR_14 SockAddr() noexcept;
386  // Note: this constructor is not explicit by design, to allow implicit conversions e.g.
387  // etcpal::SockAddr sa = etcpal_c_function_that_returns_sockaddr();
388  constexpr SockAddr(const EtcPalSockAddr& c_sa) noexcept;
389  SockAddr& operator=(const EtcPalSockAddr& c_sa) noexcept;
390 
391  ETCPAL_CONSTEXPR_14 SockAddr(uint32_t v4_data, uint16_t port) noexcept;
392  SockAddr(const uint8_t* v6_data, uint16_t port) noexcept;
393  SockAddr(const uint8_t* v6_data, unsigned long scope_id, uint16_t port) noexcept;
394  ETCPAL_CONSTEXPR_14 SockAddr(IpAddr ip, uint16_t port) noexcept;
395 
396  constexpr const EtcPalSockAddr& get() const noexcept;
398  std::string ToString() const;
399  constexpr IpAddr ip() const noexcept;
400  constexpr uint16_t port() const noexcept;
401 
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;
406 
407  constexpr bool IsValid() const noexcept;
408  constexpr IpAddrType type() const noexcept;
409  constexpr bool IsV4() const noexcept;
410  constexpr bool IsV6() const noexcept;
411  bool IsLinkLocal() const noexcept;
412  bool IsLoopback() const noexcept;
413  bool IsMulticast() const noexcept;
414  bool IsWildcard() const noexcept;
415 
416  void SetAddress(uint32_t v4_data) noexcept;
417  void SetAddress(const uint8_t* v6_data) noexcept;
418  void SetAddress(const uint8_t* v6_data, unsigned long scope_id) noexcept;
419  void SetAddress(const IpAddr& ip) noexcept;
420  void SetPort(uint16_t port) noexcept;
421 
422 private:
424 };
425 
428 {
429  ETCPAL_IP_SET_INVALID(&addr_.ip);
430 }
431 
433 constexpr SockAddr::SockAddr(const EtcPalSockAddr& c_sa) noexcept : addr_(c_sa)
434 {
435 }
436 
438 inline SockAddr& SockAddr::operator=(const EtcPalSockAddr& c_sa) noexcept
439 {
440  addr_ = c_sa;
441  return *this;
442 }
443 
447 ETCPAL_CONSTEXPR_14_OR_INLINE SockAddr::SockAddr(uint32_t v4_data, uint16_t port) noexcept
448 {
449  ETCPAL_IP_SET_V4_ADDRESS(&addr_.ip, v4_data);
450  addr_.port = port;
451 }
452 
456 inline SockAddr::SockAddr(const uint8_t* v6_data, uint16_t port) noexcept
457 {
458  ETCPAL_IP_SET_V6_ADDRESS(&addr_.ip, v6_data);
459  addr_.port = port;
460 }
461 
469 inline SockAddr::SockAddr(const uint8_t* v6_data, unsigned long scope_id, uint16_t port) noexcept
470 {
471  ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(&addr_.ip, v6_data, scope_id);
472  addr_.port = port;
473 }
474 
479 {
480  addr_.ip = ip.get();
481  addr_.port = port;
482 }
483 
485 constexpr const EtcPalSockAddr& SockAddr::get() const noexcept
486 {
487  return addr_;
488 }
489 
492 {
493  return addr_;
494 }
495 
501 inline std::string SockAddr::ToString() const
502 {
503  if (ETCPAL_IP_IS_V4(&addr_.ip))
504  return ip().ToString() + ':' + std::to_string(addr_.port);
505  if (ETCPAL_IP_IS_V6(&addr_.ip))
506  return '[' + ip().ToString() + "]:" + std::to_string(addr_.port);
507  return {};
508 }
509 
511 constexpr IpAddr SockAddr::ip() const noexcept
512 {
513  return addr_.ip;
514 }
515 
517 constexpr uint16_t SockAddr::port() const noexcept
518 {
519  return addr_.port;
520 }
521 
527 constexpr uint32_t SockAddr::v4_data() const noexcept
528 {
529  return ETCPAL_IP_V4_ADDRESS(&addr_.ip);
530 }
531 
536 constexpr const uint8_t* SockAddr::v6_data() const noexcept
537 {
538  return ETCPAL_IP_V6_ADDRESS(&addr_.ip);
539 }
540 
545 inline std::array<uint8_t, ETCPAL_IPV6_BYTES> SockAddr::ToV6Array() const
546 {
547  // RVO should hopefully make this only a single copy
548  std::array<uint8_t, ETCPAL_IPV6_BYTES> arr; // NOLINT(cppcoreguidelines-pro-type-member-init)
549  std::memcpy(arr.data(), ETCPAL_IP_V6_ADDRESS(&addr_.ip), ETCPAL_IPV6_BYTES);
550  return arr;
551 }
552 
562 constexpr unsigned long SockAddr::scope_id() const noexcept
563 {
564  return ETCPAL_IP_V6_SCOPE_ID(&addr_.ip);
565 }
566 
568 constexpr bool SockAddr::IsValid() const noexcept
569 {
570  return !ETCPAL_IP_IS_INVALID(&addr_.ip);
571 }
572 
574 constexpr IpAddrType SockAddr::type() const noexcept
575 {
576  return static_cast<IpAddrType>(addr_.ip.type);
577 }
578 
580 constexpr bool SockAddr::IsV4() const noexcept
581 {
582  return ETCPAL_IP_IS_V4(&addr_.ip);
583 }
584 
586 constexpr bool SockAddr::IsV6() const noexcept
587 {
588  return ETCPAL_IP_IS_V6(&addr_.ip);
589 }
590 
592 inline bool SockAddr::IsLinkLocal() const noexcept
593 {
594  return etcpal_ip_is_link_local(&addr_.ip);
595 }
596 
598 inline bool SockAddr::IsLoopback() const noexcept
599 {
600  return etcpal_ip_is_loopback(&addr_.ip);
601 }
602 
604 inline bool SockAddr::IsMulticast() const noexcept
605 {
606  return etcpal_ip_is_multicast(&addr_.ip);
607 }
608 
612 inline bool SockAddr::IsWildcard() const noexcept
613 {
614  return etcpal_ip_is_wildcard(&addr_.ip);
615 }
616 
622 inline void SockAddr::SetAddress(uint32_t v4_data) noexcept
623 {
624  ETCPAL_IP_SET_V4_ADDRESS(&addr_.ip, v4_data);
625 }
626 
632 inline void SockAddr::SetAddress(const uint8_t* v6_data) noexcept
633 {
634  ETCPAL_IP_SET_V6_ADDRESS(&addr_.ip, v6_data);
635 }
636 
643 inline void SockAddr::SetAddress(const uint8_t* v6_data, unsigned long scope_id) noexcept
644 {
645  ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(&addr_.ip, v6_data, scope_id);
646 }
647 
650 inline void SockAddr::SetAddress(const IpAddr& ip) noexcept
651 {
652  addr_.ip = ip.get();
653 }
654 
657 inline void SockAddr::SetPort(uint16_t port) noexcept
658 {
659  addr_.port = port;
660 }
661 
666 class MacAddr
667 {
668 public:
670  MacAddr() = default;
671  // Note: this constructor is not explicit by design, to allow implicit conversions e.g.
672  // etcpal::MacAddr sa = etcpal_c_function_that_returns_macaddr();
673  constexpr MacAddr(const EtcPalMacAddr& c_mac) noexcept;
674  MacAddr& operator=(const EtcPalMacAddr& c_mac) noexcept;
675  explicit MacAddr(const uint8_t* mac_data) noexcept;
676 
677  constexpr const EtcPalMacAddr& get() const noexcept;
679  std::string ToString() const;
680  constexpr const uint8_t* data() const noexcept;
681  std::array<uint8_t, ETCPAL_MAC_BYTES> ToArray() const noexcept;
682 
683  bool IsNull() const noexcept;
684 
685  static MacAddr FromString(const char* mac_str) noexcept;
686  static MacAddr FromString(const std::string& mac_str) noexcept;
687 
688 private:
689  EtcPalMacAddr addr_{};
690 };
691 
693 constexpr MacAddr::MacAddr(const EtcPalMacAddr& c_mac) noexcept : addr_(c_mac)
694 {
695 }
696 
698 inline MacAddr& MacAddr::operator=(const EtcPalMacAddr& c_mac) noexcept
699 {
700  addr_ = c_mac;
701  return *this;
702 }
703 
706 inline MacAddr::MacAddr(const uint8_t* mac_data) noexcept
707 {
708  std::memcpy(addr_.data, mac_data, ETCPAL_MAC_BYTES);
709 }
710 
712 constexpr const EtcPalMacAddr& MacAddr::get() const noexcept
713 {
714  return addr_;
715 }
716 
719 {
720  return addr_;
721 }
722 
726 inline std::string MacAddr::ToString() const
727 {
728  std::array<char, ETCPAL_MAC_STRING_BYTES> str_buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
729  etcpal_mac_to_string(&addr_, str_buf.data());
730  return {str_buf.data()};
731 }
732 
735 constexpr const uint8_t* MacAddr::data() const noexcept
736 {
737  return addr_.data;
738 }
739 
743 inline std::array<uint8_t, ETCPAL_MAC_BYTES> MacAddr::ToArray() const noexcept
744 {
745  // RVO should hopefully make this only a single copy
746  std::array<uint8_t, ETCPAL_MAC_BYTES> arr; // NOLINT(cppcoreguidelines-pro-type-member-init)
747  std::memcpy(arr.data(), addr_.data, ETCPAL_MAC_BYTES);
748  return arr;
749 }
750 
752 inline bool MacAddr::IsNull() const noexcept
753 {
754  return ETCPAL_MAC_IS_NULL(&addr_);
755 }
756 
760 inline MacAddr MacAddr::FromString(const char* mac_str) noexcept
761 {
762  MacAddr result;
763  etcpal_string_to_mac(mac_str, &result.addr_);
764  return result;
765 }
766 
770 inline MacAddr MacAddr::FromString(const std::string& mac_str) noexcept
771 {
772  return FromString(mac_str.c_str());
773 }
774 
776 // Operators
778 
781 
784 
785 // Special operators for comparing with the underlying types
786 
787 inline bool operator==(const EtcPalIpAddr& c_ip, const IpAddr& ip) noexcept
788 {
789  return c_ip == ip.get();
790 }
791 
792 inline bool operator!=(const EtcPalIpAddr& c_ip, const IpAddr& ip) noexcept
793 {
794  return !(c_ip == ip);
795 }
796 
797 inline bool operator==(const IpAddr& ip, const EtcPalIpAddr& c_ip) noexcept
798 {
799  return ip.get() == c_ip;
800 }
801 
802 inline bool operator!=(const IpAddr& ip, const EtcPalIpAddr& c_ip) noexcept
803 {
804  return !(ip == c_ip);
805 }
806 
807 inline bool operator==(const EtcPalSockAddr& c_ip, const SockAddr& ip) noexcept
808 {
809  return c_ip == ip.get();
810 }
811 
812 inline bool operator!=(const EtcPalSockAddr& c_ip, const SockAddr& ip) noexcept
813 {
814  return !(c_ip == ip);
815 }
816 
817 inline bool operator==(const SockAddr& ip, const EtcPalSockAddr& c_ip) noexcept
818 {
819  return ip.get() == c_ip;
820 }
821 
822 inline bool operator!=(const SockAddr& ip, const EtcPalSockAddr& c_ip) noexcept
823 {
824  return !(ip == c_ip);
825 }
826 
827 inline bool operator==(const EtcPalMacAddr& c_mac, const MacAddr& mac) noexcept
828 {
829  return c_mac == mac.get();
830 }
831 
832 inline bool operator!=(const EtcPalMacAddr& c_mac, const MacAddr& mac) noexcept
833 {
834  return !(c_mac == mac);
835 }
836 
837 inline bool operator==(const MacAddr& mac, const EtcPalMacAddr& c_mac) noexcept
838 {
839  return mac.get() == c_mac;
840 }
841 
842 inline bool operator!=(const MacAddr& mac, const EtcPalMacAddr& c_mac) noexcept
843 {
844  return !(mac == c_mac);
845 }
846 
847 // Standard operators
848 
849 inline bool operator==(const IpAddr& a, const IpAddr& b) noexcept
850 {
851  return a.get() == b.get();
852 }
853 
854 inline bool operator!=(const IpAddr& a, const IpAddr& b) noexcept
855 {
856  return !(a == b);
857 }
858 
859 inline bool operator<(const IpAddr& a, const IpAddr& b) noexcept
860 {
861  return a.get() < b.get();
862 }
863 
864 inline bool operator>(const IpAddr& a, const IpAddr& b) noexcept
865 {
866  return b < a;
867 }
868 
869 inline bool operator<=(const IpAddr& a, const IpAddr& b) noexcept
870 {
871  return !(b < a);
872 }
873 
874 inline bool operator>=(const IpAddr& a, const IpAddr& b) noexcept
875 {
876  return !(a < b);
877 }
878 
879 inline bool operator==(const SockAddr& a, const SockAddr& b) noexcept
880 {
881  return a.get() == b.get();
882 }
883 
884 inline bool operator!=(const SockAddr& a, const SockAddr& b) noexcept
885 {
886  return !(a == b);
887 }
888 
889 inline bool operator<(const SockAddr& a, const SockAddr& b) noexcept
890 {
891  return a.get() < b.get();
892 }
893 
894 inline bool operator>(const SockAddr& a, const SockAddr& b) noexcept
895 {
896  return b < a;
897 }
898 
899 inline bool operator<=(const SockAddr& a, const SockAddr& b) noexcept
900 {
901  return !(b < a);
902 }
903 
904 inline bool operator>=(const SockAddr& a, const SockAddr& b) noexcept
905 {
906  return !(a < b);
907 }
908 
909 inline bool operator==(const MacAddr& a, const MacAddr& b) noexcept
910 {
911  return a.get() == b.get();
912 }
913 
914 inline bool operator!=(const MacAddr& a, const MacAddr& b) noexcept
915 {
916  return !(a == b);
917 }
918 
919 inline bool operator<(const MacAddr& a, const MacAddr& b) noexcept
920 {
921  return a.get() < b.get();
922 }
923 
924 inline bool operator>(const MacAddr& a, const MacAddr& b) noexcept
925 {
926  return b < a;
927 }
928 
929 inline bool operator<=(const MacAddr& a, const MacAddr& b) noexcept
930 {
931  return !(b < a);
932 }
933 
934 inline bool operator>=(const MacAddr& a, const MacAddr& b) noexcept
935 {
936  return !(a < b);
937 }
938 
941 
942 }; // namespace etcpal
943 
944 #endif // ETCPAL_CPP_LOCK_H_
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