blob: 95c60eb516bc4396281d14e4199a1e860a61ea88 [file] [log] [blame]
Erik Klined26a2c22018-05-11 19:33:19 +09001/*
2 * Copyright (C) 2018 The Android Open Source Project
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
17#ifndef NETDUTILS_INTERNETADDRESSES_H_
18#define NETDUTILS_INTERNETADDRESSES_H_
19
20#include <netinet/in.h>
21#include <stdint.h>
22#include <cstring>
23#include <limits>
24#include <string>
25
26#include "netdutils/NetworkConstants.h"
27
28namespace android {
29namespace netdutils {
30
31namespace internal_ {
32
33// A structure to hold data for dealing with Internet addresses (IPAddress) and
34// related types such as IPSockAddr and IPPrefix.
35struct compact_ipdata {
36 uint8_t family{AF_UNSPEC};
Erik Kline603df202018-09-10 18:16:26 +090037 uint8_t cidrlen{0U}; // written and read in host-byte order
38 in_port_t port{0U}; // written and read in host-byte order
Erik Klined26a2c22018-05-11 19:33:19 +090039 uint32_t scope_id{0U};
40 union {
41 in_addr v4;
42 in6_addr v6;
Erik Kline603df202018-09-10 18:16:26 +090043 } ip{.v6 = IN6ADDR_ANY_INIT}; // written and read in network-byte order
Erik Klined26a2c22018-05-11 19:33:19 +090044
45 // Classes that use compact_ipdata and this method should be sure to clear
46 // (i.e. zero or make uniform) any fields not relevant to the class.
47 friend bool operator==(const compact_ipdata& a, const compact_ipdata& b) {
48 if ((a.family != b.family) || (a.cidrlen != b.cidrlen) || (a.port != b.port) ||
49 (a.scope_id != b.scope_id)) {
50 return false;
51 }
52 switch (a.family) {
Erik Kline603df202018-09-10 18:16:26 +090053 case AF_UNSPEC:
54 // After the above checks, two AF_UNSPEC objects can be
55 // considered equal, for convenience.
56 return true;
Erik Klined26a2c22018-05-11 19:33:19 +090057 case AF_INET: {
Erik Klined26a2c22018-05-11 19:33:19 +090058 const in_addr v4a = a.ip.v4;
59 const in_addr v4b = b.ip.v4;
Erik Kline603df202018-09-10 18:16:26 +090060 return (v4a.s_addr == v4b.s_addr);
Erik Klined26a2c22018-05-11 19:33:19 +090061 }
62 case AF_INET6: {
Erik Klined26a2c22018-05-11 19:33:19 +090063 const in6_addr v6a = a.ip.v6;
64 const in6_addr v6b = b.ip.v6;
Erik Kline603df202018-09-10 18:16:26 +090065 return IN6_ARE_ADDR_EQUAL(&v6a, &v6b);
Erik Klined26a2c22018-05-11 19:33:19 +090066 }
67 }
Erik Kline603df202018-09-10 18:16:26 +090068 return false;
Erik Klined26a2c22018-05-11 19:33:19 +090069 }
70
71 // Classes that use compact_ipdata and this method should be sure to clear
72 // (i.e. zero or make uniform) any fields not relevant to the class.
73 friend bool operator!=(const compact_ipdata& a, const compact_ipdata& b) { return !(a == b); }
74
75 // Classes that use compact_ipdata and this method should be sure to clear
76 // (i.e. zero or make uniform) any fields not relevant to the class.
77 friend bool operator<(const compact_ipdata& a, const compact_ipdata& b) {
78 if (a.family != b.family) return (a.family < b.family);
79 switch (a.family) {
80 case AF_INET: {
Erik Klined26a2c22018-05-11 19:33:19 +090081 const in_addr v4a = a.ip.v4;
82 const in_addr v4b = b.ip.v4;
83 if (v4a.s_addr != v4b.s_addr) return (ntohl(v4a.s_addr) < ntohl(v4b.s_addr));
84 break;
85 }
86 case AF_INET6: {
Erik Klined26a2c22018-05-11 19:33:19 +090087 const in6_addr v6a = a.ip.v6;
88 const in6_addr v6b = b.ip.v6;
Erik Kline603df202018-09-10 18:16:26 +090089 const int cmp = std::memcmp(v6a.s6_addr, v6b.s6_addr, IPV6_ADDR_LEN);
Erik Klined26a2c22018-05-11 19:33:19 +090090 if (cmp != 0) return cmp < 0;
91 break;
92 }
93 }
94 if (a.cidrlen != b.cidrlen) return (a.cidrlen < b.cidrlen);
95 if (a.port != b.port) return (a.port < b.port);
96 return (a.scope_id < b.scope_id);
97 }
98};
99
100static_assert(AF_UNSPEC <= std::numeric_limits<uint8_t>::max(), "AF_UNSPEC value too large");
101static_assert(AF_INET <= std::numeric_limits<uint8_t>::max(), "AF_INET value too large");
102static_assert(AF_INET6 <= std::numeric_limits<uint8_t>::max(), "AF_INET6 value too large");
103static_assert(sizeof(compact_ipdata) == 24U, "compact_ipdata unexpectedly large");
104
105} // namespace internal_
106
Erik Kline603df202018-09-10 18:16:26 +0900107inline bool usesScopedIds(const in6_addr& ipv6) {
Erik Klined26a2c22018-05-11 19:33:19 +0900108 return (IN6_IS_ADDR_LINKLOCAL(&ipv6) || IN6_IS_ADDR_MC_LINKLOCAL(&ipv6));
109}
110
111class IPPrefix;
112class IPSockAddr;
113
114class IPAddress {
115 public:
116 static bool forString(const std::string& repr, IPAddress* ip);
117 static IPAddress forString(const std::string& repr) {
118 IPAddress ip;
119 if (!forString(repr, &ip)) return IPAddress();
120 return ip;
121 }
122
123 IPAddress() = default;
124 IPAddress(const IPAddress&) = default;
125 IPAddress(IPAddress&&) = default;
126
127 explicit IPAddress(const in_addr& ipv4)
128 : mData({AF_INET, IPV4_ADDR_BITS, 0U, 0U, {.v4 = ipv4}}) {}
129 explicit IPAddress(const in6_addr& ipv6)
130 : mData({AF_INET6, IPV6_ADDR_BITS, 0U, 0U, {.v6 = ipv6}}) {}
131 IPAddress(const in6_addr& ipv6, uint32_t scope_id)
132 : mData({AF_INET6,
133 IPV6_ADDR_BITS,
134 0U,
135 // Sanity check: scoped_ids only for link-local addresses.
Erik Kline603df202018-09-10 18:16:26 +0900136 usesScopedIds(ipv6) ? scope_id : 0U,
Erik Klined26a2c22018-05-11 19:33:19 +0900137 {.v6 = ipv6}}) {}
138 IPAddress(const IPAddress& ip, uint32_t scope_id) : IPAddress(ip) {
Erik Kline603df202018-09-10 18:16:26 +0900139 mData.scope_id = (family() == AF_INET6 && usesScopedIds(mData.ip.v6)) ? scope_id : 0U;
Erik Klined26a2c22018-05-11 19:33:19 +0900140 }
141
142 IPAddress& operator=(const IPAddress&) = default;
143 IPAddress& operator=(IPAddress&&) = default;
144
145 constexpr sa_family_t family() const noexcept { return mData.family; }
146 constexpr uint32_t scope_id() const noexcept { return mData.scope_id; }
147
148 std::string toString() const noexcept;
149
150 friend std::ostream& operator<<(std::ostream& os, const IPAddress& ip) {
151 os << ip.toString();
152 return os;
153 }
154 friend bool operator==(const IPAddress& a, const IPAddress& b) { return (a.mData == b.mData); }
155 friend bool operator!=(const IPAddress& a, const IPAddress& b) { return (a.mData != b.mData); }
156 friend bool operator<(const IPAddress& a, const IPAddress& b) { return (a.mData < b.mData); }
157 friend bool operator>(const IPAddress& a, const IPAddress& b) { return (b.mData < a.mData); }
158 friend bool operator<=(const IPAddress& a, const IPAddress& b) { return (a < b) || (a == b); }
159 friend bool operator>=(const IPAddress& a, const IPAddress& b) { return (b < a) || (a == b); }
160
161 private:
162 friend class IPPrefix;
163 friend class IPSockAddr;
164
165 explicit IPAddress(const internal_::compact_ipdata& ipdata) : mData(ipdata) {
166 mData.port = 0U;
167 switch (mData.family) {
168 case AF_INET:
169 mData.cidrlen = IPV4_ADDR_BITS;
170 mData.scope_id = 0U;
171 break;
172 case AF_INET6:
173 mData.cidrlen = IPV6_ADDR_BITS;
Erik Kline603df202018-09-10 18:16:26 +0900174 if (usesScopedIds(ipdata.ip.v6)) mData.scope_id = ipdata.scope_id;
Erik Klined26a2c22018-05-11 19:33:19 +0900175 break;
176 default:
177 mData.cidrlen = 0U;
178 mData.scope_id = 0U;
179 break;
180 }
181 }
182
183 internal_::compact_ipdata mData{};
184};
185
186class IPPrefix {
187 public:
188 // TODO: "static forString(...)" using NetdConstants' parsePrefix().
189
190 IPPrefix() = default;
191 IPPrefix(const IPPrefix&) = default;
192 IPPrefix(IPPrefix&&) = default;
193
194 explicit IPPrefix(const IPAddress& ip) : mData(ip.mData) {}
195
196 // Truncate the IP address |ip| at length |length|. Lengths greater than
197 // the address-family-relevant maximum, along with negative values, are
198 // interpreted as if the address-family-relevant maximum had been given.
199 IPPrefix(const IPAddress& ip, int length);
200
201 IPPrefix& operator=(const IPPrefix&) = default;
202 IPPrefix& operator=(IPPrefix&&) = default;
203
204 constexpr sa_family_t family() const noexcept { return mData.family; }
205 IPAddress ip() const noexcept { return IPAddress(mData); }
206 constexpr int length() const noexcept { return mData.cidrlen; }
207
nuccachenf52f7a52018-07-17 18:07:23 +0800208 bool isUninitialized() const noexcept;
Erik Klined26a2c22018-05-11 19:33:19 +0900209 std::string toString() const noexcept;
210
211 friend std::ostream& operator<<(std::ostream& os, const IPPrefix& prefix) {
212 os << prefix.toString();
213 return os;
214 }
215 friend bool operator==(const IPPrefix& a, const IPPrefix& b) { return (a.mData == b.mData); }
216 friend bool operator!=(const IPPrefix& a, const IPPrefix& b) { return (a.mData != b.mData); }
217 friend bool operator<(const IPPrefix& a, const IPPrefix& b) { return (a.mData < b.mData); }
218 friend bool operator>(const IPPrefix& a, const IPPrefix& b) { return (b.mData < a.mData); }
219 friend bool operator<=(const IPPrefix& a, const IPPrefix& b) { return (a < b) || (a == b); }
220 friend bool operator>=(const IPPrefix& a, const IPPrefix& b) { return (b < a) || (a == b); }
221
222 private:
223 internal_::compact_ipdata mData{};
224};
225
226// An Internet socket address.
227//
228// Cannot represent other types of socket addresses (e.g. UNIX socket address, et cetera).
229class IPSockAddr {
230 public:
231 // TODO: static forString
232
233 IPSockAddr() = default;
234 IPSockAddr(const IPSockAddr&) = default;
235 IPSockAddr(IPSockAddr&&) = default;
236
237 explicit IPSockAddr(const IPAddress& ip) : mData(ip.mData) {}
238 IPSockAddr(const IPAddress& ip, in_port_t port) : mData(ip.mData) { mData.port = port; }
239 explicit IPSockAddr(const sockaddr_in& ipv4sa)
240 : IPSockAddr(IPAddress(ipv4sa.sin_addr), ntohs(ipv4sa.sin_port)) {}
241 explicit IPSockAddr(const sockaddr_in6& ipv6sa)
242 : IPSockAddr(IPAddress(ipv6sa.sin6_addr, ipv6sa.sin6_scope_id), ntohs(ipv6sa.sin6_port)) {}
243
244 IPSockAddr& operator=(const IPSockAddr&) = default;
245 IPSockAddr& operator=(IPSockAddr&&) = default;
246
247 constexpr sa_family_t family() const noexcept { return mData.family; }
248 IPAddress ip() const noexcept { return IPAddress(mData); }
249 constexpr in_port_t port() const noexcept { return mData.port; }
250
251 // Implicit conversion to sockaddr_storage.
252 operator sockaddr_storage() const noexcept {
253 sockaddr_storage ss;
254 ss.ss_family = mData.family;
255 switch (mData.family) {
256 case AF_INET:
257 reinterpret_cast<sockaddr_in*>(&ss)->sin_addr = mData.ip.v4;
258 reinterpret_cast<sockaddr_in*>(&ss)->sin_port = htons(mData.port);
259 break;
260 case AF_INET6:
261 reinterpret_cast<sockaddr_in6*>(&ss)->sin6_addr = mData.ip.v6;
262 reinterpret_cast<sockaddr_in6*>(&ss)->sin6_port = htons(mData.port);
263 reinterpret_cast<sockaddr_in6*>(&ss)->sin6_scope_id = mData.scope_id;
264 break;
265 }
266 return ss;
267 }
268
269 std::string toString() const noexcept;
270
271 friend std::ostream& operator<<(std::ostream& os, const IPSockAddr& prefix) {
272 os << prefix.toString();
273 return os;
274 }
275 friend bool operator==(const IPSockAddr& a, const IPSockAddr& b) {
276 return (a.mData == b.mData);
277 }
278 friend bool operator!=(const IPSockAddr& a, const IPSockAddr& b) {
279 return (a.mData != b.mData);
280 }
281 friend bool operator<(const IPSockAddr& a, const IPSockAddr& b) { return (a.mData < b.mData); }
282 friend bool operator>(const IPSockAddr& a, const IPSockAddr& b) { return (b.mData < a.mData); }
283 friend bool operator<=(const IPSockAddr& a, const IPSockAddr& b) { return (a < b) || (a == b); }
284 friend bool operator>=(const IPSockAddr& a, const IPSockAddr& b) { return (b < a) || (a == b); }
285
286 private:
287 internal_::compact_ipdata mData{};
288};
289
290} // namespace netdutils
291} // namespace android
292
293#endif // NETDUTILS_INTERNETADDRESSES_H_