2 * Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "anbox/android/ip_config_builder.h"
20 #include <boost/endian/buffers.hpp>
26 constexpr const char *assignment_key{"ipAssignment"};
27 constexpr const char *link_address_key{"linkAddress"};
28 constexpr const char *gateway_key{"gateway"};
29 constexpr const char *dns_key{"dns"};
30 constexpr const char *id_key{"id"};
31 constexpr const char *eos_key{"eos"};
32 constexpr const char *assignment_static{"STATIC"};
33 constexpr const char *assignment_dhcp{"DHCP"};
34 constexpr const char *assignment_unknown{"UNKNOWN"};
36 constexpr const std::uint32_t is_default_gateway{0};
37 constexpr const std::uint32_t gateway_is_present{1};
39 namespace aa = anbox::android;
40 std::string assignment_to_string(const aa::IpConfigBuilder::Assignment &value) {
42 case anbox::android::IpConfigBuilder::Assignment::Static:
43 return assignment_static;
45 case anbox::android::IpConfigBuilder::Assignment::DHCP:
46 return assignment_dhcp;
51 return assignment_unknown;
57 std::size_t IpConfigBuilder::write(common::BinaryWriter &writer) {
58 writer.set_byte_order(common::BinaryWriter::Order::Big);
60 // See http://androidxref.com/7.1.1_r6/xref/frameworks/base/services/core/java/com/android/server/net/IpConfigStore.java
61 // for more details on the binary file format used here.
63 writer.write_uint32(static_cast<std::uint32_t>(version_));
65 writer.write_string_with_size(assignment_key);
66 writer.write_string_with_size(assignment_to_string(assignment_));
68 writer.write_string_with_size(link_address_key);
69 writer.write_string_with_size(link_.address);
70 writer.write_uint32(link_.prefix_length);
72 writer.write_string_with_size(gateway_key);
73 writer.write_uint32(is_default_gateway);
74 writer.write_uint32(gateway_is_present);
75 writer.write_string_with_size(gateway_);
77 writer.write_string_with_size(dns_key);
78 for (const auto &server : dns_servers_)
79 writer.write_string_with_size(server);
81 writer.write_string_with_size(id_key);
82 writer.write_uint32(id_);
84 writer.write_string_with_size(eos_key);
86 return writer.bytes_written();
89 void IpConfigBuilder::set_version(const Version &version) {
93 void IpConfigBuilder::set_assignment(const Assignment &assignment) {
94 assignment_ = assignment;
97 void IpConfigBuilder::set_link_address(const std::string &address, uint32_t prefix_length) {
98 link_.address = address;
99 link_.prefix_length = prefix_length;
102 void IpConfigBuilder::set_gateway(const std::string &gateway) {
106 void IpConfigBuilder::set_dns_servers(const std::vector<std::string> &dns_servers) {
107 dns_servers_ = dns_servers;
110 void IpConfigBuilder::set_id(uint32_t id) {
113 } // namespace android