/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr));
}
+// static
+IPAddressV4 IPAddressV4::fromInverseArpaName(const std::string& arpaname) {
+ auto piece = StringPiece(arpaname);
+ // input must be something like 1.0.168.192.in-addr.arpa
+ if (!piece.removeSuffix(".in-addr.arpa")) {
+ throw IPAddressFormatException(
+ sformat("input does not end with '.in-addr.arpa': '{}'", arpaname));
+ }
+ std::vector<StringPiece> pieces;
+ split(".", piece, pieces);
+ if (pieces.size() != 4) {
+ throw IPAddressFormatException(sformat("Invalid input. Got {}", piece));
+ }
+ // reverse 1.0.168.192 -> 192.168.0.1
+ return IPAddressV4(join(".", pieces.rbegin(), pieces.rend()));
+}
+
// public
IPAddressV6 IPAddressV4::createIPv6() const {
ByteArray16 ba{};
return detail::fastIpv4ToString(addr_.inAddr_);
}
+// public
+string IPAddressV4::toInverseArpaName() const {
+ return sformat(
+ "{}.{}.{}.{}.in-addr.arpa",
+ addr_.bytes_[3],
+ addr_.bytes_[2],
+ addr_.bytes_[1],
+ addr_.bytes_[0]);
+}
+
// public
uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1;