Adding addTaskFuture and addTaskRemoteFuture to FiberManager.
[folly.git] / folly / IPAddressV4.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
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 #include <folly/IPAddressV4.h>
18
19 #include <ostream>
20 #include <string>
21
22 #include <folly/Format.h>
23 #include <folly/IPAddress.h>
24 #include <folly/IPAddressV6.h>
25
26 using std::ostream;
27 using std::string;
28
29 namespace folly {
30
31
32 // free functions
33 size_t hash_value(const IPAddressV4& addr) {
34   return addr.hash();
35 }
36 ostream& operator<<(ostream& os, const IPAddressV4& addr) {
37   os << addr.str();
38   return os;
39 }
40 void toAppend(IPAddressV4 addr, string* result) {
41   result->append(addr.str());
42 }
43 void toAppend(IPAddressV4 addr, fbstring* result) {
44   result->append(addr.str());
45 }
46
47
48 // public static
49 IPAddressV4 IPAddressV4::fromLong(uint32_t src) {
50   in_addr addr;
51   addr.s_addr = src;
52   return IPAddressV4(addr);
53 }
54
55 IPAddressV4 IPAddressV4::fromLongHBO(uint32_t src) {
56   in_addr addr;
57   addr.s_addr = htonl(src);
58   return IPAddressV4(addr);
59 }
60
61 // static public
62 uint32_t IPAddressV4::toLong(StringPiece ip) {
63   auto str = ip.str();
64   in_addr addr;
65   if (inet_pton(AF_INET, str.c_str(), &addr) != 1) {
66     throw IPAddressFormatException("Can't convert invalid IP '", ip, "' ",
67                                    "to long");
68   }
69   return addr.s_addr;
70 }
71
72 // static public
73 uint32_t IPAddressV4::toLongHBO(StringPiece ip) {
74   return ntohl(IPAddressV4::toLong(ip));
75 }
76
77 // public default constructor
78 IPAddressV4::IPAddressV4() {
79 }
80
81 // ByteArray4 constructor
82 IPAddressV4::IPAddressV4(const ByteArray4& src)
83   : addr_(src)
84 {
85 }
86
87 // public string constructor
88 IPAddressV4::IPAddressV4(StringPiece addr)
89   : addr_()
90 {
91   auto ip = addr.str();
92   if (inet_pton(AF_INET, ip.c_str(), &addr_.inAddr_) != 1) {
93     throw IPAddressFormatException("Invalid IPv4 address '", addr, "'");
94   }
95 }
96
97 // in_addr constructor
98 IPAddressV4::IPAddressV4(const in_addr src)
99   : addr_(src)
100 {
101 }
102
103 // public
104 void IPAddressV4::setFromBinary(ByteRange bytes) {
105   if (bytes.size() != 4) {
106     throw IPAddressFormatException("Invalid IPv4 binary data: length must "
107                                    "be 4 bytes, got ", bytes.size());
108   }
109   memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr));
110 }
111
112 // public
113 IPAddressV6 IPAddressV4::createIPv6() const {
114   ByteArray16 ba{};
115   ba[10] = 0xff;
116   ba[11] = 0xff;
117   std::memcpy(&ba[12], bytes(), 4);
118   return IPAddressV6(ba);
119 }
120
121 // public
122 IPAddressV6 IPAddressV4::getIPv6For6To4() const {
123   ByteArray16 ba{};
124   ba[0] = (uint8_t)((IPAddressV6::PREFIX_6TO4 & 0xFF00) >> 8);
125   ba[1] = (uint8_t)(IPAddressV6::PREFIX_6TO4 & 0x00FF);
126   std::memcpy(&ba[2], bytes(), 4);
127   return IPAddressV6(ba);
128 }
129
130 // public
131 string IPAddressV4::toJson() const {
132   return format(
133       "{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash()).str();
134 }
135
136 // public
137 bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const {
138   auto subnetInfo = IPAddress::createNetwork(cidrNetwork);
139   auto addr = subnetInfo.first;
140   if (!addr.isV4()) {
141     throw IPAddressFormatException("Address '", addr.toJson(), "' ",
142                                    "is not a V4 address");
143   }
144   return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second));
145 }
146
147 // public
148 bool IPAddressV4::inSubnetWithMask(const IPAddressV4& subnet,
149                                    const ByteArray4 cidrMask) const {
150   const ByteArray4 mask = detail::Bytes::mask(toByteArray(), cidrMask);
151   const ByteArray4 subMask = detail::Bytes::mask(subnet.toByteArray(),
152                                                  cidrMask);
153   return (mask == subMask);
154 }
155
156 // public
157 bool IPAddressV4::isLoopback() const {
158   static IPAddressV4 loopback_addr("127.0.0.0");
159   return inSubnetWithMask(loopback_addr, fetchMask(8));
160 }
161
162 // public
163 bool IPAddressV4::isLinkLocal() const {
164   static IPAddressV4 linklocal_addr("169.254.0.0");
165   return inSubnetWithMask(linklocal_addr, fetchMask(16));
166 }
167
168 // public
169 bool IPAddressV4::isNonroutable() const {
170   auto ip = toLongHBO();
171   return isPrivate() ||
172       (ip <= 0x00FFFFFF)                     || // 0.0.0.0-0.255.255.255
173       (ip >= 0xC0000000 && ip <= 0xC00000FF) || // 192.0.0.0-192.0.0.255
174       (ip >= 0xC0000200 && ip <= 0xC00002FF) || // 192.0.2.0-192.0.2.255
175       (ip >= 0xC6120000 && ip <= 0xC613FFFF) || // 198.18.0.0-198.19.255.255
176       (ip >= 0xC6336400 && ip <= 0xC63364FF) || // 198.51.100.0-198.51.100.255
177       (ip >= 0xCB007100 && ip <= 0xCB0071FF) || // 203.0.113.0-203.0.113.255
178       (ip >= 0xE0000000 && ip <= 0xFFFFFFFF);   // 224.0.0.0-255.255.255.255
179 }
180
181 // public
182 bool IPAddressV4::isPrivate() const {
183   auto ip = toLongHBO();
184   return
185       (ip >= 0x0A000000 && ip <= 0x0AFFFFFF) || // 10.0.0.0-10.255.255.255
186       (ip >= 0x7F000000 && ip <= 0x7FFFFFFF) || // 127.0.0.0-127.255.255.255
187       (ip >= 0xA9FE0000 && ip <= 0xA9FEFFFF) || // 169.254.0.0-169.254.255.255
188       (ip >= 0xAC100000 && ip <= 0xAC1FFFFF) || // 172.16.0.0-172.31.255.255
189       (ip >= 0xC0A80000 && ip <= 0xC0A8FFFF);   // 192.168.0.0-192.168.255.255
190 }
191
192 // public
193 bool IPAddressV4::isMulticast() const {
194   return (toLongHBO() & 0xf0000000) == 0xe0000000;
195 }
196
197 // public
198 IPAddressV4 IPAddressV4::mask(size_t numBits) const {
199   static const auto bits = bitCount();
200   if (numBits > bits) {
201     throw IPAddressFormatException("numBits(", numBits,
202                                    ") > bitsCount(", bits, ")");
203   }
204
205   ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
206   return IPAddressV4(ba);
207 }
208
209 // public
210 string IPAddressV4::str() const {
211   return detail::fastIpv4ToString(addr_.inAddr_);
212 }
213
214 // public
215 uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
216   const auto highestIndex = byteCount() - 1;
217   if (byteIndex > highestIndex) {
218     throw std::invalid_argument(to<string>("Byte index must be <= ",
219         to<string>(highestIndex), " for addresses of type :",
220         detail::familyNameStr(AF_INET)));
221   }
222   return bytes()[byteIndex];
223 }
224 // protected
225 const ByteArray4 IPAddressV4::fetchMask(size_t numBits) {
226   static const uint8_t bits = bitCount();
227   if (numBits > bits) {
228     throw IPAddressFormatException("IPv4 addresses are 32 bits");
229   }
230   // masks_ is backed by an array so is zero indexed
231   return masks_[numBits];
232 }
233
234 // static private
235 const std::array<ByteArray4, 33> IPAddressV4::masks_ = {{
236   {{0x00, 0x00, 0x00, 0x00}},
237   {{0x80, 0x00, 0x00, 0x00}},
238   {{0xc0, 0x00, 0x00, 0x00}},
239   {{0xe0, 0x00, 0x00, 0x00}},
240   {{0xf0, 0x00, 0x00, 0x00}},
241   {{0xf8, 0x00, 0x00, 0x00}},
242   {{0xfc, 0x00, 0x00, 0x00}},
243   {{0xfe, 0x00, 0x00, 0x00}},
244   {{0xff, 0x00, 0x00, 0x00}},
245   {{0xff, 0x80, 0x00, 0x00}},
246   {{0xff, 0xc0, 0x00, 0x00}},
247   {{0xff, 0xe0, 0x00, 0x00}},
248   {{0xff, 0xf0, 0x00, 0x00}},
249   {{0xff, 0xf8, 0x00, 0x00}},
250   {{0xff, 0xfc, 0x00, 0x00}},
251   {{0xff, 0xfe, 0x00, 0x00}},
252   {{0xff, 0xff, 0x00, 0x00}},
253   {{0xff, 0xff, 0x80, 0x00}},
254   {{0xff, 0xff, 0xc0, 0x00}},
255   {{0xff, 0xff, 0xe0, 0x00}},
256   {{0xff, 0xff, 0xf0, 0x00}},
257   {{0xff, 0xff, 0xf8, 0x00}},
258   {{0xff, 0xff, 0xfc, 0x00}},
259   {{0xff, 0xff, 0xfe, 0x00}},
260   {{0xff, 0xff, 0xff, 0x00}},
261   {{0xff, 0xff, 0xff, 0x80}},
262   {{0xff, 0xff, 0xff, 0xc0}},
263   {{0xff, 0xff, 0xff, 0xe0}},
264   {{0xff, 0xff, 0xff, 0xf0}},
265   {{0xff, 0xff, 0xff, 0xf8}},
266   {{0xff, 0xff, 0xff, 0xfc}},
267   {{0xff, 0xff, 0xff, 0xfe}},
268   {{0xff, 0xff, 0xff, 0xff}}
269 }};
270
271 } // folly