Merge pull request #251 from morphis/f/static-network-config
Use a static IP configuration for Android instead of a full blown DHCP setup
This commit is contained in:
commit
5c1418b849
10 changed files with 535 additions and 96 deletions
|
|
@ -66,6 +66,7 @@ set(SOURCES
|
|||
anbox/build/version.h.in
|
||||
|
||||
anbox/android/intent.cpp
|
||||
anbox/android/ip_config_builder.cpp
|
||||
|
||||
anbox/common/fd.cpp
|
||||
anbox/common/fd_sets.h
|
||||
|
|
@ -79,6 +80,7 @@ set(SOURCES
|
|||
anbox/common/loop_device.cpp
|
||||
anbox/common/loop_device_allocator.cpp
|
||||
anbox/common/mount_entry.cpp
|
||||
anbox/common/binary_writer.cpp
|
||||
|
||||
anbox/testing/gtest_utils.h
|
||||
|
||||
|
|
|
|||
114
src/anbox/android/ip_config_builder.cpp
Normal file
114
src/anbox/android/ip_config_builder.cpp
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 3, as published
|
||||
* by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranties of
|
||||
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "anbox/android/ip_config_builder.h"
|
||||
|
||||
#include <boost/endian/buffers.hpp>
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace {
|
||||
constexpr const char *assignment_key{"ipAssignment"};
|
||||
constexpr const char *link_address_key{"linkAddress"};
|
||||
constexpr const char *gateway_key{"gateway"};
|
||||
constexpr const char *dns_key{"dns"};
|
||||
constexpr const char *id_key{"id"};
|
||||
constexpr const char *eos_key{"eos"};
|
||||
constexpr const char *assignment_static{"STATIC"};
|
||||
constexpr const char *assignment_dhcp{"DHCP"};
|
||||
constexpr const char *assignment_unknown{"UNKNOWN"};
|
||||
|
||||
constexpr const std::uint32_t is_default_gateway{0};
|
||||
constexpr const std::uint32_t gateway_is_present{1};
|
||||
|
||||
namespace aa = anbox::android;
|
||||
std::string assignment_to_string(const aa::IpConfigBuilder::Assignment &value) {
|
||||
switch (value) {
|
||||
case anbox::android::IpConfigBuilder::Assignment::Static:
|
||||
return assignment_static;
|
||||
break;
|
||||
case anbox::android::IpConfigBuilder::Assignment::DHCP:
|
||||
return assignment_dhcp;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return assignment_unknown;
|
||||
}
|
||||
}
|
||||
|
||||
namespace anbox {
|
||||
namespace android {
|
||||
std::size_t IpConfigBuilder::write(common::BinaryWriter &writer) {
|
||||
writer.set_byte_order(common::BinaryWriter::Order::Big);
|
||||
|
||||
// See http://androidxref.com/7.1.1_r6/xref/frameworks/base/services/core/java/com/android/server/net/IpConfigStore.java
|
||||
// for more details on the binary file format used here.
|
||||
|
||||
writer.write_uint32(static_cast<std::uint32_t>(version_));
|
||||
|
||||
writer.write_string_with_size(assignment_key);
|
||||
writer.write_string_with_size(assignment_to_string(assignment_));
|
||||
|
||||
writer.write_string_with_size(link_address_key);
|
||||
writer.write_string_with_size(link_.address);
|
||||
writer.write_uint32(link_.prefix_length);
|
||||
|
||||
writer.write_string_with_size(gateway_key);
|
||||
writer.write_uint32(is_default_gateway);
|
||||
writer.write_uint32(gateway_is_present);
|
||||
writer.write_string_with_size(gateway_);
|
||||
|
||||
writer.write_string_with_size(dns_key);
|
||||
for (const auto &server : dns_servers_)
|
||||
writer.write_string_with_size(server);
|
||||
|
||||
writer.write_string_with_size(id_key);
|
||||
writer.write_uint32(id_);
|
||||
|
||||
writer.write_string_with_size(eos_key);
|
||||
|
||||
return writer.bytes_written();
|
||||
}
|
||||
|
||||
void IpConfigBuilder::set_version(const Version &version) {
|
||||
version_ = version;
|
||||
}
|
||||
|
||||
void IpConfigBuilder::set_assignment(const Assignment &assignment) {
|
||||
assignment_ = assignment;
|
||||
}
|
||||
|
||||
void IpConfigBuilder::set_link_address(const std::string &address, uint32_t prefix_length) {
|
||||
link_.address = address;
|
||||
link_.prefix_length = prefix_length;
|
||||
}
|
||||
|
||||
void IpConfigBuilder::set_gateway(const std::string &gateway) {
|
||||
gateway_ = gateway;
|
||||
}
|
||||
|
||||
void IpConfigBuilder::set_dns_servers(const std::vector<std::string> &dns_servers) {
|
||||
dns_servers_ = dns_servers;
|
||||
}
|
||||
|
||||
void IpConfigBuilder::set_id(uint32_t id) {
|
||||
id_ = id;
|
||||
}
|
||||
} // namespace android
|
||||
} // namespace anbox
|
||||
66
src/anbox/android/ip_config_builder.h
Normal file
66
src/anbox/android/ip_config_builder.h
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 3, as published
|
||||
* by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranties of
|
||||
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ANBOX_ANDROID_IPCONFIGBUILDER_H_
|
||||
#define ANBOX_ANDROID_IPCONFIGBUILDER_H_
|
||||
|
||||
#include "anbox/common/binary_writer.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
namespace anbox {
|
||||
namespace android {
|
||||
class IpConfigBuilder {
|
||||
public:
|
||||
enum class Version : std::uint32_t {
|
||||
Version1 = 1,
|
||||
Version2 = 2,
|
||||
};
|
||||
|
||||
enum class Assignment {
|
||||
Static,
|
||||
DHCP,
|
||||
};
|
||||
|
||||
IpConfigBuilder() = default;
|
||||
|
||||
std::size_t write(common::BinaryWriter &writer);
|
||||
|
||||
void set_version(const Version &version);
|
||||
void set_assignment(const Assignment &assignment);
|
||||
void set_link_address(const std::string &address, std::uint32_t prefix_length);
|
||||
void set_gateway(const std::string &gateway);
|
||||
void set_dns_servers(const std::vector<std::string> &dns_servers);
|
||||
void set_id(std::uint32_t id);
|
||||
|
||||
private:
|
||||
Version version_;
|
||||
Assignment assignment_;
|
||||
struct {
|
||||
std::string address;
|
||||
std::uint32_t prefix_length;
|
||||
} link_;
|
||||
std::string gateway_;
|
||||
std::vector<std::string> dns_servers_;
|
||||
std::uint32_t id_;
|
||||
};
|
||||
} // namespace android
|
||||
} // namespace anbox
|
||||
|
||||
#endif
|
||||
106
src/anbox/common/binary_writer.cpp
Normal file
106
src/anbox/common/binary_writer.cpp
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright (C) 2016 Thomas Voss <thomas.voss.bochum@gmail.com>
|
||||
* Simon Fels <morphis@gravedo.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 3, as published
|
||||
* by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranties of
|
||||
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "anbox/common/binary_writer.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/endian/buffers.hpp>
|
||||
|
||||
namespace {
|
||||
bool is_little_endian() {
|
||||
static const std::uint32_t v = 1;
|
||||
return (*reinterpret_cast<const std::uint8_t*>(&v) == 1);
|
||||
}
|
||||
}
|
||||
|
||||
namespace anbox {
|
||||
namespace common {
|
||||
|
||||
BinaryWriter::BinaryWriter(std::vector<std::uint8_t>::iterator begin,
|
||||
std::vector<std::uint8_t>::iterator end) :
|
||||
begin_{begin}, current_{begin}, end_{end},
|
||||
byte_order_{is_little_endian() ? Order::Little : Order::Big} {}
|
||||
|
||||
void BinaryWriter::set_byte_order(Order order) {
|
||||
byte_order_ = order;
|
||||
}
|
||||
|
||||
void BinaryWriter::write_uint16(std::uint16_t value) {
|
||||
if (current_ + sizeof(value) > end_)
|
||||
throw std::out_of_range{"Write buffer exhausted"};
|
||||
|
||||
std::uint16_t v = value;
|
||||
switch (byte_order_) {
|
||||
case Order::Big:
|
||||
v = boost::endian::native_to_big(value);
|
||||
break;
|
||||
case Order::Little:
|
||||
v = boost::endian::native_to_little(value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
*reinterpret_cast<std::uint16_t*>(&(*current_)) = v;
|
||||
current_ += sizeof(v);
|
||||
}
|
||||
|
||||
void BinaryWriter::write_uint32(std::uint32_t value) {
|
||||
if (current_ + sizeof(value) > end_)
|
||||
throw std::out_of_range{"Write buffer exhausted"};
|
||||
|
||||
std::uint32_t v = value;
|
||||
switch (byte_order_) {
|
||||
case Order::Big:
|
||||
v = boost::endian::native_to_big(value);
|
||||
break;
|
||||
case Order::Little:
|
||||
v = boost::endian::native_to_little(value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
*reinterpret_cast<std::uint32_t*>(&(*current_)) = v;
|
||||
current_ += sizeof(v);
|
||||
}
|
||||
|
||||
void BinaryWriter::write_string(const char *s, std::size_t size) {
|
||||
if (current_ + size > end_)
|
||||
throw std::out_of_range{"Write buffer exhausted"};
|
||||
|
||||
memcpy(&(*current_), s, size);
|
||||
current_ += size;
|
||||
}
|
||||
|
||||
void BinaryWriter::write_string_with_size(const std::string &str) {
|
||||
write_string_with_size(str.c_str(), str.length());
|
||||
}
|
||||
|
||||
void BinaryWriter::write_string_with_size(const char *s, std::size_t size) {
|
||||
write_uint16(size);
|
||||
write_string(s, size);
|
||||
}
|
||||
|
||||
std::size_t BinaryWriter::bytes_written() const {
|
||||
return current_ - begin_;
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace anbox
|
||||
57
src/anbox/common/binary_writer.h
Normal file
57
src/anbox/common/binary_writer.h
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (C) 2016 Thomas Voss <thomas.voss.bochum@gmail.com>
|
||||
* Simon Fels <morphis@gravedo.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 3, as published
|
||||
* by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranties of
|
||||
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ANBOX_COMMON_BINARY_WRITER_H_
|
||||
#define ANBOX_COMMON_BINARY_WRITER_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace anbox {
|
||||
namespace common {
|
||||
class BinaryWriter {
|
||||
public:
|
||||
enum class Order {
|
||||
Big,
|
||||
Little,
|
||||
};
|
||||
|
||||
explicit BinaryWriter(std::vector<std::uint8_t>::iterator begin_,
|
||||
std::vector<std::uint8_t>::iterator end_);
|
||||
|
||||
void set_byte_order(Order order);
|
||||
|
||||
void write_uint16(std::uint16_t value);
|
||||
void write_uint32(std::uint32_t value);
|
||||
void write_string(const char *s, std::size_t size);
|
||||
void write_string_with_size(const std::string &str);
|
||||
void write_string_with_size(const char *s, std::size_t size);
|
||||
|
||||
std::size_t bytes_written() const;
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>::iterator begin_;
|
||||
std::vector<std::uint8_t>::iterator current_;
|
||||
std::vector<std::uint8_t>::iterator end_;
|
||||
Order byte_order_;
|
||||
};
|
||||
} // namespace common
|
||||
} // namespace anbox
|
||||
|
||||
#endif
|
||||
|
|
@ -15,6 +15,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include "anbox/android/ip_config_builder.h"
|
||||
#include "anbox/container/lxc_container.h"
|
||||
#include "anbox/config.h"
|
||||
#include "anbox/logger.h"
|
||||
|
|
@ -22,6 +23,7 @@
|
|||
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
|
@ -33,6 +35,13 @@
|
|||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
namespace {
|
||||
constexpr const char *default_container_ip_address{"192.168.250.2"};
|
||||
constexpr const std::uint32_t default_container_ip_prefix_length{24};
|
||||
constexpr const char *default_host_ip_address{"192.168.250.1"};
|
||||
constexpr const char *default_dns_server{"8.8.8.8"};
|
||||
}
|
||||
|
||||
namespace anbox {
|
||||
namespace container {
|
||||
LxcContainer::LxcContainer(bool privileged, const network::Credentials &creds)
|
||||
|
|
@ -76,6 +85,50 @@ void LxcContainer::setup_id_maps() {
|
|||
max_id - creds_.gid() - 1));
|
||||
}
|
||||
|
||||
void LxcContainer::setup_network() {
|
||||
if (!fs::exists("/sys/class/net/anbox0")) {
|
||||
WARNING("Anbox bridge interface 'anbox0' doesn't exist. Network functionality will not be available");
|
||||
return;
|
||||
}
|
||||
|
||||
set_config_item("lxc.network.type", "veth");
|
||||
set_config_item("lxc.network.flags", "up");
|
||||
set_config_item("lxc.network.link", "anbox0");
|
||||
|
||||
// Instead of relying on DHCP we will give Android a static IP configuration
|
||||
// for the virtual ethernet interface LXC creates for us. This will be bridged
|
||||
// to the host and will allows us to have reliable network connectivity and
|
||||
// not depend on any other system service.
|
||||
|
||||
android::IpConfigBuilder ip_conf;
|
||||
ip_conf.set_version(android::IpConfigBuilder::Version::Version2);
|
||||
ip_conf.set_assignment(android::IpConfigBuilder::Assignment::Static);
|
||||
ip_conf.set_link_address(default_container_ip_address, default_container_ip_prefix_length);
|
||||
ip_conf.set_gateway(default_host_ip_address);
|
||||
ip_conf.set_dns_servers({default_dns_server});
|
||||
ip_conf.set_id(0);
|
||||
|
||||
std::vector<std::uint8_t> buffer(512);
|
||||
common::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
const auto size = ip_conf.write(writer);
|
||||
|
||||
const auto ip_conf_dir = SystemConfiguration::instance().data_dir() / "data" / "misc" / "ethernet";
|
||||
if (!fs::exists(ip_conf_dir))
|
||||
fs::create_directories(ip_conf_dir);
|
||||
|
||||
const auto ip_conf_path = ip_conf_dir / "ipconfig.txt";
|
||||
if (fs::exists(ip_conf_path))
|
||||
fs::remove(ip_conf_path);
|
||||
|
||||
std::ofstream f(ip_conf_path.string(), std::ofstream::binary);
|
||||
if (f.is_open()) {
|
||||
f.write(reinterpret_cast<const char*>(buffer.data()), size);
|
||||
f.close();
|
||||
} else {
|
||||
ERROR("Failed to write IP configuration. Network functionality will not be available.");
|
||||
}
|
||||
}
|
||||
|
||||
void LxcContainer::start(const Configuration &configuration) {
|
||||
if (getuid() != 0)
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error("You have to start the container as root"));
|
||||
|
|
@ -131,11 +184,7 @@ void LxcContainer::start(const Configuration &configuration) {
|
|||
const auto log_path = SystemConfiguration::instance().log_dir();
|
||||
set_config_item("lxc.logfile", utils::string_format("%s/container.log", log_path).c_str());
|
||||
|
||||
if (fs::exists("/sys/class/net/anboxbr0")) {
|
||||
set_config_item("lxc.network.type", "veth");
|
||||
set_config_item("lxc.network.flags", "up");
|
||||
set_config_item("lxc.network.link", "anboxbr0");
|
||||
}
|
||||
setup_network();
|
||||
|
||||
#if 0
|
||||
// Android uses namespaces as well so we have to allow nested namespaces for LXC
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ class LxcContainer : public Container {
|
|||
private:
|
||||
void set_config_item(const std::string &key, const std::string &value);
|
||||
void setup_id_maps();
|
||||
void setup_network();
|
||||
|
||||
State state_;
|
||||
lxc_container *container_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue