Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
anbox
提交
5bc93f76
A
anbox
项目概览
openeuler
/
anbox
通知
24
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
anbox
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5bc93f76
编写于
5月 10, 2017
作者:
S
Simon Fels
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Create static IP configuration for Android
上级
c5f1a272
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
223 addition
and
5 deletion
+223
-5
src/CMakeLists.txt
src/CMakeLists.txt
+1
-0
src/anbox/android/ip_config_builder.cpp
src/anbox/android/ip_config_builder.cpp
+108
-0
src/anbox/android/ip_config_builder.h
src/anbox/android/ip_config_builder.h
+63
-0
src/anbox/container/lxc_container.cpp
src/anbox/container/lxc_container.cpp
+50
-5
src/anbox/container/lxc_container.h
src/anbox/container/lxc_container.h
+1
-0
未找到文件。
src/CMakeLists.txt
浏览文件 @
5bc93f76
...
...
@@ -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
...
...
src/anbox/android/ip_config_builder.cpp
0 → 100644
浏览文件 @
5bc93f76
/*
* 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"
};
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
);
writer
.
write_unsigned_long
(
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_unsigned_long
(
link_
.
prefix_length
);
writer
.
write_string_with_size
(
gateway_key
);
writer
.
write_unsigned_long
(
0
);
writer
.
write_unsigned_long
(
1
);
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_unsigned_long
(
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
src/anbox/android/ip_config_builder.h
0 → 100644
浏览文件 @
5bc93f76
/*
* 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
{
struct
IpConfigBuilder
{
enum
class
Version
:
std
::
uint32_t
{
Version1
=
1
,
Version2
=
2
,
};
enum
class
Assignment
{
Static
,
DHCP
,
};
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
src/anbox/container/lxc_container.cpp
浏览文件 @
5bc93f76
...
...
@@ -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>
...
...
@@ -76,6 +78,53 @@ 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.
//
// See http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/net/IpConfiguration.java
// for more details of the IP configuration format used here.
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
(
"192.168.250.2"
,
24
);
ip_conf
.
set_gateway
(
"192.168.250.1"
);
ip_conf
.
set_dns_servers
({
"8.8.8.8"
});
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 +180,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
...
...
src/anbox/container/lxc_container.h
浏览文件 @
5bc93f76
...
...
@@ -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_
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录