• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
24 namespace cuttlefish {
25 
26 enum class ResourceType {
27   Invalid = 0,
28   MobileIface,
29   EthernetIface,
30   EthernetBridge,
31 };
32 
33 class StaticResource {
34  public:
35   StaticResource() = default;
StaticResource(const std::string & name,uid_t uid,ResourceType ty,uint32_t global_id)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 
GetName()43   std::string GetName() { return name_; }
GetUid()44   uid_t GetUid() { return uid_; }
GetResourceType()45   ResourceType GetResourceType() { return ty_; }
GetGlobalID()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 
55 class MobileIface : public StaticResource {
56  public:
57   MobileIface() = default;
58   ~MobileIface() = default;
MobileIface(const std::string & name,uid_t uid,uint16_t iface_id,uint32_t global_id,std::string ipaddr)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 
GetIfaceId()68   uint16_t GetIfaceId() { return iface_id_; }
GetIpAddr()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_;
76 };
77 
78 class EthernetIface : public StaticResource {
79  public:
80   EthernetIface() = default;
81   ~EthernetIface() = default;
82 
EthernetIface(const std::string & name,uid_t uid,uint16_t iface_id,uint32_t global_id,std::string bridge_name,std::string ipaddr)83   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)
86       : StaticResource(name, uid, ResourceType::MobileIface, global_id),
87         iface_id_(iface_id),
88         bridge_name_(bridge_name),
89         ipaddr_(ipaddr) {}
90 
91   bool ReleaseResource() override;
92   bool AcquireResource() override;
93 
GetIfaceId()94   uint16_t GetIfaceId() { return iface_id_; }
95 
GetBridgeName()96   std::string GetBridgeName() { return bridge_name_; }
GetIpAddr()97   std::string GetIpAddr() { return ipaddr_; }
98 
SetHasIpv4(bool ipv4)99   void SetHasIpv4(bool ipv4) { has_ipv4_ = ipv4; }
SetHasIpv6(bool ipv6)100   void SetHasIpv6(bool ipv6) { has_ipv6_ = ipv6; }
SetUseEbtablesLegacy(bool use_legacy)101   void SetUseEbtablesLegacy(bool use_legacy) {
102     use_ebtables_legacy_ = use_legacy;
103   }
104 
GetHasIpv4()105   bool GetHasIpv4() { return has_ipv4_; }
GetHasIpv6()106   bool GetHasIpv6() { return has_ipv6_; }
GetUseEbtablesLegacy()107   bool GetUseEbtablesLegacy() { return use_ebtables_legacy_; }
108 
109  private:
110   static constexpr char kNetmask[] = "/24";
111   uint16_t iface_id_;
112   std::string bridge_name_;
113   std::string ipaddr_;
114   bool has_ipv4_ = true;
115   bool has_ipv6_ = true;
116   bool use_ebtables_legacy_ = false;
117 };
118 
119 }  // namespace cuttlefish
120