arc: Move platform2/arc/network/ to platform2/patchpanel

Next step in the arc-networkd -> patchpanel rename, this patch moves the
location of the code.

BUG=b:151879931
TEST=units,flashed image to atlas
TEST=tasts arc.PlayStore, crostini.LaunchTerminal.download

Change-Id: I1b5cf8d670e1631d46f6449b725395157bf88dde
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2115863
Tested-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
diff --git a/patchpanel/subnet.h b/patchpanel/subnet.h
new file mode 100644
index 0000000..f8988b9
--- /dev/null
+++ b/patchpanel/subnet.h
@@ -0,0 +1,129 @@
+// Copyright 2018 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PATCHPANEL_SUBNET_H_
+#define PATCHPANEL_SUBNET_H_
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <base/callback.h>
+#include <base/macros.h>
+#include <base/memory/weak_ptr.h>
+#include <brillo/brillo_export.h>
+
+namespace patchpanel {
+
+// Represents an allocated address inside an IPv4 subnet.  The address is freed
+// when this object is destroyed.
+class BRILLO_EXPORT SubnetAddress {
+ public:
+  // Creates a new SubnetAddress with the given base address and prefix length.
+  // |base_addr| must be in network-byte order. |release_cb| runs in the
+  // destructor of this class and can be used to free other resources associated
+  // with the subnet address.
+  SubnetAddress(uint32_t addr,
+                uint32_t prefix_length,
+                base::Closure release_cb);
+  ~SubnetAddress();
+
+  // Returns this address in network-byte order.
+  uint32_t Address() const;
+
+  // Returns the CIDR representation of this address, for instance
+  // 192.168.0.34/24.
+  std::string ToCidrString() const;
+
+  // Returns the IPv4 literal representation of this address, for instance
+  // 192.168.0.34.
+  std::string ToIPv4String() const;
+
+  // Returns the subnet etmask in network-byte order.
+  uint32_t Netmask() const;
+
+ private:
+  // The address in network-byte order.
+  uint32_t addr_;
+  // The prefix length of the address.
+  uint32_t prefix_length_;
+
+  // Callback to run when this object is destroyed.
+  base::Closure release_cb_;
+
+  DISALLOW_COPY_AND_ASSIGN(SubnetAddress);
+};
+
+// Represents an allocated IPv4 subnet.
+class BRILLO_EXPORT Subnet {
+ public:
+  // Creates a new Subnet with the given base address and prefix length.
+  // |base_addr| must be in network-byte order. |release_cb| runs in the
+  // destructor of this class and can be used to free other resources associated
+  // with the subnet.
+  Subnet(uint32_t base_addr, uint32_t prefix_length, base::Closure release_cb);
+  ~Subnet();
+
+  // Marks |addr| as allocated. |addr| must be in network-byte order. Returns
+  // nullptr if |addr| has already been allocated or if |addr| is not contained
+  // within this subnet. Otherwise, the allocated address is automatically
+  // freed when the returned SubnetAddress is destroyed.
+  std::unique_ptr<SubnetAddress> Allocate(uint32_t addr);
+
+  // Allocates the address at |offset|. Returns nullptr if |offset| is invalid
+  // (exceeds available IPs in the subnet) or is already allocated.
+  // |offset| is relative to the first usable host address; e.g. network + 1
+  std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset);
+
+  // Returns the address at the given |offset| in network-byte order. Returns
+  // INADDR_ANY if the offset exceeds the available IPs in the subnet.
+  // Available IPs do not include the network id or the broadcast address.
+  // |offset| is relative to the first usable host address; e.g. network + 1
+  uint32_t AddressAtOffset(uint32_t offset) const;
+
+  // Returns the number of available IPs in this subnet.
+  uint32_t AvailableCount() const;
+
+  // Returns the base address in network-byte order.
+  uint32_t BaseAddress() const;
+
+  // Returns the netmask in network-byte order.
+  uint32_t Netmask() const;
+
+  // Returns the prefix in network-byte order.
+  uint32_t Prefix() const;
+
+  // Returns the prefix length.
+  uint32_t PrefixLength() const;
+
+  // Returns the CIDR representation of this subnet, for instance
+  // 192.168.0.0/24.
+  std::string ToCidrString() const;
+
+ private:
+  // Marks the address at |offset| as free.
+  void Free(uint32_t offset);
+
+  // Base address of the subnet, in network-byte order.
+  uint32_t base_addr_;
+
+  // Prefix length.
+  uint32_t prefix_length_;
+
+  // Keeps track of allocated addresses.
+  std::vector<bool> addrs_;
+
+  // Callback to run when this object is deleted.
+  base::Closure release_cb_;
+
+  base::WeakPtrFactory<Subnet> weak_factory_;
+
+  DISALLOW_COPY_AND_ASSIGN(Subnet);
+};
+
+}  // namespace patchpanel
+
+#endif  // PATCHPANEL_SUBNET_H_