Line data Source code
1 : // Copyright (c) 2012-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libaddr
4 : //
5 : // Permission is hereby granted, free of charge, to any person obtaining a
6 : // copy of this software and associated documentation files (the
7 : // "Software"), to deal in the Software without restriction, including
8 : // without limitation the rights to use, copy, modify, merge, publish,
9 : // distribute, sublicense, and/or sell copies of the Software, and to
10 : // permit persons to whom the Software is furnished to do so, subject to
11 : // the following conditions:
12 : //
13 : // The above copyright notice and this permission notice shall be included
14 : // in all copies or substantial portions of the Software.
15 : //
16 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 : // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 : // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 : // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 : // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 : // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 : #pragma once
24 :
25 : /** \file
26 : * \brief The various libaddr classes.
27 : *
28 : * This header includes the base addr class used to handle one binary
29 : * address.
30 : */
31 :
32 : // C++ library
33 : //
34 : #include <cstring>
35 : #include <memory>
36 : #include <string>
37 : #include <vector>
38 :
39 :
40 : // C library
41 : //
42 : #include <arpa/inet.h>
43 :
44 :
45 :
46 : namespace addr
47 : {
48 :
49 :
50 :
51 :
52 : /** \brief Initialize an IPv6 address as such.
53 : *
54 : * This function initializes a sockaddr_in6 with all zeroes except
55 : * for the sin6_family which is set to AF_INET6.
56 : *
57 : * return The initialized IPv6 address.
58 : */
59 397119 : constexpr sockaddr_in6 init_in6()
60 : {
61 397119 : sockaddr_in6 in6 = sockaddr_in6();
62 397119 : in6.sin6_family = AF_INET6;
63 397119 : return in6;
64 : }
65 :
66 :
67 : /** \brief Result of a compare between IP addresses.
68 : *
69 : * This enumeration includes compare results between IP addresses.
70 : *
71 : * The results can also be used by the addr_range class which explains
72 : * the range like results.
73 : */
74 : enum class compare_t
75 : {
76 : COMPARE_EQUAL, // lhs == rhs
77 : COMPARE_SMALLER, // lhs < rhs
78 : COMPARE_LARGER, // lhs > rhs
79 : COMPARE_OVERLAP_SMALL_VS_LARGE, // lhs is before rhs with an overlap
80 : COMPARE_OVERLAP_LARGE_VS_SMALL, // rhs is before lhs with an overlap
81 : COMPARE_INCLUDED, // rhs is included in lhs
82 : COMPARE_INCLUDES, // lhs is included in rhs
83 : COMPARE_IPV4_VS_IPV6, // lhs is an IPv4, rhs an IPv6
84 : COMPARE_IPV6_VS_IPV4, // lhs is an IPv6, rhs an IPv4
85 : COMPARE_SMALL_VS_LARGE, // lhs is smaller than rhs
86 : COMPARE_LARGE_VS_SMALL, // lhs is larger than rhs
87 : COMPARE_FOLLOWS, // rhs is just after lhs
88 : COMPARE_FOLLOWING, // lhs is just after rhs
89 : COMPARE_FIRST, // lhs is defined, rhs is empty
90 : COMPARE_LAST, // lhs is empty, rhs is defined
91 : COMPARE_UNORDERED, // lhs and rhs are both empty
92 : };
93 :
94 :
95 1326134 : class addr
96 : {
97 : public:
98 : enum class network_type_t
99 : {
100 : NETWORK_TYPE_UNDEFINED,
101 : NETWORK_TYPE_PRIVATE,
102 : NETWORK_TYPE_CARRIER,
103 : NETWORK_TYPE_LINK_LOCAL,
104 : NETWORK_TYPE_MULTICAST,
105 : NETWORK_TYPE_LOOPBACK,
106 : NETWORK_TYPE_ANY,
107 : NETWORK_TYPE_UNKNOWN,
108 : NETWORK_TYPE_PUBLIC = NETWORK_TYPE_UNKNOWN // we currently do not distinguish public and unknown
109 : };
110 :
111 : enum class string_ip_t
112 : {
113 : STRING_IP_ONLY,
114 : STRING_IP_BRACKETS, // IPv6 only
115 : STRING_IP_PORT, // in IPv6, includes brackets
116 : STRING_IP_MASK,
117 : STRING_IP_BRACKETS_MASK, // IPv6 only
118 : STRING_IP_ALL
119 : };
120 :
121 : typedef std::shared_ptr<addr> pointer_t;
122 : typedef std::vector<addr> vector_t;
123 : typedef int socket_flag_t;
124 :
125 : static socket_flag_t const SOCKET_FLAG_CLOEXEC = 0x01;
126 : static socket_flag_t const SOCKET_FLAG_NONBLOCK = 0x02;
127 : static socket_flag_t const SOCKET_FLAG_REUSE = 0x04;
128 :
129 : addr();
130 : addr(sockaddr_in const & in);
131 : addr(sockaddr_in6 const & in6);
132 :
133 : void set_hostname(std::string const & hostname);
134 : void set_from_socket(int s, bool peer);
135 : void set_ipv4(sockaddr_in const & in);
136 : void set_ipv6(sockaddr_in6 const & in6);
137 : void set_port(int port);
138 : void set_protocol(char const * protocol);
139 : void set_protocol(int protocol);
140 : void set_mask(uint8_t const * mask);
141 : void apply_mask();
142 :
143 : std::string get_hostname() const;
144 : bool is_hostname_an_ip() const;
145 : int get_family() const;
146 : bool is_default() const;
147 : bool is_ipv4() const;
148 : void get_ipv4(sockaddr_in & in) const;
149 : void get_ipv6(sockaddr_in6 & in6) const;
150 : std::string to_ipv4_string(string_ip_t mode) const;
151 : std::string to_ipv6_string(string_ip_t mode) const;
152 : std::string to_ipv4or6_string(string_ip_t mode) const;
153 : #pragma GCC diagnostic push
154 : #pragma GCC diagnostic ignored "-Wpedantic"
155 : unsigned __int128 ip_to_uint128() const;
156 : #pragma GCC diagnostic pop
157 :
158 : network_type_t get_network_type() const;
159 : std::string get_network_type_string() const;
160 :
161 : int create_socket(socket_flag_t flags) const;
162 : int connect(int s) const;
163 : int bind(int s) const;
164 : std::string get_name() const;
165 : std::string get_service() const;
166 : int get_port() const;
167 : int get_protocol() const;
168 : void get_mask(uint8_t * mask) const;
169 : int get_mask_size() const;
170 :
171 : bool match(addr const & ip, bool any = false) const;
172 : bool is_next(addr const & a) const;
173 : bool is_previous(addr const & a) const;
174 : bool operator == (addr const & rhs) const;
175 : bool operator != (addr const & rhs) const;
176 : bool operator < (addr const & rhs) const;
177 : bool operator <= (addr const & rhs) const;
178 : bool operator > (addr const & rhs) const;
179 : bool operator >= (addr const & rhs) const;
180 :
181 : private:
182 : void address_changed();
183 :
184 : // always keep address in an IPv6 structure
185 : //
186 : sockaddr_in6 f_address = init_in6();
187 : uint8_t f_mask[16] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
188 : int f_protocol = IPPROTO_TCP;
189 : mutable network_type_t f_private_network = network_type_t::NETWORK_TYPE_UNDEFINED;
190 : std::string f_hostname = std::string();
191 : };
192 :
193 :
194 :
195 :
196 :
197 : }
198 : // namespace addr
199 :
200 :
201 : inline bool operator == (sockaddr_in6 const & a, sockaddr_in6 const & b)
202 : {
203 : return memcmp(&a, &b, sizeof(sockaddr_in6)) == 0;
204 : }
205 :
206 :
207 : inline bool operator != (sockaddr_in6 const & a, sockaddr_in6 const & b)
208 : {
209 : return memcmp(&a, &b, sizeof(sockaddr_in6)) != 0;
210 : }
211 :
212 :
213 : inline bool operator < (sockaddr_in6 const & a, sockaddr_in6 const & b)
214 : {
215 : return memcmp(&a, &b, sizeof(sockaddr_in6)) < 0;
216 : }
217 :
218 :
219 : inline bool operator <= (sockaddr_in6 const & a, sockaddr_in6 const & b)
220 : {
221 : return memcmp(&a, &b, sizeof(sockaddr_in6)) <= 0;
222 : }
223 :
224 :
225 : inline bool operator > (sockaddr_in6 const & a, sockaddr_in6 const & b)
226 : {
227 : return memcmp(&a, &b, sizeof(sockaddr_in6)) > 0;
228 : }
229 :
230 :
231 : inline bool operator >= (sockaddr_in6 const & a, sockaddr_in6 const & b)
232 : {
233 : return memcmp(&a, &b, sizeof(sockaddr_in6)) >= 0;
234 : }
235 :
236 :
237 35 : inline bool operator == (in6_addr const & a, in6_addr const & b)
238 : {
239 35 : return memcmp(&a, &b, sizeof(in6_addr)) == 0;
240 : }
241 :
242 :
243 9 : inline bool operator != (in6_addr const & a, in6_addr const & b)
244 : {
245 9 : return memcmp(&a, &b, sizeof(in6_addr)) != 0;
246 : }
247 :
248 :
249 7 : inline bool operator < (in6_addr const & a, in6_addr const & b)
250 : {
251 7 : return memcmp(&a, &b, sizeof(in6_addr)) < 0;
252 : }
253 :
254 :
255 685 : inline bool operator <= (in6_addr const & a, in6_addr const & b)
256 : {
257 685 : return memcmp(&a, &b, sizeof(in6_addr)) <= 0;
258 : }
259 :
260 :
261 27 : inline bool operator > (in6_addr const & a, in6_addr const & b)
262 : {
263 27 : return memcmp(&a, &b, sizeof(in6_addr)) > 0;
264 : }
265 :
266 :
267 290 : inline bool operator >= (in6_addr const & a, in6_addr const & b)
268 : {
269 290 : return memcmp(&a, &b, sizeof(in6_addr)) >= 0;
270 : }
271 :
272 :
273 : // vim: ts=4 sw=4 et
|