Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 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, |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 29 | EthernetIface, |
| 30 | EthernetBridge, |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | class 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 | |
| 55 | class 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 Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 78 | class EthernetIface : public StaticResource { |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 79 | public: |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 80 | EthernetIface() = default; |
| 81 | ~EthernetIface() = default; |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 82 | |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 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) |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 86 | : StaticResource(name, uid, ResourceType::MobileIface, global_id), |
| 87 | iface_id_(iface_id), |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 88 | bridge_name_(bridge_name), |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 89 | ipaddr_(ipaddr) {} |
| 90 | |
| 91 | bool ReleaseResource() override; |
| 92 | bool AcquireResource() override; |
| 93 | |
| 94 | uint16_t GetIfaceId() { return iface_id_; } |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 95 | |
| 96 | std::string GetBridgeName() { return bridge_name_; } |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 97 | std::string GetIpAddr() { return ipaddr_; } |
| 98 | |
Paul Kirth | da77719 | 2020-07-17 19:41:22 +0000 | [diff] [blame] | 99 | 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 Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 109 | private: |
| 110 | static constexpr char kNetmask[] = "/24"; |
| 111 | uint16_t iface_id_; |
Alistair Delva | 57ccc65 | 2020-11-10 10:08:45 -0800 | [diff] [blame] | 112 | std::string bridge_name_; |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 113 | std::string ipaddr_; |
| 114 | bool has_ipv4_ = true; |
| 115 | bool has_ipv6_ = true; |
Paul Kirth | da77719 | 2020-07-17 19:41:22 +0000 | [diff] [blame] | 116 | bool use_ebtables_legacy_ = false; |
Paul Kirth | fa048be | 2020-07-14 03:51:38 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace cuttlefish |