EtcPal  0.3.0
ETC Platform Abstraction Layer (EtcPal)
View other versions:
inet.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 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  Invalid = kEtcPalIpTypeInvalid,
48  V4 = kEtcPalIpTypeV4,
49  V6 = 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  char str_buf[ETCPAL_IP_STRING_BYTES];
165  auto result = etcpal_ip_to_string(&addr_, str_buf);
166  if (result == kEtcPalErrOk)
167  return str_buf;
168  else
169  return std::string();
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;
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::V4);
333 }
334 
338 inline IpAddr IpAddr::WildcardV6() noexcept
339 {
340  return Wildcard(IpAddrType::V6);
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::V4, mask_length);
359 }
360 
364 inline IpAddr IpAddr::NetmaskV6(unsigned int mask_length) noexcept
365 {
366  return Netmask(IpAddrType::V6, 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  else if (ETCPAL_IP_IS_V6(&addr_.ip))
506  return '[' + ip().ToString() + "]:" + std::to_string(addr_.port);
507  else
508  return std::string();
509 }
510 
512 constexpr IpAddr SockAddr::ip() const noexcept
513 {
514  return addr_.ip;
515 }
516 
518 constexpr uint16_t SockAddr::port() const noexcept
519 {
520  return addr_.port;
521 }
522 
528 constexpr uint32_t SockAddr::v4_data() const noexcept
529 {
530  return ETCPAL_IP_V4_ADDRESS(&addr_.ip);
531 }
532 
537 constexpr const uint8_t* SockAddr::v6_data() const noexcept
538 {
539  return ETCPAL_IP_V6_ADDRESS(&addr_.ip);
540 }
541 
546 inline std::array<uint8_t, ETCPAL_IPV6_BYTES> SockAddr::ToV6Array() const
547 {
548  // RVO should hopefully make this only a single copy
549  std::array<uint8_t, ETCPAL_IPV6_BYTES> arr;
550  std::memcpy(arr.data(), ETCPAL_IP_V6_ADDRESS(&addr_.ip), ETCPAL_IPV6_BYTES);
551  return arr;
552 }
553 
563 constexpr unsigned long SockAddr::scope_id() const noexcept
564 {
565  return ETCPAL_IP_V6_SCOPE_ID(&addr_.ip);
566 }
567 
569 constexpr bool SockAddr::IsValid() const noexcept
570 {
571  return !ETCPAL_IP_IS_INVALID(&addr_.ip);
572 }
573 
575 constexpr IpAddrType SockAddr::type() const noexcept
576 {
577  return static_cast<IpAddrType>(addr_.ip.type);
578 }
579 
581 constexpr bool SockAddr::IsV4() const noexcept
582 {
583  return ETCPAL_IP_IS_V4(&addr_.ip);
584 }
585 
587 constexpr bool SockAddr::IsV6() const noexcept
588 {
589  return ETCPAL_IP_IS_V6(&addr_.ip);
590 }
591 
593 inline bool SockAddr::IsLinkLocal() const noexcept
594 {
595  return etcpal_ip_is_link_local(&addr_.ip);
596 }
597 
599 inline bool SockAddr::IsLoopback() const noexcept
600 {
601  return etcpal_ip_is_loopback(&addr_.ip);
602 }
603 
605 inline bool SockAddr::IsMulticast() const noexcept
606 {
607  return etcpal_ip_is_multicast(&addr_.ip);
608 }
609 
613 inline bool SockAddr::IsWildcard() const noexcept
614 {
615  return etcpal_ip_is_wildcard(&addr_.ip);
616 }
617 
623 inline void SockAddr::SetAddress(uint32_t v4_data) noexcept
624 {
625  ETCPAL_IP_SET_V4_ADDRESS(&addr_.ip, v4_data);
626 }
627 
633 inline void SockAddr::SetAddress(const uint8_t* v6_data) noexcept
634 {
635  ETCPAL_IP_SET_V6_ADDRESS(&addr_.ip, v6_data);
636 }
637 
644 inline void SockAddr::SetAddress(const uint8_t* v6_data, unsigned long scope_id) noexcept
645 {
646  ETCPAL_IP_SET_V6_ADDRESS_WITH_SCOPE_ID(&addr_.ip, v6_data, scope_id);
647 }
648 
651 inline void SockAddr::SetAddress(const IpAddr& ip) noexcept
652 {
653  addr_.ip = ip.get();
654 }
655 
658 inline void SockAddr::SetPort(uint16_t port) noexcept
659 {
660  addr_.port = port;
661 }
662 
667 class MacAddr
668 {
669 public:
671  MacAddr() = default;
672  // Note: this constructor is not explicit by design, to allow implicit conversions e.g.
673  // etcpal::MacAddr sa = etcpal_c_function_that_returns_macaddr();
674  constexpr MacAddr(const EtcPalMacAddr& c_mac) noexcept;
675  MacAddr& operator=(const EtcPalMacAddr& c_mac) noexcept;
676  explicit MacAddr(const uint8_t* mac_data) noexcept;
677 
678  constexpr const EtcPalMacAddr& get() const noexcept;
680  std::string ToString() const;
681  constexpr const uint8_t* data() const noexcept;
682  std::array<uint8_t, ETCPAL_MAC_BYTES> ToArray() const noexcept;
683 
684  bool IsNull() const noexcept;
685 
686  static MacAddr FromString(const char* mac_str) noexcept;
687  static MacAddr FromString(const std::string& mac_str) noexcept;
688 
689 private:
690  EtcPalMacAddr addr_{};
691 };
692 
694 constexpr MacAddr::MacAddr(const EtcPalMacAddr& c_mac) noexcept : addr_(c_mac)
695 {
696 }
697 
699 inline MacAddr& MacAddr::operator=(const EtcPalMacAddr& c_mac) noexcept
700 {
701  addr_ = c_mac;
702  return *this;
703 }
704 
707 inline MacAddr::MacAddr(const uint8_t* mac_data) noexcept
708 {
709  std::memcpy(addr_.data, mac_data, ETCPAL_MAC_BYTES);
710 }
711 
713 constexpr const EtcPalMacAddr& MacAddr::get() const noexcept
714 {
715  return addr_;
716 }
717 
720 {
721  return addr_;
722 }
723 
727 inline std::string MacAddr::ToString() const
728 {
729  char str_buf[ETCPAL_MAC_STRING_BYTES];
730  etcpal_mac_to_string(&addr_, str_buf);
731  return str_buf;
732 }
733 
736 constexpr const uint8_t* MacAddr::data() const noexcept
737 {
738  return addr_.data;
739 }
740 
744 inline std::array<uint8_t, ETCPAL_MAC_BYTES> MacAddr::ToArray() const noexcept
745 {
746  // RVO should hopefully make this only a single copy
747  std::array<uint8_t, ETCPAL_MAC_BYTES> arr;
748  std::memcpy(arr.data(), addr_.data, ETCPAL_MAC_BYTES);
749  return arr;
750 }
751 
753 inline bool MacAddr::IsNull() const noexcept
754 {
755  return ETCPAL_MAC_IS_NULL(&addr_);
756 }
757 
761 inline MacAddr MacAddr::FromString(const char* mac_str) noexcept
762 {
763  MacAddr result;
764  etcpal_string_to_mac(mac_str, &result.addr_);
765  return result;
766 }
767 
771 inline MacAddr MacAddr::FromString(const std::string& mac_str) noexcept
772 {
773  return FromString(mac_str.c_str());
774 }
775 
777 // Operators
779 
782 
785 
786 // Special operators for comparing with the underlying types
787 
788 inline bool operator==(const EtcPalIpAddr& c_ip, const IpAddr& ip) noexcept
789 {
790  return c_ip == ip.get();
791 }
792 
793 inline bool operator!=(const EtcPalIpAddr& c_ip, const IpAddr& ip) noexcept
794 {
795  return !(c_ip == ip);
796 }
797 
798 inline bool operator==(const IpAddr& ip, const EtcPalIpAddr& c_ip) noexcept
799 {
800  return ip.get() == c_ip;
801 }
802 
803 inline bool operator!=(const IpAddr& ip, const EtcPalIpAddr& c_ip) noexcept
804 {
805  return !(ip == c_ip);
806 }
807 
808 inline bool operator==(const EtcPalSockAddr& c_ip, const SockAddr& ip) noexcept
809 {
810  return c_ip == ip.get();
811 }
812 
813 inline bool operator!=(const EtcPalSockAddr& c_ip, const SockAddr& ip) noexcept
814 {
815  return !(c_ip == ip);
816 }
817 
818 inline bool operator==(const SockAddr& ip, const EtcPalSockAddr& c_ip) noexcept
819 {
820  return ip.get() == c_ip;
821 }
822 
823 inline bool operator!=(const SockAddr& ip, const EtcPalSockAddr& c_ip) noexcept
824 {
825  return !(ip == c_ip);
826 }
827 
828 inline bool operator==(const EtcPalMacAddr& c_mac, const MacAddr& mac) noexcept
829 {
830  return c_mac == mac.get();
831 }
832 
833 inline bool operator!=(const EtcPalMacAddr& c_mac, const MacAddr& mac) noexcept
834 {
835  return !(c_mac == mac);
836 }
837 
838 inline bool operator==(const MacAddr& mac, const EtcPalMacAddr& c_mac) noexcept
839 {
840  return mac.get() == c_mac;
841 }
842 
843 inline bool operator!=(const MacAddr& mac, const EtcPalMacAddr& c_mac) noexcept
844 {
845  return !(mac == c_mac);
846 }
847 
848 // Standard operators
849 
850 inline bool operator==(const IpAddr& a, const IpAddr& b) noexcept
851 {
852  return a.get() == b.get();
853 }
854 
855 inline bool operator!=(const IpAddr& a, const IpAddr& b) noexcept
856 {
857  return !(a == b);
858 }
859 
860 inline bool operator<(const IpAddr& a, const IpAddr& b) noexcept
861 {
862  return a.get() < b.get();
863 }
864 
865 inline bool operator>(const IpAddr& a, const IpAddr& b) noexcept
866 {
867  return b < a;
868 }
869 
870 inline bool operator<=(const IpAddr& a, const IpAddr& b) noexcept
871 {
872  return !(b < a);
873 }
874 
875 inline bool operator>=(const IpAddr& a, const IpAddr& b) noexcept
876 {
877  return !(a < b);
878 }
879 
880 inline bool operator==(const SockAddr& a, const SockAddr& b) noexcept
881 {
882  return a.get() == b.get();
883 }
884 
885 inline bool operator!=(const SockAddr& a, const SockAddr& b) noexcept
886 {
887  return !(a == b);
888 }
889 
890 inline bool operator<(const SockAddr& a, const SockAddr& b) noexcept
891 {
892  return a.get() < b.get();
893 }
894 
895 inline bool operator>(const SockAddr& a, const SockAddr& b) noexcept
896 {
897  return b < a;
898 }
899 
900 inline bool operator<=(const SockAddr& a, const SockAddr& b) noexcept
901 {
902  return !(b < a);
903 }
904 
905 inline bool operator>=(const SockAddr& a, const SockAddr& b) noexcept
906 {
907  return !(a < b);
908 }
909 
910 inline bool operator==(const MacAddr& a, const MacAddr& b) noexcept
911 {
912  return a.get() == b.get();
913 }
914 
915 inline bool operator!=(const MacAddr& a, const MacAddr& b) noexcept
916 {
917  return !(a == b);
918 }
919 
920 inline bool operator<(const MacAddr& a, const MacAddr& b) noexcept
921 {
922  return a.get() < b.get();
923 }
924 
925 inline bool operator>(const MacAddr& a, const MacAddr& b) noexcept
926 {
927  return b < a;
928 }
929 
930 inline bool operator<=(const MacAddr& a, const MacAddr& b) noexcept
931 {
932  return !(b < a);
933 }
934 
935 inline bool operator>=(const MacAddr& a, const MacAddr& b) noexcept
936 {
937  return !(a < b);
938 }
939 
942 
943 }; // namespace etcpal
944 
945 #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:668
std::string ToString() const
Convert the MAC address to a string representation.
Definition: inet.h:727
bool IsNull() const noexcept
Whether this MacAddr represents a null (all 0's) MAC address.
Definition: inet.h:753
static MacAddr FromString(const char *mac_str) noexcept
Construct a MacAddr from a string representation.
Definition: inet.h:761
constexpr const uint8_t * data() const noexcept
Get the raw 6-byte array representation of a MAC address.
Definition: inet.h:736
std::array< uint8_t, ETCPAL_MAC_BYTES > ToArray() const noexcept
Get a 6-byte std::array representation of a MAC address.
Definition: inet.h:744
constexpr const EtcPalMacAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: inet.h:713
MacAddr & operator=(const EtcPalMacAddr &c_mac) noexcept
Assign an instance of the C EtcPalMacAddr type to an instance of this class.
Definition: inet.h:699
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:587
constexpr unsigned long scope_id() const noexcept
Get the scope ID of the SockAddr's IPv6 address.
Definition: inet.h:563
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:593
void SetAddress(uint32_t v4_data) noexcept
Set the IPv4 address data.
Definition: inet.h:623
bool IsMulticast() const noexcept
Whether a SockAddr contains a multicast address.
Definition: inet.h:605
constexpr IpAddr ip() const noexcept
Get the IP address from the SockAddr.
Definition: inet.h:512
constexpr bool IsV4() const noexcept
Whether a SockAddr contains a valid IPv4 address.
Definition: inet.h:581
bool IsLoopback() const noexcept
Whether a SockAddr contains a loopback address.
Definition: inet.h:599
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:575
constexpr bool IsValid() const noexcept
Whether a SockAddr contains a valid IPv4 or IPv6 address.
Definition: inet.h:569
void SetPort(uint16_t port) noexcept
Set the port.
Definition: inet.h:658
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:546
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:518
bool IsWildcard() const noexcept
Whether a SockAddr contains a wildcard address.
Definition: inet.h:613
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:528
constexpr const uint8_t * v6_data() const noexcept
Get the raw 16-byte array representation of the SockAddr's IPv6 address.
Definition: inet.h:537
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:312
#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
#define ETCPAL_MAC_STRING_BYTES
Maximum length of the string representation of a MAC address.
Definition: inet.h:387
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:257
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_IP_STRING_BYTES
Maximum length of the string representation of an IP address.
Definition: inet.h:385
#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