2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <boost/operators.hpp>
26 #include <folly/Hash.h>
27 #include <folly/Range.h>
28 #include <folly/detail/IPAddress.h>
38 * Pair of IPAddressV6, netmask
40 typedef std::pair<IPAddressV6, uint8_t> CIDRNetworkV6;
43 * Specialization for IPv6 addresses
45 typedef std::array<uint8_t, 16> ByteArray16;
48 * IPv6 variation of IPAddress.
50 * Added methods: createIPv4, getIPv4For6To4, is6To4,
51 * isTeredo, isIPv4Mapped, tryCreateIPv4, type
55 class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
59 TEREDO, T6TO4, NORMAL,
61 // A constructor parameter to indicate that we should create a link-local
66 // Thrown when a type assertion fails
67 typedef std::runtime_error TypeError;
69 // Binary prefix for teredo networks
70 static const uint32_t PREFIX_TEREDO;
71 // Binary prefix for 6to4 networks
72 static const uint32_t PREFIX_6TO4;
75 * Create a new IPAddress instance from the provided binary data.
76 * @throws IPAddressFormatException if the input length is not 16 bytes.
78 static IPAddressV6 fromBinary(ByteRange bytes) {
80 addr.setFromBinary(bytes);
85 * Default constructor for IPAddressV6.
87 * The address value will be ::0
91 // Create an IPAddressV6 from a string
92 // @throws IPAddressFormatException
93 explicit IPAddressV6(StringPiece ip);
95 // ByteArray16 constructor
96 explicit IPAddressV6(const ByteArray16& src);
98 // in6_addr constructor
99 explicit IPAddressV6(const in6_addr& src);
102 * Create a link-local IPAddressV6 from the specified ethernet MAC address.
104 IPAddressV6(LinkLocalTag tag, MacAddress mac);
106 // return the mapped V4 address
107 // @throws IPAddressFormatException if !isIPv4Mapped
108 IPAddressV4 createIPv4() const;
111 * Return a V4 address if this is a 6To4 address.
112 * @throws TypeError if not a 6To4 address
114 IPAddressV4 getIPv4For6To4() const;
116 // Return true if a 6TO4 address
117 bool is6To4() const {
118 return type() == IPAddressV6::Type::T6TO4;
121 // Return true if a TEREDO address
122 bool isTeredo() const {
123 return type() == IPAddressV6::Type::TEREDO;
126 // return true if this is v4-to-v6-mapped
127 bool isIPv4Mapped() const;
129 // Return the V6 address type
133 * @see IPAddress#bitCount
136 static size_t bitCount() { return 128; }
139 * @see IPAddress#toJson
141 std::string toJson() const;
145 // @see IPAddress#inSubnet
146 // @throws IPAddressFormatException if string doesn't contain a V6 address
147 bool inSubnet(StringPiece cidrNetwork) const;
149 // return true if address is in subnet
150 bool inSubnet(const IPAddressV6& subnet, uint8_t cidr) const {
151 return inSubnetWithMask(subnet, fetchMask(cidr));
153 bool inSubnetWithMask(const IPAddressV6& subnet,
154 const ByteArray16& mask) const;
156 // @see IPAddress#isLoopback
157 bool isLoopback() const;
159 // @see IPAddress#isNonroutable
160 bool isNonroutable() const {
161 return !isRoutable();
165 * Return true if this address is routable.
167 bool isRoutable() const;
169 // @see IPAddress#isPrivate
170 bool isPrivate() const;
173 * Return true if this is a link-local IPv6 address.
175 * Note that this only returns true for addresses in the fe80::/10 range.
176 * It returns false for the loopback address (::1), even though this address
177 * is also effectively has link-local scope. It also returns false for
178 * link-scope and interface-scope multicast addresses.
180 bool isLinkLocal() const;
183 * Return true if this is a multicast address.
185 bool isMulticast() const;
188 * Return the flags for a multicast address.
189 * This method may only be called on multicast addresses.
191 uint8_t getMulticastFlags() const;
194 * Return the scope for a multicast address.
195 * This method may only be called on multicast addresses.
197 uint8_t getMulticastScope() const;
199 // @see IPAddress#isZero
200 bool isZero() const {
201 return detail::Bytes::isZero(bytes(), 16);
204 bool isLinkLocalBroadcast() const;
206 // @see IPAddress#mask
207 IPAddressV6 mask(size_t numBits) const;
209 // return underlying in6_addr structure
210 in6_addr toAddr() const { return addr_.in6Addr_; }
212 sockaddr_in6 toSockAddr() const {
214 memset(&addr, 0, sizeof(sockaddr_in6));
215 addr.sin6_family = AF_INET6;
216 memcpy(&addr.sin6_addr, &addr_.in6Addr_, sizeof(in6_addr));
220 ByteArray16 toByteArray() const {
222 std::memcpy(ba.data(), bytes(), 16);
226 // @see IPAddress#toFullyQualified
227 std::string toFullyQualified() const;
229 // @see IPAddress#str
230 std::string str() const;
232 // @see IPAddress#version
233 size_t version() const { return 6; }
236 * Return the solicited-node multicast address for this address.
238 IPAddressV6 getSolicitedNodeAddress() const;
241 * Return the mask associated with the given number of bits.
242 * If for instance numBits was 24 (e.g. /24) then the V4 mask returned should
243 * be {0xff, 0xff, 0xff, 0x00}.
244 * @param [in] numBits bitmask to retrieve
245 * @throws abort if numBits == 0 or numBits > bitCount()
246 * @return mask associated with numBits
248 static const ByteArray16 fetchMask(size_t numBits);
249 // Given 2 IPAddressV6,mask pairs extract the longest common IPAddress,
251 static CIDRNetworkV6 longestCommonPrefix(const CIDRNetworkV6& one,
252 const CIDRNetworkV6& two) {
253 auto prefix = detail::Bytes::longestCommonPrefix(
254 one.first.addr_.bytes_, one.second,
255 two.first.addr_.bytes_, two.second);
256 return {IPAddressV6(prefix.first), prefix.second};
258 // Number of bytes in the address representation.
259 static constexpr size_t byteCount() { return 16; }
261 //get nth most significant bit - 0 indexed
262 bool getNthMSBit(size_t bitIndex) const {
263 return detail::getNthMSBitImpl(*this, bitIndex, AF_INET6);
265 //get nth most significant byte - 0 indexed
266 uint8_t getNthMSByte(size_t byteIndex) const;
267 //get nth bit - 0 indexed
268 bool getNthLSBit(size_t bitIndex) const {
269 return getNthMSBit(bitCount() - bitIndex - 1);
271 //get nth byte - 0 indexed
272 uint8_t getNthLSByte(size_t byteIndex) const {
273 return getNthMSByte(byteCount() - byteIndex - 1);
276 const unsigned char* bytes() const { return addr_.in6Addr_.s6_addr; }
279 * Helper that returns true if the address is in the binary subnet specified
282 bool inBinarySubnet(const std::array<uint8_t, 2> addr,
283 size_t numBits) const;
286 union AddressStorage {
290 std::memset(this, 0, sizeof(AddressStorage));
292 explicit AddressStorage(const ByteArray16& bytes): bytes_(bytes) {}
293 explicit AddressStorage(const in6_addr& addr): in6Addr_(addr) {}
294 explicit AddressStorage(MacAddress mac);
297 static const std::array<ByteArray16, 129> masks_;
300 * Set the current IPAddressV6 object to have the address specified by bytes.
301 * @throws IPAddressFormatException if bytes.size() is not 16.
303 void setFromBinary(ByteRange bytes);
306 // boost::hash uses hash_value() so this allows boost::hash to work
307 // automatically for IPAddressV6
308 std::size_t hash_value(const IPAddressV6& addr);
309 std::ostream& operator<<(std::ostream& os, const IPAddressV6& addr);
310 // Define toAppend() to allow IPAddressV6 to be used with to<string>
311 void toAppend(IPAddressV6 addr, std::string* result);
312 void toAppend(IPAddressV6 addr, fbstring* result);
315 * Return true if two addresses are equal.
317 inline bool operator==(const IPAddressV6& addr1, const IPAddressV6& addr2) {
318 return (std::memcmp(addr1.toAddr().s6_addr, addr2.toAddr().s6_addr, 16) == 0);
320 // Return true if addr1 < addr2
321 inline bool operator<(const IPAddressV6& addr1, const IPAddressV6& addr2) {
322 return (std::memcmp(addr1.toAddr().s6_addr, addr2.toAddr().s6_addr, 16) < 0);
329 struct hash<folly::IPAddressV6> {
330 size_t operator()(const folly::IPAddressV6& addr) const {