forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_address.hpp
More file actions
147 lines (117 loc) · 3.16 KB
/
Copy pathip_address.hpp
File metadata and controls
147 lines (117 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_NET_IP_ADDRESS_HPP_INCLUDED
#define CPPCORO_NET_IP_ADDRESS_HPP_INCLUDED
#include <cppcoro/net/ipv4_address.hpp>
#include <cppcoro/net/ipv6_address.hpp>
#include <cassert>
#include <optional>
#include <string>
namespace cppcoro
{
namespace net
{
class ip_address
{
public:
// Constructs to IPv4 address 0.0.0.0
ip_address() noexcept;
ip_address(ipv4_address address) noexcept;
ip_address(ipv6_address address) noexcept;
bool is_ipv4() const noexcept { return m_family == family::ipv4; }
bool is_ipv6() const noexcept { return m_family == family::ipv6; }
const ipv4_address& to_ipv4() const;
const ipv6_address& to_ipv6() const;
const std::uint8_t* bytes() const noexcept;
std::string to_string() const;
static std::optional<ip_address> from_string(std::string_view string) noexcept;
bool operator==(const ip_address& rhs) const noexcept;
bool operator!=(const ip_address& rhs) const noexcept;
// ipv4_address sorts less than ipv6_address
bool operator<(const ip_address& rhs) const noexcept;
bool operator>(const ip_address& rhs) const noexcept;
bool operator<=(const ip_address& rhs) const noexcept;
bool operator>=(const ip_address& rhs) const noexcept;
private:
enum class family
{
ipv4,
ipv6
};
family m_family;
union
{
ipv4_address m_ipv4;
ipv6_address m_ipv6;
};
};
inline ip_address::ip_address() noexcept
: m_family(family::ipv4)
, m_ipv4()
{}
inline ip_address::ip_address(ipv4_address address) noexcept
: m_family(family::ipv4)
, m_ipv4(address)
{}
inline ip_address::ip_address(ipv6_address address) noexcept
: m_family(family::ipv6)
, m_ipv6(address)
{
}
inline const ipv4_address& ip_address::to_ipv4() const
{
assert(is_ipv4());
return m_ipv4;
}
inline const ipv6_address& ip_address::to_ipv6() const
{
assert(is_ipv6());
return m_ipv6;
}
inline const std::uint8_t* ip_address::bytes() const noexcept
{
return is_ipv4() ? m_ipv4.bytes() : m_ipv6.bytes();
}
inline bool ip_address::operator==(const ip_address& rhs) const noexcept
{
if (is_ipv4())
{
return rhs.is_ipv4() && m_ipv4 == rhs.m_ipv4;
}
else
{
return rhs.is_ipv6() && m_ipv6 == rhs.m_ipv6;
}
}
inline bool ip_address::operator!=(const ip_address& rhs) const noexcept
{
return !(*this == rhs);
}
inline bool ip_address::operator<(const ip_address& rhs) const noexcept
{
if (is_ipv4())
{
return !rhs.is_ipv4() || m_ipv4 < rhs.m_ipv4;
}
else
{
return rhs.is_ipv6() && m_ipv6 < rhs.m_ipv6;
}
}
inline bool ip_address::operator>(const ip_address& rhs) const noexcept
{
return rhs < *this;
}
inline bool ip_address::operator<=(const ip_address& rhs) const noexcept
{
return !(rhs < *this);
}
inline bool ip_address::operator>=(const ip_address& rhs) const noexcept
{
return !(*this < rhs);
}
}
}
#endif