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
|
|
@ -14,34 +14,21 @@
|
|||
# limitations under the License.
|
||||
|
||||
# Taken from https://github.com/lxc/lxd-pkg-ubuntu/blob/dpm-xenial/lxd-bridge/lxd-bridge
|
||||
# but modified for the use within anbox.
|
||||
|
||||
varrun="/run/anbox"
|
||||
varlib="/var/lib/anbox"
|
||||
|
||||
BRIDGE="anboxbr0"
|
||||
BRIDGE="anbox0"
|
||||
|
||||
# IPv4
|
||||
IPV4_ADDR="10.0.6.1"
|
||||
IPV4_ADDR="192.168.250.1"
|
||||
IPV4_NETMASK="255.255.255.0"
|
||||
IPV4_NETWORK="10.0.6.1/24"
|
||||
IPV4_DHCP_RANGE="10.0.6.2,10.0.6.254"
|
||||
IPV4_DHCP_MAX="252"
|
||||
IPV4_NETWORK="192.168.250.1/24"
|
||||
IPV4_NAT="true"
|
||||
|
||||
# IPv6
|
||||
IPV6_ADDR="fd9d:e4dc:4e00:9e98::1"
|
||||
IPV6_MASK="64"
|
||||
IPV6_NETWORK="fd9d:e4dc:4e00:9e98::1/64"
|
||||
IPV6_NAT="true"
|
||||
IPV6_PROXY="false"
|
||||
|
||||
use_iptables_lock="-w"
|
||||
iptables -w -L -n > /dev/null 2>&1 || use_iptables_lock=""
|
||||
|
||||
HAS_IPV6=false
|
||||
[ -e "/proc/sys/net/ipv6/conf/default/disable_ipv6" ] && \
|
||||
[ "$(cat /proc/sys/net/ipv6/conf/default/disable_ipv6)" = "0" ] && HAS_IPV6=true
|
||||
|
||||
_netmask2cidr () {
|
||||
# Assumes there's no "255." after a non-255 byte in the mask
|
||||
local x=${1##*255.}
|
||||
|
|
@ -88,11 +75,6 @@ start() {
|
|||
# set up the anbox network
|
||||
[ ! -d "/sys/class/net/${BRIDGE}" ] && ip link add dev "${BRIDGE}" type bridge
|
||||
|
||||
if [ "${HAS_IPV6}" = "true" ]; then
|
||||
echo 0 > "/proc/sys/net/ipv6/conf/${BRIDGE}/autoconf" || true
|
||||
echo 0 > "/proc/sys/net/ipv6/conf/${BRIDGE}/accept_dad" || true
|
||||
fi
|
||||
|
||||
# if we are run from systemd on a system with selinux enabled,
|
||||
# the mkdir will create /run/anbox as init_var_run_t which dnsmasq
|
||||
# can't write its pid into, so we restorecon it (to var_run_t)
|
||||
|
|
@ -103,13 +85,6 @@ start() {
|
|||
fi
|
||||
fi
|
||||
|
||||
if [ ! -d "${varlib}" ]; then
|
||||
mkdir -p "${varlib}"
|
||||
if which restorecon >/dev/null 2>&1; then
|
||||
restorecon "${varlib}"
|
||||
fi
|
||||
fi
|
||||
|
||||
ifup "${BRIDGE}" "${IPV4_ADDR}" "${IPV4_NETMASK}"
|
||||
|
||||
IPV4_ARG=""
|
||||
|
|
@ -118,25 +93,6 @@ start() {
|
|||
if [ "${IPV4_NAT}" = "true" ]; then
|
||||
iptables "${use_iptables_lock}" -t nat -A POSTROUTING -s "${IPV4_NETWORK}" ! -d "${IPV4_NETWORK}" -j MASQUERADE -m comment --comment "managed by anbox-bridge"
|
||||
fi
|
||||
IPV4_ARG="--listen-address ${IPV4_ADDR} --dhcp-range ${IPV4_DHCP_RANGE} --dhcp-lease-max=${IPV4_DHCP_MAX}"
|
||||
fi
|
||||
|
||||
IPV6_ARG=""
|
||||
if [ "${HAS_IPV6}" = "true" ] && [ -n "${IPV6_ADDR}" ] && [ -n "${IPV6_MASK}" ] && [ -n "${IPV6_NETWORK}" ]; then
|
||||
# IPv6 sysctls don't respect the "all" path...
|
||||
for interface in /proc/sys/net/ipv6/conf/*; do
|
||||
echo 2 > "${interface}/accept_ra"
|
||||
done
|
||||
|
||||
for interface in /proc/sys/net/ipv6/conf/*; do
|
||||
echo 1 > "${interface}/forwarding"
|
||||
done
|
||||
|
||||
ip -6 addr add dev "${BRIDGE}" "${IPV6_ADDR}/${IPV6_MASK}"
|
||||
if [ "${IPV6_NAT}" = "true" ]; then
|
||||
ip6tables "${use_iptables_lock}" -t nat -A POSTROUTING -s "${IPV6_NETWORK}" ! -d "${IPV6_NETWORK}" -j MASQUERADE -m comment --comment "managed by anbox-bridge"
|
||||
fi
|
||||
IPV6_ARG="--dhcp-range=${IPV6_ADDR},ra-stateless,ra-names --listen-address ${IPV6_ADDR}"
|
||||
fi
|
||||
|
||||
iptables "${use_iptables_lock}" -I INPUT -i "${BRIDGE}" -p udp --dport 67 -j ACCEPT -m comment --comment "managed by anbox-bridge"
|
||||
|
|
@ -147,35 +103,6 @@ start() {
|
|||
iptables "${use_iptables_lock}" -I FORWARD -o "${BRIDGE}" -j ACCEPT -m comment --comment "managed by anbox-bridge"
|
||||
iptables "${use_iptables_lock}" -t mangle -A POSTROUTING -o "${BRIDGE}" -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill -m comment --comment "managed by anbox-bridge"
|
||||
|
||||
DOMAIN_ARG=""
|
||||
if [ -n "${DOMAIN}" ]; then
|
||||
DOMAIN_ARG="-s ${DOMAIN} -S /${DOMAIN}/"
|
||||
fi
|
||||
|
||||
CONFILE_ARG=""
|
||||
if [ -n "${CONFILE}" ]; then
|
||||
CONFILE_ARG="--conf-file=${CONFILE}"
|
||||
fi
|
||||
|
||||
# https://lists.linuxcontainers.org/pipermail/lxc-devel/2014-October/010561.html
|
||||
for DNSMASQ_USER in anbox dnsmasq nobody
|
||||
do
|
||||
if getent passwd "${DNSMASQ_USER}" >/dev/null; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -n "${IPV4_ADDR}" ] || [ -n "${IPV6_ADDR}" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
dnsmasq ${CONFILE_ARG} ${DOMAIN_ARG} -u "${DNSMASQ_USER}" --strict-order --bind-interfaces --pid-file="${varrun}/dnsmasq.pid" --dhcp-no-override --except-interface=lo --interface="${BRIDGE}" --dhcp-leasefile="${varlib}/dnsmasq.${BRIDGE}.leases" --dhcp-authoritative ${IPV4_ARG} ${IPV6_ARG} || cleanup
|
||||
fi
|
||||
|
||||
if [ "${HAS_IPV6}" = "true" ] && [ "${IPV6_PROXY}" = "true" ]; then
|
||||
PATH="${PATH}:$(dirname "${0}")" anbox-bridge-proxy --addr="[fe80::1%${BRIDGE}]:13128" &
|
||||
PID=$!
|
||||
echo "${PID}" > "${varrun}/proxy.pid"
|
||||
fi
|
||||
|
||||
touch "${varrun}/network_up"
|
||||
FAILED=0
|
||||
}
|
||||
|
|
@ -197,20 +124,6 @@ stop() {
|
|||
iptables ${use_iptables_lock} -t nat -D POSTROUTING -s ${IPV4_NETWORK} ! -d ${IPV4_NETWORK} -j MASQUERADE -m comment --comment "managed by anbox-bridge"
|
||||
fi
|
||||
|
||||
if [ "${HAS_IPV6}" = "true" ] && [ -n "${IPV6_NETWORK}" ] && [ "${IPV6_NAT}" = "true" ]; then
|
||||
ip6tables ${use_iptables_lock} -t nat -D POSTROUTING -s ${IPV6_NETWORK} ! -d ${IPV6_NETWORK} -j MASQUERADE -m comment --comment "managed by anbox-bridge"
|
||||
fi
|
||||
|
||||
if [ -e "${varrun}/dnsmasq.pid" ]; then
|
||||
pid=$(cat "${varrun}/dnsmasq.pid" 2>/dev/null) && kill -9 "${pid}"
|
||||
rm -f "${varrun}/dnsmasq.pid"
|
||||
fi
|
||||
|
||||
if [ -e "${varrun}/proxy.pid" ]; then
|
||||
pid=$(cat "${varrun}/proxy.pid" 2>/dev/null) && kill -9 "${pid}"
|
||||
rm -f "${varrun}/proxy.pid"
|
||||
fi
|
||||
|
||||
# if ${BRIDGE} has attached interfaces, don't destroy the bridge
|
||||
ls /sys/class/net/${BRIDGE}/brif/* > /dev/null 2>&1 || ip link delete "${BRIDGE}"
|
||||
fi
|
||||
|
|
|
|||
|
|
@ -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_;
|
||||
|
|
|
|||
|
|
@ -2,3 +2,4 @@ ANBOX_ADD_TEST(message_channel_tests message_channel_tests.cpp)
|
|||
ANBOX_ADD_TEST(small_vector_tests small_vector_tests.cpp)
|
||||
ANBOX_ADD_TEST(type_traits_tests type_traits_tests.cpp)
|
||||
ANBOX_ADD_TEST(scope_ptr_tests scope_ptr_tests.cpp)
|
||||
ANBOX_ADD_TEST(binary_writer_tests binary_writer_tests.cpp)
|
||||
|
|
|
|||
130
tests/anbox/common/binary_writer_tests.cpp
Normal file
130
tests/anbox/common/binary_writer_tests.cpp
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* 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/common/binary_writer.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace ac = anbox::common;
|
||||
|
||||
using namespace ::testing;
|
||||
|
||||
TEST(BinaryWriter, WriteUint32) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
buffer.resize(sizeof(std::uint32_t) * 2);
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
|
||||
writer.write_uint32(0x10);
|
||||
writer.write_uint32(0x3322);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 8);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x10, 0x00, 0x00, 0x00, 0x22, 0x33, 0x00, 0x00));
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteUint32FailsWithExhaustedError) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
EXPECT_THROW(writer.write_uint32(0x11), std::out_of_range);
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteUint32WithChangedBinaryOrder) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
buffer.resize(sizeof(std::uint32_t));
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
|
||||
writer.set_byte_order(ac::BinaryWriter::Order::Big);
|
||||
writer.write_uint32(0x11223344);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 4);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x11, 0x22, 0x33, 0x44));
|
||||
|
||||
buffer.clear();
|
||||
buffer.resize(sizeof(std::uint32_t));
|
||||
|
||||
writer = ac::BinaryWriter(buffer.begin(), buffer.end());
|
||||
|
||||
writer.set_byte_order(ac::BinaryWriter::Order::Little);
|
||||
writer.write_uint32(0x11223344);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 4);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x44, 0x33, 0x22, 0x11));
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteUint16) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
buffer.resize(sizeof(std::uint16_t) * 2);
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
|
||||
writer.write_uint16(0x10);
|
||||
writer.write_uint16(0x3322);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 4);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x10, 0x00, 0x22, 0x33));
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteUint16FailsWithExhaustedError) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
EXPECT_THROW(writer.write_uint16(0x11), std::out_of_range);
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteUint16WithChangedBinaryOrder) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
buffer.resize(sizeof(std::uint16_t));
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
|
||||
writer.set_byte_order(ac::BinaryWriter::Order::Big);
|
||||
writer.write_uint16(0x1122);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 2);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x11, 0x22));
|
||||
|
||||
buffer.clear();
|
||||
buffer.resize(sizeof(std::uint16_t));
|
||||
|
||||
writer = ac::BinaryWriter(buffer.begin(), buffer.end());
|
||||
|
||||
writer.set_byte_order(ac::BinaryWriter::Order::Little);
|
||||
writer.write_uint16(0x1122);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 2);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x22, 0x11));
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteString) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
buffer.resize(sizeof(std::uint8_t) * 4);
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
|
||||
writer.write_string("test", 4);
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 4);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x74, 0x65, 0x73, 0x74));
|
||||
}
|
||||
|
||||
TEST(BinaryWriter, WriteStringWithSize) {
|
||||
std::vector<std::uint8_t> buffer;
|
||||
buffer.resize(sizeof(std::uint8_t) * 6);
|
||||
ac::BinaryWriter writer(buffer.begin(), buffer.end());
|
||||
|
||||
writer.set_byte_order(ac::BinaryWriter::Order::Big);
|
||||
|
||||
writer.write_string_with_size("test");
|
||||
|
||||
ASSERT_EQ(writer.bytes_written(), 6);
|
||||
ASSERT_THAT(buffer, ElementsAre(0x00, 0x04, 0x74, 0x65, 0x73, 0x74));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue