Android-cuttlefish cvd tool
alloc_utils.h
Go to the documentation of this file.
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 <pwd.h>
20#include <stdint.h>
21#include <sys/wait.h>
22#include <unistd.h>
23
24#include <optional>
25#include <string>
26#include <string_view>
27
29
30namespace cuttlefish {
31
32// Wireless network prefix
33inline constexpr char kWirelessIp[] = "192.168.96";
34// Mobile network prefix
35inline constexpr char kMobileIp[] = "192.168.97";
36// Ethernet network prefix
37inline constexpr char kEthernetIp[] = "192.168.98";
38// permission bits for socket
39inline constexpr int kSocketMode = 0666;
40
41// Max ID an interface can have
42// Note: Interface names only have 2 digits in addition to the username prefix
43// Additionally limited by available netmask values in MobileNetworkName
44// Exceeding 63 would result in an overflow when calculating the netmask
45inline constexpr uint32_t kMaxIfaceNameId = 63;
46
47// struct for managing configuration state
49 bool has_gateway = false;
50 bool has_dnsmasq = false;
51 bool has_iptable = false;
52};
53
54int RunExternalCommand(const std::string& command);
57std::optional<std::string> GetUserName(uid_t uid);
58
59bool CreateTap(std::string_view name);
60
61#ifdef __linux__
62Result<void> ValidateTapInterfaceIsUsable(const std::string& interface_name);
63#endif
64
65bool DestroyIface(std::string_view name);
66
67bool DestroyBridge(std::string_view name);
68
69bool CreateMobileIface(std::string_view name, uint16_t id,
70 std::string_view ipaddr);
71bool DestroyMobileIface(std::string_view name, uint16_t id,
72 std::string_view ipaddr);
73
74bool CreateEthernetIface(std::string_view name, std::string_view bridge_name);
75bool DestroyEthernetIface(std::string_view name);
76void CleanupEthernetIface(std::string_view name);
77
78bool SetupBridgeGateway(std::string_view name, std::string_view ipaddr);
79void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr,
80 const GatewayConfig& config);
81
82bool CreateEthernetBridgeIface(std::string_view name, std::string_view ipaddr);
83bool DestroyEthernetBridgeIface(std::string_view name, std::string_view ipaddr);
84
85bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway,
86 std::string_view dhcp_range);
87bool StopDnsmasq(std::string_view name);
88
89} // namespace cuttlefish
char * name
Definition: libf2fs.c:1403
Definition: alloc_driver.h:20
std::optional< std::string > GetUserName(uid_t uid)
Definition: alloc_utils.cpp:214
constexpr char kWirelessIp[]
Definition: alloc_utils.h:33
bool DestroyIface(std::string_view name)
Definition: alloc_utils.cpp:198
bool CreateEthernetIface(std::string_view name, std::string_view bridge_name)
Definition: alloc_utils.cpp:71
bool SetupBridgeGateway(std::string_view bridge_name, std::string_view ipaddr)
Definition: alloc_utils.cpp:237
bool DestroyMobileIface(std::string_view name, uint16_t id, std::string_view ipaddr)
Definition: alloc_utils.cpp:132
constexpr int kSocketMode
Definition: alloc_utils.h:39
void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr, const GatewayConfig &config)
Definition: alloc_utils.cpp:271
bool CreateEthernetBridgeIface(std::string_view name, std::string_view ipaddr)
Definition: alloc_utils.cpp:348
bool CreateMobileIface(std::string_view name, uint16_t id, std::string_view ipaddr)
Definition: alloc_utils.cpp:99
Result< std::string > DnsmasqPath()
Definition: alloc_utils.cpp:386
int RunExternalCommand(const std::string &command)
constexpr char kMobileIp[]
Definition: alloc_utils.h:35
constexpr char kEthernetIp[]
Definition: alloc_utils.h:37
Result< std::string > IptablesPath()
Definition: alloc_utils.cpp:378
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
bool CreateTap(std::string_view name)
Definition: alloc_utils.cpp:156
bool DestroyEthernetIface(std::string_view name)
Definition: alloc_utils.cpp:152
bool DestroyBridge(std::string_view name)
Definition: alloc_utils.cpp:223
void CleanupEthernetIface(std::string_view name)
Definition: alloc_utils.cpp:154
bool DestroyEthernetBridgeIface(std::string_view name, std::string_view ipaddr)
Definition: alloc_utils.cpp:367
constexpr uint32_t kMaxIfaceNameId
Definition: alloc_utils.h:45
bool StopDnsmasq(std::string_view name)
Definition: alloc_utils.cpp:322
bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway, std::string_view dhcp_range)
Definition: alloc_utils.cpp:294
Definition: alloc_utils.h:48
bool has_gateway
Definition: alloc_utils.h:49
bool has_dnsmasq
Definition: alloc_utils.h:50
bool has_iptable
Definition: alloc_utils.h:51