blob: c30e9cc5ca344e79df552006d47e665cccc30a05 [file] [log] [blame]
Paul Kirthfa048be2020-07-14 03:51:38 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
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#pragma once
18
19#include <sys/types.h>
20
21#include <cstdint>
22#include <string>
23
24namespace cuttlefish {
25
26enum class ResourceType {
27 Invalid = 0,
28 MobileIface,
Alistair Delva57ccc652020-11-10 10:08:45 -080029 EthernetIface,
30 EthernetBridge,
Paul Kirthfa048be2020-07-14 03:51:38 +000031};
32
33class StaticResource {
34 public:
35 StaticResource() = default;
36 StaticResource(const std::string& name, uid_t uid, ResourceType ty,
37 uint32_t global_id)
38 : name_(name), uid_(uid), global_id_(global_id), ty_(ty){};
39 virtual ~StaticResource() = default;
40 virtual bool ReleaseResource() = 0;
41 virtual bool AcquireResource() = 0;
42
43 std::string GetName() { return name_; }
44 uid_t GetUid() { return uid_; }
45 ResourceType GetResourceType() { return ty_; }
46 uint32_t GetGlobalID() { return global_id_; }
47
48 private:
49 std::string name_{};
50 uid_t uid_{};
51 uint32_t global_id_{};
52 ResourceType ty_ = ResourceType::Invalid;
53};
54
55class MobileIface : public StaticResource {
56 public:
57 MobileIface() = default;
58 ~MobileIface() = default;
59 MobileIface(const std::string& name, uid_t uid, uint16_t iface_id,
60 uint32_t global_id, std::string ipaddr)
61 : StaticResource(name, uid, ResourceType::MobileIface, global_id),
62 iface_id_(iface_id),
63 ipaddr_(ipaddr) {}
64
65 bool ReleaseResource() override;
66 bool AcquireResource() override;
67
68 uint16_t GetIfaceId() { return iface_id_; }
69 std::string GetIpAddr() { return ipaddr_; }
70
71 static constexpr char kNetmask[] = "/30";
72
73 private:
74 uint16_t iface_id_;
75 std::string ipaddr_;
Paul Kirthfa048be2020-07-14 03:51:38 +000076};
77
Alistair Delva57ccc652020-11-10 10:08:45 -080078class EthernetIface : public StaticResource {
Paul Kirthfa048be2020-07-14 03:51:38 +000079 public:
Alistair Delva57ccc652020-11-10 10:08:45 -080080 EthernetIface() = default;
81 ~EthernetIface() = default;
Paul Kirthfa048be2020-07-14 03:51:38 +000082
Alistair Delva57ccc652020-11-10 10:08:45 -080083 EthernetIface(const std::string& name, uid_t uid, uint16_t iface_id,
84 uint32_t global_id, std::string bridge_name,
85 std::string ipaddr)
Paul Kirthfa048be2020-07-14 03:51:38 +000086 : StaticResource(name, uid, ResourceType::MobileIface, global_id),
87 iface_id_(iface_id),
Alistair Delva57ccc652020-11-10 10:08:45 -080088 bridge_name_(bridge_name),
Paul Kirthfa048be2020-07-14 03:51:38 +000089 ipaddr_(ipaddr) {}
90
91 bool ReleaseResource() override;
92 bool AcquireResource() override;
93
94 uint16_t GetIfaceId() { return iface_id_; }
Alistair Delva57ccc652020-11-10 10:08:45 -080095
96 std::string GetBridgeName() { return bridge_name_; }
Paul Kirthfa048be2020-07-14 03:51:38 +000097 std::string GetIpAddr() { return ipaddr_; }
98
Paul Kirthda777192020-07-17 19:41:22 +000099 void SetHasIpv4(bool ipv4) { has_ipv4_ = ipv4; }
100 void SetHasIpv6(bool ipv6) { has_ipv6_ = ipv6; }
101 void SetUseEbtablesLegacy(bool use_legacy) {
102 use_ebtables_legacy_ = use_legacy;
103 }
104
105 bool GetHasIpv4() { return has_ipv4_; }
106 bool GetHasIpv6() { return has_ipv6_; }
107 bool GetUseEbtablesLegacy() { return use_ebtables_legacy_; }
108
Paul Kirthfa048be2020-07-14 03:51:38 +0000109 private:
110 static constexpr char kNetmask[] = "/24";
111 uint16_t iface_id_;
Alistair Delva57ccc652020-11-10 10:08:45 -0800112 std::string bridge_name_;
Paul Kirthfa048be2020-07-14 03:51:38 +0000113 std::string ipaddr_;
114 bool has_ipv4_ = true;
115 bool has_ipv6_ = true;
Paul Kirthda777192020-07-17 19:41:22 +0000116 bool use_ebtables_legacy_ = false;
Paul Kirthfa048be2020-07-14 03:51:38 +0000117};
118
119} // namespace cuttlefish