File: resource.h

package info (click to toggle)
android-cuttlefish 1.0.1-0~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,192 kB
  • sloc: cpp: 39,149; sh: 2,523; javascript: 242; exp: 152; python: 125; makefile: 88
file content (119 lines) | stat: -rw-r--r-- 3,346 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sys/types.h>

#include <cstdint>
#include <string>

namespace cuttlefish {

enum class ResourceType {
  Invalid = 0,
  MobileIface,
  EthernetIface,
  EthernetBridge,
};

class StaticResource {
 public:
  StaticResource() = default;
  StaticResource(const std::string& name, uid_t uid, ResourceType ty,
                 uint32_t global_id)
      : name_(name), uid_(uid), global_id_(global_id), ty_(ty){};
  virtual ~StaticResource() = default;
  virtual bool ReleaseResource() = 0;
  virtual bool AcquireResource() = 0;

  std::string GetName() { return name_; }
  uid_t GetUid() { return uid_; }
  ResourceType GetResourceType() { return ty_; }
  uint32_t GetGlobalID() { return global_id_; }

 private:
  std::string name_{};
  uid_t uid_{};
  uint32_t global_id_{};
  ResourceType ty_ = ResourceType::Invalid;
};

class MobileIface : public StaticResource {
 public:
  MobileIface() = default;
  ~MobileIface() = default;
  MobileIface(const std::string& name, uid_t uid, uint16_t iface_id,
              uint32_t global_id, std::string ipaddr)
      : StaticResource(name, uid, ResourceType::MobileIface, global_id),
        iface_id_(iface_id),
        ipaddr_(ipaddr) {}

  bool ReleaseResource() override;
  bool AcquireResource() override;

  uint16_t GetIfaceId() { return iface_id_; }
  std::string GetIpAddr() { return ipaddr_; }

  static constexpr char kNetmask[] = "/30";

 private:
  uint16_t iface_id_;
  std::string ipaddr_;
};

class EthernetIface : public StaticResource {
 public:
  EthernetIface() = default;
  ~EthernetIface() = default;

  EthernetIface(const std::string& name, uid_t uid, uint16_t iface_id,
                uint32_t global_id, std::string bridge_name,
                std::string ipaddr)
      : StaticResource(name, uid, ResourceType::MobileIface, global_id),
        iface_id_(iface_id),
        bridge_name_(bridge_name),
        ipaddr_(ipaddr) {}

  bool ReleaseResource() override;
  bool AcquireResource() override;

  uint16_t GetIfaceId() { return iface_id_; }

  std::string GetBridgeName() { return bridge_name_; }
  std::string GetIpAddr() { return ipaddr_; }

  void SetHasIpv4(bool ipv4) { has_ipv4_ = ipv4; }
  void SetHasIpv6(bool ipv6) { has_ipv6_ = ipv6; }
  void SetUseEbtablesLegacy(bool use_legacy) {
    use_ebtables_legacy_ = use_legacy;
  }

  bool GetHasIpv4() { return has_ipv4_; }
  bool GetHasIpv6() { return has_ipv6_; }
  bool GetUseEbtablesLegacy() { return use_ebtables_legacy_; }

 private:
  static constexpr char kNetmask[] = "/24";
  uint16_t iface_id_;
  std::string bridge_name_;
  std::string ipaddr_;
  bool has_ipv4_ = true;
  bool has_ipv6_ = true;
  bool use_ebtables_legacy_ = false;
};

}  // namespace cuttlefish