Fixed documentation in folly/Random.h
[folly.git] / folly / IPAddressV4.cpp
index 7a86b9a2201cc0c23f3843bb1f52f382c6267bf7..dfe46695085bc82d6ecf340d2ec34053d32d079d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -122,6 +122,23 @@ void IPAddressV4::setFromBinary(ByteRange bytes) {
   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{};
@@ -224,6 +241,16 @@ string IPAddressV4::str() const {
   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;