blob: 6e6fba798ba4b3282f290bc5bbfad199dd944c95 [file] [log] [blame]
Chirantan Ekbotebccb4752018-10-31 13:53:08 -07001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Garrick Evans4b66f632019-01-24 15:09:16 +09005#ifndef ARC_NETWORK_SUBNET_H_
6#define ARC_NETWORK_SUBNET_H_
Chirantan Ekbotebccb4752018-10-31 13:53:08 -07007
8#include <stdint.h>
9
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080010#include <memory>
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090011#include <string>
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080012#include <vector>
13
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070014#include <base/callback.h>
15#include <base/macros.h>
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080016#include <base/memory/weak_ptr.h>
Garrick Evans4b66f632019-01-24 15:09:16 +090017#include <brillo/brillo_export.h>
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070018
Garrick Evans4b66f632019-01-24 15:09:16 +090019namespace arc_networkd {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070020
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090021// Represents an allocated address inside an IPv4 subnet. The address is freed
22// when this object is destroyed.
Garrick Evans4b66f632019-01-24 15:09:16 +090023class BRILLO_EXPORT SubnetAddress {
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080024 public:
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090025 SubnetAddress(uint32_t addr,
26 uint32_t prefix_length,
27 base::Closure release_cb);
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080028 ~SubnetAddress();
29
30 // Returns this address in network-byte order.
31 uint32_t Address() const;
32
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090033 // Returns the CIDR representation of this address, for instance
34 // 192.168.0.34/24.
35 std::string ToCidrString() const;
36
37 // Returns the IPv4 literal representation of this address, for instance
38 // 192.168.0.34.
39 std::string ToIPv4String() const;
40
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080041 private:
42 // The address in host-byte order.
43 uint32_t addr_;
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090044 // The prefix length of the address.
45 uint32_t prefix_length_;
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080046
47 // Callback to run when this object is destroyed.
48 base::Closure release_cb_;
49
50 DISALLOW_COPY_AND_ASSIGN(SubnetAddress);
51};
52
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090053// Represents an allocated IPv4 subnet.
Garrick Evans4b66f632019-01-24 15:09:16 +090054class BRILLO_EXPORT Subnet {
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070055 public:
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090056 // Creates a new Subnet with the given base address and prefix length.
57 // |base_addr| must be in host-byte order. |release_cb| runs in the destructor
58 // of this class and can be used to free other resources associated with the
59 // subnet.
60 Subnet(uint32_t base_addr, uint32_t prefix_length, base::Closure release_cb);
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070061 ~Subnet();
62
Garrick Evans0dbd4182019-03-07 08:38:38 +090063 // Marks |addr| as allocated. |addr| must be in host-byte order. Returns
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080064 // nullptr if |addr| has already been allocated or if |addr| is not contained
Garrick Evans0dbd4182019-03-07 08:38:38 +090065 // within this subnet. Otherwise, the allocated address is automatically
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080066 // freed when the returned SubnetAddress is destroyed.
67 std::unique_ptr<SubnetAddress> Allocate(uint32_t addr);
68
Garrick Evans0dbd4182019-03-07 08:38:38 +090069 // Allocates the address at |offset|. Returns nullptr if |offset| is invalid
70 // (exceeds available IPs in the subnet) or is already allocated.
Garrick Evansf4a93292019-03-13 14:19:43 +090071 // |offset| is relative to the first usable host address; e.g. network + 1
Garrick Evans0dbd4182019-03-07 08:38:38 +090072 std::unique_ptr<SubnetAddress> AllocateAtOffset(uint32_t offset);
73
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070074 // Returns the address at the given |offset| in network byte order. Returns
75 // INADDR_ANY if the offset exceeds the available IPs in the subnet.
76 // Available IPs do not include the network id or the broadcast address.
Garrick Evansf4a93292019-03-13 14:19:43 +090077 // |offset| is relative to the first usable host address; e.g. network + 1
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070078 uint32_t AddressAtOffset(uint32_t offset) const;
79
80 // Returns the number of available IPs in this subnet.
Garrick Evans0dbd4182019-03-07 08:38:38 +090081 uint32_t AvailableCount() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070082
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080083 // Returns the netmask in network-byte order.
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070084 uint32_t Netmask() const;
85
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090086 // Returns the prefix in network-byte order.
Garrick Evans0dbd4182019-03-07 08:38:38 +090087 uint32_t Prefix() const;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070088
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090089 // Returns the prefix length.
90 uint32_t PrefixLength() const;
91
92 // Returns the CIDR representation of this subnet, for instance
93 // 192.168.0.0/24.
94 std::string ToCidrString() const;
95
Chirantan Ekbotebccb4752018-10-31 13:53:08 -070096 private:
Chirantan Ekbote817b0c22018-11-14 16:55:10 -080097 // Marks the address at |offset| as free.
98 void Free(uint32_t offset);
99
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900100 // Base address of the subnet, in host byte order.
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700101 uint32_t network_id_;
102
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900103 // Prefix length.
104 uint32_t prefix_length_;
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700105
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800106 // Keeps track of allocated addresses.
107 std::vector<bool> addrs_;
108
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700109 // Callback to run when this object is deleted.
110 base::Closure release_cb_;
111
Chirantan Ekbote817b0c22018-11-14 16:55:10 -0800112 base::WeakPtrFactory<Subnet> weak_factory_;
113
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700114 DISALLOW_COPY_AND_ASSIGN(Subnet);
115};
116
Hugo Benichibd8ec4d2019-05-28 12:52:49 +0900117// Returns the literal representation of the IPv4 address given in network byte
118// order.
119std::string IPv4AddressToString(uint32_t addr);
120
121// Returns the CIDR representation of an IPv4 address given in network byte
122// order.
123std::string IPv4AddressToCidrString(uint32_t addr, uint32_t prefix_length);
124
Garrick Evans4b66f632019-01-24 15:09:16 +0900125} // namespace arc_networkd
Chirantan Ekbotebccb4752018-10-31 13:53:08 -0700126
Garrick Evans4b66f632019-01-24 15:09:16 +0900127#endif // ARC_NETWORK_SUBNET_H_