11#pragma once
22
3- #include < iostream >
3+ #include < algorithm >
44#include < array>
5+ #include < compare>
56#include < cstdint>
67#include < cstring>
7- #include < limits>
8- #include < compare>
9- #include < algorithm>
108#include < format>
9+ #include < iostream>
10+ #include < limits>
1111
1212#include < ipfixprobe/ipaddr.hpp>
1313
@@ -19,10 +19,7 @@ union IPAddress {
1919 std::array<uint32_t , 4 > u32 ;
2020 std::array<uint64_t , 2 > u64 ;
2121
22- constexpr IPAddress () noexcept
23- {
24- std::memset (&u8 , 0 , sizeof (IPAddress));
25- }
22+ constexpr IPAddress () noexcept { std::memset (&u8 , 0 , sizeof (IPAddress)); }
2623
2724 constexpr IPAddress (const uint32_t ipv4) noexcept
2825 {
@@ -37,85 +34,85 @@ union IPAddress {
3734 }
3835
3936 constexpr IPAddress (const ipaddr_t address, IP version) noexcept
40- : IPAddress (address.v4 )
37+ : IPAddress (address.v4 )
4138 {
4239 if (version == IP ::v6) {
4340 std::memcpy (&u8 , address.v6 , 16 );
4441 }
4542 }
4643
47- constexpr IPAddress (const IPAddress& other) noexcept
44+ // TODO remove. Added because of AMON compatibility
45+ constexpr IPAddress (const auto & container)
4846 {
49- u8 = other.u8 ;
50- }
47+ if (container.size () != 4 && container.size () != 16 ) {
48+ throw std::invalid_argument (" IPAddress: container must have size 4 or 16" );
49+ }
5150
52- constexpr bool isIPv4 () const noexcept
53- {
54- return u32 [1 ] == 0 &&
55- u32 [2 ] == std::numeric_limits<uint32_t >::max () &&
56- u32 [ 3 ] == std::numeric_limits< uint32_t >:: max ();
51+ std::memcpy (& u8 , container. data (), container. size ());
52+ if (container. size () == 4 ) {
53+ u32 [1 ] = 0 ;
54+ u32 [2 ] = u32 [ 3 ] = std::numeric_limits<uint32_t >::max ();
55+ }
5756 }
5857
59- constexpr bool isIPv6 () const noexcept
58+ constexpr IPAddress (const IPAddress& other) noexcept { u8 = other.u8 ; }
59+
60+ constexpr bool isIPv4 () const noexcept
6061 {
61- return !isIPv4 ();
62+ return u32 [1 ] == 0 && u32 [2 ] == std::numeric_limits<uint32_t >::max ()
63+ && u32 [3 ] == std::numeric_limits<uint32_t >::max ();
6264 }
6365
64- // constexpr bool operator==(const IPAddress& ) const noexcept = default;
66+ constexpr bool isIPv6 ( ) const noexcept { return ! isIPv4 (); }
6567
68+ // constexpr bool operator==(const IPAddress&) const noexcept = default;
6669
67- constexpr auto operator <=>(const IPAddress& other) const noexcept
68- {
69- return u64 <=> other.u64 ;
70- }
70+ constexpr auto operator <=>(const IPAddress& other) const noexcept { return u64 <=> other.u64 ; }
7171
72- constexpr bool operator ==(const IPAddress& other) const noexcept
73- {
74- return u64 == other.u64 ;
75- }
72+ constexpr bool operator ==(const IPAddress& other) const noexcept { return u64 == other.u64 ; }
7673
77- // constexpr std::strong_ordering operator<=>(const IPAddress& other) const noexcept = default;
74+ // constexpr std::strong_ordering operator<=>(const IPAddress& other) const noexcept = default;
7875
7976 constexpr IPAddress& operator =(const IPAddress& other) noexcept
8077 {
81- if (this != &other) {
78+ if (this != &other) {
8279 u8 = other.u8 ;
8380 }
8481
8582 return *this ;
8683 }
8784
88- constexpr std::size_t size () const noexcept
89- {
90- return isIPv4 () ? 4 : 16 ;
91- }
85+ constexpr std::size_t size () const noexcept { return isIPv4 () ? 4 : 16 ; }
9286
93- std::string toString () const noexcept
87+ std::string toString () const noexcept
9488 {
9589 std::string res;
9690 constexpr std::size_t MAX_IP_AS_TEXT_SIZE = 30 ;
9791 res.reserve (MAX_IP_AS_TEXT_SIZE );
9892
9993 if (isIPv4 ()) {
100- std::for_each_n (reinterpret_cast <const std::byte*>(u8 .data ()), size (),
101- [&](const std::byte ipByte) {
102- std::format_to (std::back_inserter (res), " {}." , static_cast <int >(ipByte));
103- });
94+ std::for_each_n (
95+ reinterpret_cast <const std::byte*>(u8 .data ()),
96+ size (),
97+ [&](const std::byte ipByte) {
98+ std::format_to (std::back_inserter (res), " {}." , static_cast <int >(ipByte));
99+ });
104100 } else {
105- std::for_each_n (reinterpret_cast <const std::byte*>(u8 .data ()), size (),
106- [&](const std::byte ipByte) {
107- std::format_to (std::back_inserter (res), " {:02x}:" , static_cast <int >(ipByte));
108- });
101+ std::for_each_n (
102+ reinterpret_cast <const std::byte*>(u8 .data ()),
103+ size (),
104+ [&](const std::byte ipByte) {
105+ std::format_to (std::back_inserter (res), " {:02x}:" , static_cast <int >(ipByte));
106+ });
109107 }
110108 res.pop_back ();
111109 return res;
112110 }
113-
114111};
115112
116- /* friend constexpr auto operator<=>(const IPAddress& lhs, const IPAddress& rhs) noexcept
113+ /* friend constexpr auto operator<=>(const IPAddress& lhs, const IPAddress& rhs) noexcept
117114{
118- return lhs.u64 <=> rhs.u64;
115+ return lhs.u64 <=> rhs.u64;
119116}*/
120117
121118inline std::ostream& operator <<(std::ostream& os, const IPAddress& ip)
0 commit comments